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 function step_timeout(f
, t
) {
662 var outer_this
= this;
663 var args
= Array
.prototype.slice
.call(arguments
, 2);
664 return setTimeout(function() {
665 f
.apply(outer_this
, args
);
666 }, t
* tests
.timeout_multiplier
);
669 expose(test
, 'test');
670 expose(async_test
, 'async_test');
671 expose(promise_test
, 'promise_test');
672 expose(promise_rejects
, 'promise_rejects');
673 expose(generate_tests
, 'generate_tests');
674 expose(setup
, 'setup');
675 expose(done
, 'done');
676 expose(on_event
, 'on_event');
677 expose(step_timeout
, 'step_timeout');
680 * Return a string truncated to the given length, with ... added at the end
683 function truncate(s
, len
)
685 if (s
.length
> len
) {
686 return s
.substring(0, len
- 3) + "...";
692 * Return true if object is probably a Node object.
694 function is_node(object
)
696 // I use duck-typing instead of instanceof, because
697 // instanceof doesn't work if the node is from another window (like an
698 // iframe's contentWindow):
699 // http://www.w3.org/Bugs/Public/show_bug.cgi?id=12295
700 if ("nodeType" in object
&&
701 "nodeName" in object
&&
702 "nodeValue" in object
&&
703 "childNodes" in object
) {
707 // The object is probably Node.prototype or another prototype
708 // object that inherits from it, and not a Node instance.
717 * Convert a value to a nice, human-readable string
719 function format_value(val
, seen
)
724 if (typeof val
=== "object" && val
!== null) {
725 if (seen
.indexOf(val
) >= 0) {
730 if (Array
.isArray(val
)) {
731 return "[" + val
.map(function(x
) {return format_value(x
, seen
);}).join(", ") + "]";
734 switch (typeof val
) {
736 val
= val
.replace("\\", "\\\\");
737 for (var i
= 0; i
< 32; i
++) {
740 case 0: replace
+= "0"; break;
741 case 1: replace
+= "x01"; break;
742 case 2: replace
+= "x02"; break;
743 case 3: replace
+= "x03"; break;
744 case 4: replace
+= "x04"; break;
745 case 5: replace
+= "x05"; break;
746 case 6: replace
+= "x06"; break;
747 case 7: replace
+= "x07"; break;
748 case 8: replace
+= "b"; break;
749 case 9: replace
+= "t"; break;
750 case 10: replace
+= "n"; break;
751 case 11: replace
+= "v"; break;
752 case 12: replace
+= "f"; break;
753 case 13: replace
+= "r"; break;
754 case 14: replace
+= "x0e"; break;
755 case 15: replace
+= "x0f"; break;
756 case 16: replace
+= "x10"; break;
757 case 17: replace
+= "x11"; break;
758 case 18: replace
+= "x12"; break;
759 case 19: replace
+= "x13"; break;
760 case 20: replace
+= "x14"; break;
761 case 21: replace
+= "x15"; break;
762 case 22: replace
+= "x16"; break;
763 case 23: replace
+= "x17"; break;
764 case 24: replace
+= "x18"; break;
765 case 25: replace
+= "x19"; break;
766 case 26: replace
+= "x1a"; break;
767 case 27: replace
+= "x1b"; break;
768 case 28: replace
+= "x1c"; break;
769 case 29: replace
+= "x1d"; break;
770 case 30: replace
+= "x1e"; break;
771 case 31: replace
+= "x1f"; break;
773 val
= val
.replace(RegExp(String
.fromCharCode(i
), "g"), replace
);
775 return '"' + val
.replace(/"/g, '\\"') + '"';
780 // In JavaScript, -0 === 0 and String(-0) == "0", so we have to
782 if (val === -0 && 1/val === -Infinity) {
791 // Special-case Node objects, since those come up a lot in my tests. I
792 // ignore namespaces.
794 switch (val.nodeType) {
795 case Node.ELEMENT_NODE:
796 var ret = "<" + val.localName;
797 for (var i = 0; i < val.attributes.length; i++) {
798 ret += " " + val.attributes[i].name + '="' + val.attributes[i].value + '"';
800 ret += ">" + val.innerHTML + "</" + val.localName + ">";
801 return "Element node
" + truncate(ret, 60);
803 return 'Text node "' + truncate(val.data, 60) + '"';
804 case Node.PROCESSING_INSTRUCTION_NODE:
805 return "ProcessingInstruction node
with target
" + format_value(truncate(val.target, 60)) + " and data
" + format_value(truncate(val.data, 60));
806 case Node.COMMENT_NODE:
807 return "Comment node
<!--" + truncate(val.data, 60) + "-->";
808 case Node.DOCUMENT_NODE:
809 return "Document node
with " + val.childNodes.length + (val.childNodes.length == 1 ? " child
" : " children
");
810 case Node.DOCUMENT_TYPE_NODE:
811 return "DocumentType node
";
812 case Node.DOCUMENT_FRAGMENT_NODE:
813 return "DocumentFragment node
with " + val.childNodes.length + (val.childNodes.length == 1 ? " child
" : " children
");
815 return "Node object
of unknown type
";
821 return typeof val + ' "' + truncate(String(val), 60) + '"';
824 expose(format_value, "format_value
");
830 function assert_true(actual, description)
832 assert(actual === true, "assert_true
", description,
833 "expected
true got ${actual}
", {actual:actual});
835 expose(assert_true, "assert_true
");
837 function assert_false(actual, description)
839 assert(actual === false, "assert_false
", description,
840 "expected
false got ${actual}
", {actual:actual});
842 expose(assert_false, "assert_false
");
844 function same_value(x, y) {
849 if (x === 0 && y === 0) {
850 //Distinguish +0 and -0
856 function assert_equals(actual, expected, description)
859 * Test if two primitives are equal or two objects
860 * are the same object
862 if (typeof actual != typeof expected) {
863 assert(false, "assert_equals
", description,
864 "expected (" + typeof expected + ") ${expected} but
got (" + typeof actual + ") ${actual}
",
865 {expected:expected, actual:actual});
868 assert(same_value(actual, expected), "assert_equals
", description,
869 "expected ${expected} but got ${actual}
",
870 {expected:expected, actual:actual});
872 expose(assert_equals, "assert_equals
");
874 function assert_not_equals(actual, expected, description)
877 * Test if two primitives are unequal or two objects
878 * are different objects
880 assert(!same_value(actual, expected), "assert_not_equals
", description,
881 "got disallowed value ${actual}
",
884 expose(assert_not_equals, "assert_not_equals
");
886 function assert_in_array(actual, expected, description)
888 assert(expected.indexOf(actual) != -1, "assert_in_array
", description,
889 "value ${actual} not
in array ${expected}
",
890 {actual:actual, expected:expected});
892 expose(assert_in_array, "assert_in_array
");
894 function assert_object_equals(actual, expected, description)
896 //This needs to be improved a great deal
897 function check_equal(actual, expected, stack)
903 assert(expected.hasOwnProperty(p), "assert_object_equals
", description,
904 "unexpected property ${p}
", {p:p});
906 if (typeof actual[p] === "object
" && actual[p] !== null) {
907 if (stack.indexOf(actual[p]) === -1) {
908 check_equal(actual[p], expected[p], stack);
911 assert(same_value(actual[p], expected[p]), "assert_object_equals
", description,
912 "property ${p} expected ${expected} got ${actual}
",
913 {p:p, expected:expected, actual:actual});
916 for (p in expected) {
917 assert(actual.hasOwnProperty(p),
918 "assert_object_equals
", description,
919 "expected property ${p} missing
", {p:p});
923 check_equal(actual, expected, []);
925 expose(assert_object_equals, "assert_object_equals
");
927 function assert_array_equals(actual, expected, description)
929 assert(actual.length === expected.length,
930 "assert_array_equals
", description,
931 "lengths differ
, expected ${expected} got ${actual}
",
932 {expected:expected.length, actual:actual.length});
934 for (var i = 0; i < actual.length; i++) {
935 assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i),
936 "assert_array_equals
", description,
937 "property ${i}
, property expected to be ${expected} but was ${actual}
",
938 {i:i, expected:expected.hasOwnProperty(i) ? "present
" : "missing
",
939 actual:actual.hasOwnProperty(i) ? "present
" : "missing
"});
940 assert(same_value(expected[i], actual[i]),
941 "assert_array_equals
", description,
942 "property ${i}
, expected ${expected} but got ${actual}
",
943 {i:i, expected:expected[i], actual:actual[i]});
946 expose(assert_array_equals, "assert_array_equals
");
948 function assert_approx_equals(actual, expected, epsilon, description)
951 * Test if two primitive numbers are equal withing +/- epsilon
953 assert(typeof actual === "number
",
954 "assert_approx_equals
", description,
955 "expected a number but got a ${type_actual}
",
956 {type_actual:typeof actual});
958 assert(Math.abs(actual - expected) <= epsilon,
959 "assert_approx_equals
", description,
960 "expected ${expected}
+/- ${epsilon} but got ${actual}
",
961 {expected:expected, actual:actual, epsilon:epsilon});
963 expose(assert_approx_equals, "assert_approx_equals
");
965 function assert_less_than(actual, expected, description)
968 * Test if a primitive number is less than another
970 assert(typeof actual === "number
",
971 "assert_less_than
", description,
972 "expected a number but got a ${type_actual}
",
973 {type_actual:typeof actual});
975 assert(actual < expected,
976 "assert_less_than
", description,
977 "expected a number less than ${expected} but got ${actual}
",
978 {expected:expected, actual:actual});
980 expose(assert_less_than, "assert_less_than
");
982 function assert_greater_than(actual, expected, description)
985 * Test if a primitive number is greater than another
987 assert(typeof actual === "number
",
988 "assert_greater_than
", description,
989 "expected a number but got a ${type_actual}
",
990 {type_actual:typeof actual});
992 assert(actual > expected,
993 "assert_greater_than
", description,
994 "expected a number greater than ${expected} but got ${actual}
",
995 {expected:expected, actual:actual});
997 expose(assert_greater_than, "assert_greater_than
");
999 function assert_between_exclusive(actual, lower, upper, description)
1002 * Test if a primitive number is between two others
1004 assert(typeof actual === "number
",
1005 "assert_between_exclusive
", description,
1006 "expected a number but got a ${type_actual}
",
1007 {type_actual:typeof actual});
1009 assert(actual > lower && actual < upper,
1010 "assert_between_exclusive
", description,
1011 "expected a number greater than ${lower}
" +
1012 "and less than ${upper} but got ${actual}
",
1013 {lower:lower, upper:upper, actual:actual});
1015 expose(assert_between_exclusive, "assert_between_exclusive
");
1017 function assert_less_than_equal(actual, expected, description)
1020 * Test if a primitive number is less than or equal to another
1022 assert(typeof actual === "number
",
1023 "assert_less_than_equal
", description,
1024 "expected a number but got a ${type_actual}
",
1025 {type_actual:typeof actual});
1027 assert(actual <= expected,
1028 "assert_less_than_equal
", description,
1029 "expected a number less than or equal to ${expected} but got ${actual}
",
1030 {expected:expected, actual:actual});
1032 expose(assert_less_than_equal, "assert_less_than_equal
");
1034 function assert_greater_than_equal(actual, expected, description)
1037 * Test if a primitive number is greater than or equal to another
1039 assert(typeof actual === "number
",
1040 "assert_greater_than_equal
", description,
1041 "expected a number but got a ${type_actual}
",
1042 {type_actual:typeof actual});
1044 assert(actual >= expected,
1045 "assert_greater_than_equal
", description,
1046 "expected a number greater than or equal to ${expected} but got ${actual}
",
1047 {expected:expected, actual:actual});
1049 expose(assert_greater_than_equal, "assert_greater_than_equal
");
1051 function assert_between_inclusive(actual, lower, upper, description)
1054 * Test if a primitive number is between to two others or equal to either of them
1056 assert(typeof actual === "number
",
1057 "assert_between_inclusive
", description,
1058 "expected a number but got a ${type_actual}
",
1059 {type_actual:typeof actual});
1061 assert(actual >= lower && actual <= upper,
1062 "assert_between_inclusive
", description,
1063 "expected a number greater than or equal to ${lower}
" +
1064 "and less than or equal to ${upper} but got ${actual}
",
1065 {lower:lower, upper:upper, actual:actual});
1067 expose(assert_between_inclusive, "assert_between_inclusive
");
1069 function assert_regexp_match(actual, expected, description) {
1071 * Test if a string (actual) matches a regexp (expected)
1073 assert(expected.test(actual),
1074 "assert_regexp_match
", description,
1075 "expected ${expected} but got ${actual}
",
1076 {expected:expected, actual:actual});
1078 expose(assert_regexp_match, "assert_regexp_match
");
1080 function assert_class_string(object, class_string, description) {
1081 assert_equals({}.toString.call(object), "[object
" + class_string + "]",
1084 expose(assert_class_string, "assert_class_string
");
1087 function _assert_own_property(name) {
1088 return function(object, property_name, description)
1090 assert(object.hasOwnProperty(property_name),
1092 "expected property ${p} missing
", {p:property_name});
1095 expose(_assert_own_property("assert_exists
"), "assert_exists
");
1096 expose(_assert_own_property("assert_own_property
"), "assert_own_property
");
1098 function assert_not_exists(object, property_name, description)
1100 assert(!object.hasOwnProperty(property_name),
1101 "assert_not_exists
", description,
1102 "unexpected property ${p} found
", {p:property_name});
1104 expose(assert_not_exists, "assert_not_exists
");
1106 function _assert_inherits(name) {
1107 return function (object, property_name, description)
1109 assert(typeof object === "object
",
1111 "provided value is not an object
");
1113 assert("hasOwnProperty
" in object,
1115 "provided value is an object but has no hasOwnProperty method
");
1117 assert(!object.hasOwnProperty(property_name),
1119 "property ${p} found on object expected
in prototype chain
",
1122 assert(property_name in object,
1124 "property ${p} not found
in prototype chain
",
1128 expose(_assert_inherits("assert_inherits
"), "assert_inherits
");
1129 expose(_assert_inherits("assert_idl_attribute
"), "assert_idl_attribute
");
1131 function assert_readonly(object, property_name, description)
1133 var initial_value = object[property_name];
1135 //Note that this can have side effects in the case where
1136 //the property has PutForwards
1137 object[property_name] = initial_value + "a
"; //XXX use some other value here?
1138 assert(same_value(object[property_name], initial_value),
1139 "assert_readonly
", description,
1140 "changing property ${p} succeeded
",
1143 object[property_name] = initial_value;
1146 expose(assert_readonly, "assert_readonly
");
1148 function assert_throws(code, func, description)
1152 assert(false, "assert_throws
", description,
1153 "${func} did not
throw", {func:func});
1155 if (e instanceof AssertionError) {
1158 if (code === null) {
1161 if (typeof code === "object
") {
1162 assert(typeof e == "object
" && "name
" in e && e.name == code.name,
1163 "assert_throws
", description,
1164 "${func} threw ${actual}
(${actual_name}
) expected ${expected}
(${expected_name}
)",
1165 {func:func, actual:e, actual_name:e.name,
1167 expected_name:code.name});
1171 var code_name_map = {
1172 INDEX_SIZE_ERR: 'IndexSizeError',
1173 HIERARCHY_REQUEST_ERR: 'HierarchyRequestError',
1174 WRONG_DOCUMENT_ERR: 'WrongDocumentError',
1175 INVALID_CHARACTER_ERR: 'InvalidCharacterError',
1176 NO_MODIFICATION_ALLOWED_ERR: 'NoModificationAllowedError',
1177 NOT_FOUND_ERR: 'NotFoundError',
1178 NOT_SUPPORTED_ERR: 'NotSupportedError',
1179 INVALID_STATE_ERR: 'InvalidStateError',
1180 SYNTAX_ERR: 'SyntaxError',
1181 INVALID_MODIFICATION_ERR: 'InvalidModificationError',
1182 NAMESPACE_ERR: 'NamespaceError',
1183 INVALID_ACCESS_ERR: 'InvalidAccessError',
1184 TYPE_MISMATCH_ERR: 'TypeMismatchError',
1185 SECURITY_ERR: 'SecurityError',
1186 NETWORK_ERR: 'NetworkError',
1187 ABORT_ERR: 'AbortError',
1188 URL_MISMATCH_ERR: 'URLMismatchError',
1189 QUOTA_EXCEEDED_ERR: 'QuotaExceededError',
1190 TIMEOUT_ERR: 'TimeoutError',
1191 INVALID_NODE_TYPE_ERR: 'InvalidNodeTypeError',
1192 DATA_CLONE_ERR: 'DataCloneError'
1195 var name = code in code_name_map ? code_name_map[code] : code;
1197 var name_code_map = {
1199 HierarchyRequestError: 3,
1200 WrongDocumentError: 4,
1201 InvalidCharacterError: 5,
1202 NoModificationAllowedError: 7,
1204 NotSupportedError: 9,
1205 InvalidStateError: 11,
1207 InvalidModificationError: 13,
1209 InvalidAccessError: 15,
1210 TypeMismatchError: 17,
1214 URLMismatchError: 21,
1215 QuotaExceededError: 22,
1217 InvalidNodeTypeError: 24,
1221 NotReadableError: 0,
1225 TransactionInactiveError: 0,
1231 if (!(name in name_code_map)) {
1232 throw new AssertionError('Test bug: unrecognized DOMException code "' + code + '" passed to assert_throws()');
1235 var required_props = { code: name_code_map[name] };
1237 if (required_props.code === 0 ||
1238 (typeof e == "object
" &&
1240 e.name !== e.name.toUpperCase() &&
1241 e.name !== "DOMException
")) {
1242 // New style exception: also test the name property.
1243 required_props.name = name;
1246 //We'd like to test that e instanceof the appropriate interface,
1247 //but we can't, because we don't know what window it was created
1248 //in. It might be an instanceof the appropriate interface on some
1249 //unknown other window. TODO: Work around this somehow?
1251 assert(typeof e == "object
",
1252 "assert_throws
", description,
1253 "${func} threw ${e}
with type ${type}
, not an object
",
1254 {func:func, e:e, type:typeof e});
1256 for (var prop in required_props) {
1257 assert(typeof e == "object
" && prop in e && e[prop] == required_props[prop],
1258 "assert_throws
", description,
1259 "${func} threw ${e} that is not a DOMException
" + code + ": property ${prop} is equal to ${actual}
, expected ${expected}
",
1260 {func:func, e:e, prop:prop, actual:e[prop], expected:required_props[prop]});
1264 expose(assert_throws, "assert_throws
");
1266 function assert_unreached(description) {
1267 assert(false, "assert_unreached
", description,
1268 "Reached unreachable code
");
1270 expose(assert_unreached, "assert_unreached
");
1272 function assert_any(assert_func, actual, expected_array)
1274 var args = [].slice.call(arguments, 3);
1277 forEach(expected_array,
1281 assert_func.apply(this, [actual, expected].concat(args));
1284 errors.push(e.message);
1288 throw new AssertionError(errors.join("\n\n"));
1291 expose(assert_any, "assert_any
");
1293 function Test(name, properties)
1295 if (tests.file_is_test && tests.tests.length) {
1296 throw new Error("Tried to create a test
with file_is_test
");
1300 this.phase = this.phases.INITIAL;
1302 this.status = this.NOTRUN;
1303 this.timeout_id = null;
1306 this.properties = properties;
1307 var timeout = properties.timeout ? properties.timeout : settings.test_timeout;
1308 if (timeout !== null) {
1309 this.timeout_length = timeout * tests.timeout_multiplier;
1311 this.timeout_length = null;
1314 this.message = null;
1319 this.cleanup_callbacks = [];
1331 Test.prototype = merge({}, Test.statuses);
1333 Test.prototype.phases = {
1340 Test.prototype.structured_clone = function()
1342 if (!this._structured_clone) {
1343 var msg = this.message;
1344 msg = msg ? String(msg) : msg;
1345 this._structured_clone = merge({
1346 name:String(this.name),
1347 properties:merge({}, this.properties),
1350 this._structured_clone.status = this.status;
1351 this._structured_clone.message = this.message;
1352 this._structured_clone.stack = this.stack;
1353 this._structured_clone.index = this.index;
1354 return this._structured_clone;
1357 Test.prototype.step = function(func, this_obj)
1359 if (this.phase > this.phases.STARTED) {
1362 this.phase = this.phases.STARTED;
1363 //If we don't get a result before the harness times out that will be a test timout
1364 this.set_status(this.TIMEOUT, "Test timed out
");
1366 tests.started = true;
1367 tests.notify_test_state(this);
1369 if (this.timeout_id === null) {
1373 this.steps.push(func);
1375 if (arguments.length === 1) {
1380 return func.apply(this_obj, Array.prototype.slice.call(arguments, 2));
1382 if (this.phase >= this.phases.HAS_RESULT) {
1385 var message = String((typeof e === "object
" && e !== null) ? e.message : e);
1386 var stack = e.stack ? e.stack : null;
1388 this.set_status(this.FAIL, message, stack);
1389 this.phase = this.phases.HAS_RESULT;
1394 Test.prototype.step_func = function(func, this_obj)
1396 var test_this = this;
1398 if (arguments.length === 1) {
1399 this_obj = test_this;
1404 return test_this.step.apply(test_this, [func, this_obj].concat(
1405 Array.prototype.slice.call(arguments)));
1409 Test.prototype.step_func_done = function(func, this_obj)
1411 var test_this = this;
1413 if (arguments.length === 1) {
1414 this_obj = test_this;
1420 test_this.step.apply(test_this, [func, this_obj].concat(
1421 Array.prototype.slice.call(arguments)));
1427 Test.prototype.unreached_func = function(description)
1429 return this.step_func(function() {
1430 assert_unreached(description);
1434 Test.prototype.step_timeout = function(f, timeout) {
1435 var test_this = this;
1436 var args = Array.prototype.slice.call(arguments, 2);
1437 return setTimeout(this.step_func(function() {
1438 return f.apply(test_this, args);
1439 }, timeout * tests.timeout_multiplier));
1442 Test.prototype.add_cleanup = function(callback) {
1443 this.cleanup_callbacks.push(callback);
1446 Test.prototype.force_timeout = function() {
1447 this.set_status(this.TIMEOUT);
1448 this.phase = this.phases.HAS_RESULT;
1451 Test.prototype.set_timeout = function()
1453 if (this.timeout_length !== null) {
1454 var this_obj = this;
1455 this.timeout_id = setTimeout(function()
1458 }, this.timeout_length);
1462 Test.prototype.set_status = function(status, message, stack)
1464 this.status = status;
1465 this.message = message;
1466 this.stack = stack ? stack : null;
1469 Test.prototype.timeout = function()
1471 this.timeout_id = null;
1472 this.set_status(this.TIMEOUT, "Test timed out
");
1473 this.phase = this.phases.HAS_RESULT;
1477 Test.prototype.done = function()
1479 if (this.phase == this.phases.COMPLETE) {
1483 if (this.phase <= this.phases.STARTED) {
1484 this.set_status(this.PASS, null);
1487 this.phase = this.phases.COMPLETE;
1489 clearTimeout(this.timeout_id);
1494 Test.prototype.cleanup = function() {
1495 forEach(this.cleanup_callbacks,
1496 function(cleanup_callback) {
1502 * A RemoteTest object mirrors a Test object on a remote worker. The
1503 * associated RemoteWorker updates the RemoteTest object in response to
1504 * received events. In turn, the RemoteTest object replicates these events
1505 * on the local document. This allows listeners (test result reporting
1506 * etc..) to transparently handle local and remote events.
1508 function RemoteTest(clone) {
1509 var this_obj = this;
1510 Object.keys(clone).forEach(
1512 this_obj[key] = clone[key];
1515 this.phase = this.phases.INITIAL;
1516 this.update_state_from(clone);
1520 RemoteTest.prototype.structured_clone = function() {
1522 Object.keys(this).forEach(
1524 if (typeof(this[key]) === "object
") {
1525 clone[key] = merge({}, this[key]);
1527 clone[key] = this[key];
1530 clone.phases = merge({}, this.phases);
1534 RemoteTest.prototype.cleanup = function() {};
1535 RemoteTest.prototype.phases = Test.prototype.phases;
1536 RemoteTest.prototype.update_state_from = function(clone) {
1537 this.status = clone.status;
1538 this.message = clone.message;
1539 this.stack = clone.stack;
1540 if (this.phase === this.phases.INITIAL) {
1541 this.phase = this.phases.STARTED;
1544 RemoteTest.prototype.done = function() {
1545 this.phase = this.phases.COMPLETE;
1549 * A RemoteWorker listens for test events from a worker. These events are
1550 * then used to construct and maintain RemoteTest objects that mirror the
1551 * tests running on the remote worker.
1553 function RemoteWorker(worker) {
1554 this.running = true;
1555 this.tests = new Array();
1557 var this_obj = this;
1558 worker.onerror = function(error) { this_obj.worker_error(error); };
1562 if (is_service_worker(worker)) {
1563 if (window.MessageChannel) {
1564 // The ServiceWorker's implicit MessagePort is currently not
1565 // reliably accessible from the ServiceWorkerGlobalScope due to
1566 // Blink setting MessageEvent.source to null for messages sent
1567 // via ServiceWorker.postMessage(). Until that's resolved,
1568 // create an explicit MessageChannel and pass one end to the
1570 var message_channel = new MessageChannel();
1571 message_port = message_channel.port1;
1572 message_port.start();
1573 worker.postMessage({type: "connect
"}, [message_channel.port2]);
1575 // If MessageChannel is not available, then try the
1576 // ServiceWorker.postMessage() approach using MessageEvent.source
1577 // on the other end.
1578 message_port = navigator.serviceWorker;
1579 worker.postMessage({type: "connect
"});
1581 } else if (is_shared_worker(worker)) {
1582 message_port = worker.port;
1584 message_port = worker;
1587 // Keeping a reference to the worker until worker_done() is seen
1588 // prevents the Worker object and its MessageChannel from going away
1589 // before all the messages are dispatched.
1590 this.worker = worker;
1592 message_port.onmessage =
1594 if (this_obj.running && (message.data.type in this_obj.message_handlers)) {
1595 this_obj.message_handlers[message.data.type].call(this_obj, message.data);
1600 RemoteWorker.prototype.worker_error = function(error) {
1601 var message = error.message || String(error);
1602 var filename = (error.filename ? " " + error.filename: "");
1603 // FIXME: Display worker error states separately from main document
1607 status: tests.status.ERROR,
1608 message: "Error
in worker
" + filename + ": " + message,
1612 error.preventDefault();
1615 RemoteWorker.prototype.test_state = function(data) {
1616 var remote_test = this.tests[data.test.index];
1618 remote_test = new RemoteTest(data.test);
1619 this.tests[data.test.index] = remote_test;
1621 remote_test.update_state_from(data.test);
1622 tests.notify_test_state(remote_test);
1625 RemoteWorker.prototype.test_done = function(data) {
1626 var remote_test = this.tests[data.test.index];
1627 remote_test.update_state_from(data.test);
1629 tests.result(remote_test);
1632 RemoteWorker.prototype.worker_done = function(data) {
1633 if (tests.status.status === null &&
1634 data.status.status !== data.status.OK) {
1635 tests.status.status = data.status.status;
1636 tests.status.message = data.status.message;
1637 tests.status.stack = data.status.stack;
1639 this.running = false;
1641 if (tests.all_done()) {
1646 RemoteWorker.prototype.message_handlers = {
1647 test_state: RemoteWorker.prototype.test_state,
1648 result: RemoteWorker.prototype.test_done,
1649 complete: RemoteWorker.prototype.worker_done
1656 function TestsStatus()
1659 this.message = null;
1663 TestsStatus.statuses = {
1669 TestsStatus.prototype = merge({}, TestsStatus.statuses);
1671 TestsStatus.prototype.structured_clone = function()
1673 if (!this._structured_clone) {
1674 var msg = this.message;
1675 msg = msg ? String(msg) : msg;
1676 this._structured_clone = merge({
1680 }, TestsStatus.statuses);
1682 return this._structured_clone;
1688 this.num_pending = 0;
1697 this.phase = this.phases.INITIAL;
1699 this.properties = {};
1701 this.wait_for_finish = false;
1702 this.processing_callbacks = false;
1704 this.allow_uncaught_exception = false;
1706 this.file_is_test = false;
1708 this.timeout_multiplier = 1;
1709 this.timeout_length = test_environment.test_timeout();
1710 this.timeout_id = null;
1712 this.start_callbacks = [];
1713 this.test_state_callbacks = [];
1714 this.test_done_callbacks = [];
1715 this.all_done_callbacks = [];
1717 this.pending_workers = [];
1719 this.status = new TestsStatus();
1721 var this_obj = this;
1723 test_environment.add_on_loaded_callback(function() {
1724 if (this_obj.all_done()) {
1725 this_obj.complete();
1732 Tests.prototype.setup = function(func, properties)
1734 if (this.phase >= this.phases.HAVE_RESULTS) {
1738 if (this.phase < this.phases.SETUP) {
1739 this.phase = this.phases.SETUP;
1742 this.properties = properties;
1744 for (var p in properties) {
1745 if (properties.hasOwnProperty(p)) {
1746 var value = properties[p];
1747 if (p == "allow_uncaught_exception
") {
1748 this.allow_uncaught_exception = value;
1749 } else if (p == "explicit_done
" && value) {
1750 this.wait_for_finish = true;
1751 } else if (p == "explicit_timeout
" && value) {
1752 this.timeout_length = null;
1753 if (this.timeout_id)
1755 clearTimeout(this.timeout_id);
1757 } else if (p == "timeout_multiplier
") {
1758 this.timeout_multiplier = value;
1767 this.status.status = this.status.ERROR;
1768 this.status.message = String(e);
1769 this.status.stack = e.stack ? e.stack : null;
1775 Tests.prototype.set_file_is_test = function() {
1776 if (this.tests.length > 0) {
1777 throw new Error("Tried to
set file as test after creating a test
");
1779 this.wait_for_finish = true;
1780 this.file_is_test = true;
1781 // Create the test, which will add it to the list of tests
1785 Tests.prototype.set_timeout = function() {
1786 var this_obj = this;
1787 clearTimeout(this.timeout_id);
1788 if (this.timeout_length !== null) {
1789 this.timeout_id = setTimeout(function() {
1791 }, this.timeout_length);
1795 Tests.prototype.timeout = function() {
1796 if (this.status.status === null) {
1797 this.status.status = this.status.TIMEOUT;
1802 Tests.prototype.end_wait = function()
1804 this.wait_for_finish = false;
1805 if (this.all_done()) {
1810 Tests.prototype.push = function(test)
1812 if (this.phase < this.phases.HAVE_TESTS) {
1816 test.index = this.tests.push(test);
1817 this.notify_test_state(test);
1820 Tests.prototype.notify_test_state = function(test) {
1821 var this_obj = this;
1822 forEach(this.test_state_callbacks,
1823 function(callback) {
1824 callback(test, this_obj);
1828 Tests.prototype.all_done = function() {
1829 return (this.tests.length > 0 && test_environment.all_loaded &&
1830 this.num_pending === 0 && !this.wait_for_finish &&
1831 !this.processing_callbacks &&
1832 !this.pending_workers.some(function(w) { return w.running; }));
1835 Tests.prototype.start = function() {
1836 this.phase = this.phases.HAVE_TESTS;
1837 this.notify_start();
1840 Tests.prototype.notify_start = function() {
1841 var this_obj = this;
1842 forEach (this.start_callbacks,
1845 callback(this_obj.properties);
1849 Tests.prototype.result = function(test)
1851 if (this.phase > this.phases.HAVE_RESULTS) {
1854 this.phase = this.phases.HAVE_RESULTS;
1856 this.notify_result(test);
1859 Tests.prototype.notify_result = function(test) {
1860 var this_obj = this;
1861 this.processing_callbacks = true;
1862 forEach(this.test_done_callbacks,
1865 callback(test, this_obj);
1867 this.processing_callbacks = false;
1868 if (this_obj.all_done()) {
1869 this_obj.complete();
1873 Tests.prototype.complete = function() {
1874 if (this.phase === this.phases.COMPLETE) {
1877 this.phase = this.phases.COMPLETE;
1878 var this_obj = this;
1882 if (x.phase < x.phases.COMPLETE) {
1883 this_obj.notify_result(x);
1885 x.phase = x.phases.COMPLETE;
1889 this.notify_complete();
1892 Tests.prototype.notify_complete = function() {
1893 var this_obj = this;
1894 if (this.status.status === null) {
1895 this.status.status = this.status.OK;
1898 forEach (this.all_done_callbacks,
1901 callback(this_obj.tests, this_obj.status);
1905 Tests.prototype.fetch_tests_from_worker = function(worker) {
1906 if (this.phase >= this.phases.COMPLETE) {
1910 this.pending_workers.push(new RemoteWorker(worker));
1913 function fetch_tests_from_worker(port) {
1914 tests.fetch_tests_from_worker(port);
1916 expose(fetch_tests_from_worker, 'fetch_tests_from_worker');
1918 function timeout() {
1919 if (tests.timeout_length === null) {
1923 expose(timeout, 'timeout');
1925 function add_start_callback(callback) {
1926 tests.start_callbacks.push(callback);
1929 function add_test_state_callback(callback) {
1930 tests.test_state_callbacks.push(callback);
1933 function add_result_callback(callback) {
1934 tests.test_done_callbacks.push(callback);
1937 function add_completion_callback(callback) {
1938 tests.all_done_callbacks.push(callback);
1941 expose(add_start_callback, 'add_start_callback');
1942 expose(add_test_state_callback, 'add_test_state_callback');
1943 expose(add_result_callback, 'add_result_callback');
1944 expose(add_completion_callback, 'add_completion_callback');
1946 function remove(array, item) {
1947 var index = array.indexOf(item);
1949 array.splice(index, 1);
1953 function remove_start_callback(callback) {
1954 remove(tests.start_callbacks, callback);
1957 function remove_test_state_callback(callback) {
1958 remove(tests.test_state_callbacks, callback);
1961 function remove_result_callback(callback) {
1962 remove(tests.test_done_callbacks, callback);
1965 function remove_completion_callback(callback) {
1966 remove(tests.all_done_callbacks, callback);
1974 this.output_document = document;
1975 this.output_node = null;
1976 this.enabled = settings.output;
1977 this.phase = this.INITIAL;
1980 Output.prototype.INITIAL = 0;
1981 Output.prototype.STARTED = 1;
1982 Output.prototype.HAVE_RESULTS = 2;
1983 Output.prototype.COMPLETE = 3;
1985 Output.prototype.setup = function(properties) {
1986 if (this.phase > this.INITIAL) {
1990 //If output is disabled in testharnessreport.js the test shouldn't be
1991 //able to override that
1992 this.enabled = this.enabled && (properties.hasOwnProperty("output
") ?
1993 properties.output : settings.output);
1996 Output.prototype.init = function(properties) {
1997 if (this.phase >= this.STARTED) {
2000 if (properties.output_document) {
2001 this.output_document = properties.output_document;
2003 this.output_document = document;
2005 this.phase = this.STARTED;
2008 Output.prototype.resolve_log = function() {
2009 var output_document;
2010 if (typeof this.output_document === "function") {
2011 output_document = this.output_document.apply(undefined);
2013 output_document = this.output_document;
2015 if (!output_document) {
2018 var node = output_document.getElementById("log
");
2020 if (!document.body || document.readyState == "loading
") {
2023 node = output_document.createElement("div
");
2025 output_document.body.appendChild(node);
2027 this.output_document = output_document;
2028 this.output_node = node;
2031 Output.prototype.show_status = function() {
2032 if (this.phase < this.STARTED) {
2035 if (!this.enabled) {
2038 if (this.phase < this.HAVE_RESULTS) {
2040 this.phase = this.HAVE_RESULTS;
2042 var done_count = tests.tests.length - tests.num_pending;
2043 if (this.output_node) {
2044 if (done_count < 100 ||
2045 (done_count < 1000 && done_count % 100 === 0) ||
2046 done_count % 1000 === 0) {
2047 this.output_node.textContent = "Running
, " +
2048 done_count + " complete
, " +
2049 tests.num_pending + " remain
";
2054 Output.prototype.show_results = function (tests, harness_status) {
2055 if (this.phase >= this.COMPLETE) {
2058 if (!this.enabled) {
2061 if (!this.output_node) {
2064 this.phase = this.COMPLETE;
2066 var log = this.output_node;
2070 var output_document = this.output_document;
2072 while (log.lastChild) {
2073 log.removeChild(log.lastChild);
2076 var harness_url = get_harness_url();
2077 if (harness_url !== null) {
2078 var stylesheet = output_document.createElementNS(xhtml_ns, "link
");
2079 stylesheet.setAttribute("rel
", "stylesheet
");
2080 stylesheet.setAttribute("href
", harness_url + "testharness
.css
");
2081 var heads = output_document.getElementsByTagName("head
");
2083 heads[0].appendChild(stylesheet);
2087 var status_text_harness = {};
2088 status_text_harness[harness_status.OK] = "OK
";
2089 status_text_harness[harness_status.ERROR] = "Error
";
2090 status_text_harness[harness_status.TIMEOUT] = "Timeout
";
2092 var status_text = {};
2093 status_text[Test.prototype.PASS] = "Pass
";
2094 status_text[Test.prototype.FAIL] = "Fail
";
2095 status_text[Test.prototype.TIMEOUT] = "Timeout
";
2096 status_text[Test.prototype.NOTRUN] = "Not Run
";
2098 var status_number = {};
2101 var status = status_text[test.status];
2102 if (status_number.hasOwnProperty(status)) {
2103 status_number[status] += 1;
2105 status_number[status] = 1;
2109 function status_class(status)
2111 return status.replace(/\s/g, '').toLowerCase();
2114 var summary_template = ["section
", {"id
":"summary
"},
2115 ["h2
", {}, "Summary
"],
2119 var status = status_text_harness[harness_status.status];
2120 var rv = [["section
", {},
2123 ["span
", {"class":status_class(status)},
2129 if (harness_status.status === harness_status.ERROR) {
2130 rv[0].push(["pre
", {}, harness_status.message]);
2131 if (harness_status.stack) {
2132 rv[0].push(["pre
", {}, harness_status.stack]);
2137 ["p
", {}, "Found ${num_tests} tests
"],
2139 var rv = [["div
", {}]];
2141 while (status_text.hasOwnProperty(i)) {
2142 if (status_number.hasOwnProperty(status_text[i])) {
2143 var status = status_text[i];
2144 rv[0].push(["div
", {"class":status_class(status)},
2146 ["input
", {type:"checkbox
", checked:"checked
"}],
2147 status_number[status] + " " + status]]);
2155 log.appendChild(render(summary_template, {num_tests:tests.length}, output_document));
2157 forEach(output_document.querySelectorAll("section
#summary label
"),
2160 on_event(element, "click
",
2163 if (output_document.getElementById("results
") === null) {
2167 var result_class = element.parentNode.getAttribute("class");
2168 var style_element = output_document.querySelector("style
#hide
-" + result_class);
2169 var input_element = element.querySelector("input
");
2170 if (!style_element && !input_element.checked) {
2171 style_element = output_document.createElementNS(xhtml_ns, "style
");
2172 style_element.id = "hide
-" + result_class;
2173 style_element.textContent = "table
#results
> tbody
> tr
."+result_class+"{display
:none
}";
2174 output_document.body.appendChild(style_element);
2175 } else if (style_element && input_element.checked) {
2176 style_element.parentNode.removeChild(style_element);
2181 // This use of innerHTML plus manual escaping is not recommended in
2182 // general, but is necessary here for performance. Using textContent
2183 // on each individual <td> adds tens of seconds of execution time for
2184 // large test suites (tens of thousands of tests).
2185 function escape_html(s)
2187 return s.replace(/\&/g, "&
;")
2188 .replace(/</g, "<
;")
2189 .replace(/"/g
, """)
2190 .replace(/'/g, "'");
2193 function has_assertions()
2195 for (var i = 0; i < tests.length; i++) {
2196 if (tests[i].properties.hasOwnProperty("assert")) {
2203 function get_assertion(test)
2205 if (test.properties.hasOwnProperty("assert")) {
2206 if (Array.isArray(test.properties.assert)) {
2207 return test.properties.assert.join(' ');
2209 return test.properties.assert;
2214 log.appendChild(document.createElementNS(xhtml_ns, "section"));
2215 var assertions = has_assertions();
2216 var html = "<h2>Details</h2><table id='results
' " + (assertions ? "class='assertions
'" : "" ) + ">" +
2217 "<thead><tr><th>Result</th><th>Test Name</th>" +
2218 (assertions ? "<th>Assertion</th>" : "") +
2219 "<th>Message</th></tr></thead>" +
2221 for (var i = 0; i < tests.length; i++) {
2222 html += '<tr
class="' +
2223 escape_html(status_class(status_text[tests[i].status])) +
2225 escape_html(status_text[tests[i].status]) +
2227 escape_html(tests[i].name) +
2229 (assertions ? escape_html(get_assertion(tests[i])) + "</td><td>" : "") +
2230 escape_html(tests[i].message ? tests[i].message : " ") +
2231 (tests[i].stack ? "<pre>" +
2232 escape_html(tests[i].stack) +
2236 html += "</tbody></table>";
2238 log.lastChild.innerHTML = html;
2240 log.appendChild(document.createElementNS(xhtml_ns, "p"))
2241 .textContent = "Setting innerHTML for the log threw an exception.";
2242 log.appendChild(document.createElementNS(xhtml_ns, "pre"))
2243 .textContent = html;
2250 * A template is just a javascript structure. An element is represented as:
2252 * [tag_name, {attr_name:attr_value}, child1, child2]
2254 * the children can either be strings (which act like text nodes), other templates or
2255 * functions (see below)
2257 * A text node is represented as
2261 * String values have a simple substitution syntax; ${foo} represents a variable foo.
2263 * It is possible to embed logic in templates by using a function in a place where a
2264 * node would usually go. The function must either return part of a template or null.
2266 * In cases where a set of nodes are required as output rather than a single node
2267 * with children it is possible to just use a list
2268 * [node1, node2, node3]
2272 * render(template, substitutions) - take a template and an object mapping
2273 * variable names to parameters and return either a DOM node or a list of DOM nodes
2275 * substitute(template, substitutions) - take a template and variable mapping object,
2276 * make the variable substitutions and return the substituted template
2280 function is_single_node(template)
2282 return typeof template[0] === "string";
2285 function substitute(template, substitutions)
2287 if (typeof template === "function") {
2288 var replacement = template(substitutions);
2293 return substitute(replacement, substitutions);
2296 if (is_single_node(template)) {
2297 return substitute_single(template, substitutions);
2300 return filter(map(template, function(x) {
2301 return substitute(x, substitutions);
2302 }), function(x) {return x !== null;});
2305 function substitute_single(template, substitutions)
2307 var substitution_re = /\$\{([^ }]*)\}/g;
2309 function do_substitution(input) {
2310 var components = input.split(substitution_re);
2312 for (var i = 0; i < components.length; i += 2) {
2313 rv.push(components[i]);
2314 if (components[i + 1]) {
2315 rv.push(String(substitutions[components[i + 1]]));
2321 function substitute_attrs(attrs, rv)
2324 for (var name in template[1]) {
2325 if (attrs.hasOwnProperty(name)) {
2326 var new_name = do_substitution(name).join("");
2327 var new_value = do_substitution(attrs[name]).join("");
2328 rv[1][new_name] = new_value;
2333 function substitute_children(children, rv)
2335 for (var i = 0; i < children.length; i++) {
2336 if (children[i] instanceof Object) {
2337 var replacement = substitute(children[i], substitutions);
2338 if (replacement !== null) {
2339 if (is_single_node(replacement)) {
2340 rv.push(replacement);
2342 extend(rv, replacement);
2346 extend(rv, do_substitution(String(children[i])));
2353 rv.push(do_substitution(String(template[0])).join(""));
2355 if (template[0] === "{text}") {
2356 substitute_children(template.slice(1), rv);
2358 substitute_attrs(template[1], rv);
2359 substitute_children(template.slice(2), rv);
2365 function make_dom_single(template, doc)
2367 var output_document = doc || document;
2369 if (template[0] === "{text}") {
2370 element = output_document.createTextNode("");
2371 for (var i = 1; i < template.length; i++) {
2372 element.data += template[i];
2375 element = output_document.createElementNS(xhtml_ns, template[0]);
2376 for (var name in template[1]) {
2377 if (template[1].hasOwnProperty(name)) {
2378 element.setAttribute(name, template[1][name]);
2381 for (var i = 2; i < template.length; i++) {
2382 if (template[i] instanceof Object) {
2383 var sub_element = make_dom(template[i]);
2384 element.appendChild(sub_element);
2386 var text_node = output_document.createTextNode(template[i]);
2387 element.appendChild(text_node);
2395 function make_dom(template, substitutions, output_document)
2397 if (is_single_node(template)) {
2398 return make_dom_single(template, output_document);
2401 return map(template, function(x) {
2402 return make_dom_single(x, output_document);
2406 function render(template, substitutions, output_document)
2408 return make_dom(substitute(template, substitutions), output_document);
2414 function assert(expected_true, function_name, description, error, substitutions)
2416 if (tests.tests.length === 0) {
2417 tests.set_file_is_test();
2419 if (expected_true !== true) {
2420 var msg = make_message(function_name, description,
2421 error, substitutions);
2422 throw new AssertionError(msg);
2426 function AssertionError(message)
2428 this.message = message;
2429 this.stack = this.get_stack();
2432 AssertionError.prototype = Object.create(Error.prototype);
2434 AssertionError.prototype.get_stack = function() {
2435 var stack = new Error().stack;
2436 // IE11 does not initialize 'Error
.stack
' until the object is thrown.
2445 var lines = stack.split("\n");
2447 // Create a pattern to match stack frames originating within testharness.js. These include the
2448 // script URL, followed by the line/col (e.g., '/resources
/testharness
.js
:120:21').
2449 var re = new RegExp((get_script_url() || "\\btestharness.js") + ":\\d+:\\d+");
2451 // Some browsers include a preamble that specifies the type of the error object. Skip this by
2452 // advancing until we find the first stack frame originating from testharness.js.
2454 while (!re.test(lines[i]) && i < lines.length) {
2458 // Then skip the top frames originating from testharness.js to begin the stack at the test code.
2459 while (re.test(lines[i]) && i < lines.length) {
2463 // Paranoid check that we didn't skip all frames
. If so
, return the original stack unmodified
.
2464 if (i
>= lines
.length
) {
2468 return lines
.slice(i
).join("\n");
2471 function make_message(function_name
, description
, error
, substitutions
)
2473 for (var p
in substitutions
) {
2474 if (substitutions
.hasOwnProperty(p
)) {
2475 substitutions
[p
] = format_value(substitutions
[p
]);
2478 var node_form
= substitute(["{text}", "${function_name}: ${description}" + error
],
2479 merge({function_name
:function_name
,
2480 description
:(description
?description
+ " ":"")},
2482 return node_form
.slice(1).join("");
2485 function filter(array
, callable
, thisObj
) {
2487 for (var i
= 0; i
< array
.length
; i
++) {
2488 if (array
.hasOwnProperty(i
)) {
2489 var pass
= callable
.call(thisObj
, array
[i
], i
, array
);
2498 function map(array
, callable
, thisObj
)
2501 rv
.length
= array
.length
;
2502 for (var i
= 0; i
< array
.length
; i
++) {
2503 if (array
.hasOwnProperty(i
)) {
2504 rv
[i
] = callable
.call(thisObj
, array
[i
], i
, array
);
2510 function extend(array
, items
)
2512 Array
.prototype.push
.apply(array
, items
);
2515 function forEach(array
, callback
, thisObj
)
2517 for (var i
= 0; i
< array
.length
; i
++) {
2518 if (array
.hasOwnProperty(i
)) {
2519 callback
.call(thisObj
, array
[i
], i
, array
);
2537 function expose(object
, name
)
2539 var components
= name
.split(".");
2540 var target
= test_environment
.global_scope();
2541 for (var i
= 0; i
< components
.length
- 1; i
++) {
2542 if (!(components
[i
] in target
)) {
2543 target
[components
[i
]] = {};
2545 target
= target
[components
[i
]];
2547 target
[components
[components
.length
- 1]] = object
;
2550 function is_same_origin(w
) {
2559 /** Returns the 'src' URL of the first <script> tag in the page to include the file 'testharness.js'. */
2560 function get_script_url()
2562 if (!('document' in self
)) {
2566 var scripts
= document
.getElementsByTagName("script");
2567 for (var i
= 0; i
< scripts
.length
; i
++) {
2569 if (scripts
[i
].src
) {
2570 src
= scripts
[i
].src
;
2571 } else if (scripts
[i
].href
) {
2573 src
= scripts
[i
].href
.baseVal
;
2576 var matches
= src
&& src
.match(/^(.*\/|)testharness\.js$/);
2584 /** Returns the URL path at which the files for testharness.js are assumed to reside (e.g., '/resources/').
2585 The path is derived from inspecting the 'src' of the <script> tag that included 'testharness.js'. */
2586 function get_harness_url()
2588 var script_url
= get_script_url();
2590 // Exclude the 'testharness.js' file from the returned path, but '+ 1' to include the trailing slash.
2591 return script_url
? script_url
.slice(0, script_url
.lastIndexOf('/') + 1) : undefined;
2594 function supports_post_message(w
)
2598 // Given IE implements postMessage across nested iframes but not across
2599 // windows or tabs, you can't infer cross-origin communication from the presence
2600 // of postMessage on the current window object only.
2602 // Touching the postMessage prop on a window can throw if the window is
2603 // not from the same origin AND post message is not supported in that
2604 // browser. So just doing an existence test here won't do, you also need
2605 // to wrap it in a try..cacth block.
2607 type
= typeof w
.postMessage
;
2608 if (type
=== "function") {
2612 // IE8 supports postMessage, but implements it as a host object which
2613 // returns "object" as its `typeof`.
2614 else if (type
=== "object") {
2618 // This is the case where postMessage isn't supported AND accessing a
2619 // window property across origins does NOT throw (e.g. old Safari browser).
2624 // This is the case where postMessage isn't supported AND accessing a
2625 // window property across origins throws (e.g. old Firefox browser).
2635 var tests
= new Tests();
2637 addEventListener("error", function(e
) {
2638 if (tests
.file_is_test
) {
2639 var test
= tests
.tests
[0];
2640 if (test
.phase
>= test
.phases
.HAS_RESULT
) {
2643 test
.set_status(test
.FAIL
, e
.message
, e
.stack
);
2644 test
.phase
= test
.phases
.HAS_RESULT
;
2647 } else if (!tests
.allow_uncaught_exception
) {
2648 tests
.status
.status
= tests
.status
.ERROR
;
2649 tests
.status
.message
= e
.message
;
2650 tests
.status
.stack
= e
.stack
;
2654 test_environment
.on_tests_ready();
2657 // vim: set expandtab shiftwidth=4 tabstop=4: