|
Line 0
a/LayoutTests/http/tests/resources/js-test-pre.js_sec1
|
|
|
1 |
// svg/dynamic-updates tests set enablePixelTesting=true, as we want to dump text + pixel results |
| 2 |
if (self.layoutTestController) |
| 3 |
layoutTestController.dumpAsText(self.enablePixelTesting); |
| 4 |
|
| 5 |
var description, debug, successfullyParsed, errorMessage; |
| 6 |
|
| 7 |
(function() { |
| 8 |
|
| 9 |
function getOrCreate(id, tagName) |
| 10 |
{ |
| 11 |
var element = document.getElementById(id); |
| 12 |
if (element) |
| 13 |
return element; |
| 14 |
|
| 15 |
element = document.createElement(tagName); |
| 16 |
element.id = id; |
| 17 |
var refNode; |
| 18 |
var parent = document.body || document.documentElement; |
| 19 |
if (id == "description") |
| 20 |
refNode = getOrCreate("console", "div"); |
| 21 |
else |
| 22 |
refNode = parent.firstChild; |
| 23 |
|
| 24 |
parent.insertBefore(element, refNode); |
| 25 |
return element; |
| 26 |
} |
| 27 |
|
| 28 |
description = function description(msg, quiet) |
| 29 |
{ |
| 30 |
// For MSIE 6 compatibility |
| 31 |
var span = document.createElement("span"); |
| 32 |
if (quiet) |
| 33 |
span.innerHTML = '<p>' + msg + '</p><p>On success, you will see no "<span class="fail">FAIL</span>" messages, followed by "<span class="pass">TEST COMPLETE</span>".</p>'; |
| 34 |
else |
| 35 |
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>'; |
| 36 |
|
| 37 |
var description = getOrCreate("description", "p"); |
| 38 |
if (description.firstChild) |
| 39 |
description.replaceChild(span, description.firstChild); |
| 40 |
else |
| 41 |
description.appendChild(span); |
| 42 |
}; |
| 43 |
|
| 44 |
debug = function debug(msg) |
| 45 |
{ |
| 46 |
var span = document.createElement("span"); |
| 47 |
getOrCreate("console", "div").appendChild(span); // insert it first so XHTML knows the namespace |
| 48 |
span.innerHTML = msg + '<br />'; |
| 49 |
}; |
| 50 |
|
| 51 |
var css = |
| 52 |
".pass {" + |
| 53 |
"font-weight: bold;" + |
| 54 |
"color: green;" + |
| 55 |
"}" + |
| 56 |
".fail {" + |
| 57 |
"font-weight: bold;" + |
| 58 |
"color: red;" + |
| 59 |
"}" + |
| 60 |
"#console {" + |
| 61 |
"white-space: pre-wrap;" + |
| 62 |
"font-family: monospace;" + |
| 63 |
"}"; |
| 64 |
|
| 65 |
function insertStyleSheet() |
| 66 |
{ |
| 67 |
var styleElement = document.createElement("style"); |
| 68 |
styleElement.textContent = css; |
| 69 |
(document.head || document.documentElement).appendChild(styleElement); |
| 70 |
} |
| 71 |
|
| 72 |
if (!isWorker()) |
| 73 |
insertStyleSheet(); |
| 74 |
|
| 75 |
self.onerror = function(message) |
| 76 |
{ |
| 77 |
errorMessage = message; |
| 78 |
}; |
| 79 |
|
| 80 |
})(); |
| 81 |
|
| 82 |
function isWorker() |
| 83 |
{ |
| 84 |
// It's conceivable that someone would stub out 'document' in a worker so |
| 85 |
// also check for childNodes, an arbitrary DOM-related object that is |
| 86 |
// meaningless in a WorkerContext. |
| 87 |
return (typeof document === 'undefined' || typeof document.childNodes === 'undefined') && !!self.importScripts; |
| 88 |
} |
| 89 |
|
| 90 |
function descriptionQuiet(msg) { description(msg, true); } |
| 91 |
|
| 92 |
function escapeHTML(text) |
| 93 |
{ |
| 94 |
return text.replace(/&/g, "&").replace(/</g, "<").replace(/\0/g, "\\0"); |
| 95 |
} |
| 96 |
|
| 97 |
function testPassed(msg) |
| 98 |
{ |
| 99 |
debug('<span><span class="pass">PASS</span> ' + escapeHTML(msg) + '</span>'); |
| 100 |
} |
| 101 |
|
| 102 |
function testFailed(msg) |
| 103 |
{ |
| 104 |
debug('<span><span class="fail">FAIL</span> ' + escapeHTML(msg) + '</span>'); |
| 105 |
} |
| 106 |
|
| 107 |
function areArraysEqual(_a, _b) |
| 108 |
{ |
| 109 |
try { |
| 110 |
if (_a.length !== _b.length) |
| 111 |
return false; |
| 112 |
for (var i = 0; i < _a.length; i++) |
| 113 |
if (_a[i] !== _b[i]) |
| 114 |
return false; |
| 115 |
} catch (ex) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
function isMinusZero(n) |
| 122 |
{ |
| 123 |
// the only way to tell 0 from -0 in JS is the fact that 1/-0 is |
| 124 |
// -Infinity instead of Infinity |
| 125 |
return n === 0 && 1/n < 0; |
| 126 |
} |
| 127 |
|
| 128 |
function isResultCorrect(_actual, _expected) |
| 129 |
{ |
| 130 |
if (_expected === 0) |
| 131 |
return _actual === _expected && (1/_actual) === (1/_expected); |
| 132 |
if (_actual === _expected) |
| 133 |
return true; |
| 134 |
if (typeof(_expected) == "number" && isNaN(_expected)) |
| 135 |
return typeof(_actual) == "number" && isNaN(_actual); |
| 136 |
if (_expected && (Object.prototype.toString.call(_expected) == Object.prototype.toString.call([]))) |
| 137 |
return areArraysEqual(_actual, _expected); |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
function stringify(v) |
| 142 |
{ |
| 143 |
if (v === 0 && 1/v < 0) |
| 144 |
return "-0"; |
| 145 |
else return "" + v; |
| 146 |
} |
| 147 |
|
| 148 |
function evalAndLog(_a) |
| 149 |
{ |
| 150 |
if (typeof _a != "string") |
| 151 |
debug("WARN: tryAndLog() expects a string argument"); |
| 152 |
|
| 153 |
// Log first in case things go horribly wrong or this causes a sync event. |
| 154 |
debug(_a); |
| 155 |
|
| 156 |
var _av; |
| 157 |
try { |
| 158 |
_av = eval(_a); |
| 159 |
} catch (e) { |
| 160 |
testFailed(_a + " threw exception " + e); |
| 161 |
} |
| 162 |
return _av; |
| 163 |
} |
| 164 |
|
| 165 |
function shouldBe(_a, _b, quiet) |
| 166 |
{ |
| 167 |
if (typeof _a != "string" || typeof _b != "string") |
| 168 |
debug("WARN: shouldBe() expects string arguments"); |
| 169 |
var exception; |
| 170 |
var _av; |
| 171 |
try { |
| 172 |
_av = eval(_a); |
| 173 |
} catch (e) { |
| 174 |
exception = e; |
| 175 |
} |
| 176 |
var _bv = eval(_b); |
| 177 |
|
| 178 |
if (exception) |
| 179 |
testFailed(_a + " should be " + _bv + ". Threw exception " + exception); |
| 180 |
else if (isResultCorrect(_av, _bv)) { |
| 181 |
if (!quiet) { |
| 182 |
testPassed(_a + " is " + _b); |
| 183 |
} |
| 184 |
} else if (typeof(_av) == typeof(_bv)) |
| 185 |
testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + "."); |
| 186 |
else |
| 187 |
testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Was " + _av + " (of type " + typeof _av + ")."); |
| 188 |
} |
| 189 |
|
| 190 |
// Variant of shouldBe()--confirms that result of eval(_to_eval) is within |
| 191 |
// numeric _tolerance of numeric _target. |
| 192 |
function shouldBeCloseTo(_to_eval, _target, _tolerance, quiet) |
| 193 |
{ |
| 194 |
if (typeof _to_eval != "string") { |
| 195 |
testFailed("shouldBeCloseTo() requires string argument _to_eval. was type " + typeof _to_eval); |
| 196 |
return; |
| 197 |
} |
| 198 |
if (typeof _target != "number") { |
| 199 |
testFailed("shouldBeCloseTo() requires numeric argument _target. was type " + typeof _target); |
| 200 |
return; |
| 201 |
} |
| 202 |
if (typeof _tolerance != "number") { |
| 203 |
testFailed("shouldBeCloseTo() requires numeric argument _tolerance. was type " + typeof _tolerance); |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
var _result; |
| 208 |
try { |
| 209 |
_result = eval(_to_eval); |
| 210 |
} catch (e) { |
| 211 |
testFailed(_to_eval + " should be within " + _tolerance + " of " |
| 212 |
+ _target + ". Threw exception " + e); |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
if (typeof(_result) != typeof(_target)) { |
| 217 |
testFailed(_to_eval + " should be of type " + typeof _target |
| 218 |
+ " but was of type " + typeof _result); |
| 219 |
} else if (Math.abs(_result - _target) <= _tolerance) { |
| 220 |
if (!quiet) { |
| 221 |
testPassed(_to_eval + " is within " + _tolerance + " of " + _target); |
| 222 |
} |
| 223 |
} else { |
| 224 |
testFailed(_to_eval + " should be within " + _tolerance + " of " + _target |
| 225 |
+ ". Was " + _result + "."); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
function shouldNotBe(_a, _b, quiet) |
| 230 |
{ |
| 231 |
if (typeof _a != "string" || typeof _b != "string") |
| 232 |
debug("WARN: shouldNotBe() expects string arguments"); |
| 233 |
var exception; |
| 234 |
var _av; |
| 235 |
try { |
| 236 |
_av = eval(_a); |
| 237 |
} catch (e) { |
| 238 |
exception = e; |
| 239 |
} |
| 240 |
var _bv = eval(_b); |
| 241 |
|
| 242 |
if (exception) |
| 243 |
testFailed(_a + " should not be " + _bv + ". Threw exception " + exception); |
| 244 |
else if (!isResultCorrect(_av, _bv)) { |
| 245 |
if (!quiet) { |
| 246 |
testPassed(_a + " is not " + _b); |
| 247 |
} |
| 248 |
} else |
| 249 |
testFailed(_a + " should not be " + _bv + "."); |
| 250 |
} |
| 251 |
|
| 252 |
function shouldBeTrue(_a) { shouldBe(_a, "true"); } |
| 253 |
function shouldBeTrueQuiet(_a) { shouldBe(_a, "true", true); } |
| 254 |
function shouldBeFalse(_a) { shouldBe(_a, "false"); } |
| 255 |
function shouldBeNaN(_a) { shouldBe(_a, "NaN"); } |
| 256 |
function shouldBeNull(_a) { shouldBe(_a, "null"); } |
| 257 |
function shouldBeZero(_a) { shouldBe(_a, "0"); } |
| 258 |
|
| 259 |
function shouldBeEqualToString(a, b) |
| 260 |
{ |
| 261 |
var unevaledString = '"' + b.replace(/\\/g, "\\\\").replace(/"/g, "\"").replace(/\n/g, "\\n").replace(/\r/g, "\\r") + '"'; |
| 262 |
shouldBe(a, unevaledString); |
| 263 |
} |
| 264 |
|
| 265 |
function shouldBeEmptyString(_a) { shouldBeEqualToString(_a, ""); } |
| 266 |
|
| 267 |
function shouldEvaluateTo(actual, expected) { |
| 268 |
// A general-purpose comparator. 'actual' should be a string to be |
| 269 |
// evaluated, as for shouldBe(). 'expected' may be any type and will be |
| 270 |
// used without being eval'ed. |
| 271 |
if (expected == null) { |
| 272 |
// Do this before the object test, since null is of type 'object'. |
| 273 |
shouldBeNull(actual); |
| 274 |
} else if (typeof expected == "undefined") { |
| 275 |
shouldBeUndefined(actual); |
| 276 |
} else if (typeof expected == "function") { |
| 277 |
// All this fuss is to avoid the string-arg warning from shouldBe(). |
| 278 |
try { |
| 279 |
actualValue = eval(actual); |
| 280 |
} catch (e) { |
| 281 |
testFailed("Evaluating " + actual + ": Threw exception " + e); |
| 282 |
return; |
| 283 |
} |
| 284 |
shouldBe("'" + actualValue.toString().replace(/\n/g, "") + "'", |
| 285 |
"'" + expected.toString().replace(/\n/g, "") + "'"); |
| 286 |
} else if (typeof expected == "object") { |
| 287 |
shouldBeTrue(actual + " == '" + expected + "'"); |
| 288 |
} else if (typeof expected == "string") { |
| 289 |
shouldBe(actual, expected); |
| 290 |
} else if (typeof expected == "boolean") { |
| 291 |
shouldBe("typeof " + actual, "'boolean'"); |
| 292 |
if (expected) |
| 293 |
shouldBeTrue(actual); |
| 294 |
else |
| 295 |
shouldBeFalse(actual); |
| 296 |
} else if (typeof expected == "number") { |
| 297 |
shouldBe(actual, stringify(expected)); |
| 298 |
} else { |
| 299 |
debug(expected + " is unknown type " + typeof expected); |
| 300 |
shouldBeTrue(actual, "'" +expected.toString() + "'"); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
function shouldBeNonZero(_a) |
| 305 |
{ |
| 306 |
var exception; |
| 307 |
var _av; |
| 308 |
try { |
| 309 |
_av = eval(_a); |
| 310 |
} catch (e) { |
| 311 |
exception = e; |
| 312 |
} |
| 313 |
|
| 314 |
if (exception) |
| 315 |
testFailed(_a + " should be non-zero. Threw exception " + exception); |
| 316 |
else if (_av != 0) |
| 317 |
testPassed(_a + " is non-zero."); |
| 318 |
else |
| 319 |
testFailed(_a + " should be non-zero. Was " + _av); |
| 320 |
} |
| 321 |
|
| 322 |
function shouldBeNonNull(_a) |
| 323 |
{ |
| 324 |
var exception; |
| 325 |
var _av; |
| 326 |
try { |
| 327 |
_av = eval(_a); |
| 328 |
} catch (e) { |
| 329 |
exception = e; |
| 330 |
} |
| 331 |
|
| 332 |
if (exception) |
| 333 |
testFailed(_a + " should be non-null. Threw exception " + exception); |
| 334 |
else if (_av != null) |
| 335 |
testPassed(_a + " is non-null."); |
| 336 |
else |
| 337 |
testFailed(_a + " should be non-null. Was " + _av); |
| 338 |
} |
| 339 |
|
| 340 |
function shouldBeUndefined(_a) |
| 341 |
{ |
| 342 |
var exception; |
| 343 |
var _av; |
| 344 |
try { |
| 345 |
_av = eval(_a); |
| 346 |
} catch (e) { |
| 347 |
exception = e; |
| 348 |
} |
| 349 |
|
| 350 |
if (exception) |
| 351 |
testFailed(_a + " should be undefined. Threw exception " + exception); |
| 352 |
else if (typeof _av == "undefined") |
| 353 |
testPassed(_a + " is undefined."); |
| 354 |
else |
| 355 |
testFailed(_a + " should be undefined. Was " + _av); |
| 356 |
} |
| 357 |
|
| 358 |
function shouldBeDefined(_a) |
| 359 |
{ |
| 360 |
var exception; |
| 361 |
var _av; |
| 362 |
try { |
| 363 |
_av = eval(_a); |
| 364 |
} catch (e) { |
| 365 |
exception = e; |
| 366 |
} |
| 367 |
|
| 368 |
if (exception) |
| 369 |
testFailed(_a + " should be defined. Threw exception " + exception); |
| 370 |
else if (_av !== undefined) |
| 371 |
testPassed(_a + " is defined."); |
| 372 |
else |
| 373 |
testFailed(_a + " should be defined. Was " + _av); |
| 374 |
} |
| 375 |
|
| 376 |
function shouldBeGreaterThanOrEqual(_a, _b) { |
| 377 |
if (typeof _a != "string" || typeof _b != "string") |
| 378 |
debug("WARN: shouldBeGreaterThanOrEqual expects string arguments"); |
| 379 |
|
| 380 |
var exception; |
| 381 |
var _av; |
| 382 |
try { |
| 383 |
_av = eval(_a); |
| 384 |
} catch (e) { |
| 385 |
exception = e; |
| 386 |
} |
| 387 |
var _bv = eval(_b); |
| 388 |
|
| 389 |
if (exception) |
| 390 |
testFailed(_a + " should be >= " + _b + ". Threw exception " + exception); |
| 391 |
else if (typeof _av == "undefined" || _av < _bv) |
| 392 |
testFailed(_a + " should be >= " + _b + ". Was " + _av + " (of type " + typeof _av + ")."); |
| 393 |
else |
| 394 |
testPassed(_a + " is >= " + _b); |
| 395 |
} |
| 396 |
|
| 397 |
function shouldThrow(_a, _e) |
| 398 |
{ |
| 399 |
var exception; |
| 400 |
var _av; |
| 401 |
try { |
| 402 |
_av = eval(_a); |
| 403 |
} catch (e) { |
| 404 |
exception = e; |
| 405 |
} |
| 406 |
|
| 407 |
var _ev; |
| 408 |
if (_e) |
| 409 |
_ev = eval(_e); |
| 410 |
|
| 411 |
if (exception) { |
| 412 |
if (typeof _e == "undefined" || exception == _ev) |
| 413 |
testPassed(_a + " threw exception " + exception + "."); |
| 414 |
else |
| 415 |
testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Threw exception " + exception + "."); |
| 416 |
} else if (typeof _av == "undefined") |
| 417 |
testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was undefined."); |
| 418 |
else |
| 419 |
testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was " + _av + "."); |
| 420 |
} |
| 421 |
|
| 422 |
function shouldHaveHadError(message) |
| 423 |
{ |
| 424 |
if (errorMessage) { |
| 425 |
if (!message) |
| 426 |
testPassed("Got expected error"); |
| 427 |
else if (errorMessage.indexOf(message) !== -1) |
| 428 |
testPassed("Got expected error: '" + message + "'"); |
| 429 |
else |
| 430 |
testFailed("Unexpexted error '" + message + "'"); |
| 431 |
} else |
| 432 |
testFailed("Missing expexted error"); |
| 433 |
errorMessage = undefined; |
| 434 |
} |
| 435 |
|
| 436 |
function gc() { |
| 437 |
if (typeof GCController !== "undefined") |
| 438 |
GCController.collect(); |
| 439 |
else { |
| 440 |
var gcRec = function (n) { |
| 441 |
if (n < 1) |
| 442 |
return {}; |
| 443 |
var temp = {i: "ab" + i + (i / 100000)}; |
| 444 |
temp += "foo"; |
| 445 |
gcRec(n-1); |
| 446 |
}; |
| 447 |
for (var i = 0; i < 1000; i++) |
| 448 |
gcRec(10) |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
function isSuccessfullyParsed() |
| 453 |
{ |
| 454 |
// FIXME: Remove this and only report unexpected syntax errors. |
| 455 |
if (!errorMessage) |
| 456 |
successfullyParsed = true; |
| 457 |
shouldBeTrue("successfullyParsed"); |
| 458 |
debug('<br /><span class="pass">TEST COMPLETE</span>'); |
| 459 |
} |
| 460 |
|
| 461 |
// It's possible for an async test to call finishJSTest() before js-test-post.js |
| 462 |
// has been parsed. |
| 463 |
function finishJSTest() |
| 464 |
{ |
| 465 |
wasFinishJSTestCalled = true; |
| 466 |
if (!self.wasPostTestScriptParsed) |
| 467 |
return; |
| 468 |
isSuccessfullyParsed(); |
| 469 |
if (self.jsTestIsAsync && self.layoutTestController) |
| 470 |
layoutTestController.notifyDone(); |
| 471 |
} |
| 472 |
|
| 473 |
function startWorker(testScriptURL, shared) |
| 474 |
{ |
| 475 |
self.jsTestIsAsync = true; |
| 476 |
debug('Starting worker: ' + testScriptURL); |
| 477 |
var worker = shared ? new SharedWorker(testScriptURL) : new Worker(testScriptURL); |
| 478 |
if (shared) |
| 479 |
worker.port.onmessage = function(event) { worker.onmessage(event); }; |
| 480 |
worker.onmessage = function(event) |
| 481 |
{ |
| 482 |
var workerPrefix = "[Worker] "; |
| 483 |
if (event.data.length < 5 || event.data.charAt(4) != ':') { |
| 484 |
debug(workerPrefix + event.data); |
| 485 |
return; |
| 486 |
} |
| 487 |
var code = event.data.substring(0, 4); |
| 488 |
var payload = workerPrefix + event.data.substring(5); |
| 489 |
if (code == "PASS") |
| 490 |
testPassed(payload); |
| 491 |
else if (code == "FAIL") |
| 492 |
testFailed(payload); |
| 493 |
else if (code == "DESC") |
| 494 |
description(payload); |
| 495 |
else if (code == "DONE") |
| 496 |
finishJSTest(); |
| 497 |
else |
| 498 |
debug(workerPrefix + event.data); |
| 499 |
}; |
| 500 |
|
| 501 |
worker.onerror = function(event) |
| 502 |
{ |
| 503 |
debug('Got error from worker: ' + event.message); |
| 504 |
finishJSTest(); |
| 505 |
} |
| 506 |
|
| 507 |
return worker; |
| 508 |
} |
| 509 |
|
| 510 |
if (isWorker()) { |
| 511 |
description = function(msg, quiet) { |
| 512 |
postMessage('DESC:' + msg); |
| 513 |
} |
| 514 |
testFailed = function(msg) { |
| 515 |
postMessage('FAIL:' + msg); |
| 516 |
} |
| 517 |
testPassed = function(msg) { |
| 518 |
postMessage('PASS:' + msg); |
| 519 |
} |
| 520 |
finishJSTest = function() { |
| 521 |
postMessage('DONE:'); |
| 522 |
} |
| 523 |
debug = function(msg) { |
| 524 |
postMessage(msg); |
| 525 |
} |
| 526 |
} |