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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
import { actionCreators as ac, actionTypes as at } from "common/Actions.jsm";
import {
ActivityStreamMessageChannel,
DEFAULT_OPTIONS,
} from "lib/ActivityStreamMessageChannel.jsm";
import { addNumberReducer, GlobalOverrider } from "test/unit/utils";
import { applyMiddleware, createStore } from "redux";
const OPTIONS = [
"pageURL, outgoingMessageName",
"incomingMessageName",
"dispatch",
];
describe("ActivityStreamMessageChannel", () => {
let globals;
let dispatch;
let mm;
let RPmessagePorts;
beforeEach(() => {
RPmessagePorts = [];
function RP(url, isFromAboutNewTab = false) {
this.url = url;
this.messagePorts = RPmessagePorts;
this.addMessageListener = globals.sandbox.spy();
this.removeMessageListener = globals.sandbox.spy();
this.sendAsyncMessage = globals.sandbox.spy();
this.destroy = globals.sandbox.spy();
this.isFromAboutNewTab = isFromAboutNewTab;
}
globals = new GlobalOverrider();
const overridePageListener = globals.sandbox.stub();
overridePageListener.withArgs(true).returns(new RP("about:newtab", true));
overridePageListener.withArgs(false).returns(null);
globals.set("AboutNewTab", {
overridePageListener,
reset: globals.sandbox.spy(),
});
globals.set("RemotePages", RP);
globals.set("AboutHomeStartupCache", { onPreloadedNewTabMessage() {} });
dispatch = globals.sandbox.spy();
mm = new ActivityStreamMessageChannel({ dispatch });
});
afterEach(() => globals.restore());
describe("portID validation", () => {
let sandbox;
beforeEach(() => {
sandbox = sinon.createSandbox();
sandbox.spy(global.Cu, "reportError");
});
afterEach(() => {
sandbox.restore();
});
it("should log errors for an invalid portID", () => {
mm.validatePortID({});
mm.validatePortID({});
mm.validatePortID({});
assert.equal(global.Cu.reportError.callCount, 3);
});
});
it("should exist", () => {
assert.ok(ActivityStreamMessageChannel);
});
it("should apply default options", () => {
mm = new ActivityStreamMessageChannel();
OPTIONS.forEach(o => assert.equal(mm[o], DEFAULT_OPTIONS[o], o));
});
it("should add options", () => {
const options = {
dispatch: () => {},
pageURL: "FOO.html",
outgoingMessageName: "OUT",
incomingMessageName: "IN",
};
mm = new ActivityStreamMessageChannel(options);
OPTIONS.forEach(o => assert.equal(mm[o], options[o], o));
});
it("should throw an error if no dispatcher was provided", () => {
mm = new ActivityStreamMessageChannel();
assert.throws(() => mm.dispatch({ type: "FOO" }));
});
describe("Creating/destroying the channel", () => {
describe("#createChannel", () => {
it("should create .channel with the correct URL", () => {
mm.createChannel();
assert.ok(mm.channel);
assert.equal(mm.channel.url, mm.pageURL);
});
it("should add 4 message listeners", () => {
mm.createChannel();
assert.callCount(mm.channel.addMessageListener, 4);
});
it("should add the custom message listener to the channel", () => {
mm.createChannel();
assert.calledWith(
mm.channel.addMessageListener,
mm.incomingMessageName,
mm.onMessage
);
});
it("should override AboutNewTab", () => {
mm.createChannel();
assert.calledOnce(global.AboutNewTab.overridePageListener);
});
it("should use the channel passed by AboutNewTab on override", () => {
mm.createChannel();
assert.ok(mm.channel.isFromAboutNewTab);
});
it("should not override AboutNewTab if the pageURL is not about:newtab", () => {
mm = new ActivityStreamMessageChannel({ pageURL: "foo.html" });
mm.createChannel();
assert.notCalled(global.AboutNewTab.overridePageListener);
});
});
describe("#simulateMessagesForExistingTabs", () => {
beforeEach(() => {
sinon.stub(mm, "onActionFromContent");
mm.createChannel();
});
it("should simulate init for existing ports", () => {
RPmessagePorts.push({
url: "about:monkeys",
loaded: false,
portID: "inited",
simulated: true,
browser: {
getAttribute: () => "preloaded",
ownerGlobal: {},
},
});
RPmessagePorts.push({
url: "about:sheep",
loaded: true,
portID: "loaded",
simulated: true,
browser: {
getAttribute: () => "preloaded",
ownerGlobal: {},
},
});
mm.simulateMessagesForExistingTabs();
assert.calledWith(mm.onActionFromContent.firstCall, {
type: at.NEW_TAB_INIT,
data: RPmessagePorts[0],
});
assert.calledWith(mm.onActionFromContent.secondCall, {
type: at.NEW_TAB_INIT,
data: RPmessagePorts[1],
});
});
it("should simulate load for loaded ports", () => {
RPmessagePorts.push({
loaded: true,
portID: "foo",
browser: {
getAttribute: () => "preloaded",
ownerGlobal: {},
},
});
mm.simulateMessagesForExistingTabs();
assert.calledWith(
mm.onActionFromContent,
{ type: at.NEW_TAB_LOAD },
"foo"
);
});
it("should set renderLayers on preloaded browsers after load", () => {
RPmessagePorts.push({
loaded: true,
portID: "foo",
browser: {
getAttribute: () => "preloaded",
ownerGlobal: {
STATE_MAXIMIZED: 1,
STATE_MINIMIZED: 2,
STATE_NORMAL: 3,
STATE_FULLSCREEN: 4,
windowState: 3,
isFullyOccluded: false,
},
},
});
mm.simulateMessagesForExistingTabs();
assert.equal(RPmessagePorts[0].browser.renderLayers, true);
});
});
describe("#destroyChannel", () => {
let channel;
beforeEach(() => {
mm.createChannel();
channel = mm.channel;
});
it("should set .channel to null", () => {
mm.destroyChannel();
assert.isNull(mm.channel);
});
it("should reset AboutNewTab, and pass back its channel", () => {
mm.destroyChannel();
assert.calledOnce(global.AboutNewTab.reset);
assert.calledWith(global.AboutNewTab.reset, channel);
});
it("should not reset AboutNewTab if the pageURL is not about:newtab", () => {
mm = new ActivityStreamMessageChannel({ pageURL: "foo.html" });
mm.createChannel();
mm.destroyChannel();
assert.notCalled(global.AboutNewTab.reset);
});
it("should call channel.destroy() if pageURL is not about:newtab", () => {
mm = new ActivityStreamMessageChannel({ pageURL: "foo.html" });
mm.createChannel();
channel = mm.channel;
mm.destroyChannel();
assert.calledOnce(channel.destroy);
});
});
});
describe("Message handling", () => {
describe("#getTargetById", () => {
it("should get an id if it exists", () => {
const t = { portID: "foo:1" };
mm.createChannel();
mm.channel.messagePorts.push(t);
assert.equal(mm.getTargetById("foo:1"), t);
});
it("should return null if the target doesn't exist", () => {
const t = { portID: "foo:2" };
mm.createChannel();
mm.channel.messagePorts.push(t);
assert.equal(mm.getTargetById("bar:3"), null);
});
});
describe("#getPreloadedBrowser", () => {
it("should get a preloaded browser if it exists", () => {
const port = {
browser: {
getAttribute: () => "preloaded",
ownerGlobal: {},
},
};
mm.createChannel();
mm.channel.messagePorts.push(port);
assert.equal(mm.getPreloadedBrowser()[0], port);
});
it("should get all the preloaded browsers across windows if they exist", () => {
const port = {
browser: {
getAttribute: () => "preloaded",
ownerGlobal: {},
},
};
mm.createChannel();
mm.channel.messagePorts.push(port);
mm.channel.messagePorts.push(port);
assert.equal(mm.getPreloadedBrowser().length, 2);
});
it("should return null if there is no preloaded browser", () => {
const port = {
browser: {
getAttribute: () => "consumed",
ownerGlobal: {},
},
};
mm.createChannel();
mm.channel.messagePorts.push(port);
assert.equal(mm.getPreloadedBrowser(), null);
});
});
describe("#onNewTabInit", () => {
it("should dispatch a NEW_TAB_INIT action", () => {
const t = { portID: "foo", url: "about:monkeys" };
sinon.stub(mm, "onActionFromContent");
mm.onNewTabInit({ target: t });
assert.calledWith(mm.onActionFromContent, {
type: at.NEW_TAB_INIT,
data: t,
});
});
});
describe("#onNewTabLoad", () => {
it("should dispatch a NEW_TAB_LOAD action", () => {
const t = {
portID: "foo",
browser: {
getAttribute: () => "preloaded",
ownerGlobal: {},
},
};
sinon.stub(mm, "onActionFromContent");
mm.onNewTabLoad({ target: t });
assert.calledWith(
mm.onActionFromContent,
{ type: at.NEW_TAB_LOAD },
"foo"
);
});
});
describe("#onNewTabUnload", () => {
it("should dispatch a NEW_TAB_UNLOAD action", () => {
const t = { portID: "foo" };
sinon.stub(mm, "onActionFromContent");
mm.onNewTabUnload({ target: t });
assert.calledWith(
mm.onActionFromContent,
{ type: at.NEW_TAB_UNLOAD },
"foo"
);
});
});
describe("#onMessage", () => {
let sandbox;
beforeEach(() => {
sandbox = sinon.createSandbox();
sandbox.spy(global.Cu, "reportError");
});
afterEach(() => sandbox.restore());
it("should report an error if the msg.data is missing", () => {
mm.onMessage({ target: { portID: "foo" } });
assert.calledOnce(global.Cu.reportError);
});
it("should report an error if the msg.data.type is missing", () => {
mm.onMessage({ target: { portID: "foo" }, data: "foo" });
assert.calledOnce(global.Cu.reportError);
});
it("should call onActionFromContent", () => {
sinon.stub(mm, "onActionFromContent");
const action = {
data: { data: {}, type: "FOO" },
target: { portID: "foo" },
};
const expectedAction = {
type: action.data.type,
data: action.data.data,
_target: { portID: "foo" },
};
mm.onMessage(action);
assert.calledWith(mm.onActionFromContent, expectedAction, "foo");
});
});
});
describe("Sending and broadcasting", () => {
describe("#send", () => {
it("should send a message on the right port", () => {
const t = { portID: "foo:3", sendAsyncMessage: sinon.spy() };
mm.createChannel();
mm.channel.messagePorts = [t];
const action = ac.AlsoToOneContent({ type: "HELLO" }, "foo:3");
mm.send(action);
assert.calledWith(
t.sendAsyncMessage,
DEFAULT_OPTIONS.outgoingMessageName,
action
);
});
it("should not throw if the target isn't around", () => {
mm.createChannel();
// port is not added to the channel
const action = ac.AlsoToOneContent({ type: "HELLO" }, "foo:4");
assert.doesNotThrow(() => mm.send(action));
});
});
describe("#broadcast", () => {
it("should send a message on the channel", () => {
mm.createChannel();
const action = ac.BroadcastToContent({ type: "HELLO" });
mm.broadcast(action);
assert.calledWith(
mm.channel.sendAsyncMessage,
DEFAULT_OPTIONS.outgoingMessageName,
action
);
});
});
describe("#preloaded browser", () => {
it("should send the message to the preloaded browser if there's data and a preloaded browser exists", () => {
const port = {
browser: {
getAttribute: () => "preloaded",
ownerGlobal: {},
},
sendAsyncMessage: sinon.spy(),
};
mm.createChannel();
mm.channel.messagePorts.push(port);
const action = ac.AlsoToPreloaded({ type: "HELLO", data: 10 });
mm.sendToPreloaded(action);
assert.calledWith(
port.sendAsyncMessage,
DEFAULT_OPTIONS.outgoingMessageName,
action
);
});
it("should send the message to all the preloaded browsers if there's data and they exist", () => {
const port = {
browser: {
getAttribute: () => "preloaded",
ownerGlobal: {},
},
sendAsyncMessage: sinon.spy(),
};
mm.createChannel();
mm.channel.messagePorts.push(port);
mm.channel.messagePorts.push(port);
mm.sendToPreloaded(ac.AlsoToPreloaded({ type: "HELLO", data: 10 }));
assert.calledTwice(port.sendAsyncMessage);
});
it("should not send the message to the preloaded browser if there's no data and a preloaded browser does not exists", () => {
const port = {
browser: {
getAttribute: () => "consumed",
ownerGlobal: {},
},
sendAsyncMessage: sinon.spy(),
};
mm.createChannel();
mm.channel.messagePorts.push(port);
const action = ac.AlsoToPreloaded({ type: "HELLO" });
mm.sendToPreloaded(action);
assert.notCalled(port.sendAsyncMessage);
});
});
});
describe("Handling actions", () => {
describe("#onActionFromContent", () => {
beforeEach(() => mm.onActionFromContent({ type: "FOO" }, "foo:5"));
it("should dispatch a AlsoToMain action", () => {
assert.calledOnce(dispatch);
const [action] = dispatch.firstCall.args;
assert.equal(action.type, "FOO", "action.type");
});
it("should have the right fromTarget", () => {
const [action] = dispatch.firstCall.args;
assert.equal(action.meta.fromTarget, "foo:5", "meta.fromTarget");
});
});
describe("#middleware", () => {
let store;
beforeEach(() => {
store = createStore(addNumberReducer, applyMiddleware(mm.middleware));
});
it("should just call next if no channel is found", () => {
store.dispatch({ type: "ADD", data: 10 });
assert.equal(store.getState(), 10);
});
it("should call .send but not affect the main store if an OnlyToOneContent action is dispatched", () => {
sinon.stub(mm, "send");
const action = ac.OnlyToOneContent({ type: "ADD", data: 10 }, "foo");
mm.createChannel();
store.dispatch(action);
assert.calledWith(mm.send, action);
assert.equal(store.getState(), 0);
});
it("should call .send and update the main store if an AlsoToOneContent action is dispatched", () => {
sinon.stub(mm, "send");
const action = ac.AlsoToOneContent({ type: "ADD", data: 10 }, "foo");
mm.createChannel();
store.dispatch(action);
assert.calledWith(mm.send, action);
assert.equal(store.getState(), 10);
});
it("should call .broadcast if the action is BroadcastToContent", () => {
sinon.stub(mm, "broadcast");
const action = ac.BroadcastToContent({ type: "FOO" });
mm.createChannel();
store.dispatch(action);
assert.calledWith(mm.broadcast, action);
});
it("should call .sendToPreloaded if the action is AlsoToPreloaded", () => {
sinon.stub(mm, "sendToPreloaded");
const action = ac.AlsoToPreloaded({ type: "FOO" });
mm.createChannel();
store.dispatch(action);
assert.calledWith(mm.sendToPreloaded, action);
});
it("should dispatch other actions normally", () => {
sinon.stub(mm, "send");
sinon.stub(mm, "broadcast");
sinon.stub(mm, "sendToPreloaded");
mm.createChannel();
store.dispatch({ type: "ADD", data: 1 });
assert.equal(store.getState(), 1);
assert.notCalled(mm.send);
assert.notCalled(mm.broadcast);
assert.notCalled(mm.sendToPreloaded);
});
});
});
});
|