Bug 1025824 - Fix mHwcLayerMap handling r=sushil
[gecko.git] / dom / imptests / testharness.js
blobf2423edaa3ab59ba137b6acc243d4cf5681f3ce1
1 /*
2 Distributed under both the W3C Test Suite License [1] and the W3C
3 3-clause BSD License [2]. To contribute to a W3C Test Suite, see the
4 policies and contribution forms [3].
6 [1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license
7 [2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license
8 [3] http://www.w3.org/2004/10/27-testcases
9 */
12  * == Introduction ==
13  *
14  * This file provides a framework for writing testcases. It is intended to
15  * provide a convenient API for making common assertions, and to work both
16  * for testing synchronous and asynchronous DOM features in a way that
17  * promotes clear, robust, tests.
18  *
19  * == Basic Usage ==
20  *
21  * To use this file, import the script and the testharnessreport script into
22  * the test document:
23  * <script src="/resources/testharness.js"></script>
24  * <script src="/resources/testharnessreport.js"></script>
25  *
26  * Within each file one may define one or more tests. Each test is atomic
27  * in the sense that a single test has a single result (pass/fail/timeout).
28  * Within each test one may have a number of asserts. The test fails at the
29  * first failing assert, and the remainder of the test is (typically) not run.
30  *
31  * If the file containing the tests is a HTML file with an element of id "log"
32  * this will be populated with a table containing the test results after all
33  * the tests have run.
34  *
35  * NOTE: By default tests must be created before the load event fires. For ways
36  *       to create tests after the load event, see "Determining when all tests
37  *       are complete", below
38  *
39  * == Synchronous Tests ==
40  *
41  * To create a synchronous test use the test() function:
42  *
43  * test(test_function, name, properties)
44  *
45  * test_function is a function that contains the code to test. For example a
46  * trivial passing test would be:
47  *
48  * test(function() {assert_true(true)}, "assert_true with true")
49  *
50  * The function passed in is run in the test() call.
51  *
52  * properties is an object that overrides default test properties. The
53  * recognised properties are:
54  *    timeout - the test timeout in ms
55  *
56  * e.g.
57  * test(test_function, "Sample test", {timeout:1000})
58  *
59  * would run test_function with a timeout of 1s.
60  *
61  * Additionally, test-specific metadata can be passed in the properties. These
62  * are used when the individual test has different metadata from that stored
63  * in the <head>.
64  * The recognized metadata properties are:
65  *
66  *    help - The url of the part of the specification being tested
67  *
68  *    assert - A human readable description of what the test is attempting
69  *             to prove
70  *
71  *    author - Name and contact information for the author of the test in the
72  *             format: "Name <email_addr>" or "Name http://contact/url"
73  *
74  * == Asynchronous Tests ==
75  *
76  * Testing asynchronous features is somewhat more complex since the result of
77  * a test may depend on one or more events or other callbacks. The API provided
78  * for testing these features is indended to be rather low-level but hopefully
79  * applicable to many situations.
80  *
81  * To create a test, one starts by getting a Test object using async_test:
82  *
83  * async_test(name, properties)
84  *
85  * e.g.
86  * var t = async_test("Simple async test")
87  *
88  * Assertions can be added to the test by calling the step method of the test
89  * object with a function containing the test assertions:
90  *
91  * t.step(function() {assert_true(true)});
92  *
93  * When all the steps are complete, the done() method must be called:
94  *
95  * t.done();
96  *
97  * As a convenience, async_test can also takes a function as first argument.
98  * This function is called with the test object as both its `this` object and
99  * first argument. The above example can be rewritten as:
101  * async_test(function(t) {
102  *     object.some_event = function() {
103  *         t.step(function (){assert_true(true); t.done();});
104  *     };
105  * }, "Simple async test");
107  * which avoids cluttering the global scope with references to async
108  * tests instances.
110  * The properties argument is identical to that for test().
112  * In many cases it is convenient to run a step in response to an event or a
113  * callback. A convenient method of doing this is through the step_func method
114  * which returns a function that, when called runs a test step. For example
116  * object.some_event = t.step_func(function(e) {assert_true(e.a)});
118  * == Making assertions ==
120  * Functions for making assertions start assert_
121  * The best way to get a list is to look in this file for functions names
122  * matching that pattern. The general signature is
124  * assert_something(actual, expected, description)
126  * although not all assertions precisely match this pattern e.g. assert_true
127  * only takes actual and description as arguments.
129  * The description parameter is used to present more useful error messages when
130  * a test fails
132  * NOTE: All asserts must be located in a test() or a step of an async_test().
133  *       asserts outside these places won't be detected correctly by the harness
134  *       and may cause a file to stop testing.
136  * == Harness Timeout ==
137  * 
138  * The overall harness admits two timeout values "normal" (the
139  * default) and "long", used for tests which have an unusually long
140  * runtime. After the timeout is reached, the harness will stop
141  * waiting for further async tests to complete. By default the
142  * timeouts are set to 10s and 60s, respectively, but may be changed
143  * when the test is run on hardware with different performance
144  * characteristics to a common desktop computer.  In order to opt-in
145  * to the longer test timeout, the test must specify a meta element:
146  * <meta name="timeout" content="long">
148  * == Setup ==
150  * Sometimes tests require non-trivial setup that may fail. For this purpose
151  * there is a setup() function, that may be called with one or two arguments.
152  * The two argument version is:
154  * setup(func, properties)
156  * The one argument versions may omit either argument.
157  * func is a function to be run synchronously. setup() becomes a no-op once
158  * any tests have returned results. Properties are global properties of the test
159  * harness. Currently recognised properties are:
162  * explicit_done - Wait for an explicit call to done() before declaring all
163  *                 tests complete (see below)
165  * output_document - The document to which results should be logged. By default
166  *                   this is the current document but could be an ancestor
167  *                   document in some cases e.g. a SVG test loaded in an HTML
168  *                   wrapper
170  * explicit_timeout - disable file timeout; only stop waiting for results
171  *                    when the timeout() function is called (typically for
172  *                    use when integrating with some existing test framework
173  *                    that has its own timeout mechanism).
175  * allow_uncaught_exception - don't treat an uncaught exception as an error;
176  *                            needed when e.g. testing the window.onerror
177  *                            handler.
179  * timeout_multiplier - Multiplier to apply to per-test timeouts.
181  * == Determining when all tests are complete ==
183  * By default the test harness will assume there are no more results to come
184  * when:
185  * 1) There are no Test objects that have been created but not completed
186  * 2) The load event on the document has fired
188  * This behaviour can be overridden by setting the explicit_done property to
189  * true in a call to setup(). If explicit_done is true, the test harness will
190  * not assume it is done until the global done() function is called. Once done()
191  * is called, the two conditions above apply like normal.
193  * == Generating tests ==
195  * NOTE: this functionality may be removed
197  * There are scenarios in which is is desirable to create a large number of
198  * (synchronous) tests that are internally similar but vary in the parameters
199  * used. To make this easier, the generate_tests function allows a single
200  * function to be called with each set of parameters in a list:
202  * generate_tests(test_function, parameter_lists, properties)
204  * For example:
206  * generate_tests(assert_equals, [
207  *     ["Sum one and one", 1+1, 2],
208  *     ["Sum one and zero", 1+0, 1]
209  *     ])
211  * Is equivalent to:
213  * test(function() {assert_equals(1+1, 2)}, "Sum one and one")
214  * test(function() {assert_equals(1+0, 1)}, "Sum one and zero")
216  * Note that the first item in each parameter list corresponds to the name of
217  * the test.
219  * The properties argument is identical to that for test(). This may be a
220  * single object (used for all generated tests) or an array.
222  * == Callback API ==
224  * The framework provides callbacks corresponding to 3 events:
226  * start - happens when the first Test is created
227  * result - happens when a test result is recieved
228  * complete - happens when all results are recieved
230  * The page defining the tests may add callbacks for these events by calling
231  * the following methods:
233  *   add_start_callback(callback) - callback called with no arguments
234  *   add_result_callback(callback) - callback called with a test argument
235  *   add_completion_callback(callback) - callback called with an array of tests
236  *                                       and an status object
238  * tests have the following properties:
239  *   status: A status code. This can be compared to the PASS, FAIL, TIMEOUT and
240  *           NOTRUN properties on the test object
241  *   message: A message indicating the reason for failure. In the future this
242  *            will always be a string
244  *  The status object gives the overall status of the harness. It has the
245  *  following properties:
246  *    status: Can be compared to the OK, ERROR and TIMEOUT properties
247  *    message: An error message set when the status is ERROR
249  * == External API ==
251  * In order to collect the results of multiple pages containing tests, the test
252  * harness will, when loaded in a nested browsing context, attempt to call
253  * certain functions in each ancestor and opener browsing context:
255  * start - start_callback
256  * result - result_callback
257  * complete - completion_callback
259  * These are given the same arguments as the corresponding internal callbacks
260  * described above.
262  * == External API through cross-document messaging ==
264  * Where supported, the test harness will also send messages using
265  * cross-document messaging to each ancestor and opener browsing context. Since
266  * it uses the wildcard keyword (*), cross-origin communication is enabled and
267  * script on different origins can collect the results.
269  * This API follows similar conventions as those described above only slightly
270  * modified to accommodate message event API. Each message is sent by the harness
271  * is passed a single vanilla object, available as the `data` property of the
272  * event object. These objects are structures as follows:
274  * start - { type: "start" }
275  * result - { type: "result", test: Test }
276  * complete - { type: "complete", tests: [Test, ...], status: TestsStatus }
278  * == List of assertions ==
280  * assert_true(actual, description)
281  *   asserts that /actual/ is strictly true
283  * assert_false(actual, description)
284  *   asserts that /actual/ is strictly false
286  * assert_equals(actual, expected, description)
287  *   asserts that /actual/ is the same value as /expected/
289  * assert_not_equals(actual, expected, description)
290  *   asserts that /actual/ is a different value to /expected/. Yes, this means
291  *   that "expected" is a misnomer
293  * assert_in_array(actual, expected, description)
294  *   asserts that /expected/ is an Array, and /actual/ is equal to one of the
295  *   members -- expected.indexOf(actual) != -1
297  * assert_array_equals(actual, expected, description)
298  *   asserts that /actual/ and /expected/ have the same length and the value of
299  *   each indexed property in /actual/ is the strictly equal to the corresponding
300  *   property value in /expected/
302  * assert_approx_equals(actual, expected, epsilon, description)
303  *   asserts that /actual/ is a number within +/- /epsilon/ of /expected/
305  * assert_less_than(actual, expected, description)
306  *   asserts that /actual/ is a number less than /expected/
308  * assert_greater_than(actual, expected, description)
309  *   asserts that /actual/ is a number greater than /expected/
311  * assert_less_than_equal(actual, expected, description)
312  *   asserts that /actual/ is a number less than or equal to /expected/
314  * assert_greater_than_equal(actual, expected, description)
315  *   asserts that /actual/ is a number greater than or equal to /expected/
317  * assert_regexp_match(actual, expected, description)
318  *   asserts that /actual/ matches the regexp /expected/
320  * assert_class_string(object, class_name, description)
321  *   asserts that the class string of /object/ as returned in
322  *   Object.prototype.toString is equal to /class_name/.
324  * assert_own_property(object, property_name, description)
325  *   assert that object has own property property_name
327  * assert_inherits(object, property_name, description)
328  *   assert that object does not have an own property named property_name
329  *   but that property_name is present in the prototype chain for object
331  * assert_idl_attribute(object, attribute_name, description)
332  *   assert that an object that is an instance of some interface has the
333  *   attribute attribute_name following the conditions specified by WebIDL
335  * assert_readonly(object, property_name, description)
336  *   assert that property property_name on object is readonly
338  * assert_throws(code, func, description)
339  *   code - the expected exception:
340  *     o string: the thrown exception must be a DOMException with the given
341  *               name, e.g., "TimeoutError" (for compatibility with existing
342  *               tests, a constant is also supported, e.g., "TIMEOUT_ERR")
343  *     o object: the thrown exception must have a property called "name" that
344  *               matches code.name
345  *     o null:   allow any exception (in general, one of the options above
346  *               should be used)
347  *   func - a function that should throw
349  * assert_unreached(description)
350  *   asserts if called. Used to ensure that some codepath is *not* taken e.g.
351  *   an event does not fire.
353  * assert_any(assert_func, actual, expected_array, extra_arg_1, ... extra_arg_N)
354  *   asserts that one assert_func(actual, expected_array_N, extra_arg1, ..., extra_arg_N)
355  *   is true for some expected_array_N in expected_array. This only works for assert_func
356  *   with signature assert_func(actual, expected, args_1, ..., args_N). Note that tests
357  *   with multiple allowed pass conditions are bad practice unless the spec specifically
358  *   allows multiple behaviours. Test authors should not use this method simply to hide
359  *   UA bugs.
361  * assert_exists(object, property_name, description)
362  *   *** deprecated ***
363  *   asserts that object has an own property property_name
365  * assert_not_exists(object, property_name, description)
366  *   *** deprecated ***
367  *   assert that object does not have own property property_name
368  */
370 (function ()
372     var debug = false;
373     // default timeout is 10 seconds, test can override if needed
374     var settings = {
375       output:true,
376       harness_timeout:{"normal":10000,
377                        "long":60000},
378       test_timeout:null
379     };
381     var xhtml_ns = "http://www.w3.org/1999/xhtml";
383     // script_prefix is used by Output.prototype.show_results() to figure out
384     // where to get testharness.css from.  It's enclosed in an extra closure to
385     // not pollute the library's namespace with variables like "src".
386     var script_prefix = null;
387     (function ()
388     {
389         var scripts = document.getElementsByTagName("script");
390         for (var i = 0; i < scripts.length; i++)
391         {
392             if (scripts[i].src)
393             {
394                 var src = scripts[i].src;
395             }
396             else if (scripts[i].href)
397             {
398                 //SVG case
399                 var src = scripts[i].href.baseVal;
400             }
401             if (src && src.slice(src.length - "testharness.js".length) === "testharness.js")
402             {
403                 script_prefix = src.slice(0, src.length - "testharness.js".length);
404                 break;
405             }
406         }
407     })();
409     /*
410      * API functions
411      */
413     var name_counter = 0;
414     function next_default_name()
415     {
416         //Don't use document.title to work around an Opera bug in XHTML documents
417         var title = document.getElementsByTagName("title")[0];
418         var prefix = (title && title.firstChild && title.firstChild.data) || "Untitled";
419         var suffix = name_counter > 0 ? " " + name_counter : "";
420         name_counter++;
421         return prefix + suffix;
422     }
424     function test(func, name, properties)
425     {
426         var test_name = name ? name : next_default_name();
427         properties = properties ? properties : {};
428         var test_obj = new Test(test_name, properties);
429         test_obj.step(func);
430         if (test_obj.phase === test_obj.phases.STARTED) {
431             test_obj.done();
432         }
433     }
435     function async_test(func, name, properties)
436     {
437         if (typeof func !== "function") {
438             properties = name;
439             name = func;
440             func = null;
441         }
442         var test_name = name ? name : next_default_name();
443         properties = properties ? properties : {};
444         var test_obj = new Test(test_name, properties);
445         if (func) {
446             test_obj.step(func, test_obj, test_obj);
447         }
448         return test_obj;
449     }
451     function setup(func_or_properties, maybe_properties)
452     {
453         var func = null;
454         var properties = {};
455         if (arguments.length === 2) {
456             func = func_or_properties;
457             properties = maybe_properties;
458         } else if (func_or_properties instanceof Function){
459             func = func_or_properties;
460         } else {
461             properties = func_or_properties;
462         }
463         tests.setup(func, properties);
464         output.setup(properties);
465     }
467     function done() {
468         tests.end_wait();
469     }
471     function generate_tests(func, args, properties) {
472         forEach(args, function(x, i)
473                 {
474                     var name = x[0];
475                     test(function()
476                          {
477                              func.apply(this, x.slice(1));
478                          },
479                          name,
480                          Array.isArray(properties) ? properties[i] : properties);
481                 });
482     }
484     function on_event(object, event, callback)
485     {
486       object.addEventListener(event, callback, false);
487     }
489     expose(test, 'test');
490     expose(async_test, 'async_test');
491     expose(generate_tests, 'generate_tests');
492     expose(setup, 'setup');
493     expose(done, 'done');
494     expose(on_event, 'on_event');
496     /*
497      * Return a string truncated to the given length, with ... added at the end
498      * if it was longer.
499      */
500     function truncate(s, len)
501     {
502         if (s.length > len) {
503             return s.substring(0, len - 3) + "...";
504         }
505         return s;
506     }
508     /*
509      * Return true if object is probably a Node object.
510      */
511     function is_node(object)
512     {
513         // I use duck-typing instead of instanceof, because
514         // instanceof doesn't work if the node is from another window (like an
515         // iframe's contentWindow):
516         // http://www.w3.org/Bugs/Public/show_bug.cgi?id=12295
517         if ("nodeType" in object
518         && "nodeName" in object
519         && "nodeValue" in object
520         && "childNodes" in object)
521         {
522             try
523             {
524                 object.nodeType;
525             }
526             catch (e)
527             {
528                 // The object is probably Node.prototype or another prototype
529                 // object that inherits from it, and not a Node instance.
530                 return false;
531             }
532             return true;
533         }
534         return false;
535     }
537     /*
538      * Convert a value to a nice, human-readable string
539      */
540     function format_value(val, seen)
541     {
542         if (!seen) {
543             seen = [];
544         }
545         if (typeof val === "object" && val !== null)
546         {
547             if (seen.indexOf(val) >= 0)
548             {
549                 return "[...]";
550             }
551             seen.push(val);
552         }
553         if (Array.isArray(val))
554         {
555             return "[" + val.map(function(x) {return format_value(x, seen)}).join(", ") + "]";
556         }
558         switch (typeof val)
559         {
560         case "string":
561             val = val.replace("\\", "\\\\");
562             for (var i = 0; i < 32; i++)
563             {
564                 var replace = "\\";
565                 switch (i) {
566                 case 0: replace += "0"; break;
567                 case 1: replace += "x01"; break;
568                 case 2: replace += "x02"; break;
569                 case 3: replace += "x03"; break;
570                 case 4: replace += "x04"; break;
571                 case 5: replace += "x05"; break;
572                 case 6: replace += "x06"; break;
573                 case 7: replace += "x07"; break;
574                 case 8: replace += "b"; break;
575                 case 9: replace += "t"; break;
576                 case 10: replace += "n"; break;
577                 case 11: replace += "v"; break;
578                 case 12: replace += "f"; break;
579                 case 13: replace += "r"; break;
580                 case 14: replace += "x0e"; break;
581                 case 15: replace += "x0f"; break;
582                 case 16: replace += "x10"; break;
583                 case 17: replace += "x11"; break;
584                 case 18: replace += "x12"; break;
585                 case 19: replace += "x13"; break;
586                 case 20: replace += "x14"; break;
587                 case 21: replace += "x15"; break;
588                 case 22: replace += "x16"; break;
589                 case 23: replace += "x17"; break;
590                 case 24: replace += "x18"; break;
591                 case 25: replace += "x19"; break;
592                 case 26: replace += "x1a"; break;
593                 case 27: replace += "x1b"; break;
594                 case 28: replace += "x1c"; break;
595                 case 29: replace += "x1d"; break;
596                 case 30: replace += "x1e"; break;
597                 case 31: replace += "x1f"; break;
598                 }
599                 val = val.replace(RegExp(String.fromCharCode(i), "g"), replace);
600             }
601             return '"' + val.replace(/"/g, '\\"') + '"';
602         case "boolean":
603         case "undefined":
604             return String(val);
605         case "number":
606             // In JavaScript, -0 === 0 and String(-0) == "0", so we have to
607             // special-case.
608             if (val === -0 && 1/val === -Infinity)
609             {
610                 return "-0";
611             }
612             return String(val);
613         case "object":
614             if (val === null)
615             {
616                 return "null";
617             }
619             // Special-case Node objects, since those come up a lot in my tests.  I
620             // ignore namespaces.
621             if (is_node(val))
622             {
623                 switch (val.nodeType)
624                 {
625                 case Node.ELEMENT_NODE:
626                     var ret = "<" + val.tagName.toLowerCase();
627                     for (var i = 0; i < val.attributes.length; i++)
628                     {
629                         ret += " " + val.attributes[i].name + '="' + val.attributes[i].value + '"';
630                     }
631                     ret += ">" + val.innerHTML + "</" + val.tagName.toLowerCase() + ">";
632                     return "Element node " + truncate(ret, 60);
633                 case Node.TEXT_NODE:
634                     return 'Text node "' + truncate(val.data, 60) + '"';
635                 case Node.PROCESSING_INSTRUCTION_NODE:
636                     return "ProcessingInstruction node with target " + format_value(truncate(val.target, 60)) + " and data " + format_value(truncate(val.data, 60));
637                 case Node.COMMENT_NODE:
638                     return "Comment node <!--" + truncate(val.data, 60) + "-->";
639                 case Node.DOCUMENT_NODE:
640                     return "Document node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children");
641                 case Node.DOCUMENT_TYPE_NODE:
642                     return "DocumentType node";
643                 case Node.DOCUMENT_FRAGMENT_NODE:
644                     return "DocumentFragment node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children");
645                 default:
646                     return "Node object of unknown type";
647                 }
648             }
650             // Fall through to default
651         default:
652             return typeof val + ' "' + truncate(String(val), 60) + '"';
653         }
654     }
655     expose(format_value, "format_value");
657     /*
658      * Assertions
659      */
661     function assert_true(actual, description)
662     {
663         assert(actual === true, "assert_true", description,
664                                 "expected true got ${actual}", {actual:actual});
665     };
666     expose(assert_true, "assert_true");
668     function assert_false(actual, description)
669     {
670         assert(actual === false, "assert_false", description,
671                                  "expected false got ${actual}", {actual:actual});
672     };
673     expose(assert_false, "assert_false");
675     function same_value(x, y) {
676         if (y !== y)
677         {
678             //NaN case
679             return x !== x;
680         }
681         else if (x === 0 && y === 0) {
682             //Distinguish +0 and -0
683             return 1/x === 1/y;
684         }
685         else
686         {
687             //typical case
688             return x === y;
689         }
690     }
692     function assert_equals(actual, expected, description)
693     {
694          /*
695           * Test if two primitives are equal or two objects
696           * are the same object
697           */
698         if (typeof actual != typeof expected)
699         {
700             assert(false, "assert_equals", description,
701                           "expected (" + typeof expected + ") ${expected} but got (" + typeof actual + ") ${actual}",
702                           {expected:expected, actual:actual});
703             return;
704         }
705         assert(same_value(actual, expected), "assert_equals", description,
706                                              "expected ${expected} but got ${actual}",
707                                              {expected:expected, actual:actual});
708     };
709     expose(assert_equals, "assert_equals");
711     function assert_not_equals(actual, expected, description)
712     {
713          /*
714           * Test if two primitives are unequal or two objects
715           * are different objects
716           */
717         assert(!same_value(actual, expected), "assert_not_equals", description,
718                                               "got disallowed value ${actual}",
719                                               {actual:actual});
720     };
721     expose(assert_not_equals, "assert_not_equals");
723     function assert_in_array(actual, expected, description)
724     {
725         assert(expected.indexOf(actual) != -1, "assert_in_array", description,
726                                                "value ${actual} not in array ${expected}",
727                                                {actual:actual, expected:expected});
728     }
729     expose(assert_in_array, "assert_in_array");
731     function assert_object_equals(actual, expected, description)
732     {
733          //This needs to be improved a great deal
734          function check_equal(actual, expected, stack)
735          {
736              stack.push(actual);
738              var p;
739              for (p in actual)
740              {
741                  assert(expected.hasOwnProperty(p), "assert_object_equals", description,
742                                                     "unexpected property ${p}", {p:p});
744                  if (typeof actual[p] === "object" && actual[p] !== null)
745                  {
746                      if (stack.indexOf(actual[p]) === -1)
747                      {
748                          check_equal(actual[p], expected[p], stack);
749                      }
750                  }
751                  else
752                  {
753                      assert(same_value(actual[p], expected[p]), "assert_object_equals", description,
754                                                        "property ${p} expected ${expected} got ${actual}",
755                                                        {p:p, expected:expected, actual:actual});
756                  }
757              }
758              for (p in expected)
759              {
760                  assert(actual.hasOwnProperty(p),
761                         "assert_object_equals", description,
762                         "expected property ${p} missing", {p:p});
763              }
764              stack.pop();
765          }
766          check_equal(actual, expected, []);
767     };
768     expose(assert_object_equals, "assert_object_equals");
770     function assert_array_equals(actual, expected, description)
771     {
772         assert(actual.length === expected.length,
773                "assert_array_equals", description,
774                "lengths differ, expected ${expected} got ${actual}",
775                {expected:expected.length, actual:actual.length});
777         for (var i=0; i < actual.length; i++)
778         {
779             assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i),
780                    "assert_array_equals", description,
781                    "property ${i}, property expected to be $expected but was $actual",
782                    {i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing",
783                    actual:actual.hasOwnProperty(i) ? "present" : "missing"});
784             assert(same_value(expected[i], actual[i]),
785                    "assert_array_equals", description,
786                    "property ${i}, expected ${expected} but got ${actual}",
787                    {i:i, expected:expected[i], actual:actual[i]});
788         }
789     }
790     expose(assert_array_equals, "assert_array_equals");
792     function assert_approx_equals(actual, expected, epsilon, description)
793     {
794         /*
795          * Test if two primitive numbers are equal withing +/- epsilon
796          */
797         assert(typeof actual === "number",
798                "assert_approx_equals", description,
799                "expected a number but got a ${type_actual}",
800                {type_actual:typeof actual});
802         assert(Math.abs(actual - expected) <= epsilon,
803                "assert_approx_equals", description,
804                "expected ${expected} +/- ${epsilon} but got ${actual}",
805                {expected:expected, actual:actual, epsilon:epsilon});
806     };
807     expose(assert_approx_equals, "assert_approx_equals");
809     function assert_less_than(actual, expected, description)
810     {
811         /*
812          * Test if a primitive number is less than another
813          */
814         assert(typeof actual === "number",
815                "assert_less_than", description,
816                "expected a number but got a ${type_actual}",
817                {type_actual:typeof actual});
819         assert(actual < expected,
820                "assert_less_than", description,
821                "expected a number less than ${expected} but got ${actual}",
822                {expected:expected, actual:actual});
823     };
824     expose(assert_less_than, "assert_less_than");
826     function assert_greater_than(actual, expected, description)
827     {
828         /*
829          * Test if a primitive number is greater than another
830          */
831         assert(typeof actual === "number",
832                "assert_greater_than", description,
833                "expected a number but got a ${type_actual}",
834                {type_actual:typeof actual});
836         assert(actual > expected,
837                "assert_greater_than", description,
838                "expected a number greater than ${expected} but got ${actual}",
839                {expected:expected, actual:actual});
840     };
841     expose(assert_greater_than, "assert_greater_than");
843     function assert_less_than_equal(actual, expected, description)
844     {
845         /*
846          * Test if a primitive number is less than or equal to another
847          */
848         assert(typeof actual === "number",
849                "assert_less_than_equal", description,
850                "expected a number but got a ${type_actual}",
851                {type_actual:typeof actual});
853         assert(actual <= expected,
854                "assert_less_than", description,
855                "expected a number less than or equal to ${expected} but got ${actual}",
856                {expected:expected, actual:actual});
857     };
858     expose(assert_less_than_equal, "assert_less_than_equal");
860     function assert_greater_than_equal(actual, expected, description)
861     {
862         /*
863          * Test if a primitive number is greater than or equal to another
864          */
865         assert(typeof actual === "number",
866                "assert_greater_than_equal", description,
867                "expected a number but got a ${type_actual}",
868                {type_actual:typeof actual});
870         assert(actual >= expected,
871                "assert_greater_than_equal", description,
872                "expected a number greater than or equal to ${expected} but got ${actual}",
873                {expected:expected, actual:actual});
874     };
875     expose(assert_greater_than_equal, "assert_greater_than_equal");
877     function assert_regexp_match(actual, expected, description) {
878         /*
879          * Test if a string (actual) matches a regexp (expected)
880          */
881         assert(expected.test(actual),
882                "assert_regexp_match", description,
883                "expected ${expected} but got ${actual}",
884                {expected:expected, actual:actual});
885     }
886     expose(assert_regexp_match, "assert_regexp_match");
888     function assert_class_string(object, class_string, description) {
889         assert_equals({}.toString.call(object), "[object " + class_string + "]",
890                       description);
891     }
892     expose(assert_class_string, "assert_class_string");
895     function _assert_own_property(name) {
896         return function(object, property_name, description)
897         {
898             assert(object.hasOwnProperty(property_name),
899                    name, description,
900                    "expected property ${p} missing", {p:property_name});
901         };
902     }
903     expose(_assert_own_property("assert_exists"), "assert_exists");
904     expose(_assert_own_property("assert_own_property"), "assert_own_property");
906     function assert_not_exists(object, property_name, description)
907     {
908         assert(!object.hasOwnProperty(property_name),
909                "assert_not_exists", description,
910                "unexpected property ${p} found", {p:property_name});
911     };
912     expose(assert_not_exists, "assert_not_exists");
914     function _assert_inherits(name) {
915         return function (object, property_name, description)
916         {
917             assert(typeof object === "object",
918                    name, description,
919                    "provided value is not an object");
921             assert("hasOwnProperty" in object,
922                    name, description,
923                    "provided value is an object but has no hasOwnProperty method");
925             assert(!object.hasOwnProperty(property_name),
926                    name, description,
927                    "property ${p} found on object expected in prototype chain",
928                    {p:property_name});
930             assert(property_name in object,
931                    name, description,
932                    "property ${p} not found in prototype chain",
933                    {p:property_name});
934         };
935     }
936     expose(_assert_inherits("assert_inherits"), "assert_inherits");
937     expose(_assert_inherits("assert_idl_attribute"), "assert_idl_attribute");
939     function assert_readonly(object, property_name, description)
940     {
941          var initial_value = object[property_name];
942          try {
943              //Note that this can have side effects in the case where
944              //the property has PutForwards
945              object[property_name] = initial_value + "a"; //XXX use some other value here?
946              assert(same_value(object[property_name], initial_value),
947                     "assert_readonly", description,
948                     "changing property ${p} succeeded",
949                     {p:property_name});
950          }
951          finally
952          {
953              object[property_name] = initial_value;
954          }
955     };
956     expose(assert_readonly, "assert_readonly");
958     function assert_throws(code, func, description)
959     {
960         try
961         {
962             func.call(this);
963             assert(false, "assert_throws", description,
964                    "${func} did not throw", {func:func});
965         }
966         catch(e)
967         {
968             if (e instanceof AssertionError) {
969                 throw(e);
970             }
971             if (code === null)
972             {
973                 return;
974             }
975             if (typeof code === "object")
976             {
977                 assert(typeof e == "object" && "name" in e && e.name == code.name,
978                        "assert_throws", description,
979                        "${func} threw ${actual} (${actual_name}) expected ${expected} (${expected_name})",
980                                     {func:func, actual:e, actual_name:e.name,
981                                      expected:code,
982                                      expected_name:code.name});
983                 return;
984             }
986             var code_name_map = {
987                 INDEX_SIZE_ERR: 'IndexSizeError',
988                 HIERARCHY_REQUEST_ERR: 'HierarchyRequestError',
989                 WRONG_DOCUMENT_ERR: 'WrongDocumentError',
990                 INVALID_CHARACTER_ERR: 'InvalidCharacterError',
991                 NO_MODIFICATION_ALLOWED_ERR: 'NoModificationAllowedError',
992                 NOT_FOUND_ERR: 'NotFoundError',
993                 NOT_SUPPORTED_ERR: 'NotSupportedError',
994                 INVALID_STATE_ERR: 'InvalidStateError',
995                 SYNTAX_ERR: 'SyntaxError',
996                 INVALID_MODIFICATION_ERR: 'InvalidModificationError',
997                 NAMESPACE_ERR: 'NamespaceError',
998                 INVALID_ACCESS_ERR: 'InvalidAccessError',
999                 TYPE_MISMATCH_ERR: 'TypeMismatchError',
1000                 SECURITY_ERR: 'SecurityError',
1001                 NETWORK_ERR: 'NetworkError',
1002                 ABORT_ERR: 'AbortError',
1003                 URL_MISMATCH_ERR: 'URLMismatchError',
1004                 QUOTA_EXCEEDED_ERR: 'QuotaExceededError',
1005                 TIMEOUT_ERR: 'TimeoutError',
1006                 INVALID_NODE_TYPE_ERR: 'InvalidNodeTypeError',
1007                 DATA_CLONE_ERR: 'DataCloneError'
1008             };
1010             var name = code in code_name_map ? code_name_map[code] : code;
1012             var name_code_map = {
1013                 IndexSizeError: 1,
1014                 HierarchyRequestError: 3,
1015                 WrongDocumentError: 4,
1016                 InvalidCharacterError: 5,
1017                 NoModificationAllowedError: 7,
1018                 NotFoundError: 8,
1019                 NotSupportedError: 9,
1020                 InvalidStateError: 11,
1021                 SyntaxError: 12,
1022                 InvalidModificationError: 13,
1023                 NamespaceError: 14,
1024                 InvalidAccessError: 15,
1025                 TypeMismatchError: 17,
1026                 SecurityError: 18,
1027                 NetworkError: 19,
1028                 AbortError: 20,
1029                 URLMismatchError: 21,
1030                 QuotaExceededError: 22,
1031                 TimeoutError: 23,
1032                 InvalidNodeTypeError: 24,
1033                 DataCloneError: 25,
1035                 UnknownError: 0,
1036                 ConstraintError: 0,
1037                 DataError: 0,
1038                 TransactionInactiveError: 0,
1039                 ReadOnlyError: 0,
1040                 VersionError: 0
1041             };
1043             if (!(name in name_code_map))
1044             {
1045                 throw new AssertionError('Test bug: unrecognized DOMException code "' + code + '" passed to assert_throws()');
1046             }
1048             var required_props = { code: name_code_map[name] };
1050             if (required_props.code === 0
1051             || ("name" in e && e.name !== e.name.toUpperCase() && e.name !== "DOMException"))
1052             {
1053                 // New style exception: also test the name property.
1054                 required_props.name = name;
1055             }
1057             //We'd like to test that e instanceof the appropriate interface,
1058             //but we can't, because we don't know what window it was created
1059             //in.  It might be an instanceof the appropriate interface on some
1060             //unknown other window.  TODO: Work around this somehow?
1062             assert(typeof e == "object",
1063                    "assert_throws", description,
1064                    "${func} threw ${e} with type ${type}, not an object",
1065                    {func:func, e:e, type:typeof e});
1067             for (var prop in required_props)
1068             {
1069                 assert(typeof e == "object" && prop in e && e[prop] == required_props[prop],
1070                        "assert_throws", description,
1071                        "${func} threw ${e} that is not a DOMException " + code + ": property ${prop} is equal to ${actual}, expected ${expected}",
1072                        {func:func, e:e, prop:prop, actual:e[prop], expected:required_props[prop]});
1073             }
1074         }
1075     }
1076     expose(assert_throws, "assert_throws");
1078     function assert_unreached(description) {
1079          assert(false, "assert_unreached", description,
1080                 "Reached unreachable code");
1081     }
1082     expose(assert_unreached, "assert_unreached");
1084     function assert_any(assert_func, actual, expected_array)
1085     {
1086         var args = [].slice.call(arguments, 3)
1087         var errors = []
1088         var passed = false;
1089         forEach(expected_array,
1090                 function(expected)
1091                 {
1092                     try {
1093                         assert_func.apply(this, [actual, expected].concat(args))
1094                         passed = true;
1095                     } catch(e) {
1096                         errors.push(e.message);
1097                     }
1098                 });
1099         if (!passed) {
1100             throw new AssertionError(errors.join("\n\n"));
1101         }
1102     }
1103     expose(assert_any, "assert_any");
1105     function Test(name, properties)
1106     {
1107         this.name = name;
1109         this.phases = {
1110             INITIAL:0,
1111             STARTED:1,
1112             HAS_RESULT:2,
1113             COMPLETE:3
1114         };
1115         this.phase = this.phases.INITIAL;
1117         this.status = this.NOTRUN;
1118         this.timeout_id = null;
1120         this.properties = properties;
1121         var timeout = properties.timeout ? properties.timeout : settings.test_timeout
1122         if (timeout != null) {
1123             this.timeout_length = timeout * tests.timeout_multiplier;
1124         } else {
1125             this.timeout_length = null;
1126         }
1128         this.message = null;
1130         var this_obj = this;
1131         this.steps = [];
1133         tests.push(this);
1134     }
1136     Test.statuses = {
1137         PASS:0,
1138         FAIL:1,
1139         TIMEOUT:2,
1140         NOTRUN:3
1141     };
1143     Test.prototype = merge({}, Test.statuses);
1145     Test.prototype.structured_clone = function()
1146     {
1147         if(!this._structured_clone)
1148         {
1149             var msg = this.message;
1150             msg = msg ? String(msg) : msg;
1151             this._structured_clone = merge({
1152                 name:String(this.name),
1153                 status:this.status,
1154                 message:msg
1155             }, Test.statuses);
1156         }
1157         return this._structured_clone;
1158     };
1160     Test.prototype.step = function(func, this_obj)
1161     {
1162         if (this.phase > this.phases.STARTED)
1163         {
1164           return;
1165         }
1166         this.phase = this.phases.STARTED;
1167         //If we don't get a result before the harness times out that will be a test timout
1168         this.set_status(this.TIMEOUT, "Test timed out");
1170         tests.started = true;
1172         if (this.timeout_id === null)
1173         {
1174             this.set_timeout();
1175         }
1177         this.steps.push(func);
1179         if (arguments.length === 1)
1180         {
1181             this_obj = this;
1182         }
1184         try
1185         {
1186             return func.apply(this_obj, Array.prototype.slice.call(arguments, 2));
1187         }
1188         catch(e)
1189         {
1190             if (this.phase >= this.phases.HAS_RESULT)
1191             {
1192                 return;
1193             }
1194             var message = (typeof e === "object" && e !== null) ? e.message : e;
1195             if (typeof e.stack != "undefined" && typeof e.message == "string") {
1196                 //Try to make it more informative for some exceptions, at least
1197                 //in Gecko and WebKit.  This results in a stack dump instead of
1198                 //just errors like "Cannot read property 'parentNode' of null"
1199                 //or "root is null".  Makes it a lot longer, of course.
1200                 message += "(stack: " + e.stack + ")";
1201             }
1202             this.set_status(this.FAIL, message);
1203             this.phase = this.phases.HAS_RESULT;
1204             this.done();
1205         }
1206     };
1208     Test.prototype.step_func = function(func, this_obj)
1209     {
1210         var test_this = this;
1212         if (arguments.length === 1)
1213         {
1214             this_obj = test_this;
1215         }
1217         return function()
1218         {
1219             test_this.step.apply(test_this, [func, this_obj].concat(
1220                 Array.prototype.slice.call(arguments)));
1221         };
1222     };
1224     Test.prototype.step_func_done = function(func, this_obj)
1225     {
1226         var test_this = this;
1228         if (arguments.length === 1)
1229         {
1230             this_obj = test_this;
1231         }
1233         return function()
1234         {
1235             test_this.step.apply(test_this, [func, this_obj].concat(
1236                 Array.prototype.slice.call(arguments)));
1237             test_this.done();
1238         };
1239     }
1241     Test.prototype.set_timeout = function()
1242     {
1243         if (this.timeout_length !== null)
1244         {
1245             var this_obj = this;
1246             this.timeout_id = setTimeout(function()
1247                                          {
1248                                              this_obj.timeout();
1249                                          }, this.timeout_length);
1250         }
1251     }
1253     Test.prototype.set_status = function(status, message)
1254     {
1255         this.status = status;
1256         this.message = message;
1257     }
1259     Test.prototype.timeout = function()
1260     {
1261         this.timeout_id = null;
1262         this.set_status(this.TIMEOUT, "Test timed out")
1263         this.phase = this.phases.HAS_RESULT;
1264         this.done();
1265     };
1267     Test.prototype.done = function()
1268     {
1269         if (this.phase == this.phases.COMPLETE) {
1270             return;
1271         } else if (this.phase <= this.phases.STARTED)
1272         {
1273             this.set_status(this.PASS, null);
1274         }
1276         if (this.status == this.NOTRUN)
1277         {
1278             alert(this.phase);
1279         }
1281         this.phase = this.phases.COMPLETE;
1283         clearTimeout(this.timeout_id);
1284         tests.result(this);
1285     };
1288     /*
1289      * Harness
1290      */
1292     function TestsStatus()
1293     {
1294         this.status = null;
1295         this.message = null;
1296     }
1298     TestsStatus.statuses = {
1299         OK:0,
1300         ERROR:1,
1301         TIMEOUT:2
1302     };
1304     TestsStatus.prototype = merge({}, TestsStatus.statuses);
1306     TestsStatus.prototype.structured_clone = function()
1307     {
1308         if(!this._structured_clone)
1309         {
1310             var msg = this.message;
1311             msg = msg ? String(msg) : msg;
1312             this._structured_clone = merge({
1313                 status:this.status,
1314                 message:msg
1315             }, TestsStatus.statuses);
1316         }
1317         return this._structured_clone;
1318     };
1320     function Tests()
1321     {
1322         this.tests = [];
1323         this.num_pending = 0;
1325         this.phases = {
1326             INITIAL:0,
1327             SETUP:1,
1328             HAVE_TESTS:2,
1329             HAVE_RESULTS:3,
1330             COMPLETE:4
1331         };
1332         this.phase = this.phases.INITIAL;
1334         this.properties = {};
1336         //All tests can't be done until the load event fires
1337         this.all_loaded = false;
1338         this.wait_for_finish = false;
1339         this.processing_callbacks = false;
1341         this.allow_uncaught_exception = false;
1343         this.timeout_multiplier = 1;
1344         this.timeout_length = this.get_timeout();
1345         this.timeout_id = null;
1347         this.start_callbacks = [];
1348         this.test_done_callbacks = [];
1349         this.all_done_callbacks = [];
1351         this.status = new TestsStatus();
1353         var this_obj = this;
1355         on_event(window, "load",
1356                  function()
1357                  {
1358                      this_obj.all_loaded = true;
1359                      if (this_obj.all_done())
1360                      {
1361                          this_obj.complete();
1362                      }
1363                  });
1365         this.set_timeout();
1366     }
1368     Tests.prototype.setup = function(func, properties)
1369     {
1370         if (this.phase >= this.phases.HAVE_RESULTS)
1371         {
1372             return;
1373         }
1374         if (this.phase < this.phases.SETUP)
1375         {
1376             this.phase = this.phases.SETUP;
1377         }
1379         this.properties = properties;
1381         for (var p in properties)
1382         {
1383             if (properties.hasOwnProperty(p))
1384             {
1385                 var value = properties[p]
1386                 if (p == "allow_uncaught_exception") {
1387                     this.allow_uncaught_exception = value;
1388                 }
1389                 else if (p == "explicit_done" && value)
1390                 {
1391                     this.wait_for_finish = true;
1392                 }
1393                 else if (p == "explicit_timeout" && value) {
1394                     this.timeout_length = null;
1395                     if (this.timeout_id)
1396                     {
1397                         clearTimeout(this.timeout_id);
1398                     }
1399                 }
1400                 else if (p == "timeout_multiplier")
1401                 {
1402                     this.timeout_multiplier = value;
1403                 }
1404             }
1405         }
1407         if (func)
1408         {
1409             try
1410             {
1411                 func();
1412             } catch(e)
1413             {
1414                 this.status.status = this.status.ERROR;
1415                 this.status.message = e;
1416             };
1417         }
1418         this.set_timeout();
1419     };
1421     Tests.prototype.get_timeout = function()
1422     {
1423         var metas = document.getElementsByTagName("meta");
1424         for (var i=0; i<metas.length; i++)
1425         {
1426             if (metas[i].name == "timeout")
1427             {
1428                 if (metas[i].content == "long")
1429                 {
1430                     return settings.harness_timeout.long;
1431                 }
1432                 break;
1433             }
1434         }
1435         return settings.harness_timeout.normal;
1436     }
1438     Tests.prototype.set_timeout = function()
1439     {
1440         var this_obj = this;
1441         clearTimeout(this.timeout_id);
1442         if (this.timeout_length !== null)
1443         {
1444             this.timeout_id = setTimeout(function() {
1445                                              this_obj.timeout();
1446                                          }, this.timeout_length);
1447         }
1448     };
1450     Tests.prototype.timeout = function() {
1451         this.status.status = this.status.TIMEOUT;
1452         this.complete();
1453     };
1455     Tests.prototype.end_wait = function()
1456     {
1457         this.wait_for_finish = false;
1458         if (this.all_done()) {
1459             this.complete();
1460         }
1461     };
1463     Tests.prototype.push = function(test)
1464     {
1465         if (this.phase < this.phases.HAVE_TESTS) {
1466             this.start();
1467         }
1468         this.num_pending++;
1469         this.tests.push(test);
1470     };
1472     Tests.prototype.all_done = function() {
1473         return (this.all_loaded && this.num_pending === 0 &&
1474                 !this.wait_for_finish && !this.processing_callbacks);
1475     };
1477     Tests.prototype.start = function() {
1478         this.phase = this.phases.HAVE_TESTS;
1479         this.notify_start();
1480     };
1482     Tests.prototype.notify_start = function() {
1483         var this_obj = this;
1484         forEach (this.start_callbacks,
1485                  function(callback)
1486                  {
1487                      callback(this_obj.properties);
1488                  });
1489         forEach_windows(
1490                 function(w, is_same_origin)
1491                 {
1492                     if(is_same_origin && w.start_callback)
1493                     {
1494                         try
1495                         {
1496                             w.start_callback(this_obj.properties);
1497                         }
1498                         catch(e)
1499                         {
1500                             if (debug)
1501                             {
1502                                 throw(e);
1503                             }
1504                         }
1505                     }
1506                     if (supports_post_message(w) && w !== self)
1507                     {
1508                         w.postMessage({
1509                             type: "start",
1510                             properties: this_obj.properties
1511                         }, "*");
1512                     }
1513                 });
1514     };
1516     Tests.prototype.result = function(test)
1517     {
1518         if (this.phase > this.phases.HAVE_RESULTS)
1519         {
1520             return;
1521         }
1522         this.phase = this.phases.HAVE_RESULTS;
1523         this.num_pending--;
1524         this.notify_result(test);
1525     };
1527     Tests.prototype.notify_result = function(test) {
1528         var this_obj = this;
1529         this.processing_callbacks = true;
1530         forEach(this.test_done_callbacks,
1531                 function(callback)
1532                 {
1533                     callback(test, this_obj);
1534                 });
1536         forEach_windows(
1537                 function(w, is_same_origin)
1538                 {
1539                     if(is_same_origin && w.result_callback)
1540                     {
1541                         try
1542                         {
1543                             w.result_callback(test);
1544                         }
1545                         catch(e)
1546                         {
1547                             if(debug) {
1548                                 throw e;
1549                             }
1550                         }
1551                     }
1552                     if (supports_post_message(w) && w !== self)
1553                     {
1554                         w.postMessage({
1555                             type: "result",
1556                             test: test.structured_clone()
1557                         }, "*");
1558                     }
1559                 });
1560         this.processing_callbacks = false;
1561         if (this_obj.all_done())
1562         {
1563             this_obj.complete();
1564         }
1565     };
1567     Tests.prototype.complete = function() {
1568         if (this.phase === this.phases.COMPLETE) {
1569             return;
1570         }
1571         this.phase = this.phases.COMPLETE;
1572         var this_obj = this;
1573         this.tests.forEach(
1574             function(x)
1575             {
1576                 if(x.status === x.NOTRUN)
1577                 {
1578                     this_obj.notify_result(x);
1579                 }
1580             }
1581         );
1582         this.notify_complete();
1583     };
1585     Tests.prototype.notify_complete = function()
1586     {
1587         clearTimeout(this.timeout_id);
1588         var this_obj = this;
1589         var tests = map(this_obj.tests,
1590                         function(test)
1591                         {
1592                             return test.structured_clone();
1593                         });
1594         if (this.status.status === null)
1595         {
1596             this.status.status = this.status.OK;
1597         }
1599         forEach (this.all_done_callbacks,
1600                  function(callback)
1601                  {
1602                      callback(this_obj.tests, this_obj.status);
1603                  });
1605         forEach_windows(
1606                 function(w, is_same_origin)
1607                 {
1608                     if(is_same_origin && w.completion_callback)
1609                     {
1610                         try
1611                         {
1612                             w.completion_callback(this_obj.tests, this_obj.status);
1613                         }
1614                         catch(e)
1615                         {
1616                             if (debug)
1617                             {
1618                                 throw e;
1619                             }
1620                         }
1621                     }
1622                     if (supports_post_message(w) && w !== self)
1623                     {
1624                         w.postMessage({
1625                             type: "complete",
1626                             tests: tests,
1627                             status: this_obj.status.structured_clone()
1628                         }, "*");
1629                     }
1630                 });
1631     };
1633     var tests = new Tests();
1635     window.onerror = function(msg) {
1636         if (!tests.allow_uncaught_exception)
1637         {
1638             tests.status.status = tests.status.ERROR;
1639             tests.status.message = msg;
1640             tests.complete();
1641         }
1642     }
1644     function timeout() {
1645         if (tests.timeout_length === null)
1646         {
1647             tests.timeout();
1648         }
1649     }
1650     expose(timeout, 'timeout');
1652     function add_start_callback(callback) {
1653         tests.start_callbacks.push(callback);
1654     }
1656     function add_result_callback(callback)
1657     {
1658         tests.test_done_callbacks.push(callback);
1659     }
1661     function add_completion_callback(callback)
1662     {
1663        tests.all_done_callbacks.push(callback);
1664     }
1666     expose(add_start_callback, 'add_start_callback');
1667     expose(add_result_callback, 'add_result_callback');
1668     expose(add_completion_callback, 'add_completion_callback');
1670     /*
1671      * Output listener
1672     */
1674     function Output() {
1675       this.output_document = document;
1676       this.output_node = null;
1677       this.done_count = 0;
1678       this.enabled = settings.output;
1679       this.phase = this.INITIAL;
1680     }
1682     Output.prototype.INITIAL = 0;
1683     Output.prototype.STARTED = 1;
1684     Output.prototype.HAVE_RESULTS = 2;
1685     Output.prototype.COMPLETE = 3;
1687     Output.prototype.setup = function(properties) {
1688         if (this.phase > this.INITIAL) {
1689             return;
1690         }
1692         //If output is disabled in testharnessreport.js the test shouldn't be
1693         //able to override that
1694         this.enabled = this.enabled && (properties.hasOwnProperty("output") ?
1695                                         properties.output : settings.output);
1696     };
1698     Output.prototype.init = function(properties)
1699     {
1700         if (this.phase >= this.STARTED) {
1701             return;
1702         }
1703         if (properties.output_document) {
1704             this.output_document = properties.output_document;
1705         } else {
1706             this.output_document = document;
1707         }
1708         this.phase = this.STARTED;
1709     };
1711     Output.prototype.resolve_log = function()
1712     {
1713         var output_document;
1714         if (typeof this.output_document === "function")
1715         {
1716             output_document = this.output_document.apply(undefined);
1717         } else 
1718         {
1719             output_document = this.output_document;
1720         }
1721         if (!output_document)
1722         {
1723             return;
1724         }
1725         var node = output_document.getElementById("log");
1726         if (node)
1727         {
1728             this.output_document = output_document;
1729             this.output_node = node;
1730         }
1731     };
1733     Output.prototype.show_status = function(test)
1734     {
1735         if (this.phase < this.STARTED)
1736         {
1737             this.init();
1738         }
1739         if (!this.enabled)
1740         {
1741             return;
1742         }
1743         if (this.phase < this.HAVE_RESULTS)
1744         {
1745             this.resolve_log();
1746             this.phase = this.HAVE_RESULTS;
1747         }
1748         this.done_count++;
1749         if (this.output_node)
1750         {
1751             if (this.done_count < 100
1752             || (this.done_count < 1000 && this.done_count % 100 == 0)
1753             || this.done_count % 1000 == 0) {
1754                 this.output_node.textContent = "Running, "
1755                     + this.done_count + " complete, "
1756                     + tests.num_pending + " remain";
1757             }
1758         }
1759     };
1761     Output.prototype.show_results = function (tests, harness_status)
1762     {
1763         if (this.phase >= this.COMPLETE) {
1764             return;
1765         }
1766         if (!this.enabled)
1767         {
1768             return;
1769         }
1770         if (!this.output_node) {
1771             this.resolve_log();
1772         }
1773         this.phase = this.COMPLETE;
1775         var log = this.output_node;
1776         if (!log)
1777         {
1778             return;
1779         }
1780         var output_document = this.output_document;
1782         while (log.lastChild)
1783         {
1784             log.removeChild(log.lastChild);
1785         }
1787         if (script_prefix != null) {
1788             var stylesheet = output_document.createElementNS(xhtml_ns, "link");
1789             stylesheet.setAttribute("rel", "stylesheet");
1790             stylesheet.setAttribute("href", script_prefix + "testharness.css");
1791             var heads = output_document.getElementsByTagName("head");
1792             if (heads.length) {
1793                 heads[0].appendChild(stylesheet);
1794             }
1795         }
1797         var status_text_harness = {};
1798         status_text_harness[harness_status.OK] = "OK";
1799         status_text_harness[harness_status.ERROR] = "Error";
1800         status_text_harness[harness_status.TIMEOUT] = "Timeout";
1802         var status_text = {};
1803         status_text[Test.prototype.PASS] = "Pass";
1804         status_text[Test.prototype.FAIL] = "Fail";
1805         status_text[Test.prototype.TIMEOUT] = "Timeout";
1806         status_text[Test.prototype.NOTRUN] = "Not Run";
1808         var status_number = {};
1809         forEach(tests, function(test) {
1810                     var status = status_text[test.status];
1811                     if (status_number.hasOwnProperty(status))
1812                     {
1813                         status_number[status] += 1;
1814                     } else {
1815                         status_number[status] = 1;
1816                     }
1817                 });
1819         function status_class(status)
1820         {
1821             return status.replace(/\s/g, '').toLowerCase();
1822         }
1824         var summary_template = ["section", {"id":"summary"},
1825                                 ["h2", {}, "Summary"],
1826                                 function(vars)
1827                                 {
1828                                     if (harness_status.status === harness_status.OK)
1829                                     {
1830                                         return null;
1831                                     }
1832                                     else
1833                                     {
1834                                         var status = status_text_harness[harness_status.status];
1835                                         var rv = [["p", {"class":status_class(status)}]];
1837                                         if (harness_status.status === harness_status.ERROR)
1838                                         {
1839                                             rv[0].push("Harness encountered an error:");
1840                                             rv.push(["pre", {}, harness_status.message]);
1841                                         }
1842                                         else if (harness_status.status === harness_status.TIMEOUT)
1843                                         {
1844                                             rv[0].push("Harness timed out.");
1845                                         }
1846                                         else
1847                                         {
1848                                             rv[0].push("Harness got an unexpected status.");
1849                                         }
1851                                         return rv;
1852                                     }
1853                                 },
1854                                 ["p", {}, "Found ${num_tests} tests"],
1855                                 function(vars) {
1856                                     var rv = [["div", {}]];
1857                                     var i=0;
1858                                     while (status_text.hasOwnProperty(i)) {
1859                                         if (status_number.hasOwnProperty(status_text[i])) {
1860                                             var status = status_text[i];
1861                                             rv[0].push(["div", {"class":status_class(status)},
1862                                                         ["label", {},
1863                                                          ["input", {type:"checkbox", checked:"checked"}],
1864                                                          status_number[status] + " " + status]]);
1865                                         }
1866                                         i++;
1867                                     }
1868                                     return rv;
1869                                 }];
1871         log.appendChild(render(summary_template, {num_tests:tests.length}, output_document));
1873         forEach(output_document.querySelectorAll("section#summary label"),
1874                 function(element)
1875                 {
1876                     on_event(element, "click",
1877                              function(e)
1878                              {
1879                                  if (output_document.getElementById("results") === null)
1880                                  {
1881                                      e.preventDefault();
1882                                      return;
1883                                  }
1884                                  var result_class = element.parentNode.getAttribute("class");
1885                                  var style_element = output_document.querySelector("style#hide-" + result_class);
1886                                  var input_element = element.querySelector("input");
1887                                  if (!style_element && !input_element.checked) {
1888                                      style_element = output_document.createElementNS(xhtml_ns, "style");
1889                                      style_element.id = "hide-" + result_class;
1890                                      style_element.textContent = "table#results > tbody > tr."+result_class+"{display:none}";
1891                                      output_document.body.appendChild(style_element);
1892                                  } else if (style_element && input_element.checked) {
1893                                      style_element.parentNode.removeChild(style_element);
1894                                  }
1895                              });
1896                 });
1898         // This use of innerHTML plus manual escaping is not recommended in
1899         // general, but is necessary here for performance.  Using textContent
1900         // on each individual <td> adds tens of seconds of execution time for
1901         // large test suites (tens of thousands of tests).
1902         function escape_html(s)
1903         {
1904             return s.replace(/\&/g, "&amp;")
1905                 .replace(/</g, "&lt;")
1906                 .replace(/"/g, "&quot;")
1907                 .replace(/'/g, "&#39;");
1908         }
1910         function has_assertions()
1911         {
1912             for (var i = 0; i < tests.length; i++) {
1913                 if (tests[i].properties.hasOwnProperty("assert")) {
1914                     return true;
1915                 }
1916             }
1917             return false;
1918         }
1920         function get_assertion(test)
1921         {
1922             if (test.properties.hasOwnProperty("assert")) {
1923                 if (Array.isArray(test.properties.assert)) {
1924                     return test.properties.assert.join(' ');
1925                 }
1926                 return test.properties.assert;
1927             }
1928             return '';
1929         }
1931         log.appendChild(document.createElementNS(xhtml_ns, "section"));
1932         var assertions = has_assertions();
1933         var html = "<h2>Details</h2><table id='results' " + (assertions ? "class='assertions'" : "" ) + ">"
1934             + "<thead><tr><th>Result</th><th>Test Name</th>"
1935             + (assertions ? "<th>Assertion</th>" : "")
1936             + "<th>Message</th></tr></thead>"
1937             + "<tbody>";
1938         for (var i = 0; i < tests.length; i++) {
1939             html += '<tr class="'
1940                 + escape_html(status_class(status_text[tests[i].status]))
1941                 + '"><td>'
1942                 + escape_html(status_text[tests[i].status])
1943                 + "</td><td>"
1944                 + escape_html(tests[i].name)
1945                 + "</td><td>"
1946                 + (assertions ? escape_html(get_assertion(tests[i])) + "</td><td>" : "")
1947                 + escape_html(tests[i].message ? tests[i].message : " ")
1948                 + "</td></tr>";
1949         }
1950         html += "</tbody></table>";
1951         try {
1952             log.lastChild.innerHTML = html;
1953         } catch (e) {
1954             log.appendChild(document.createElementNS(xhtml_ns, "p"))
1955                .textContent = "Setting innerHTML for the log threw an exception.";
1956             log.appendChild(document.createElementNS(xhtml_ns, "pre"))
1957                .textContent = html;
1958         }
1959     };
1961     var output = new Output();
1962     add_start_callback(function (properties) {output.init(properties);});
1963     add_result_callback(function (test) {output.show_status(tests);});
1964     add_completion_callback(function (tests, harness_status) {output.show_results(tests, harness_status);});
1966     /*
1967      * Template code
1968      *
1969      * A template is just a javascript structure. An element is represented as:
1970      *
1971      * [tag_name, {attr_name:attr_value}, child1, child2]
1972      *
1973      * the children can either be strings (which act like text nodes), other templates or
1974      * functions (see below)
1975      *
1976      * A text node is represented as
1977      *
1978      * ["{text}", value]
1979      *
1980      * String values have a simple substitution syntax; ${foo} represents a variable foo.
1981      *
1982      * It is possible to embed logic in templates by using a function in a place where a
1983      * node would usually go. The function must either return part of a template or null.
1984      *
1985      * In cases where a set of nodes are required as output rather than a single node
1986      * with children it is possible to just use a list
1987      * [node1, node2, node3]
1988      *
1989      * Usage:
1990      *
1991      * render(template, substitutions) - take a template and an object mapping
1992      * variable names to parameters and return either a DOM node or a list of DOM nodes
1993      *
1994      * substitute(template, substitutions) - take a template and variable mapping object,
1995      * make the variable substitutions and return the substituted template
1996      *
1997      */
1999     function is_single_node(template)
2000     {
2001         return typeof template[0] === "string";
2002     }
2004     function substitute(template, substitutions)
2005     {
2006         if (typeof template === "function") {
2007             var replacement = template(substitutions);
2008             if (replacement)
2009             {
2010                 var rv = substitute(replacement, substitutions);
2011                 return rv;
2012             }
2013             else
2014             {
2015                 return null;
2016             }
2017         }
2018         else if (is_single_node(template))
2019         {
2020             return substitute_single(template, substitutions);
2021         }
2022         else
2023         {
2024             return filter(map(template, function(x) {
2025                                   return substitute(x, substitutions);
2026                               }), function(x) {return x !== null;});
2027         }
2028     }
2030     function substitute_single(template, substitutions)
2031     {
2032         var substitution_re = /\${([^ }]*)}/g;
2034         function do_substitution(input) {
2035             var components = input.split(substitution_re);
2036             var rv = [];
2037             for (var i=0; i<components.length; i+=2)
2038             {
2039                 rv.push(components[i]);
2040                 if (components[i+1])
2041                 {
2042                     rv.push(String(substitutions[components[i+1]]));
2043                 }
2044             }
2045             return rv;
2046         }
2048         var rv = [];
2049         rv.push(do_substitution(String(template[0])).join(""));
2051         if (template[0] === "{text}") {
2052             substitute_children(template.slice(1), rv);
2053         } else {
2054             substitute_attrs(template[1], rv);
2055             substitute_children(template.slice(2), rv);
2056         }
2058         function substitute_attrs(attrs, rv)
2059         {
2060             rv[1] = {};
2061             for (var name in template[1])
2062             {
2063                 if (attrs.hasOwnProperty(name))
2064                 {
2065                     var new_name = do_substitution(name).join("");
2066                     var new_value = do_substitution(attrs[name]).join("");
2067                     rv[1][new_name] = new_value;
2068                 };
2069             }
2070         }
2072         function substitute_children(children, rv)
2073         {
2074             for (var i=0; i<children.length; i++)
2075             {
2076                 if (children[i] instanceof Object) {
2077                     var replacement = substitute(children[i], substitutions);
2078                     if (replacement !== null)
2079                     {
2080                         if (is_single_node(replacement))
2081                         {
2082                             rv.push(replacement);
2083                         }
2084                         else
2085                         {
2086                             extend(rv, replacement);
2087                         }
2088                     }
2089                 }
2090                 else
2091                 {
2092                     extend(rv, do_substitution(String(children[i])));
2093                 }
2094             }
2095             return rv;
2096         }
2098         return rv;
2099     }
2101  function make_dom_single(template, doc)
2103      var output_document = doc || document;
2104      if (template[0] === "{text}")
2105      {
2106          var element = output_document.createTextNode("");
2107          for (var i=1; i<template.length; i++)
2108          {
2109              element.data += template[i];
2110          }
2111      }
2112      else
2113      {
2114          var element = output_document.createElementNS(xhtml_ns, template[0]);
2115          for (var name in template[1]) {
2116              if (template[1].hasOwnProperty(name))
2117              {
2118                  element.setAttribute(name, template[1][name]);
2119              }
2120          }
2121          for (var i=2; i<template.length; i++)
2122          {
2123              if (template[i] instanceof Object)
2124              {
2125                  var sub_element = make_dom(template[i]);
2126                  element.appendChild(sub_element);
2127              }
2128              else
2129              {
2130                  var text_node = output_document.createTextNode(template[i]);
2131                  element.appendChild(text_node);
2132              }
2133          }
2134      }
2136      return element;
2141  function make_dom(template, substitutions, output_document)
2142     {
2143         if (is_single_node(template))
2144         {
2145             return make_dom_single(template, output_document);
2146         }
2147         else
2148         {
2149             return map(template, function(x) {
2150                            return make_dom_single(x, output_document);
2151                        });
2152         }
2153     }
2155  function render(template, substitutions, output_document)
2156     {
2157         return make_dom(substitute(template, substitutions), output_document);
2158     }
2160     /*
2161      * Utility funcions
2162      */
2163     function assert(expected_true, function_name, description, error, substitutions)
2164     {
2165         if (expected_true !== true)
2166         {
2167             throw new AssertionError(make_message(function_name, description,
2168                                                   error, substitutions));
2169         }
2170     }
2172     function AssertionError(message)
2173     {
2174         this.message = message;
2175     }
2177     function make_message(function_name, description, error, substitutions)
2178     {
2179         for (var p in substitutions) {
2180             if (substitutions.hasOwnProperty(p)) {
2181                 substitutions[p] = format_value(substitutions[p]);
2182             }
2183         }
2184         var node_form = substitute(["{text}", "${function_name}: ${description}" + error],
2185                                    merge({function_name:function_name,
2186                                           description:(description?description + " ":"")},
2187                                           substitutions));
2188         return node_form.slice(1).join("");
2189     }
2191     function filter(array, callable, thisObj) {
2192         var rv = [];
2193         for (var i=0; i<array.length; i++)
2194         {
2195             if (array.hasOwnProperty(i))
2196             {
2197                 var pass = callable.call(thisObj, array[i], i, array);
2198                 if (pass) {
2199                     rv.push(array[i]);
2200                 }
2201             }
2202         }
2203         return rv;
2204     }
2206     function map(array, callable, thisObj)
2207     {
2208         var rv = [];
2209         rv.length = array.length;
2210         for (var i=0; i<array.length; i++)
2211         {
2212             if (array.hasOwnProperty(i))
2213             {
2214                 rv[i] = callable.call(thisObj, array[i], i, array);
2215             }
2216         }
2217         return rv;
2218     }
2220     function extend(array, items)
2221     {
2222         Array.prototype.push.apply(array, items);
2223     }
2225     function forEach (array, callback, thisObj)
2226     {
2227         for (var i=0; i<array.length; i++)
2228         {
2229             if (array.hasOwnProperty(i))
2230             {
2231                 callback.call(thisObj, array[i], i, array);
2232             }
2233         }
2234     }
2236     function merge(a,b)
2237     {
2238         var rv = {};
2239         var p;
2240         for (p in a)
2241         {
2242             rv[p] = a[p];
2243         }
2244         for (p in b) {
2245             rv[p] = b[p];
2246         }
2247         return rv;
2248     }
2250     function expose(object, name)
2251     {
2252         var components = name.split(".");
2253         var target = window;
2254         for (var i=0; i<components.length - 1; i++)
2255         {
2256             if (!(components[i] in target))
2257             {
2258                 target[components[i]] = {};
2259             }
2260             target = target[components[i]];
2261         }
2262         target[components[components.length - 1]] = object;
2263     }
2265     function forEach_windows(callback) {
2266         // Iterate of the the windows [self ... top, opener]. The callback is passed
2267         // two objects, the first one is the windows object itself, the second one
2268         // is a boolean indicating whether or not its on the same origin as the
2269         // current window.
2270         var cache = forEach_windows.result_cache;
2271         if (!cache) {
2272             cache = [[self, true]];
2273             var w = self;
2274             var i = 0;
2275             var so;
2276             var origins = location.ancestorOrigins;
2277             while (w != w.parent)
2278             {
2279                 w = w.parent;
2280                 // In WebKit, calls to parent windows' properties that aren't on the same
2281                 // origin cause an error message to be displayed in the error console but
2282                 // don't throw an exception. This is a deviation from the current HTML5
2283                 // spec. See: https://bugs.webkit.org/show_bug.cgi?id=43504
2284                 // The problem with WebKit's behavior is that it pollutes the error console
2285                 // with error messages that can't be caught.
2286                 //
2287                 // This issue can be mitigated by relying on the (for now) proprietary
2288                 // `location.ancestorOrigins` property which returns an ordered list of
2289                 // the origins of enclosing windows. See:
2290                 // http://trac.webkit.org/changeset/113945.
2291                 if(origins) {
2292                     so = (location.origin == origins[i]);
2293                 }
2294                 else
2295                 {
2296                     so = is_same_origin(w);
2297                 }
2298                 cache.push([w, so]);
2299                 i++;
2300             }
2301             w = window.opener;
2302             if(w)
2303             {
2304                 // window.opener isn't included in the `location.ancestorOrigins` prop.
2305                 // We'll just have to deal with a simple check and an error msg on WebKit
2306                 // browsers in this case.
2307                 cache.push([w, is_same_origin(w)]);
2308             }
2309             forEach_windows.result_cache = cache;
2310         }
2312         forEach(cache,
2313                 function(a)
2314                 {
2315                     callback.apply(null, a);
2316                 });
2317     }
2319     function is_same_origin(w) {
2320         try {
2321             'random_prop' in w;
2322             return true;
2323         } catch(e) {
2324             return false;
2325         }
2326     }
2328     function supports_post_message(w)
2329     {
2330         var supports;
2331         var type;
2332         // Given IE  implements postMessage across nested iframes but not across
2333         // windows or tabs, you can't infer cross-origin communication from the presence
2334         // of postMessage on the current window object only.
2335         //
2336         // Touching the postMessage prop on a window can throw if the window is
2337         // not from the same origin AND post message is not supported in that
2338         // browser. So just doing an existence test here won't do, you also need
2339         // to wrap it in a try..cacth block.
2340         try
2341         {
2342             type = typeof w.postMessage;
2343             if (type === "function")
2344             {
2345                 supports = true;
2346             }
2347             // IE8 supports postMessage, but implements it as a host object which
2348             // returns "object" as its `typeof`.
2349             else if (type === "object")
2350             {
2351                 supports = true;
2352             }
2353             // This is the case where postMessage isn't supported AND accessing a
2354             // window property across origins does NOT throw (e.g. old Safari browser).
2355             else
2356             {
2357                 supports = false;
2358             }
2359         }
2360         catch(e) {
2361             // This is the case where postMessage isn't supported AND accessing a
2362             // window property across origins throws (e.g. old Firefox browser).
2363             supports = false;
2364         }
2365         return supports;
2366     }
2367 })();
2368 // vim: set expandtab shiftwidth=4 tabstop=4: