summaryrefslogtreecommitdiff
path: root/installed-tests/js/testGObject.js
blob: d88487238b5e15dc56e303398b288ed278853eaf (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
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2013 Giovanni Campagna <gcampagna@src.gnome.org>
// SPDX-FileCopyrightText: 2018 Red Hat, Inc.

// This is where overrides in modules/overrides/GObject.js are tested,
// except for the class machinery, interface machinery, and GObject.ParamSpec,
// which are big enough to get their own files.

const {GLib, GObject} = imports.gi;
const {system: System} = imports;

const TestObj = GObject.registerClass({
    Properties: {
        int: GObject.ParamSpec.int('int', '', '', GObject.ParamFlags.READWRITE,
            0, GLib.MAXINT32, 0),
        string: GObject.ParamSpec.string('string', '', '',
            GObject.ParamFlags.READWRITE, ''),
    },
    Signals: {
        test: {},
    },
}, class TestObj extends GObject.Object {});

describe('GObject overrides', function () {
    it('GObject.set()', function () {
        const o = new TestObj();
        o.set({string: 'Answer', int: 42});
        expect(o.string).toBe('Answer');
        expect(o.int).toBe(42);
    });

    describe('Signal alternative syntax', function () {
        let o, handler;
        beforeEach(function () {
            handler = jasmine.createSpy('handler');
            o = new TestObj();
            const handlerId = GObject.signal_connect(o, 'test', handler);
            handler.and.callFake(() =>
                GObject.signal_handler_disconnect(o, handlerId));

            GObject.signal_emit_by_name(o, 'test');
        });

        it('handler is called with the right object', function () {
            expect(handler).toHaveBeenCalledTimes(1);
            expect(handler).toHaveBeenCalledWith(o);
        });

        it('disconnected handler is not called', function () {
            handler.calls.reset();
            GObject.signal_emit_by_name(o, 'test');
            expect(handler).not.toHaveBeenCalled();
        });
    });

    it('toString() shows the native object address', function () {
        const o = new TestObj();
        const address = System.addressOfGObject(o);
        expect(o.toString()).toMatch(
            new RegExp(`[object instance wrapper .* jsobj@0x[a-f0-9]+ native@${address}`));
    });
});

describe('GObject should', function () {
    const types = ['gpointer', 'GBoxed', 'GParam', 'GInterface', 'GObject', 'GVariant'];

    types.forEach(type => {
        it(`be able to create a GType object for ${type}`, function () {
            const gtype = GObject.Type(type);
            expect(gtype.name).toEqual(type);
        });
    });

    it('be able to query signals', function () {
        const query = GObject.signal_query(1);

        expect(query instanceof GObject.SignalQuery).toBeTruthy();
        expect(query.param_types).not.toBeNull();
        expect(Array.isArray(query.param_types)).toBeTruthy();
        expect(query.signal_id).toBe(1);
    });
});

describe('GObject.Object.new()', function () {
    const gon = GObject.Object.new;

    it('can be called with a property bag', function () {
        const o = gon(TestObj, {
            string: 'Answer',
            int: 42,
        });
        expect(o.string).toBe('Answer');
        expect(o.int).toBe(42);
    });

    it('can be called to construct an object without setting properties', function () {
        const o1 = gon(TestObj);
        expect(o1.string).toBe('');
        expect(o1.int).toBe(0);

        const o2 = gon(TestObj, {});
        expect(o2.string).toBe('');
        expect(o2.int).toBe(0);
    });

    it('complains about wrong types', function () {
        expect(() => gon(TestObj, {
            string: 42,
            int: 'Answer',
        })).toThrow();
    });

    it('complains about wrong properties', function () {
        expect(() => gon(TestObj, {foo: 'bar'})).toThrow();
    });

    it('can construct C GObjects as well', function () {
        const o = gon(GObject.Object, {});
        expect(o.constructor.$gtype.name).toBe('GObject');
    });
});

describe('GObject.Object.new_with_properties()', function () {
    const gonwp = GObject.Object.new_with_properties;

    it('can be called with two arrays', function () {
        const o = gonwp(TestObj, ['string', 'int'], ['Answer', 42]);
        expect(o.string).toBe('Answer');
        expect(o.int).toBe(42);
    });

    it('can be called to construct an object without setting properties', function () {
        const o = gonwp(TestObj, [], []);
        expect(o.string).toBe('');
        expect(o.int).toBe(0);
    });

    it('complains about various incorrect usages', function () {
        expect(() => gonwp(TestObj)).toThrow();
        expect(() => gonwp(TestObj, ['string', 'int'])).toThrow();
        expect(() => gonwp(TestObj, ['string', 'int'], ['Answer'])).toThrow();
        expect(() => gonwp(TestObj, {}, ['Answer', 42])).toThrow();
    });

    it('complains about wrong types', function () {
        expect(() => gonwp(TestObj, ['string', 'int'], [42, 'Answer'])).toThrow();
    });

    it('complains about wrong properties', function () {
        expect(() => gonwp(TestObj, ['foo'], ['bar'])).toThrow();
    });

    it('can construct C GObjects as well', function () {
        const o = gonwp(GObject.Object, [], []);
        expect(o.constructor.$gtype.name).toBe('GObject');
    });
});