Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>Tabs create 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() {
16
let extension = ExtensionTestUtils.loadExtension({
17
manifest: {
18
"permissions": ["tabs"],
19
20
"background": {"page": "bg/background.html"},
21
},
22
23
files: {
24
"bg/blank.html": `<html><head><meta charset="utf-8"></head></html>`,
25
26
"bg/background.html": `<html><head>
27
<meta charset="utf-8">
28
<script src="background.js"><\/script>
29
</head></html>`,
30
31
"bg/background.js": function() {
32
let activeTab;
33
let activeWindow;
34
35
function runTests() {
36
const DEFAULTS = {
37
windowId: activeWindow,
38
active: true,
39
url: "about:blank",
40
};
41
42
let tests = [
43
{
44
create: {url: "http://example.com/"},
45
result: {url: "http://example.com/"},
46
},
47
{
48
create: {url: "blank.html"},
49
result: {url: browser.runtime.getURL("bg/blank.html")},
50
},
51
{
52
create: {},
53
},
54
{
55
create: {active: false},
56
result: {active: false},
57
},
58
{
59
create: {active: true},
60
result: {active: true},
61
},
62
];
63
64
async function nextTest() {
65
if (!tests.length) {
66
browser.test.notifyPass("tabs.create");
67
return;
68
}
69
70
let test = tests.shift();
71
let expected = Object.assign({}, DEFAULTS, test.result);
72
73
browser.test.log(`Testing tabs.create(${JSON.stringify(test.create)}), expecting ${JSON.stringify(test.result)}`);
74
75
let updatedPromise = new Promise(resolve => {
76
let onUpdated = (changedTabId, changed) => {
77
if (changed.url) {
78
browser.tabs.onUpdated.removeListener(onUpdated);
79
resolve({tabId: changedTabId, url: changed.url});
80
}
81
};
82
browser.tabs.onUpdated.addListener(onUpdated);
83
});
84
85
let createdPromise = new Promise(resolve => {
86
let onCreated = tab => {
87
browser.test.assertTrue("id" in tab, `Expected tabs.onCreated callback to receive tab object`);
88
resolve();
89
};
90
browser.tabs.onCreated.addListener(onCreated);
91
});
92
93
let [tab] = await Promise.all([
94
browser.tabs.create(test.create),
95
createdPromise,
96
]);
97
let tabId = tab.id;
98
99
for (let key of Object.keys(expected)) {
100
if (key === "url") {
101
// FIXME: This doesn't get updated until later in the load cycle.
102
continue;
103
}
104
105
browser.test.assertEq(expected[key], tab[key], `Expected value for tab.${key}`);
106
}
107
108
let updated = await updatedPromise;
109
browser.test.assertEq(tabId, updated.tabId, `Expected value for tab.id`);
110
browser.test.assertEq(expected.url, updated.url, `Expected value for tab.url`);
111
112
await browser.tabs.remove(tabId);
113
await browser.tabs.update(activeTab, {active: true});
114
115
nextTest();
116
}
117
118
nextTest();
119
}
120
121
browser.tabs.query({active: true, currentWindow: true}, tabs => {
122
activeTab = tabs[0].id;
123
activeWindow = tabs[0].windowId;
124
125
runTests();
126
});
127
},
128
},
129
});
130
131
await extension.startup();
132
await extension.awaitFinish("tabs.create");
133
await extension.unload();
134
});
135
</script>
136
137
</body>
138
</html>