OLD | NEW |
(Empty) | |
| 1 /* |
| 2 Copyright (C) 2009 Apple Computer, Inc. All rights reserved. |
| 3 |
| 4 Redistribution and use in source and binary forms, with or without |
| 5 modification, are permitted provided that the following conditions |
| 6 are met: |
| 7 1. Redistributions of source code must retain the above copyright |
| 8 notice, this list of conditions and the following disclaimer. |
| 9 2. Redistributions in binary form must reproduce the above copyright |
| 10 notice, this list of conditions and the following disclaimer in the |
| 11 documentation and/or other materials provided with the distribution. |
| 12 |
| 13 THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 14 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ |
| 25 |
| 26 // WebKit Specfic code. Add your own here. |
| 27 function initNonKhronosFramework(waitUntilDone) { |
| 28 if (window.layoutTestController) { |
| 29 layoutTestController.overridePreference("WebKitWebGLEnabled", "1"); |
| 30 layoutTestController.dumpAsText(); |
| 31 if (waitUntilDone) { |
| 32 layoutTestController.waitUntilDone(); |
| 33 } |
| 34 } |
| 35 } |
| 36 |
| 37 function nonKhronosFrameworkNotifyDone() { |
| 38 if (window.layoutTestController) { |
| 39 layoutTestController.notifyDone(); |
| 40 } |
| 41 } |
| 42 |
| 43 function reportTestResultsToHarness(success, msg) { |
| 44 if (window.parent.webglTestHarness) { |
| 45 window.parent.webglTestHarness.reportResults(success, msg); |
| 46 } |
| 47 } |
| 48 |
| 49 function notifyFinishedToHarness() { |
| 50 if (window.parent.webglTestHarness) { |
| 51 window.parent.webglTestHarness.notifyFinished(); |
| 52 } |
| 53 } |
| 54 |
| 55 function description(msg) |
| 56 { |
| 57 // For MSIE 6 compatibility |
| 58 var span = document.createElement("span"); |
| 59 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>'; |
| 60 var description = document.getElementById("description"); |
| 61 if (description.firstChild) |
| 62 description.replaceChild(span, description.firstChild); |
| 63 else |
| 64 description.appendChild(span); |
| 65 } |
| 66 |
| 67 function debug(msg) |
| 68 { |
| 69 var span = document.createElement("span"); |
| 70 document.getElementById("console").appendChild(span); // insert it first so
XHTML knows the namespace |
| 71 span.innerHTML = msg + '<br />'; |
| 72 } |
| 73 |
| 74 function escapeHTML(text) |
| 75 { |
| 76 return text.replace(/&/g, "&").replace(/</g, "<"); |
| 77 } |
| 78 |
| 79 function testPassed(msg) |
| 80 { |
| 81 reportTestResultsToHarness(true, msg); |
| 82 debug('<span><span class="pass">PASS</span> ' + escapeHTML(msg) + '</span>')
; |
| 83 } |
| 84 |
| 85 function testFailed(msg) |
| 86 { |
| 87 reportTestResultsToHarness(false, msg); |
| 88 debug('<span><span class="fail">FAIL</span> ' + escapeHTML(msg) + '</span>')
; |
| 89 } |
| 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 function isMinusZero(n) |
| 106 { |
| 107 // the only way to tell 0 from -0 in JS is the fact that 1/-0 is |
| 108 // -Infinity instead of Infinity |
| 109 return n === 0 && 1/n < 0; |
| 110 } |
| 111 |
| 112 function isResultCorrect(_actual, _expected) |
| 113 { |
| 114 if (_expected === 0) |
| 115 return _actual === _expected && (1/_actual) === (1/_expected); |
| 116 if (_actual === _expected) |
| 117 return true; |
| 118 if (typeof(_expected) == "number" && isNaN(_expected)) |
| 119 return typeof(_actual) == "number" && isNaN(_actual); |
| 120 if (Object.prototype.toString.call(_expected) == Object.prototype.toString.c
all([])) |
| 121 return areArraysEqual(_actual, _expected); |
| 122 return false; |
| 123 } |
| 124 |
| 125 function stringify(v) |
| 126 { |
| 127 if (v === 0 && 1/v < 0) |
| 128 return "-0"; |
| 129 else return "" + v; |
| 130 } |
| 131 |
| 132 function evalAndLog(_a) |
| 133 { |
| 134 if (typeof _a != "string") |
| 135 debug("WARN: tryAndLog() expects a string argument"); |
| 136 |
| 137 // Log first in case things go horribly wrong or this causes a sync event. |
| 138 debug(_a); |
| 139 |
| 140 var _av; |
| 141 try { |
| 142 _av = eval(_a); |
| 143 } catch (e) { |
| 144 testFailed(_a + " threw exception " + e); |
| 145 } |
| 146 return _av; |
| 147 } |
| 148 |
| 149 function shouldBe(_a, _b, quiet) |
| 150 { |
| 151 if (typeof _a != "string" || typeof _b != "string") |
| 152 debug("WARN: shouldBe() expects string arguments"); |
| 153 var exception; |
| 154 var _av; |
| 155 try { |
| 156 _av = eval(_a); |
| 157 } catch (e) { |
| 158 exception = e; |
| 159 } |
| 160 var _bv = eval(_b); |
| 161 |
| 162 if (exception) |
| 163 testFailed(_a + " should be " + _bv + ". Threw exception " + exception); |
| 164 else if (isResultCorrect(_av, _bv)) { |
| 165 if (!quiet) { |
| 166 testPassed(_a + " is " + _b); |
| 167 } |
| 168 } else if (typeof(_av) == typeof(_bv)) |
| 169 testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + "."); |
| 170 else |
| 171 testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Wa
s " + _av + " (of type " + typeof _av + ")."); |
| 172 } |
| 173 |
| 174 function shouldNotBe(_a, _b, quiet) |
| 175 { |
| 176 if (typeof _a != "string" || typeof _b != "string") |
| 177 debug("WARN: shouldNotBe() expects string arguments"); |
| 178 var exception; |
| 179 var _av; |
| 180 try { |
| 181 _av = eval(_a); |
| 182 } catch (e) { |
| 183 exception = e; |
| 184 } |
| 185 var _bv = eval(_b); |
| 186 |
| 187 if (exception) |
| 188 testFailed(_a + " should not be " + _bv + ". Threw exception " + excepti
on); |
| 189 else if (!isResultCorrect(_av, _bv)) { |
| 190 if (!quiet) { |
| 191 testPassed(_a + " is not " + _b); |
| 192 } |
| 193 } else |
| 194 testFailed(_a + " should not be " + _bv + "."); |
| 195 } |
| 196 |
| 197 function shouldBeTrue(_a) { shouldBe(_a, "true"); } |
| 198 function shouldBeFalse(_a) { shouldBe(_a, "false"); } |
| 199 function shouldBeNaN(_a) { shouldBe(_a, "NaN"); } |
| 200 function shouldBeNull(_a) { shouldBe(_a, "null"); } |
| 201 |
| 202 function shouldBeEqualToString(a, b) |
| 203 { |
| 204 var unevaledString = '"' + b.replace(/"/g, "\"") + '"'; |
| 205 shouldBe(a, unevaledString); |
| 206 } |
| 207 |
| 208 function shouldEvaluateTo(actual, expected) { |
| 209 // A general-purpose comparator. 'actual' should be a string to be |
| 210 // evaluated, as for shouldBe(). 'expected' may be any type and will be |
| 211 // used without being eval'ed. |
| 212 if (expected == null) { |
| 213 // Do this before the object test, since null is of type 'object'. |
| 214 shouldBeNull(actual); |
| 215 } else if (typeof expected == "undefined") { |
| 216 shouldBeUndefined(actual); |
| 217 } else if (typeof expected == "function") { |
| 218 // All this fuss is to avoid the string-arg warning from shouldBe(). |
| 219 try { |
| 220 actualValue = eval(actual); |
| 221 } catch (e) { |
| 222 testFailed("Evaluating " + actual + ": Threw exception " + e); |
| 223 return; |
| 224 } |
| 225 shouldBe("'" + actualValue.toString().replace(/\n/g, "") + "'", |
| 226 "'" + expected.toString().replace(/\n/g, "") + "'"); |
| 227 } else if (typeof expected == "object") { |
| 228 shouldBeTrue(actual + " == '" + expected + "'"); |
| 229 } else if (typeof expected == "string") { |
| 230 shouldBe(actual, expected); |
| 231 } else if (typeof expected == "boolean") { |
| 232 shouldBe("typeof " + actual, "'boolean'"); |
| 233 if (expected) |
| 234 shouldBeTrue(actual); |
| 235 else |
| 236 shouldBeFalse(actual); |
| 237 } else if (typeof expected == "number") { |
| 238 shouldBe(actual, stringify(expected)); |
| 239 } else { |
| 240 debug(expected + " is unknown type " + typeof expected); |
| 241 shouldBeTrue(actual, "'" +expected.toString() + "'"); |
| 242 } |
| 243 } |
| 244 |
| 245 function shouldBeNonZero(_a) |
| 246 { |
| 247 var exception; |
| 248 var _av; |
| 249 try { |
| 250 _av = eval(_a); |
| 251 } catch (e) { |
| 252 exception = e; |
| 253 } |
| 254 |
| 255 if (exception) |
| 256 testFailed(_a + " should be non-zero. Threw exception " + exception); |
| 257 else if (_av != 0) |
| 258 testPassed(_a + " is non-zero."); |
| 259 else |
| 260 testFailed(_a + " should be non-zero. Was " + _av); |
| 261 } |
| 262 |
| 263 function shouldBeNonNull(_a) |
| 264 { |
| 265 var exception; |
| 266 var _av; |
| 267 try { |
| 268 _av = eval(_a); |
| 269 } catch (e) { |
| 270 exception = e; |
| 271 } |
| 272 |
| 273 if (exception) |
| 274 testFailed(_a + " should be non-null. Threw exception " + exception); |
| 275 else if (_av != null) |
| 276 testPassed(_a + " is non-null."); |
| 277 else |
| 278 testFailed(_a + " should be non-null. Was " + _av); |
| 279 } |
| 280 |
| 281 function shouldBeUndefined(_a) |
| 282 { |
| 283 var exception; |
| 284 var _av; |
| 285 try { |
| 286 _av = eval(_a); |
| 287 } catch (e) { |
| 288 exception = e; |
| 289 } |
| 290 |
| 291 if (exception) |
| 292 testFailed(_a + " should be undefined. Threw exception " + exception); |
| 293 else if (typeof _av == "undefined") |
| 294 testPassed(_a + " is undefined."); |
| 295 else |
| 296 testFailed(_a + " should be undefined. Was " + _av); |
| 297 } |
| 298 |
| 299 function shouldBeDefined(_a) |
| 300 { |
| 301 var exception; |
| 302 var _av; |
| 303 try { |
| 304 _av = eval(_a); |
| 305 } catch (e) { |
| 306 exception = e; |
| 307 } |
| 308 |
| 309 if (exception) |
| 310 testFailed(_a + " should be defined. Threw exception " + exception); |
| 311 else if (_av !== undefined) |
| 312 testPassed(_a + " is defined."); |
| 313 else |
| 314 testFailed(_a + " should be defined. Was " + _av); |
| 315 } |
| 316 |
| 317 function shouldBeGreaterThanOrEqual(_a, _b) { |
| 318 if (typeof _a != "string" || typeof _b != "string") |
| 319 debug("WARN: shouldBeGreaterThanOrEqual expects string arguments"); |
| 320 |
| 321 var exception; |
| 322 var _av; |
| 323 try { |
| 324 _av = eval(_a); |
| 325 } catch (e) { |
| 326 exception = e; |
| 327 } |
| 328 var _bv = eval(_b); |
| 329 |
| 330 if (exception) |
| 331 testFailed(_a + " should be >= " + _b + ". Threw exception " + exception
); |
| 332 else if (typeof _av == "undefined" || _av < _bv) |
| 333 testFailed(_a + " should be >= " + _b + ". Was " + _av + " (of type " +
typeof _av + ")."); |
| 334 else |
| 335 testPassed(_a + " is >= " + _b); |
| 336 } |
| 337 |
| 338 function shouldThrow(_a, _e) |
| 339 { |
| 340 var exception; |
| 341 var _av; |
| 342 try { |
| 343 _av = eval(_a); |
| 344 } catch (e) { |
| 345 exception = e; |
| 346 } |
| 347 |
| 348 var _ev; |
| 349 if (_e) |
| 350 _ev = eval(_e); |
| 351 |
| 352 if (exception) { |
| 353 if (typeof _e == "undefined" || exception == _ev) |
| 354 testPassed(_a + " threw exception " + exception + "."); |
| 355 else |
| 356 testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an excepti
on" : _ev) + ". Threw exception " + exception + "."); |
| 357 } else if (typeof _av == "undefined") |
| 358 testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception
" : _ev) + ". Was undefined."); |
| 359 else |
| 360 testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception
" : _ev) + ". Was " + _av + "."); |
| 361 } |
| 362 |
| 363 function assertMsg(assertion, msg) { |
| 364 if (assertion) { |
| 365 testPassed(msg); |
| 366 } else { |
| 367 testFailed(msg); |
| 368 } |
| 369 } |
| 370 |
| 371 function gc() { |
| 372 if (typeof GCController !== "undefined") |
| 373 GCController.collect(); |
| 374 else { |
| 375 function gcRec(n) { |
| 376 if (n < 1) |
| 377 return {}; |
| 378 var temp = {i: "ab" + i + (i / 100000)}; |
| 379 temp += "foo"; |
| 380 gcRec(n-1); |
| 381 } |
| 382 for (var i = 0; i < 1000; i++) |
| 383 gcRec(10) |
| 384 } |
| 385 } |
| 386 |
| 387 function finishTest() { |
| 388 successfullyParsed = true; |
| 389 var epilogue = document.createElement("script"); |
| 390 epilogue.onload = function() { |
| 391 if (window.nonKhronosFrameworkNotifyDone) { |
| 392 window.nonKhronosFrameworkNotifyDone(); |
| 393 } |
| 394 }; |
| 395 |
| 396 var basePath = ""; |
| 397 var expectedBase = "js-test-pre.js"; |
| 398 var scripts = document.getElementsByTagName('script'); |
| 399 for (var script, i = 0; script = scripts[i]; i++) { |
| 400 var src = script.src; |
| 401 var l = src.length; |
| 402 if (src.substr(l - expectedBase.length) == expectedBase) { |
| 403 basePath = src.substr(0, l - expectedBase.length); |
| 404 break; |
| 405 } |
| 406 } |
| 407 epilogue.src = basePath + "js-test-post.js"; |
| 408 document.body.appendChild(epilogue); |
| 409 } |
| 410 |
OLD | NEW |