Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Tabs runtimeConnect Test</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script type="text/javascript" src="head.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="text/javascript">
"use strict";
add_task(async function() {
const win = window.open("http://mochi.test:8888/");
const extension = ExtensionTestUtils.loadExtension({
manifest: {
"permissions": ["tabs"],
},
background: function() {
const messages_received = [];
let tabId;
browser.runtime.onConnect.addListener((port) => {
browser.test.assertTrue(!!port, "tab to background port received");
browser.test.assertEq("tab-connection-name", port.name, "port name should be defined and equal to connectInfo.name");
browser.test.assertTrue(!!port.sender.tab, "port.sender.tab should be defined");
browser.test.assertEq(tabId, port.sender.tab.id, "port.sender.tab.id should be equal to the expected tabId");
port.onMessage.addListener((msg) => {
messages_received.push(msg);
if (messages_received.length == 1) {
browser.test.assertEq("tab to background port message", msg, "'tab to background' port message received");
port.postMessage("background to tab port message");
}
if (messages_received.length == 2) {
browser.test.assertTrue(!!msg.tabReceived, "'background to tab' reply port message received");
browser.test.assertEq("background to tab port message", msg.tabReceived, "reply port content contains the message received");
browser.test.notifyPass("tabRuntimeConnect.pass");
}
});
});
browser.tabs.create({url: "tab.html"},
(tab) => { tabId = tab.id; });
},
files: {
"tab.js": function() {
const port = browser.runtime.connect({name: "tab-connection-name"});
port.postMessage("tab to background port message");
port.onMessage.addListener((msg) => {
port.postMessage({tabReceived: msg});
});
},
"tab.html": `
<!DOCTYPE html>
<html>
<head>
<title>test tab extension page</title>
<meta charset="utf-8">
<script src="tab.js" async><\/script>
</head>
<body>
<h1>test tab extension page</h1>
</body>
</html>
`,
},
});
await extension.startup();
await extension.awaitFinish("tabRuntimeConnect.pass");
await extension.unload();
win.close();
});
</script>
</body>
</html>