summaryrefslogtreecommitdiffstats
path: root/tests/unit/signalTracker.js
blob: 7943d0a20f468380447abc6691a40cf28a64a5ca (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
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-

// Test cases for version comparison

const { GObject } = imports.gi;

const JsUnit = imports.jsUnit;
const Signals = imports.misc.signals;

const Environment = imports.ui.environment;
const { TransientSignalHolder, registerDestroyableType } = imports.misc.signalTracker;

Environment.init();

const Destroyable = GObject.registerClass({
    Signals: { 'destroy': {} },
}, class Destroyable extends GObject.Object {});
registerDestroyableType(Destroyable);

const GObjectEmitter = GObject.registerClass({
    Signals: { 'signal': {} },
}, class GObjectEmitter extends Destroyable {});

const emitter1 = new Signals.EventEmitter();
const emitter2 = new GObjectEmitter();

const tracked1 = new Destroyable();
const tracked2 = {};

let count = 0;
const handler = () => count++;

emitter1.connectObject('signal', handler, tracked1);
emitter2.connectObject('signal', handler, tracked1);

emitter1.connectObject('signal', handler, tracked2);
emitter2.connectObject('signal', handler, tracked2);

JsUnit.assertEquals(count, 0);

emitter1.emit('signal');
emitter2.emit('signal');

JsUnit.assertEquals(count, 4);

tracked1.emit('destroy');

emitter1.emit('signal');
emitter2.emit('signal');

JsUnit.assertEquals(count, 6);

emitter1.disconnectObject(tracked2);
emitter2.emit('destroy');

emitter1.emit('signal');
emitter2.emit('signal');

JsUnit.assertEquals(count, 6);

emitter1.connectObject(
    'signal', handler,
    'signal', handler, GObject.ConnectFlags.AFTER,
    tracked1);
emitter2.connectObject(
    'signal', handler,
    'signal', handler, GObject.ConnectFlags.AFTER,
    tracked1);

emitter1.emit('signal');
emitter2.emit('signal');

JsUnit.assertEquals(count, 10);

tracked1.emit('destroy');
emitter1.emit('signal');
emitter2.emit('signal');

JsUnit.assertEquals(count, 10);

emitter1.connectObject('signal', handler, tracked1);
emitter2.connectObject('signal', handler, tracked1);

transientHolder = new TransientSignalHolder(tracked1);

emitter1.connectObject('signal', handler, transientHolder);
emitter2.connectObject('signal', handler, transientHolder);

emitter1.emit('signal');
emitter2.emit('signal');

JsUnit.assertEquals(count, 14);

transientHolder.destroy();

emitter1.emit('signal');
emitter2.emit('signal');

JsUnit.assertEquals(count, 16);

transientHolder = new TransientSignalHolder(tracked1);

emitter1.connectObject('signal', handler, transientHolder);
emitter2.connectObject('signal', handler, transientHolder);

emitter1.emit('signal');
emitter2.emit('signal');

JsUnit.assertEquals(count, 20);

tracked1.emit('destroy');
emitter1.emit('signal');
emitter2.emit('signal');

JsUnit.assertEquals(count, 20);