1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 debug = function debug(msg)
10 description = function description(msg, quiet)
15 finishJSTest = function finishJSTest() {
16 console.log("TEST FINISHED");
19 function handleTestFinished() {
20 if (!window.jsTestIsAsync)
24 function testFailed(msg) {
25 debug('FAIL: ' + msg);
28 function testPassed(msg)
30 debug('PASS: ' + msg);
35 // It's conceivable that someone would stub out 'document' in a worker so
36 // also check for childNodes, an arbitrary DOM-related object that is
37 // meaningless in a WorkerContext.
38 return (typeof document === 'undefined' ||
39 typeof document.childNodes === 'undefined') && !!self.importScripts;
43 window.addEventListener('DOMContentLoaded', handleTestFinished, false);
47 // Functions in common with js-test.js in blink,
48 // see third_party/WebKit/LayoutTests/resources/js-test.js
50 function areArraysEqual(a, b)
53 if (a.length !== b.length)
55 for (var i = 0; i < a.length; i++)
64 // Returns a sorted array of property names of object. This function returns
65 // not only own properties but also properties on prototype chains.
66 function getAllPropertyNames(object) {
68 for (var property in object) {
69 properties.push(property);
71 return properties.sort();
74 function isNewSVGTearOffType(v)
76 return ['[object SVGLength]', '[object SVGLengthList]',
77 '[object SVGPoint]', '[object SVGPointList]',
78 '[object SVGNumber]', '[object SVGTransform]',
79 '[object SVGTransformList]'].indexOf(""+v) != -1;
84 if (isNewSVGTearOffType(v))
85 return v.valueAsString;
86 if (v === 0 && 1/v < 0)
91 function isResultCorrect(actual, expected)
94 return actual === expected && (1/actual) === (1/expected);
95 if (actual === expected)
97 // http://crbug.com/308818 : The new implementation of SVGListProperties
98 // do not necessary return the same wrapper object, so === operator would
99 // not work. We compare for their string representation instead.
100 if (isNewSVGTearOffType(expected) && typeof(expected) == typeof(actual)
101 && actual.valueAsString == expected.valueAsString)
103 if (typeof(expected) == "number" && isNaN(expected))
104 return typeof(actual) == "number" && isNaN(actual);
105 if (expected && (Object.prototype.toString.call(expected)
106 == Object.prototype.toString.call([])))
107 return areArraysEqual(actual, expected);
111 function shouldBe(_a, _b, quiet, opt_tolerance)
113 if (typeof _a != "string" || typeof _b != "string")
114 debug("WARN: shouldBe() expects string arguments");
125 testFailed(_a + " should be " + _bv + ". Threw exception "
127 else if (isResultCorrect(_av, _bv)
128 || (typeof opt_tolerance == 'number' && typeof _av == 'number'
129 && Math.abs(_av - _bv) <= opt_tolerance)) {
131 testPassed(_a + " is " + _b);
133 } else if (typeof(_av) == typeof(_bv)) {
134 testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + ".");
136 testFailed(_a + " should be " + _bv + " (of type " + typeof _bv
137 + "). Was " + _av + " (of type " + typeof _av + ").");
141 function shouldBeEqualToString(a, b)
143 if (typeof a !== "string" || typeof b !== "string")
144 debug("WARN: shouldBeEqualToString() expects string arguments");
145 var unevaledString = JSON.stringify(b);
146 shouldBe(a, unevaledString);
149 function shouldBeTrue(a, quiet) { shouldBe(a, "true", quiet); }
150 function shouldBeFalse(a, quiet) { shouldBe(a, "false", quiet); }