2 /*jshint latedef: nofunc*/
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 */
18 // default timeout is 10 seconds, test can override if needed
26 message_events: ["start", "test_state", "result", "completion"]
29 var xhtml_ns = "http://www.w3.org/1999/xhtml";
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:
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();
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);
45 * // Should return a new unique default test name.
46 * DOMString next_default_test_name();
48 * // Should return the test harness timeout duration in milliseconds.
49 * float test_timeout();
51 * // Should return the global scope object.
52 * object global_scope();
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.
62 function WindowTestEnvironment() {
63 this.name_counter = 0;
64 this.window_cache = null;
65 this.output_handler = null;
66 this.all_loaded = false;
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});
77 test_state: [add_test_state_callback, remove_test_state_callback,
79 this_obj._dispatch("test_state_callback", [test],
81 test: test.structured_clone()});
83 result: [add_result_callback, remove_result_callback,
85 this_obj.output_handler.show_status();
86 this_obj._dispatch("result_callback", [test],
88 test: test.structured_clone()});
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();
95 this_obj._dispatch("completion_callback", [tests, harness_status],
98 status: harness_status.structured_clone()});
102 on_event(window, 'load', function() {
103 this_obj.all_loaded = true;
107 WindowTestEnvironment.prototype._dispatch = function(selector, callback_args, message_arg) {
108 this._forEach_windows(
109 function(w, same_origin) {
112 var has_selector = selector in w;
114 // If document.domain was set at some point same_origin can be
115 // wrong and the above will fail.
116 has_selector = false;
120 w[selector].apply(undefined, callback_args);
128 if (supports_post_message(w) && w !== self) {
129 w.postMessage(message_arg, "*");
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
139 var cache = this.window_cache;
141 cache = [[self, true]];
145 var origins = location.ancestorOrigins;
146 while (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.
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.
160 so = (location.origin == origins[i]);
162 so = is_same_origin(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)]);
174 this.window_cache = cache;
179 callback.apply(null, a);
183 WindowTestEnvironment.prototype.on_tests_ready = function() {
184 var output = new Output();
185 this.output_handler = output;
189 add_start_callback(function (properties) {
190 this_obj.output_handler.init(properties);
193 add_test_state_callback(function(test) {
194 this_obj.output_handler.show_status();
197 add_result_callback(function (test) {
198 this_obj.output_handler.show_status();
201 add_completion_callback(function (tests, harness_status) {
202 this_obj.output_handler.show_results(tests, harness_status);
204 this.setup_messages(settings.message_events);
207 WindowTestEnvironment.prototype.setup_messages = function(new_events) {
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]);
218 this.message_events = new_events;
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 : "";
227 return prefix + suffix;
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);
237 WindowTestEnvironment.prototype.add_on_loaded_callback = function(callback) {
238 on_event(window, 'load', callback);
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;
251 return settings.harness_timeout.normal;
254 WindowTestEnvironment.prototype.global_scope = function() {
259 * Base TestEnvironment implementation for a generic web worker.
261 * Workers accumulate test results. One or more clients can connect and
262 * retrieve results from a worker at any time.
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
270 * A client document using testharness can use fetch_tests_from_worker() to
271 * retrieve results from a worker. See apisample16.html.
273 function WorkerTestEnvironment() {
274 this.name_counter = 0;
275 this.all_loaded = true;
276 this.message_list = [];
277 this.message_ports = [];
280 WorkerTestEnvironment.prototype._dispatch = function(message) {
281 this.message_list.push(message);
282 for (var i = 0; i < this.message_ports.length; ++i)
284 this.message_ports[i].postMessage(message);
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)
294 port.postMessage(this.message_list[i]);
298 WorkerTestEnvironment.prototype.next_default_test_name = function() {
299 var suffix = this.name_counter > 0 ? " " + this.name_counter : "";
301 return "Untitled" + suffix;
304 WorkerTestEnvironment.prototype.on_new_harness_properties = function() {};
306 WorkerTestEnvironment.prototype.on_tests_ready = function() {
309 function(properties) {
312 properties: properties,
315 add_test_state_callback(
319 test: test.structured_clone()
326 test: test.structured_clone()
329 add_completion_callback(
330 function(tests, harness_status) {
335 return test.structured_clone();
337 status: harness_status.structured_clone()
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.
350 WorkerTestEnvironment.prototype.global_scope = function() {
355 * Dedicated web workers.
356 * https://html.spec.whatwg.org/multipage/workers.html#dedicatedworkerglobalscope
358 * This class is used as the test_environment when testharness is running
359 * inside a dedicated worker.
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);
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;
378 * Shared web workers.
379 * https://html.spec.whatwg.org/multipage/workers.html#sharedworkerglobalscope
381 * This class is used as the test_environment when testharness is running
382 * inside a shared web worker.
384 function SharedWorkerTestEnvironment() {
385 WorkerTestEnvironment.call(this);
387 // Shared workers receive message ports via the 'onconnect' event for
389 self.addEventListener("connect",
390 function(message_event) {
391 this_obj._add_message_port(message_event.source);
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;
405 * http://www.w3.org/TR/service-workers/
407 * This class is used as the test_environment when testharness is running
408 * inside a service worker.
410 function ServiceWorkerTestEnvironment() {
411 WorkerTestEnvironment.call(this);
412 this.all_loaded = false;
413 this.on_loaded_callback = null;
415 self.addEventListener("message",
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();
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);
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",
442 this_obj.all_loaded = true;
443 if (this_obj.on_loaded_callback) {
444 this_obj.on_loaded_callback();
448 ServiceWorkerTestEnvironment.prototype = Object.create(WorkerTestEnvironment.prototype);
450 ServiceWorkerTestEnvironment.prototype.add_on_loaded_callback = function(callback) {
451 if (this.all_loaded) {
454 this.on_loaded_callback = callback;
458 function create_test_environment() {
459 if ('document' in self) {
460 return new WindowTestEnvironment();
462 if ('DedicatedWorkerGlobalScope' in self &&
463 self instanceof DedicatedWorkerGlobalScope) {
464 return new DedicatedWorkerTestEnvironment();
466 if ('SharedWorkerGlobalScope' in self &&
467 self instanceof SharedWorkerGlobalScope) {
468 return new SharedWorkerTestEnvironment();
470 if ('ServiceWorkerGlobalScope' in self &&
471 self instanceof ServiceWorkerGlobalScope) {
472 return new ServiceWorkerTestEnvironment();
474 throw new Error("Unsupported test environment");
477 var test_environment = create_test_environment();
479 function is_shared_worker(worker) {
480 return 'SharedWorker' in self && worker instanceof SharedWorker;
483 function is_service_worker(worker) {
484 return 'ServiceWorker' in self && worker instanceof ServiceWorker;
491 function test(func, name, properties)
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) {
502 function async_test(func, name, properties)
504 if (typeof func !== "function") {
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);
513 test_obj.step(func, test_obj, test_obj);
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();
526 tests.promise_tests = tests.promise_tests.then(function() {
527 return Promise.resolve(test.step(func, test, test))
532 .catch(test.step_func(
534 if (value instanceof AssertionError) {
537 assert(false, "promise_test", null,
538 "Unhandled rejection with value: ${value}", {value:value});
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 });
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.
554 function EventWatcher(test, watchedNode, eventTypes)
556 if (typeof eventTypes == 'string') {
557 eventTypes = [eventTypes];
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();
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;
581 for (var i = 0; i < eventTypes.length; i++) {
582 watchedNode.addEventListener(eventTypes[i], eventHandler);
586 * Returns a Promise that will resolve after the specified event or
587 * series of events has occured.
589 this.wait_for = function(types) {
591 return Promise.reject('Already waiting for an event or events');
593 if (typeof types == 'string') {
596 return new Promise(function(resolve, reject) {
605 function stop_watching() {
606 for (var i = 0; i < eventTypes.length; i++) {
607 watchedNode.removeEventListener(eventTypes[i], eventHandler);
611 test.add_cleanup(stop_watching);
615 expose(EventWatcher, 'EventWatcher');
617 function setup(func_or_properties, maybe_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;
627 properties = func_or_properties;
629 tests.setup(func, properties);
630 test_environment.on_new_harness_properties(properties);
634 if (tests.tests.length === 0) {
635 tests.set_file_is_test();
637 if (tests.file_is_test) {
638 tests.tests[0].done();
643 function generate_tests(func, args, properties) {
644 forEach(args, function(x, i)
649 func.apply(this, x.slice(1));
652 Array.isArray(properties) ? properties[i] : properties);
656 function on_event(object, event, callback)
658 object.addEventListener(event, callback, false);
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');
671 * Return a string truncated to the given length, with ... added at the end
674 function truncate(s, len)
676 if (s.length > len) {
677 return s.substring(0, len - 3) + "...";
683 * Return true if object is probably a Node object.
685 function is_node(object)
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) {
698 // The object is probably Node.prototype or another prototype
699 // object that inherits from it, and not a Node instance.
708 * Convert a value to a nice, human-readable string
710 function format_value(val, seen)
715 if (typeof val === "object" && val !== null) {
716 if (seen.indexOf(val) >= 0) {
721 if (Array.isArray(val)) {
722 return "[" + val.map(function(x) {return format_value(x, seen);}).join(", ") + "]";
725 switch (typeof val) {
727 val = val.replace("\\", "\\\\");
728 for (var i = 0; i < 32; 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;
764 val = val.replace(RegExp(String.fromCharCode(i), "g"), replace);
766 return '"' + val.replace(/"/g, '\\"') + '"';
771 // In JavaScript, -0 === 0 and String(-0) == "0", so we have to
773 if (val === -0 && 1/val === -Infinity) {
782 // Special-case Node objects, since those come up a lot in my tests. I
783 // ignore namespaces.
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 + '"';
791 ret += ">" + val.innerHTML + "</" + val.localName + ">";
792 return "Element node " + truncate(ret, 60);
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");
806 return "Node object of unknown type";
812 return typeof val + ' "' + truncate(String(val), 60) + '"';
815 expose(format_value, "format_value");
821 function assert_true(actual, description)
823 assert(actual === true, "assert_true", description,
824 "expected true got ${actual}", {actual:actual});
826 expose(assert_true, "assert_true");
828 function assert_false(actual, description)
830 assert(actual === false, "assert_false", description,
831 "expected false got ${actual}", {actual:actual});
833 expose(assert_false, "assert_false");
835 function same_value(x, y) {
840 if (x === 0 && y === 0) {
841 //Distinguish +0 and -0
847 function assert_equals(actual, expected, description)
850 * Test if two primitives are equal or two objects
851 * are the same object
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});
859 assert(same_value(actual, expected), "assert_equals", description,
860 "expected ${expected} but got ${actual}",
861 {expected:expected, actual:actual});
863 expose(assert_equals, "assert_equals");
865 function assert_not_equals(actual, expected, description)
868 * Test if two primitives are unequal or two objects
869 * are different objects
871 assert(!same_value(actual, expected), "assert_not_equals", description,
872 "got disallowed value ${actual}",
875 expose(assert_not_equals, "assert_not_equals");
877 function assert_in_array(actual, expected, description)
879 assert(expected.indexOf(actual) != -1, "assert_in_array", description,
880 "value ${actual} not in array ${expected}",
881 {actual:actual, expected:expected});
883 expose(assert_in_array, "assert_in_array");
885 function assert_object_equals(actual, expected, description)
887 //This needs to be improved a great deal
888 function check_equal(actual, expected, stack)
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);
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});
907 for (p in expected) {
908 assert(actual.hasOwnProperty(p),
909 "assert_object_equals", description,
910 "expected property ${p} missing", {p:p});
914 check_equal(actual, expected, []);
916 expose(assert_object_equals, "assert_object_equals");
918 function assert_array_equals(actual, expected, description)
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]});
937 expose(assert_array_equals, "assert_array_equals");
939 function assert_approx_equals(actual, expected, epsilon, description)
942 * Test if two primitive numbers are equal withing +/- epsilon
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});
954 expose(assert_approx_equals, "assert_approx_equals");
956 function assert_less_than(actual, expected, description)
959 * Test if a primitive number is less than another
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});
971 expose(assert_less_than, "assert_less_than");
973 function assert_greater_than(actual, expected, description)
976 * Test if a primitive number is greater than another
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});
988 expose(assert_greater_than, "assert_greater_than");
990 function assert_between_exclusive(actual, lower, upper, description)
993 * Test if a primitive number is between two others
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});
1006 expose(assert_between_exclusive, "assert_between_exclusive");
1008 function assert_less_than_equal(actual, expected, description)
1011 * Test if a primitive number is less than or equal to another
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});
1023 expose(assert_less_than_equal, "assert_less_than_equal");
1025 function assert_greater_than_equal(actual, expected, description)
1028 * Test if a primitive number is greater than or equal to another
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});
1040 expose(assert_greater_than_equal, "assert_greater_than_equal");
1042 function assert_between_inclusive(actual, lower, upper, description)
1045 * Test if a primitive number is between to two others or equal to either of them
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});
1058 expose(assert_between_inclusive, "assert_between_inclusive");
1060 function assert_regexp_match(actual, expected, description) {
1062 * Test if a string (actual) matches a regexp (expected)
1064 assert(expected.test(actual),
1065 "assert_regexp_match", description,
1066 "expected ${expected} but got ${actual}",
1067 {expected:expected, actual:actual});
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 + "]",
1075 expose(assert_class_string, "assert_class_string");
1078 function _assert_own_property(name) {
1079 return function(object, property_name, description)
1081 assert(object.hasOwnProperty(property_name),
1083 "expected property ${p} missing", {p:property_name});
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)
1091 assert(!object.hasOwnProperty(property_name),
1092 "assert_not_exists", description,
1093 "unexpected property ${p} found", {p:property_name});
1095 expose(assert_not_exists, "assert_not_exists");
1097 function _assert_inherits(name) {
1098 return function (object, property_name, description)
1100 assert(typeof object === "object",
1102 "provided value is not an object");
1104 assert("hasOwnProperty" in object,
1106 "provided value is an object but has no hasOwnProperty method");
1108 assert(!object.hasOwnProperty(property_name),
1110 "property ${p} found on object expected in prototype chain",
1113 assert(property_name in object,
1115 "property ${p} not found in prototype chain",
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)
1124 var initial_value = object[property_name];
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",
1134 object[property_name] = initial_value;
1137 expose(assert_readonly, "assert_readonly");
1139 function assert_throws(code, func, description)
1143 assert(false, "assert_throws", description,
1144 "${func} did not throw", {func:func});
1146 if (e instanceof AssertionError) {
1149 if (code === null) {
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,
1158 expected_name:code.name});
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'
1186 var name = code in code_name_map ? code_name_map[code] : code;
1188 var name_code_map = {
1190 HierarchyRequestError: 3,
1191 WrongDocumentError: 4,
1192 InvalidCharacterError: 5,
1193 NoModificationAllowedError: 7,
1195 NotSupportedError: 9,
1196 InvalidStateError: 11,
1198 InvalidModificationError: 13,
1200 InvalidAccessError: 15,
1201 TypeMismatchError: 17,
1205 URLMismatchError: 21,
1206 QuotaExceededError: 22,
1208 InvalidNodeTypeError: 24,
1212 NotReadableError: 0,
1216 TransactionInactiveError: 0,
1222 if (!(name in name_code_map)) {
1223 throw new AssertionError('Test bug: unrecognized DOMException code "' + code + '" passed to assert_throws()');
1226 var required_props = { code: name_code_map[name] };
1228 if (required_props.code === 0 ||
1229 (typeof e == "object" &&
1231 e.name !== e.name.toUpperCase() &&
1232 e.name !== "DOMException")) {
1233 // New style exception: also test the name property.
1234 required_props.name = name;
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]});
1255 expose(assert_throws, "assert_throws");
1257 function assert_unreached(description) {
1258 assert(false, "assert_unreached", description,
1259 "Reached unreachable code");
1261 expose(assert_unreached, "assert_unreached");
1263 function assert_any(assert_func, actual, expected_array)
1265 var args = [].slice.call(arguments, 3);
1268 forEach(expected_array,
1272 assert_func.apply(this, [actual, expected].concat(args));
1275 errors.push(e.message);
1279 throw new AssertionError(errors.join("\n\n"));
1282 expose(assert_any, "assert_any");
1284 function Test(name, properties)
1286 if (tests.file_is_test && tests.tests.length) {
1287 throw new Error("Tried to create a test with file_is_test");
1291 this.phase = this.phases.INITIAL;
1293 this.status = this.NOTRUN;
1294 this.timeout_id = 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;
1302 this.timeout_length = null;
1305 this.message = null;
1310 this.cleanup_callbacks = [];
1322 Test.prototype = merge({}, Test.statuses);
1324 Test.prototype.phases = {
1331 Test.prototype.structured_clone = function()
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),
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;
1348 Test.prototype.step = function(func, this_obj)
1350 if (this.phase > this.phases.STARTED) {
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) {
1364 this.steps.push(func);
1366 if (arguments.length === 1) {
1371 return func.apply(this_obj, Array.prototype.slice.call(arguments, 2));
1373 if (this.phase >= this.phases.HAS_RESULT) {
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;
1385 Test.prototype.step_func = function(func, this_obj)
1387 var test_this = this;
1389 if (arguments.length === 1) {
1390 this_obj = test_this;
1395 return test_this.step.apply(test_this, [func, this_obj].concat(
1396 Array.prototype.slice.call(arguments)));
1400 Test.prototype.step_func_done = function(func, this_obj)
1402 var test_this = this;
1404 if (arguments.length === 1) {
1405 this_obj = test_this;
1411 test_this.step.apply(test_this, [func, this_obj].concat(
1412 Array.prototype.slice.call(arguments)));
1418 Test.prototype.unreached_func = function(description)
1420 return this.step_func(function() {
1421 assert_unreached(description);
1425 Test.prototype.add_cleanup = function(callback) {
1426 this.cleanup_callbacks.push(callback);
1429 Test.prototype.force_timeout = function() {
1430 this.set_status(this.TIMEOUT);
1431 this.phase = this.phases.HAS_RESULT;
1434 Test.prototype.set_timeout = function()
1436 if (this.timeout_length !== null) {
1437 var this_obj = this;
1438 this.timeout_id = setTimeout(function()
1441 }, this.timeout_length);
1445 Test.prototype.set_status = function(status, message, stack)
1447 this.status = status;
1448 this.message = message;
1449 this.stack = stack ? stack : null;
1452 Test.prototype.timeout = function()
1454 this.timeout_id = null;
1455 this.set_status(this.TIMEOUT, "Test timed out");
1456 this.phase = this.phases.HAS_RESULT;
1460 Test.prototype.done = function()
1462 if (this.phase == this.phases.COMPLETE) {
1466 if (this.phase <= this.phases.STARTED) {
1467 this.set_status(this.PASS, null);
1470 this.phase = this.phases.COMPLETE;
1472 clearTimeout(this.timeout_id);
1477 Test.prototype.cleanup = function() {
1478 forEach(this.cleanup_callbacks,
1479 function(cleanup_callback) {
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.
1491 function RemoteTest(clone) {
1492 var this_obj = this;
1493 Object.keys(clone).forEach(
1495 this_obj[key] = clone[key];
1498 this.phase = this.phases.INITIAL;
1499 this.update_state_from(clone);
1503 RemoteTest.prototype.structured_clone = function() {
1505 Object.keys(this).forEach(
1507 if (typeof(this[key]) === "object") {
1508 clone[key] = merge({}, this[key]);
1510 clone[key] = this[key];
1513 clone.phases = merge({}, this.phases);
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;
1527 RemoteTest.prototype.done = function() {
1528 this.phase = this.phases.COMPLETE;
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.
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); };
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
1553 var message_channel = new MessageChannel();
1554 message_port = message_channel.port1;
1555 message_port.start();
1556 worker.postMessage({type: "connect"}, [message_channel.port2]);
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"});
1564 } else if (is_shared_worker(worker)) {
1565 message_port = worker.port;
1567 message_port = worker;
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 =
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);
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
1590 status: tests.status.ERROR,
1591 message: "Error in worker" + filename + ": " + message,
1595 error.preventDefault();
1598 RemoteWorker.prototype.test_state = function(data) {
1599 var remote_test = this.tests[data.test.index];
1601 remote_test = new RemoteTest(data.test);
1602 this.tests[data.test.index] = remote_test;
1604 remote_test.update_state_from(data.test);
1605 tests.notify_test_state(remote_test);
1608 RemoteWorker.prototype.test_done = function(data) {
1609 var remote_test = this.tests[data.test.index];
1610 remote_test.update_state_from(data.test);
1612 tests.result(remote_test);
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;
1622 this.running = false;
1624 if (tests.all_done()) {
1629 RemoteWorker.prototype.message_handlers = {
1630 test_state: RemoteWorker.prototype.test_state,
1631 result: RemoteWorker.prototype.test_done,
1632 complete: RemoteWorker.prototype.worker_done
1639 function TestsStatus()
1642 this.message = null;
1646 TestsStatus.statuses = {
1652 TestsStatus.prototype = merge({}, TestsStatus.statuses);
1654 TestsStatus.prototype.structured_clone = function()
1656 if (!this._structured_clone) {
1657 var msg = this.message;
1658 msg = msg ? String(msg) : msg;
1659 this._structured_clone = merge({
1663 }, TestsStatus.statuses);
1665 return this._structured_clone;
1671 this.num_pending = 0;
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();
1715 Tests.prototype.setup = function(func, properties)
1717 if (this.phase >= this.phases.HAVE_RESULTS) {
1721 if (this.phase < this.phases.SETUP) {
1722 this.phase = this.phases.SETUP;
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)
1738 clearTimeout(this.timeout_id);
1740 } else if (p == "timeout_multiplier") {
1741 this.timeout_multiplier = value;
1750 this.status.status = this.status.ERROR;
1751 this.status.message = String(e);
1752 this.status.stack = e.stack ? e.stack : null;
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");
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
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() {
1774 }, this.timeout_length);
1778 Tests.prototype.timeout = function() {
1779 if (this.status.status === null) {
1780 this.status.status = this.status.TIMEOUT;
1785 Tests.prototype.end_wait = function()
1787 this.wait_for_finish = false;
1788 if (this.all_done()) {
1793 Tests.prototype.push = function(test)
1795 if (this.phase < this.phases.HAVE_TESTS) {
1799 test.index = this.tests.push(test);
1800 this.notify_test_state(test);
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);
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; }));
1818 Tests.prototype.start = function() {
1819 this.phase = this.phases.HAVE_TESTS;
1820 this.notify_start();
1823 Tests.prototype.notify_start = function() {
1824 var this_obj = this;
1825 forEach (this.start_callbacks,
1828 callback(this_obj.properties);
1832 Tests.prototype.result = function(test)
1834 if (this.phase > this.phases.HAVE_RESULTS) {
1837 this.phase = this.phases.HAVE_RESULTS;
1839 this.notify_result(test);
1842 Tests.prototype.notify_result = function(test) {
1843 var this_obj = this;
1844 this.processing_callbacks = true;
1845 forEach(this.test_done_callbacks,
1848 callback(test, this_obj);
1850 this.processing_callbacks = false;
1851 if (this_obj.all_done()) {
1852 this_obj.complete();
1856 Tests.prototype.complete = function() {
1857 if (this.phase === this.phases.COMPLETE) {
1860 this.phase = this.phases.COMPLETE;
1861 var this_obj = this;
1865 if (x.phase < x.phases.COMPLETE) {
1866 this_obj.notify_result(x);
1868 x.phase = x.phases.COMPLETE;
1872 this.notify_complete();
1875 Tests.prototype.notify_complete = function() {
1876 var this_obj = this;
1877 if (this.status.status === null) {
1878 this.status.status = this.status.OK;
1881 forEach (this.all_done_callbacks,
1884 callback(this_obj.tests, this_obj.status);
1888 Tests.prototype.fetch_tests_from_worker = function(worker) {
1889 if (this.phase >= this.phases.COMPLETE) {
1893 this.pending_workers.push(new RemoteWorker(worker));
1896 function fetch_tests_from_worker(port) {
1897 tests.fetch_tests_from_worker(port);
1899 expose(fetch_tests_from_worker, 'fetch_tests_from_worker');
1901 function timeout() {
1902 if (tests.timeout_length === null) {
1906 expose(timeout, 'timeout');
1908 function add_start_callback(callback) {
1909 tests.start_callbacks.push(callback);
1912 function add_test_state_callback(callback) {
1913 tests.test_state_callbacks.push(callback);
1916 function add_result_callback(callback) {
1917 tests.test_done_callbacks.push(callback);
1920 function add_completion_callback(callback) {
1921 tests.all_done_callbacks.push(callback);
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);
1932 array.splice(index, 1);
1936 function remove_start_callback(callback) {
1937 remove(tests.start_callbacks, callback);
1940 function remove_test_state_callback(callback) {
1941 remove(tests.test_state_callbacks, callback);
1944 function remove_result_callback(callback) {
1945 remove(tests.test_done_callbacks, callback);
1948 function remove_completion_callback(callback) {
1949 remove(tests.all_done_callbacks, callback);
1957 this.output_document = document;
1958 this.output_node = null;
1959 this.enabled = settings.output;
1960 this.phase = this.INITIAL;
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) {
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);
1979 Output.prototype.init = function(properties) {
1980 if (this.phase >= this.STARTED) {
1983 if (properties.output_document) {
1984 this.output_document = properties.output_document;
1986 this.output_document = document;
1988 this.phase = this.STARTED;
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);
1996 output_document = this.output_document;
1998 if (!output_document) {
2001 var node = output_document.getElementById("log");
2003 if (!document.body || document.readyState == "loading") {
2006 node = output_document.createElement("div");
2008 output_document.body.appendChild(node);
2010 this.output_document = output_document;
2011 this.output_node = node;
2014 Output.prototype.show_status = function() {
2015 if (this.phase < this.STARTED) {
2018 if (!this.enabled) {
2021 if (this.phase < this.HAVE_RESULTS) {
2023 this.phase = this.HAVE_RESULTS;
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";
2037 Output.prototype.show_results = function (tests, harness_status) {
2038 if (this.phase >= this.COMPLETE) {
2041 if (!this.enabled) {
2044 if (!this.output_node) {
2047 this.phase = this.COMPLETE;
2049 var log = this.output_node;
2053 var output_document = this.output_document;
2055 while (log.lastChild) {
2056 log.removeChild(log.lastChild);
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");
2066 heads[0].appendChild(stylesheet);
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 = {};
2084 var status = status_text[test.status];
2085 if (status_number.hasOwnProperty(status)) {
2086 status_number[status] += 1;
2088 status_number[status] = 1;
2092 function status_class(status)
2094 return status.replace(/\s/g, '').toLowerCase();
2097 var summary_template = ["section", {"id":"summary"},
2098 ["h2", {}, "Summary"],
2102 var status = status_text_harness[harness_status.status];
2103 var rv = [["section", {},
2106 ["span", {"class":status_class(status)},
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]);
2120 ["p", {}, "Found ${num_tests} tests"],
2122 var rv = [["div", {}]];
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)},
2129 ["input", {type:"checkbox", checked:"checked"}],
2130 status_number[status] + " " + status]]);
2138 log.appendChild(render(summary_template, {num_tests:tests.length}, output_document));
2140 forEach(output_document.querySelectorAll("section#summary label"),
2143 on_event(element, "click",
2146 if (output_document.getElementById("results") === null) {
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);
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)
2170 return s.replace(/\&/g, "&")
2171 .replace(/</g, "<")
2172 .replace(/"/g, """)
2173 .replace(/'/g, "'");
2176 function has_assertions()
2178 for (var i = 0; i < tests.length; i++) {
2179 if (tests[i].properties.hasOwnProperty("assert")) {
2186 function get_assertion(test)
2188 if (test.properties.hasOwnProperty("assert")) {
2189 if (Array.isArray(test.properties.assert)) {
2190 return test.properties.assert.join(' ');
2192 return test.properties.assert;
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>" +
2204 for (var i = 0; i < tests.length; i++) {
2205 html += '<tr class="' +
2206 escape_html(status_class(status_text[tests[i].status])) +
2208 escape_html(status_text[tests[i].status]) +
2210 escape_html(tests[i].name) +
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) +
2219 html += "</tbody></table>";
2221 log.lastChild.innerHTML = html;
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;
2233 * A template is just a javascript structure. An element is represented as:
2235 * [tag_name, {attr_name:attr_value}, child1, child2]
2237 * the children can either be strings (which act like text nodes), other templates or
2238 * functions (see below)
2240 * A text node is represented as
2244 * String values have a simple substitution syntax; ${foo} represents a variable foo.
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.
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]
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
2258 * substitute(template, substitutions) - take a template and variable mapping object,
2259 * make the variable substitutions and return the substituted template
2263 function is_single_node(template)
2265 return typeof template[0] === "string";
2268 function substitute(template, substitutions)
2270 if (typeof template === "function") {
2271 var replacement = template(substitutions);
2276 return substitute(replacement, substitutions);
2279 if (is_single_node(template)) {
2280 return substitute_single(template, substitutions);
2283 return filter(map(template, function(x) {
2284 return substitute(x, substitutions);
2285 }), function(x) {return x !== null;});
2288 function substitute_single(template, substitutions)
2290 var substitution_re = /\$\{([^ }]*)\}/g;
2292 function do_substitution(input) {
2293 var components = input.split(substitution_re);
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]]));
2304 function substitute_attrs(attrs, rv)
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;
2316 function substitute_children(children, rv)
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);
2325 extend(rv, replacement);
2329 extend(rv, do_substitution(String(children[i])));
2336 rv.push(do_substitution(String(template[0])).join(""));
2338 if (template[0] === "{text}") {
2339 substitute_children(template.slice(1), rv);
2341 substitute_attrs(template[1], rv);
2342 substitute_children(template.slice(2), rv);
2348 function make_dom_single(template, doc)
2350 var output_document = doc || document;
2352 if (template[0] === "{text}") {
2353 element = output_document.createTextNode("");
2354 for (var i = 1; i < template.length; i++) {
2355 element.data += template[i];
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]);
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);
2369 var text_node = output_document.createTextNode(template[i]);
2370 element.appendChild(text_node);
2378 function make_dom(template, substitutions, output_document)
2380 if (is_single_node(template)) {
2381 return make_dom_single(template, output_document);
2384 return map(template, function(x) {
2385 return make_dom_single(x, output_document);
2389 function render(template, substitutions, output_document)
2391 return make_dom(substitute(template, substitutions), output_document);
2397 function assert(expected_true, function_name, description, error, substitutions)
2399 if (tests.tests.length === 0) {
2400 tests.set_file_is_test();
2402 if (expected_true !== true) {
2403 var msg = make_message(function_name, description,
2404 error, substitutions);
2405 throw new AssertionError(msg);
2409 function AssertionError(message)
2411 this.message = message;
2412 this.stack = this.get_stack();
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.
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.
2437 while (!re.test(lines[i]) && i < lines.length) {
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) {
2446 // Paranoid check that we didn't skip all frames. If so, return the original stack unmodified.
2447 if (i >= lines.length) {
2451 return lines.slice(i).join("\n");
2454 function make_message(function_name, description, error, substitutions)
2456 for (var p in substitutions) {
2457 if (substitutions.hasOwnProperty(p)) {
2458 substitutions[p] = format_value(substitutions[p]);
2461 var node_form = substitute(["{text}", "${function_name}: ${description}" + error],
2462 merge({function_name:function_name,
2463 description:(description?description + " ":"")},
2465 return node_form.slice(1).join("");
2468 function filter(array, callable, thisObj) {
2470 for (var i = 0; i < array.length; i++) {
2471 if (array.hasOwnProperty(i)) {
2472 var pass = callable.call(thisObj, array[i], i, array);
2481 function map(array, callable, thisObj)
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);
2493 function extend(array, items)
2495 Array.prototype.push.apply(array, items);
2498 function forEach(array, callback, thisObj)
2500 for (var i = 0; i < array.length; i++) {
2501 if (array.hasOwnProperty(i)) {
2502 callback.call(thisObj, array[i], i, array);
2520 function expose(object, name)
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]] = {};
2528 target = target[components[i]];
2530 target[components[components.length - 1]] = object;
2533 function is_same_origin(w) {
2542 /** Returns the 'src' URL of the first <script> tag in the page to include the file 'testharness.js'. */
2543 function get_script_url()
2545 if (!('document' in self)) {
2549 var scripts = document.getElementsByTagName("script");
2550 for (var i = 0; i < scripts.length; i++) {
2552 if (scripts[i].src) {
2553 src = scripts[i].src;
2554 } else if (scripts[i].href) {
2556 src = scripts[i].href.baseVal;
2559 var matches = src && src.match(/^(.*\/|)testharness\.js$/);
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()
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;
2577 function supports_post_message(w)
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.
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.
2590 type = typeof w.postMessage;
2591 if (type === "function") {
2595 // IE8 supports postMessage, but implements it as a host object which
2596 // returns "object" as its `typeof`.
2597 else if (type === "object") {
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).
2607 // This is the case where postMessage isn't supported AND accessing a
2608 // window property across origins throws (e.g. old Firefox browser).
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) {
2626 test.set_status(test.FAIL, e.message, e.stack);
2627 test.phase = test.phases.HAS_RESULT;
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;
2637 test_environment.on_tests_ready();
2640 // vim: set expandtab shiftwidth=4 tabstop=4: