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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
|
"use strict";
// This file expects these globals to be defined by the test case.
/* global gTestTab:true, gContentAPI:true, tests:false */
ChromeUtils.defineESModuleGetters(this, {
UITour: "resource:///modules/UITour.sys.mjs",
});
const { PermissionTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/PermissionTestUtils.sys.mjs"
);
const SINGLE_TRY_TIMEOUT = 100;
const NUMBER_OF_TRIES = 30;
let gProxyCallbackMap = new Map();
function waitForConditionPromise(
condition,
timeoutMsg,
tryCount = NUMBER_OF_TRIES
) {
return new Promise((resolve, reject) => {
let tries = 0;
function checkCondition() {
if (tries >= tryCount) {
reject(timeoutMsg);
}
var conditionPassed;
try {
conditionPassed = condition();
} catch (e) {
return reject(e);
}
if (conditionPassed) {
return resolve();
}
tries++;
setTimeout(checkCondition, SINGLE_TRY_TIMEOUT);
return undefined;
}
setTimeout(checkCondition, SINGLE_TRY_TIMEOUT);
});
}
function waitForCondition(condition, nextTestFn, errorMsg) {
waitForConditionPromise(condition, errorMsg).then(nextTestFn, reason => {
ok(false, reason + (reason.stack ? "\n" + reason.stack : ""));
});
}
/**
* Wrapper to partially transition tests to Task. Use `add_UITour_task` instead for new tests.
*/
function taskify(fun) {
return doneFn => {
// Output the inner function name otherwise no name will be output.
info("\t" + fun.name);
return fun().then(doneFn, reason => {
console.error(reason);
ok(false, reason);
doneFn();
});
};
}
function is_hidden(element) {
let win = element.ownerGlobal;
let style = win.getComputedStyle(element);
if (style.display == "none") {
return true;
}
if (style.visibility != "visible") {
return true;
}
if (win.XULPopupElement.isInstance(element)) {
return ["hiding", "closed"].includes(element.state);
}
// Hiding a parent element will hide all its children
if (element.parentNode != element.ownerDocument) {
return is_hidden(element.parentNode);
}
return false;
}
function is_visible(element) {
let win = element.ownerGlobal;
let style = win.getComputedStyle(element);
if (style.display == "none") {
return false;
}
if (style.visibility != "visible") {
return false;
}
if (win.XULPopupElement.isInstance(element) && element.state != "open") {
return false;
}
// Hiding a parent element will hide all its children
if (element.parentNode != element.ownerDocument) {
return is_visible(element.parentNode);
}
return true;
}
function is_element_visible(element, msg) {
isnot(element, null, "Element should not be null, when checking visibility");
ok(is_visible(element), msg);
}
function waitForElementToBeVisible(element, nextTestFn, msg) {
waitForCondition(
() => is_visible(element),
() => {
ok(true, msg);
nextTestFn();
},
"Timeout waiting for visibility: " + msg
);
}
function waitForElementToBeHidden(element, nextTestFn, msg) {
waitForCondition(
() => is_hidden(element),
() => {
ok(true, msg);
nextTestFn();
},
"Timeout waiting for invisibility: " + msg
);
}
function elementVisiblePromise(element, msg) {
return waitForConditionPromise(
() => is_visible(element),
"Timeout waiting for visibility: " + msg
);
}
function elementHiddenPromise(element, msg) {
return waitForConditionPromise(
() => is_hidden(element),
"Timeout waiting for invisibility: " + msg
);
}
function waitForPopupAtAnchor(popup, anchorNode, nextTestFn, msg) {
waitForCondition(
() => is_visible(popup) && popup.anchorNode == anchorNode,
() => {
ok(true, msg);
is_element_visible(popup, "Popup should be visible");
nextTestFn();
},
"Timeout waiting for popup at anchor: " + msg
);
}
function getConfigurationPromise(configName) {
return SpecialPowers.spawn(
gTestTab.linkedBrowser,
[configName],
contentConfigName => {
return new Promise(resolve => {
let contentWin = Cu.waiveXrays(content);
contentWin.Mozilla.UITour.getConfiguration(contentConfigName, resolve);
});
}
);
}
function getShowHighlightTargetName() {
let highlight = document.getElementById("UITourHighlight");
return highlight.parentElement.getAttribute("targetName");
}
function getShowInfoTargetName() {
let tooltip = document.getElementById("UITourTooltip");
return tooltip.getAttribute("targetName");
}
function hideInfoPromise(...args) {
let popup = document.getElementById("UITourTooltip");
gContentAPI.hideInfo.apply(gContentAPI, args);
return promisePanelElementHidden(window, popup);
}
/**
* `buttons` and `options` require functions from the content scope so we take a
* function name to call to generate the buttons/options instead of the
* buttons/options themselves. This makes the signature differ from the content one.
*/
function showInfoPromise(
target,
title,
text,
icon,
buttonsFunctionName,
optionsFunctionName
) {
let popup = document.getElementById("UITourTooltip");
let shownPromise = promisePanelElementShown(window, popup);
return SpecialPowers.spawn(gTestTab.linkedBrowser, [[...arguments]], args => {
let contentWin = Cu.waiveXrays(content);
let [
contentTarget,
contentTitle,
contentText,
contentIcon,
contentButtonsFunctionName,
contentOptionsFunctionName,
] = args;
let buttons = contentButtonsFunctionName
? contentWin[contentButtonsFunctionName]()
: null;
let options = contentOptionsFunctionName
? contentWin[contentOptionsFunctionName]()
: null;
contentWin.Mozilla.UITour.showInfo(
contentTarget,
contentTitle,
contentText,
contentIcon,
buttons,
options
);
}).then(() => shownPromise);
}
function showHighlightPromise(...args) {
let popup = document.getElementById("UITourHighlightContainer");
gContentAPI.showHighlight.apply(gContentAPI, args);
return promisePanelElementShown(window, popup);
}
function showMenuPromise(name) {
return SpecialPowers.spawn(gTestTab.linkedBrowser, [name], contentName => {
return new Promise(resolve => {
let contentWin = Cu.waiveXrays(content);
contentWin.Mozilla.UITour.showMenu(contentName, resolve);
});
});
}
function waitForCallbackResultPromise() {
return SpecialPowers.spawn(gTestTab.linkedBrowser, [], async function () {
let contentWin = Cu.waiveXrays(content);
await ContentTaskUtils.waitForCondition(() => {
return contentWin.callbackResult;
}, "callback should be called");
return {
data: contentWin.callbackData,
result: contentWin.callbackResult,
};
});
}
function promisePanelShown(win) {
let panelEl = win.PanelUI.panel;
return promisePanelElementShown(win, panelEl);
}
function promisePanelElementEvent(win, aPanel, aEvent) {
return new Promise((resolve, reject) => {
let timeoutId = win.setTimeout(() => {
aPanel.removeEventListener(aEvent, onPanelEvent);
reject(aEvent + " event did not happen within 5 seconds.");
}, 5000);
function onPanelEvent(e) {
aPanel.removeEventListener(aEvent, onPanelEvent);
win.clearTimeout(timeoutId);
// Wait one tick to let UITour.sys.mjs process the event as well.
executeSoon(resolve);
}
aPanel.addEventListener(aEvent, onPanelEvent);
});
}
function promisePanelElementShown(win, aPanel) {
return promisePanelElementEvent(win, aPanel, "popupshown");
}
function promisePanelElementHidden(win, aPanel) {
return promisePanelElementEvent(win, aPanel, "popuphidden");
}
function is_element_hidden(element, msg) {
isnot(element, null, "Element should not be null, when checking visibility");
ok(is_hidden(element), msg);
}
function isTourBrowser(aBrowser) {
let chromeWindow = aBrowser.ownerGlobal;
return (
UITour.tourBrowsersByWindow.has(chromeWindow) &&
UITour.tourBrowsersByWindow.get(chromeWindow).has(aBrowser)
);
}
async function loadUITourTestPage(callback, host = "https://example.org/") {
if (gTestTab) {
gProxyCallbackMap.clear();
gBrowser.removeTab(gTestTab);
}
if (!window.gProxyCallbackMap) {
window.gProxyCallbackMap = gProxyCallbackMap;
}
let url = getRootDirectory(gTestPath) + "uitour.html";
url = url.replace("chrome://mochitests/content/", host);
gTestTab = await BrowserTestUtils.openNewForegroundTab(gBrowser, url);
// When e10s is enabled, make gContentAPI a proxy which has every property
// return a function which calls the method of the same name on
// contentWin.Mozilla.UITour in a ContentTask.
let UITourHandler = {
get(target, prop, receiver) {
return (...args) => {
let browser = gTestTab.linkedBrowser;
// We need to proxy any callback functions using messages:
let fnIndices = [];
args = args.map((arg, index) => {
// Replace function arguments with "", and add them to the list of
// forwarded functions. We'll construct a function on the content-side
// that forwards all its arguments to a message, and we'll listen for
// those messages on our side and call the corresponding function with
// the arguments we got from the content side.
if (typeof arg == "function") {
gProxyCallbackMap.set(index, arg);
fnIndices.push(index);
return "";
}
return arg;
});
let taskArgs = {
methodName: prop,
args,
fnIndices,
};
return SpecialPowers.spawn(
browser,
[taskArgs],
async function (contentArgs) {
let contentWin = Cu.waiveXrays(content);
let callbacksCalled = 0;
let resolveCallbackPromise;
let allCallbacksCalledPromise = new Promise(
resolve => (resolveCallbackPromise = resolve)
);
let argumentsWithFunctions = Cu.cloneInto(
contentArgs.args.map((arg, index) => {
if (arg === "" && contentArgs.fnIndices.includes(index)) {
return function () {
callbacksCalled++;
SpecialPowers.spawnChrome(
[index, Array.from(arguments)],
(indexParent, argumentsParent) => {
// Please note that this handler only allows the callback to be used once.
// That means that a single gContentAPI.observer() call can't be used
// to observe multiple events.
let window = this.browsingContext.topChromeWindow;
let cb = window.gProxyCallbackMap.get(indexParent);
window.gProxyCallbackMap.delete(indexParent);
cb.apply(null, argumentsParent);
}
);
if (callbacksCalled >= contentArgs.fnIndices.length) {
resolveCallbackPromise();
}
};
}
return arg;
}),
content,
{ cloneFunctions: true }
);
let rv = contentWin.Mozilla.UITour[contentArgs.methodName].apply(
contentWin.Mozilla.UITour,
argumentsWithFunctions
);
if (contentArgs.fnIndices.length) {
await allCallbacksCalledPromise;
}
return rv;
}
);
};
},
};
gContentAPI = new Proxy({}, UITourHandler);
await SimpleTest.promiseFocus(gTestTab.linkedBrowser);
callback();
}
// Wrapper for UITourTest to be used by add_task tests.
function setup_UITourTest() {
return UITourTest(true);
}
// Use `add_task(setup_UITourTest);` instead as we will fold this into `setup_UITourTest` once all tests are using `add_UITour_task`.
function UITourTest(usingAddTask = false) {
Services.prefs.setBoolPref("browser.uitour.enabled", true);
let testHttpsOrigin = "https://example.org";
let testHttpOrigin = "http://example.org";
PermissionTestUtils.add(
testHttpsOrigin,
"uitour",
Services.perms.ALLOW_ACTION
);
PermissionTestUtils.add(
testHttpOrigin,
"uitour",
Services.perms.ALLOW_ACTION
);
UITour.getHighlightContainerAndMaybeCreate(window.document);
UITour.getTooltipAndMaybeCreate(window.document);
// If a test file is using add_task, we don't need to have a test function or
// call `waitForExplicitFinish`.
if (!usingAddTask) {
waitForExplicitFinish();
}
registerCleanupFunction(function () {
delete window.gContentAPI;
if (gTestTab) {
gBrowser.removeTab(gTestTab);
}
delete window.gTestTab;
delete window.gProxyCallbackMap;
Services.prefs.clearUserPref("browser.uitour.enabled");
PermissionTestUtils.remove(testHttpsOrigin, "uitour");
PermissionTestUtils.remove(testHttpOrigin, "uitour");
});
// When using tasks, the harness will call the next added task for us.
if (!usingAddTask) {
nextTest();
}
}
function done(usingAddTask = false) {
info("== Done test, doing shared checks before teardown ==");
return new Promise(resolve => {
executeSoon(() => {
if (gTestTab) {
gBrowser.removeTab(gTestTab);
}
gTestTab = null;
gProxyCallbackMap.clear();
let highlight = document.getElementById("UITourHighlightContainer");
is_element_hidden(
highlight,
"Highlight should be closed/hidden after UITour tab is closed"
);
let tooltip = document.getElementById("UITourTooltip");
is_element_hidden(
tooltip,
"Tooltip should be closed/hidden after UITour tab is closed"
);
ok(
!PanelUI.panel.hasAttribute("noautohide"),
"@noautohide on the menu panel should have been cleaned up"
);
ok(
!PanelUI.panel.hasAttribute("panelopen"),
"The panel shouldn't have @panelopen"
);
isnot(PanelUI.panel.state, "open", "The panel shouldn't be open");
is(
document.getElementById("PanelUI-menu-button").hasAttribute("open"),
false,
"Menu button should know that the menu is closed"
);
info("Done shared checks");
if (usingAddTask) {
executeSoon(resolve);
} else {
executeSoon(nextTest);
}
});
});
}
function nextTest() {
if (!tests.length) {
info("finished tests in this file");
finish();
return;
}
let test = tests.shift();
info("Starting " + test.name);
waitForFocus(function () {
loadUITourTestPage(function () {
test(done);
});
});
}
/**
* All new tests that need the help of `loadUITourTestPage` should use this
* wrapper around their test's generator function to reduce boilerplate.
*/
function add_UITour_task(func) {
let genFun = async function () {
await new Promise(resolve => {
waitForFocus(function () {
loadUITourTestPage(function () {
let funcPromise = (func() || Promise.resolve()).then(
() => done(true),
reason => {
ok(false, reason);
return done(true);
}
);
resolve(funcPromise);
});
});
});
};
Object.defineProperty(genFun, "name", {
configurable: true,
value: func.name,
});
add_task(genFun);
}
|