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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
XPCOMUtils.defineLazyGetter(this, "isXpcshell", function () {
return Services.env.exists("XPCSHELL_TEST_PROFILE_DIR");
});
/**
* Checks whether the given error matches the given expectations.
*
* @param {*} error
* The error to check.
* @param {string | RegExp | Function | null} expectedError
* The expectation to check against. If this parameter is:
*
* - a string, the error message must exactly equal the string.
* - a regular expression, it must match the error message.
* - a function, it is called with the error object and its
* return value is returned.
* @param {BaseContext} context
*
* @returns {boolean}
* True if the error matches the expected error.
*/
const errorMatches = (error, expectedError, context) => {
if (
typeof error === "object" &&
error !== null &&
!context.principal.subsumes(Cu.getObjectPrincipal(error))
) {
Cu.reportError("Error object belongs to the wrong scope.");
return false;
}
if (typeof expectedError === "function") {
return context.runSafeWithoutClone(expectedError, error);
}
if (
typeof error !== "object" ||
error == null ||
typeof error.message !== "string"
) {
return false;
}
if (typeof expectedError === "string") {
return error.message === expectedError;
}
try {
return expectedError.test(error.message);
} catch (e) {
Cu.reportError(e);
}
return false;
};
// Checks whether |v| should use string serialization instead of JSON.
function useStringInsteadOfJSON(v) {
return (
// undefined to string, or else it is omitted from object after stringify.
v === undefined ||
// Values that would have become null.
(typeof v === "number" && (isNaN(v) || !isFinite(v)))
);
}
// A very strict deep equality comparator that throws for unsupported values.
// For context, see https://bugzilla.mozilla.org/show_bug.cgi?id=1782816#c2
function deepEquals(a, b) {
// Some values don't have a JSON representation. To disambiguate from null or
// regular strings, we prepend this prefix instead.
const NON_JSON_PREFIX = "#NOT_JSON_SERIALIZABLE#";
function replacer(key, value) {
if (typeof value == "object" && value !== null && !Array.isArray(value)) {
const cls = ChromeUtils.getClassName(value);
if (cls === "Object") {
// Return plain object with keys sorted in a predictable order.
return Object.fromEntries(
Object.keys(value)
.sort()
.map(k => [k, value[k]])
);
}
// Just throw to avoid potentially inaccurate serializations (e.g. {}).
throw new ExtensionUtils.ExtensionError(`Unsupported obj type: ${cls}`);
}
if (useStringInsteadOfJSON(value)) {
return `${NON_JSON_PREFIX}${value}`;
}
return value;
}
return JSON.stringify(a, replacer) === JSON.stringify(b, replacer);
}
/**
* Serializes the given value for use in informative assertion messages.
*
* @param {*} value
* @returns {string}
*/
const toSource = value => {
function cannotJSONserialize(v) {
return (
useStringInsteadOfJSON(v) ||
// Not a plain object. E.g. [object X], /regexp/, etc.
(typeof v == "object" &&
v !== null &&
!Array.isArray(v) &&
ChromeUtils.getClassName(v) !== "Object")
);
}
try {
if (cannotJSONserialize(value)) {
return String(value);
}
const replacer = (k, v) => (cannotJSONserialize(v) ? String(v) : v);
return JSON.stringify(value, replacer);
} catch (e) {
return "<unknown>";
}
};
this.test = class extends ExtensionAPI {
getAPI(context) {
const { extension } = context;
function getStack(savedFrame = null) {
if (savedFrame) {
return ChromeUtils.createError("", savedFrame).stack.replace(
/^/gm,
" "
);
}
return new context.Error().stack.replace(/^/gm, " ");
}
function assertTrue(value, msg) {
extension.emit(
"test-result",
Boolean(value),
String(msg),
getStack(context.getCaller())
);
}
class TestEventManager extends EventManager {
constructor(...args) {
super(...args);
// A map to keep track of the listeners wrappers being added in
// addListener (the wrapper will be needed to be able to remove
// the listener from this EventManager instance if the extension
// does call test.onMessage.removeListener).
this._listenerWrappers = new Map();
context.callOnClose({
close: () => this._listenerWrappers.clear(),
});
}
addListener(callback, ...args) {
const listenerWrapper = function (...args) {
try {
callback.call(this, ...args);
} catch (e) {
assertTrue(false, `${e}\n${e.stack}`);
}
};
super.addListener(listenerWrapper, ...args);
this._listenerWrappers.set(callback, listenerWrapper);
}
removeListener(callback) {
if (!this._listenerWrappers.has(callback)) {
return;
}
super.removeListener(this._listenerWrappers.get(callback));
this._listenerWrappers.delete(callback);
}
}
if (!Cu.isInAutomation && !isXpcshell) {
return { test: {} };
}
return {
test: {
withHandlingUserInput(callback) {
// TODO(Bug 1598804): remove this once we don't expose anymore the
// entire test API namespace based on an environment variable.
if (!Cu.isInAutomation) {
// This dangerous method should only be available if the
// automation pref is set, which is the case in browser tests.
throw new ExtensionUtils.ExtensionError(
"withHandlingUserInput can only be called in automation"
);
}
ExtensionCommon.withHandlingUserInput(
context.contentWindow,
callback
);
},
sendMessage(...args) {
extension.emit("test-message", ...args);
},
notifyPass(msg) {
extension.emit("test-done", true, msg, getStack(context.getCaller()));
},
notifyFail(msg) {
extension.emit(
"test-done",
false,
msg,
getStack(context.getCaller())
);
},
log(msg) {
extension.emit("test-log", true, msg, getStack(context.getCaller()));
},
fail(msg) {
assertTrue(false, msg);
},
succeed(msg) {
assertTrue(true, msg);
},
assertTrue(value, msg) {
assertTrue(value, msg);
},
assertFalse(value, msg) {
assertTrue(!value, msg);
},
assertDeepEq(expected, actual, msg) {
// The bindings generated by Schemas.jsm accepts any input, but the
// WebIDL-generated binding expects a structurally cloneable input.
// To ensure consistent behavior regardless of which mechanism was
// used, verify that the inputs are structurally cloneable.
// These will throw if the values cannot be cloned.
function ensureStructurallyCloneable(v) {
if (typeof v == "object" && v !== null) {
// Waive xrays to unhide callable members, so that cloneInto will
// throw if needed.
v = ChromeUtils.waiveXrays(v);
}
new StructuredCloneHolder("test.assertEq", null, v, globalThis);
}
// When WebIDL bindings are used, the objects are already cloned
// structurally, so we don't need to check again.
if (!context.useWebIDLBindings) {
ensureStructurallyCloneable(expected);
ensureStructurallyCloneable(actual);
}
extension.emit(
"test-eq",
deepEquals(actual, expected),
String(msg),
toSource(expected),
toSource(actual),
getStack(context.getCaller())
);
},
assertEq(expected, actual, msg) {
let equal = expected === actual;
expected = String(expected);
actual = String(actual);
if (!equal && expected === actual) {
actual += " (different)";
}
extension.emit(
"test-eq",
equal,
String(msg),
expected,
actual,
getStack(context.getCaller())
);
},
assertRejects(promise, expectedError, msg) {
// Wrap in a native promise for consistency.
promise = Promise.resolve(promise);
return promise.then(
result => {
let message = `Promise resolved, expected rejection '${toSource(
expectedError
)}'`;
if (msg) {
message += `: ${msg}`;
}
assertTrue(false, message);
},
error => {
let expected = toSource(expectedError);
let message = `got '${toSource(error)}'`;
if (msg) {
message += `: ${msg}`;
}
assertTrue(
errorMatches(error, expectedError, context),
`Promise rejected, expecting rejection to match '${expected}', ${message}`
);
}
);
},
assertThrows(func, expectedError, msg) {
try {
func();
let message = `Function did not throw, expected error '${toSource(
expectedError
)}'`;
if (msg) {
message += `: ${msg}`;
}
assertTrue(false, message);
} catch (error) {
let expected = toSource(expectedError);
let message = `got '${toSource(error)}'`;
if (msg) {
message += `: ${msg}`;
}
assertTrue(
errorMatches(error, expectedError, context),
`Function threw, expecting error to match '${expected}', ${message}`
);
}
},
onMessage: new TestEventManager({
context,
name: "test.onMessage",
register: fire => {
let handler = (event, ...args) => {
fire.async(...args);
};
extension.on("test-harness-message", handler);
return () => {
extension.off("test-harness-message", handler);
};
},
}).api(),
},
};
}
};
|