|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 "use strict"; |
|
5 |
|
6 const { Cc, Ci } = require('chrome'); |
|
7 const { Symbiont } = require('sdk/deprecated/symbiont'); |
|
8 const self = require('sdk/self'); |
|
9 const fixtures = require("./fixtures"); |
|
10 const { close } = require('sdk/window/helpers'); |
|
11 const app = require("sdk/system/xul-app"); |
|
12 |
|
13 function makeWindow() { |
|
14 let content = |
|
15 '<?xml version="1.0"?>' + |
|
16 '<window ' + |
|
17 'xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">' + |
|
18 '<iframe id="content" type="content"/>' + |
|
19 '</window>'; |
|
20 var url = "data:application/vnd.mozilla.xul+xml;charset=utf-8," + |
|
21 encodeURIComponent(content); |
|
22 var features = ["chrome", "width=10", "height=10"]; |
|
23 |
|
24 return Cc["@mozilla.org/embedcomp/window-watcher;1"]. |
|
25 getService(Ci.nsIWindowWatcher). |
|
26 openWindow(null, url, null, features.join(","), null); |
|
27 } |
|
28 |
|
29 exports['test:constructing symbiont && validating API'] = function(assert) { |
|
30 let contentScript = ["1;", "2;"]; |
|
31 let contentScriptFile = fixtures.url("test-content-symbiont.js"); |
|
32 |
|
33 // We can avoid passing a `frame` argument. Symbiont will create one |
|
34 // by using HiddenFrame module |
|
35 let contentSymbiont = Symbiont({ |
|
36 contentScriptFile: contentScriptFile, |
|
37 contentScript: contentScript, |
|
38 contentScriptWhen: "start" |
|
39 }); |
|
40 |
|
41 assert.equal( |
|
42 contentScriptFile, |
|
43 contentSymbiont.contentScriptFile, |
|
44 "There is one contentScriptFile, as specified in options." |
|
45 ); |
|
46 assert.equal( |
|
47 contentScript.length, |
|
48 contentSymbiont.contentScript.length, |
|
49 "There are two contentScripts, as specified in options." |
|
50 ); |
|
51 assert.equal( |
|
52 contentScript[0], |
|
53 contentSymbiont.contentScript[0], |
|
54 "There are two contentScripts, as specified in options." |
|
55 ); |
|
56 assert.equal( |
|
57 contentScript[1], |
|
58 contentSymbiont.contentScript[1], |
|
59 "There are two contentScripts, as specified in options." |
|
60 ) |
|
61 assert.equal( |
|
62 contentSymbiont.contentScriptWhen, |
|
63 "start", |
|
64 "contentScriptWhen is as specified in options." |
|
65 ); |
|
66 |
|
67 contentSymbiont.destroy(); |
|
68 }; |
|
69 |
|
70 exports["test:communication with worker global scope"] = function(assert, done) { |
|
71 if (app.is('Fennec')) { |
|
72 assert.pass('Test skipped on Fennec'); |
|
73 done(); |
|
74 } |
|
75 |
|
76 let window = makeWindow(); |
|
77 let contentSymbiont; |
|
78 |
|
79 assert.ok(!!window, 'there is a window'); |
|
80 |
|
81 function onMessage1(message) { |
|
82 assert.equal(message, 1, "Program gets message via onMessage."); |
|
83 contentSymbiont.removeListener('message', onMessage1); |
|
84 contentSymbiont.on('message', onMessage2); |
|
85 contentSymbiont.postMessage(2); |
|
86 }; |
|
87 |
|
88 function onMessage2(message) { |
|
89 if (5 == message) { |
|
90 close(window).then(done); |
|
91 } |
|
92 else { |
|
93 assert.equal(message, 3, "Program gets message via onMessage2."); |
|
94 contentSymbiont.postMessage(4) |
|
95 } |
|
96 } |
|
97 |
|
98 window.addEventListener("load", function onLoad() { |
|
99 window.removeEventListener("load", onLoad, false); |
|
100 let frame = window.document.getElementById("content"); |
|
101 contentSymbiont = Symbiont({ |
|
102 frame: frame, |
|
103 contentScript: 'new ' + function() { |
|
104 self.postMessage(1); |
|
105 self.on("message", function onMessage(message) { |
|
106 if (message === 2) |
|
107 self.postMessage(3); |
|
108 if (message === 4) |
|
109 self.postMessage(5); |
|
110 }); |
|
111 } + '()', |
|
112 contentScriptWhen: 'ready', |
|
113 onMessage: onMessage1 |
|
114 }); |
|
115 |
|
116 frame.setAttribute("src", "data:text/html;charset=utf-8,<html><body></body></html>"); |
|
117 }, false); |
|
118 }; |
|
119 |
|
120 exports['test:pageWorker'] = function(assert, done) { |
|
121 let worker = Symbiont({ |
|
122 contentURL: 'about:buildconfig', |
|
123 contentScript: 'new ' + function WorkerScope() { |
|
124 self.on('message', function(data) { |
|
125 if (data.valid) |
|
126 self.postMessage('bye!'); |
|
127 }) |
|
128 self.postMessage(window.location.toString()); |
|
129 }, |
|
130 onMessage: function(msg) { |
|
131 if (msg == 'bye!') { |
|
132 done() |
|
133 } else { |
|
134 assert.equal( |
|
135 worker.contentURL + '', |
|
136 msg |
|
137 ); |
|
138 worker.postMessage({ valid: true }); |
|
139 } |
|
140 } |
|
141 }); |
|
142 }; |
|
143 |
|
144 exports["test:document element present on 'start'"] = function(assert, done) { |
|
145 let xulApp = require("sdk/system/xul-app"); |
|
146 let worker = Symbiont({ |
|
147 contentURL: "about:buildconfig", |
|
148 contentScript: "self.postMessage(!!document.documentElement)", |
|
149 contentScriptWhen: "start", |
|
150 onMessage: function(message) { |
|
151 if (xulApp.versionInRange(xulApp.platformVersion, "2.0b6", "*")) |
|
152 assert.ok(message, "document element present on 'start'"); |
|
153 else |
|
154 assert.pass("document element not necessarily present on 'start'"); |
|
155 done(); |
|
156 } |
|
157 }); |
|
158 }; |
|
159 |
|
160 require("test").run(exports); |