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
var {BrowserActions} = SpecialPowers.Cu.import("resource://gre/modules/BrowserActions.jsm", {});
16
var {Services} = SpecialPowers.Cu.import("resource://gre/modules/Services.jsm", {});
17
18
add_task(async function test_query_highlighted() {
19
if (true) {
20
// GeckoView does not support extension popups
21
todo(false, 'skipping test_query_highlighted');
22
return;
23
}
24
let extension = ExtensionTestUtils.loadExtension({
25
manifest: {
26
"permissions": ["tabs"],
27
"browser_action": {
28
"default_popup": "popup.html",
29
},
30
},
31
32
background: async function() {
33
let tabs1 = await browser.tabs.query({highlighted: false});
34
browser.test.assertEq(3, tabs1.length, "should have three non-highlighted tabs");
35
36
let tabs2 = await browser.tabs.query({highlighted: true});
37
browser.test.assertEq(1, tabs2.length, "should have one highlighted tab");
38
39
for (let tab of [...tabs1, ...tabs2]) {
40
browser.test.assertEq(tab.active, tab.highlighted, "highlighted and active are equal in tab " + tab.index);
41
}
42
43
browser.test.notifyPass("tabs.query");
44
},
45
46
files: {
47
"popup.html": `<script src="popup.js"><\/script>`,
48
"popup.js": async function popupScript() {
49
let active = await browser.tabs.query({active: true});
50
let highlighted = await browser.tabs.query({highlighted: true});
51
52
browser.test.assertEq(1, active.length, "should have one active tab");
53
browser.test.assertEq(1, highlighted.length, "should have one highlighted tab");
54
browser.test.assertEq(active[0].id, highlighted[0].id, "the active and highlighted tabs are the same one");
55
browser.test.assertEq(true, active[0].active, "the tab should be considered to be active");
56
browser.test.assertEq(true, active[0].highlighted, "the tab should be considered to be highlighted");
57
58
browser.test.sendMessage("tabs.query.popup");
59
},
60
},
61
});
62
63
const {BrowserApp} = Services.wm.getMostRecentWindow("navigator:browser");
64
let tabs = [];
66
tabs.push(BrowserApp.addTab(url));
67
}
68
69
await extension.startup();
70
await extension.awaitFinish("tabs.query");
71
72
// Open popup
73
BrowserActions.synthesizeClick(`{${extension.uuid}}`);
74
await extension.awaitMessage("tabs.query.popup");
75
76
await extension.unload();
77
78
for (let tab of tabs) {
79
BrowserApp.closeTab(tab);
80
}
81
});
82
83
add_task(async function test_query_index() {
84
let extension = ExtensionTestUtils.loadExtension({
85
manifest: {
86
"permissions": ["tabs"],
87
},
88
89
background: function() {
90
browser.tabs.onCreated.addListener(async function({index, windowId, id}) {
91
browser.test.assertThrows(
92
() => browser.tabs.query({index: -1}),
93
/-1 is too small \(must be at least 0\)/,
94
"tab indices must be non-negative");
95
96
let tabs = await browser.tabs.query({index, windowId});
97
browser.test.assertEq(tabs.length, 1, `Got one tab at index ${index}`);
98
browser.test.assertEq(tabs[0].id, id, "The tab is the right one");
99
100
tabs = await browser.tabs.query({index: 1e5, windowId});
101
browser.test.assertEq(tabs.length, 0, "There is no tab at this index");
102
103
browser.test.notifyPass("tabs.query");
104
});
105
},
106
});
107
108
await extension.startup();
109
const win = window.open("http://example.com");
110
await extension.awaitFinish("tabs.query");
111
win.close();
112
await extension.unload();
113
});
114
</script>
115
116
</body>
117
</html>