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
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
16
17
var {PageActions} = ChromeUtils.import("resource://gre/modules/PageActions.jsm");
18
19
add_task(async function test_setPopup_and_getPopup() {
20
async function background() {
21
let tabCreatedPromise = new Promise(resolve => {
22
let onTabCreated = tab => {
23
browser.tabs.onCreated.removeListener(onTabCreated);
24
resolve();
25
};
26
browser.tabs.onCreated.addListener(onTabCreated);
27
});
28
29
async function createAndTestNewTab(expectedPopup, url) {
30
// Create a tab.
31
let [, tab] = await Promise.all([
32
tabCreatedPromise,
33
browser.tabs.create({url}),
34
]);
35
36
// Test that the default popup is returned before the popup is set for the tab.
37
let defaultPopup = await browser.pageAction.getPopup({tabId: tab.id});
38
browser.test.assertTrue(defaultPopup.includes("default.html"), "Expected the default popup to be returned");
39
40
// Set the title for the new tab and test that getTitle returns the correct title.
41
await browser.pageAction.setPopup({tabId: tab.id, popup: expectedPopup});
42
let actualPopup = await browser.pageAction.getPopup({tabId: tab.id});
43
browser.test.assertTrue(actualPopup.includes(expectedPopup), "Expected the new tab popup to be returned");
44
45
return tab;
46
}
47
48
// Create and test 2 new tabs.
49
let tab1 = await createAndTestNewTab("a.html", "about:blank");
50
let tab2 = await createAndTestNewTab("b.html", "about:blank");
51
52
// Check all of the created tabs now.
53
let popup = await browser.pageAction.getPopup({tabId: tab1.id});
54
browser.test.assertTrue(popup.includes("a.html"), "Expected the first tab popup");
55
popup = await browser.pageAction.getPopup({tabId: tab2.id});
56
browser.test.assertTrue(popup.includes("b.html"), "Expected the second tab popup");
57
58
// Unset the popup for the first tab and confirm that it is unset.
59
browser.pageAction.setPopup({tabId: tab1.id, popup: ""});
60
popup = await browser.pageAction.getPopup({tabId: tab1.id});
61
browser.test.assertTrue(popup.includes("default.html"), "Expected the default popup to be returned");
62
63
// Set the popup for the first tab.
64
browser.pageAction.setPopup({tabId: tab1.id, popup: "a.html"});
65
popup = await browser.pageAction.getPopup({tabId: tab1.id});
66
browser.test.assertTrue(popup.includes("a.html"), "Expected the first tab popup");
67
68
// Keeps track of the tabs for which onClicked should fire when the page action is clicked on.
69
let expectingOnClicked = {};
70
71
browser.pageAction.onClicked.addListener(tab => {
72
browser.test.assertTrue(expectingOnClicked[tab.id], "The onClicked listener should only fire when we expect it to.");
73
browser.test.sendMessage("page-action-onClicked-fired");
74
});
75
76
browser.test.onMessage.addListener(async (msg, data) => {
77
if (msg === "select-tab") {
78
// Check if the requested tabId is already selected.
79
const [activeTab] = await browser.tabs.query({active: true});
80
if (activeTab.id === data.tabId) {
81
browser.test.sendMessage("tab-selected");
82
return;
83
}
84
85
// Select the requested tabId and wait the tab to be activated.
86
const onActivatedListener = ({tabId}) => {
87
if (tabId === data.tabId) {
88
browser.tabs.onActivated.removeListener(onActivatedListener);
89
browser.test.sendMessage("tab-selected");
90
}
91
};
92
browser.tabs.onActivated.addListener(onActivatedListener);
93
94
await browser.tabs.update(data.tabId, {active: true});
95
} else if (msg === "page-action-show") {
96
await browser.pageAction.show(data.tabId);
97
browser.test.sendMessage("page-action-shown");
98
} else if (msg == "page-action-set-popup") {
99
if (data.popup == "") {
100
expectingOnClicked[data.tabId] = true;
101
} else {
102
delete expectingOnClicked[data.tabId];
103
}
104
await browser.pageAction.setPopup({tabId: data.tabId, popup: data.popup});
105
browser.test.sendMessage("page-action-popup-set");
106
} else if (msg == "page-action-get-popup") {
107
const url = await browser.pageAction.getPopup({tabId: data.tabId});
108
browser.test.sendMessage("page-action-got-popup", url);
109
} else if (msg === "finish") {
110
await browser.tabs.remove([tab1.id, tab2.id]);
111
browser.test.notifyPass("page-action-popup");
112
}
113
});
114
115
browser.test.sendMessage("tabs", {tab1, tab2});
116
}
117
118
function popupScript() {
119
window.onload = () => {
120
browser.test.sendMessage("page-action-from-popup", location.href);
121
};
122
123
browser.test.onMessage.addListener((msg, details) => {
124
if (msg == "page-action-close-popup") {
125
if (details.location == location.href) {
126
window.close();
127
}
128
}
129
});
130
}
131
132
let extension = ExtensionTestUtils.loadExtension({
133
background,
134
manifest: {
135
"name": "PageAction Extension",
136
"page_action": {
137
"default_title": "Page Action",
138
"default_popup": "default.html",
139
"default_icon": {
140
"18": "extension.png",
141
},
142
},
143
},
144
files: {
145
"default.html": `<html><head><meta charset="utf-8"><script src="popup.js"><\/script></head></html>`,
146
"extension.png": TEST_ICON_ARRAYBUFFER,
147
"a.html": `<html><head><meta charset="utf-8"><script src="popup.js"><\/script></head></html>`,
148
"b.html": `<html><head><meta charset="utf-8"><script src="popup.js"><\/script></head></html>`,
149
"popup.js": popupScript,
150
},
151
});
152
153
let tabClosedPromise = () => {
154
return new Promise(resolve => {
155
let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
156
let BrowserApp = chromeWin.BrowserApp;
157
158
let tabCloseListener = (event) => {
159
BrowserApp.deck.removeEventListener("TabClose", tabCloseListener);
160
let browser = event.target;
161
let url = browser.currentURI.spec;
162
resolve(url);
163
};
164
165
BrowserApp.deck.addEventListener("TabClose", tabCloseListener);
166
});
167
};
168
169
async function testPopup(tabId, expectedPopup, uuid) {
170
extension.sendMessage("page-action-get-popup", {tabId});
171
let actualPopup = await extension.awaitMessage("page-action-got-popup");
172
ok(actualPopup.includes(expectedPopup), `Expected the correct popup for tab ${tabId}`);
173
174
extension.sendMessage("select-tab", {tabId});
175
await extension.awaitMessage("tab-selected");
176
177
extension.sendMessage("page-action-show", {tabId});
178
await extension.awaitMessage("page-action-shown");
179
180
ok(PageActions.isShown(uuid), "page action is shown");
181
182
info(`Click on the pageAction on tab ${tabId} and wait the popup to be loaded`);
183
PageActions.synthesizeClick(uuid);
184
let location = await extension.awaitMessage("page-action-from-popup");
185
186
ok(location.includes(expectedPopup), "The popup with the correct URL should be shown.");
187
188
const onceTabClosed = tabClosedPromise();
189
extension.sendMessage("page-action-close-popup", {location});
190
location = await onceTabClosed;
191
ok(location.includes(expectedPopup), "The popup with the correct URL should be closed");
192
}
193
194
await extension.startup();
195
196
let {tab1, tab2} = await extension.awaitMessage("tabs");
197
198
const uuid = `{${extension.uuid}}`;
199
await testPopup(tab1.id, "a.html", uuid);
200
await testPopup(tab2.id, "b.html", uuid);
201
202
// Test that the default popup is used when the first tabs popup is unset.
203
extension.sendMessage("page-action-set-popup", {tabId: tab1.id, popup: ""});
204
await extension.awaitMessage("page-action-popup-set");
205
206
await testPopup(tab1.id, "default.html", uuid);
207
208
extension.sendMessage("finish");
209
await extension.awaitFinish("page-action-popup");
210
211
await extension.unload();
212
});
213
214
215
</script>
216
217
</body>
218
</html>