Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / resources / testharness.js
blob9ad4f9aa5c1acdfd6be2c6903f53d7013e92fee2
1 /*global self*/
2 /*jshint latedef: nofunc*/
3 /*
4 Distributed under both the W3C Test Suite License [1] and the W3C
5 3-clause BSD License [2]. To contribute to a W3C Test Suite, see the
6 policies and contribution forms [3].
8 [1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license
9 [2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license
10 [3] http://www.w3.org/2004/10/27-testcases
13 /* Documentation is in docs/api.md */
15 (function ()
17     var debug = false;
18     // default timeout is 10 seconds, test can override if needed
19     var settings = {
20         output:true,
21         harness_timeout:{
22             "normal":10000,
23             "long":60000
24         },
25         test_timeout:null,
26         message_events: ["start", "test_state", "result", "completion"]
27     };
29     var xhtml_ns = "http://www.w3.org/1999/xhtml";
31     /*
32      * TestEnvironment is an abstraction for the environment in which the test
33      * harness is used. Each implementation of a test environment has to provide
34      * the following interface:
35      *
36      * interface TestEnvironment {
37      *   // Invoked after the global 'tests' object has been created and it's
38      *   // safe to call add_*_callback() to register event handlers.
39      *   void on_tests_ready();
40      *
41      *   // Invoked after setup() has been called to notify the test environment
42      *   // of changes to the test harness properties.
43      *   void on_new_harness_properties(object properties);
44      *
45      *   // Should return a new unique default test name.
46      *   DOMString next_default_test_name();
47      *
48      *   // Should return the test harness timeout duration in milliseconds.
49      *   float test_timeout();
50      *
51      *   // Should return the global scope object.
52      *   object global_scope();
53      * };
54      */
56     /*
57      * A test environment with a DOM. The global object is 'window'. By default
58      * test results are displayed in a table. Any parent windows receive
59      * callbacks or messages via postMessage() when test events occur. See
60      * apisample11.html and apisample12.html.
61      */
62     function WindowTestEnvironment() {
63         this.name_counter = 0;
64         this.window_cache = null;
65         this.output_handler = null;
66         this.all_loaded = false;
67         var this_obj = this;
68         this.message_events = [];
70         this.message_functions = {
71             start: [add_start_callback, remove_start_callback,
72                     function (properties) {
73                         this_obj._dispatch("start_callback", [properties],
74                                            {type: "start", properties: properties});
75                     }],
77             test_state: [add_test_state_callback, remove_test_state_callback,
78                          function(test) {
79                              this_obj._dispatch("test_state_callback", [test],
80                                                 {type: "test_state",
81                                                  test: test.structured_clone()});
82                          }],
83             result: [add_result_callback, remove_result_callback,
84                      function (test) {
85                          this_obj.output_handler.show_status();
86                          this_obj._dispatch("result_callback", [test],
87                                             {type: "result",
88                                              test: test.structured_clone()});
89                      }],
90             completion: [add_completion_callback, remove_completion_callback,
91                          function (tests, harness_status) {
92                              var cloned_tests = map(tests, function(test) {
93                                  return test.structured_clone();
94                              });
95                              this_obj._dispatch("completion_callback", [tests, harness_status],
96                                                 {type: "complete",
97                                                  tests: cloned_tests,
98                                                  status: harness_status.structured_clone()});
99                          }]
100         }
102         on_event(window, 'load', function() {
103             this_obj.all_loaded = true;
104         });
105     }
107     WindowTestEnvironment.prototype._dispatch = function(selector, callback_args, message_arg) {
108         this._forEach_windows(
109                 function(w, same_origin) {
110                     if (same_origin) {
111                         try {
112                             var has_selector = selector in w;
113                         } catch(e) {
114                             // If document.domain was set at some point same_origin can be
115                             // wrong and the above will fail.
116                             has_selector = false;
117                         }
118                         if (has_selector) {
119                             try {
120                                 w[selector].apply(undefined, callback_args);
121                             } catch (e) {
122                                 if (debug) {
123                                     throw e;
124                                 }
125                             }
126                         }
127                     }
128                     if (supports_post_message(w) && w !== self) {
129                         w.postMessage(message_arg, "*");
130                     }
131                 });
132     };
134     WindowTestEnvironment.prototype._forEach_windows = function(callback) {
135         // Iterate of the the windows [self ... top, opener]. The callback is passed
136         // two objects, the first one is the windows object itself, the second one
137         // is a boolean indicating whether or not its on the same origin as the
138         // current window.
139         var cache = this.window_cache;
140         if (!cache) {
141             cache = [[self, true]];
142             var w = self;
143             var i = 0;
144             var so;
145             var origins = location.ancestorOrigins;
146             while (w != w.parent) {
147                 w = w.parent;
148                 // In WebKit, calls to parent windows' properties that aren't on the same
149                 // origin cause an error message to be displayed in the error console but
150                 // don't throw an exception. This is a deviation from the current HTML5
151                 // spec. See: https://bugs.webkit.org/show_bug.cgi?id=43504
152                 // The problem with WebKit's behavior is that it pollutes the error console
153                 // with error messages that can't be caught.
154                 //
155                 // This issue can be mitigated by relying on the (for now) proprietary
156                 // `location.ancestorOrigins` property which returns an ordered list of
157                 // the origins of enclosing windows. See:
158                 // http://trac.webkit.org/changeset/113945.
159                 if (origins) {
160                     so = (location.origin == origins[i]);
161                 } else {
162                     so = is_same_origin(w);
163                 }
164                 cache.push([w, so]);
165                 i++;
166             }
167             w = window.opener;
168             if (w) {
169                 // window.opener isn't included in the `location.ancestorOrigins` prop.
170                 // We'll just have to deal with a simple check and an error msg on WebKit
171                 // browsers in this case.
172                 cache.push([w, is_same_origin(w)]);
173             }
174             this.window_cache = cache;
175         }
177         forEach(cache,
178                 function(a) {
179                     callback.apply(null, a);
180                 });
181     };
183     WindowTestEnvironment.prototype.on_tests_ready = function() {
184         var output = new Output();
185         this.output_handler = output;
187         var this_obj = this;
189         add_start_callback(function (properties) {
190             this_obj.output_handler.init(properties);
191         });
193         add_test_state_callback(function(test) {
194             this_obj.output_handler.show_status();
195         });
197         add_result_callback(function (test) {
198             this_obj.output_handler.show_status();
199         });
201         add_completion_callback(function (tests, harness_status) {
202             this_obj.output_handler.show_results(tests, harness_status);
203         });
204         this.setup_messages(settings.message_events);
205     };
207     WindowTestEnvironment.prototype.setup_messages = function(new_events) {
208         var this_obj = this;
209         forEach(settings.message_events, function(x) {
210             var current_dispatch = this_obj.message_events.indexOf(x) !== -1;
211             var new_dispatch = new_events.indexOf(x) !== -1;
212             if (!current_dispatch && new_dispatch) {
213                 this_obj.message_functions[x][0](this_obj.message_functions[x][2]);
214             } else if (current_dispatch && !new_dispatch) {
215                 this_obj.message_functions[x][1](this_obj.message_functions[x][2]);
216             }
217         });
218         this.message_events = new_events;
219     }
221     WindowTestEnvironment.prototype.next_default_test_name = function() {
222         //Don't use document.title to work around an Opera bug in XHTML documents
223         var title = document.getElementsByTagName("title")[0];
224         var prefix = (title && title.firstChild && title.firstChild.data) || "Untitled";
225         var suffix = this.name_counter > 0 ? " " + this.name_counter : "";
226         this.name_counter++;
227         return prefix + suffix;
228     };
230     WindowTestEnvironment.prototype.on_new_harness_properties = function(properties) {
231         this.output_handler.setup(properties);
232         if (properties.hasOwnProperty("message_events")) {
233             this.setup_messages(properties.message_events);
234         }
235     };
237     WindowTestEnvironment.prototype.add_on_loaded_callback = function(callback) {
238         on_event(window, 'load', callback);
239     };
241     WindowTestEnvironment.prototype.test_timeout = function() {
242         var metas = document.getElementsByTagName("meta");
243         for (var i = 0; i < metas.length; i++) {
244             if (metas[i].name == "timeout") {
245                 if (metas[i].content == "long") {
246                     return settings.harness_timeout.long;
247                 }
248                 break;
249             }
250         }
251         return settings.harness_timeout.normal;
252     };
254     WindowTestEnvironment.prototype.global_scope = function() {
255         return window;
256     };
258     /*
259      * Base TestEnvironment implementation for a generic web worker.
260      *
261      * Workers accumulate test results. One or more clients can connect and
262      * retrieve results from a worker at any time.
263      *
264      * WorkerTestEnvironment supports communicating with a client via a
265      * MessagePort.  The mechanism for determining the appropriate MessagePort
266      * for communicating with a client depends on the type of worker and is
267      * implemented by the various specializations of WorkerTestEnvironment
268      * below.
269      *
270      * A client document using testharness can use fetch_tests_from_worker() to
271      * retrieve results from a worker. See apisample16.html.
272      */
273     function WorkerTestEnvironment() {
274         this.name_counter = 0;
275         this.all_loaded = true;
276         this.message_list = [];
277         this.message_ports = [];
278     }
280     WorkerTestEnvironment.prototype._dispatch = function(message) {
281         this.message_list.push(message);
282         for (var i = 0; i < this.message_ports.length; ++i)
283         {
284             this.message_ports[i].postMessage(message);
285         }
286     };
288     // The only requirement is that port has a postMessage() method. It doesn't
289     // have to be an instance of a MessagePort, and often isn't.
290     WorkerTestEnvironment.prototype._add_message_port = function(port) {
291         this.message_ports.push(port);
292         for (var i = 0; i < this.message_list.length; ++i)
293         {
294             port.postMessage(this.message_list[i]);
295         }
296     };
298     WorkerTestEnvironment.prototype.next_default_test_name = function() {
299         var suffix = this.name_counter > 0 ? " " + this.name_counter : "";
300         this.name_counter++;
301         return "Untitled" + suffix;
302     };
304     WorkerTestEnvironment.prototype.on_new_harness_properties = function() {};
306     WorkerTestEnvironment.prototype.on_tests_ready = function() {
307         var this_obj = this;
308         add_start_callback(
309                 function(properties) {
310                     this_obj._dispatch({
311                         type: "start",
312                         properties: properties,
313                     });
314                 });
315         add_test_state_callback(
316                 function(test) {
317                     this_obj._dispatch({
318                         type: "test_state",
319                         test: test.structured_clone()
320                     });
321                 });
322         add_result_callback(
323                 function(test) {
324                     this_obj._dispatch({
325                         type: "result",
326                         test: test.structured_clone()
327                     });
328                 });
329         add_completion_callback(
330                 function(tests, harness_status) {
331                     this_obj._dispatch({
332                         type: "complete",
333                         tests: map(tests,
334                             function(test) {
335                                 return test.structured_clone();
336                             }),
337                         status: harness_status.structured_clone()
338                     });
339                 });
340     };
342     WorkerTestEnvironment.prototype.add_on_loaded_callback = function() {};
344     WorkerTestEnvironment.prototype.test_timeout = function() {
345         // Tests running in a worker don't have a default timeout. I.e. all
346         // worker tests behave as if settings.explicit_timeout is true.
347         return null;
348     };
350     WorkerTestEnvironment.prototype.global_scope = function() {
351         return self;
352     };
354     /*
355      * Dedicated web workers.
356      * https://html.spec.whatwg.org/multipage/workers.html#dedicatedworkerglobalscope
357      *
358      * This class is used as the test_environment when testharness is running
359      * inside a dedicated worker.
360      */
361     function DedicatedWorkerTestEnvironment() {
362         WorkerTestEnvironment.call(this);
363         // self is an instance of DedicatedWorkerGlobalScope which exposes
364         // a postMessage() method for communicating via the message channel
365         // established when the worker is created.
366         this._add_message_port(self);
367     }
368     DedicatedWorkerTestEnvironment.prototype = Object.create(WorkerTestEnvironment.prototype);
370     DedicatedWorkerTestEnvironment.prototype.on_tests_ready = function() {
371         WorkerTestEnvironment.prototype.on_tests_ready.call(this);
372         // In the absence of an onload notification, we a require dedicated
373         // workers to explicitly signal when the tests are done.
374         tests.wait_for_finish = true;
375     };
377     /*
378      * Shared web workers.
379      * https://html.spec.whatwg.org/multipage/workers.html#sharedworkerglobalscope
380      *
381      * This class is used as the test_environment when testharness is running
382      * inside a shared web worker.
383      */
384     function SharedWorkerTestEnvironment() {
385         WorkerTestEnvironment.call(this);
386         var this_obj = this;
387         // Shared workers receive message ports via the 'onconnect' event for
388         // each connection.
389         self.addEventListener("connect",
390                 function(message_event) {
391                     this_obj._add_message_port(message_event.source);
392                 });
393     }
394     SharedWorkerTestEnvironment.prototype = Object.create(WorkerTestEnvironment.prototype);
396     SharedWorkerTestEnvironment.prototype.on_tests_ready = function() {
397         WorkerTestEnvironment.prototype.on_tests_ready.call(this);
398         // In the absence of an onload notification, we a require shared
399         // workers to explicitly signal when the tests are done.
400         tests.wait_for_finish = true;
401     };
403     /*
404      * Service workers.
405      * http://www.w3.org/TR/service-workers/
406      *
407      * This class is used as the test_environment when testharness is running
408      * inside a service worker.
409      */
410     function ServiceWorkerTestEnvironment() {
411         WorkerTestEnvironment.call(this);
412         this.all_loaded = false;
413         this.on_loaded_callback = null;
414         var this_obj = this;
415         self.addEventListener("message",
416                 function(event) {
417                     if (event.data.type && event.data.type === "connect") {
418                         if (event.ports && event.ports[0]) {
419                             // If a MessageChannel was passed, then use it to
420                             // send results back to the main window.  This
421                             // allows the tests to work even if the browser
422                             // does not fully support MessageEvent.source in
423                             // ServiceWorkers yet.
424                             this_obj._add_message_port(event.ports[0]);
425                             event.ports[0].start();
426                         } else {
427                             // If there is no MessageChannel, then attempt to
428                             // use the MessageEvent.source to send results
429                             // back to the main window.
430                             this_obj._add_message_port(event.source);
431                         }
432                     }
433                 });
435         // The oninstall event is received after the service worker script and
436         // all imported scripts have been fetched and executed. It's the
437         // equivalent of an onload event for a document. All tests should have
438         // been added by the time this event is received, thus it's not
439         // necessary to wait until the onactivate event.
440         on_event(self, "install",
441                 function(event) {
442                     this_obj.all_loaded = true;
443                     if (this_obj.on_loaded_callback) {
444                         this_obj.on_loaded_callback();
445                     }
446                 });
447     }
448     ServiceWorkerTestEnvironment.prototype = Object.create(WorkerTestEnvironment.prototype);
450     ServiceWorkerTestEnvironment.prototype.add_on_loaded_callback = function(callback) {
451         if (this.all_loaded) {
452             callback();
453         } else {
454             this.on_loaded_callback = callback;
455         }
456     };
458     function create_test_environment() {
459         if ('document' in self) {
460             return new WindowTestEnvironment();
461         }
462         if ('DedicatedWorkerGlobalScope' in self &&
463             self instanceof DedicatedWorkerGlobalScope) {
464             return new DedicatedWorkerTestEnvironment();
465         }
466         if ('SharedWorkerGlobalScope' in self &&
467             self instanceof SharedWorkerGlobalScope) {
468             return new SharedWorkerTestEnvironment();
469         }
470         if ('ServiceWorkerGlobalScope' in self &&
471             self instanceof ServiceWorkerGlobalScope) {
472             return new ServiceWorkerTestEnvironment();
473         }
474         throw new Error("Unsupported test environment");
475     }
477     var test_environment = create_test_environment();
479     function is_shared_worker(worker) {
480         return 'SharedWorker' in self && worker instanceof SharedWorker;
481     }
483     function is_service_worker(worker) {
484         return 'ServiceWorker' in self && worker instanceof ServiceWorker;
485     }
487     /*
488      * API functions
489      */
491     function test(func, name, properties)
492     {
493         var test_name = name ? name : test_environment.next_default_test_name();
494         properties = properties ? properties : {};
495         var test_obj = new Test(test_name, properties);
496         test_obj.step(func, test_obj, test_obj);
497         if (test_obj.phase === test_obj.phases.STARTED) {
498             test_obj.done();
499         }
500     }
502     function async_test(func, name, properties)
503     {
504         if (typeof func !== "function") {
505             properties = name;
506             name = func;
507             func = null;
508         }
509         var test_name = name ? name : test_environment.next_default_test_name();
510         properties = properties ? properties : {};
511         var test_obj = new Test(test_name, properties);
512         if (func) {
513             test_obj.step(func, test_obj, test_obj);
514         }
515         return test_obj;
516     }
518     function promise_test(func, name, properties) {
519         var test = async_test(name, properties);
520         // If there is no promise tests queue make one.
521         test.step(function() {
522             if (!tests.promise_tests) {
523                 tests.promise_tests = Promise.resolve();
524             }
525         });
526         tests.promise_tests = tests.promise_tests.then(function() {
527             return Promise.resolve(test.step(func, test, test))
528                 .then(
529                     function() {
530                         test.done();
531                     })
532                 .catch(test.step_func(
533                     function(value) {
534                         if (value instanceof AssertionError) {
535                             throw value;
536                         }
537                         assert(false, "promise_test", null,
538                                "Unhandled rejection with value: ${value}", {value:value});
539                     }));
540         });
541     }
543     function promise_rejects(test, expected, promise) {
544         return promise.then(test.unreached_func("Should have rejected.")).catch(function(e) {
545             assert_throws(expected, function() { throw e });
546         });
547     }
549     /**
550      * This constructor helper allows DOM events to be handled using Promises,
551      * which can make it a lot easier to test a very specific series of events,
552      * including ensuring that unexpected events are not fired at any point.
553      */
554     function EventWatcher(test, watchedNode, eventTypes)
555     {
556         if (typeof eventTypes == 'string') {
557             eventTypes = [eventTypes];
558         }
560         var waitingFor = null;
562         var eventHandler = test.step_func(function(evt) {
563             assert_true(!!waitingFor,
564                         'Not expecting event, but got ' + evt.type + ' event');
565             assert_equals(evt.type, waitingFor.types[0],
566                           'Expected ' + waitingFor.types[0] + ' event, but got ' +
567                           evt.type + ' event instead');
568             if (waitingFor.types.length > 1) {
569                 // Pop first event from array
570                 waitingFor.types.shift();
571                 return;
572             }
573             // We need to null out waitingFor before calling the resolve function
574             // since the Promise's resolve handlers may call wait_for() which will
575             // need to set waitingFor.
576             var resolveFunc = waitingFor.resolve;
577             waitingFor = null;
578             resolveFunc(evt);
579         });
581         for (var i = 0; i < eventTypes.length; i++) {
582             watchedNode.addEventListener(eventTypes[i], eventHandler);
583         }
585         /**
586          * Returns a Promise that will resolve after the specified event or
587          * series of events has occured.
588          */
589         this.wait_for = function(types) {
590             if (waitingFor) {
591                 return Promise.reject('Already waiting for an event or events');
592             }
593             if (typeof types == 'string') {
594                 types = [types];
595             }
596             return new Promise(function(resolve, reject) {
597                 waitingFor = {
598                     types: types,
599                     resolve: resolve,
600                     reject: reject
601                 };
602             });
603         };
605         function stop_watching() {
606             for (var i = 0; i < eventTypes.length; i++) {
607                 watchedNode.removeEventListener(eventTypes[i], eventHandler);
608             }
609         };
611         test.add_cleanup(stop_watching);
613         return this;
614     }
615     expose(EventWatcher, 'EventWatcher');
617     function setup(func_or_properties, maybe_properties)
618     {
619         var func = null;
620         var properties = {};
621         if (arguments.length === 2) {
622             func = func_or_properties;
623             properties = maybe_properties;
624         } else if (func_or_properties instanceof Function) {
625             func = func_or_properties;
626         } else {
627             properties = func_or_properties;
628         }
629         tests.setup(func, properties);
630         test_environment.on_new_harness_properties(properties);
631     }
633     function done() {
634         if (tests.tests.length === 0) {
635             tests.set_file_is_test();
636         }
637         if (tests.file_is_test) {
638             tests.tests[0].done();
639         }
640         tests.end_wait();
641     }
643     function generate_tests(func, args, properties) {
644         forEach(args, function(x, i)
645                 {
646                     var name = x[0];
647                     test(function()
648                          {
649                              func.apply(this, x.slice(1));
650                          },
651                          name,
652                          Array.isArray(properties) ? properties[i] : properties);
653                 });
654     }
656     function on_event(object, event, callback)
657     {
658         object.addEventListener(event, callback, false);
659     }
661     expose(test, 'test');
662     expose(async_test, 'async_test');
663     expose(promise_test, 'promise_test');
664     expose(promise_rejects, 'promise_rejects');
665     expose(generate_tests, 'generate_tests');
666     expose(setup, 'setup');
667     expose(done, 'done');
668     expose(on_event, 'on_event');
670     /*
671      * Return a string truncated to the given length, with ... added at the end
672      * if it was longer.
673      */
674     function truncate(s, len)
675     {
676         if (s.length > len) {
677             return s.substring(0, len - 3) + "...";
678         }
679         return s;
680     }
682     /*
683      * Return true if object is probably a Node object.
684      */
685     function is_node(object)
686     {
687         // I use duck-typing instead of instanceof, because
688         // instanceof doesn't work if the node is from another window (like an
689         // iframe's contentWindow):
690         // http://www.w3.org/Bugs/Public/show_bug.cgi?id=12295
691         if ("nodeType" in object &&
692             "nodeName" in object &&
693             "nodeValue" in object &&
694             "childNodes" in object) {
695             try {
696                 object.nodeType;
697             } catch (e) {
698                 // The object is probably Node.prototype or another prototype
699                 // object that inherits from it, and not a Node instance.
700                 return false;
701             }
702             return true;
703         }
704         return false;
705     }
707     /*
708      * Convert a value to a nice, human-readable string
709      */
710     function format_value(val, seen)
711     {
712         if (!seen) {
713             seen = [];
714         }
715         if (typeof val === "object" && val !== null) {
716             if (seen.indexOf(val) >= 0) {
717                 return "[...]";
718             }
719             seen.push(val);
720         }
721         if (Array.isArray(val)) {
722             return "[" + val.map(function(x) {return format_value(x, seen);}).join(", ") + "]";
723         }
725         switch (typeof val) {
726         case "string":
727             val = val.replace("\\", "\\\\");
728             for (var i = 0; i < 32; i++) {
729                 var replace = "\\";
730                 switch (i) {
731                 case 0: replace += "0"; break;
732                 case 1: replace += "x01"; break;
733                 case 2: replace += "x02"; break;
734                 case 3: replace += "x03"; break;
735                 case 4: replace += "x04"; break;
736                 case 5: replace += "x05"; break;
737                 case 6: replace += "x06"; break;
738                 case 7: replace += "x07"; break;
739                 case 8: replace += "b"; break;
740                 case 9: replace += "t"; break;
741                 case 10: replace += "n"; break;
742                 case 11: replace += "v"; break;
743                 case 12: replace += "f"; break;
744                 case 13: replace += "r"; break;
745                 case 14: replace += "x0e"; break;
746                 case 15: replace += "x0f"; break;
747                 case 16: replace += "x10"; break;
748                 case 17: replace += "x11"; break;
749                 case 18: replace += "x12"; break;
750                 case 19: replace += "x13"; break;
751                 case 20: replace += "x14"; break;
752                 case 21: replace += "x15"; break;
753                 case 22: replace += "x16"; break;
754                 case 23: replace += "x17"; break;
755                 case 24: replace += "x18"; break;
756                 case 25: replace += "x19"; break;
757                 case 26: replace += "x1a"; break;
758                 case 27: replace += "x1b"; break;
759                 case 28: replace += "x1c"; break;
760                 case 29: replace += "x1d"; break;
761                 case 30: replace += "x1e"; break;
762                 case 31: replace += "x1f"; break;
763                 }
764                 val = val.replace(RegExp(String.fromCharCode(i), "g"), replace);
765             }
766             return '"' + val.replace(/"/g, '\\"') + '"';
767         case "boolean":
768         case "undefined":
769             return String(val);
770         case "number":
771             // In JavaScript, -0 === 0 and String(-0) == "0", so we have to
772             // special-case.
773             if (val === -0 && 1/val === -Infinity) {
774                 return "-0";
775             }
776             return String(val);
777         case "object":
778             if (val === null) {
779                 return "null";
780             }
782             // Special-case Node objects, since those come up a lot in my tests.  I
783             // ignore namespaces.
784             if (is_node(val)) {
785                 switch (val.nodeType) {
786                 case Node.ELEMENT_NODE:
787                     var ret = "<" + val.localName;
788                     for (var i = 0; i < val.attributes.length; i++) {
789                         ret += " " + val.attributes[i].name + '="' + val.attributes[i].value + '"';
790                     }
791                     ret += ">" + val.innerHTML + "</" + val.localName + ">";
792                     return "Element node " + truncate(ret, 60);
793                 case Node.TEXT_NODE:
794                     return 'Text node "' + truncate(val.data, 60) + '"';
795                 case Node.PROCESSING_INSTRUCTION_NODE:
796                     return "ProcessingInstruction node with target " + format_value(truncate(val.target, 60)) + " and data " + format_value(truncate(val.data, 60));
797                 case Node.COMMENT_NODE:
798                     return "Comment node <!--" + truncate(val.data, 60) + "-->";
799                 case Node.DOCUMENT_NODE:
800                     return "Document node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children");
801                 case Node.DOCUMENT_TYPE_NODE:
802                     return "DocumentType node";
803                 case Node.DOCUMENT_FRAGMENT_NODE:
804                     return "DocumentFragment node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children");
805                 default:
806                     return "Node object of unknown type";
807                 }
808             }
810         /* falls through */
811         default:
812             return typeof val + ' "' + truncate(String(val), 60) + '"';
813         }
814     }
815     expose(format_value, "format_value");
817     /*
818      * Assertions
819      */
821     function assert_true(actual, description)
822     {
823         assert(actual === true, "assert_true", description,
824                                 "expected true got ${actual}", {actual:actual});
825     }
826     expose(assert_true, "assert_true");
828     function assert_false(actual, description)
829     {
830         assert(actual === false, "assert_false", description,
831                                  "expected false got ${actual}", {actual:actual});
832     }
833     expose(assert_false, "assert_false");
835     function same_value(x, y) {
836         if (y !== y) {
837             //NaN case
838             return x !== x;
839         }
840         if (x === 0 && y === 0) {
841             //Distinguish +0 and -0
842             return 1/x === 1/y;
843         }
844         return x === y;
845     }
847     function assert_equals(actual, expected, description)
848     {
849          /*
850           * Test if two primitives are equal or two objects
851           * are the same object
852           */
853         if (typeof actual != typeof expected) {
854             assert(false, "assert_equals", description,
855                           "expected (" + typeof expected + ") ${expected} but got (" + typeof actual + ") ${actual}",
856                           {expected:expected, actual:actual});
857             return;
858         }
859         assert(same_value(actual, expected), "assert_equals", description,
860                                              "expected ${expected} but got ${actual}",
861                                              {expected:expected, actual:actual});
862     }
863     expose(assert_equals, "assert_equals");
865     function assert_not_equals(actual, expected, description)
866     {
867          /*
868           * Test if two primitives are unequal or two objects
869           * are different objects
870           */
871         assert(!same_value(actual, expected), "assert_not_equals", description,
872                                               "got disallowed value ${actual}",
873                                               {actual:actual});
874     }
875     expose(assert_not_equals, "assert_not_equals");
877     function assert_in_array(actual, expected, description)
878     {
879         assert(expected.indexOf(actual) != -1, "assert_in_array", description,
880                                                "value ${actual} not in array ${expected}",
881                                                {actual:actual, expected:expected});
882     }
883     expose(assert_in_array, "assert_in_array");
885     function assert_object_equals(actual, expected, description)
886     {
887          //This needs to be improved a great deal
888          function check_equal(actual, expected, stack)
889          {
890              stack.push(actual);
892              var p;
893              for (p in actual) {
894                  assert(expected.hasOwnProperty(p), "assert_object_equals", description,
895                                                     "unexpected property ${p}", {p:p});
897                  if (typeof actual[p] === "object" && actual[p] !== null) {
898                      if (stack.indexOf(actual[p]) === -1) {
899                          check_equal(actual[p], expected[p], stack);
900                      }
901                  } else {
902                      assert(same_value(actual[p], expected[p]), "assert_object_equals", description,
903                                                        "property ${p} expected ${expected} got ${actual}",
904                                                        {p:p, expected:expected, actual:actual});
905                  }
906              }
907              for (p in expected) {
908                  assert(actual.hasOwnProperty(p),
909                         "assert_object_equals", description,
910                         "expected property ${p} missing", {p:p});
911              }
912              stack.pop();
913          }
914          check_equal(actual, expected, []);
915     }
916     expose(assert_object_equals, "assert_object_equals");
918     function assert_array_equals(actual, expected, description)
919     {
920         assert(actual.length === expected.length,
921                "assert_array_equals", description,
922                "lengths differ, expected ${expected} got ${actual}",
923                {expected:expected.length, actual:actual.length});
925         for (var i = 0; i < actual.length; i++) {
926             assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i),
927                    "assert_array_equals", description,
928                    "property ${i}, property expected to be ${expected} but was ${actual}",
929                    {i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing",
930                    actual:actual.hasOwnProperty(i) ? "present" : "missing"});
931             assert(same_value(expected[i], actual[i]),
932                    "assert_array_equals", description,
933                    "property ${i}, expected ${expected} but got ${actual}",
934                    {i:i, expected:expected[i], actual:actual[i]});
935         }
936     }
937     expose(assert_array_equals, "assert_array_equals");
939     function assert_approx_equals(actual, expected, epsilon, description)
940     {
941         /*
942          * Test if two primitive numbers are equal withing +/- epsilon
943          */
944         assert(typeof actual === "number",
945                "assert_approx_equals", description,
946                "expected a number but got a ${type_actual}",
947                {type_actual:typeof actual});
949         assert(Math.abs(actual - expected) <= epsilon,
950                "assert_approx_equals", description,
951                "expected ${expected} +/- ${epsilon} but got ${actual}",
952                {expected:expected, actual:actual, epsilon:epsilon});
953     }
954     expose(assert_approx_equals, "assert_approx_equals");
956     function assert_less_than(actual, expected, description)
957     {
958         /*
959          * Test if a primitive number is less than another
960          */
961         assert(typeof actual === "number",
962                "assert_less_than", description,
963                "expected a number but got a ${type_actual}",
964                {type_actual:typeof actual});
966         assert(actual < expected,
967                "assert_less_than", description,
968                "expected a number less than ${expected} but got ${actual}",
969                {expected:expected, actual:actual});
970     }
971     expose(assert_less_than, "assert_less_than");
973     function assert_greater_than(actual, expected, description)
974     {
975         /*
976          * Test if a primitive number is greater than another
977          */
978         assert(typeof actual === "number",
979                "assert_greater_than", description,
980                "expected a number but got a ${type_actual}",
981                {type_actual:typeof actual});
983         assert(actual > expected,
984                "assert_greater_than", description,
985                "expected a number greater than ${expected} but got ${actual}",
986                {expected:expected, actual:actual});
987     }
988     expose(assert_greater_than, "assert_greater_than");
990     function assert_between_exclusive(actual, lower, upper, description)
991     {
992         /*
993          * Test if a primitive number is between two others
994          */
995         assert(typeof actual === "number",
996                "assert_between_exclusive", description,
997                "expected a number but got a ${type_actual}",
998                {type_actual:typeof actual});
1000         assert(actual > lower && actual < upper,
1001                "assert_between_exclusive", description,
1002                "expected a number greater than ${lower} " +
1003                "and less than ${upper} but got ${actual}",
1004                {lower:lower, upper:upper, actual:actual});
1005     }
1006     expose(assert_between_exclusive, "assert_between_exclusive");
1008     function assert_less_than_equal(actual, expected, description)
1009     {
1010         /*
1011          * Test if a primitive number is less than or equal to another
1012          */
1013         assert(typeof actual === "number",
1014                "assert_less_than_equal", description,
1015                "expected a number but got a ${type_actual}",
1016                {type_actual:typeof actual});
1018         assert(actual <= expected,
1019                "assert_less_than_equal", description,
1020                "expected a number less than or equal to ${expected} but got ${actual}",
1021                {expected:expected, actual:actual});
1022     }
1023     expose(assert_less_than_equal, "assert_less_than_equal");
1025     function assert_greater_than_equal(actual, expected, description)
1026     {
1027         /*
1028          * Test if a primitive number is greater than or equal to another
1029          */
1030         assert(typeof actual === "number",
1031                "assert_greater_than_equal", description,
1032                "expected a number but got a ${type_actual}",
1033                {type_actual:typeof actual});
1035         assert(actual >= expected,
1036                "assert_greater_than_equal", description,
1037                "expected a number greater than or equal to ${expected} but got ${actual}",
1038                {expected:expected, actual:actual});
1039     }
1040     expose(assert_greater_than_equal, "assert_greater_than_equal");
1042     function assert_between_inclusive(actual, lower, upper, description)
1043     {
1044         /*
1045          * Test if a primitive number is between to two others or equal to either of them
1046          */
1047         assert(typeof actual === "number",
1048                "assert_between_inclusive", description,
1049                "expected a number but got a ${type_actual}",
1050                {type_actual:typeof actual});
1052         assert(actual >= lower && actual <= upper,
1053                "assert_between_inclusive", description,
1054                "expected a number greater than or equal to ${lower} " +
1055                "and less than or equal to ${upper} but got ${actual}",
1056                {lower:lower, upper:upper, actual:actual});
1057     }
1058     expose(assert_between_inclusive, "assert_between_inclusive");
1060     function assert_regexp_match(actual, expected, description) {
1061         /*
1062          * Test if a string (actual) matches a regexp (expected)
1063          */
1064         assert(expected.test(actual),
1065                "assert_regexp_match", description,
1066                "expected ${expected} but got ${actual}",
1067                {expected:expected, actual:actual});
1068     }
1069     expose(assert_regexp_match, "assert_regexp_match");
1071     function assert_class_string(object, class_string, description) {
1072         assert_equals({}.toString.call(object), "[object " + class_string + "]",
1073                       description);
1074     }
1075     expose(assert_class_string, "assert_class_string");
1078     function _assert_own_property(name) {
1079         return function(object, property_name, description)
1080         {
1081             assert(object.hasOwnProperty(property_name),
1082                    name, description,
1083                    "expected property ${p} missing", {p:property_name});
1084         };
1085     }
1086     expose(_assert_own_property("assert_exists"), "assert_exists");
1087     expose(_assert_own_property("assert_own_property"), "assert_own_property");
1089     function assert_not_exists(object, property_name, description)
1090     {
1091         assert(!object.hasOwnProperty(property_name),
1092                "assert_not_exists", description,
1093                "unexpected property ${p} found", {p:property_name});
1094     }
1095     expose(assert_not_exists, "assert_not_exists");
1097     function _assert_inherits(name) {
1098         return function (object, property_name, description)
1099         {
1100             assert(typeof object === "object",
1101                    name, description,
1102                    "provided value is not an object");
1104             assert("hasOwnProperty" in object,
1105                    name, description,
1106                    "provided value is an object but has no hasOwnProperty method");
1108             assert(!object.hasOwnProperty(property_name),
1109                    name, description,
1110                    "property ${p} found on object expected in prototype chain",
1111                    {p:property_name});
1113             assert(property_name in object,
1114                    name, description,
1115                    "property ${p} not found in prototype chain",
1116                    {p:property_name});
1117         };
1118     }
1119     expose(_assert_inherits("assert_inherits"), "assert_inherits");
1120     expose(_assert_inherits("assert_idl_attribute"), "assert_idl_attribute");
1122     function assert_readonly(object, property_name, description)
1123     {
1124          var initial_value = object[property_name];
1125          try {
1126              //Note that this can have side effects in the case where
1127              //the property has PutForwards
1128              object[property_name] = initial_value + "a"; //XXX use some other value here?
1129              assert(same_value(object[property_name], initial_value),
1130                     "assert_readonly", description,
1131                     "changing property ${p} succeeded",
1132                     {p:property_name});
1133          } finally {
1134              object[property_name] = initial_value;
1135          }
1136     }
1137     expose(assert_readonly, "assert_readonly");
1139     function assert_throws(code, func, description)
1140     {
1141         try {
1142             func.call(this);
1143             assert(false, "assert_throws", description,
1144                    "${func} did not throw", {func:func});
1145         } catch (e) {
1146             if (e instanceof AssertionError) {
1147                 throw e;
1148             }
1149             if (code === null) {
1150                 return;
1151             }
1152             if (typeof code === "object") {
1153                 assert(typeof e == "object" && "name" in e && e.name == code.name,
1154                        "assert_throws", description,
1155                        "${func} threw ${actual} (${actual_name}) expected ${expected} (${expected_name})",
1156                                     {func:func, actual:e, actual_name:e.name,
1157                                      expected:code,
1158                                      expected_name:code.name});
1159                 return;
1160             }
1162             var code_name_map = {
1163                 INDEX_SIZE_ERR: 'IndexSizeError',
1164                 HIERARCHY_REQUEST_ERR: 'HierarchyRequestError',
1165                 WRONG_DOCUMENT_ERR: 'WrongDocumentError',
1166                 INVALID_CHARACTER_ERR: 'InvalidCharacterError',
1167                 NO_MODIFICATION_ALLOWED_ERR: 'NoModificationAllowedError',
1168                 NOT_FOUND_ERR: 'NotFoundError',
1169                 NOT_SUPPORTED_ERR: 'NotSupportedError',
1170                 INVALID_STATE_ERR: 'InvalidStateError',
1171                 SYNTAX_ERR: 'SyntaxError',
1172                 INVALID_MODIFICATION_ERR: 'InvalidModificationError',
1173                 NAMESPACE_ERR: 'NamespaceError',
1174                 INVALID_ACCESS_ERR: 'InvalidAccessError',
1175                 TYPE_MISMATCH_ERR: 'TypeMismatchError',
1176                 SECURITY_ERR: 'SecurityError',
1177                 NETWORK_ERR: 'NetworkError',
1178                 ABORT_ERR: 'AbortError',
1179                 URL_MISMATCH_ERR: 'URLMismatchError',
1180                 QUOTA_EXCEEDED_ERR: 'QuotaExceededError',
1181                 TIMEOUT_ERR: 'TimeoutError',
1182                 INVALID_NODE_TYPE_ERR: 'InvalidNodeTypeError',
1183                 DATA_CLONE_ERR: 'DataCloneError'
1184             };
1186             var name = code in code_name_map ? code_name_map[code] : code;
1188             var name_code_map = {
1189                 IndexSizeError: 1,
1190                 HierarchyRequestError: 3,
1191                 WrongDocumentError: 4,
1192                 InvalidCharacterError: 5,
1193                 NoModificationAllowedError: 7,
1194                 NotFoundError: 8,
1195                 NotSupportedError: 9,
1196                 InvalidStateError: 11,
1197                 SyntaxError: 12,
1198                 InvalidModificationError: 13,
1199                 NamespaceError: 14,
1200                 InvalidAccessError: 15,
1201                 TypeMismatchError: 17,
1202                 SecurityError: 18,
1203                 NetworkError: 19,
1204                 AbortError: 20,
1205                 URLMismatchError: 21,
1206                 QuotaExceededError: 22,
1207                 TimeoutError: 23,
1208                 InvalidNodeTypeError: 24,
1209                 DataCloneError: 25,
1211                 EncodingError: 0,
1212                 NotReadableError: 0,
1213                 UnknownError: 0,
1214                 ConstraintError: 0,
1215                 DataError: 0,
1216                 TransactionInactiveError: 0,
1217                 ReadOnlyError: 0,
1218                 VersionError: 0,
1219                 OperationError: 0,
1220             };
1222             if (!(name in name_code_map)) {
1223                 throw new AssertionError('Test bug: unrecognized DOMException code "' + code + '" passed to assert_throws()');
1224             }
1226             var required_props = { code: name_code_map[name] };
1228             if (required_props.code === 0 ||
1229                (typeof e == "object" &&
1230                 "name" in e &&
1231                 e.name !== e.name.toUpperCase() &&
1232                 e.name !== "DOMException")) {
1233                 // New style exception: also test the name property.
1234                 required_props.name = name;
1235             }
1237             //We'd like to test that e instanceof the appropriate interface,
1238             //but we can't, because we don't know what window it was created
1239             //in.  It might be an instanceof the appropriate interface on some
1240             //unknown other window.  TODO: Work around this somehow?
1242             assert(typeof e == "object",
1243                    "assert_throws", description,
1244                    "${func} threw ${e} with type ${type}, not an object",
1245                    {func:func, e:e, type:typeof e});
1247             for (var prop in required_props) {
1248                 assert(typeof e == "object" && prop in e && e[prop] == required_props[prop],
1249                        "assert_throws", description,
1250                        "${func} threw ${e} that is not a DOMException " + code + ": property ${prop} is equal to ${actual}, expected ${expected}",
1251                        {func:func, e:e, prop:prop, actual:e[prop], expected:required_props[prop]});
1252             }
1253         }
1254     }
1255     expose(assert_throws, "assert_throws");
1257     function assert_unreached(description) {
1258          assert(false, "assert_unreached", description,
1259                 "Reached unreachable code");
1260     }
1261     expose(assert_unreached, "assert_unreached");
1263     function assert_any(assert_func, actual, expected_array)
1264     {
1265         var args = [].slice.call(arguments, 3);
1266         var errors = [];
1267         var passed = false;
1268         forEach(expected_array,
1269                 function(expected)
1270                 {
1271                     try {
1272                         assert_func.apply(this, [actual, expected].concat(args));
1273                         passed = true;
1274                     } catch (e) {
1275                         errors.push(e.message);
1276                     }
1277                 });
1278         if (!passed) {
1279             throw new AssertionError(errors.join("\n\n"));
1280         }
1281     }
1282     expose(assert_any, "assert_any");
1284     function Test(name, properties)
1285     {
1286         if (tests.file_is_test && tests.tests.length) {
1287             throw new Error("Tried to create a test with file_is_test");
1288         }
1289         this.name = name;
1291         this.phase = this.phases.INITIAL;
1293         this.status = this.NOTRUN;
1294         this.timeout_id = null;
1295         this.index = null;
1297         this.properties = properties;
1298         var timeout = properties.timeout ? properties.timeout : settings.test_timeout;
1299         if (timeout !== null) {
1300             this.timeout_length = timeout * tests.timeout_multiplier;
1301         } else {
1302             this.timeout_length = null;
1303         }
1305         this.message = null;
1306         this.stack = null;
1308         this.steps = [];
1310         this.cleanup_callbacks = [];
1312         tests.push(this);
1313     }
1315     Test.statuses = {
1316         PASS:0,
1317         FAIL:1,
1318         TIMEOUT:2,
1319         NOTRUN:3
1320     };
1322     Test.prototype = merge({}, Test.statuses);
1324     Test.prototype.phases = {
1325         INITIAL:0,
1326         STARTED:1,
1327         HAS_RESULT:2,
1328         COMPLETE:3
1329     };
1331     Test.prototype.structured_clone = function()
1332     {
1333         if (!this._structured_clone) {
1334             var msg = this.message;
1335             msg = msg ? String(msg) : msg;
1336             this._structured_clone = merge({
1337                 name:String(this.name),
1338                 properties:merge({}, this.properties),
1339             }, Test.statuses);
1340         }
1341         this._structured_clone.status = this.status;
1342         this._structured_clone.message = this.message;
1343         this._structured_clone.stack = this.stack;
1344         this._structured_clone.index = this.index;
1345         return this._structured_clone;
1346     };
1348     Test.prototype.step = function(func, this_obj)
1349     {
1350         if (this.phase > this.phases.STARTED) {
1351             return;
1352         }
1353         this.phase = this.phases.STARTED;
1354         //If we don't get a result before the harness times out that will be a test timout
1355         this.set_status(this.TIMEOUT, "Test timed out");
1357         tests.started = true;
1358         tests.notify_test_state(this);
1360         if (this.timeout_id === null) {
1361             this.set_timeout();
1362         }
1364         this.steps.push(func);
1366         if (arguments.length === 1) {
1367             this_obj = this;
1368         }
1370         try {
1371             return func.apply(this_obj, Array.prototype.slice.call(arguments, 2));
1372         } catch (e) {
1373             if (this.phase >= this.phases.HAS_RESULT) {
1374                 return;
1375             }
1376             var message = String((typeof e === "object" && e !== null) ? e.message : e);
1377             var stack = e.stack ? e.stack : null;
1379             this.set_status(this.FAIL, message, stack);
1380             this.phase = this.phases.HAS_RESULT;
1381             this.done();
1382         }
1383     };
1385     Test.prototype.step_func = function(func, this_obj)
1386     {
1387         var test_this = this;
1389         if (arguments.length === 1) {
1390             this_obj = test_this;
1391         }
1393         return function()
1394         {
1395             return test_this.step.apply(test_this, [func, this_obj].concat(
1396                 Array.prototype.slice.call(arguments)));
1397         };
1398     };
1400     Test.prototype.step_func_done = function(func, this_obj)
1401     {
1402         var test_this = this;
1404         if (arguments.length === 1) {
1405             this_obj = test_this;
1406         }
1408         return function()
1409         {
1410             if (func) {
1411                 test_this.step.apply(test_this, [func, this_obj].concat(
1412                     Array.prototype.slice.call(arguments)));
1413             }
1414             test_this.done();
1415         };
1416     };
1418     Test.prototype.unreached_func = function(description)
1419     {
1420         return this.step_func(function() {
1421             assert_unreached(description);
1422         });
1423     };
1425     Test.prototype.add_cleanup = function(callback) {
1426         this.cleanup_callbacks.push(callback);
1427     };
1429     Test.prototype.force_timeout = function() {
1430         this.set_status(this.TIMEOUT);
1431         this.phase = this.phases.HAS_RESULT;
1432     };
1434     Test.prototype.set_timeout = function()
1435     {
1436         if (this.timeout_length !== null) {
1437             var this_obj = this;
1438             this.timeout_id = setTimeout(function()
1439                                          {
1440                                              this_obj.timeout();
1441                                          }, this.timeout_length);
1442         }
1443     };
1445     Test.prototype.set_status = function(status, message, stack)
1446     {
1447         this.status = status;
1448         this.message = message;
1449         this.stack = stack ? stack : null;
1450     };
1452     Test.prototype.timeout = function()
1453     {
1454         this.timeout_id = null;
1455         this.set_status(this.TIMEOUT, "Test timed out");
1456         this.phase = this.phases.HAS_RESULT;
1457         this.done();
1458     };
1460     Test.prototype.done = function()
1461     {
1462         if (this.phase == this.phases.COMPLETE) {
1463             return;
1464         }
1466         if (this.phase <= this.phases.STARTED) {
1467             this.set_status(this.PASS, null);
1468         }
1470         this.phase = this.phases.COMPLETE;
1472         clearTimeout(this.timeout_id);
1473         tests.result(this);
1474         this.cleanup();
1475     };
1477     Test.prototype.cleanup = function() {
1478         forEach(this.cleanup_callbacks,
1479                 function(cleanup_callback) {
1480                     cleanup_callback();
1481                 });
1482     };
1484     /*
1485      * A RemoteTest object mirrors a Test object on a remote worker. The
1486      * associated RemoteWorker updates the RemoteTest object in response to
1487      * received events. In turn, the RemoteTest object replicates these events
1488      * on the local document. This allows listeners (test result reporting
1489      * etc..) to transparently handle local and remote events.
1490      */
1491     function RemoteTest(clone) {
1492         var this_obj = this;
1493         Object.keys(clone).forEach(
1494                 function(key) {
1495                     this_obj[key] = clone[key];
1496                 });
1497         this.index = null;
1498         this.phase = this.phases.INITIAL;
1499         this.update_state_from(clone);
1500         tests.push(this);
1501     }
1503     RemoteTest.prototype.structured_clone = function() {
1504         var clone = {};
1505         Object.keys(this).forEach(
1506                 (function(key) {
1507                     if (typeof(this[key]) === "object") {
1508                         clone[key] = merge({}, this[key]);
1509                     } else {
1510                         clone[key] = this[key];
1511                     }
1512                 }).bind(this));
1513         clone.phases = merge({}, this.phases);
1514         return clone;
1515     };
1517     RemoteTest.prototype.cleanup = function() {};
1518     RemoteTest.prototype.phases = Test.prototype.phases;
1519     RemoteTest.prototype.update_state_from = function(clone) {
1520         this.status = clone.status;
1521         this.message = clone.message;
1522         this.stack = clone.stack;
1523         if (this.phase === this.phases.INITIAL) {
1524             this.phase = this.phases.STARTED;
1525         }
1526     };
1527     RemoteTest.prototype.done = function() {
1528         this.phase = this.phases.COMPLETE;
1529     }
1531     /*
1532      * A RemoteWorker listens for test events from a worker. These events are
1533      * then used to construct and maintain RemoteTest objects that mirror the
1534      * tests running on the remote worker.
1535      */
1536     function RemoteWorker(worker) {
1537         this.running = true;
1538         this.tests = new Array();
1540         var this_obj = this;
1541         worker.onerror = function(error) { this_obj.worker_error(error); };
1543         var message_port;
1545         if (is_service_worker(worker)) {
1546             if (window.MessageChannel) {
1547                 // The ServiceWorker's implicit MessagePort is currently not
1548                 // reliably accessible from the ServiceWorkerGlobalScope due to
1549                 // Blink setting MessageEvent.source to null for messages sent
1550                 // via ServiceWorker.postMessage(). Until that's resolved,
1551                 // create an explicit MessageChannel and pass one end to the
1552                 // worker.
1553                 var message_channel = new MessageChannel();
1554                 message_port = message_channel.port1;
1555                 message_port.start();
1556                 worker.postMessage({type: "connect"}, [message_channel.port2]);
1557             } else {
1558                 // If MessageChannel is not available, then try the
1559                 // ServiceWorker.postMessage() approach using MessageEvent.source
1560                 // on the other end.
1561                 message_port = navigator.serviceWorker;
1562                 worker.postMessage({type: "connect"});
1563             }
1564         } else if (is_shared_worker(worker)) {
1565             message_port = worker.port;
1566         } else {
1567             message_port = worker;
1568         }
1570         // Keeping a reference to the worker until worker_done() is seen
1571         // prevents the Worker object and its MessageChannel from going away
1572         // before all the messages are dispatched.
1573         this.worker = worker;
1575         message_port.onmessage =
1576             function(message) {
1577                 if (this_obj.running && (message.data.type in this_obj.message_handlers)) {
1578                     this_obj.message_handlers[message.data.type].call(this_obj, message.data);
1579                 }
1580             };
1581     }
1583     RemoteWorker.prototype.worker_error = function(error) {
1584         var message = error.message || String(error);
1585         var filename = (error.filename ? " " + error.filename: "");
1586         // FIXME: Display worker error states separately from main document
1587         // error state.
1588         this.worker_done({
1589             status: {
1590                 status: tests.status.ERROR,
1591                 message: "Error in worker" + filename + ": " + message,
1592                 stack: error.stack
1593             }
1594         });
1595         error.preventDefault();
1596     };
1598     RemoteWorker.prototype.test_state = function(data) {
1599         var remote_test = this.tests[data.test.index];
1600         if (!remote_test) {
1601             remote_test = new RemoteTest(data.test);
1602             this.tests[data.test.index] = remote_test;
1603         }
1604         remote_test.update_state_from(data.test);
1605         tests.notify_test_state(remote_test);
1606     };
1608     RemoteWorker.prototype.test_done = function(data) {
1609         var remote_test = this.tests[data.test.index];
1610         remote_test.update_state_from(data.test);
1611         remote_test.done();
1612         tests.result(remote_test);
1613     };
1615     RemoteWorker.prototype.worker_done = function(data) {
1616         if (tests.status.status === null &&
1617             data.status.status !== data.status.OK) {
1618             tests.status.status = data.status.status;
1619             tests.status.message = data.status.message;
1620             tests.status.stack = data.status.stack;
1621         }
1622         this.running = false;
1623         this.worker = null;
1624         if (tests.all_done()) {
1625             tests.complete();
1626         }
1627     };
1629     RemoteWorker.prototype.message_handlers = {
1630         test_state: RemoteWorker.prototype.test_state,
1631         result: RemoteWorker.prototype.test_done,
1632         complete: RemoteWorker.prototype.worker_done
1633     };
1635     /*
1636      * Harness
1637      */
1639     function TestsStatus()
1640     {
1641         this.status = null;
1642         this.message = null;
1643         this.stack = null;
1644     }
1646     TestsStatus.statuses = {
1647         OK:0,
1648         ERROR:1,
1649         TIMEOUT:2
1650     };
1652     TestsStatus.prototype = merge({}, TestsStatus.statuses);
1654     TestsStatus.prototype.structured_clone = function()
1655     {
1656         if (!this._structured_clone) {
1657             var msg = this.message;
1658             msg = msg ? String(msg) : msg;
1659             this._structured_clone = merge({
1660                 status:this.status,
1661                 message:msg,
1662                 stack:this.stack
1663             }, TestsStatus.statuses);
1664         }
1665         return this._structured_clone;
1666     };
1668     function Tests()
1669     {
1670         this.tests = [];
1671         this.num_pending = 0;
1673         this.phases = {
1674             INITIAL:0,
1675             SETUP:1,
1676             HAVE_TESTS:2,
1677             HAVE_RESULTS:3,
1678             COMPLETE:4
1679         };
1680         this.phase = this.phases.INITIAL;
1682         this.properties = {};
1684         this.wait_for_finish = false;
1685         this.processing_callbacks = false;
1687         this.allow_uncaught_exception = false;
1689         this.file_is_test = false;
1691         this.timeout_multiplier = 1;
1692         this.timeout_length = test_environment.test_timeout();
1693         this.timeout_id = null;
1695         this.start_callbacks = [];
1696         this.test_state_callbacks = [];
1697         this.test_done_callbacks = [];
1698         this.all_done_callbacks = [];
1700         this.pending_workers = [];
1702         this.status = new TestsStatus();
1704         var this_obj = this;
1706         test_environment.add_on_loaded_callback(function() {
1707             if (this_obj.all_done()) {
1708                 this_obj.complete();
1709             }
1710         });
1712         this.set_timeout();
1713     }
1715     Tests.prototype.setup = function(func, properties)
1716     {
1717         if (this.phase >= this.phases.HAVE_RESULTS) {
1718             return;
1719         }
1721         if (this.phase < this.phases.SETUP) {
1722             this.phase = this.phases.SETUP;
1723         }
1725         this.properties = properties;
1727         for (var p in properties) {
1728             if (properties.hasOwnProperty(p)) {
1729                 var value = properties[p];
1730                 if (p == "allow_uncaught_exception") {
1731                     this.allow_uncaught_exception = value;
1732                 } else if (p == "explicit_done" && value) {
1733                     this.wait_for_finish = true;
1734                 } else if (p == "explicit_timeout" && value) {
1735                     this.timeout_length = null;
1736                     if (this.timeout_id)
1737                     {
1738                         clearTimeout(this.timeout_id);
1739                     }
1740                 } else if (p == "timeout_multiplier") {
1741                     this.timeout_multiplier = value;
1742                 }
1743             }
1744         }
1746         if (func) {
1747             try {
1748                 func();
1749             } catch (e) {
1750                 this.status.status = this.status.ERROR;
1751                 this.status.message = String(e);
1752                 this.status.stack = e.stack ? e.stack : null;
1753             }
1754         }
1755         this.set_timeout();
1756     };
1758     Tests.prototype.set_file_is_test = function() {
1759         if (this.tests.length > 0) {
1760             throw new Error("Tried to set file as test after creating a test");
1761         }
1762         this.wait_for_finish = true;
1763         this.file_is_test = true;
1764         // Create the test, which will add it to the list of tests
1765         async_test();
1766     };
1768     Tests.prototype.set_timeout = function() {
1769         var this_obj = this;
1770         clearTimeout(this.timeout_id);
1771         if (this.timeout_length !== null) {
1772             this.timeout_id = setTimeout(function() {
1773                                              this_obj.timeout();
1774                                          }, this.timeout_length);
1775         }
1776     };
1778     Tests.prototype.timeout = function() {
1779         if (this.status.status === null) {
1780             this.status.status = this.status.TIMEOUT;
1781         }
1782         this.complete();
1783     };
1785     Tests.prototype.end_wait = function()
1786     {
1787         this.wait_for_finish = false;
1788         if (this.all_done()) {
1789             this.complete();
1790         }
1791     };
1793     Tests.prototype.push = function(test)
1794     {
1795         if (this.phase < this.phases.HAVE_TESTS) {
1796             this.start();
1797         }
1798         this.num_pending++;
1799         test.index = this.tests.push(test);
1800         this.notify_test_state(test);
1801     };
1803     Tests.prototype.notify_test_state = function(test) {
1804         var this_obj = this;
1805         forEach(this.test_state_callbacks,
1806                 function(callback) {
1807                     callback(test, this_obj);
1808                 });
1809     };
1811     Tests.prototype.all_done = function() {
1812         return (this.tests.length > 0 && test_environment.all_loaded &&
1813                 this.num_pending === 0 && !this.wait_for_finish &&
1814                 !this.processing_callbacks &&
1815                 !this.pending_workers.some(function(w) { return w.running; }));
1816     };
1818     Tests.prototype.start = function() {
1819         this.phase = this.phases.HAVE_TESTS;
1820         this.notify_start();
1821     };
1823     Tests.prototype.notify_start = function() {
1824         var this_obj = this;
1825         forEach (this.start_callbacks,
1826                  function(callback)
1827                  {
1828                      callback(this_obj.properties);
1829                  });
1830     };
1832     Tests.prototype.result = function(test)
1833     {
1834         if (this.phase > this.phases.HAVE_RESULTS) {
1835             return;
1836         }
1837         this.phase = this.phases.HAVE_RESULTS;
1838         this.num_pending--;
1839         this.notify_result(test);
1840     };
1842     Tests.prototype.notify_result = function(test) {
1843         var this_obj = this;
1844         this.processing_callbacks = true;
1845         forEach(this.test_done_callbacks,
1846                 function(callback)
1847                 {
1848                     callback(test, this_obj);
1849                 });
1850         this.processing_callbacks = false;
1851         if (this_obj.all_done()) {
1852             this_obj.complete();
1853         }
1854     };
1856     Tests.prototype.complete = function() {
1857         if (this.phase === this.phases.COMPLETE) {
1858             return;
1859         }
1860         this.phase = this.phases.COMPLETE;
1861         var this_obj = this;
1862         this.tests.forEach(
1863             function(x)
1864             {
1865                 if (x.phase < x.phases.COMPLETE) {
1866                     this_obj.notify_result(x);
1867                     x.cleanup();
1868                     x.phase = x.phases.COMPLETE;
1869                 }
1870             }
1871         );
1872         this.notify_complete();
1873     };
1875     Tests.prototype.notify_complete = function() {
1876         var this_obj = this;
1877         if (this.status.status === null) {
1878             this.status.status = this.status.OK;
1879         }
1881         forEach (this.all_done_callbacks,
1882                  function(callback)
1883                  {
1884                      callback(this_obj.tests, this_obj.status);
1885                  });
1886     };
1888     Tests.prototype.fetch_tests_from_worker = function(worker) {
1889         if (this.phase >= this.phases.COMPLETE) {
1890             return;
1891         }
1893         this.pending_workers.push(new RemoteWorker(worker));
1894     };
1896     function fetch_tests_from_worker(port) {
1897         tests.fetch_tests_from_worker(port);
1898     }
1899     expose(fetch_tests_from_worker, 'fetch_tests_from_worker');
1901     function timeout() {
1902         if (tests.timeout_length === null) {
1903             tests.timeout();
1904         }
1905     }
1906     expose(timeout, 'timeout');
1908     function add_start_callback(callback) {
1909         tests.start_callbacks.push(callback);
1910     }
1912     function add_test_state_callback(callback) {
1913         tests.test_state_callbacks.push(callback);
1914     }
1916     function add_result_callback(callback) {
1917         tests.test_done_callbacks.push(callback);
1918     }
1920     function add_completion_callback(callback) {
1921         tests.all_done_callbacks.push(callback);
1922     }
1924     expose(add_start_callback, 'add_start_callback');
1925     expose(add_test_state_callback, 'add_test_state_callback');
1926     expose(add_result_callback, 'add_result_callback');
1927     expose(add_completion_callback, 'add_completion_callback');
1929     function remove(array, item) {
1930         var index = array.indexOf(item);
1931         if (index > -1) {
1932             array.splice(index, 1);
1933         }
1934     }
1936     function remove_start_callback(callback) {
1937         remove(tests.start_callbacks, callback);
1938     }
1940     function remove_test_state_callback(callback) {
1941         remove(tests.test_state_callbacks, callback);
1942     }
1944     function remove_result_callback(callback) {
1945         remove(tests.test_done_callbacks, callback);
1946     }
1948     function remove_completion_callback(callback) {
1949        remove(tests.all_done_callbacks, callback);
1950     }
1952     /*
1953      * Output listener
1954     */
1956     function Output() {
1957         this.output_document = document;
1958         this.output_node = null;
1959         this.enabled = settings.output;
1960         this.phase = this.INITIAL;
1961     }
1963     Output.prototype.INITIAL = 0;
1964     Output.prototype.STARTED = 1;
1965     Output.prototype.HAVE_RESULTS = 2;
1966     Output.prototype.COMPLETE = 3;
1968     Output.prototype.setup = function(properties) {
1969         if (this.phase > this.INITIAL) {
1970             return;
1971         }
1973         //If output is disabled in testharnessreport.js the test shouldn't be
1974         //able to override that
1975         this.enabled = this.enabled && (properties.hasOwnProperty("output") ?
1976                                         properties.output : settings.output);
1977     };
1979     Output.prototype.init = function(properties) {
1980         if (this.phase >= this.STARTED) {
1981             return;
1982         }
1983         if (properties.output_document) {
1984             this.output_document = properties.output_document;
1985         } else {
1986             this.output_document = document;
1987         }
1988         this.phase = this.STARTED;
1989     };
1991     Output.prototype.resolve_log = function() {
1992         var output_document;
1993         if (typeof this.output_document === "function") {
1994             output_document = this.output_document.apply(undefined);
1995         } else {
1996             output_document = this.output_document;
1997         }
1998         if (!output_document) {
1999             return;
2000         }
2001         var node = output_document.getElementById("log");
2002         if (!node) {
2003             if (!document.body || document.readyState == "loading") {
2004                 return;
2005             }
2006             node = output_document.createElement("div");
2007             node.id = "log";
2008             output_document.body.appendChild(node);
2009         }
2010         this.output_document = output_document;
2011         this.output_node = node;
2012     };
2014     Output.prototype.show_status = function() {
2015         if (this.phase < this.STARTED) {
2016             this.init();
2017         }
2018         if (!this.enabled) {
2019             return;
2020         }
2021         if (this.phase < this.HAVE_RESULTS) {
2022             this.resolve_log();
2023             this.phase = this.HAVE_RESULTS;
2024         }
2025         var done_count = tests.tests.length - tests.num_pending;
2026         if (this.output_node) {
2027             if (done_count < 100 ||
2028                 (done_count < 1000 && done_count % 100 === 0) ||
2029                 done_count % 1000 === 0) {
2030                 this.output_node.textContent = "Running, " +
2031                     done_count + " complete, " +
2032                     tests.num_pending + " remain";
2033             }
2034         }
2035     };
2037     Output.prototype.show_results = function (tests, harness_status) {
2038         if (this.phase >= this.COMPLETE) {
2039             return;
2040         }
2041         if (!this.enabled) {
2042             return;
2043         }
2044         if (!this.output_node) {
2045             this.resolve_log();
2046         }
2047         this.phase = this.COMPLETE;
2049         var log = this.output_node;
2050         if (!log) {
2051             return;
2052         }
2053         var output_document = this.output_document;
2055         while (log.lastChild) {
2056             log.removeChild(log.lastChild);
2057         }
2059         var harness_url = get_harness_url();
2060         if (harness_url !== null) {
2061             var stylesheet = output_document.createElementNS(xhtml_ns, "link");
2062             stylesheet.setAttribute("rel", "stylesheet");
2063             stylesheet.setAttribute("href", harness_url + "testharness.css");
2064             var heads = output_document.getElementsByTagName("head");
2065             if (heads.length) {
2066                 heads[0].appendChild(stylesheet);
2067             }
2068         }
2070         var status_text_harness = {};
2071         status_text_harness[harness_status.OK] = "OK";
2072         status_text_harness[harness_status.ERROR] = "Error";
2073         status_text_harness[harness_status.TIMEOUT] = "Timeout";
2075         var status_text = {};
2076         status_text[Test.prototype.PASS] = "Pass";
2077         status_text[Test.prototype.FAIL] = "Fail";
2078         status_text[Test.prototype.TIMEOUT] = "Timeout";
2079         status_text[Test.prototype.NOTRUN] = "Not Run";
2081         var status_number = {};
2082         forEach(tests,
2083                 function(test) {
2084                     var status = status_text[test.status];
2085                     if (status_number.hasOwnProperty(status)) {
2086                         status_number[status] += 1;
2087                     } else {
2088                         status_number[status] = 1;
2089                     }
2090                 });
2092         function status_class(status)
2093         {
2094             return status.replace(/\s/g, '').toLowerCase();
2095         }
2097         var summary_template = ["section", {"id":"summary"},
2098                                 ["h2", {}, "Summary"],
2099                                 function()
2100                                 {
2102                                     var status = status_text_harness[harness_status.status];
2103                                     var rv = [["section", {},
2104                                                ["p", {},
2105                                                 "Harness status: ",
2106                                                 ["span", {"class":status_class(status)},
2107                                                  status
2108                                                 ],
2109                                                ]
2110                                               ]];
2112                                     if (harness_status.status === harness_status.ERROR) {
2113                                         rv[0].push(["pre", {}, harness_status.message]);
2114                                         if (harness_status.stack) {
2115                                             rv[0].push(["pre", {}, harness_status.stack]);
2116                                         }
2117                                     }
2118                                     return rv;
2119                                 },
2120                                 ["p", {}, "Found ${num_tests} tests"],
2121                                 function() {
2122                                     var rv = [["div", {}]];
2123                                     var i = 0;
2124                                     while (status_text.hasOwnProperty(i)) {
2125                                         if (status_number.hasOwnProperty(status_text[i])) {
2126                                             var status = status_text[i];
2127                                             rv[0].push(["div", {"class":status_class(status)},
2128                                                         ["label", {},
2129                                                          ["input", {type:"checkbox", checked:"checked"}],
2130                                                          status_number[status] + " " + status]]);
2131                                         }
2132                                         i++;
2133                                     }
2134                                     return rv;
2135                                 },
2136                                ];
2138         log.appendChild(render(summary_template, {num_tests:tests.length}, output_document));
2140         forEach(output_document.querySelectorAll("section#summary label"),
2141                 function(element)
2142                 {
2143                     on_event(element, "click",
2144                              function(e)
2145                              {
2146                                  if (output_document.getElementById("results") === null) {
2147                                      e.preventDefault();
2148                                      return;
2149                                  }
2150                                  var result_class = element.parentNode.getAttribute("class");
2151                                  var style_element = output_document.querySelector("style#hide-" + result_class);
2152                                  var input_element = element.querySelector("input");
2153                                  if (!style_element && !input_element.checked) {
2154                                      style_element = output_document.createElementNS(xhtml_ns, "style");
2155                                      style_element.id = "hide-" + result_class;
2156                                      style_element.textContent = "table#results > tbody > tr."+result_class+"{display:none}";
2157                                      output_document.body.appendChild(style_element);
2158                                  } else if (style_element && input_element.checked) {
2159                                      style_element.parentNode.removeChild(style_element);
2160                                  }
2161                              });
2162                 });
2164         // This use of innerHTML plus manual escaping is not recommended in
2165         // general, but is necessary here for performance.  Using textContent
2166         // on each individual <td> adds tens of seconds of execution time for
2167         // large test suites (tens of thousands of tests).
2168         function escape_html(s)
2169         {
2170             return s.replace(/\&/g, "&amp;")
2171                 .replace(/</g, "&lt;")
2172                 .replace(/"/g, "&quot;")
2173                 .replace(/'/g, "&#39;");
2174         }
2176         function has_assertions()
2177         {
2178             for (var i = 0; i < tests.length; i++) {
2179                 if (tests[i].properties.hasOwnProperty("assert")) {
2180                     return true;
2181                 }
2182             }
2183             return false;
2184         }
2186         function get_assertion(test)
2187         {
2188             if (test.properties.hasOwnProperty("assert")) {
2189                 if (Array.isArray(test.properties.assert)) {
2190                     return test.properties.assert.join(' ');
2191                 }
2192                 return test.properties.assert;
2193             }
2194             return '';
2195         }
2197         log.appendChild(document.createElementNS(xhtml_ns, "section"));
2198         var assertions = has_assertions();
2199         var html = "<h2>Details</h2><table id='results' " + (assertions ? "class='assertions'" : "" ) + ">" +
2200             "<thead><tr><th>Result</th><th>Test Name</th>" +
2201             (assertions ? "<th>Assertion</th>" : "") +
2202             "<th>Message</th></tr></thead>" +
2203             "<tbody>";
2204         for (var i = 0; i < tests.length; i++) {
2205             html += '<tr class="' +
2206                 escape_html(status_class(status_text[tests[i].status])) +
2207                 '"><td>' +
2208                 escape_html(status_text[tests[i].status]) +
2209                 "</td><td>" +
2210                 escape_html(tests[i].name) +
2211                 "</td><td>" +
2212                 (assertions ? escape_html(get_assertion(tests[i])) + "</td><td>" : "") +
2213                 escape_html(tests[i].message ? tests[i].message : " ") +
2214                 (tests[i].stack ? "<pre>" +
2215                  escape_html(tests[i].stack) +
2216                  "</pre>": "") +
2217                 "</td></tr>";
2218         }
2219         html += "</tbody></table>";
2220         try {
2221             log.lastChild.innerHTML = html;
2222         } catch (e) {
2223             log.appendChild(document.createElementNS(xhtml_ns, "p"))
2224                .textContent = "Setting innerHTML for the log threw an exception.";
2225             log.appendChild(document.createElementNS(xhtml_ns, "pre"))
2226                .textContent = html;
2227         }
2228     };
2230     /*
2231      * Template code
2232      *
2233      * A template is just a javascript structure. An element is represented as:
2234      *
2235      * [tag_name, {attr_name:attr_value}, child1, child2]
2236      *
2237      * the children can either be strings (which act like text nodes), other templates or
2238      * functions (see below)
2239      *
2240      * A text node is represented as
2241      *
2242      * ["{text}", value]
2243      *
2244      * String values have a simple substitution syntax; ${foo} represents a variable foo.
2245      *
2246      * It is possible to embed logic in templates by using a function in a place where a
2247      * node would usually go. The function must either return part of a template or null.
2248      *
2249      * In cases where a set of nodes are required as output rather than a single node
2250      * with children it is possible to just use a list
2251      * [node1, node2, node3]
2252      *
2253      * Usage:
2254      *
2255      * render(template, substitutions) - take a template and an object mapping
2256      * variable names to parameters and return either a DOM node or a list of DOM nodes
2257      *
2258      * substitute(template, substitutions) - take a template and variable mapping object,
2259      * make the variable substitutions and return the substituted template
2260      *
2261      */
2263     function is_single_node(template)
2264     {
2265         return typeof template[0] === "string";
2266     }
2268     function substitute(template, substitutions)
2269     {
2270         if (typeof template === "function") {
2271             var replacement = template(substitutions);
2272             if (!replacement) {
2273                 return null;
2274             }
2276             return substitute(replacement, substitutions);
2277         }
2279         if (is_single_node(template)) {
2280             return substitute_single(template, substitutions);
2281         }
2283         return filter(map(template, function(x) {
2284                               return substitute(x, substitutions);
2285                           }), function(x) {return x !== null;});
2286     }
2288     function substitute_single(template, substitutions)
2289     {
2290         var substitution_re = /\$\{([^ }]*)\}/g;
2292         function do_substitution(input) {
2293             var components = input.split(substitution_re);
2294             var rv = [];
2295             for (var i = 0; i < components.length; i += 2) {
2296                 rv.push(components[i]);
2297                 if (components[i + 1]) {
2298                     rv.push(String(substitutions[components[i + 1]]));
2299                 }
2300             }
2301             return rv;
2302         }
2304         function substitute_attrs(attrs, rv)
2305         {
2306             rv[1] = {};
2307             for (var name in template[1]) {
2308                 if (attrs.hasOwnProperty(name)) {
2309                     var new_name = do_substitution(name).join("");
2310                     var new_value = do_substitution(attrs[name]).join("");
2311                     rv[1][new_name] = new_value;
2312                 }
2313             }
2314         }
2316         function substitute_children(children, rv)
2317         {
2318             for (var i = 0; i < children.length; i++) {
2319                 if (children[i] instanceof Object) {
2320                     var replacement = substitute(children[i], substitutions);
2321                     if (replacement !== null) {
2322                         if (is_single_node(replacement)) {
2323                             rv.push(replacement);
2324                         } else {
2325                             extend(rv, replacement);
2326                         }
2327                     }
2328                 } else {
2329                     extend(rv, do_substitution(String(children[i])));
2330                 }
2331             }
2332             return rv;
2333         }
2335         var rv = [];
2336         rv.push(do_substitution(String(template[0])).join(""));
2338         if (template[0] === "{text}") {
2339             substitute_children(template.slice(1), rv);
2340         } else {
2341             substitute_attrs(template[1], rv);
2342             substitute_children(template.slice(2), rv);
2343         }
2345         return rv;
2346     }
2348     function make_dom_single(template, doc)
2349     {
2350         var output_document = doc || document;
2351         var element;
2352         if (template[0] === "{text}") {
2353             element = output_document.createTextNode("");
2354             for (var i = 1; i < template.length; i++) {
2355                 element.data += template[i];
2356             }
2357         } else {
2358             element = output_document.createElementNS(xhtml_ns, template[0]);
2359             for (var name in template[1]) {
2360                 if (template[1].hasOwnProperty(name)) {
2361                     element.setAttribute(name, template[1][name]);
2362                 }
2363             }
2364             for (var i = 2; i < template.length; i++) {
2365                 if (template[i] instanceof Object) {
2366                     var sub_element = make_dom(template[i]);
2367                     element.appendChild(sub_element);
2368                 } else {
2369                     var text_node = output_document.createTextNode(template[i]);
2370                     element.appendChild(text_node);
2371                 }
2372             }
2373         }
2375         return element;
2376     }
2378     function make_dom(template, substitutions, output_document)
2379     {
2380         if (is_single_node(template)) {
2381             return make_dom_single(template, output_document);
2382         }
2384         return map(template, function(x) {
2385                        return make_dom_single(x, output_document);
2386                    });
2387     }
2389     function render(template, substitutions, output_document)
2390     {
2391         return make_dom(substitute(template, substitutions), output_document);
2392     }
2394     /*
2395      * Utility funcions
2396      */
2397     function assert(expected_true, function_name, description, error, substitutions)
2398     {
2399         if (tests.tests.length === 0) {
2400             tests.set_file_is_test();
2401         }
2402         if (expected_true !== true) {
2403             var msg = make_message(function_name, description,
2404                                    error, substitutions);
2405             throw new AssertionError(msg);
2406         }
2407     }
2409     function AssertionError(message)
2410     {
2411         this.message = message;
2412         this.stack = this.get_stack();
2413     }
2415     AssertionError.prototype = Object.create(Error.prototype);
2417     AssertionError.prototype.get_stack = function() {
2418         var stack = new Error().stack;
2419         // IE11 does not initialize 'Error.stack' until the object is thrown.
2420         if (!stack) {
2421             try {
2422                 throw new Error();
2423             } catch (e) {
2424                 stack = e.stack;
2425             }
2426         }
2428         var lines = stack.split("\n");
2430         // Create a pattern to match stack frames originating within testharness.js.  These include the
2431         // script URL, followed by the line/col (e.g., '/resources/testharness.js:120:21').
2432         var re = new RegExp((get_script_url() || "\\btestharness.js") + ":\\d+:\\d+");
2434         // Some browsers include a preamble that specifies the type of the error object.  Skip this by
2435         // advancing until we find the first stack frame originating from testharness.js.
2436         var i = 0;
2437         while (!re.test(lines[i]) && i < lines.length) {
2438             i++;
2439         }
2441         // Then skip the top frames originating from testharness.js to begin the stack at the test code.
2442         while (re.test(lines[i]) && i < lines.length) {
2443             i++;
2444         }
2446         // Paranoid check that we didn't skip all frames.  If so, return the original stack unmodified.
2447         if (i >= lines.length) {
2448             return stack;
2449         }
2451         return lines.slice(i).join("\n");
2452     }
2454     function make_message(function_name, description, error, substitutions)
2455     {
2456         for (var p in substitutions) {
2457             if (substitutions.hasOwnProperty(p)) {
2458                 substitutions[p] = format_value(substitutions[p]);
2459             }
2460         }
2461         var node_form = substitute(["{text}", "${function_name}: ${description}" + error],
2462                                    merge({function_name:function_name,
2463                                           description:(description?description + " ":"")},
2464                                           substitutions));
2465         return node_form.slice(1).join("");
2466     }
2468     function filter(array, callable, thisObj) {
2469         var rv = [];
2470         for (var i = 0; i < array.length; i++) {
2471             if (array.hasOwnProperty(i)) {
2472                 var pass = callable.call(thisObj, array[i], i, array);
2473                 if (pass) {
2474                     rv.push(array[i]);
2475                 }
2476             }
2477         }
2478         return rv;
2479     }
2481     function map(array, callable, thisObj)
2482     {
2483         var rv = [];
2484         rv.length = array.length;
2485         for (var i = 0; i < array.length; i++) {
2486             if (array.hasOwnProperty(i)) {
2487                 rv[i] = callable.call(thisObj, array[i], i, array);
2488             }
2489         }
2490         return rv;
2491     }
2493     function extend(array, items)
2494     {
2495         Array.prototype.push.apply(array, items);
2496     }
2498     function forEach(array, callback, thisObj)
2499     {
2500         for (var i = 0; i < array.length; i++) {
2501             if (array.hasOwnProperty(i)) {
2502                 callback.call(thisObj, array[i], i, array);
2503             }
2504         }
2505     }
2507     function merge(a,b)
2508     {
2509         var rv = {};
2510         var p;
2511         for (p in a) {
2512             rv[p] = a[p];
2513         }
2514         for (p in b) {
2515             rv[p] = b[p];
2516         }
2517         return rv;
2518     }
2520     function expose(object, name)
2521     {
2522         var components = name.split(".");
2523         var target = test_environment.global_scope();
2524         for (var i = 0; i < components.length - 1; i++) {
2525             if (!(components[i] in target)) {
2526                 target[components[i]] = {};
2527             }
2528             target = target[components[i]];
2529         }
2530         target[components[components.length - 1]] = object;
2531     }
2533     function is_same_origin(w) {
2534         try {
2535             'random_prop' in w;
2536             return true;
2537         } catch (e) {
2538             return false;
2539         }
2540     }
2542     /** Returns the 'src' URL of the first <script> tag in the page to include the file 'testharness.js'. */
2543     function get_script_url()
2544     {
2545         if (!('document' in self)) {
2546             return undefined;
2547         }
2549         var scripts = document.getElementsByTagName("script");
2550         for (var i = 0; i < scripts.length; i++) {
2551             var src;
2552             if (scripts[i].src) {
2553                 src = scripts[i].src;
2554             } else if (scripts[i].href) {
2555                 //SVG case
2556                 src = scripts[i].href.baseVal;
2557             }
2559             var matches = src && src.match(/^(.*\/|)testharness\.js$/);
2560             if (matches) {
2561                 return src;
2562             }
2563         }
2564         return undefined;
2565     }
2567     /** Returns the URL path at which the files for testharness.js are assumed to reside (e.g., '/resources/').
2568         The path is derived from inspecting the 'src' of the <script> tag that included 'testharness.js'. */
2569     function get_harness_url()
2570     {
2571         var script_url = get_script_url();
2573         // Exclude the 'testharness.js' file from the returned path, but '+ 1' to include the trailing slash.
2574         return script_url ? script_url.slice(0, script_url.lastIndexOf('/') + 1) : undefined;
2575     }
2577     function supports_post_message(w)
2578     {
2579         var supports;
2580         var type;
2581         // Given IE implements postMessage across nested iframes but not across
2582         // windows or tabs, you can't infer cross-origin communication from the presence
2583         // of postMessage on the current window object only.
2584         //
2585         // Touching the postMessage prop on a window can throw if the window is
2586         // not from the same origin AND post message is not supported in that
2587         // browser. So just doing an existence test here won't do, you also need
2588         // to wrap it in a try..cacth block.
2589         try {
2590             type = typeof w.postMessage;
2591             if (type === "function") {
2592                 supports = true;
2593             }
2595             // IE8 supports postMessage, but implements it as a host object which
2596             // returns "object" as its `typeof`.
2597             else if (type === "object") {
2598                 supports = true;
2599             }
2601             // This is the case where postMessage isn't supported AND accessing a
2602             // window property across origins does NOT throw (e.g. old Safari browser).
2603             else {
2604                 supports = false;
2605             }
2606         } catch (e) {
2607             // This is the case where postMessage isn't supported AND accessing a
2608             // window property across origins throws (e.g. old Firefox browser).
2609             supports = false;
2610         }
2611         return supports;
2612     }
2614     /**
2615      * Setup globals
2616      */
2618     var tests = new Tests();
2620     addEventListener("error", function(e) {
2621         if (tests.file_is_test) {
2622             var test = tests.tests[0];
2623             if (test.phase >= test.phases.HAS_RESULT) {
2624                 return;
2625             }
2626             test.set_status(test.FAIL, e.message, e.stack);
2627             test.phase = test.phases.HAS_RESULT;
2628             test.done();
2629             done();
2630         } else if (!tests.allow_uncaught_exception) {
2631             tests.status.status = tests.status.ERROR;
2632             tests.status.message = e.message;
2633             tests.status.stack = e.stack;
2634         }
2635     });
2637     test_environment.on_tests_ready();
2639 })();
2640 // vim: set expandtab shiftwidth=4 tabstop=4: