2 * testharness-helpers contains various useful extensions to testharness.js to
3 * allow them to be used across multiple tests before they have been
4 * upstreamed. This file is intended to be usable from both document and worker
5 * environments, so code should for example not rely on the DOM.
8 // Returns a promise that fulfills after the provided |promise| is fulfilled.
9 // The |test| succeeds only if |promise| rejects with an exception matching
10 // |code|. Accepted values for |code| follow those accepted for assert_throws().
11 // The optional |description| describes the test being performed.
14 // assert_promise_rejects(
15 // new Promise(...), // something that should throw an exception.
17 // 'Should throw NotFoundError.');
19 // assert_promise_rejects(
22 // 'Should throw TypeError');
23 function assert_promise_rejects(promise, code, description) {
26 throw 'assert_promise_rejects: ' + description + ' Promise did not reject.';
29 if (code !== undefined) {
30 assert_throws(code, function() { throw e; }, description);
35 // Asserts that |object| that is an instance of some interface has the attribute
36 // |attribute_name| following the conditions specified by WebIDL, but it's
37 // acceptable that the attribute |attribute_name| is an own property of the
38 // object because we're in the middle of moving the attribute to a prototype
39 // chain. Once we complete the transition to prototype chains,
40 // assert_will_be_idl_attribute must be replaced with assert_idl_attribute
41 // defined in testharness.js.
43 // FIXME: Remove assert_will_be_idl_attribute once we complete the transition
44 // of moving the DOM attributes to prototype chains. (http://crbug.com/43394)
45 function assert_will_be_idl_attribute(object, attribute_name, description) {
46 assert_true(typeof object === "object", description);
48 assert_true("hasOwnProperty" in object, description);
50 // Do not test if |attribute_name| is not an own property because
51 // |attribute_name| is in the middle of the transition to a prototype
52 // chain. (http://crbug.com/43394)
54 assert_true(attribute_name in object, description);
57 // Stringifies a DOM object. This function stringifies not only own properties
58 // but also DOM attributes which are on a prototype chain. Note that
59 // JSON.stringify only stringifies own properties.
60 function stringifyDOMObject(object)
62 function deepCopy(src) {
63 if (typeof src != "object")
65 var dst = Array.isArray(src) ? [] : {};
66 for (var property in src) {
67 dst[property] = deepCopy(src[property]);
71 return JSON.stringify(deepCopy(object));