UNPKG

@webcomponents/custom-elements

Version:
1,308 lines (1,187 loc) 95 kB
/*global self*/ /*jshint latedef: nofunc*/ /* Distributed under both the W3C Test Suite License [1] and the W3C 3-clause BSD License [2]. To contribute to a W3C Test Suite, see the policies and contribution forms [3]. [1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license [2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license [3] http://www.w3.org/2004/10/27-testcases */ /* Documentation is in docs/api.md */ (function () { var debug = false; // default timeout is 10 seconds, test can override if needed var settings = { output:true, harness_timeout:{ "normal":10000, "long":60000 }, test_timeout:null, message_events: ["start", "test_state", "result", "completion"] }; var xhtml_ns = "http://www.w3.org/1999/xhtml"; /* * TestEnvironment is an abstraction for the environment in which the test * harness is used. Each implementation of a test environment has to provide * the following interface: * * interface TestEnvironment { * // Invoked after the global 'tests' object has been created and it's * // safe to call add_*_callback() to register event handlers. * void on_tests_ready(); * * // Invoked after setup() has been called to notify the test environment * // of changes to the test harness properties. * void on_new_harness_properties(object properties); * * // Should return a new unique default test name. * DOMString next_default_test_name(); * * // Should return the test harness timeout duration in milliseconds. * float test_timeout(); * * // Should return the global scope object. * object global_scope(); * }; */ /* * A test environment with a DOM. The global object is 'window'. By default * test results are displayed in a table. Any parent windows receive * callbacks or messages via postMessage() when test events occur. See * apisample11.html and apisample12.html. */ function WindowTestEnvironment() { this.name_counter = 0; this.window_cache = null; this.output_handler = null; this.all_loaded = false; var this_obj = this; this.message_events = []; this.message_functions = { start: [add_start_callback, remove_start_callback, function (properties) { this_obj._dispatch("start_callback", [properties], {type: "start", properties: properties}); }], test_state: [add_test_state_callback, remove_test_state_callback, function(test) { this_obj._dispatch("test_state_callback", [test], {type: "test_state", test: test.structured_clone()}); }], result: [add_result_callback, remove_result_callback, function (test) { this_obj.output_handler.show_status(); this_obj._dispatch("result_callback", [test], {type: "result", test: test.structured_clone()}); }], completion: [add_completion_callback, remove_completion_callback, function (tests, harness_status) { var cloned_tests = map(tests, function(test) { return test.structured_clone(); }); this_obj._dispatch("completion_callback", [tests, harness_status], {type: "complete", tests: cloned_tests, status: harness_status.structured_clone()}); }] } on_event(window, 'load', function() { this_obj.all_loaded = true; }); } WindowTestEnvironment.prototype._dispatch = function(selector, callback_args, message_arg) { this._forEach_windows( function(w, same_origin) { if (same_origin) { try { var has_selector = selector in w; } catch(e) { // If document.domain was set at some point same_origin can be // wrong and the above will fail. has_selector = false; } if (has_selector) { try { w[selector].apply(undefined, callback_args); } catch (e) { if (debug) { throw e; } } } } if (supports_post_message(w) && w !== self) { w.postMessage(message_arg, "*"); } }); }; WindowTestEnvironment.prototype._forEach_windows = function(callback) { // Iterate of the the windows [self ... top, opener]. The callback is passed // two objects, the first one is the windows object itself, the second one // is a boolean indicating whether or not its on the same origin as the // current window. var cache = this.window_cache; if (!cache) { cache = [[self, true]]; var w = self; var i = 0; var so; var origins = location.ancestorOrigins; while (w != w.parent) { w = w.parent; // In WebKit, calls to parent windows' properties that aren't on the same // origin cause an error message to be displayed in the error console but // don't throw an exception. This is a deviation from the current HTML5 // spec. See: https://bugs.webkit.org/show_bug.cgi?id=43504 // The problem with WebKit's behavior is that it pollutes the error console // with error messages that can't be caught. // // This issue can be mitigated by relying on the (for now) proprietary // `location.ancestorOrigins` property which returns an ordered list of // the origins of enclosing windows. See: // http://trac.webkit.org/changeset/113945. if (origins) { so = (location.origin == origins[i]); } else { so = is_same_origin(w); } cache.push([w, so]); i++; } w = window.opener; if (w) { // window.opener isn't included in the `location.ancestorOrigins` prop. // We'll just have to deal with a simple check and an error msg on WebKit // browsers in this case. cache.push([w, is_same_origin(w)]); } this.window_cache = cache; } forEach(cache, function(a) { callback.apply(null, a); }); }; WindowTestEnvironment.prototype.on_tests_ready = function() { var output = new Output(); this.output_handler = output; var this_obj = this; add_start_callback(function (properties) { this_obj.output_handler.init(properties); }); add_test_state_callback(function(test) { this_obj.output_handler.show_status(); }); add_result_callback(function (test) { this_obj.output_handler.show_status(); }); add_completion_callback(function (tests, harness_status) { this_obj.output_handler.show_results(tests, harness_status); }); this.setup_messages(settings.message_events); }; WindowTestEnvironment.prototype.setup_messages = function(new_events) { var this_obj = this; forEach(settings.message_events, function(x) { var current_dispatch = this_obj.message_events.indexOf(x) !== -1; var new_dispatch = new_events.indexOf(x) !== -1; if (!current_dispatch && new_dispatch) { this_obj.message_functions[x][0](this_obj.message_functions[x][2]); } else if (current_dispatch && !new_dispatch) { this_obj.message_functions[x][1](this_obj.message_functions[x][2]); } }); this.message_events = new_events; } WindowTestEnvironment.prototype.next_default_test_name = function() { //Don't use document.title to work around an Opera bug in XHTML documents var title = document.getElementsByTagName("title")[0]; var prefix = (title && title.firstChild && title.firstChild.data) || "Untitled"; var suffix = this.name_counter > 0 ? " " + this.name_counter : ""; this.name_counter++; return prefix + suffix; }; WindowTestEnvironment.prototype.on_new_harness_properties = function(properties) { this.output_handler.setup(properties); if (properties.hasOwnProperty("message_events")) { this.setup_messages(properties.message_events); } }; WindowTestEnvironment.prototype.add_on_loaded_callback = function(callback) { on_event(window, 'load', callback); }; WindowTestEnvironment.prototype.test_timeout = function() { var metas = document.getElementsByTagName("meta"); for (var i = 0; i < metas.length; i++) { if (metas[i].name == "timeout") { if (metas[i].content == "long") { return settings.harness_timeout.long; } break; } } return settings.harness_timeout.normal; }; WindowTestEnvironment.prototype.global_scope = function() { return window; }; /* * Base TestEnvironment implementation for a generic web worker. * * Workers accumulate test results. One or more clients can connect and * retrieve results from a worker at any time. * * WorkerTestEnvironment supports communicating with a client via a * MessagePort. The mechanism for determining the appropriate MessagePort * for communicating with a client depends on the type of worker and is * implemented by the various specializations of WorkerTestEnvironment * below. * * A client document using testharness can use fetch_tests_from_worker() to * retrieve results from a worker. See apisample16.html. */ function WorkerTestEnvironment() { this.name_counter = 0; this.all_loaded = true; this.message_list = []; this.message_ports = []; } WorkerTestEnvironment.prototype._dispatch = function(message) { this.message_list.push(message); for (var i = 0; i < this.message_ports.length; ++i) { this.message_ports[i].postMessage(message); } }; // The only requirement is that port has a postMessage() method. It doesn't // have to be an instance of a MessagePort, and often isn't. WorkerTestEnvironment.prototype._add_message_port = function(port) { this.message_ports.push(port); for (var i = 0; i < this.message_list.length; ++i) { port.postMessage(this.message_list[i]); } }; WorkerTestEnvironment.prototype.next_default_test_name = function() { var suffix = this.name_counter > 0 ? " " + this.name_counter : ""; this.name_counter++; return "Untitled" + suffix; }; WorkerTestEnvironment.prototype.on_new_harness_properties = function() {}; WorkerTestEnvironment.prototype.on_tests_ready = function() { var this_obj = this; add_start_callback( function(properties) { this_obj._dispatch({ type: "start", properties: properties, }); }); add_test_state_callback( function(test) { this_obj._dispatch({ type: "test_state", test: test.structured_clone() }); }); add_result_callback( function(test) { this_obj._dispatch({ type: "result", test: test.structured_clone() }); }); add_completion_callback( function(tests, harness_status) { this_obj._dispatch({ type: "complete", tests: map(tests, function(test) { return test.structured_clone(); }), status: harness_status.structured_clone() }); }); }; WorkerTestEnvironment.prototype.add_on_loaded_callback = function() {}; WorkerTestEnvironment.prototype.test_timeout = function() { // Tests running in a worker don't have a default timeout. I.e. all // worker tests behave as if settings.explicit_timeout is true. return null; }; WorkerTestEnvironment.prototype.global_scope = function() { return self; }; /* * Dedicated web workers. * https://html.spec.whatwg.org/multipage/workers.html#dedicatedworkerglobalscope * * This class is used as the test_environment when testharness is running * inside a dedicated worker. */ function DedicatedWorkerTestEnvironment() { WorkerTestEnvironment.call(this); // self is an instance of DedicatedWorkerGlobalScope which exposes // a postMessage() method for communicating via the message channel // established when the worker is created. this._add_message_port(self); } DedicatedWorkerTestEnvironment.prototype = Object.create(WorkerTestEnvironment.prototype); DedicatedWorkerTestEnvironment.prototype.on_tests_ready = function() { WorkerTestEnvironment.prototype.on_tests_ready.call(this); // In the absence of an onload notification, we a require dedicated // workers to explicitly signal when the tests are done. tests.wait_for_finish = true; }; /* * Shared web workers. * https://html.spec.whatwg.org/multipage/workers.html#sharedworkerglobalscope * * This class is used as the test_environment when testharness is running * inside a shared web worker. */ function SharedWorkerTestEnvironment() { WorkerTestEnvironment.call(this); var this_obj = this; // Shared workers receive message ports via the 'onconnect' event for // each connection. self.addEventListener("connect", function(message_event) { this_obj._add_message_port(message_event.source); }); } SharedWorkerTestEnvironment.prototype = Object.create(WorkerTestEnvironment.prototype); SharedWorkerTestEnvironment.prototype.on_tests_ready = function() { WorkerTestEnvironment.prototype.on_tests_ready.call(this); // In the absence of an onload notification, we a require shared // workers to explicitly signal when the tests are done. tests.wait_for_finish = true; }; /* * Service workers. * http://www.w3.org/TR/service-workers/ * * This class is used as the test_environment when testharness is running * inside a service worker. */ function ServiceWorkerTestEnvironment() { WorkerTestEnvironment.call(this); this.all_loaded = false; this.on_loaded_callback = null; var this_obj = this; self.addEventListener("message", function(event) { if (event.data.type && event.data.type === "connect") { if (event.ports && event.ports[0]) { // If a MessageChannel was passed, then use it to // send results back to the main window. This // allows the tests to work even if the browser // does not fully support MessageEvent.source in // ServiceWorkers yet. this_obj._add_message_port(event.ports[0]); event.ports[0].start(); } else { // If there is no MessageChannel, then attempt to // use the MessageEvent.source to send results // back to the main window. this_obj._add_message_port(event.source); } } }); // The oninstall event is received after the service worker script and // all imported scripts have been fetched and executed. It's the // equivalent of an onload event for a document. All tests should have // been added by the time this event is received, thus it's not // necessary to wait until the onactivate event. on_event(self, "install", function(event) { this_obj.all_loaded = true; if (this_obj.on_loaded_callback) { this_obj.on_loaded_callback(); } }); } ServiceWorkerTestEnvironment.prototype = Object.create(WorkerTestEnvironment.prototype); ServiceWorkerTestEnvironment.prototype.add_on_loaded_callback = function(callback) { if (this.all_loaded) { callback(); } else { this.on_loaded_callback = callback; } }; function create_test_environment() { if ('document' in self) { return new WindowTestEnvironment(); } if ('DedicatedWorkerGlobalScope' in self && self instanceof DedicatedWorkerGlobalScope) { return new DedicatedWorkerTestEnvironment(); } if ('SharedWorkerGlobalScope' in self && self instanceof SharedWorkerGlobalScope) { return new SharedWorkerTestEnvironment(); } if ('ServiceWorkerGlobalScope' in self && self instanceof ServiceWorkerGlobalScope) { return new ServiceWorkerTestEnvironment(); } if ('WorkerGlobalScope' in self && self instanceof WorkerGlobalScope) { return new DedicatedWorkerTestEnvironment(); } throw new Error("Unsupported test environment"); } var test_environment = create_test_environment(); function is_shared_worker(worker) { return 'SharedWorker' in self && worker instanceof SharedWorker; } function is_service_worker(worker) { return 'ServiceWorker' in self && worker instanceof ServiceWorker; } /* * API functions */ function test(func, name, properties) { var test_name = name ? name : test_environment.next_default_test_name(); properties = properties ? properties : {}; var test_obj = new Test(test_name, properties); test_obj.step(func, test_obj, test_obj); if (test_obj.phase === test_obj.phases.STARTED) { test_obj.done(); } } function async_test(func, name, properties) { if (typeof func !== "function") { properties = name; name = func; func = null; } var test_name = name ? name : test_environment.next_default_test_name(); properties = properties ? properties : {}; var test_obj = new Test(test_name, properties); if (func) { test_obj.step(func, test_obj, test_obj); } return test_obj; } function promise_test(func, name, properties) { var test = async_test(name, properties); // If there is no promise tests queue make one. test.step(function() { if (!tests.promise_tests) { tests.promise_tests = Promise.resolve(); } }); tests.promise_tests = tests.promise_tests.then(function() { return Promise.resolve(test.step(func, test, test)) .then( function() { test.done(); }) .catch(test.step_func( function(value) { if (value instanceof AssertionError) { throw value; } assert(false, "promise_test", null, "Unhandled rejection with value: ${value}", {value:value}); })); }); } function promise_rejects(test, expected, promise, description) { return promise.then(test.unreached_func("Should have rejected: " + description)).catch(function(e) { assert_throws(expected, function() { throw e }, description); }); } /** * This constructor helper allows DOM events to be handled using Promises, * which can make it a lot easier to test a very specific series of events, * including ensuring that unexpected events are not fired at any point. */ function EventWatcher(test, watchedNode, eventTypes) { if (typeof eventTypes == 'string') { eventTypes = [eventTypes]; } var waitingFor = null; var eventHandler = test.step_func(function(evt) { assert_true(!!waitingFor, 'Not expecting event, but got ' + evt.type + ' event'); assert_equals(evt.type, waitingFor.types[0], 'Expected ' + waitingFor.types[0] + ' event, but got ' + evt.type + ' event instead'); if (waitingFor.types.length > 1) { // Pop first event from array waitingFor.types.shift(); return; } // We need to null out waitingFor before calling the resolve function // since the Promise's resolve handlers may call wait_for() which will // need to set waitingFor. var resolveFunc = waitingFor.resolve; waitingFor = null; resolveFunc(evt); }); for (var i = 0; i < eventTypes.length; i++) { watchedNode.addEventListener(eventTypes[i], eventHandler); } /** * Returns a Promise that will resolve after the specified event or * series of events has occured. */ this.wait_for = function(types) { if (waitingFor) { return Promise.reject('Already waiting for an event or events'); } if (typeof types == 'string') { types = [types]; } return new Promise(function(resolve, reject) { waitingFor = { types: types, resolve: resolve, reject: reject }; }); }; function stop_watching() { for (var i = 0; i < eventTypes.length; i++) { watchedNode.removeEventListener(eventTypes[i], eventHandler); } }; test.add_cleanup(stop_watching); return this; } expose(EventWatcher, 'EventWatcher'); function setup(func_or_properties, maybe_properties) { var func = null; var properties = {}; if (arguments.length === 2) { func = func_or_properties; properties = maybe_properties; } else if (func_or_properties instanceof Function) { func = func_or_properties; } else { properties = func_or_properties; } tests.setup(func, properties); test_environment.on_new_harness_properties(properties); } function done() { if (tests.tests.length === 0) { tests.set_file_is_test(); } if (tests.file_is_test) { tests.tests[0].done(); } tests.end_wait(); } function generate_tests(func, args, properties) { forEach(args, function(x, i) { var name = x[0]; test(function() { func.apply(this, x.slice(1)); }, name, Array.isArray(properties) ? properties[i] : properties); }); } function on_event(object, event, callback) { object.addEventListener(event, callback, false); } function step_timeout(f, t) { var outer_this = this; var args = Array.prototype.slice.call(arguments, 2); return setTimeout(function() { f.apply(outer_this, args); }, t * tests.timeout_multiplier); } expose(test, 'test'); expose(async_test, 'async_test'); expose(promise_test, 'promise_test'); expose(promise_rejects, 'promise_rejects'); expose(generate_tests, 'generate_tests'); expose(setup, 'setup'); expose(done, 'done'); expose(on_event, 'on_event'); expose(step_timeout, 'step_timeout'); /* * Return a string truncated to the given length, with ... added at the end * if it was longer. */ function truncate(s, len) { if (s.length > len) { return s.substring(0, len - 3) + "..."; } return s; } /* * Return true if object is probably a Node object. */ function is_node(object) { // I use duck-typing instead of instanceof, because // instanceof doesn't work if the node is from another window (like an // iframe's contentWindow): // http://www.w3.org/Bugs/Public/show_bug.cgi?id=12295 try { var has_node_properties = ("nodeType" in object && "nodeName" in object && "nodeValue" in object && "childNodes" in object); } catch (e) { // We're probably cross-origin, which means we aren't a node return false; } if (has_node_properties) { try { object.nodeType; } catch (e) { // The object is probably Node.prototype or another prototype // object that inherits from it, and not a Node instance. return false; } return true; } return false; } var replacements = { "0": "0", "1": "x01", "2": "x02", "3": "x03", "4": "x04", "5": "x05", "6": "x06", "7": "x07", "8": "b", "9": "t", "10": "n", "11": "v", "12": "f", "13": "r", "14": "x0e", "15": "x0f", "16": "x10", "17": "x11", "18": "x12", "19": "x13", "20": "x14", "21": "x15", "22": "x16", "23": "x17", "24": "x18", "25": "x19", "26": "x1a", "27": "x1b", "28": "x1c", "29": "x1d", "30": "x1e", "31": "x1f", "0xfffd": "ufffd", "0xfffe": "ufffe", "0xffff": "uffff", }; /* * Convert a value to a nice, human-readable string */ function format_value(val, seen) { if (!seen) { seen = []; } if (typeof val === "object" && val !== null) { if (seen.indexOf(val) >= 0) { return "[...]"; } seen.push(val); } if (Array.isArray(val)) { return "[" + val.map(function(x) {return format_value(x, seen);}).join(", ") + "]"; } switch (typeof val) { case "string": val = val.replace("\\", "\\\\"); for (var p in replacements) { var replace = "\\" + replacements[p]; val = val.replace(RegExp(String.fromCharCode(p), "g"), replace); } return '"' + val.replace(/"/g, '\\"') + '"'; case "boolean": case "undefined": return String(val); case "number": // In JavaScript, -0 === 0 and String(-0) == "0", so we have to // special-case. if (val === -0 && 1/val === -Infinity) { return "-0"; } return String(val); case "object": if (val === null) { return "null"; } // Special-case Node objects, since those come up a lot in my tests. I // ignore namespaces. if (is_node(val)) { switch (val.nodeType) { case Node.ELEMENT_NODE: var ret = "<" + val.localName; for (var i = 0; i < val.attributes.length; i++) { ret += " " + val.attributes[i].name + '="' + val.attributes[i].value + '"'; } ret += ">" + val.innerHTML + "</" + val.localName + ">"; return "Element node " + truncate(ret, 60); case Node.TEXT_NODE: return 'Text node "' + truncate(val.data, 60) + '"'; case Node.PROCESSING_INSTRUCTION_NODE: return "ProcessingInstruction node with target " + format_value(truncate(val.target, 60)) + " and data " + format_value(truncate(val.data, 60)); case Node.COMMENT_NODE: return "Comment node <!--" + truncate(val.data, 60) + "-->"; case Node.DOCUMENT_NODE: return "Document node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children"); case Node.DOCUMENT_TYPE_NODE: return "DocumentType node"; case Node.DOCUMENT_FRAGMENT_NODE: return "DocumentFragment node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children"); default: return "Node object of unknown type"; } } /* falls through */ default: try { return typeof val + ' "' + truncate(String(val), 1000) + '"'; } catch(e) { return ("[stringifying object threw " + String(e) + " with type " + String(typeof e) + "]"); } } } expose(format_value, "format_value"); /* * Assertions */ function assert_true(actual, description) { assert(actual === true, "assert_true", description, "expected true got ${actual}", {actual:actual}); } expose(assert_true, "assert_true"); function assert_false(actual, description) { assert(actual === false, "assert_false", description, "expected false got ${actual}", {actual:actual}); } expose(assert_false, "assert_false"); function same_value(x, y) { if (y !== y) { //NaN case return x !== x; } if (x === 0 && y === 0) { //Distinguish +0 and -0 return 1/x === 1/y; } return x === y; } function assert_equals(actual, expected, description) { /* * Test if two primitives are equal or two objects * are the same object */ if (typeof actual != typeof expected) { assert(false, "assert_equals", description, "expected (" + typeof expected + ") ${expected} but got (" + typeof actual + ") ${actual}", {expected:expected, actual:actual}); return; } assert(same_value(actual, expected), "assert_equals", description, "expected ${expected} but got ${actual}", {expected:expected, actual:actual}); } expose(assert_equals, "assert_equals"); function assert_not_equals(actual, expected, description) { /* * Test if two primitives are unequal or two objects * are different objects */ assert(!same_value(actual, expected), "assert_not_equals", description, "got disallowed value ${actual}", {actual:actual}); } expose(assert_not_equals, "assert_not_equals"); function assert_in_array(actual, expected, description) { assert(expected.indexOf(actual) != -1, "assert_in_array", description, "value ${actual} not in array ${expected}", {actual:actual, expected:expected}); } expose(assert_in_array, "assert_in_array"); function assert_object_equals(actual, expected, description) { //This needs to be improved a great deal function check_equal(actual, expected, stack) { stack.push(actual); var p; for (p in actual) { assert(expected.hasOwnProperty(p), "assert_object_equals", description, "unexpected property ${p}", {p:p}); if (typeof actual[p] === "object" && actual[p] !== null) { if (stack.indexOf(actual[p]) === -1) { check_equal(actual[p], expected[p], stack); } } else { assert(same_value(actual[p], expected[p]), "assert_object_equals", description, "property ${p} expected ${expected} got ${actual}", {p:p, expected:expected, actual:actual}); } } for (p in expected) { assert(actual.hasOwnProperty(p), "assert_object_equals", description, "expected property ${p} missing", {p:p}); } stack.pop(); } check_equal(actual, expected, []); } expose(assert_object_equals, "assert_object_equals"); function assert_array_equals(actual, expected, description) { assert(actual.length === expected.length, "assert_array_equals", description, "lengths differ, expected ${expected} got ${actual}", {expected:expected.length, actual:actual.length}); for (var i = 0; i < actual.length; i++) { assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i), "assert_array_equals", description, "property ${i}, property expected to be ${expected} but was ${actual}", {i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing", actual:actual.hasOwnProperty(i) ? "present" : "missing"}); assert(same_value(expected[i], actual[i]), "assert_array_equals", description, "property ${i}, expected ${expected} but got ${actual}", {i:i, expected:expected[i], actual:actual[i]}); } } expose(assert_array_equals, "assert_array_equals"); function assert_approx_equals(actual, expected, epsilon, description) { /* * Test if two primitive numbers are equal withing +/- epsilon */ assert(typeof actual === "number", "assert_approx_equals", description, "expected a number but got a ${type_actual}", {type_actual:typeof actual}); assert(Math.abs(actual - expected) <= epsilon, "assert_approx_equals", description, "expected ${expected} +/- ${epsilon} but got ${actual}", {expected:expected, actual:actual, epsilon:epsilon}); } expose(assert_approx_equals, "assert_approx_equals"); function assert_less_than(actual, expected, description) { /* * Test if a primitive number is less than another */ assert(typeof actual === "number", "assert_less_than", description, "expected a number but got a ${type_actual}", {type_actual:typeof actual}); assert(actual < expected, "assert_less_than", description, "expected a number less than ${expected} but got ${actual}", {expected:expected, actual:actual}); } expose(assert_less_than, "assert_less_than"); function assert_greater_than(actual, expected, description) { /* * Test if a primitive number is greater than another */ assert(typeof actual === "number", "assert_greater_than", description, "expected a number but got a ${type_actual}", {type_actual:typeof actual}); assert(actual > expected, "assert_greater_than", description, "expected a number greater than ${expected} but got ${actual}", {expected:expected, actual:actual}); } expose(assert_greater_than, "assert_greater_than"); function assert_between_exclusive(actual, lower, upper, description) { /* * Test if a primitive number is between two others */ assert(typeof actual === "number", "assert_between_exclusive", description, "expected a number but got a ${type_actual}", {type_actual:typeof actual}); assert(actual > lower && actual < upper, "assert_between_exclusive", description, "expected a number greater than ${lower} " + "and less than ${upper} but got ${actual}", {lower:lower, upper:upper, actual:actual}); } expose(assert_between_exclusive, "assert_between_exclusive"); function assert_less_than_equal(actual, expected, description) { /* * Test if a primitive number is less than or equal to another */ assert(typeof actual === "number", "assert_less_than_equal", description, "expected a number but got a ${type_actual}", {type_actual:typeof actual}); assert(actual <= expected, "assert_less_than_equal", description, "expected a number less than or equal to ${expected} but got ${actual}", {expected:expected, actual:actual}); } expose(assert_less_than_equal, "assert_less_than_equal"); function assert_greater_than_equal(actual, expected, description) { /* * Test if a primitive number is greater than or equal to another */ assert(typeof actual === "number", "assert_greater_than_equal", description, "expected a number but got a ${type_actual}", {type_actual:typeof actual}); assert(actual >= expected, "assert_greater_than_equal", description, "expected a number greater than or equal to ${expected} but got ${actual}", {expected:expected, actual:actual}); } expose(assert_greater_than_equal, "assert_greater_than_equal"); function assert_between_inclusive(actual, lower, upper, description) { /* * Test if a primitive number is between to two others or equal to either of them */ assert(typeof actual === "number", "assert_between_inclusive", description, "expected a number but got a ${type_actual}", {type_actual:typeof actual}); assert(actual >= lower && actual <= upper, "assert_between_inclusive", description, "expected a number greater than or equal to ${lower} " + "and less than or equal to ${upper} but got ${actual}", {lower:lower, upper:upper, actual:actual}); } expose(assert_between_inclusive, "assert_between_inclusive"); function assert_regexp_match(actual, expected, description) { /* * Test if a string (actual) matches a regexp (expected) */ assert(expected.test(actual), "assert_regexp_match", description, "expected ${expected} but got ${actual}", {expected:expected, actual:actual}); } expose(assert_regexp_match, "assert_regexp_match"); function assert_class_string(object, class_string, description) { assert_equals({}.toString.call(object), "[object " + class_string + "]", description); } expose(assert_class_string, "assert_class_string"); function _assert_own_property(name) { return function(object, property_name, description) { assert(object.hasOwnProperty(property_name), name, description, "expected property ${p} missing", {p:property_name}); }; } expose(_assert_own_property("assert_exists"), "assert_exists"); expose(_assert_own_property("assert_own_property"), "assert_own_property"); function assert_not_exists(object, property_name, description) { assert(!object.hasOwnProperty(property_name), "assert_not_exists", description, "unexpected property ${p} found", {p:property_name}); } expose(assert_not_exists, "assert_not_exists"); function _assert_inherits(name) { return function (object, property_name, description) { assert(typeof object === "object" || typeof object === "function", name, description, "provided value is not an object"); assert("hasOwnProperty" in object, name, description, "provided value is an object but has no hasOwnProperty method"); assert(!object.hasOwnProperty(property_name), name, description, "property ${p} found on object expected in prototype chain", {p:property_name}); assert(property_name in object, name, description, "property ${p} not found in prototype chain", {p:property_name}); }; } expose(_assert_inherits("assert_inherits"), "assert_inherits"); expose(_assert_inherits("assert_idl_attribute"), "assert_idl_attribute"); function assert_readonly(object, property_name, description) { var initial_value = object[property_name]; try { //Note that this can have side effects in the case where //the property has PutForwards object[property_name] = initial_value + "a"; //XXX use some other value here? assert(same_value(object[property_name], initial_value), "assert_readonly", description, "changing property ${p} succeeded", {p:property_name}); } finally { object[property_name] = initial_value; } } expose(assert_readonly, "assert_readonly"); function assert_throws(code, func, description) { try { func.call(this); assert(false, "assert_throws", description, "${func} did not throw", {func:func}); } catch (e) { if (e instanceof AssertionError) { throw e; } if (code === null) { return; } if (typeof code === "object") { assert(typeof e == "object" && "name" in e && e.name == code.name, "assert_throws", description, "${func} threw ${actual} (${actual_name}) expected ${expected} (${expected_name})", {func:func, actual:e, actual_name:e.name, expected:code, expected_name:code.name}); return; } var code_name_map = { INDEX_SIZE_ERR: 'IndexSizeError', HIERARCHY_REQUEST_ERR: 'HierarchyRequestError', WRONG_DOCUMENT_ERR: 'WrongDocumentError', INVALID_CHARACTER_ERR: 'InvalidCharacterError', NO_MODIFICATION_ALLOWED_ERR: 'NoModificationAllowedError', NOT_FOUND_ERR: 'NotFoundError', NOT_SUPPORTED_ERR: 'NotSupportedError', INUSE_ATTRIBUTE_ERR: 'InUseAttributeError', INVALID_STATE_ERR: 'InvalidStateError', SYNTAX_ERR: 'SyntaxError', INVALID_MODIFICATION_ERR: 'InvalidModificationError', NAMESPACE_ERR: 'NamespaceError', INVALID_ACCESS_ERR: 'InvalidAccessError', TYPE_MISMATCH_ERR: 'TypeMismatchError', SECURITY_ERR: 'SecurityError', NETWORK_ERR: 'NetworkError', ABORT_ERR: 'AbortError', URL_MISMATCH_ERR: 'URLMismatchError', QUOTA_EXCEEDED_ERR: 'QuotaExceededError', TIMEOUT_ERR: 'TimeoutError', INVALID_NODE_TYPE_ERR: 'InvalidNodeTypeError', DATA_CLONE_ERR: 'DataCloneError' }; var name = code in code_name_map ? code_name_map[code] : code; var name_code_map = { IndexSizeError: 1, HierarchyRequestError: 3, WrongDocumentError: 4, InvalidCharacterError: 5, NoModificationAllowedError: 7, NotFoundError: 8, NotSupportedError: 9, InUseAttributeError: 10, InvalidStateError: 11, SyntaxError: 12, InvalidModificationError: 13, NamespaceError: 14, InvalidAccessError: 15, TypeMismatchError: 17, SecurityError: 18, NetworkError: 19, AbortError: 20, URLMismatchError: 21, QuotaExceededError: 22, TimeoutError: 23, InvalidNodeTypeError: 24, DataCloneError: 25, EncodingError: 0, NotReadableError: 0, UnknownError: 0, ConstraintError: 0, DataError: 0, TransactionInactiveError: 0, ReadOnlyError: 0, VersionError: 0, OperationError: 0, }; if (!(name in name_code_map)) { throw new AssertionError('Test bug: unrecognized DOMException code "' + code + '" passed to assert_throws()'); } var required_props = { code: name_code_map[name] }; if (required_props.code === 0 || (typeof e == "object" && "name" in e && e.name !== e.name.toUpperCase() && e.name !== "DOMException")) { // New style exception: also test the name property. required_props.name = name; } //We'd like to test that e instanceof the appropriate interface, //but we can't, because we don't know what window it was created //in. It might be an instanceof the appropriate interface on some //unknown other window. TODO: Work around this somehow? assert(typeof e == "object", "assert_throws", description, "${func} threw ${e} with type ${type}, not an object", {func:func, e:e, type:typeof e}); for (var prop in required_props) { assert(typeof e == "object" && prop in e && e[prop] == required_props[prop], "assert_throws", description, "${func} threw ${e} that is not a DOMException " + code + ": property ${prop} is equal to ${actual}, expected ${expected}", {func:func, e:e, prop:prop, actual:e[prop], expected:required_props[prop]}); } } } expose(assert_throws, "assert_throws"); function assert_unreached(description) { assert(false, "assert_unreached", description, "Reached unreachable code"); } expose(assert_unreached, "assert_unreached"); function assert_any(assert_func, actual, expected_array) { var args = [].slice.call(arguments, 3); var errors = []; var passed = false; forEach(expected_array, function(expected) { try { assert_func.apply(this, [actual, expected].concat(args)); passed = true; } catch (e) { errors.push(e.message); }