Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>Tabs update 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
async function testTabsUpdateURL(existentTabURL, tabsUpdateURL, isErrorExpected) {
16
let extension = ExtensionTestUtils.loadExtension({
17
manifest: {
18
"permissions": ["tabs"],
19
},
20
21
files: {
22
"tab.html": `
23
<!DOCTYPE html>
24
<html>
25
<head>
26
<meta charset="utf-8">
27
</head>
28
<body>
29
<h1>tab page</h1>
30
</body>
31
</html>
32
`.trim(),
33
},
34
background: function() {
35
browser.test.sendMessage("ready", browser.runtime.getURL("tab.html"));
36
37
browser.test.onMessage.addListener(async (msg, tabsUpdateURL, isErrorExpected) => {
38
let tabs = await browser.tabs.query({lastFocusedWindow: true});
39
40
try {
41
let tab = await browser.tabs.update(tabs[1].id, {url: tabsUpdateURL});
42
43
browser.test.assertFalse(isErrorExpected, `tabs.update with URL ${tabsUpdateURL} should be rejected`);
44
browser.test.assertTrue(tab, "on success the tab should be defined");
45
} catch (error) {
46
browser.test.assertTrue(isErrorExpected, `tabs.update with URL ${tabsUpdateURL} should not be rejected`);
47
browser.test.assertTrue(/^Illegal URL/.test(error.message),
48
"tabs.update should be rejected with the expected error message");
49
}
50
51
browser.test.sendMessage("done");
52
});
53
},
54
});
55
56
await extension.startup();
57
58
let mozExtTabURL = await extension.awaitMessage("ready");
59
60
if (tabsUpdateURL == "self") {
61
tabsUpdateURL = mozExtTabURL;
62
}
63
64
info(`tab.update URL "${tabsUpdateURL}" on tab with URL "${existentTabURL}"`);
65
66
let tab1 = window.open(existentTabURL);
67
68
extension.sendMessage("start", tabsUpdateURL, isErrorExpected);
69
await extension.awaitMessage("done");
70
71
tab1.close();
72
await extension.unload();
73
}
74
75
add_task(async function() {
76
info("Start testing tabs.update on javascript URLs");
77
78
let dataURLPage = `data:text/html,
79
<!DOCTYPE html>
80
<html>
81
<head>
82
<meta charset="utf-8">
83
</head>
84
<body>
85
<h1>data url page</h1>
86
</body>
87
</html>`;
88
89
let checkList = [
90
{
91
tabsUpdateURL: "http://example.net",
92
isErrorExpected: false,
93
},
94
{
95
tabsUpdateURL: "self",
96
isErrorExpected: false,
97
},
98
{
99
tabsUpdateURL: "about:addons",
100
isErrorExpected: true,
101
},
102
{
103
tabsUpdateURL: "javascript:console.log('tabs.update execute javascript')",
104
isErrorExpected: true,
105
},
106
{
107
tabsUpdateURL: dataURLPage,
108
isErrorExpected: true,
109
},
110
];
111
112
let testCases = checkList
113
.map((check) => Object.assign({}, check, {existentTabURL: "about:blank"}));
114
115
for (let {existentTabURL, tabsUpdateURL, isErrorExpected} of testCases) {
116
await testTabsUpdateURL(existentTabURL, tabsUpdateURL, isErrorExpected);
117
}
118
119
info("done");
120
});
121
</script>
122
123
</body>
124
</html>