Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>Tabs runtimeConnect Test</title>
5
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
6
<script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
7
<script type="text/javascript" src="head.js"></script>
8
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
9
</head>
10
<body>
11
12
<script type="text/javascript">
13
"use strict";
14
15
add_task(async function() {
16
let win = window.open("http://mochi.test:8888/");
17
18
let extension = ExtensionTestUtils.loadExtension({
19
manifest: {
20
"permissions": ["tabs"],
21
},
22
23
background: function() {
24
let messages_received = [];
25
26
let tabId;
27
28
browser.runtime.onConnect.addListener((port) => {
29
browser.test.assertTrue(!!port, "tab to background port received");
30
browser.test.assertEq("tab-connection-name", port.name, "port name should be defined and equal to connectInfo.name");
31
browser.test.assertTrue(!!port.sender.tab, "port.sender.tab should be defined");
32
browser.test.assertEq(tabId, port.sender.tab.id, "port.sender.tab.id should be equal to the expected tabId");
33
34
port.onMessage.addListener((msg) => {
35
messages_received.push(msg);
36
37
if (messages_received.length == 1) {
38
browser.test.assertEq("tab to background port message", msg, "'tab to background' port message received");
39
port.postMessage("background to tab port message");
40
}
41
42
if (messages_received.length == 2) {
43
browser.test.assertTrue(!!msg.tabReceived, "'background to tab' reply port message received");
44
browser.test.assertEq("background to tab port message", msg.tabReceived, "reply port content contains the message received");
45
46
browser.test.notifyPass("tabRuntimeConnect.pass");
47
}
48
});
49
});
50
51
browser.tabs.create({url: "tab.html"},
52
(tab) => { tabId = tab.id; });
53
},
54
55
files: {
56
"tab.js": function() {
57
let port = browser.runtime.connect({name: "tab-connection-name"});
58
port.postMessage("tab to background port message");
59
port.onMessage.addListener((msg) => {
60
port.postMessage({tabReceived: msg});
61
});
62
},
63
"tab.html": `
64
<!DOCTYPE html>
65
<html>
66
<head>
67
<title>test tab extension page</title>
68
<meta charset="utf-8">
69
<script src="tab.js" async><\/script>
70
</head>
71
<body>
72
<h1>test tab extension page</h1>
73
</body>
74
</html>
75
`,
76
},
77
});
78
79
await extension.startup();
80
await extension.awaitFinish("tabRuntimeConnect.pass");
81
await extension.unload();
82
83
win.close();
84
});
85
</script>
86
87
</body>
88
</html>