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
|
/* 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/. */
"use strict";
const { id: addonID, name: addonName } = require("sdk/self");
const { Cc, Ci, Cu } = require("chrome");
const { Loader, LoaderWithHookedConsole2 } = require("sdk/test/loader");
const { InputPort } = require("sdk/input/system");
const { OutputPort } = require("sdk/output/system");
const { removeObserver, addObserver,
notifyObservers } = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
const { lift, start, stop, send } = require("sdk/event/utils");
const isConsoleEvent = topic =>
["console-api-log-event",
"console-storage-cache-event"].indexOf(topic) >= 0;
const message = x => ({wrappedJSObject: {data: x}});
exports["test start / stop ports"] = assert => {
const input = new InputPort({ id: Date.now().toString(32), initial: {data:0} });
const topic = input.topic;
assert.ok(topic.includes(addonID), "topics are namespaced to add-on");
const xs = lift(({data}) => "x:" + data, input);
const ys = lift(({data}) => "y:" + data, input);
assert.deepEqual(input.value, {data:0}, "initila value is set");
assert.deepEqual(xs.value, "x:0", "initial value is mapped");
assert.deepEqual(ys.value, "y:0", "initial value is mapped");
notifyObservers(message(1), topic, null);
assert.deepEqual(input.value, {data:0}, "no message received on input port");
assert.deepEqual(xs.value, "x:0", "no message received on xs");
assert.deepEqual(ys.value, "y:0", "no message received on ys");
start(xs);
notifyObservers(message(2), topic, null);
assert.deepEqual(input.value, {data:2}, "message received on input port");
assert.deepEqual(xs.value, "x:2", "message received on xs");
assert.deepEqual(ys.value, "y:2", "no message received on (not started) ys");
notifyObservers(message(3), topic, null);
assert.deepEqual(input.value, {data:3}, "message received on input port");
assert.deepEqual(xs.value, "x:3", "message received on xs");
assert.deepEqual(ys.value, "y:3", "message received on ys");
notifyObservers(message(4), topic, null);
assert.deepEqual(input.value, {data:4}, "message received on input port");
assert.deepEqual(xs.value, "x:4", "message not received on (stopped) xs");
assert.deepEqual(ys.value, "y:4", "message received on ys");
stop(input);
notifyObservers(message(5), topic, null);
assert.deepEqual(input.value, {data:4}, "message note received on input port");
assert.deepEqual(xs.value, "x:4", "message not received on (stopped) xs");
assert.deepEqual(ys.value, "y:4", "message not received on (stopped) ys");
};
exports["test send messages to nsIObserverService"] = assert => {
let messages = [];
const { newURI } = Cc['@mozilla.org/network/io-service;1'].
getService(Ci.nsIIOService);
const output = new OutputPort({ id: Date.now().toString(32), sync: true });
const topic = output.topic;
const observer = {
QueryInterface: function() {
return this;
},
observe: (subject, topic, data) => {
// Ignores internal console events
if (!isConsoleEvent(topic)) {
messages.push({
topic: topic,
subject: subject
});
}
}
};
addObserver(observer, topic, false);
send(output, null);
assert.deepEqual(messages.shift(), { topic: topic, subject: null },
"null message received");
const uri = newURI("http://www.foo.com", null, null);
send(output, uri);
assert.deepEqual(messages.shift(), { topic: topic, subject: uri },
"message received");
function customSubject() {}
send(output, customSubject);
let message = messages.shift();
assert.equal(message.topic, topic, "topic was received");
assert.equal(message.subject.wrappedJSObject, customSubject,
"custom subject is received");
removeObserver(observer, topic);
send(output, { data: "more data" });
assert.deepEqual(messages, [],
"no more data received");
addObserver(observer, "*", false);
send(output, { data: "data again" });
message = messages.shift();
assert.equal(message.topic, topic, "topic was received");
assert.deepEqual(message.subject.wrappedJSObject,
{ data: "data again" },
"wrapped message received");
removeObserver(observer, "*");
send(output, { data: "last data" });
assert.deepEqual(messages, [],
"no more data received");
assert.throws(() => send(output, "hi"),
/Unsupproted message type: `string`/,
"strings can't be send");
assert.throws(() => send(output, 4),
/Unsupproted message type: `number`/,
"numbers can't be send");
assert.throws(() => send(output, void(0)),
/Unsupproted message type: `undefined`/,
"undefineds can't be send");
assert.throws(() => send(output, true),
/Unsupproted message type: `boolean`/,
"booleans can't be send");
};
exports["test async OutputPort"] = (assert, done) => {
let async = false;
const output = new OutputPort({ id: Date.now().toString(32) });
const observer = {
observe: (subject, topic, data) => {
removeObserver(observer, topic);
assert.equal(topic, output.topic, "correct topic");
assert.deepEqual(subject.wrappedJSObject, {foo: "bar"}, "message received");
assert.ok(async, "message received async");
done();
}
};
addObserver(observer, output.topic, false);
send(output, {foo: "bar"});
assert.throws(() => send(output, "boom"), "can only send object");
async = true;
};
exports["test explicit output topic"] = (assert, done) => {
const topic = Date.now().toString(32);
const output = new OutputPort({ topic: topic });
const observer = {
observe: (subject, topic, data) => {
removeObserver(observer, topic);
assert.deepEqual(subject.wrappedJSObject, {foo: "bar"}, "message received");
done();
}
};
assert.equal(output.topic, topic, "given topic is used");
addObserver(observer, topic, false);
send(output, {foo: "bar"});
};
exports["test explicit input topic"] = (assert) => {
const topic = Date.now().toString(32);
const input = new InputPort({ topic: topic });
start(input);
assert.equal(input.topic, topic, "given topic is used");
notifyObservers({wrappedJSObject: {foo: "bar"}}, topic, null);
assert.deepEqual(input.value, {foo: "bar"}, "message received");
};
exports["test receive what was send"] = assert => {
const id = Date.now().toString(32);
const input = new InputPort({ id: id, initial: 0});
const output = new OutputPort({ id: id, sync: true });
assert.ok(input.topic.includes(addonID),
"input topic is namespaced to addon");
assert.equal(input.topic, output.topic,
"input & output get same topics from id.");
start(input);
assert.equal(input.value, 0, "initial value is set");
send(output, { data: 1 });
assert.deepEqual(input.value, {data: 1}, "message unwrapped");
send(output, []);
assert.deepEqual(input.value, [], "array message unwrapped");
send(output, null);
assert.deepEqual(input.value, null, "null message received");
send(output, new String("message"));
assert.deepEqual(input.value, new String("message"),
"string instance received");
send(output, /pattern/);
assert.deepEqual(input.value, /pattern/, "regexp received");
assert.throws(() => send(output, "hi"),
/Unsupproted message type: `string`/,
"strings can't be send");
assert.throws(() => send(output, 4),
/Unsupproted message type: `number`/,
"numbers can't be send");
assert.throws(() => send(output, void(0)),
/Unsupproted message type: `undefined`/,
"undefineds can't be send");
assert.throws(() => send(output, true),
/Unsupproted message type: `boolean`/,
"booleans can't be send");
stop(input);
};
exports["-test error reporting"] = function(assert) {
let { loader, messages } = LoaderWithHookedConsole2(module);
const { start, stop, lift } = loader.require("sdk/event/utils");
const { InputPort } = loader.require("sdk/input/system");
const { OutputPort } = loader.require("sdk/output/system");
const id = "error:" + Date.now().toString(32);
const raise = x => { if (x) throw new Error("foo"); };
const input = new InputPort({ id: id });
const output = new OutputPort({ id: id, sync: true });
const xs = lift(raise, input);
assert.equal(input.value, null, "initial inherited");
send(output, { data: "yo yo" });
assert.deepEqual(messages, [], "nothing happend yet");
start(xs);
send(output, { data: "first" });
assert.equal(messages.length, 4, "Got an exception");
assert.equal(messages[0], "console.error: " + addonName + ": \n",
"error is logged");
assert.ok(/Unhandled error/.test(messages[1]),
"expected error message");
loader.unload();
};
exports["test unload ends input port"] = assert => {
const loader = Loader(module);
const { start, stop, lift } = loader.require("sdk/event/utils");
const { InputPort } = loader.require("sdk/input/system");
const id = "unload!" + Date.now().toString(32);
const input = new InputPort({ id: id });
start(input);
notifyObservers(message(1), input.topic, null);
assert.deepEqual(input.value, {data: 1}, "message received");
notifyObservers(message(2), input.topic, null);
assert.deepEqual(input.value, {data: 2}, "message received");
loader.unload();
notifyObservers(message(3), input.topic, null);
assert.deepEqual(input.value, {data: 2}, "message wasn't received");
};
require("sdk/test").run(exports);
|