Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!DOCTYPE HTML>
<html>
<head>
<title>Test for simple WebExtension</title>
<script type="text/javascript" src="head.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="text/javascript">
"use strict";
var {UrlClassifierTestUtils} = ChromeUtils.importESModule(
);
function tp_background(expectFail = true) {
browser.test.assertTrue(!expectFail, "fetch received");
browser.test.sendMessage("done");
}, () => {
browser.test.assertTrue(expectFail, "fetch failure");
browser.test.sendMessage("done");
});
}
async function test_permission(permissions, expectFail) {
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions,
},
background: `(${tp_background})(${expectFail})`,
});
await extension.startup();
await extension.awaitMessage("done");
await extension.unload();
}
add_task(async function setup() {
await UrlClassifierTestUtils.addTestTrackers();
await SpecialPowers.pushPrefEnv({
set: [["privacy.trackingprotection.enabled", true]],
});
});
// Fetch would be blocked with these tests
add_task(async function() { await test_permission([], true); });
add_task(async function() { await test_permission(["http://*/"], true); });
add_task(async function() { await test_permission(["http://*.example.com/"], true); });
add_task(async function() { await test_permission(["http://localhost/*"], true); });
// Fetch will not be blocked if the extension has host permissions.
add_task(async function() { await test_permission(["<all_urls>"], false); });
add_task(async function() { await test_permission(["*://tracking.example.com/*"], false); });
add_task(async function test_contentscript() {
function contentScript() {
browser.test.notifyPass("fetch received");
}, () => {
browser.test.notifyFail("fetch failure");
});
}
let extensionData = {
manifest: {
permissions: ["*://tracking.example.com/*"],
content_scripts: [
{
"js": ["content_script.js"],
"run_at": "document_start",
},
],
},
files: {
"content_script.js": contentScript,
},
};
let extension = ExtensionTestUtils.loadExtension(extensionData);
await extension.startup();
let win = window.open(url);
await extension.awaitFinish();
win.close();
await extension.unload();
});
add_task(async function teardown() {
UrlClassifierTestUtils.cleanupTestTrackers();
});
</script>
</body>
</html>