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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* 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/. */
// NOTE: If you're adding new test harness functionality to this file -- first,
// should you at all? Most stuff is better in specific tests, or in
// nested shell.js/browser.js. Second, can you instead add it to
// shell.js? Our goal is to unify these two files for readability, and
// the plan is to empty out this file into that one over time. Third,
// supposing you must add to this file, please add it to this IIFE for
// better modularity/resilience against tests that must do particularly
// bizarre things that might break the harness.
(function initializeUtilityExports(global, parent) {
"use strict";
/**********************************************************************
* CACHED PRIMORDIAL FUNCTIONALITY (before a test might overwrite it) *
**********************************************************************/
var Error = global.Error;
var String = global.String;
var GlobalEval = global.eval;
var ReflectApply = global.Reflect.apply;
var FunctionToString = global.Function.prototype.toString;
var ObjectDefineProperty = global.Object.defineProperty;
// BEWARE: ObjectGetOwnPropertyDescriptor is only safe to use if its result
// is inspected using own-property-examining functionality. Directly
// accessing properties on a returned descriptor without first
// verifying the property's existence can invoke user-modifiable
// behavior.
var ObjectGetOwnPropertyDescriptor = global.Object.getOwnPropertyDescriptor;
var {get: ArrayBufferByteLength} =
ObjectGetOwnPropertyDescriptor(global.ArrayBuffer.prototype, "byteLength");
var Worker = global.Worker;
var Blob = global.Blob;
var URL = global.URL;
var document = global.document;
var documentDocumentElement = global.document.documentElement;
var DocumentCreateElement = global.document.createElement;
var DocumentPrototypeAllGetter = ObjectGetOwnPropertyDescriptor(global.Document.prototype, "all").get;
var EventTargetPrototypeAddEventListener = global.EventTarget.prototype.addEventListener;
var HTMLElementPrototypeStyleSetter =
ObjectGetOwnPropertyDescriptor(global.HTMLElement.prototype, "style").set;
var HTMLIFramePrototypeContentWindowGetter =
ObjectGetOwnPropertyDescriptor(global.HTMLIFrameElement.prototype, "contentWindow").get;
var HTMLScriptElementTextSetter =
ObjectGetOwnPropertyDescriptor(global.HTMLScriptElement.prototype, "text").set;
var NodePrototypeAppendChild = global.Node.prototype.appendChild;
var NodePrototypeRemoveChild = global.Node.prototype.removeChild;
var {get: WindowOnErrorGetter, set: WindowOnErrorSetter} =
ObjectGetOwnPropertyDescriptor(global, "onerror");
var WorkerPrototypePostMessage = Worker.prototype.postMessage;
var URLCreateObjectURL = URL.createObjectURL;
// List of saved window.onerror handlers.
var savedGlobalOnError = [];
// Set |newOnError| as the current window.onerror handler.
function setGlobalOnError(newOnError) {
var currentOnError = ReflectApply(WindowOnErrorGetter, global, []);
ArrayPush(savedGlobalOnError, currentOnError);
ReflectApply(WindowOnErrorSetter, global, [newOnError]);
}
// Restore the previous window.onerror handler.
function restoreGlobalOnError() {
var previousOnError = ArrayPop(savedGlobalOnError);
ReflectApply(WindowOnErrorSetter, global, [previousOnError]);
}
/****************************
* GENERAL HELPER FUNCTIONS *
****************************/
function ArrayPush(array, value) {
ReflectApply(ObjectDefineProperty, null, [
array, array.length,
{__proto__: null, value, writable: true, enumerable: true, configurable: true}
]);
}
function ArrayPop(array) {
if (array.length) {
var item = array[array.length - 1];
array.length -= 1;
return item;
}
}
function AppendChild(elt, kid) {
ReflectApply(NodePrototypeAppendChild, elt, [kid]);
}
function CreateElement(name) {
return ReflectApply(DocumentCreateElement, document, [name]);
}
function RemoveChild(elt, kid) {
ReflectApply(NodePrototypeRemoveChild, elt, [kid]);
}
function CreateWorker(script) {
var blob = new Blob([script], {__proto__: null, type: "text/javascript"});
return new Worker(URLCreateObjectURL(blob));
}
/****************************
* UTILITY FUNCTION EXPORTS *
****************************/
var evaluate = global.evaluate;
if (typeof evaluate !== "function") {
// Shim in "evaluate".
evaluate = function evaluate(code) {
if (typeof code !== "string")
throw Error("Expected string argument for evaluate()");
return GlobalEval(code);
};
global.evaluate = evaluate;
}
var evaluateScript = global.evaluateScript;
if (typeof evaluateScript !== "function") {
evaluateScript = function evaluateScript(code) {
code = String(code);
var script = CreateElement("script");
// Temporarily install a new onerror handler to catch script errors.
var hasUncaughtError = false;
var uncaughtError;
var eventOptions = {__proto__: null, once: true};
ReflectApply(EventTargetPrototypeAddEventListener, script, [
"beforescriptexecute", function() {
setGlobalOnError(function(messageOrEvent, source, lineno, colno, error) {
hasUncaughtError = true;
uncaughtError = error;
return true;
});
}, eventOptions
]);
ReflectApply(EventTargetPrototypeAddEventListener, script, [
"afterscriptexecute", function() {
restoreGlobalOnError();
}, eventOptions
]);
ReflectApply(HTMLScriptElementTextSetter, script, [code]);
AppendChild(documentDocumentElement, script);
RemoveChild(documentDocumentElement, script);
if (hasUncaughtError)
throw uncaughtError;
};
global.evaluateScript = evaluateScript;
}
var newGlobal = global.newGlobal;
if (typeof newGlobal !== "function") {
// Reuse the parent's newGlobal to ensure iframes can be added to the DOM.
newGlobal = parent ? parent.newGlobal : function newGlobal() {
var iframe = CreateElement("iframe");
AppendChild(documentDocumentElement, iframe);
var win =
ReflectApply(HTMLIFramePrototypeContentWindowGetter, iframe, []);
// Removing the iframe breaks evaluateScript() and detachArrayBuffer().
ReflectApply(HTMLElementPrototypeStyleSetter, iframe, ["display:none"]);
// Create utility functions in the new global object.
var initFunction = ReflectApply(FunctionToString, initializeUtilityExports, []);
win.Function("parent", initFunction + "; initializeUtilityExports(this, parent);")(global);
return win;
};
global.newGlobal = newGlobal;
}
var detachArrayBuffer = global.detachArrayBuffer;
if (typeof detachArrayBuffer !== "function") {
var worker = null;
detachArrayBuffer = function detachArrayBuffer(arrayBuffer) {
if (worker === null) {
worker = CreateWorker("/* black hole */");
}
try {
ReflectApply(WorkerPrototypePostMessage, worker, ["detach", [arrayBuffer]]);
} catch (e) {
// postMessage throws an error if the array buffer was already detached.
// Test for this condition by checking if the byte length is zero.
if (ReflectApply(ArrayBufferByteLength, arrayBuffer, []) !== 0) {
throw e;
}
}
};
global.detachArrayBuffer = detachArrayBuffer;
}
var createIsHTMLDDA = global.createIsHTMLDDA;
if (typeof createIsHTMLDDA !== "function") {
createIsHTMLDDA = function() {
return ReflectApply(DocumentPrototypeAllGetter, document, []);
};
global.createIsHTMLDDA = createIsHTMLDDA;
}
})(this);
(function(global) {
"use strict";
/**********************************************************************
* CACHED PRIMORDIAL FUNCTIONALITY (before a test might overwrite it) *
**********************************************************************/
var undefined; // sigh
var Error = global.Error;
var Number = global.Number;
var Object = global.Object;
var String = global.String;
var decodeURIComponent = global.decodeURIComponent;
var ReflectApply = global.Reflect.apply;
var ObjectDefineProperty = Object.defineProperty;
var ObjectPrototypeHasOwnProperty = Object.prototype.hasOwnProperty;
var ObjectPrototypeIsPrototypeOf = Object.prototype.isPrototypeOf;
// BEWARE: ObjectGetOwnPropertyDescriptor is only safe to use if its result
// is inspected using own-property-examining functionality. Directly
// accessing properties on a returned descriptor without first
// verifying the property's existence can invoke user-modifiable
// behavior.
var ObjectGetOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
var window = global.window;
var document = global.document;
var documentDocumentElement = document.documentElement;
var DocumentCreateElement = document.createElement;
var ElementSetClassName =
ObjectGetOwnPropertyDescriptor(global.Element.prototype, "className").set;
var NodePrototypeAppendChild = global.Node.prototype.appendChild;
var NodePrototypeTextContentSetter =
ObjectGetOwnPropertyDescriptor(global.Node.prototype, "textContent").set;
var setTimeout = global.setTimeout;
// Saved harness functions.
var dump = global.dump;
var gczeal = global.gczeal;
var print = global.print;
var reportFailure = global.reportFailure;
var TestCase = global.TestCase;
var SpecialPowers = global.SpecialPowers;
var SpecialPowersCu = SpecialPowers.Cu;
var SpecialPowersForceGC = SpecialPowers.forceGC;
var TestingFunctions = SpecialPowers.Cu.getJSTestingFunctions();
var ClearKeptObjects = TestingFunctions.clearKeptObjects;
// Cached DOM nodes used by the test harness itself. (We assume the test
// doesn't misbehave in a way that actively interferes with what the test
// harness runner observes, e.g. navigating the page to a different location.
// Short of running every test in a worker -- which has its own problems --
// there's no way to isolate a test from the page to that extent.)
var printOutputContainer =
global.document.getElementById("jsreftest-print-output-container");
/****************************
* GENERAL HELPER FUNCTIONS *
****************************/
function ArrayPush(array, value) {
ReflectApply(ObjectDefineProperty, null, [
array, array.length,
{__proto__: null, value, writable: true, enumerable: true, configurable: true}
]);
}
function ArrayPop(array) {
if (array.length) {
var item = array[array.length - 1];
array.length -= 1;
return item;
}
}
function HasOwnProperty(object, property) {
return ReflectApply(ObjectPrototypeHasOwnProperty, object, [property]);
}
function AppendChild(elt, kid) {
ReflectApply(NodePrototypeAppendChild, elt, [kid]);
}
function CreateElement(name) {
return ReflectApply(DocumentCreateElement, document, [name]);
}
function SetTextContent(element, text) {
ReflectApply(NodePrototypeTextContentSetter, element, [text]);
}
// Object containing the set options.
var currentOptions;
// browser.js version of shell.js' |shellOptionsClear| function.
function browserOptionsClear() {
for (var optionName in currentOptions) {
delete currentOptions[optionName];
SpecialPowersCu[optionName] = false;
}
}
// This function is *only* used by shell.js's for-browsers |print()| function!
// It's only defined/exported here because it needs CreateElement and friends,
// only defined here, and we're not yet ready to move them to shell.js.
function AddPrintOutput(s) {
var msgDiv = CreateElement("div");
SetTextContent(msgDiv, s);
AppendChild(printOutputContainer, msgDiv);
}
global.AddPrintOutput = AddPrintOutput;
/*************************************************************************
* HARNESS-CENTRIC EXPORTS (we should generally work to eliminate these) *
*************************************************************************/
// This overwrites shell.js's version that merely prints the given string.
function writeHeaderToLog(string) {
string = String(string);
// First dump to the console.
dump(string + "\n");
// Then output to the page.
var h2 = CreateElement("h2");
SetTextContent(h2, string);
AppendChild(printOutputContainer, h2);
}
global.writeHeaderToLog = writeHeaderToLog;
/*************************
* GLOBAL ERROR HANDLING *
*************************/
// Possible values:
// - "Unknown" if no error is expected,
// - "error" if no specific error type is expected,
// - otherwise the error name, e.g. "TypeError" or "RangeError".
var expectedError;
window.onerror = function (msg, page, line, column, error) {
// Unset all options even when the test finished with an error.
browserOptionsClear();
if (DESCRIPTION === undefined) {
DESCRIPTION = "Unknown";
}
var actual = "error";
var expected = expectedError;
if (expected !== "error" && expected !== "Unknown") {
// Check the error type when an actual Error object is available.
// NB: The |error| parameter of the onerror handler is not required to
// be an Error instance.
if (ReflectApply(ObjectPrototypeIsPrototypeOf, Error.prototype, [error])) {
actual = error.constructor.name;
} else {
expected = "error";
}
}
var reason = `${page}:${line}: ${msg}`;
new TestCase(DESCRIPTION, expected, actual, reason);
reportFailure(msg);
};
/**********************************************
* BROWSER IMPLEMENTATION FOR SHELL FUNCTIONS *
**********************************************/
function gc() {
try {
SpecialPowersForceGC();
} catch (ex) {
print("gc: " + ex);
}
}
global.gc = gc;
global.clearKeptObjects = ClearKeptObjects;
function options(aOptionName) {
// return value of options() is a comma delimited list
// of the previously set values
var value = "";
for (var optionName in currentOptions) {
if (value)
value += ",";
value += optionName;
}
if (aOptionName) {
if (!HasOwnProperty(SpecialPowersCu, aOptionName)) {
// This test is trying to flip an unsupported option, so it's
// likely no longer testing what it was supposed to. Fail it
// hard.
throw "Unsupported JSContext option '" + aOptionName + "'";
}
if (aOptionName in currentOptions) {
// option is set, toggle it to unset
delete currentOptions[aOptionName];
SpecialPowersCu[aOptionName] = false;
} else {
// option is not set, toggle it to set
currentOptions[aOptionName] = true;
SpecialPowersCu[aOptionName] = true;
}
}
return value;
}
global.options = options;
/****************************************
* HARNESS SETUP AND TEARDOWN FUNCTIONS *
****************************************/
function jsTestDriverBrowserInit() {
// Initialize with an empty set, because we just turned off all options.
currentOptions = Object.create(null);
if (document.location.search.indexOf("?") !== 0) {
// not called with a query string
return;
}
var properties = Object.create(null);
var fields = document.location.search.slice(1).split(";");
for (var i = 0; i < fields.length; i++) {
var propertycaptures = /^([^=]+)=(.*)$/.exec(fields[i]);
if (propertycaptures === null) {
properties[fields[i]] = true;
} else {
properties[propertycaptures[1]] = decodeURIComponent(propertycaptures[2]);
}
}
// The test path may contain \ separators for the path.
// Bug 1877606: use / consistently
properties.test = properties.test.replace(/\\/g, "/");
global.gTestPath = properties.test;
var testpathparts = properties.test.split("/");
if (testpathparts.length < 2) {
// must have at least suitepath/testcase.js
return;
}
var testFileName = testpathparts[testpathparts.length - 1];
if (testFileName.endsWith("-n.js")) {
// Negative test without a specific error type.
expectedError = "error";
} else if (properties.error) {
// Negative test which expects a specific error type.
expectedError = properties.error;
} else {
// No error is expected.
expectedError = "Unknown";
}
if (properties.gczeal) {
gczeal(Number(properties.gczeal));
}
// Display the test path in the title.
document.title = properties.test;
// Output script tags for shell.js, then browser.js, at each level of the
// test path hierarchy.
var prepath = "";
var scripts = [];
for (var i = 0; i < testpathparts.length - 1; i++) {
prepath += testpathparts[i] + "/";
if (properties["test262-raw"]) {
// Skip running test harness files (shell.js and browser.js) if the
// test has the raw flag.
continue;
}
scripts.push({src: prepath + "shell.js", module: false});
scripts.push({src: prepath + "browser.js", module: false});
}
// Output the test script itself.
var moduleTest = !!properties.module;
scripts.push({src: prepath + testFileName, module: moduleTest});
// Finally output the driver-end script to advance to the next test.
scripts.push({src: "js-test-driver-end.js", module: false});
if (properties.async) {
gDelayTestDriverEnd = true;
}
if (!moduleTest) {
for (var i = 0; i < scripts.length; i++) {
var src = scripts[i].src;
document.write(`<script src="${src}" charset="utf-8"><\/script>`);
}
} else {
// Modules are loaded asynchronously by default, but for the test harness
// we need to execute all scripts and modules one after the other.
// Appends the next script element to the DOM.
function appendScript(index) {
var script = scriptElements[index];
scriptElements[index] = null;
if (script !== null) {
ReflectApply(NodePrototypeAppendChild, documentDocumentElement, [script]);
}
}
// Create all script elements upfront, so we don't need to worry about
// modified built-ins.
var scriptElements = [];
for (var i = 0; i < scripts.length; i++) {
var spec = scripts[i];
var script = document.createElement("script");
script.charset = "utf-8";
if (spec.module) {
script.type = "module";
}
script.src = spec.src;
let nextScriptIndex = i + 1;
if (nextScriptIndex < scripts.length) {
var callNextAppend = () => appendScript(nextScriptIndex);
script.addEventListener("afterscriptexecute", callNextAppend, {once: true});
// Module scripts don't fire the "afterscriptexecute" event when there
// was an error, instead the "error" event is emitted. So listen for
// both events when creating module scripts.
if (spec.module) {
script.addEventListener("error", callNextAppend, {once: true});
}
}
scriptElements[i] = script;
}
// Append the first script.
appendScript(0);
}
}
global.gDelayTestDriverEnd = false;
function jsTestDriverEnd() {
// gDelayTestDriverEnd is used to delay collection of the test result and
// signal to Spider so that tests can continue to run after page load has
// fired. They are responsible for setting gDelayTestDriverEnd = true then
// when completed, setting gDelayTestDriverEnd = false then calling
// jsTestDriverEnd()
if (gDelayTestDriverEnd) {
return;
}
window.onerror = null;
// Unset all options when the test has finished.
browserOptionsClear();
if (window.opener && window.opener.runNextTest) {
if (window.opener.reportCallBack) {
window.opener.reportCallBack(window.opener.gWindow);
}
setTimeout("window.opener.runNextTest()", 250);
} else {
// tell reftest the test is complete.
ReflectApply(ElementSetClassName, documentDocumentElement, [""]);
// tell Spider page is complete
gPageCompleted = true;
}
}
global.jsTestDriverEnd = jsTestDriverEnd;
/***************************************************************************
* DIALOG CLOSER, PRESUMABLY TO CLOSE SLOW SCRIPT DIALOGS AND OTHER POPUPS *
***************************************************************************/
// dialog closer from http://bclary.com/projects/spider/spider/chrome/content/spider/dialog-closer.js
// Use an array to handle the case where multiple dialogs appear at one time.
var dialogCloserSubjects = [];
var dialogCloser = SpecialPowers
.Cc["@mozilla.org/embedcomp/window-watcher;1"]
.getService(SpecialPowers.Ci.nsIWindowWatcher);
var dialogCloserObserver = {
observe(subject, topic, data) {
if (topic === "domwindowopened" && subject.isChromeWindow) {
ArrayPush(dialogCloserSubjects, subject);
// Timeout of 0 needed when running under reftest framework.
subject.setTimeout(closeDialog, 0);
}
}
};
function closeDialog() {
while (dialogCloserSubjects.length > 0) {
var subject = ArrayPop(dialogCloserSubjects);
subject.close();
}
}
function unregisterDialogCloser() {
gczeal(0);
if (!dialogCloserObserver || !dialogCloser) {
return;
}
dialogCloser.unregisterNotification(dialogCloserObserver);
dialogCloserObserver = null;
dialogCloser = null;
}
dialogCloser.registerNotification(dialogCloserObserver);
window.addEventListener("unload", unregisterDialogCloser, true);
/*******************************************
* RUN ONCE CODE TO SETUP ADDITIONAL STATE *
*******************************************/
jsTestDriverBrowserInit();
})(this);
var gPageCompleted;
|