Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>Tabs Events 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 testTabEvents() {
16
async function background() {
17
let events = [];
18
let eventPromise;
19
let checkEvents = () => {
20
if (eventPromise && events.length >= eventPromise.names.length) {
21
eventPromise.resolve();
22
}
23
};
24
25
browser.tabs.onCreated.addListener(tab => {
26
events.push({type: "onCreated", tab});
27
checkEvents();
28
});
29
30
browser.tabs.onAttached.addListener((tabId, info) => {
31
events.push(Object.assign({type: "onAttached", tabId}, info));
32
checkEvents();
33
});
34
35
browser.tabs.onDetached.addListener((tabId, info) => {
36
events.push(Object.assign({type: "onDetached", tabId}, info));
37
checkEvents();
38
});
39
40
browser.tabs.onRemoved.addListener((tabId, info) => {
41
events.push(Object.assign({type: "onRemoved", tabId}, info));
42
checkEvents();
43
});
44
45
browser.tabs.onMoved.addListener((tabId, info) => {
46
events.push(Object.assign({type: "onMoved", tabId}, info));
47
checkEvents();
48
});
49
50
async function expectEvents(names) {
51
browser.test.log(`Expecting events: ${names.join(", ")}`);
52
53
await new Promise(resolve => {
54
eventPromise = {names, resolve};
55
checkEvents();
56
});
57
58
browser.test.assertEq(names.length, events.length, "Got expected number of events");
59
for (let [i, name] of names.entries()) {
60
browser.test.assertEq(name, i in events && events[i].type,
61
`Got expected ${name} event`);
62
}
63
return events.splice(0);
64
}
65
66
try {
67
browser.test.log("Create tab");
68
let tab = await browser.tabs.create({url: "about:blank"});
69
let oldIndex = tab.index;
70
71
let [created] = await expectEvents(["onCreated"]);
72
browser.test.assertEq(tab.id, created.tab.id, "Got expected tab ID");
73
browser.test.assertEq(oldIndex, created.tab.index, "Got expected tab index");
74
75
76
browser.test.log("Remove tab");
77
await browser.tabs.remove(tab.id);
78
let [removed] = await expectEvents(["onRemoved"]);
79
80
browser.test.assertEq(tab.id, removed.tabId, "Expected removed tab ID");
81
browser.test.assertEq(tab.windowId, removed.windowId, "Expected removed tab window ID");
82
// Note: We want to test for the actual boolean value false here.
83
browser.test.assertEq(false, removed.isWindowClosing, "Expected isWindowClosing value");
84
85
browser.test.notifyPass("tabs-events");
86
} catch (e) {
87
browser.test.fail(`${e} :: ${e.stack}`);
88
browser.test.notifyFail("tabs-events");
89
}
90
}
91
92
let extension = ExtensionTestUtils.loadExtension({
93
manifest: {
94
"permissions": ["tabs"],
95
},
96
97
background,
98
});
99
100
await extension.startup();
101
await extension.awaitFinish("tabs-events");
102
await extension.unload();
103
});
104
105
add_task(async function testTabRemovalEvent() {
106
async function background() {
107
function awaitLoad(tabId) {
108
return new Promise(resolve => {
109
browser.tabs.onUpdated.addListener(function listener(tabId_, changed, tab) {
110
if (tabId == tabId_ && changed.status == "complete") {
111
browser.tabs.onUpdated.removeListener(listener);
112
resolve();
113
}
114
});
115
});
116
}
117
118
chrome.tabs.onRemoved.addListener((tabId, info) => {
119
browser.test.log("Make sure the removed tab is not available in the tabs.query callback.");
120
chrome.tabs.query({}, tabs => {
121
for (let tab of tabs) {
122
browser.test.assertTrue(tab.id != tabId, "Tab query should not include removed tabId");
123
}
124
browser.test.notifyPass("tabs-events");
125
});
126
});
127
128
try {
130
let tab = await browser.tabs.create({url: url});
131
await awaitLoad(tab.id);
132
133
await browser.tabs.remove(tab.id);
134
} catch (e) {
135
browser.test.fail(`${e} :: ${e.stack}`);
136
browser.test.notifyFail("tabs-events");
137
}
138
}
139
140
let extension = ExtensionTestUtils.loadExtension({
141
manifest: {
142
"permissions": ["tabs"],
143
},
144
145
background,
146
});
147
148
await extension.startup();
149
await extension.awaitFinish("tabs-events");
150
await extension.unload();
151
});
152
153
add_task(async function testTabActivationEvent() {
154
// TODO bug 1565536: tabs.onActivated is not supported in GeckoView.
155
if (true) {
156
todo(false, "skipping testTabActivationEvent");
157
return;
158
}
159
async function background() {
160
function makeExpectable() {
161
let expectation = null, resolver = null;
162
const expectable = param => {
163
if (expectation === null) {
164
browser.test.fail("unexpected call to expectable");
165
} else {
166
try {
167
resolver(expectation(param));
168
} catch (e) {
169
resolver(Promise.reject(e));
170
} finally {
171
expectation = null;
172
}
173
}
174
};
175
expectable.expect = e => {
176
expectation = e;
177
return new Promise(r => { resolver = r; });
178
};
179
return expectable;
180
}
181
try {
182
const listener = makeExpectable();
183
browser.tabs.onActivated.addListener(listener);
184
185
const [tab0] = await browser.tabs.query({active: true});
186
const [, tab1] = await Promise.all([
187
listener.expect(info => {
188
browser.test.assertEq(tab0.id, info.previousTabId, "Got expected previousTabId");
189
}),
190
browser.tabs.create({url: "about:blank"}),
191
]);
192
const [, tab2] = await Promise.all([
193
listener.expect(info => {
194
browser.test.assertEq(tab1.id, info.previousTabId, "Got expected previousTabId");
195
}),
196
browser.tabs.create({url: "about:blank"}),
197
]);
198
199
await Promise.all([
200
listener.expect(info => {
201
browser.test.assertEq(tab1.id, info.tabId, "Got expected tabId");
202
browser.test.assertEq(tab2.id, info.previousTabId, "Got expected previousTabId");
203
}),
204
browser.tabs.update(tab1.id, {active: true}),
205
]);
206
207
await Promise.all([
208
listener.expect(info => {
209
browser.test.assertEq(tab2.id, info.tabId, "Got expected tabId");
210
browser.test.assertEq(undefined, info.previousTabId, "previousTabId should not be defined when previous tab was closed");
211
}),
212
browser.tabs.remove(tab1.id),
213
]);
214
215
browser.tabs.onActivated.removeListener(listener);
216
await browser.tabs.remove(tab2.id);
217
218
browser.test.notifyPass("tabs-events");
219
} catch (e) {
220
browser.test.fail(`${e} :: ${e.stack}`);
221
browser.test.notifyFail("tabs-events");
222
}
223
}
224
225
let extension = ExtensionTestUtils.loadExtension({
226
manifest: {
227
"permissions": ["tabs"],
228
},
229
230
background,
231
});
232
233
await extension.startup();
234
await extension.awaitFinish("tabs-events");
235
await extension.unload();
236
});
237
</script>
238
239
</body>
240
</html>