Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>Tabs executeScript Bad 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
function* testHasNoPermission(params) {
16
let contentSetup = params.contentSetup || (() => Promise.resolve());
17
18
async function background(contentSetup) {
19
browser.test.onMessage.addListener(async msg => {
20
browser.test.assertEq(msg, "execute-script");
21
22
await browser.test.assertRejects(browser.tabs.executeScript({
23
file: "script.js",
24
}), /Missing host permission for the tab/);
25
26
browser.test.notifyPass("executeScript");
27
});
28
29
await contentSetup();
30
31
browser.test.sendMessage("ready");
32
}
33
34
let extension = ExtensionTestUtils.loadExtension({
35
manifest: params.manifest,
36
37
background: `(${background})(${contentSetup})`,
38
39
files: {
40
"script.js": function() {
41
browser.runtime.sendMessage("first script ran");
42
},
43
},
44
});
45
46
yield extension.startup();
47
yield extension.awaitMessage("ready");
48
49
if (params.setup) {
50
yield params.setup(extension);
51
}
52
53
extension.sendMessage("execute-script");
54
55
yield extension.awaitFinish("executeScript");
56
yield extension.unload();
57
}
58
59
add_task(async function testBadPermissions() {
60
let win = window.open("http://mochi.test:8888/");
61
62
await new Promise(resolve => setTimeout(resolve, 0));
63
64
info("Test no special permissions");
65
await testHasNoPermission({
66
manifest: {"permissions": []},
67
});
68
69
info("Test tabs permissions");
70
await testHasNoPermission({
71
manifest: {"permissions": ["tabs"]},
72
});
73
74
win.close();
75
});
76
77
add_task(async function testBadURL() {
78
async function background() {
79
let promises = [
80
new Promise(resolve => {
81
browser.tabs.executeScript({
83
}, result => {
84
browser.test.assertEq(undefined, result, "Result value");
85
86
browser.test.assertTrue(browser.extension.lastError instanceof Error,
87
"runtime.lastError is Error");
88
89
browser.test.assertTrue(browser.runtime.lastError instanceof Error,
90
"runtime.lastError is Error");
91
92
browser.test.assertEq(
93
"Files to be injected must be within the extension",
94
browser.extension.lastError && browser.extension.lastError.message,
95
"extension.lastError value");
96
97
browser.test.assertEq(
98
"Files to be injected must be within the extension",
99
browser.runtime.lastError && browser.runtime.lastError.message,
100
"runtime.lastError value");
101
102
resolve();
103
});
104
}),
105
106
browser.tabs.executeScript({
108
}).catch(error => {
109
browser.test.assertTrue(error instanceof Error, "Error is Error");
110
111
browser.test.assertEq(null, browser.extension.lastError,
112
"extension.lastError value");
113
114
browser.test.assertEq(null, browser.runtime.lastError,
115
"runtime.lastError value");
116
117
browser.test.assertEq(
118
"Files to be injected must be within the extension",
119
error && error.message,
120
"error value");
121
}),
122
];
123
124
await Promise.all(promises);
125
126
browser.test.notifyPass("executeScript-lastError");
127
}
128
129
let extension = ExtensionTestUtils.loadExtension({
130
manifest: {
131
"permissions": ["<all_urls>"],
132
},
133
134
background,
135
});
136
137
await extension.startup();
138
139
await extension.awaitFinish("executeScript-lastError");
140
141
await extension.unload();
142
});
143
144
// TODO bug 1435100: Test that |executeScript| fails if the tab has navigated
145
// to a new page, and no longer matches our expected state. This involves
146
// intentionally trying to trigger a race condition.
147
</script>
148
149
</body>
150
</html>