forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test-system-events.js
278 lines (205 loc) · 9.06 KB
/
test-system-events.js
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const events = require("sdk/system/events");
const self = require("sdk/self");
const { Cc, Ci, Cu } = require("chrome");
const { setTimeout } = require("sdk/timers");
const { Loader, LoaderWithHookedConsole2 } = require("sdk/test/loader");
const nsIObserverService = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
var isConsoleEvent = (topic) =>
!!~["console-api-log-event", "console-storage-cache-event"].indexOf(topic)
exports["test basic"] = function(assert) {
let type = Date.now().toString(32);
let timesCalled = 0;
function handler({subject, data}) { timesCalled++; };
events.on(type, handler);
events.emit(type, { data: "yo yo" });
assert.equal(timesCalled, 1, "event handler was called");
events.off(type, handler);
events.emit(type, { data: "no way" });
assert.equal(timesCalled, 1, "removed handler is no longer called");
events.once(type, handler);
events.emit(type, { data: "and we meet again" });
events.emit(type, { data: "it's always hard to say bye" });
assert.equal(timesCalled, 2, "handlers added via once are triggered once");
}
exports["test simple argument passing"] = function (assert) {
let type = Date.now().toString(32);
let lastArg;
function handler({data}) { lastArg = data; }
events.on(type, handler);
[true, false, 100, 0, 'a string', ''].forEach(arg => {
events.emit(type, arg);
assert.strictEqual(lastArg, arg + '',
'event emitted for ' + arg + ' has correct data value');
events.emit(type, { data: arg });
assert.strictEqual(lastArg, arg + '',
'event emitted for ' + arg + ' has correct data value when a property on an object');
});
[null, undefined, {}].forEach(arg => {
events.emit(type, arg);
assert.strictEqual(lastArg, null,
'emitting ' + arg + ' gets null data');
});
events.off(type, handler);
};
exports["test error reporting"] = function(assert) {
let { loader, messages } = LoaderWithHookedConsole2(module);
let events = loader.require("sdk/system/events");
function brokenHandler(subject, data) { throw new Error("foo"); };
let lineNumber;
try { brokenHandler() } catch (error) { lineNumber = error.lineNumber }
let errorType = Date.now().toString(32);
events.on(errorType, brokenHandler);
events.emit(errorType, { data: "yo yo" });
assert.equal(messages.length, 2, "Got an exception");
assert.equal(messages[0], "console.error: " + self.name + ": \n",
"error is logged");
let text = messages[1];
assert.ok(text.indexOf("Error: foo") >= 0, "error message is logged");
assert.ok(text.indexOf(module.uri) >= 0, "module uri is logged");
assert.ok(text.indexOf(lineNumber) >= 0, "error line is logged");
events.off(errorType, brokenHandler);
loader.unload();
};
exports["test listeners are GC-ed"] = function(assert, done) {
let receivedFromWeak = [];
let receivedFromStrong = [];
let loader = Loader(module);
let events = loader.require('sdk/system/events');
let type = 'test-listeners-are-garbage-collected';
function handler(event) { receivedFromStrong.push(event); }
function weakHandler(event) { receivedFromWeak.push(event); }
events.on(type, handler, true);
events.on(type, weakHandler);
events.emit(type, { data: 1 });
assert.equal(receivedFromStrong.length, 1, "strong listener invoked");
assert.equal(receivedFromWeak.length, 1, "weak listener invoked");
handler = weakHandler = null;
Cu.schedulePreciseGC(function() {
events.emit(type, { data: 2 });
assert.equal(receivedFromWeak.length, 1, "weak listener was GC-ed");
assert.equal(receivedFromStrong.length, 2, "strong listener was invoked");
loader.unload();
done();
});
};
exports["test alive listeners are removed on unload"] = function(assert) {
let receivedFromWeak = [];
let receivedFromStrong = [];
let loader = Loader(module);
let events = loader.require('sdk/system/events');
let type = 'test-alive-listeners-are-removed';
const handler = (event) => receivedFromStrong.push(event);
const weakHandler = (event) => receivedFromWeak.push(event);
events.on(type, handler, true);
events.on(type, weakHandler);
events.emit(type, { data: 1 });
assert.equal(receivedFromStrong.length, 1, "strong listener invoked");
assert.equal(receivedFromWeak.length, 1, "weak listener invoked");
loader.unload();
events.emit(type, { data: 2 });
assert.equal(receivedFromWeak.length, 1, "weak listener was removed");
assert.equal(receivedFromStrong.length, 1, "strong listener was removed");
};
exports["test handle nsIObserverService notifications"] = function(assert) {
let ios = Cc['@mozilla.org/network/io-service;1']
.getService(Ci.nsIIOService);
let uri = ios.newURI("http://www.foo.com", null, null);
let type = Date.now().toString(32);
let timesCalled = 0;
let lastSubject = null;
let lastData = null;
let lastType = null;
function handler({ subject, data, type }) {
// Ignores internal console events
if (isConsoleEvent(type))
return;
timesCalled++;
lastSubject = subject;
lastData = data;
lastType = type;
};
events.on(type, handler);
nsIObserverService.notifyObservers(uri, type, "some data");
assert.equal(timesCalled, 1, "notification invokes handler");
assert.equal(lastType, type, "event.type is notification topic");
assert.equal(lastSubject, uri, "event.subject is notification subject");
assert.equal(lastData, "some data", "event.data is notification data");
function customSubject() {}
function customData() {}
events.emit(type, { data: customData, subject: customSubject });
assert.equal(timesCalled, 2, "notification invokes handler");
assert.equal(lastType, type, "event.type is notification topic");
assert.equal(lastSubject, customSubject,
"event.subject is wrapped & unwrapped");
assert.equal(lastData, customData, "event.data is wrapped & unwrapped");
events.off(type, handler);
nsIObserverService.notifyObservers(null, type, "some data");
assert.equal(timesCalled, 2, "event handler is removed");
events.on("*", handler);
nsIObserverService.notifyObservers(null, type, "more data");
assert.equal(timesCalled, 3, "notification invokes * handler");
assert.equal(lastType, type, "event.type is notification topic");
assert.equal(lastSubject, null,
"event.subject is notification subject");
assert.equal(lastData, "more data", "event.data is notification data");
events.off("*", handler);
nsIObserverService.notifyObservers(null, type, "last data");
assert.equal(timesCalled, 3, "* event handler is removed");
};
exports["test emit to nsIObserverService observers"] = function(assert) {
let ios = Cc['@mozilla.org/network/io-service;1']
.getService(Ci.nsIIOService);
let uri = ios.newURI("http://www.foo.com", null, null);
let timesCalled = 0;
let lastSubject = null;
let lastData = null;
let lastTopic = null;
var topic = Date.now().toString(32)
let nsIObserver = {
QueryInterface: function() {
return nsIObserver;
},
observe: function(subject, topic, data) {
// Ignores internal console events
if (isConsoleEvent(topic))
return;
timesCalled = timesCalled + 1;
lastSubject = subject;
lastData = data;
lastTopic = topic;
}
};
nsIObserverService.addObserver(nsIObserver, topic, false);
events.emit(topic, { subject: uri, data: "some data" });
assert.equal(timesCalled, 1, "emit notifies observers");
assert.equal(lastTopic, topic, "event type is notification topic");
assert.equal(lastSubject.wrappedJSObject.object, uri,
"event.subject is notification subject");
assert.equal(lastData, "some data", "event.data is notification data");
function customSubject() {}
function customData() {}
events.emit(topic, { subject: customSubject, data: customData });
assert.equal(timesCalled, 2, "emit notifies observers");
assert.equal(lastTopic, topic, "event.type is notification");
assert.equal(lastSubject.wrappedJSObject.object, customSubject,
"event.subject is notification subject");
assert.equal(lastData, customData, "event.data is notification data");
nsIObserverService.removeObserver(nsIObserver, topic);
events.emit(topic, { data: "more data" });
assert.equal(timesCalled, 2, "removed observers no longer invoked");
nsIObserverService.addObserver(nsIObserver, "*", false);
events.emit(topic, { data: "data again" });
assert.equal(timesCalled, 3, "emit notifies * observers");
assert.equal(lastTopic, topic, "event.type is notification");
assert.equal(lastSubject, null,
"event.subject is notification subject");
assert.equal(lastData, "data again", "event.data is notification data");
nsIObserverService.removeObserver(nsIObserver, "*");
events.emit(topic, { data: "last data" });
assert.equal(timesCalled, 3, "removed observers no longer invoked");
}
require("sdk/test").run(exports);