Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>BrowserAction 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 {ContentTaskUtils} = SpecialPowers.Cu.import("resource://testing-common/ContentTaskUtils.jsm", {});
17
var {Services} = SpecialPowers.Cu.import("resource://gre/modules/Services.jsm", {});
18
19
function createPopupHTML({text, js}) {
20
return `<!DOCTYPE html>
21
<html>
22
<head>
23
<meta charset="utf-8">
24
<title>${text}</title>
25
</head>
26
<body>
27
<h1>${text}</h1>
28
<script src="${js}"><\/script>
29
</body>
30
</html>
31
`;
32
}
33
34
async function ensureTabSelected(nativeTab) {
35
const {BrowserApp} = Services.wm.getMostRecentWindow("navigator:browser");
36
37
BrowserApp.selectTab(nativeTab);
38
39
await ContentTaskUtils.waitForCondition(() => {
40
return nativeTab.getActive();
41
});
42
}
43
44
add_task(async function test_browserAction_setPopup_and_getPopup() {
45
const DEFAULT_POPUP = "/default_popup.html";
46
const CUSTOM_POPUP_1 = "/custom_popup_1.html";
47
const CUSTOM_POPUP_2 = "/custom_popup_2.html";
48
49
const {BrowserApp} = Services.wm.getMostRecentWindow("navigator:browser");
50
const initialTab = BrowserApp.selectedTab;
51
52
const tab1 = BrowserApp.addTab("about:blank#tab1", {parentId: initialTab.id});
53
const tab2 = BrowserApp.addTab("about:blank#tab2", {parentId: initialTab.id});
54
const tab3 = BrowserApp.addTab("about:blank#tab3", {parentId: initialTab.id});
55
56
function background() {
57
async function handleBrowserActionSetPopup({tabId, popup}) {
58
await browser.browserAction.setPopup({tabId, popup});
59
60
browser.test.sendMessage("browserAction-setPopup:done", {tabId, popup});
61
}
62
63
async function handleBrowserActionGetPopup({tabId}) {
64
const popup = await browser.browserAction.getPopup({tabId});
65
66
const popupURL = new URL(popup);
67
68
browser.test.sendMessage("browserAction-getPopup:done", {
69
tabId,
70
popup: popupURL.pathname,
71
});
72
}
73
74
browser.test.onMessage.addListener((msg, args) => {
75
switch (msg) {
76
case "browserAction-setPopup":
77
handleBrowserActionSetPopup(args);
78
break;
79
case "browserAction-getPopup":
80
handleBrowserActionGetPopup(args);
81
break;
82
default:
83
browser.test.fail(`Unexpected test message received: ${msg}`);
84
}
85
});
86
87
browser.test.sendMessage("background-ready");
88
}
89
90
function popupScript() {
91
browser.test.sendMessage("popup-loaded", {popup: window.location.pathname});
92
93
window.close();
94
}
95
96
const extension = ExtensionTestUtils.loadExtension({
97
background,
98
manifest: {
99
"browser_action": {
100
"default_popup": "default_popup.html",
101
"default_title": "BrowserAction title",
102
},
103
},
104
files: {
105
"default_popup.html": createPopupHTML({text: "Default Popup", js: "popup.js"}),
106
"custom_popup_1.html": createPopupHTML({text: "Custom Popup 1", js: "popup.js"}),
107
"custom_popup_2.html": createPopupHTML({text: "Custom Popup 2", js: "popup.js"}),
108
"popup.js": popupScript,
109
},
110
});
111
112
await extension.startup();
113
114
const uuid = `{${extension.uuid}}`;
115
116
await extension.awaitMessage("background-ready");
117
118
// Test that the browserAction popup is currently the default popup for all the opened tabs.
119
120
for (const tab of [tab1, tab2, tab3]) {
121
extension.sendMessage("browserAction-getPopup", {tabId: tab.id});
122
123
const res = await extension.awaitMessage("browserAction-getPopup:done");
124
125
isDeeply(res, {tabId: tab.id, popup: DEFAULT_POPUP},
126
"All the tabs should have been associated the same default browserAction popup");
127
}
128
129
// Customize the popup for the first two tabs and checks that getPopup return the expected popup.
130
131
extension.sendMessage("browserAction-setPopup", {
132
tabId: tab1.id,
133
popup: CUSTOM_POPUP_1,
134
});
135
136
await extension.awaitMessage("browserAction-setPopup:done");
137
138
extension.sendMessage("browserAction-setPopup", {
139
tabId: tab2.id,
140
popup: CUSTOM_POPUP_2,
141
});
142
143
await extension.awaitMessage("browserAction-setPopup:done");
144
145
extension.sendMessage("browserAction-getPopup", {tabId: tab1.id});
146
const resTab1 = await extension.awaitMessage("browserAction-getPopup:done");
147
isDeeply(resTab1, {tabId: tab1.id, popup: CUSTOM_POPUP_1},
148
"The first tab should have been associated to the custom popup 1");
149
150
extension.sendMessage("browserAction-getPopup", {tabId: tab2.id});
151
const resTab2 = await extension.awaitMessage("browserAction-getPopup:done");
152
isDeeply(resTab2, {tabId: tab2.id, popup: CUSTOM_POPUP_2},
153
"The second tab should have been associated to the custom popup 2");
154
155
extension.sendMessage("browserAction-getPopup", {tabId: tab3.id});
156
const resTab3 = await extension.awaitMessage("browserAction-getPopup:done");
157
isDeeply(resTab3, {tabId: tab3.id, popup: DEFAULT_POPUP},
158
"The third tab should still be associated to the default popup");
159
160
// Test browserAction popup opened by clicking on the browserAction.
161
162
await ensureTabSelected(tab1);
163
BrowserActions.synthesizeClick(uuid);
164
const popupLoadedTab1 = await extension.awaitMessage("popup-loaded");
165
isDeeply(popupLoadedTab1, {popup: CUSTOM_POPUP_1},
166
"The expected custom popup has been opened for the first tab");
167
168
await ensureTabSelected(tab2);
169
BrowserActions.synthesizeClick(uuid);
170
const popupLoadedTab2 = await extension.awaitMessage("popup-loaded");
171
isDeeply(popupLoadedTab2, {popup: CUSTOM_POPUP_2},
172
"The expected custom popup has been opened for the second tab");
173
174
await ensureTabSelected(tab3);
175
BrowserActions.synthesizeClick(uuid);
176
const popupLoadedTab3 = await extension.awaitMessage("popup-loaded");
177
isDeeply(popupLoadedTab3, {popup: DEFAULT_POPUP},
178
"The expected default popup has been opened for the third tab");
179
180
// Cleanup the browser before exiting.
181
182
BrowserApp.closeTab(tab1);
183
BrowserApp.closeTab(tab2);
184
BrowserApp.closeTab(tab3);
185
186
await extension.unload();
187
});
188
</script>
189
190
</body>
191
</html>