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
|
"use strict";
ChromeUtils.defineESModuleGetters(this, {
setTimeout: "resource://gre/modules/Timer.sys.mjs",
});
const kFixtureBaseURL =
"https://example.com/browser/toolkit/modules/tests/browser/";
function removeDupes(list) {
let j = 0;
for (let i = 1; i < list.length; i++) {
if (list[i] != list[j]) {
j++;
if (i != j) {
list[j] = list[i];
}
}
}
list.length = j + 1;
}
function compareLists(list1, list2, kind) {
list1.sort();
removeDupes(list1);
list2.sort();
removeDupes(list2);
is(String(list1), String(list2), `${kind} URLs correct`);
}
async function promiseOpenFindbar(findbar) {
await gBrowser.getFindBar();
findbar.onFindCommand();
return gFindBar._startFindDeferred && gFindBar._startFindDeferred.promise;
}
function promiseFindResult(findbar, str = null) {
let highlightFinished = false;
let findFinished = false;
return new Promise(resolve => {
let listener = {
onFindResult({ searchString }) {
if (str !== null && str != searchString) {
return;
}
findFinished = true;
if (highlightFinished) {
findbar.browser.finder.removeResultListener(listener);
resolve();
}
},
onHighlightFinished() {
highlightFinished = true;
if (findFinished) {
findbar.browser.finder.removeResultListener(listener);
resolve();
}
},
onMatchesCountResult: () => {},
};
findbar.browser.finder.addResultListener(listener);
});
}
function promiseEnterStringIntoFindField(findbar, str) {
let promise = promiseFindResult(findbar, str);
for (let i = 0; i < str.length; i++) {
let event = new KeyboardEvent("keypress", {
bubbles: true,
cancelable: true,
view: null,
keyCode: 0,
charCode: str.charCodeAt(i),
});
findbar._findField.dispatchEvent(event);
}
return promise;
}
function promiseTestHighlighterOutput(
browser,
word,
expectedResult,
extraTest = () => {}
) {
return SpecialPowers.spawn(
browser,
[{ word, expectedResult, extraTest: extraTest.toSource() }],
async function({ word, expectedResult, extraTest }) {
return new Promise((resolve, reject) => {
let stubbed = {};
let callCounts = {
insertCalls: [],
removeCalls: [],
animationCalls: [],
};
let lastMaskNode, lastOutlineNode;
let rects = [];
// Amount of milliseconds to wait after the last time one of our stubs
// was called.
const kTimeoutMs = 1000;
// The initial timeout may wait for a while for results to come in.
let timeout = content.setTimeout(
() => finish(false, "Timeout"),
kTimeoutMs * 5
);
function finish(ok = true, message = "finished with error") {
// Restore the functions we stubbed out.
try {
content.document.insertAnonymousContent = stubbed.insert;
content.document.removeAnonymousContent = stubbed.remove;
} catch (ex) {}
stubbed = {};
content.clearTimeout(timeout);
if (expectedResult.rectCount !== 0) {
Assert.ok(ok, message);
}
Assert.greaterOrEqual(
callCounts.insertCalls.length,
expectedResult.insertCalls[0],
`Min. insert calls should match for '${word}'.`
);
Assert.lessOrEqual(
callCounts.insertCalls.length,
expectedResult.insertCalls[1],
`Max. insert calls should match for '${word}'.`
);
Assert.greaterOrEqual(
callCounts.removeCalls.length,
expectedResult.removeCalls[0],
`Min. remove calls should match for '${word}'.`
);
Assert.lessOrEqual(
callCounts.removeCalls.length,
expectedResult.removeCalls[1],
`Max. remove calls should match for '${word}'.`
);
// We reached the amount of calls we expected, so now we can check
// the amount of rects.
if (!lastMaskNode && expectedResult.rectCount !== 0) {
Assert.ok(
false,
`No mask node found, but expected ${expectedResult.rectCount} rects.`
);
}
Assert.equal(
rects.length,
expectedResult.rectCount,
`Amount of inserted rects should match for '${word}'.`
);
if ("animationCalls" in expectedResult) {
Assert.greaterOrEqual(
callCounts.animationCalls.length,
expectedResult.animationCalls[0],
`Min. animation calls should match for '${word}'.`
);
Assert.lessOrEqual(
callCounts.animationCalls.length,
expectedResult.animationCalls[1],
`Max. animation calls should match for '${word}'.`
);
}
// Allow more specific assertions to be tested in `extraTest`.
// eslint-disable-next-line no-eval
extraTest = eval(extraTest);
extraTest(lastMaskNode, lastOutlineNode, rects);
resolve();
}
function stubAnonymousContentNode(domNode, anonNode) {
let originals = [
anonNode.setTextContentForElement,
anonNode.setAttributeForElement,
anonNode.removeAttributeForElement,
anonNode.setCutoutRectsForElement,
anonNode.setAnimationForElement,
];
anonNode.setTextContentForElement = (id, text) => {
try {
(domNode.querySelector("#" + id) || domNode).textContent = text;
} catch (ex) {}
return originals[0].call(anonNode, id, text);
};
anonNode.setAttributeForElement = (id, attrName, attrValue) => {
try {
(domNode.querySelector("#" + id) || domNode).setAttribute(
attrName,
attrValue
);
} catch (ex) {}
return originals[1].call(anonNode, id, attrName, attrValue);
};
anonNode.removeAttributeForElement = (id, attrName) => {
try {
let node = domNode.querySelector("#" + id) || domNode;
if (node.hasAttribute(attrName)) {
node.removeAttribute(attrName);
}
} catch (ex) {}
return originals[2].call(anonNode, id, attrName);
};
anonNode.setCutoutRectsForElement = (id, cutoutRects) => {
rects = cutoutRects;
return originals[3].call(anonNode, id, cutoutRects);
};
anonNode.setAnimationForElement = (id, keyframes, options) => {
callCounts.animationCalls.push([keyframes, options]);
return originals[4].call(anonNode, id, keyframes, options);
};
}
// Create a function that will stub the original version and collects
// the arguments so we can check the results later.
function stub(which) {
stubbed[which] = content.document[which + "AnonymousContent"];
let prop = which + "Calls";
return function(node) {
callCounts[prop].push(node);
if (which == "insert") {
if (node.outerHTML.indexOf("outlineMask") > -1) {
lastMaskNode = node;
} else {
lastOutlineNode = node;
}
}
content.clearTimeout(timeout);
timeout = content.setTimeout(() => {
finish();
}, kTimeoutMs);
let res = stubbed[which].call(content.document, node);
if (which == "insert") {
stubAnonymousContentNode(node, res);
}
return res;
};
}
content.document.insertAnonymousContent = stub("insert");
content.document.removeAnonymousContent = stub("remove");
});
}
);
}
|