summaryrefslogtreecommitdiff
path: root/installed-tests/js/testGObjectInterface.js
blob: 40f5eca829a18455a4b9c97d3c1df39dbe3cb393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2015 Endless Mobile, Inc.

const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;

const AGObjectInterface = GObject.registerClass({
    GTypeName: 'ArbitraryGTypeName',
    Requires: [GObject.Object],
    Properties: {
        'interface-prop': GObject.ParamSpec.string('interface-prop',
            'Interface property', 'Must be overridden in implementation',
            GObject.ParamFlags.READABLE,
            'foobar'),
    },
    Signals: {
        'interface-signal': {},
    },
}, class AGObjectInterface extends GObject.Interface {
    requiredG() {
        throw new GObject.NotImplementedError();
    }

    optionalG() {
        return 'AGObjectInterface.optionalG()';
    }
});

const InterfaceRequiringGObjectInterface = GObject.registerClass({
    Requires: [AGObjectInterface],
}, class InterfaceRequiringGObjectInterface extends GObject.Interface {
    optionalG() {
        return `InterfaceRequiringGObjectInterface.optionalG()\n${
            AGObjectInterface.optionalG(this)}`;
    }
});

const GObjectImplementingGObjectInterface = GObject.registerClass({
    Implements: [AGObjectInterface],
    Properties: {
        'interface-prop': GObject.ParamSpec.override('interface-prop',
            AGObjectInterface),
        'class-prop': GObject.ParamSpec.string('class-prop', 'Class property',
            'A property that is not on the interface',
            GObject.ParamFlags.READABLE, 'meh'),
    },
    Signals: {
        'class-signal': {},
    },
}, class GObjectImplementingGObjectInterface extends GObject.Object {
    get interface_prop() {
        return 'foobar';
    }

    get class_prop() {
        return 'meh';
    }

    requiredG() {}

    optionalG() {
        return AGObjectInterface.optionalG(this);
    }
});

const MinimalImplementationOfAGObjectInterface = GObject.registerClass({
    Implements: [AGObjectInterface],
    Properties: {
        'interface-prop': GObject.ParamSpec.override('interface-prop',
            AGObjectInterface),
    },
}, class MinimalImplementationOfAGObjectInterface extends GObject.Object {
    requiredG() {}
});

const ImplementationOfTwoInterfaces = GObject.registerClass({
    Implements: [AGObjectInterface, InterfaceRequiringGObjectInterface],
    Properties: {
        'interface-prop': GObject.ParamSpec.override('interface-prop',
            AGObjectInterface),
    },
}, class ImplementationOfTwoInterfaces extends GObject.Object {
    requiredG() {}

    optionalG() {
        return InterfaceRequiringGObjectInterface.optionalG(this);
    }
});

const ImplementationOfIntrospectedInterface = GObject.registerClass({
    Implements: [Gio.Action],
    Properties: {
        'enabled': GObject.ParamSpec.override('enabled', Gio.Action),
        'name': GObject.ParamSpec.override('name', Gio.Action),
        'state': GObject.ParamSpec.override('state', Gio.Action),
        'state-type': GObject.ParamSpec.override('state-type', Gio.Action),
        'parameter-type': GObject.ParamSpec.override('parameter-type',
            Gio.Action),
    },
}, class ImplementationOfIntrospectedInterface extends GObject.Object {
    get name() {
        return 'inaction';
    }
});

describe('GObject interface', function () {
    it('cannot be instantiated', function () {
        expect(() => new AGObjectInterface()).toThrow();
    });

    it('has a name', function () {
        expect(AGObjectInterface.name).toEqual('AGObjectInterface');
    });

    it('reports its type name', function () {
        expect(AGObjectInterface.$gtype.name).toEqual('ArbitraryGTypeName');
    });

    it('can be implemented by a GObject class', function () {
        let obj;
        expect(() => {
            obj = new GObjectImplementingGObjectInterface();
        }).not.toThrow();
        expect(obj instanceof AGObjectInterface).toBeTruthy();
    });

    it('is implemented by a GObject class with the correct class object', function () {
        let obj = new GObjectImplementingGObjectInterface();
        expect(obj.constructor).toBe(GObjectImplementingGObjectInterface);
        expect(obj.constructor.name)
            .toEqual('GObjectImplementingGObjectInterface');
    });

    it('can have its required function implemented', function () {
        expect(() => {
            let obj = new GObjectImplementingGObjectInterface();
            obj.requiredG();
        }).not.toThrow();
    });

    it('must have its required function implemented', function () {
        const BadObject = GObject.registerClass({
            Implements: [AGObjectInterface],
            Properties: {
                'interface-prop': GObject.ParamSpec.override('interface-prop',
                    AGObjectInterface),
            },
        }, class BadObject extends GObject.Object {});
        expect(() => new BadObject().requiredG())
            .toThrowError(GObject.NotImplementedError);
    });

    it("doesn't have to have its optional function implemented", function () {
        let obj;
        expect(() => {
            obj = new MinimalImplementationOfAGObjectInterface();
        }).not.toThrow();
        expect(obj instanceof AGObjectInterface).toBeTruthy();
    });

    it('can have its optional function deferred to by the implementation', function () {
        let obj = new MinimalImplementationOfAGObjectInterface();
        expect(obj.optionalG()).toEqual('AGObjectInterface.optionalG()');
    });

    it('can have its function chained up to', function () {
        let obj = new GObjectImplementingGObjectInterface();
        expect(obj.optionalG()).toEqual('AGObjectInterface.optionalG()');
    });

    it('can require another interface', function () {
        let obj;
        expect(() => {
            obj = new ImplementationOfTwoInterfaces();
        }).not.toThrow();
        expect(obj instanceof AGObjectInterface).toBeTruthy();
        expect(obj instanceof InterfaceRequiringGObjectInterface).toBeTruthy();
    });

    it('can chain up to another interface', function () {
        let obj = new ImplementationOfTwoInterfaces();
        expect(obj.optionalG())
            .toEqual('InterfaceRequiringGObjectInterface.optionalG()\nAGObjectInterface.optionalG()');
    });

    it("defers to the last interface's optional function", function () {
        const MinimalImplementationOfTwoInterfaces = GObject.registerClass({
            Implements: [AGObjectInterface, InterfaceRequiringGObjectInterface],
            Properties: {
                'interface-prop': GObject.ParamSpec.override('interface-prop',
                    AGObjectInterface),
            },
        }, class MinimalImplementationOfTwoInterfaces extends GObject.Object {
            requiredG() {}
        });
        let obj = new MinimalImplementationOfTwoInterfaces();
        expect(obj.optionalG())
            .toEqual('InterfaceRequiringGObjectInterface.optionalG()\nAGObjectInterface.optionalG()');
    });

    it('must be implemented by a class that implements all required interfaces', function () {
        expect(() => GObject.registerClass({
            Implements: [InterfaceRequiringGObjectInterface],
        }, class BadObject {
            required() {}
        })).toThrow();
    });

    it('must be implemented by a class that implements required interfaces in correct order', function () {
        expect(() => GObject.registerClass({
            Implements: [InterfaceRequiringGObjectInterface, AGObjectInterface],
        }, class BadObject {
            required() {}
        })).toThrow();
    });

    it('can require an interface from C', function () {
        const InitableInterface = GObject.registerClass({
            Requires: [GObject.Object, Gio.Initable],
        }, class InitableInterface extends GObject.Interface {});
        expect(() => GObject.registerClass({
            Implements: [InitableInterface],
        }, class BadObject {})).toThrow();
    });

    it('can connect class signals on the implementing class', function (done) {
        function quitLoop() {
            expect(classSignalSpy).toHaveBeenCalled();
            done();
        }
        let obj = new GObjectImplementingGObjectInterface();
        let classSignalSpy = jasmine.createSpy('classSignalSpy')
            .and.callFake(quitLoop);
        obj.connect('class-signal', classSignalSpy);
        GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
            obj.emit('class-signal');
            return GLib.SOURCE_REMOVE;
        });
    });

    it('can connect interface signals on the implementing class', function (done) {
        function quitLoop() {
            expect(interfaceSignalSpy).toHaveBeenCalled();
            done();
        }
        let obj = new GObjectImplementingGObjectInterface();
        let interfaceSignalSpy = jasmine.createSpy('interfaceSignalSpy')
            .and.callFake(quitLoop);
        obj.connect('interface-signal', interfaceSignalSpy);
        GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
            obj.emit('interface-signal');
            return GLib.SOURCE_REMOVE;
        });
    });

    it('can define properties on the implementing class', function () {
        let obj = new GObjectImplementingGObjectInterface();
        expect(obj.interface_prop).toEqual('foobar');
        expect(obj.class_prop).toEqual('meh');
    });

    it('must have its properties overridden', function () {
        // Failing to override an interface property doesn't raise an error but
        // instead logs a critical warning.
        GLib.test_expect_message('GLib-GObject', GLib.LogLevelFlags.LEVEL_CRITICAL,
            "Object class * doesn't implement property 'interface-prop' from " +
            "interface 'ArbitraryGTypeName'");
        GObject.registerClass({
            Implements: [AGObjectInterface],
        }, class MyNaughtyObject extends GObject.Object {
            requiredG() {}
        });
        // g_test_assert_expected_messages() is a macro, not introspectable
        GLib.test_assert_expected_messages_internal('Gjs', 'testGObjectInterface.js',
            253, 'testGObjectMustOverrideInterfaceProperties');
    });

    it('can have introspected properties overriden', function () {
        let obj = new ImplementationOfIntrospectedInterface();
        expect(obj.name).toEqual('inaction');
    });

    it('can be implemented by a class as well as its parent class', function () {
        const SubObject = GObject.registerClass(
            class SubObject extends GObjectImplementingGObjectInterface {});
        let obj = new SubObject();
        expect(obj instanceof AGObjectInterface).toBeTruthy();
        expect(obj.interface_prop).toEqual('foobar');  // override not needed
    });

    it('can be reimplemented by a subclass of a class that already implements it', function () {
        const SubImplementer = GObject.registerClass({
            Implements: [AGObjectInterface],
        }, class SubImplementer extends GObjectImplementingGObjectInterface {});
        let obj = new SubImplementer();
        expect(obj instanceof AGObjectInterface).toBeTruthy();
        expect(obj.interface_prop).toEqual('foobar');  // override not needed
    });

    it('has a toString() defintion', function () {
        expect(new GObjectImplementingGObjectInterface().toString()).toMatch(
            /\[object instance wrapper GType:Gjs_GObjectImplementingGObjectInterface jsobj@0x[a-f0-9]+ native@0x[a-f0-9]+\]/);
    });

    it('has instance definition', function () {
        const obj = new GObjectImplementingGObjectInterface();
        const obj2 = new ImplementationOfTwoInterfaces();
        const file = Gio.File.new_for_path('/');
        expect(obj).toBeInstanceOf(AGObjectInterface);
        expect(obj).not.toBeInstanceOf(InterfaceRequiringGObjectInterface);
        expect(obj2).toBeInstanceOf(AGObjectInterface);
        expect(obj2).toBeInstanceOf(InterfaceRequiringGObjectInterface);
        expect(new GObject.Object()).not.toBeInstanceOf(AGObjectInterface);
        expect(file).toBeInstanceOf(Gio.File);
        expect(file).toBeInstanceOf(GObject.Object);
    });

    it('has instance definition for non-object type', function () {
        expect(null).not.toBeInstanceOf(AGObjectInterface);
        expect(true).not.toBeInstanceOf(AGObjectInterface);
        expect(undefined).not.toBeInstanceOf(AGObjectInterface);
        expect(123456).not.toBeInstanceOf(AGObjectInterface);
        expect(54321n).not.toBeInstanceOf(AGObjectInterface);
        expect('no way!').not.toBeInstanceOf(AGObjectInterface);
        expect(new Date()).not.toBeInstanceOf(AGObjectInterface);
    });

    it('has instance definition for non-object type for native interface', function () {
        expect(null).not.toBeInstanceOf(Gio.File);
        expect(true).not.toBeInstanceOf(Gio.File);
        expect(undefined).not.toBeInstanceOf(Gio.File);
        expect(12345).not.toBeInstanceOf(Gio.File);
        expect(54321n).not.toBeInstanceOf(Gio.File);
        expect('no way!').not.toBeInstanceOf(Gio.File);
        expect(new Date()).not.toBeInstanceOf(Gio.File);
    });

    describe('prototype', function () {
        let file, originalDup;

        beforeAll(function () {
            file = Gio.File.new_for_path('/');
            originalDup = Gio.File.prototype.dup;
        });

        it('toString is enumerable and defined', function () {
            expect(Object.getOwnPropertyNames(Gio.File.prototype)).toContain('toString');
            expect(Gio.File.prototype.toString).toBeDefined();
        });

        it('method properties are enumerated', function () {
            const expectedMethods = [
                'copy_attributes',
                'copy_async',
                'create_async',
                'create_readwrite_async',
                'delete_async',
                'enumerate_children',
            ];

            const methods = Object.getOwnPropertyNames(Gio.File.prototype);
            expect(methods).toEqual(jasmine.arrayContaining(expectedMethods));
        });

        it('method properties are defined', function () {
            const methods = Object.getOwnPropertyNames(Gio.File.prototype);

            for (const method of methods) {
                expect(Gio.File.prototype[method]).toBeDefined();
                expect(Gio.File.prototype[method]).toBeInstanceOf(Function);
            }
        });

        it('overrides are inherited by implementing classes', function () {
            spyOn(Gio.File.prototype, 'dup');

            expect(file).toBeInstanceOf(Gio.File);
            expect(file).toBeInstanceOf(Gio._LocalFilePrototype.constructor);

            file.dup();
            expect(Gio.File.prototype.dup).toHaveBeenCalledOnceWith();

            Gio.File.prototype.dup = originalDup;
            expect(file.dup).toBe(originalDup);
        });

        it('overrides cannot be changed by instances of child classes', function () {
            spyOn(Gio.File.prototype, 'dup');

            expect(file).toBeInstanceOf(Gio.File);
            expect(file).toBeInstanceOf(Gio._LocalFilePrototype.constructor);

            file.dup = 5;
            expect(Gio.File.prototype.dup).not.toBe(5);
            expect(Gio._LocalFilePrototype.dup).not.toBe(5);

            file.dup = originalDup;
            expect(file.dup).toBe(originalDup);
        });

        it('unknown properties are inherited by implementing classes', function () {
            Gio.File.prototype._originalDup = originalDup;
            expect(file._originalDup).toBe(originalDup);

            Gio.File.prototype._originalDup = 5;
            expect(file._originalDup).toBe(5);

            delete Gio.File.prototype._originalDup;
            expect(file._originalDup).not.toBeDefined();
        });

        it('original property can be shadowed by class prototype property', function () {
            spyOn(Gio._LocalFilePrototype, 'dup').and.returnValue(5);

            expect(file.dup()).toBe(5);
            expect(Gio._LocalFilePrototype.dup).toHaveBeenCalled();
        });

        it('overridden property can be shadowed by class prototype property', function () {
            spyOn(Gio._LocalFilePrototype, 'dup');
            spyOn(Gio.File.prototype, 'dup');

            file.dup();
            expect(Gio._LocalFilePrototype.dup).toHaveBeenCalled();
            expect(Gio.File.prototype.dup).not.toHaveBeenCalled();
        });

        it('shadowed property can be restored', function () {
            Gio._LocalFilePrototype.dup = 5;
            expect(file.dup).toBe(5);

            delete Gio._LocalFilePrototype.dup;
            expect(file.dup).toBeInstanceOf(Function);
        });
    });
});

describe('Specific class and interface checks', function () {
    it('Gio.AsyncInitable must implement vfunc_async_init', function () {
        expect(() => GObject.registerClass({
            Implements: [Gio.Initable, Gio.AsyncInitable],
        }, class BadAsyncInitable extends GObject.Object {
            vfunc_init() {}
        })).toThrow();
    });
});