Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>Tabs executeScript 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 testExecuteScript() {
16
let win = window.open("http://mochi.test:8888/");
17
18
async function background() {
19
let tasks = [
20
{
21
background: "rgba(0, 0, 0, 0)",
22
foreground: "rgb(0, 113, 4)",
23
promise: () => {
24
return browser.tabs.insertCSS({
25
file: "file2.css",
26
});
27
},
28
},
29
{
30
background: "rgb(42, 42, 42)",
31
foreground: "rgb(0, 113, 4)",
32
promise: () => {
33
return browser.tabs.insertCSS({
34
code: "* { background: rgb(42, 42, 42) }",
35
});
36
},
37
},
38
{
39
background: "rgb(42, 42, 42)",
40
foreground: "rgb(0, 113, 4)",
41
promise: () => {
42
return browser.tabs.insertCSS({
43
code: "* { background: rgb(100, 100, 100) !important }",
44
cssOrigin: "author",
45
}).then(r => browser.tabs.insertCSS({
46
code: "* { background: rgb(42, 42, 42) !important }",
47
cssOrigin: "author",
48
}));
49
},
50
},
51
{
52
background: "rgb(100, 100, 100)",
53
foreground: "rgb(0, 113, 4)",
54
promise: () => {
55
// User has higher importance
56
return browser.tabs.insertCSS({
57
code: "* { background: rgb(100, 100, 100) !important }",
58
cssOrigin: "user",
59
}).then(r => browser.tabs.insertCSS({
60
code: "* { background: rgb(42, 42, 42) !important }",
61
cssOrigin: "author",
62
}));
63
},
64
},
65
];
66
67
function checkCSS() {
68
let computedStyle = window.getComputedStyle(document.body);
69
return [computedStyle.backgroundColor, computedStyle.color];
70
}
71
72
try {
73
for (let {promise, background, foreground} of tasks) {
74
let result = await promise();
75
76
browser.test.assertEq(undefined, result, "Expected callback result");
77
78
[result] = await browser.tabs.executeScript({
79
code: `(${checkCSS})()`,
80
});
81
82
browser.test.assertEq(background, result[0], "Expected background color");
83
browser.test.assertEq(foreground, result[1], "Expected foreground color");
84
}
85
86
browser.test.notifyPass("insertCSS");
87
} catch (e) {
88
browser.test.fail(`Error: ${e} :: ${e.stack}`);
89
browser.test.notifyFailure("insertCSS");
90
}
91
}
92
93
let extension = ExtensionTestUtils.loadExtension({
94
manifest: {
95
"permissions": ["http://mochi.test/"],
96
},
97
98
background,
99
100
files: {
101
"file2.css": "* { color: rgb(0, 113, 4) }",
102
},
103
});
104
105
await extension.startup();
106
107
await extension.awaitFinish("insertCSS");
108
109
await extension.unload();
110
111
win.close();
112
});
113
</script>
114
115
</body>
116
</html>