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
|
<!DOCTYPE HTML>
<html>
<head>
<title>WebExtension test</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script type="text/javascript" src="head.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="text/javascript">
"use strict";
function backgroundScript(token, id, otherId) {
browser.tabs.create({url: "tab.html"});
browser.runtime.onMessage.addListener((msg, sender, sendReply) => {
browser.test.assertEq(id, sender.id, `${id}: Got expected sender ID`);
if (msg === `content-${token}`) {
browser.test.assertTrue(sender.tab.url.endsWith("file_sample.html"),
`${id}: sender url correct`);
let tabId = sender.tab.id;
browser.tabs.sendMessage(tabId, `${token}-contentMessage`);
sendReply(`${token}-done`);
} else if (msg === `tab-${token}`) {
browser.runtime.sendMessage(otherId, `${otherId}-tabMessage`);
browser.runtime.sendMessage(`${token}-tabMessage`);
sendReply(`${token}-done`);
} else {
browser.test.fail(`${id}: Unexpected runtime message received: ${msg} ${uneval(sender)}`);
}
});
browser.runtime.onMessageExternal.addListener((msg, sender, sendReply) => {
browser.test.assertEq(otherId, sender.id, `${id}: Got expected external sender ID`);
if (msg === `content-${id}`) {
browser.test.assertTrue(sender.tab.url.endsWith("file_sample.html"),
`${id}: external sender url correct`);
sendReply(`${otherId}-done`);
} else if (msg === `tab-${id}`) {
sendReply(`${otherId}-done`);
} else if (msg !== `${id}-tabMessage`) {
browser.test.fail(`${id}: Unexpected runtime external message received: ${msg} ${uneval(sender)}`);
}
});
}
function contentScript(token, id, otherId) {
let gotContentMessage = false;
browser.runtime.onMessage.addListener((msg, sender, sendReply) => {
browser.test.assertEq(id, sender.id, `${id}: Got expected sender ID`);
browser.test.assertEq(`${token}-contentMessage`, msg,
`${id}: Correct content script message`);
if (msg === `${token}-contentMessage`) {
gotContentMessage = true;
}
});
Promise.all([
browser.runtime.sendMessage(otherId, `content-${otherId}`).then(resp => {
browser.test.assertEq(`${id}-done`, resp, `${id}: Correct content script external response token`);
}),
browser.runtime.sendMessage(`content-${token}`).then(resp => {
browser.test.assertEq(`${token}-done`, resp, `${id}: Correct content script response token`);
}),
]).then(() => {
browser.test.assertTrue(gotContentMessage, `${id}: Got content script message`);
browser.test.sendMessage("content-script-done");
});
}
function tabScript(token, id, otherId) {
let gotTabMessage = false;
browser.runtime.onMessage.addListener((msg, sender, sendReply) => {
browser.test.assertEq(id, sender.id, `${id}: Got expected sender ID`);
if (String(msg).startsWith("content-")) {
return;
}
browser.test.assertEq(`${token}-tabMessage`, msg,
`${id}: Correct tab script message`);
if (msg === `${token}-tabMessage`) {
gotTabMessage = true;
}
});
Promise.all([
browser.runtime.sendMessage(otherId, `tab-${otherId}`).then(resp => {
browser.test.assertEq(`${id}-done`, resp, `${id}: Correct tab script external response token`);
}),
browser.runtime.sendMessage(`tab-${token}`).then(resp => {
browser.test.assertEq(`${token}-done`, resp, `${id}: Correct tab script response token`);
}),
]).then(() => {
browser.test.assertTrue(gotTabMessage, `${id}: Got tab script message`);
window.close();
browser.test.sendMessage("tab-script-done");
});
}
function makeExtension(id, otherId) {
let token = Math.random();
let args = `${token}, ${JSON.stringify(id)}, ${JSON.stringify(otherId)}`;
let extensionData = {
background: `(${backgroundScript})(${args})`,
manifest: {
"applications": {"gecko": {id}},
"permissions": ["tabs"],
"content_scripts": [{
"matches": ["http://mochi.test/*/file_sample.html"],
"js": ["content_script.js"],
"run_at": "document_start",
}],
},
files: {
"tab.html": `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="tab.js"><\/script>
</head>
</html>`,
"tab.js": `(${tabScript})(${args})`,
"content_script.js": `(${contentScript})(${args})`,
},
};
return extensionData;
}
add_task(function* test_contentscript() {
const ID1 = "sendmessage1@mochitest.mozilla.org";
const ID2 = "sendmessage2@mochitest.mozilla.org";
let extension1 = ExtensionTestUtils.loadExtension(makeExtension(ID1, ID2));
let extension2 = ExtensionTestUtils.loadExtension(makeExtension(ID2, ID1));
yield Promise.all([extension1.startup(), extension2.startup()]);
let win = window.open("file_sample.html");
yield waitForLoad(win);
yield Promise.all([
extension1.awaitMessage("content-script-done"),
extension2.awaitMessage("content-script-done"),
extension1.awaitMessage("tab-script-done"),
extension2.awaitMessage("tab-script-done"),
]);
win.close();
yield extension1.unload();
yield extension2.unload();
});
</script>
</body>
</html>
|