michael@0: /* michael@0: Distributed under both the W3C Test Suite License [1] and the W3C michael@0: 3-clause BSD License [2]. To contribute to a W3C Test Suite, see the michael@0: policies and contribution forms [3]. michael@0: michael@0: [1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license michael@0: [2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license michael@0: [3] http://www.w3.org/2004/10/27-testcases michael@0: */ michael@0: michael@0: /* michael@0: * == Introduction == michael@0: * michael@0: * This file provides a framework for writing testcases. It is intended to michael@0: * provide a convenient API for making common assertions, and to work both michael@0: * for testing synchronous and asynchronous DOM features in a way that michael@0: * promotes clear, robust, tests. michael@0: * michael@0: * == Basic Usage == michael@0: * michael@0: * To use this file, import the script and the testharnessreport script into michael@0: * the test document: michael@0: * michael@0: * michael@0: * michael@0: * Within each file one may define one or more tests. Each test is atomic michael@0: * in the sense that a single test has a single result (pass/fail/timeout). michael@0: * Within each test one may have a number of asserts. The test fails at the michael@0: * first failing assert, and the remainder of the test is (typically) not run. michael@0: * michael@0: * If the file containing the tests is a HTML file with an element of id "log" michael@0: * this will be populated with a table containing the test results after all michael@0: * the tests have run. michael@0: * michael@0: * NOTE: By default tests must be created before the load event fires. For ways michael@0: * to create tests after the load event, see "Determining when all tests michael@0: * are complete", below michael@0: * michael@0: * == Synchronous Tests == michael@0: * michael@0: * To create a synchronous test use the test() function: michael@0: * michael@0: * test(test_function, name, properties) michael@0: * michael@0: * test_function is a function that contains the code to test. For example a michael@0: * trivial passing test would be: michael@0: * michael@0: * test(function() {assert_true(true)}, "assert_true with true") michael@0: * michael@0: * The function passed in is run in the test() call. michael@0: * michael@0: * properties is an object that overrides default test properties. The michael@0: * recognised properties are: michael@0: * timeout - the test timeout in ms michael@0: * michael@0: * e.g. michael@0: * test(test_function, "Sample test", {timeout:1000}) michael@0: * michael@0: * would run test_function with a timeout of 1s. michael@0: * michael@0: * Additionally, test-specific metadata can be passed in the properties. These michael@0: * are used when the individual test has different metadata from that stored michael@0: * in the . michael@0: * The recognized metadata properties are: michael@0: * michael@0: * help - The url of the part of the specification being tested michael@0: * michael@0: * assert - A human readable description of what the test is attempting michael@0: * to prove michael@0: * michael@0: * author - Name and contact information for the author of the test in the michael@0: * format: "Name " or "Name http://contact/url" michael@0: * michael@0: * == Asynchronous Tests == michael@0: * michael@0: * Testing asynchronous features is somewhat more complex since the result of michael@0: * a test may depend on one or more events or other callbacks. The API provided michael@0: * for testing these features is indended to be rather low-level but hopefully michael@0: * applicable to many situations. michael@0: * michael@0: * To create a test, one starts by getting a Test object using async_test: michael@0: * michael@0: * async_test(name, properties) michael@0: * michael@0: * e.g. michael@0: * var t = async_test("Simple async test") michael@0: * michael@0: * Assertions can be added to the test by calling the step method of the test michael@0: * object with a function containing the test assertions: michael@0: * michael@0: * t.step(function() {assert_true(true)}); michael@0: * michael@0: * When all the steps are complete, the done() method must be called: michael@0: * michael@0: * t.done(); michael@0: * michael@0: * As a convenience, async_test can also takes a function as first argument. michael@0: * This function is called with the test object as both its `this` object and michael@0: * first argument. The above example can be rewritten as: michael@0: * michael@0: * async_test(function(t) { michael@0: * object.some_event = function() { michael@0: * t.step(function (){assert_true(true); t.done();}); michael@0: * }; michael@0: * }, "Simple async test"); michael@0: * michael@0: * which avoids cluttering the global scope with references to async michael@0: * tests instances. michael@0: * michael@0: * The properties argument is identical to that for test(). michael@0: * michael@0: * In many cases it is convenient to run a step in response to an event or a michael@0: * callback. A convenient method of doing this is through the step_func method michael@0: * which returns a function that, when called runs a test step. For example michael@0: * michael@0: * object.some_event = t.step_func(function(e) {assert_true(e.a)}); michael@0: * michael@0: * == Making assertions == michael@0: * michael@0: * Functions for making assertions start assert_ michael@0: * The best way to get a list is to look in this file for functions names michael@0: * matching that pattern. The general signature is michael@0: * michael@0: * assert_something(actual, expected, description) michael@0: * michael@0: * although not all assertions precisely match this pattern e.g. assert_true michael@0: * only takes actual and description as arguments. michael@0: * michael@0: * The description parameter is used to present more useful error messages when michael@0: * a test fails michael@0: * michael@0: * NOTE: All asserts must be located in a test() or a step of an async_test(). michael@0: * asserts outside these places won't be detected correctly by the harness michael@0: * and may cause a file to stop testing. michael@0: * michael@0: * == Harness Timeout == michael@0: * michael@0: * The overall harness admits two timeout values "normal" (the michael@0: * default) and "long", used for tests which have an unusually long michael@0: * runtime. After the timeout is reached, the harness will stop michael@0: * waiting for further async tests to complete. By default the michael@0: * timeouts are set to 10s and 60s, respectively, but may be changed michael@0: * when the test is run on hardware with different performance michael@0: * characteristics to a common desktop computer. In order to opt-in michael@0: * to the longer test timeout, the test must specify a meta element: michael@0: * michael@0: * michael@0: * == Setup == michael@0: * michael@0: * Sometimes tests require non-trivial setup that may fail. For this purpose michael@0: * there is a setup() function, that may be called with one or two arguments. michael@0: * The two argument version is: michael@0: * michael@0: * setup(func, properties) michael@0: * michael@0: * The one argument versions may omit either argument. michael@0: * func is a function to be run synchronously. setup() becomes a no-op once michael@0: * any tests have returned results. Properties are global properties of the test michael@0: * harness. Currently recognised properties are: michael@0: * michael@0: * michael@0: * explicit_done - Wait for an explicit call to done() before declaring all michael@0: * tests complete (see below) michael@0: * michael@0: * output_document - The document to which results should be logged. By default michael@0: * this is the current document but could be an ancestor michael@0: * document in some cases e.g. a SVG test loaded in an HTML michael@0: * wrapper michael@0: * michael@0: * explicit_timeout - disable file timeout; only stop waiting for results michael@0: * when the timeout() function is called (typically for michael@0: * use when integrating with some existing test framework michael@0: * that has its own timeout mechanism). michael@0: * michael@0: * allow_uncaught_exception - don't treat an uncaught exception as an error; michael@0: * needed when e.g. testing the window.onerror michael@0: * handler. michael@0: * michael@0: * timeout_multiplier - Multiplier to apply to per-test timeouts. michael@0: * michael@0: * == Determining when all tests are complete == michael@0: * michael@0: * By default the test harness will assume there are no more results to come michael@0: * when: michael@0: * 1) There are no Test objects that have been created but not completed michael@0: * 2) The load event on the document has fired michael@0: * michael@0: * This behaviour can be overridden by setting the explicit_done property to michael@0: * true in a call to setup(). If explicit_done is true, the test harness will michael@0: * not assume it is done until the global done() function is called. Once done() michael@0: * is called, the two conditions above apply like normal. michael@0: * michael@0: * == Generating tests == michael@0: * michael@0: * NOTE: this functionality may be removed michael@0: * michael@0: * There are scenarios in which is is desirable to create a large number of michael@0: * (synchronous) tests that are internally similar but vary in the parameters michael@0: * used. To make this easier, the generate_tests function allows a single michael@0: * function to be called with each set of parameters in a list: michael@0: * michael@0: * generate_tests(test_function, parameter_lists, properties) michael@0: * michael@0: * For example: michael@0: * michael@0: * generate_tests(assert_equals, [ michael@0: * ["Sum one and one", 1+1, 2], michael@0: * ["Sum one and zero", 1+0, 1] michael@0: * ]) michael@0: * michael@0: * Is equivalent to: michael@0: * michael@0: * test(function() {assert_equals(1+1, 2)}, "Sum one and one") michael@0: * test(function() {assert_equals(1+0, 1)}, "Sum one and zero") michael@0: * michael@0: * Note that the first item in each parameter list corresponds to the name of michael@0: * the test. michael@0: * michael@0: * The properties argument is identical to that for test(). This may be a michael@0: * single object (used for all generated tests) or an array. michael@0: * michael@0: * == Callback API == michael@0: * michael@0: * The framework provides callbacks corresponding to 3 events: michael@0: * michael@0: * start - happens when the first Test is created michael@0: * result - happens when a test result is recieved michael@0: * complete - happens when all results are recieved michael@0: * michael@0: * The page defining the tests may add callbacks for these events by calling michael@0: * the following methods: michael@0: * michael@0: * add_start_callback(callback) - callback called with no arguments michael@0: * add_result_callback(callback) - callback called with a test argument michael@0: * add_completion_callback(callback) - callback called with an array of tests michael@0: * and an status object michael@0: * michael@0: * tests have the following properties: michael@0: * status: A status code. This can be compared to the PASS, FAIL, TIMEOUT and michael@0: * NOTRUN properties on the test object michael@0: * message: A message indicating the reason for failure. In the future this michael@0: * will always be a string michael@0: * michael@0: * The status object gives the overall status of the harness. It has the michael@0: * following properties: michael@0: * status: Can be compared to the OK, ERROR and TIMEOUT properties michael@0: * message: An error message set when the status is ERROR michael@0: * michael@0: * == External API == michael@0: * michael@0: * In order to collect the results of multiple pages containing tests, the test michael@0: * harness will, when loaded in a nested browsing context, attempt to call michael@0: * certain functions in each ancestor and opener browsing context: michael@0: * michael@0: * start - start_callback michael@0: * result - result_callback michael@0: * complete - completion_callback michael@0: * michael@0: * These are given the same arguments as the corresponding internal callbacks michael@0: * described above. michael@0: * michael@0: * == External API through cross-document messaging == michael@0: * michael@0: * Where supported, the test harness will also send messages using michael@0: * cross-document messaging to each ancestor and opener browsing context. Since michael@0: * it uses the wildcard keyword (*), cross-origin communication is enabled and michael@0: * script on different origins can collect the results. michael@0: * michael@0: * This API follows similar conventions as those described above only slightly michael@0: * modified to accommodate message event API. Each message is sent by the harness michael@0: * is passed a single vanilla object, available as the `data` property of the michael@0: * event object. These objects are structures as follows: michael@0: * michael@0: * start - { type: "start" } michael@0: * result - { type: "result", test: Test } michael@0: * complete - { type: "complete", tests: [Test, ...], status: TestsStatus } michael@0: * michael@0: * == List of assertions == michael@0: * michael@0: * assert_true(actual, description) michael@0: * asserts that /actual/ is strictly true michael@0: * michael@0: * assert_false(actual, description) michael@0: * asserts that /actual/ is strictly false michael@0: * michael@0: * assert_equals(actual, expected, description) michael@0: * asserts that /actual/ is the same value as /expected/ michael@0: * michael@0: * assert_not_equals(actual, expected, description) michael@0: * asserts that /actual/ is a different value to /expected/. Yes, this means michael@0: * that "expected" is a misnomer michael@0: * michael@0: * assert_in_array(actual, expected, description) michael@0: * asserts that /expected/ is an Array, and /actual/ is equal to one of the michael@0: * members -- expected.indexOf(actual) != -1 michael@0: * michael@0: * assert_array_equals(actual, expected, description) michael@0: * asserts that /actual/ and /expected/ have the same length and the value of michael@0: * each indexed property in /actual/ is the strictly equal to the corresponding michael@0: * property value in /expected/ michael@0: * michael@0: * assert_approx_equals(actual, expected, epsilon, description) michael@0: * asserts that /actual/ is a number within +/- /epsilon/ of /expected/ michael@0: * michael@0: * assert_less_than(actual, expected, description) michael@0: * asserts that /actual/ is a number less than /expected/ michael@0: * michael@0: * assert_greater_than(actual, expected, description) michael@0: * asserts that /actual/ is a number greater than /expected/ michael@0: * michael@0: * assert_less_than_equal(actual, expected, description) michael@0: * asserts that /actual/ is a number less than or equal to /expected/ michael@0: * michael@0: * assert_greater_than_equal(actual, expected, description) michael@0: * asserts that /actual/ is a number greater than or equal to /expected/ michael@0: * michael@0: * assert_regexp_match(actual, expected, description) michael@0: * asserts that /actual/ matches the regexp /expected/ michael@0: * michael@0: * assert_class_string(object, class_name, description) michael@0: * asserts that the class string of /object/ as returned in michael@0: * Object.prototype.toString is equal to /class_name/. michael@0: * michael@0: * assert_own_property(object, property_name, description) michael@0: * assert that object has own property property_name michael@0: * michael@0: * assert_inherits(object, property_name, description) michael@0: * assert that object does not have an own property named property_name michael@0: * but that property_name is present in the prototype chain for object michael@0: * michael@0: * assert_idl_attribute(object, attribute_name, description) michael@0: * assert that an object that is an instance of some interface has the michael@0: * attribute attribute_name following the conditions specified by WebIDL michael@0: * michael@0: * assert_readonly(object, property_name, description) michael@0: * assert that property property_name on object is readonly michael@0: * michael@0: * assert_throws(code, func, description) michael@0: * code - the expected exception: michael@0: * o string: the thrown exception must be a DOMException with the given michael@0: * name, e.g., "TimeoutError" (for compatibility with existing michael@0: * tests, a constant is also supported, e.g., "TIMEOUT_ERR") michael@0: * o object: the thrown exception must have a property called "name" that michael@0: * matches code.name michael@0: * o null: allow any exception (in general, one of the options above michael@0: * should be used) michael@0: * func - a function that should throw michael@0: * michael@0: * assert_unreached(description) michael@0: * asserts if called. Used to ensure that some codepath is *not* taken e.g. michael@0: * an event does not fire. michael@0: * michael@0: * assert_any(assert_func, actual, expected_array, extra_arg_1, ... extra_arg_N) michael@0: * asserts that one assert_func(actual, expected_array_N, extra_arg1, ..., extra_arg_N) michael@0: * is true for some expected_array_N in expected_array. This only works for assert_func michael@0: * with signature assert_func(actual, expected, args_1, ..., args_N). Note that tests michael@0: * with multiple allowed pass conditions are bad practice unless the spec specifically michael@0: * allows multiple behaviours. Test authors should not use this method simply to hide michael@0: * UA bugs. michael@0: * michael@0: * assert_exists(object, property_name, description) michael@0: * *** deprecated *** michael@0: * asserts that object has an own property property_name michael@0: * michael@0: * assert_not_exists(object, property_name, description) michael@0: * *** deprecated *** michael@0: * assert that object does not have own property property_name michael@0: */ michael@0: michael@0: (function () michael@0: { michael@0: var debug = false; michael@0: // default timeout is 10 seconds, test can override if needed michael@0: var settings = { michael@0: output:true, michael@0: harness_timeout:{"normal":10000, michael@0: "long":60000}, michael@0: test_timeout:null michael@0: }; michael@0: michael@0: var xhtml_ns = "http://www.w3.org/1999/xhtml"; michael@0: michael@0: // script_prefix is used by Output.prototype.show_results() to figure out michael@0: // where to get testharness.css from. It's enclosed in an extra closure to michael@0: // not pollute the library's namespace with variables like "src". michael@0: var script_prefix = null; michael@0: (function () michael@0: { michael@0: var scripts = document.getElementsByTagName("script"); michael@0: for (var i = 0; i < scripts.length; i++) michael@0: { michael@0: if (scripts[i].src) michael@0: { michael@0: var src = scripts[i].src; michael@0: } michael@0: else if (scripts[i].href) michael@0: { michael@0: //SVG case michael@0: var src = scripts[i].href.baseVal; michael@0: } michael@0: if (src && src.slice(src.length - "testharness.js".length) === "testharness.js") michael@0: { michael@0: script_prefix = src.slice(0, src.length - "testharness.js".length); michael@0: break; michael@0: } michael@0: } michael@0: })(); michael@0: michael@0: /* michael@0: * API functions michael@0: */ michael@0: michael@0: var name_counter = 0; michael@0: function next_default_name() michael@0: { michael@0: //Don't use document.title to work around an Opera bug in XHTML documents michael@0: var title = document.getElementsByTagName("title")[0]; michael@0: var prefix = (title && title.firstChild && title.firstChild.data) || "Untitled"; michael@0: var suffix = name_counter > 0 ? " " + name_counter : ""; michael@0: name_counter++; michael@0: return prefix + suffix; michael@0: } michael@0: michael@0: function test(func, name, properties) michael@0: { michael@0: var test_name = name ? name : next_default_name(); michael@0: properties = properties ? properties : {}; michael@0: var test_obj = new Test(test_name, properties); michael@0: test_obj.step(func); michael@0: if (test_obj.phase === test_obj.phases.STARTED) { michael@0: test_obj.done(); michael@0: } michael@0: } michael@0: michael@0: function async_test(func, name, properties) michael@0: { michael@0: if (typeof func !== "function") { michael@0: properties = name; michael@0: name = func; michael@0: func = null; michael@0: } michael@0: var test_name = name ? name : next_default_name(); michael@0: properties = properties ? properties : {}; michael@0: var test_obj = new Test(test_name, properties); michael@0: if (func) { michael@0: test_obj.step(func, test_obj, test_obj); michael@0: } michael@0: return test_obj; michael@0: } michael@0: michael@0: function setup(func_or_properties, maybe_properties) michael@0: { michael@0: var func = null; michael@0: var properties = {}; michael@0: if (arguments.length === 2) { michael@0: func = func_or_properties; michael@0: properties = maybe_properties; michael@0: } else if (func_or_properties instanceof Function){ michael@0: func = func_or_properties; michael@0: } else { michael@0: properties = func_or_properties; michael@0: } michael@0: tests.setup(func, properties); michael@0: output.setup(properties); michael@0: } michael@0: michael@0: function done() { michael@0: tests.end_wait(); michael@0: } michael@0: michael@0: function generate_tests(func, args, properties) { michael@0: forEach(args, function(x, i) michael@0: { michael@0: var name = x[0]; michael@0: test(function() michael@0: { michael@0: func.apply(this, x.slice(1)); michael@0: }, michael@0: name, michael@0: Array.isArray(properties) ? properties[i] : properties); michael@0: }); michael@0: } michael@0: michael@0: function on_event(object, event, callback) michael@0: { michael@0: object.addEventListener(event, callback, false); michael@0: } michael@0: michael@0: expose(test, 'test'); michael@0: expose(async_test, 'async_test'); michael@0: expose(generate_tests, 'generate_tests'); michael@0: expose(setup, 'setup'); michael@0: expose(done, 'done'); michael@0: expose(on_event, 'on_event'); michael@0: michael@0: /* michael@0: * Return a string truncated to the given length, with ... added at the end michael@0: * if it was longer. michael@0: */ michael@0: function truncate(s, len) michael@0: { michael@0: if (s.length > len) { michael@0: return s.substring(0, len - 3) + "..."; michael@0: } michael@0: return s; michael@0: } michael@0: michael@0: /* michael@0: * Return true if object is probably a Node object. michael@0: */ michael@0: function is_node(object) michael@0: { michael@0: // I use duck-typing instead of instanceof, because michael@0: // instanceof doesn't work if the node is from another window (like an michael@0: // iframe's contentWindow): michael@0: // http://www.w3.org/Bugs/Public/show_bug.cgi?id=12295 michael@0: if ("nodeType" in object michael@0: && "nodeName" in object michael@0: && "nodeValue" in object michael@0: && "childNodes" in object) michael@0: { michael@0: try michael@0: { michael@0: object.nodeType; michael@0: } michael@0: catch (e) michael@0: { michael@0: // The object is probably Node.prototype or another prototype michael@0: // object that inherits from it, and not a Node instance. michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: /* michael@0: * Convert a value to a nice, human-readable string michael@0: */ michael@0: function format_value(val, seen) michael@0: { michael@0: if (!seen) { michael@0: seen = []; michael@0: } michael@0: if (typeof val === "object" && val !== null) michael@0: { michael@0: if (seen.indexOf(val) >= 0) michael@0: { michael@0: return "[...]"; michael@0: } michael@0: seen.push(val); michael@0: } michael@0: if (Array.isArray(val)) michael@0: { michael@0: return "[" + val.map(function(x) {return format_value(x, seen)}).join(", ") + "]"; michael@0: } michael@0: michael@0: switch (typeof val) michael@0: { michael@0: case "string": michael@0: val = val.replace("\\", "\\\\"); michael@0: for (var i = 0; i < 32; i++) michael@0: { michael@0: var replace = "\\"; michael@0: switch (i) { michael@0: case 0: replace += "0"; break; michael@0: case 1: replace += "x01"; break; michael@0: case 2: replace += "x02"; break; michael@0: case 3: replace += "x03"; break; michael@0: case 4: replace += "x04"; break; michael@0: case 5: replace += "x05"; break; michael@0: case 6: replace += "x06"; break; michael@0: case 7: replace += "x07"; break; michael@0: case 8: replace += "b"; break; michael@0: case 9: replace += "t"; break; michael@0: case 10: replace += "n"; break; michael@0: case 11: replace += "v"; break; michael@0: case 12: replace += "f"; break; michael@0: case 13: replace += "r"; break; michael@0: case 14: replace += "x0e"; break; michael@0: case 15: replace += "x0f"; break; michael@0: case 16: replace += "x10"; break; michael@0: case 17: replace += "x11"; break; michael@0: case 18: replace += "x12"; break; michael@0: case 19: replace += "x13"; break; michael@0: case 20: replace += "x14"; break; michael@0: case 21: replace += "x15"; break; michael@0: case 22: replace += "x16"; break; michael@0: case 23: replace += "x17"; break; michael@0: case 24: replace += "x18"; break; michael@0: case 25: replace += "x19"; break; michael@0: case 26: replace += "x1a"; break; michael@0: case 27: replace += "x1b"; break; michael@0: case 28: replace += "x1c"; break; michael@0: case 29: replace += "x1d"; break; michael@0: case 30: replace += "x1e"; break; michael@0: case 31: replace += "x1f"; break; michael@0: } michael@0: val = val.replace(RegExp(String.fromCharCode(i), "g"), replace); michael@0: } michael@0: return '"' + val.replace(/"/g, '\\"') + '"'; michael@0: case "boolean": michael@0: case "undefined": michael@0: return String(val); michael@0: case "number": michael@0: // In JavaScript, -0 === 0 and String(-0) == "0", so we have to michael@0: // special-case. michael@0: if (val === -0 && 1/val === -Infinity) michael@0: { michael@0: return "-0"; michael@0: } michael@0: return String(val); michael@0: case "object": michael@0: if (val === null) michael@0: { michael@0: return "null"; michael@0: } michael@0: michael@0: // Special-case Node objects, since those come up a lot in my tests. I michael@0: // ignore namespaces. michael@0: if (is_node(val)) michael@0: { michael@0: switch (val.nodeType) michael@0: { michael@0: case Node.ELEMENT_NODE: michael@0: var ret = "<" + val.tagName.toLowerCase(); michael@0: for (var i = 0; i < val.attributes.length; i++) michael@0: { michael@0: ret += " " + val.attributes[i].name + '="' + val.attributes[i].value + '"'; michael@0: } michael@0: ret += ">" + val.innerHTML + ""; michael@0: return "Element node " + truncate(ret, 60); michael@0: case Node.TEXT_NODE: michael@0: return 'Text node "' + truncate(val.data, 60) + '"'; michael@0: case Node.PROCESSING_INSTRUCTION_NODE: michael@0: return "ProcessingInstruction node with target " + format_value(truncate(val.target, 60)) + " and data " + format_value(truncate(val.data, 60)); michael@0: case Node.COMMENT_NODE: michael@0: return "Comment node "; michael@0: case Node.DOCUMENT_NODE: michael@0: return "Document node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children"); michael@0: case Node.DOCUMENT_TYPE_NODE: michael@0: return "DocumentType node"; michael@0: case Node.DOCUMENT_FRAGMENT_NODE: michael@0: return "DocumentFragment node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children"); michael@0: default: michael@0: return "Node object of unknown type"; michael@0: } michael@0: } michael@0: michael@0: // Fall through to default michael@0: default: michael@0: return typeof val + ' "' + truncate(String(val), 60) + '"'; michael@0: } michael@0: } michael@0: expose(format_value, "format_value"); michael@0: michael@0: /* michael@0: * Assertions michael@0: */ michael@0: michael@0: function assert_true(actual, description) michael@0: { michael@0: assert(actual === true, "assert_true", description, michael@0: "expected true got ${actual}", {actual:actual}); michael@0: }; michael@0: expose(assert_true, "assert_true"); michael@0: michael@0: function assert_false(actual, description) michael@0: { michael@0: assert(actual === false, "assert_false", description, michael@0: "expected false got ${actual}", {actual:actual}); michael@0: }; michael@0: expose(assert_false, "assert_false"); michael@0: michael@0: function same_value(x, y) { michael@0: if (y !== y) michael@0: { michael@0: //NaN case michael@0: return x !== x; michael@0: } michael@0: else if (x === 0 && y === 0) { michael@0: //Distinguish +0 and -0 michael@0: return 1/x === 1/y; michael@0: } michael@0: else michael@0: { michael@0: //typical case michael@0: return x === y; michael@0: } michael@0: } michael@0: michael@0: function assert_equals(actual, expected, description) michael@0: { michael@0: /* michael@0: * Test if two primitives are equal or two objects michael@0: * are the same object michael@0: */ michael@0: if (typeof actual != typeof expected) michael@0: { michael@0: assert(false, "assert_equals", description, michael@0: "expected (" + typeof expected + ") ${expected} but got (" + typeof actual + ") ${actual}", michael@0: {expected:expected, actual:actual}); michael@0: return; michael@0: } michael@0: assert(same_value(actual, expected), "assert_equals", description, michael@0: "expected ${expected} but got ${actual}", michael@0: {expected:expected, actual:actual}); michael@0: }; michael@0: expose(assert_equals, "assert_equals"); michael@0: michael@0: function assert_not_equals(actual, expected, description) michael@0: { michael@0: /* michael@0: * Test if two primitives are unequal or two objects michael@0: * are different objects michael@0: */ michael@0: assert(!same_value(actual, expected), "assert_not_equals", description, michael@0: "got disallowed value ${actual}", michael@0: {actual:actual}); michael@0: }; michael@0: expose(assert_not_equals, "assert_not_equals"); michael@0: michael@0: function assert_in_array(actual, expected, description) michael@0: { michael@0: assert(expected.indexOf(actual) != -1, "assert_in_array", description, michael@0: "value ${actual} not in array ${expected}", michael@0: {actual:actual, expected:expected}); michael@0: } michael@0: expose(assert_in_array, "assert_in_array"); michael@0: michael@0: function assert_object_equals(actual, expected, description) michael@0: { michael@0: //This needs to be improved a great deal michael@0: function check_equal(actual, expected, stack) michael@0: { michael@0: stack.push(actual); michael@0: michael@0: var p; michael@0: for (p in actual) michael@0: { michael@0: assert(expected.hasOwnProperty(p), "assert_object_equals", description, michael@0: "unexpected property ${p}", {p:p}); michael@0: michael@0: if (typeof actual[p] === "object" && actual[p] !== null) michael@0: { michael@0: if (stack.indexOf(actual[p]) === -1) michael@0: { michael@0: check_equal(actual[p], expected[p], stack); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: assert(same_value(actual[p], expected[p]), "assert_object_equals", description, michael@0: "property ${p} expected ${expected} got ${actual}", michael@0: {p:p, expected:expected, actual:actual}); michael@0: } michael@0: } michael@0: for (p in expected) michael@0: { michael@0: assert(actual.hasOwnProperty(p), michael@0: "assert_object_equals", description, michael@0: "expected property ${p} missing", {p:p}); michael@0: } michael@0: stack.pop(); michael@0: } michael@0: check_equal(actual, expected, []); michael@0: }; michael@0: expose(assert_object_equals, "assert_object_equals"); michael@0: michael@0: function assert_array_equals(actual, expected, description) michael@0: { michael@0: assert(actual.length === expected.length, michael@0: "assert_array_equals", description, michael@0: "lengths differ, expected ${expected} got ${actual}", michael@0: {expected:expected.length, actual:actual.length}); michael@0: michael@0: for (var i=0; i < actual.length; i++) michael@0: { michael@0: assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i), michael@0: "assert_array_equals", description, michael@0: "property ${i}, property expected to be $expected but was $actual", michael@0: {i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing", michael@0: actual:actual.hasOwnProperty(i) ? "present" : "missing"}); michael@0: assert(same_value(expected[i], actual[i]), michael@0: "assert_array_equals", description, michael@0: "property ${i}, expected ${expected} but got ${actual}", michael@0: {i:i, expected:expected[i], actual:actual[i]}); michael@0: } michael@0: } michael@0: expose(assert_array_equals, "assert_array_equals"); michael@0: michael@0: function assert_approx_equals(actual, expected, epsilon, description) michael@0: { michael@0: /* michael@0: * Test if two primitive numbers are equal withing +/- epsilon michael@0: */ michael@0: assert(typeof actual === "number", michael@0: "assert_approx_equals", description, michael@0: "expected a number but got a ${type_actual}", michael@0: {type_actual:typeof actual}); michael@0: michael@0: assert(Math.abs(actual - expected) <= epsilon, michael@0: "assert_approx_equals", description, michael@0: "expected ${expected} +/- ${epsilon} but got ${actual}", michael@0: {expected:expected, actual:actual, epsilon:epsilon}); michael@0: }; michael@0: expose(assert_approx_equals, "assert_approx_equals"); michael@0: michael@0: function assert_less_than(actual, expected, description) michael@0: { michael@0: /* michael@0: * Test if a primitive number is less than another michael@0: */ michael@0: assert(typeof actual === "number", michael@0: "assert_less_than", description, michael@0: "expected a number but got a ${type_actual}", michael@0: {type_actual:typeof actual}); michael@0: michael@0: assert(actual < expected, michael@0: "assert_less_than", description, michael@0: "expected a number less than ${expected} but got ${actual}", michael@0: {expected:expected, actual:actual}); michael@0: }; michael@0: expose(assert_less_than, "assert_less_than"); michael@0: michael@0: function assert_greater_than(actual, expected, description) michael@0: { michael@0: /* michael@0: * Test if a primitive number is greater than another michael@0: */ michael@0: assert(typeof actual === "number", michael@0: "assert_greater_than", description, michael@0: "expected a number but got a ${type_actual}", michael@0: {type_actual:typeof actual}); michael@0: michael@0: assert(actual > expected, michael@0: "assert_greater_than", description, michael@0: "expected a number greater than ${expected} but got ${actual}", michael@0: {expected:expected, actual:actual}); michael@0: }; michael@0: expose(assert_greater_than, "assert_greater_than"); michael@0: michael@0: function assert_less_than_equal(actual, expected, description) michael@0: { michael@0: /* michael@0: * Test if a primitive number is less than or equal to another michael@0: */ michael@0: assert(typeof actual === "number", michael@0: "assert_less_than_equal", description, michael@0: "expected a number but got a ${type_actual}", michael@0: {type_actual:typeof actual}); michael@0: michael@0: assert(actual <= expected, michael@0: "assert_less_than", description, michael@0: "expected a number less than or equal to ${expected} but got ${actual}", michael@0: {expected:expected, actual:actual}); michael@0: }; michael@0: expose(assert_less_than_equal, "assert_less_than_equal"); michael@0: michael@0: function assert_greater_than_equal(actual, expected, description) michael@0: { michael@0: /* michael@0: * Test if a primitive number is greater than or equal to another michael@0: */ michael@0: assert(typeof actual === "number", michael@0: "assert_greater_than_equal", description, michael@0: "expected a number but got a ${type_actual}", michael@0: {type_actual:typeof actual}); michael@0: michael@0: assert(actual >= expected, michael@0: "assert_greater_than_equal", description, michael@0: "expected a number greater than or equal to ${expected} but got ${actual}", michael@0: {expected:expected, actual:actual}); michael@0: }; michael@0: expose(assert_greater_than_equal, "assert_greater_than_equal"); michael@0: michael@0: function assert_regexp_match(actual, expected, description) { michael@0: /* michael@0: * Test if a string (actual) matches a regexp (expected) michael@0: */ michael@0: assert(expected.test(actual), michael@0: "assert_regexp_match", description, michael@0: "expected ${expected} but got ${actual}", michael@0: {expected:expected, actual:actual}); michael@0: } michael@0: expose(assert_regexp_match, "assert_regexp_match"); michael@0: michael@0: function assert_class_string(object, class_string, description) { michael@0: assert_equals({}.toString.call(object), "[object " + class_string + "]", michael@0: description); michael@0: } michael@0: expose(assert_class_string, "assert_class_string"); michael@0: michael@0: michael@0: function _assert_own_property(name) { michael@0: return function(object, property_name, description) michael@0: { michael@0: assert(object.hasOwnProperty(property_name), michael@0: name, description, michael@0: "expected property ${p} missing", {p:property_name}); michael@0: }; michael@0: } michael@0: expose(_assert_own_property("assert_exists"), "assert_exists"); michael@0: expose(_assert_own_property("assert_own_property"), "assert_own_property"); michael@0: michael@0: function assert_not_exists(object, property_name, description) michael@0: { michael@0: assert(!object.hasOwnProperty(property_name), michael@0: "assert_not_exists", description, michael@0: "unexpected property ${p} found", {p:property_name}); michael@0: }; michael@0: expose(assert_not_exists, "assert_not_exists"); michael@0: michael@0: function _assert_inherits(name) { michael@0: return function (object, property_name, description) michael@0: { michael@0: assert(typeof object === "object", michael@0: name, description, michael@0: "provided value is not an object"); michael@0: michael@0: assert("hasOwnProperty" in object, michael@0: name, description, michael@0: "provided value is an object but has no hasOwnProperty method"); michael@0: michael@0: assert(!object.hasOwnProperty(property_name), michael@0: name, description, michael@0: "property ${p} found on object expected in prototype chain", michael@0: {p:property_name}); michael@0: michael@0: assert(property_name in object, michael@0: name, description, michael@0: "property ${p} not found in prototype chain", michael@0: {p:property_name}); michael@0: }; michael@0: } michael@0: expose(_assert_inherits("assert_inherits"), "assert_inherits"); michael@0: expose(_assert_inherits("assert_idl_attribute"), "assert_idl_attribute"); michael@0: michael@0: function assert_readonly(object, property_name, description) michael@0: { michael@0: var initial_value = object[property_name]; michael@0: try { michael@0: //Note that this can have side effects in the case where michael@0: //the property has PutForwards michael@0: object[property_name] = initial_value + "a"; //XXX use some other value here? michael@0: assert(same_value(object[property_name], initial_value), michael@0: "assert_readonly", description, michael@0: "changing property ${p} succeeded", michael@0: {p:property_name}); michael@0: } michael@0: finally michael@0: { michael@0: object[property_name] = initial_value; michael@0: } michael@0: }; michael@0: expose(assert_readonly, "assert_readonly"); michael@0: michael@0: function assert_throws(code, func, description) michael@0: { michael@0: try michael@0: { michael@0: func.call(this); michael@0: assert(false, "assert_throws", description, michael@0: "${func} did not throw", {func:func}); michael@0: } michael@0: catch(e) michael@0: { michael@0: if (e instanceof AssertionError) { michael@0: throw(e); michael@0: } michael@0: if (code === null) michael@0: { michael@0: return; michael@0: } michael@0: if (typeof code === "object") michael@0: { michael@0: assert(typeof e == "object" && "name" in e && e.name == code.name, michael@0: "assert_throws", description, michael@0: "${func} threw ${actual} (${actual_name}) expected ${expected} (${expected_name})", michael@0: {func:func, actual:e, actual_name:e.name, michael@0: expected:code, michael@0: expected_name:code.name}); michael@0: return; michael@0: } michael@0: michael@0: var code_name_map = { michael@0: INDEX_SIZE_ERR: 'IndexSizeError', michael@0: HIERARCHY_REQUEST_ERR: 'HierarchyRequestError', michael@0: WRONG_DOCUMENT_ERR: 'WrongDocumentError', michael@0: INVALID_CHARACTER_ERR: 'InvalidCharacterError', michael@0: NO_MODIFICATION_ALLOWED_ERR: 'NoModificationAllowedError', michael@0: NOT_FOUND_ERR: 'NotFoundError', michael@0: NOT_SUPPORTED_ERR: 'NotSupportedError', michael@0: INVALID_STATE_ERR: 'InvalidStateError', michael@0: SYNTAX_ERR: 'SyntaxError', michael@0: INVALID_MODIFICATION_ERR: 'InvalidModificationError', michael@0: NAMESPACE_ERR: 'NamespaceError', michael@0: INVALID_ACCESS_ERR: 'InvalidAccessError', michael@0: TYPE_MISMATCH_ERR: 'TypeMismatchError', michael@0: SECURITY_ERR: 'SecurityError', michael@0: NETWORK_ERR: 'NetworkError', michael@0: ABORT_ERR: 'AbortError', michael@0: URL_MISMATCH_ERR: 'URLMismatchError', michael@0: QUOTA_EXCEEDED_ERR: 'QuotaExceededError', michael@0: TIMEOUT_ERR: 'TimeoutError', michael@0: INVALID_NODE_TYPE_ERR: 'InvalidNodeTypeError', michael@0: DATA_CLONE_ERR: 'DataCloneError' michael@0: }; michael@0: michael@0: var name = code in code_name_map ? code_name_map[code] : code; michael@0: michael@0: var name_code_map = { michael@0: IndexSizeError: 1, michael@0: HierarchyRequestError: 3, michael@0: WrongDocumentError: 4, michael@0: InvalidCharacterError: 5, michael@0: NoModificationAllowedError: 7, michael@0: NotFoundError: 8, michael@0: NotSupportedError: 9, michael@0: InvalidStateError: 11, michael@0: SyntaxError: 12, michael@0: InvalidModificationError: 13, michael@0: NamespaceError: 14, michael@0: InvalidAccessError: 15, michael@0: TypeMismatchError: 17, michael@0: SecurityError: 18, michael@0: NetworkError: 19, michael@0: AbortError: 20, michael@0: URLMismatchError: 21, michael@0: QuotaExceededError: 22, michael@0: TimeoutError: 23, michael@0: InvalidNodeTypeError: 24, michael@0: DataCloneError: 25, michael@0: michael@0: UnknownError: 0, michael@0: ConstraintError: 0, michael@0: DataError: 0, michael@0: TransactionInactiveError: 0, michael@0: ReadOnlyError: 0, michael@0: VersionError: 0 michael@0: }; michael@0: michael@0: if (!(name in name_code_map)) michael@0: { michael@0: throw new AssertionError('Test bug: unrecognized DOMException code "' + code + '" passed to assert_throws()'); michael@0: } michael@0: michael@0: var required_props = { code: name_code_map[name] }; michael@0: michael@0: if (required_props.code === 0 michael@0: || ("name" in e && e.name !== e.name.toUpperCase() && e.name !== "DOMException")) michael@0: { michael@0: // New style exception: also test the name property. michael@0: required_props.name = name; michael@0: } michael@0: michael@0: //We'd like to test that e instanceof the appropriate interface, michael@0: //but we can't, because we don't know what window it was created michael@0: //in. It might be an instanceof the appropriate interface on some michael@0: //unknown other window. TODO: Work around this somehow? michael@0: michael@0: assert(typeof e == "object", michael@0: "assert_throws", description, michael@0: "${func} threw ${e} with type ${type}, not an object", michael@0: {func:func, e:e, type:typeof e}); michael@0: michael@0: for (var prop in required_props) michael@0: { michael@0: assert(typeof e == "object" && prop in e && e[prop] == required_props[prop], michael@0: "assert_throws", description, michael@0: "${func} threw ${e} that is not a DOMException " + code + ": property ${prop} is equal to ${actual}, expected ${expected}", michael@0: {func:func, e:e, prop:prop, actual:e[prop], expected:required_props[prop]}); michael@0: } michael@0: } michael@0: } michael@0: expose(assert_throws, "assert_throws"); michael@0: michael@0: function assert_unreached(description) { michael@0: assert(false, "assert_unreached", description, michael@0: "Reached unreachable code"); michael@0: } michael@0: expose(assert_unreached, "assert_unreached"); michael@0: michael@0: function assert_any(assert_func, actual, expected_array) michael@0: { michael@0: var args = [].slice.call(arguments, 3) michael@0: var errors = [] michael@0: var passed = false; michael@0: forEach(expected_array, michael@0: function(expected) michael@0: { michael@0: try { michael@0: assert_func.apply(this, [actual, expected].concat(args)) michael@0: passed = true; michael@0: } catch(e) { michael@0: errors.push(e.message); michael@0: } michael@0: }); michael@0: if (!passed) { michael@0: throw new AssertionError(errors.join("\n\n")); michael@0: } michael@0: } michael@0: expose(assert_any, "assert_any"); michael@0: michael@0: function Test(name, properties) michael@0: { michael@0: this.name = name; michael@0: michael@0: this.phases = { michael@0: INITIAL:0, michael@0: STARTED:1, michael@0: HAS_RESULT:2, michael@0: COMPLETE:3 michael@0: }; michael@0: this.phase = this.phases.INITIAL; michael@0: michael@0: this.status = this.NOTRUN; michael@0: this.timeout_id = null; michael@0: michael@0: this.properties = properties; michael@0: var timeout = properties.timeout ? properties.timeout : settings.test_timeout michael@0: if (timeout != null) { michael@0: this.timeout_length = timeout * tests.timeout_multiplier; michael@0: } else { michael@0: this.timeout_length = null; michael@0: } michael@0: michael@0: this.message = null; michael@0: michael@0: var this_obj = this; michael@0: this.steps = []; michael@0: michael@0: tests.push(this); michael@0: } michael@0: michael@0: Test.statuses = { michael@0: PASS:0, michael@0: FAIL:1, michael@0: TIMEOUT:2, michael@0: NOTRUN:3 michael@0: }; michael@0: michael@0: Test.prototype = merge({}, Test.statuses); michael@0: michael@0: Test.prototype.structured_clone = function() michael@0: { michael@0: if(!this._structured_clone) michael@0: { michael@0: var msg = this.message; michael@0: msg = msg ? String(msg) : msg; michael@0: this._structured_clone = merge({ michael@0: name:String(this.name), michael@0: status:this.status, michael@0: message:msg michael@0: }, Test.statuses); michael@0: } michael@0: return this._structured_clone; michael@0: }; michael@0: michael@0: Test.prototype.step = function(func, this_obj) michael@0: { michael@0: if (this.phase > this.phases.STARTED) michael@0: { michael@0: return; michael@0: } michael@0: this.phase = this.phases.STARTED; michael@0: //If we don't get a result before the harness times out that will be a test timout michael@0: this.set_status(this.TIMEOUT, "Test timed out"); michael@0: michael@0: tests.started = true; michael@0: michael@0: if (this.timeout_id === null) michael@0: { michael@0: this.set_timeout(); michael@0: } michael@0: michael@0: this.steps.push(func); michael@0: michael@0: if (arguments.length === 1) michael@0: { michael@0: this_obj = this; michael@0: } michael@0: michael@0: try michael@0: { michael@0: return func.apply(this_obj, Array.prototype.slice.call(arguments, 2)); michael@0: } michael@0: catch(e) michael@0: { michael@0: if (this.phase >= this.phases.HAS_RESULT) michael@0: { michael@0: return; michael@0: } michael@0: var message = (typeof e === "object" && e !== null) ? e.message : e; michael@0: if (typeof e.stack != "undefined" && typeof e.message == "string") { michael@0: //Try to make it more informative for some exceptions, at least michael@0: //in Gecko and WebKit. This results in a stack dump instead of michael@0: //just errors like "Cannot read property 'parentNode' of null" michael@0: //or "root is null". Makes it a lot longer, of course. michael@0: message += "(stack: " + e.stack + ")"; michael@0: } michael@0: this.set_status(this.FAIL, message); michael@0: this.phase = this.phases.HAS_RESULT; michael@0: this.done(); michael@0: } michael@0: }; michael@0: michael@0: Test.prototype.step_func = function(func, this_obj) michael@0: { michael@0: var test_this = this; michael@0: michael@0: if (arguments.length === 1) michael@0: { michael@0: this_obj = test_this; michael@0: } michael@0: michael@0: return function() michael@0: { michael@0: test_this.step.apply(test_this, [func, this_obj].concat( michael@0: Array.prototype.slice.call(arguments))); michael@0: }; michael@0: }; michael@0: michael@0: Test.prototype.step_func_done = function(func, this_obj) michael@0: { michael@0: var test_this = this; michael@0: michael@0: if (arguments.length === 1) michael@0: { michael@0: this_obj = test_this; michael@0: } michael@0: michael@0: return function() michael@0: { michael@0: test_this.step.apply(test_this, [func, this_obj].concat( michael@0: Array.prototype.slice.call(arguments))); michael@0: test_this.done(); michael@0: }; michael@0: } michael@0: michael@0: Test.prototype.set_timeout = function() michael@0: { michael@0: if (this.timeout_length !== null) michael@0: { michael@0: var this_obj = this; michael@0: this.timeout_id = setTimeout(function() michael@0: { michael@0: this_obj.timeout(); michael@0: }, this.timeout_length); michael@0: } michael@0: } michael@0: michael@0: Test.prototype.set_status = function(status, message) michael@0: { michael@0: this.status = status; michael@0: this.message = message; michael@0: } michael@0: michael@0: Test.prototype.timeout = function() michael@0: { michael@0: this.timeout_id = null; michael@0: this.set_status(this.TIMEOUT, "Test timed out") michael@0: this.phase = this.phases.HAS_RESULT; michael@0: this.done(); michael@0: }; michael@0: michael@0: Test.prototype.done = function() michael@0: { michael@0: if (this.phase == this.phases.COMPLETE) { michael@0: return; michael@0: } else if (this.phase <= this.phases.STARTED) michael@0: { michael@0: this.set_status(this.PASS, null); michael@0: } michael@0: michael@0: if (this.status == this.NOTRUN) michael@0: { michael@0: alert(this.phase); michael@0: } michael@0: michael@0: this.phase = this.phases.COMPLETE; michael@0: michael@0: clearTimeout(this.timeout_id); michael@0: tests.result(this); michael@0: }; michael@0: michael@0: michael@0: /* michael@0: * Harness michael@0: */ michael@0: michael@0: function TestsStatus() michael@0: { michael@0: this.status = null; michael@0: this.message = null; michael@0: } michael@0: michael@0: TestsStatus.statuses = { michael@0: OK:0, michael@0: ERROR:1, michael@0: TIMEOUT:2 michael@0: }; michael@0: michael@0: TestsStatus.prototype = merge({}, TestsStatus.statuses); michael@0: michael@0: TestsStatus.prototype.structured_clone = function() michael@0: { michael@0: if(!this._structured_clone) michael@0: { michael@0: var msg = this.message; michael@0: msg = msg ? String(msg) : msg; michael@0: this._structured_clone = merge({ michael@0: status:this.status, michael@0: message:msg michael@0: }, TestsStatus.statuses); michael@0: } michael@0: return this._structured_clone; michael@0: }; michael@0: michael@0: function Tests() michael@0: { michael@0: this.tests = []; michael@0: this.num_pending = 0; michael@0: michael@0: this.phases = { michael@0: INITIAL:0, michael@0: SETUP:1, michael@0: HAVE_TESTS:2, michael@0: HAVE_RESULTS:3, michael@0: COMPLETE:4 michael@0: }; michael@0: this.phase = this.phases.INITIAL; michael@0: michael@0: this.properties = {}; michael@0: michael@0: //All tests can't be done until the load event fires michael@0: this.all_loaded = false; michael@0: this.wait_for_finish = false; michael@0: this.processing_callbacks = false; michael@0: michael@0: this.allow_uncaught_exception = false; michael@0: michael@0: this.timeout_multiplier = 1; michael@0: this.timeout_length = this.get_timeout(); michael@0: this.timeout_id = null; michael@0: michael@0: this.start_callbacks = []; michael@0: this.test_done_callbacks = []; michael@0: this.all_done_callbacks = []; michael@0: michael@0: this.status = new TestsStatus(); michael@0: michael@0: var this_obj = this; michael@0: michael@0: on_event(window, "load", michael@0: function() michael@0: { michael@0: this_obj.all_loaded = true; michael@0: if (this_obj.all_done()) michael@0: { michael@0: this_obj.complete(); michael@0: } michael@0: }); michael@0: michael@0: this.set_timeout(); michael@0: } michael@0: michael@0: Tests.prototype.setup = function(func, properties) michael@0: { michael@0: if (this.phase >= this.phases.HAVE_RESULTS) michael@0: { michael@0: return; michael@0: } michael@0: if (this.phase < this.phases.SETUP) michael@0: { michael@0: this.phase = this.phases.SETUP; michael@0: } michael@0: michael@0: this.properties = properties; michael@0: michael@0: for (var p in properties) michael@0: { michael@0: if (properties.hasOwnProperty(p)) michael@0: { michael@0: var value = properties[p] michael@0: if (p == "allow_uncaught_exception") { michael@0: this.allow_uncaught_exception = value; michael@0: } michael@0: else if (p == "explicit_done" && value) michael@0: { michael@0: this.wait_for_finish = true; michael@0: } michael@0: else if (p == "explicit_timeout" && value) { michael@0: this.timeout_length = null; michael@0: if (this.timeout_id) michael@0: { michael@0: clearTimeout(this.timeout_id); michael@0: } michael@0: } michael@0: else if (p == "timeout_multiplier") michael@0: { michael@0: this.timeout_multiplier = value; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (func) michael@0: { michael@0: try michael@0: { michael@0: func(); michael@0: } catch(e) michael@0: { michael@0: this.status.status = this.status.ERROR; michael@0: this.status.message = e; michael@0: }; michael@0: } michael@0: this.set_timeout(); michael@0: }; michael@0: michael@0: Tests.prototype.get_timeout = function() michael@0: { michael@0: var metas = document.getElementsByTagName("meta"); michael@0: for (var i=0; i this.phases.HAVE_RESULTS) michael@0: { michael@0: return; michael@0: } michael@0: this.phase = this.phases.HAVE_RESULTS; michael@0: this.num_pending--; michael@0: this.notify_result(test); michael@0: }; michael@0: michael@0: Tests.prototype.notify_result = function(test) { michael@0: var this_obj = this; michael@0: this.processing_callbacks = true; michael@0: forEach(this.test_done_callbacks, michael@0: function(callback) michael@0: { michael@0: callback(test, this_obj); michael@0: }); michael@0: michael@0: forEach_windows( michael@0: function(w, is_same_origin) michael@0: { michael@0: if(is_same_origin && w.result_callback) michael@0: { michael@0: try michael@0: { michael@0: w.result_callback(test); michael@0: } michael@0: catch(e) michael@0: { michael@0: if(debug) { michael@0: throw e; michael@0: } michael@0: } michael@0: } michael@0: if (supports_post_message(w) && w !== self) michael@0: { michael@0: w.postMessage({ michael@0: type: "result", michael@0: test: test.structured_clone() michael@0: }, "*"); michael@0: } michael@0: }); michael@0: this.processing_callbacks = false; michael@0: if (this_obj.all_done()) michael@0: { michael@0: this_obj.complete(); michael@0: } michael@0: }; michael@0: michael@0: Tests.prototype.complete = function() { michael@0: if (this.phase === this.phases.COMPLETE) { michael@0: return; michael@0: } michael@0: this.phase = this.phases.COMPLETE; michael@0: var this_obj = this; michael@0: this.tests.forEach( michael@0: function(x) michael@0: { michael@0: if(x.status === x.NOTRUN) michael@0: { michael@0: this_obj.notify_result(x); michael@0: } michael@0: } michael@0: ); michael@0: this.notify_complete(); michael@0: }; michael@0: michael@0: Tests.prototype.notify_complete = function() michael@0: { michael@0: clearTimeout(this.timeout_id); michael@0: var this_obj = this; michael@0: var tests = map(this_obj.tests, michael@0: function(test) michael@0: { michael@0: return test.structured_clone(); michael@0: }); michael@0: if (this.status.status === null) michael@0: { michael@0: this.status.status = this.status.OK; michael@0: } michael@0: michael@0: forEach (this.all_done_callbacks, michael@0: function(callback) michael@0: { michael@0: callback(this_obj.tests, this_obj.status); michael@0: }); michael@0: michael@0: forEach_windows( michael@0: function(w, is_same_origin) michael@0: { michael@0: if(is_same_origin && w.completion_callback) michael@0: { michael@0: try michael@0: { michael@0: w.completion_callback(this_obj.tests, this_obj.status); michael@0: } michael@0: catch(e) michael@0: { michael@0: if (debug) michael@0: { michael@0: throw e; michael@0: } michael@0: } michael@0: } michael@0: if (supports_post_message(w) && w !== self) michael@0: { michael@0: w.postMessage({ michael@0: type: "complete", michael@0: tests: tests, michael@0: status: this_obj.status.structured_clone() michael@0: }, "*"); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: var tests = new Tests(); michael@0: michael@0: window.onerror = function(msg) { michael@0: if (!tests.allow_uncaught_exception) michael@0: { michael@0: tests.status.status = tests.status.ERROR; michael@0: tests.status.message = msg; michael@0: tests.complete(); michael@0: } michael@0: } michael@0: michael@0: function timeout() { michael@0: if (tests.timeout_length === null) michael@0: { michael@0: tests.timeout(); michael@0: } michael@0: } michael@0: expose(timeout, 'timeout'); michael@0: michael@0: function add_start_callback(callback) { michael@0: tests.start_callbacks.push(callback); michael@0: } michael@0: michael@0: function add_result_callback(callback) michael@0: { michael@0: tests.test_done_callbacks.push(callback); michael@0: } michael@0: michael@0: function add_completion_callback(callback) michael@0: { michael@0: tests.all_done_callbacks.push(callback); michael@0: } michael@0: michael@0: expose(add_start_callback, 'add_start_callback'); michael@0: expose(add_result_callback, 'add_result_callback'); michael@0: expose(add_completion_callback, 'add_completion_callback'); michael@0: michael@0: /* michael@0: * Output listener michael@0: */ michael@0: michael@0: function Output() { michael@0: this.output_document = document; michael@0: this.output_node = null; michael@0: this.done_count = 0; michael@0: this.enabled = settings.output; michael@0: this.phase = this.INITIAL; michael@0: } michael@0: michael@0: Output.prototype.INITIAL = 0; michael@0: Output.prototype.STARTED = 1; michael@0: Output.prototype.HAVE_RESULTS = 2; michael@0: Output.prototype.COMPLETE = 3; michael@0: michael@0: Output.prototype.setup = function(properties) { michael@0: if (this.phase > this.INITIAL) { michael@0: return; michael@0: } michael@0: michael@0: //If output is disabled in testharnessreport.js the test shouldn't be michael@0: //able to override that michael@0: this.enabled = this.enabled && (properties.hasOwnProperty("output") ? michael@0: properties.output : settings.output); michael@0: }; michael@0: michael@0: Output.prototype.init = function(properties) michael@0: { michael@0: if (this.phase >= this.STARTED) { michael@0: return; michael@0: } michael@0: if (properties.output_document) { michael@0: this.output_document = properties.output_document; michael@0: } else { michael@0: this.output_document = document; michael@0: } michael@0: this.phase = this.STARTED; michael@0: }; michael@0: michael@0: Output.prototype.resolve_log = function() michael@0: { michael@0: var output_document; michael@0: if (typeof this.output_document === "function") michael@0: { michael@0: output_document = this.output_document.apply(undefined); michael@0: } else michael@0: { michael@0: output_document = this.output_document; michael@0: } michael@0: if (!output_document) michael@0: { michael@0: return; michael@0: } michael@0: var node = output_document.getElementById("log"); michael@0: if (node) michael@0: { michael@0: this.output_document = output_document; michael@0: this.output_node = node; michael@0: } michael@0: }; michael@0: michael@0: Output.prototype.show_status = function(test) michael@0: { michael@0: if (this.phase < this.STARTED) michael@0: { michael@0: this.init(); michael@0: } michael@0: if (!this.enabled) michael@0: { michael@0: return; michael@0: } michael@0: if (this.phase < this.HAVE_RESULTS) michael@0: { michael@0: this.resolve_log(); michael@0: this.phase = this.HAVE_RESULTS; michael@0: } michael@0: this.done_count++; michael@0: if (this.output_node) michael@0: { michael@0: if (this.done_count < 100 michael@0: || (this.done_count < 1000 && this.done_count % 100 == 0) michael@0: || this.done_count % 1000 == 0) { michael@0: this.output_node.textContent = "Running, " michael@0: + this.done_count + " complete, " michael@0: + tests.num_pending + " remain"; michael@0: } michael@0: } michael@0: }; michael@0: michael@0: Output.prototype.show_results = function (tests, harness_status) michael@0: { michael@0: if (this.phase >= this.COMPLETE) { michael@0: return; michael@0: } michael@0: if (!this.enabled) michael@0: { michael@0: return; michael@0: } michael@0: if (!this.output_node) { michael@0: this.resolve_log(); michael@0: } michael@0: this.phase = this.COMPLETE; michael@0: michael@0: var log = this.output_node; michael@0: if (!log) michael@0: { michael@0: return; michael@0: } michael@0: var output_document = this.output_document; michael@0: michael@0: while (log.lastChild) michael@0: { michael@0: log.removeChild(log.lastChild); michael@0: } michael@0: michael@0: if (script_prefix != null) { michael@0: var stylesheet = output_document.createElementNS(xhtml_ns, "link"); michael@0: stylesheet.setAttribute("rel", "stylesheet"); michael@0: stylesheet.setAttribute("href", script_prefix + "testharness.css"); michael@0: var heads = output_document.getElementsByTagName("head"); michael@0: if (heads.length) { michael@0: heads[0].appendChild(stylesheet); michael@0: } michael@0: } michael@0: michael@0: var status_text_harness = {}; michael@0: status_text_harness[harness_status.OK] = "OK"; michael@0: status_text_harness[harness_status.ERROR] = "Error"; michael@0: status_text_harness[harness_status.TIMEOUT] = "Timeout"; michael@0: michael@0: var status_text = {}; michael@0: status_text[Test.prototype.PASS] = "Pass"; michael@0: status_text[Test.prototype.FAIL] = "Fail"; michael@0: status_text[Test.prototype.TIMEOUT] = "Timeout"; michael@0: status_text[Test.prototype.NOTRUN] = "Not Run"; michael@0: michael@0: var status_number = {}; michael@0: forEach(tests, function(test) { michael@0: var status = status_text[test.status]; michael@0: if (status_number.hasOwnProperty(status)) michael@0: { michael@0: status_number[status] += 1; michael@0: } else { michael@0: status_number[status] = 1; michael@0: } michael@0: }); michael@0: michael@0: function status_class(status) michael@0: { michael@0: return status.replace(/\s/g, '').toLowerCase(); michael@0: } michael@0: michael@0: var summary_template = ["section", {"id":"summary"}, michael@0: ["h2", {}, "Summary"], michael@0: function(vars) michael@0: { michael@0: if (harness_status.status === harness_status.OK) michael@0: { michael@0: return null; michael@0: } michael@0: else michael@0: { michael@0: var status = status_text_harness[harness_status.status]; michael@0: var rv = [["p", {"class":status_class(status)}]]; michael@0: michael@0: if (harness_status.status === harness_status.ERROR) michael@0: { michael@0: rv[0].push("Harness encountered an error:"); michael@0: rv.push(["pre", {}, harness_status.message]); michael@0: } michael@0: else if (harness_status.status === harness_status.TIMEOUT) michael@0: { michael@0: rv[0].push("Harness timed out."); michael@0: } michael@0: else michael@0: { michael@0: rv[0].push("Harness got an unexpected status."); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: }, michael@0: ["p", {}, "Found ${num_tests} tests"], michael@0: function(vars) { michael@0: var rv = [["div", {}]]; michael@0: var i=0; michael@0: while (status_text.hasOwnProperty(i)) { michael@0: if (status_number.hasOwnProperty(status_text[i])) { michael@0: var status = status_text[i]; michael@0: rv[0].push(["div", {"class":status_class(status)}, michael@0: ["label", {}, michael@0: ["input", {type:"checkbox", checked:"checked"}], michael@0: status_number[status] + " " + status]]); michael@0: } michael@0: i++; michael@0: } michael@0: return rv; michael@0: }]; michael@0: michael@0: log.appendChild(render(summary_template, {num_tests:tests.length}, output_document)); michael@0: michael@0: forEach(output_document.querySelectorAll("section#summary label"), michael@0: function(element) michael@0: { michael@0: on_event(element, "click", michael@0: function(e) michael@0: { michael@0: if (output_document.getElementById("results") === null) michael@0: { michael@0: e.preventDefault(); michael@0: return; michael@0: } michael@0: var result_class = element.parentNode.getAttribute("class"); michael@0: var style_element = output_document.querySelector("style#hide-" + result_class); michael@0: var input_element = element.querySelector("input"); michael@0: if (!style_element && !input_element.checked) { michael@0: style_element = output_document.createElementNS(xhtml_ns, "style"); michael@0: style_element.id = "hide-" + result_class; michael@0: style_element.textContent = "table#results > tbody > tr."+result_class+"{display:none}"; michael@0: output_document.body.appendChild(style_element); michael@0: } else if (style_element && input_element.checked) { michael@0: style_element.parentNode.removeChild(style_element); michael@0: } michael@0: }); michael@0: }); michael@0: michael@0: // This use of innerHTML plus manual escaping is not recommended in michael@0: // general, but is necessary here for performance. Using textContent michael@0: // on each individual adds tens of seconds of execution time for michael@0: // large test suites (tens of thousands of tests). michael@0: function escape_html(s) michael@0: { michael@0: return s.replace(/\&/g, "&") michael@0: .replace(/" michael@0: + "ResultTest Name" michael@0: + (assertions ? "Assertion" : "") michael@0: + "Message" michael@0: + ""; michael@0: for (var i = 0; i < tests.length; i++) { michael@0: html += '' michael@0: + escape_html(status_text[tests[i].status]) michael@0: + "" michael@0: + escape_html(tests[i].name) michael@0: + "" michael@0: + (assertions ? escape_html(get_assertion(tests[i])) + "" : "") michael@0: + escape_html(tests[i].message ? tests[i].message : " ") michael@0: + ""; michael@0: } michael@0: html += ""; michael@0: try { michael@0: log.lastChild.innerHTML = html; michael@0: } catch (e) { michael@0: log.appendChild(document.createElementNS(xhtml_ns, "p")) michael@0: .textContent = "Setting innerHTML for the log threw an exception."; michael@0: log.appendChild(document.createElementNS(xhtml_ns, "pre")) michael@0: .textContent = html; michael@0: } michael@0: }; michael@0: michael@0: var output = new Output(); michael@0: add_start_callback(function (properties) {output.init(properties);}); michael@0: add_result_callback(function (test) {output.show_status(tests);}); michael@0: add_completion_callback(function (tests, harness_status) {output.show_results(tests, harness_status);}); michael@0: michael@0: /* michael@0: * Template code michael@0: * michael@0: * A template is just a javascript structure. An element is represented as: michael@0: * michael@0: * [tag_name, {attr_name:attr_value}, child1, child2] michael@0: * michael@0: * the children can either be strings (which act like text nodes), other templates or michael@0: * functions (see below) michael@0: * michael@0: * A text node is represented as michael@0: * michael@0: * ["{text}", value] michael@0: * michael@0: * String values have a simple substitution syntax; ${foo} represents a variable foo. michael@0: * michael@0: * It is possible to embed logic in templates by using a function in a place where a michael@0: * node would usually go. The function must either return part of a template or null. michael@0: * michael@0: * In cases where a set of nodes are required as output rather than a single node michael@0: * with children it is possible to just use a list michael@0: * [node1, node2, node3] michael@0: * michael@0: * Usage: michael@0: * michael@0: * render(template, substitutions) - take a template and an object mapping michael@0: * variable names to parameters and return either a DOM node or a list of DOM nodes michael@0: * michael@0: * substitute(template, substitutions) - take a template and variable mapping object, michael@0: * make the variable substitutions and return the substituted template michael@0: * michael@0: */ michael@0: michael@0: function is_single_node(template) michael@0: { michael@0: return typeof template[0] === "string"; michael@0: } michael@0: michael@0: function substitute(template, substitutions) michael@0: { michael@0: if (typeof template === "function") { michael@0: var replacement = template(substitutions); michael@0: if (replacement) michael@0: { michael@0: var rv = substitute(replacement, substitutions); michael@0: return rv; michael@0: } michael@0: else michael@0: { michael@0: return null; michael@0: } michael@0: } michael@0: else if (is_single_node(template)) michael@0: { michael@0: return substitute_single(template, substitutions); michael@0: } michael@0: else michael@0: { michael@0: return filter(map(template, function(x) { michael@0: return substitute(x, substitutions); michael@0: }), function(x) {return x !== null;}); michael@0: } michael@0: } michael@0: michael@0: function substitute_single(template, substitutions) michael@0: { michael@0: var substitution_re = /\${([^ }]*)}/g; michael@0: michael@0: function do_substitution(input) { michael@0: var components = input.split(substitution_re); michael@0: var rv = []; michael@0: for (var i=0; i