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
|
<!DOCTYPE HTML>
<html>
<head>
<title>Test for simple WebExtension</title>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
<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(
"resource://testing-common/UrlClassifierTestUtils.sys.mjs"
);
function tp_background(expectFail = true) {
fetch("https://tracking.example.com/example.txt").then(() => {
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() {
fetch("https://tracking.example.com/example.txt").then(() => {
browser.test.notifyPass("fetch received");
}, () => {
browser.test.notifyFail("fetch failure");
});
}
let extensionData = {
manifest: {
permissions: ["*://tracking.example.com/*"],
content_scripts: [
{
"matches": ["http://mochi.test/*/file_sample.html"],
"js": ["content_script.js"],
"run_at": "document_start",
},
],
},
files: {
"content_script.js": contentScript,
},
};
const url = "http://mochi.test:8888/chrome/toolkit/components/extensions/test/mochitest/file_sample.html";
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>
|