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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const {
ConsoleAPIListener,
} = require("resource://devtools/server/actors/webconsole/listeners/console-api.js");
const {
on,
once,
off,
emit,
count,
handler,
} = require("resource://devtools/shared/event-emitter.js");
const pass = message => ok(true, message);
const fail = message => ok(false, message);
/**
* Each method of this object is a test; tests can be synchronous or asynchronous:
*
* 1. Plain method are synchronous tests.
* 2. methods with `async` keyword are asynchronous tests.
* 3. methods with `done` as argument are asynchronous tests (`done` needs to be called to
* complete the test).
*/
const TESTS = {
testAddListener() {
const events = [{ name: "event#1" }, "event#2"];
const target = { name: "target" };
on(target, "message", function (message) {
equal(this, target, "this is a target object");
equal(message, events.shift(), "message is emitted event");
});
emit(target, "message", events[0]);
emit(target, "message", events[0]);
},
testListenerIsUniquePerType() {
const actual = [];
const target = {};
listener = () => actual.push(1);
on(target, "message", listener);
on(target, "message", listener);
on(target, "message", listener);
on(target, "foo", listener);
on(target, "foo", listener);
emit(target, "message");
deepEqual([1], actual, "only one message listener added");
emit(target, "foo");
deepEqual([1, 1], actual, "same listener added for other event");
},
testEventTypeMatters() {
const target = { name: "target" };
on(target, "message", () => fail("no event is expected"));
on(target, "done", () => pass("event is emitted"));
emit(target, "foo");
emit(target, "done");
},
testAllArgumentsArePassed() {
const foo = { name: "foo" },
bar = "bar";
const target = { name: "target" };
on(target, "message", (a, b) => {
equal(a, foo, "first argument passed");
equal(b, bar, "second argument passed");
});
emit(target, "message", foo, bar);
},
testNoSideEffectsInEmit() {
const target = { name: "target" };
on(target, "message", () => {
pass("first listener is called");
on(target, "message", () => fail("second listener is called"));
});
emit(target, "message");
},
testCanRemoveNextListener() {
const target = { name: "target" };
on(target, "data", () => {
pass("first listener called");
off(target, "data", fail);
});
on(target, "data", fail);
emit(target, "data", "Listener should be removed");
},
testOrderOfPropagation() {
const actual = [];
const target = { name: "target" };
on(target, "message", () => actual.push(1));
on(target, "message", () => actual.push(2));
on(target, "message", () => actual.push(3));
emit(target, "message");
deepEqual([1, 2, 3], actual, "called in order they were added");
},
testRemoveListener() {
const target = { name: "target" };
const actual = [];
on(target, "message", function listener() {
actual.push(1);
on(target, "message", () => {
off(target, "message", listener);
actual.push(2);
});
});
emit(target, "message");
deepEqual([1], actual, "first listener called");
emit(target, "message");
deepEqual([1, 1, 2], actual, "second listener called");
emit(target, "message");
deepEqual([1, 1, 2, 2, 2], actual, "first listener removed");
},
testRemoveAllListenersForType() {
const actual = [];
const target = { name: "target" };
on(target, "message", () => actual.push(1));
on(target, "message", () => actual.push(2));
on(target, "message", () => actual.push(3));
on(target, "bar", () => actual.push("b"));
off(target, "message");
emit(target, "message");
emit(target, "bar");
deepEqual(["b"], actual, "all message listeners were removed");
},
testRemoveAllListeners() {
const actual = [];
const target = { name: "target" };
on(target, "message", () => actual.push(1));
on(target, "message", () => actual.push(2));
on(target, "message", () => actual.push(3));
on(target, "bar", () => actual.push("b"));
off(target);
emit(target, "message");
emit(target, "bar");
deepEqual([], actual, "all listeners events were removed");
},
testFalsyArgumentsAreFine() {
let type, listener;
const target = { name: "target" },
actual = [];
on(target, "bar", () => actual.push(0));
off(target, "bar", listener);
emit(target, "bar");
deepEqual([0], actual, "3rd bad arg will keep listener");
off(target, type);
emit(target, "bar");
deepEqual([0, 0], actual, "2nd bad arg will keep listener");
off(target, type, listener);
emit(target, "bar");
deepEqual([0, 0, 0], actual, "2nd & 3rd bad args will keep listener");
},
testUnhandledExceptions(done) {
const listener = new ConsoleAPIListener(null, message => {
equal(message.level, "error", "Got the first exception");
equal(
message.arguments[0].message,
"Boom!",
"unhandled exception is logged"
);
listener.destroy();
done();
});
listener.init();
const target = {};
on(target, "message", () => {
throw Error("Boom!");
});
emit(target, "message");
},
testCount() {
const target = { name: "target" };
equal(count(target, "foo"), 0, "no listeners for 'foo' events");
on(target, "foo", () => {});
equal(count(target, "foo"), 1, "listener registered");
on(target, "foo", () => {});
equal(count(target, "foo"), 2, "another listener registered");
off(target);
equal(count(target, "foo"), 0, "listeners unregistered");
},
async testOnce() {
const target = { name: "target" };
const called = false;
const pFoo = once(target, "foo", function (value) {
ok(!called, "listener called only once");
equal(value, "bar", "correct argument was passed");
equal(this, target, "the contextual object is correct");
});
const pDone = once(target, "done");
emit(target, "foo", "bar");
emit(target, "foo", "baz");
emit(target, "done", "");
await Promise.all([pFoo, pDone]);
},
testRemovingOnce(done) {
const target = { name: "target" };
once(target, "foo", fail);
once(target, "done", done);
off(target, "foo", fail);
emit(target, "foo", "listener was called");
emit(target, "done", "");
},
testAddListenerWithHandlerMethod() {
const target = { name: "target" };
const actual = [];
const listener = function (...args) {
equal(
this,
target,
"the contextual object is correct for function listener"
);
deepEqual(args, [10, 20, 30], "arguments are properly passed");
};
const object = {
name: "target",
[handler](type, ...rest) {
actual.push(type);
equal(
this,
object,
"the contextual object is correct for object listener"
);
deepEqual(rest, [10, 20, 30], "arguments are properly passed");
},
};
on(target, "foo", listener);
on(target, "bar", object);
on(target, "baz", object);
emit(target, "foo", 10, 20, 30);
emit(target, "bar", 10, 20, 30);
emit(target, "baz", 10, 20, 30);
deepEqual(
actual,
["bar", "baz"],
"object's listener called in the expected order"
);
},
testRemoveListenerWithHandlerMethod() {
const target = {};
const actual = [];
const object = {
[handler](type) {
actual.push(1);
on(target, "message", () => {
off(target, "message", object);
actual.push(2);
});
},
};
on(target, "message", object);
emit(target, "message");
deepEqual([1], actual, "first listener called");
emit(target, "message");
deepEqual([1, 1, 2], actual, "second listener called");
emit(target, "message");
deepEqual([1, 1, 2, 2, 2], actual, "first listener removed");
},
async testOnceListenerWithHandlerMethod() {
const target = { name: "target" };
const called = false;
const object = {
[handler](type, value) {
ok(!called, "listener called only once");
equal(type, "foo", "event type is properly passed");
equal(value, "bar", "correct argument was passed");
equal(
this,
object,
"the contextual object is correct for object listener"
);
},
};
const pFoo = once(target, "foo", object);
const pDone = once(target, "done");
emit(target, "foo", "bar");
emit(target, "foo", "baz");
emit(target, "done", "");
await Promise.all([pFoo, pDone]);
},
testCallingOffWithMoreThan3Args() {
const target = { name: "target" };
on(target, "data", fail);
off(target, "data", fail, undefined);
emit(target, "data", "Listener should be removed");
},
};
/**
* Create a runnable tests based on the tests descriptor given.
*
* @param {Object} tests
* The tests descriptor object, contains the tests to run.
*/
const runnable = tests =>
async function () {
for (const name of Object.keys(tests)) {
info(name);
if (tests[name].length === 1) {
await new Promise(resolve => tests[name](resolve));
} else {
await tests[name]();
}
}
};
add_task(runnable(TESTS));
|