Source code
Revision control
Copy as Markdown
Other Tools
Test Info:
<!DOCTYPE HTML>
<html>
<head>
<title>extension.getViews 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";
function genericChecker() {
const params = new URLSearchParams(window.location.search);
const kind = params.get("kind");
const createdTabIds = [];
browser.test.onMessage.addListener(async msg => {
if (msg == `${kind}-test-get-views`) {
try {
let result = browser.extension.getViews({});
browser.test.assertEq(3, result.length, "Expect 3 extension views to be found");
result = browser.extension.getViews({ type: "tab" });
browser.test.assertEq(1, result.length, "Expect 1 tab extension view to be found");
result = browser.extension.getViews({ type: "popup" });
browser.test.assertEq(1, result.length, "Expect 1 popup extension view to be found");
browser.test.sendMessage(`${msg}:done`);
} catch (err) {
browser.test.fail(`browser.extension.getViews exception: ${err}`);
browser.test.sendMessage(`${msg}:done`);
}
} else if (msg == `${kind}-open-tab`) {
const tab = await browser.tabs.create({ url: "/page.html?kind=tab" });
createdTabIds.push(tab.id);
} else if (msg == `${kind}-close-tabs`) {
await browser.tabs.remove(createdTabIds);
browser.test.sendMessage(`${msg}:done`);
}
});
browser.test.log(`${kind} extension page loaded`);
browser.test.sendMessage(`${kind}-loaded`);
}
add_task(async function test_runtime_getContexts() {
const EXT_ID = "runtime-getContexts@mochitest";
let extension = ExtensionTestUtils.loadExtension({
useAddonManager: "temporary", // To automatically show sidebar on load.
incognitoOverride: "spanning",
manifest: {
manifest_version: 3,
browser_specific_settings: { gecko: { id: EXT_ID } },
action: {
default_popup: "page.html?kind=action",
default_area: "navbar",
},
background: {
page: "page.html?kind=background",
},
},
files: {
"page.html": `
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><\/head>
<body>
<script src="page.js"><\/script>
<\/body>
<\/html>
`,
"page.js": genericChecker,
},
});
await extension.startup();
await extension.awaitMessage("background-loaded");
extension.sendMessage("background-open-tab");
await extension.awaitMessage("tab-loaded");
await AppTestDelegate.clickBrowserAction(window, extension);
await extension.awaitMessage("action-loaded");
extension.sendMessage("background-test-get-views");
await extension.awaitMessage("background-test-get-views:done");
// Close popup window.
await AppTestDelegate.closeBrowserAction(window, extension);
// Close extension page tabs.
extension.sendMessage("background-close-tabs");
await extension.awaitMessage("background-close-tabs:done");
await extension.unload();
});
</script>
</body>
</html>