Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>PageAction Test</title>
7
<script type="text/javascript" src="head.js"></script>
8
<link rel="stylesheet" href="chrome://mochikit/contents/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
function promiseDispatchedWindowEvent(eventName) {
19
return new Promise(resolve => {
20
let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
21
let WindowEventDispatcher = chromeWin.WindowEventDispatcher;
22
23
let listener = (event) => {
24
WindowEventDispatcher.unregisterListener(listener, eventName);
25
resolve();
26
};
27
28
WindowEventDispatcher.registerListener(listener, eventName);
29
});
30
}
31
32
async function closeTabAndWaitTabClosed({BrowserApp, tab}) {
33
let onceTabClosed = promiseDispatchedWindowEvent("Tab:Closed");
34
BrowserApp.closeTab(tab);
35
await onceTabClosed;
36
}
37
38
async function selectTabAndWaitTabSelected({BrowserApp, tab}) {
39
let onceTabSelected = promiseDispatchedWindowEvent("Tab:Selected");
40
BrowserApp.selectTab(tab);
41
await onceTabSelected;
42
}
43
44
add_task(async function test_popup_behavior() {
45
const chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
46
const BrowserApp = chromeWin.BrowserApp;
47
48
async function background() {
49
const tab1 = await browser.tabs.create({url: "http://example.com#test_popup_behavior_1"});
50
const tab2 = await browser.tabs.create({url: "http://example.com#test_popup_behavior_2"});
51
const tab3 = await browser.tabs.create({url: "http://example.com#test_popup_behavior_3"});
52
53
browser.tabs.onUpdated.addListener((tabId, changeInfo) => {
54
if (![tab1.id, tab2.id, tab3.id].includes(tabId) ||
55
changeInfo.status !== "complete") {
56
return;
57
}
58
59
browser.test.sendMessage("page-loaded", tabId);
60
});
61
62
browser.test.sendMessage("background_page.ready", {
63
tabId1: tab1.id,
64
tabId2: tab2.id,
65
tabId3: tab3.id,
66
});
67
}
68
69
async function popupScript() {
70
browser.test.sendMessage("popup_script.loaded");
71
}
72
73
let popupHtml = `<!DOCTYPE html>
74
<html>
75
<head>
76
<meta charset="utf-8">
77
</head>
78
<body>
79
<h1>Extension Popup</h1>
80
<script src="popup.js"><\/script>
81
</body>
82
</html>
83
`;
84
85
let extension = ExtensionTestUtils.loadExtension({
86
background,
87
manifest: {
88
"name": "BrowserAction Extension",
89
"browser_action": {
90
"default_title": "Browser Action",
91
"default_popup": "popup.html",
92
"default_icon": {
93
"18": "extension.png",
94
},
95
},
96
"permissions": ["activeTab"],
97
},
98
files: {
99
"popup.html": popupHtml,
100
"popup.js": popupScript,
101
},
102
});
103
104
await extension.startup();
105
106
const {
107
tabId1,
108
tabId2,
109
tabId3,
110
} = await extension.awaitMessage("background_page.ready");
111
112
const uuid = `{${extension.uuid}}`;
113
114
ok(BrowserActions.isShown(uuid), "browser action is shown");
115
116
info("Wait the new tabs to be loaded");
117
118
await extension.awaitMessage("page-loaded");
119
await extension.awaitMessage("page-loaded");
120
await extension.awaitMessage("page-loaded");
121
122
is(BrowserApp.selectedTab.id, tabId3, "The third of the new tabs has been selected");
123
124
BrowserActions.synthesizeClick(uuid);
125
await extension.awaitMessage("popup_script.loaded");
126
127
// Check that while the extension popup tab is selected the active tab is still the tab
128
// from which the user has opened the extension popup.
129
ok(BrowserApp.selectedBrowser.currentURI.spec.endsWith("popup.html"),
130
"The first popup tab has been selected");
131
132
let popupParentTabId = BrowserApp.selectedTab.parentId;
133
is(popupParentTabId, tabId3, "The parent of the first popup tab is the third tab");
134
135
// Close the popup and test that its parent tab is selected.
136
await closeTabAndWaitTabClosed({BrowserApp, tab: BrowserApp.selectedTab});
137
138
const tab1 = BrowserApp.getTabForId(tabId1);
139
const tab2 = BrowserApp.getTabForId(tabId2);
140
const tab3 = BrowserApp.getTabForId(tabId3);
141
142
BrowserActions.synthesizeClick(uuid);
143
await extension.awaitMessage("popup_script.loaded");
144
145
ok(BrowserApp.selectedBrowser.currentURI.spec.endsWith("popup.html"),
146
"The second popup tab has been selected");
147
148
popupParentTabId = BrowserApp.selectedTab.parentId;
149
is(popupParentTabId, tabId3, "The parent of the second popup tab is the third tab");
150
151
// Switch to the second tab and expect the popup tab to be closed.
152
let onceTabClosed = promiseDispatchedWindowEvent("Tab:Closed");
153
await Promise.all([
154
selectTabAndWaitTabSelected({BrowserApp, tab: tab2}),
155
onceTabClosed,
156
]);
157
158
// Switch to the first tab and expect it to be the next selected tab.
159
// (which ensure that Bug 1373170 has been fixed and the closed popup tab
160
// has not selected its parent tab).
161
await selectTabAndWaitTabSelected({BrowserApp, tab: tab1});
162
163
is(BrowserApp.selectedTab.id, tabId1,
164
"The first tab is the currently selected tab");
165
166
// Close all the tabs before exiting the test.
167
await Promise.all([
168
closeTabAndWaitTabClosed({BrowserApp, tab: tab1}),
169
closeTabAndWaitTabClosed({BrowserApp, tab: tab2}),
170
closeTabAndWaitTabClosed({BrowserApp, tab: tab3}),
171
]);
172
173
await extension.unload();
174
});
175
176
add_task(async function test_popup_incognito() {
177
const chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
178
const BrowserApp = chromeWin.BrowserApp;
179
const extension = ExtensionTestUtils.loadExtension({
180
background() {
181
browser.tabs.onCreated.addListener(async (tab) => {
182
browser.test.sendMessage("page-loaded", tab);
183
});
184
},
185
manifest: {
186
"name": "BrowserAction Extension",
187
"browser_action": {
188
"default_popup": "popup.html",
189
},
190
},
191
files: {
192
"popup.html": "<h1>Extension Popup</h1>",
193
},
194
});
195
196
await extension.startup();
197
const uuid = `{${extension.uuid}}`;
198
ok(BrowserActions.isShown(uuid), "Browser action should be shown");
199
200
for (let isPrivate of [false, true]) {
201
// Open tab with provided privateness
202
let nativeTab = BrowserApp.addTab("http://example.com/#normal", {
203
isPrivate,
204
selected: true,
205
});
206
let tab = await extension.awaitMessage("page-loaded");
207
is(tab.incognito, isPrivate, "Tab should have expected privateness");
208
209
// Open browserAction popup in that tab
210
BrowserActions.synthesizeClick(uuid);
211
let popup = await extension.awaitMessage("page-loaded");
212
is(popup.incognito, isPrivate, "Popup should have same privateness as tab");
213
214
// Close popup and tab
215
await closeTabAndWaitTabClosed({BrowserApp, tab: BrowserApp.selectedTab});
216
await closeTabAndWaitTabClosed({BrowserApp, tab: nativeTab});
217
}
218
219
await extension.unload();
220
});
221
</script>
222
223
</body>
224
</html>