1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
import { Assert } from "resource://testing-common/Assert.sys.mjs";
import { BrowserTestUtils } from "resource://testing-common/BrowserTestUtils.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
CustomizableUI: "resource:///modules/CustomizableUI.sys.mjs",
});
async function promiseAnimationFrame(window) {
await new Promise(resolve => window.requestAnimationFrame(resolve));
let { tm } = Services;
return new Promise(resolve => tm.dispatchToMainThread(resolve));
}
function makeWidgetId(id) {
id = id.toLowerCase();
return id.replace(/[^a-z0-9_-]/g, "_");
}
async function getPageActionButtonId(window, extensionId) {
// This would normally be set automatically on navigation, and cleared
// when the user types a value into the URL bar, to show and hide page
// identity info and icons such as page action buttons.
//
// Unfortunately, that doesn't happen automatically in browser chrome
// tests.
window.gURLBar.setPageProxyState("valid");
let { gIdentityHandler } = window.gBrowser.ownerGlobal;
// If the current tab is blank and the previously selected tab was an internal
// page, the urlbar will now be showing the internal identity box due to the
// setPageProxyState call above. The page action button is hidden in that
// case, so make sure we're not showing the internal identity box.
gIdentityHandler._identityBox.classList.remove("chromeUI");
await promiseAnimationFrame(window);
return window.BrowserPageActions.urlbarButtonNodeIDForActionID(
makeWidgetId(extensionId)
);
}
async function getPageActionButton(window, extensionId) {
let pageActionId = await getPageActionButtonId(window, extensionId);
return window.document.getElementById(pageActionId);
}
async function clickPageAction(window, extensionId, modifiers = {}) {
let pageActionId = await getPageActionButtonId(window, extensionId);
await BrowserTestUtils.synthesizeMouseAtCenter(
`#${pageActionId}`,
modifiers,
window.browsingContext
);
return new Promise(resolve => Services.tm.dispatchToMainThread(resolve));
}
function getBrowserActionWidgetId(extensionId) {
return makeWidgetId(extensionId) + "-browser-action";
}
function getBrowserActionWidget(extensionId) {
return lazy.CustomizableUI.getWidget(getBrowserActionWidgetId(extensionId));
}
async function showBrowserAction(window, extensionId) {
let group = getBrowserActionWidget(extensionId);
let widget = group.forWindow(window);
if (!widget.node) {
return;
}
let navbar = window.document.getElementById("nav-bar");
if (group.areaType == lazy.CustomizableUI.TYPE_TOOLBAR) {
Assert.equal(
widget.overflowed,
navbar.hasAttribute("overflowing"),
"Expect widget overflow state to match toolbar"
);
} else if (group.areaType == lazy.CustomizableUI.TYPE_PANEL) {
let panel = window.gUnifiedExtensions.panel;
let shown = BrowserTestUtils.waitForPopupEvent(panel, "shown");
window.gUnifiedExtensions.togglePanel();
await shown;
}
}
async function clickBrowserAction(window, extensionId, modifiers) {
await promiseAnimationFrame(window);
await showBrowserAction(window, extensionId);
if (modifiers) {
let widgetId = getBrowserActionWidgetId(extensionId);
BrowserTestUtils.synthesizeMouseAtCenter(
`#${widgetId}`,
modifiers,
window.browsingContext
);
} else {
let widget = getBrowserActionWidget(extensionId).forWindow(window);
widget.node.firstElementChild.click();
}
}
async function promisePopupShown(popup) {
return new Promise(resolve => {
if (popup.state == "open") {
resolve();
} else {
let onPopupShown = event => {
popup.removeEventListener("popupshown", onPopupShown);
resolve();
};
popup.addEventListener("popupshown", onPopupShown);
}
});
}
function awaitBrowserLoaded(browser) {
if (
browser.ownerGlobal.document.readyState === "complete" &&
browser.currentURI.spec !== "about:blank"
) {
return Promise.resolve();
}
return new Promise(resolve => {
const listener = ev => {
if (browser.currentURI.spec === "about:blank") {
return;
}
browser.removeEventListener("AppTestDelegate:load", listener);
resolve();
};
browser.addEventListener("AppTestDelegate:load", listener);
});
}
function getPanelForNode(node) {
return node.closest("panel");
}
async function awaitExtensionPanel(window, extensionId, awaitLoad = true) {
let { originalTarget: browser } = await BrowserTestUtils.waitForEvent(
window.document,
"WebExtPopupLoaded",
true,
event => event.detail.extension.id === extensionId
);
await Promise.all([
promisePopupShown(getPanelForNode(browser)),
awaitLoad && awaitBrowserLoaded(browser),
]);
return browser;
}
function closeBrowserAction(window, extensionId) {
let group = getBrowserActionWidget(extensionId);
let node = window.document.getElementById(group.viewId);
lazy.CustomizableUI.hidePanelForNode(node);
return Promise.resolve();
}
function getPageActionPopup(window, extensionId) {
let panelId = makeWidgetId(extensionId) + "-panel";
return window.document.getElementById(panelId);
}
function closePageAction(window, extensionId) {
let node = getPageActionPopup(window, extensionId);
if (node) {
return promisePopupShown(node).then(() => {
node.hidePopup();
});
}
return Promise.resolve();
}
function openNewForegroundTab(window, url, waitForLoad = true) {
return BrowserTestUtils.openNewForegroundTab(
window.gBrowser,
url,
waitForLoad
);
}
async function removeTab(tab) {
BrowserTestUtils.removeTab(tab);
}
// These metods are exported so that they can be used in head.js but are
// *not* part of the AppUiTestDelegate API.
export var AppUiTestInternals = {
awaitBrowserLoaded,
getBrowserActionWidget,
getBrowserActionWidgetId,
getPageActionButton,
getPageActionPopup,
getPanelForNode,
makeWidgetId,
promiseAnimationFrame,
promisePopupShown,
showBrowserAction,
};
// These methods are part of the TestDelegate API and need to be compatible
// with the `mobile` AppUiTestDelegate counterpart.
export var AppUiTestDelegate = {
awaitExtensionPanel,
clickBrowserAction,
clickPageAction,
closeBrowserAction,
closePageAction,
openNewForegroundTab,
removeTab,
};
|