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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
/* 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 { Loader } = require('sdk/test/loader');
const { Page } = require("sdk/page-worker");
const { URL } = require("sdk/url");
const fixtures = require("./fixtures");
const testURI = fixtures.url("test.html");
const { getActiveView } = require("sdk/view/core");
const { getDocShell } = require('sdk/frame/utils');
const ERR_DESTROYED =
"Couldn't find the worker to receive this message. " +
"The script may not be initialized yet, or may already have been unloaded.";
const Isolate = fn => "(" + fn + ")()";
exports.testSimplePageCreation = function(assert, done) {
let page = new Page({
contentScript: "self.postMessage(window.location.href)",
contentScriptWhen: "end",
onMessage: function (message) {
assert.equal(message, "about:blank",
"Page Worker should start with a blank page by default");
assert.equal(this, page, "The 'this' object is the page itself.");
done();
}
});
}
/*
* Tests that we can't be tricked by document overloads as we have access
* to wrapped nodes
*/
exports.testWrappedDOM = function(assert, done) {
let page = Page({
allow: { script: true },
contentURL: "data:text/html;charset=utf-8,<script>document.getElementById=3;window.scrollTo=3;</script>",
contentScript: 'new ' + function() {
function send() {
self.postMessage([typeof(document.getElementById), typeof(window.scrollTo)]);
}
if (document.readyState !== 'complete')
window.addEventListener('load', send, true)
else
send();
},
onMessage: function (message) {
assert.equal(message[0],
"function",
"getElementById from content script is the native one");
assert.equal(message[1],
"function",
"scrollTo from content script is the native one");
done();
}
});
}
/*
// We do not offer unwrapped access to DOM since bug 601295 landed
// See 660780 to track progress of unwrap feature
exports.testUnwrappedDOM = function(assert, done) {
let page = Page({
allow: { script: true },
contentURL: "data:text/html;charset=utf-8,<script>document.getElementById=3;window.scrollTo=3;</script>",
contentScript: "window.addEventListener('load', function () {" +
"return self.postMessage([typeof(unsafeWindow.document.getElementById), " +
"typeof(unsafeWindow.scrollTo)]); }, true)",
onMessage: function (message) {
assert.equal(message[0],
"number",
"document inside page is free to be changed");
assert.equal(message[1],
"number",
"window inside page is free to be changed");
done();
}
});
}
*/
exports.testPageProperties = function(assert) {
let page = new Page();
for (let prop of ['contentURL', 'allow', 'contentScriptFile',
'contentScript', 'contentScriptWhen', 'on',
'postMessage', 'removeListener']) {
assert.ok(prop in page, prop + " property is defined on page.");
}
assert.ok(() => page.postMessage("foo") || true,
"postMessage doesn't throw exception on page.");
}
exports.testConstructorAndDestructor = function(assert, done) {
let loader = Loader(module);
let { Page } = loader.require("sdk/page-worker");
let global = loader.sandbox("sdk/page-worker");
let pagesReady = 0;
let page1 = Page({
contentScript: "self.postMessage('')",
contentScriptWhen: "end",
onMessage: pageReady
});
let page2 = Page({
contentScript: "self.postMessage('')",
contentScriptWhen: "end",
onMessage: pageReady
});
assert.notEqual(page1, page2,
"Page 1 and page 2 should be different objects.");
function pageReady() {
if (++pagesReady == 2) {
page1.destroy();
page2.destroy();
assert.ok(isDestroyed(page1), "page1 correctly unloaded.");
assert.ok(isDestroyed(page2), "page2 correctly unloaded.");
loader.unload();
done();
}
}
}
exports.testAutoDestructor = function(assert, done) {
let loader = Loader(module);
let { Page } = loader.require("sdk/page-worker");
let page = Page({
contentScript: "self.postMessage('')",
contentScriptWhen: "end",
onMessage: function() {
loader.unload();
assert.ok(isDestroyed(page), "Page correctly unloaded.");
done();
}
});
}
exports.testValidateOptions = function(assert) {
assert.throws(
() => Page({ contentURL: 'home' }),
/The `contentURL` option must be a valid URL\./,
"Validation correctly denied a non-URL contentURL"
);
assert.throws(
() => Page({ onMessage: "This is not a function."}),
/The option "onMessage" must be one of the following types: function/,
"Validation correctly denied a non-function onMessage."
);
assert.pass("Options validation is working.");
}
exports.testContentAndAllowGettersAndSetters = function(assert, done) {
let content = "data:text/html;charset=utf-8,<script>window.localStorage.allowScript=3;</script>";
// Load up the page with testURI initially for the resource:// principal,
// then load the actual data:* content, as data:* URIs no longer
// have localStorage
let page = Page({
contentURL: testURI,
contentScript: "if (window.location.href==='"+testURI+"')" +
" self.postMessage('reload');" +
"else " +
" self.postMessage(window.localStorage.allowScript)",
contentScriptWhen: "end",
onMessage: step0
});
function step0(message) {
if (message === 'reload')
return page.contentURL = content;
assert.equal(message, "3",
"Correct value expected for allowScript - 3");
assert.equal(page.contentURL, content,
"Correct content expected");
page.removeListener('message', step0);
page.on('message', step1);
page.allow = { script: false };
page.contentURL = content =
"data:text/html;charset=utf-8,<script>window.localStorage.allowScript='f'</script>";
}
function step1(message) {
assert.equal(message, "3",
"Correct value expected for allowScript - 3");
assert.equal(page.contentURL, content, "Correct content expected");
page.removeListener('message', step1);
page.on('message', step2);
page.allow = { script: true };
page.contentURL = content =
"data:text/html;charset=utf-8,<script>window.localStorage.allowScript='g'</script>";
}
function step2(message) {
assert.equal(message, "g",
"Correct value expected for allowScript - g");
assert.equal(page.contentURL, content, "Correct content expected");
page.removeListener('message', step2);
page.on('message', step3);
page.allow.script = false;
page.contentURL = content =
"data:text/html;charset=utf-8,<script>window.localStorage.allowScript=3</script>";
}
function step3(message) {
assert.equal(message, "g",
"Correct value expected for allowScript - g");
assert.equal(page.contentURL, content, "Correct content expected");
page.removeListener('message', step3);
page.on('message', step4);
page.allow.script = true;
page.contentURL = content =
"data:text/html;charset=utf-8,<script>window.localStorage.allowScript=4</script>";
}
function step4(message) {
assert.equal(message, "4",
"Correct value expected for allowScript - 4");
assert.equal(page.contentURL, content, "Correct content expected");
done();
}
}
exports.testOnMessageCallback = function(assert, done) {
Page({
contentScript: "self.postMessage('')",
contentScriptWhen: "end",
onMessage: function() {
assert.pass("onMessage callback called");
done();
}
});
}
exports.testMultipleOnMessageCallbacks = function(assert, done) {
let count = 0;
let page = Page({
contentScript: "self.postMessage('')",
contentScriptWhen: "end",
onMessage: () => count += 1
});
page.on('message', () => count += 2);
page.on('message', () => count *= 3);
page.on('message', () =>
assert.equal(count, 9, "All callbacks were called, in order."));
page.on('message', done);
};
exports.testLoadContentPage = function(assert, done) {
let page = Page({
onMessage: function(message) {
// The message is an array whose first item is the test method to call
// and the rest of whose items are arguments to pass it.
let msg = message.shift();
if (msg == "done")
return done();
assert[msg].apply(assert, message);
},
contentURL: fixtures.url("addon-sdk/data/test-page-worker.html"),
contentScriptFile: fixtures.url("addon-sdk/data/test-page-worker.js"),
contentScriptWhen: "ready"
});
}
exports.testLoadContentPageRelativePath = function(assert, done) {
const self = require("sdk/self");
const { merge } = require("sdk/util/object");
const options = merge({}, require('@loader/options'),
{ id: "testloader", prefixURI: require('./fixtures').url() });
let loader = Loader(module, null, options);
let page = loader.require("sdk/page-worker").Page({
onMessage: function(message) {
// The message is an array whose first item is the test method to call
// and the rest of whose items are arguments to pass it.
let msg = message.shift();
if (msg == "done")
return done();
assert[msg].apply(assert, message);
},
contentURL: "./test-page-worker.html",
contentScriptFile: "./test-page-worker.js",
contentScriptWhen: "ready"
});
}
exports.testAllowScriptDefault = function(assert, done) {
let page = Page({
onMessage: function(message) {
assert.ok(message, "Script is allowed to run by default.");
done();
},
contentURL: "data:text/html;charset=utf-8,<script>document.documentElement.setAttribute('foo', 3);</script>",
contentScript: "self.postMessage(document.documentElement.getAttribute('foo'))",
contentScriptWhen: "ready"
});
}
exports.testAllowScript = function(assert, done) {
let page = Page({
onMessage: function(message) {
assert.ok(message, "Script runs when allowed to do so.");
page.destroy();
done();
},
allow: { script: true },
contentURL: "data:text/html;charset=utf-8,<script>document.documentElement.setAttribute('foo', 3);</script>",
contentScript: "self.postMessage(document.documentElement.hasAttribute('foo') && " +
" document.documentElement.getAttribute('foo') == 3)",
contentScriptWhen: "ready"
});
}
exports.testPingPong = function(assert, done) {
let page = Page({
contentURL: 'data:text/html;charset=utf-8,ping-pong',
contentScript: 'self.on("message", message => self.postMessage("pong"));'
+ 'self.postMessage("ready");',
onMessage: function(message) {
if ('ready' == message) {
page.postMessage('ping');
}
else {
assert.ok(message, 'pong', 'Callback from contentScript');
done();
}
}
});
};
exports.testRedirect = function (assert, done) {
let page = Page({
contentURL: 'data:text/html;charset=utf-8,first-page',
contentScriptWhen: "end",
contentScript: '' +
'if (/first-page/.test(document.location.href)) ' +
' document.location.href = "data:text/html;charset=utf-8,redirect";' +
'else ' +
' self.port.emit("redirect", document.location.href);'
});
page.port.on('redirect', function (url) {
assert.equal(url, 'data:text/html;charset=utf-8,redirect', 'Reinjects contentScript on reload');
done();
});
};
exports.testRedirectIncludeArrays = function (assert, done) {
let firstURL = 'data:text/html;charset=utf-8,first-page';
let page = Page({
contentURL: firstURL,
contentScript: '(function () {' +
'self.port.emit("load", document.location.href);' +
' self.port.on("redirect", function (url) {' +
' document.location.href = url;' +
' })' +
'})();',
include: ['about:blank', 'data:*']
});
page.port.on('load', function (url) {
if (url === firstURL) {
page.port.emit('redirect', 'about:blank');
} else if (url === 'about:blank') {
page.port.emit('redirect', 'about:mozilla');
assert.ok('`include` property handles arrays');
assert.equal(url, 'about:blank', 'Redirects work with accepted domains');
done();
} else if (url === 'about:mozilla') {
assert.fail('Should not redirect to restricted domain');
}
});
};
exports.testRedirectFromWorker = function (assert, done) {
let firstURL = 'data:text/html;charset=utf-8,first-page';
let secondURL = 'data:text/html;charset=utf-8,second-page';
let thirdURL = 'data:text/html;charset=utf-8,third-page';
let page = Page({
contentURL: firstURL,
contentScript: '(function () {' +
'self.port.emit("load", document.location.href);' +
' self.port.on("redirect", function (url) {' +
' document.location.href = url;' +
' })' +
'})();',
include: 'data:*'
});
page.port.on('load', function (url) {
if (url === firstURL) {
page.port.emit('redirect', secondURL);
} else if (url === secondURL) {
page.port.emit('redirect', thirdURL);
} else if (url === thirdURL) {
page.port.emit('redirect', 'about:mozilla');
assert.equal(url, thirdURL, 'Redirects work with accepted domains on include strings');
done();
} else {
assert.fail('Should not redirect to unauthorized domains');
}
});
};
exports.testRedirectWithContentURL = function (assert, done) {
let firstURL = 'data:text/html;charset=utf-8,first-page';
let secondURL = 'data:text/html;charset=utf-8,second-page';
let thirdURL = 'data:text/html;charset=utf-8,third-page';
let page = Page({
contentURL: firstURL,
contentScript: '(function () {' +
'self.port.emit("load", document.location.href);' +
'})();',
include: 'data:*'
});
page.port.on('load', function (url) {
if (url === firstURL) {
page.contentURL = secondURL;
} else if (url === secondURL) {
page.contentURL = thirdURL;
} else if (url === thirdURL) {
page.contentURL = 'about:mozilla';
assert.equal(url, thirdURL, 'Redirects work with accepted domains on include strings');
done();
} else {
assert.fail('Should not redirect to unauthorized domains');
}
});
};
exports.testMultipleDestroys = function(assert) {
let page = Page();
page.destroy();
page.destroy();
assert.pass("Multiple destroys should not cause an error");
};
exports.testContentScriptOptionsOption = function(assert, done) {
let page = new Page({
contentScript: "self.postMessage( [typeof self.options.d, self.options] );",
contentScriptWhen: "end",
contentScriptOptions: {a: true, b: [1,2,3], c: "string", d: function(){ return 'test'}},
onMessage: function(msg) {
assert.equal(msg[0], 'undefined', 'functions are stripped from contentScriptOptions');
assert.equal(typeof msg[1], 'object', 'object as contentScriptOptions');
assert.equal(msg[1].a, true, 'boolean in contentScriptOptions');
assert.equal(msg[1].b.join(), '1,2,3', 'array and numbers in contentScriptOptions');
assert.equal(msg[1].c, 'string', 'string in contentScriptOptions');
done();
}
});
};
exports.testMessageQueue = function (assert, done) {
let page = new Page({
contentScript: 'self.on("message", function (m) {' +
'self.postMessage(m);' +
'});',
contentURL: 'data:text/html;charset=utf-8,',
});
page.postMessage('ping');
page.on('message', function (m) {
assert.equal(m, 'ping', 'postMessage should queue messages');
done();
});
};
exports.testWindowStopDontBreak = function (assert, done) {
const { Ci, Cc } = require('chrome');
const consoleService = Cc['@mozilla.org/consoleservice;1'].
getService(Ci.nsIConsoleService);
const listener = {
observe: ({message}) => {
if (message.includes('contentWorker is null'))
assert.fail('contentWorker is null');
}
};
consoleService.registerListener(listener)
let page = new Page({
contentURL: 'data:text/html;charset=utf-8,testWindowStopDontBreak',
contentScriptWhen: 'ready',
contentScript: Isolate(() => {
window.stop();
self.port.on('ping', () => self.port.emit('pong'));
})
});
page.port.on('pong', () => {
assert.pass('page-worker works after window.stop');
page.destroy();
consoleService.unregisterListener(listener);
done();
});
page.port.emit("ping");
};
/**
* bug 1138545 - the docs claim you can pass in a bare regexp.
*/
exports.testRegexArgument = function (assert, done) {
let url = 'data:text/html;charset=utf-8,testWindowStopDontBreak';
let page = new Page({
contentURL: url,
contentScriptWhen: 'ready',
contentScript: Isolate(() => {
self.port.emit("pong", document.location.href);
}),
include: /^data\:text\/html;.*/
});
assert.pass("We can pass in a RegExp into page-worker's include option.");
page.port.on("pong", (href) => {
assert.equal(href, url, "we get back the same url from the content script.");
page.destroy();
done();
});
};
function isDestroyed(page) {
try {
page.postMessage("foo");
}
catch (err) {
if (err.message == ERR_DESTROYED) {
return true;
}
else {
throw err;
}
}
return false;
}
require("sdk/test").run(exports);
|