OLD | NEW |
| (Empty) |
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. | |
4 | |
5 debug = function debug(msg) | |
6 { | |
7 console.log(msg); | |
8 }; | |
9 | |
10 description = function description(msg, quiet) | |
11 { | |
12 console.log(msg); | |
13 }; | |
14 | |
15 finishJSTest = function finishJSTest() { | |
16 console.log("TEST FINISHED"); | |
17 }; | |
18 | |
19 function handleTestFinished() { | |
20 if (!window.jsTestIsAsync) | |
21 finishJSTest(); | |
22 } | |
23 | |
24 function testFailed(msg) { | |
25 debug('FAIL: ' + msg); | |
26 } | |
27 | |
28 function testPassed(msg) | |
29 { | |
30 debug('PASS: ' + msg); | |
31 } | |
32 | |
33 function isWorker() | |
34 { | |
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; | |
40 } | |
41 | |
42 if (!isWorker()) { | |
43 window.addEventListener('DOMContentLoaded', handleTestFinished, false); | |
44 } | |
45 | |
46 function _compareLessThan(_a, _b) { | |
47 return _a < _b; | |
48 } | |
49 | |
50 function _compareGreaterThan(_a, _b) { | |
51 return _a > _b; | |
52 } | |
53 | |
54 // TODO(timvolodine): consider moving this code to js-test.js in blink and | |
55 // reusing it from there (crbug.com/535209). | |
56 function _comp(_a, _b, _inv_comparison_func, _comparison_str) { | |
57 if (typeof _a != "string" || typeof _b != "string") | |
58 debug("WARN: _comp expects string arguments"); | |
59 | |
60 var _exception; | |
61 var _av; | |
62 try { | |
63 _av = eval(_a); | |
64 } catch (e) { | |
65 _exception = e; | |
66 } | |
67 var _bv = eval(_b); | |
68 | |
69 if (_exception) { | |
70 testFailed(_a + " should be" + _comparison_str + _b + ". Threw exception " | |
71 + _exception); | |
72 } else if (typeof _av == "undefined" || _inv_comparison_func(_av, _bv)) { | |
73 testFailed(_a + " should be" + _comparison_str + _b + ". Was " + _av | |
74 + " (of type " + typeof _av + ")."); | |
75 } else { | |
76 testPassed(_a + " is" + _comparison_str + _b); | |
77 } | |
78 } | |
79 | |
80 function shouldBeGreaterThanOrEqual(_a, _b) { | |
81 _comp(_a, _b, _compareLessThan, " >= "); | |
82 } | |
83 | |
84 function shouldBeLessThanOrEqual(_a, _b) { | |
85 _comp(_a, _b, _compareGreaterThan, " <= "); | |
86 } | |
87 | |
88 // Functions in common with js-test.js in blink, | |
89 // see third_party/WebKit/LayoutTests/resources/js-test.js | |
90 | |
91 function areArraysEqual(a, b) | |
92 { | |
93 try { | |
94 if (a.length !== b.length) | |
95 return false; | |
96 for (var i = 0; i < a.length; i++) | |
97 if (a[i] !== b[i]) | |
98 return false; | |
99 } catch (ex) { | |
100 return false; | |
101 } | |
102 return true; | |
103 } | |
104 | |
105 // Returns a sorted array of property names of object. This function returns | |
106 // not only own properties but also properties on prototype chains. | |
107 function getAllPropertyNames(object) { | |
108 var properties = []; | |
109 for (var property in object) { | |
110 properties.push(property); | |
111 } | |
112 return properties.sort(); | |
113 } | |
114 | |
115 function isNewSVGTearOffType(v) | |
116 { | |
117 return ['[object SVGLength]', '[object SVGLengthList]', | |
118 '[object SVGPoint]', '[object SVGPointList]', | |
119 '[object SVGNumber]', '[object SVGTransform]', | |
120 '[object SVGTransformList]'].indexOf(""+v) != -1; | |
121 } | |
122 | |
123 function stringify(v) | |
124 { | |
125 if (isNewSVGTearOffType(v)) | |
126 return v.valueAsString; | |
127 if (v === 0 && 1/v < 0) | |
128 return "-0"; | |
129 else return "" + v; | |
130 } | |
131 | |
132 function isResultCorrect(actual, expected) | |
133 { | |
134 if (expected === 0) | |
135 return actual === expected && (1/actual) === (1/expected); | |
136 if (actual === expected) | |
137 return true; | |
138 // http://crbug.com/308818 : The new implementation of SVGListProperties | |
139 // do not necessary return the same wrapper object, so === operator would | |
140 // not work. We compare for their string representation instead. | |
141 if (isNewSVGTearOffType(expected) && typeof(expected) == typeof(actual) | |
142 && actual.valueAsString == expected.valueAsString) | |
143 return true; | |
144 if (typeof(expected) == "number" && isNaN(expected)) | |
145 return typeof(actual) == "number" && isNaN(actual); | |
146 if (expected && (Object.prototype.toString.call(expected) | |
147 == Object.prototype.toString.call([]))) | |
148 return areArraysEqual(actual, expected); | |
149 return false; | |
150 } | |
151 | |
152 function shouldBe(_a, _b, quiet, opt_tolerance) | |
153 { | |
154 if (typeof _a != "string" || typeof _b != "string") | |
155 debug("WARN: shouldBe() expects string arguments"); | |
156 var _exception; | |
157 var _av; | |
158 try { | |
159 _av = eval(_a); | |
160 } catch (e) { | |
161 _exception = e; | |
162 } | |
163 var _bv = eval(_b); | |
164 | |
165 if (_exception) | |
166 testFailed(_a + " should be " + _bv + ". Threw exception " | |
167 + _exception); | |
168 else if (isResultCorrect(_av, _bv) | |
169 || (typeof opt_tolerance == 'number' && typeof _av == 'number' | |
170 && Math.abs(_av - _bv) <= opt_tolerance)) { | |
171 if (!quiet) { | |
172 testPassed(_a + " is " + _b); | |
173 } | |
174 } else if (typeof(_av) == typeof(_bv)) { | |
175 testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + "."); | |
176 } else { | |
177 testFailed(_a + " should be " + _bv + " (of type " + typeof _bv | |
178 + "). Was " + _av + " (of type " + typeof _av + ")."); | |
179 } | |
180 } | |
181 | |
182 function shouldBeEqualToString(a, b) | |
183 { | |
184 if (typeof a !== "string" || typeof b !== "string") | |
185 debug("WARN: shouldBeEqualToString() expects string arguments"); | |
186 var unevaledString = JSON.stringify(b); | |
187 shouldBe(a, unevaledString); | |
188 } | |
189 | |
190 function shouldBeTrue(a, quiet) { shouldBe(a, "true", quiet); } | |
191 function shouldBeFalse(a, quiet) { shouldBe(a, "false", quiet); } | |
OLD | NEW |