Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / js / mozilla / resources / moz-test-pre.js
blob8564decd0830632bb3658b4f45ae158f1c1f3488
1 // svg/dynamic-updates tests set enablePixelTesting=true, as we want to dump text + pixel results
2 if (window.testRunner) {
3 if (window.enablePixelTesting)
4 testRunner.dumpAsTextWithPixelResults();
5 else
6 testRunner.dumpAsText();
9 function description(msg)
11 // For MSIE 6 compatibility
12 var span = document.createElement("span");
13 span.innerHTML = '<p>' + msg + '</p><p>On success, you will see a series of "<span class="pass">PASS</span>" messages, followed by "<span class="pass">TEST COMPLETE</span>".</p>';
14 var description = document.getElementById("description");
15 if (description.firstChild)
16 description.replaceChild(span, description.firstChild);
17 else
18 description.appendChild(span);
21 function debug(msg)
23 var span = document.createElement("span");
24 document.getElementById("console").appendChild(span); // insert it first so XHTML knows the namespace
25 span.innerHTML = msg + '<br />';
28 function escapeHTML(text)
30 return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/\0/g, "\\0");
33 function testPassed(msg)
35 debug('<span><span class="pass">PASS</span> ' + escapeHTML(msg) + '</span>');
38 function testFailed(msg)
40 debug('<span><span class="fail">FAIL</span> ' + escapeHTML(msg) + '</span>');
43 function areArraysEqual(_a, _b)
45 try {
46 if (_a.length !== _b.length)
47 return false;
48 for (var i = 0; i < _a.length; i++)
49 if (_a[i] !== _b[i])
50 return false;
51 } catch (ex) {
52 return false;
54 return true;
57 function isMinusZero(n)
59 // the only way to tell 0 from -0 in JS is the fact that 1/-0 is
60 // -Infinity instead of Infinity
61 return n === 0 && 1/n < 0;
64 function isResultCorrect(_actual, _expected)
66 if (_expected === 0)
67 return _actual === _expected && (1/_actual) === (1/_expected);
68 if (_actual === _expected)
69 return true;
70 if (typeof(_expected) == "number" && isNaN(_expected))
71 return typeof(_actual) == "number" && isNaN(_actual);
72 if (Object.prototype.toString.call(_expected) == Object.prototype.toString.call([]))
73 return areArraysEqual(_actual, _expected);
74 return false;
77 function stringify(v)
79 if (v === 0 && 1/v < 0)
80 return "-0";
81 else return "" + v;
84 function evalAndLog(_a)
86 if (typeof _a != "string")
87 debug("WARN: tryAndLog() expects a string argument");
89 // Log first in case things go horribly wrong or this causes a sync event.
90 debug(_a);
92 var _av;
93 try {
94 _av = eval(_a);
95 } catch (e) {
96 testFailed(_a + " threw exception " + e);
98 return _av;
101 function shouldBe(_a, _b)
103 if (typeof _a != "string" || typeof _b != "string")
104 debug("WARN: shouldBe() expects string arguments");
105 var exception;
106 var _av;
107 try {
108 _av = eval(_a);
109 } catch (e) {
110 exception = e;
112 var _bv = eval(_b);
114 if (exception)
115 testFailed(_a + " should be " + _bv + ". Threw exception " + exception);
116 else if (isResultCorrect(_av, _bv))
117 testPassed(_a + " is " + _b);
118 else if (typeof(_av) == typeof(_bv))
119 testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + ".");
120 else
121 testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Was " + _av + " (of type " + typeof _av + ").");
124 function shouldBeTrue(_a) { shouldBe(_a, "true"); }
125 function shouldBeFalse(_a) { shouldBe(_a, "false"); }
126 function shouldBeNaN(_a) { shouldBe(_a, "NaN"); }
127 function shouldBeNull(_a) { shouldBe(_a, "null"); }
129 function shouldBeEqualToString(a, b)
131 var unevaledString = '"' + b.replace(/\\/g, "\\\\").replace(/"/g, "\"").replace(/\n/g, "\\n").replace(/\r/g, "\\r") + '"';
132 shouldBe(a, unevaledString);
135 function shouldEvaluateTo(actual, expected) {
136 // A general-purpose comparator. 'actual' should be a string to be
137 // evaluated, as for shouldBe(). 'expected' may be any type and will be
138 // used without being eval'ed.
139 if (expected == null) {
140 // Do this before the object test, since null is of type 'object'.
141 shouldBeNull(actual);
142 } else if (typeof expected == "undefined") {
143 shouldBeUndefined(actual);
144 } else if (typeof expected == "function") {
145 // All this fuss is to avoid the string-arg warning from shouldBe().
146 try {
147 actualValue = eval(actual);
148 } catch (e) {
149 testFailed("Evaluating " + actual + ": Threw exception " + e);
150 return;
152 shouldBe("'" + actualValue.toString().replace(/\n/g, "") + "'",
153 "'" + expected.toString().replace(/\n/g, "") + "'");
154 } else if (typeof expected == "object") {
155 shouldBeTrue(actual + " == '" + expected + "'");
156 } else if (typeof expected == "string") {
157 shouldBe(actual, expected);
158 } else if (typeof expected == "boolean") {
159 shouldBe("typeof " + actual, "'boolean'");
160 if (expected)
161 shouldBeTrue(actual);
162 else
163 shouldBeFalse(actual);
164 } else if (typeof expected == "number") {
165 shouldBe(actual, stringify(expected));
166 } else {
167 debug(expected + " is unknown type " + typeof expected);
168 shouldBeTrue(actual, "'" +expected.toString() + "'");
172 function shouldBeNonZero(_a)
174 var exception;
175 var _av;
176 try {
177 _av = eval(_a);
178 } catch (e) {
179 exception = e;
182 if (exception)
183 testFailed(_a + " should be non-zero. Threw exception " + exception);
184 else if (_av != 0)
185 testPassed(_a + " is non-zero.");
186 else
187 testFailed(_a + " should be non-zero. Was " + _av);
190 function shouldBeNonNull(_a)
192 var exception;
193 var _av;
194 try {
195 _av = eval(_a);
196 } catch (e) {
197 exception = e;
200 if (exception)
201 testFailed(_a + " should be non-null. Threw exception " + exception);
202 else if (_av != null)
203 testPassed(_a + " is non-null.");
204 else
205 testFailed(_a + " should be non-null. Was " + _av);
208 function shouldBeUndefined(_a)
210 var exception;
211 var _av;
212 try {
213 _av = eval(_a);
214 } catch (e) {
215 exception = e;
218 if (exception)
219 testFailed(_a + " should be undefined. Threw exception " + exception);
220 else if (typeof _av == "undefined")
221 testPassed(_a + " is undefined.");
222 else
223 testFailed(_a + " should be undefined. Was " + _av);
226 function shouldBeDefined(_a)
228 var exception;
229 var _av;
230 try {
231 _av = eval(_a);
232 } catch (e) {
233 exception = e;
236 if (exception)
237 testFailed(_a + " should be defined. Threw exception " + exception);
238 else if (_av !== undefined)
239 testPassed(_a + " is defined.");
240 else
241 testFailed(_a + " should be defined. Was " + _av);
244 function shouldBeGreaterThanOrEqual(_a, _b) {
245 if (typeof _a != "string" || typeof _b != "string")
246 debug("WARN: shouldBeGreaterThanOrEqual expects string arguments");
248 var exception;
249 var _av;
250 try {
251 _av = eval(_a);
252 } catch (e) {
253 exception = e;
255 var _bv = eval(_b);
257 if (exception)
258 testFailed(_a + " should be >= " + _b + ". Threw exception " + exception);
259 else if (typeof _av == "undefined" || _av < _bv)
260 testFailed(_a + " should be >= " + _b + ". Was " + _av + " (of type " + typeof _av + ").");
261 else
262 testPassed(_a + " is >= " + _b);
265 function shouldThrowType(_a, _e)
267 var exception;
268 var _av;
269 try {
270 _av = eval(_a);
271 } catch (e) {
272 exception = e;
275 if (exception) {
276 if (exception instanceof _e)
277 testPassed(_a + " threw exception of type " + _e.name + ".");
278 else
279 testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Threw exception " + exception + ".");
280 } else
281 testFailed(_a + " should throw an instance of " + _e.name);
284 function gc() {
285 if (typeof GCController !== "undefined")
286 GCController.collect();
287 else {
288 function gcRec(n) {
289 if (n < 1)
290 return {};
291 var temp = {i: "ab" + i + (i / 100000)};
292 temp += "foo";
293 gcRec(n-1);
295 for (var i = 0; i < 1000; i++)
296 gcRec(10)
300 // It's possible for an async test to call finishJSTest() before moz-test-post.js
301 // has been parsed.
302 function finishJSTest()
304 wasFinishJSTestCalled = true;
305 if (!window.wasPostTestScriptParsed)
306 return;
307 shouldBeTrue("successfullyParsed");
308 debug('<br /><span class="pass">TEST COMPLETE</span>');
309 if (window.jsTestIsAsync && window.testRunner)
310 testRunner.notifyDone();
313 // shadow print()
314 function print()
316 debug.apply(this, arguments);
319 function assertEq(left, right, message)
321 if (left === right)
322 testPassed(left + " === " + right);
323 else if (message)
324 testFailed(message);
325 else
326 testFailed("assertEq failed: " + left + "(of type " + (typeof left) + ") !== " + right + "(of type " + (typeof right) + ")");