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
|
"use strict";
/* exported AppConstants, Assert, AppTestDelegate */
var { AppConstants } = SpecialPowers.ChromeUtils.importESModule(
"resource://gre/modules/AppConstants.sys.mjs"
);
var { AppTestDelegate } = SpecialPowers.ChromeUtils.importESModule(
"resource://specialpowers/AppTestDelegate.sys.mjs"
);
{
let chromeScript = SpecialPowers.loadChromeScript(
SimpleTest.getTestFileURL("chrome_cleanup_script.js")
);
SimpleTest.registerCleanupFunction(async () => {
await new Promise(resolve => setTimeout(resolve, 0));
chromeScript.sendAsyncMessage("check-cleanup");
let results = await chromeScript.promiseOneMessage("cleanup-results");
chromeScript.destroy();
if (results.extraWindows.length || results.extraTabs.length) {
ok(
false,
`Test left extra windows or tabs: ${JSON.stringify(results)}\n`
);
}
});
}
let Assert = {
// Cut-down version based on Assert.sys.mjs. Only supports regexp and objects as
// the expected variables.
rejects(promise, expected, msg) {
return promise.then(
() => {
ok(false, msg);
},
actual => {
let matched = false;
if (Object.prototype.toString.call(expected) == "[object RegExp]") {
if (expected.test(actual)) {
matched = true;
}
} else if (actual instanceof expected) {
matched = true;
}
if (matched) {
ok(true, msg);
} else {
ok(false, `Unexpected exception for "${msg}": ${actual}`);
}
}
);
},
};
/* exported waitForLoad */
function waitForLoad(win) {
return new Promise(resolve => {
win.addEventListener(
"load",
function () {
resolve();
},
{ capture: true, once: true }
);
});
}
/* exported loadChromeScript */
function loadChromeScript(fn) {
let wrapper = `
(${fn.toString()})();`;
return SpecialPowers.loadChromeScript(new Function(wrapper));
}
/* exported consoleMonitor */
let consoleMonitor = {
start(messages) {
this.chromeScript = SpecialPowers.loadChromeScript(
SimpleTest.getTestFileURL("mochitest_console.js")
);
this.chromeScript.sendAsyncMessage("consoleStart", messages);
},
async finished() {
let done = this.chromeScript.promiseOneMessage("consoleDone").then(done => {
this.chromeScript.destroy();
return done;
});
this.chromeScript.sendAsyncMessage("waitForConsole");
let test = await done;
ok(test.ok, test.message);
},
};
/* exported waitForState */
function waitForState(sw, state) {
return new Promise(resolve => {
if (sw.state === state) {
return resolve();
}
sw.addEventListener("statechange", function onStateChange() {
if (sw.state === state) {
sw.removeEventListener("statechange", onStateChange);
resolve();
}
});
});
}
|