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) |
| 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 testPassed(_a + " is " + _b); |
| 166 else if (typeof(_av) == typeof(_bv)) |
| 167 testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + "."); |
| 168 else |
| 169 testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Was "
+ _av + " (of type " + typeof _av + ")."); |
| 170 } |
| 171 |
| 172 function shouldBeTrue(_a) { shouldBe(_a, "true"); } |
| 173 function shouldBeFalse(_a) { shouldBe(_a, "false"); } |
| 174 function shouldBeNaN(_a) { shouldBe(_a, "NaN"); } |
| 175 function shouldBeNull(_a) { shouldBe(_a, "null"); } |
| 176 |
| 177 function shouldBeEqualToString(a, b) |
| 178 { |
| 179 var unevaledString = '"' + b.replace(/"/g, "\"") + '"'; |
| 180 shouldBe(a, unevaledString); |
| 181 } |
| 182 |
| 183 function shouldEvaluateTo(actual, expected) { |
| 184 // A general-purpose comparator. 'actual' should be a string to be |
| 185 // evaluated, as for shouldBe(). 'expected' may be any type and will be |
| 186 // used without being eval'ed. |
| 187 if (expected == null) { |
| 188 // Do this before the object test, since null is of type 'object'. |
| 189 shouldBeNull(actual); |
| 190 } else if (typeof expected == "undefined") { |
| 191 shouldBeUndefined(actual); |
| 192 } else if (typeof expected == "function") { |
| 193 // All this fuss is to avoid the string-arg warning from shouldBe(). |
| 194 try { |
| 195 actualValue = eval(actual); |
| 196 } catch (e) { |
| 197 testFailed("Evaluating " + actual + ": Threw exception " + e); |
| 198 return; |
| 199 } |
| 200 shouldBe("'" + actualValue.toString().replace(/\n/g, "") + "'", |
| 201 "'" + expected.toString().replace(/\n/g, "") + "'"); |
| 202 } else if (typeof expected == "object") { |
| 203 shouldBeTrue(actual + " == '" + expected + "'"); |
| 204 } else if (typeof expected == "string") { |
| 205 shouldBe(actual, expected); |
| 206 } else if (typeof expected == "boolean") { |
| 207 shouldBe("typeof " + actual, "'boolean'"); |
| 208 if (expected) |
| 209 shouldBeTrue(actual); |
| 210 else |
| 211 shouldBeFalse(actual); |
| 212 } else if (typeof expected == "number") { |
| 213 shouldBe(actual, stringify(expected)); |
| 214 } else { |
| 215 debug(expected + " is unknown type " + typeof expected); |
| 216 shouldBeTrue(actual, "'" +expected.toString() + "'"); |
| 217 } |
| 218 } |
| 219 |
| 220 function shouldBeNonZero(_a) |
| 221 { |
| 222 var exception; |
| 223 var _av; |
| 224 try { |
| 225 _av = eval(_a); |
| 226 } catch (e) { |
| 227 exception = e; |
| 228 } |
| 229 |
| 230 if (exception) |
| 231 testFailed(_a + " should be non-zero. Threw exception " + exception); |
| 232 else if (_av != 0) |
| 233 testPassed(_a + " is non-zero."); |
| 234 else |
| 235 testFailed(_a + " should be non-zero. Was " + _av); |
| 236 } |
| 237 |
| 238 function shouldBeNonNull(_a) |
| 239 { |
| 240 var exception; |
| 241 var _av; |
| 242 try { |
| 243 _av = eval(_a); |
| 244 } catch (e) { |
| 245 exception = e; |
| 246 } |
| 247 |
| 248 if (exception) |
| 249 testFailed(_a + " should be non-null. Threw exception " + exception); |
| 250 else if (_av != null) |
| 251 testPassed(_a + " is non-null."); |
| 252 else |
| 253 testFailed(_a + " should be non-null. Was " + _av); |
| 254 } |
| 255 |
| 256 function shouldBeUndefined(_a) |
| 257 { |
| 258 var exception; |
| 259 var _av; |
| 260 try { |
| 261 _av = eval(_a); |
| 262 } catch (e) { |
| 263 exception = e; |
| 264 } |
| 265 |
| 266 if (exception) |
| 267 testFailed(_a + " should be undefined. Threw exception " + exception); |
| 268 else if (typeof _av == "undefined") |
| 269 testPassed(_a + " is undefined."); |
| 270 else |
| 271 testFailed(_a + " should be undefined. Was " + _av); |
| 272 } |
| 273 |
| 274 function shouldBeDefined(_a) |
| 275 { |
| 276 var exception; |
| 277 var _av; |
| 278 try { |
| 279 _av = eval(_a); |
| 280 } catch (e) { |
| 281 exception = e; |
| 282 } |
| 283 |
| 284 if (exception) |
| 285 testFailed(_a + " should be defined. Threw exception " + exception); |
| 286 else if (_av !== undefined) |
| 287 testPassed(_a + " is defined."); |
| 288 else |
| 289 testFailed(_a + " should be defined. Was " + _av); |
| 290 } |
| 291 |
| 292 function shouldBeGreaterThanOrEqual(_a, _b) { |
| 293 if (typeof _a != "string" || typeof _b != "string") |
| 294 debug("WARN: shouldBeGreaterThanOrEqual expects string arguments"); |
| 295 |
| 296 var exception; |
| 297 var _av; |
| 298 try { |
| 299 _av = eval(_a); |
| 300 } catch (e) { |
| 301 exception = e; |
| 302 } |
| 303 var _bv = eval(_b); |
| 304 |
| 305 if (exception) |
| 306 testFailed(_a + " should be >= " + _b + ". Threw exception " + exception
); |
| 307 else if (typeof _av == "undefined" || _av < _bv) |
| 308 testFailed(_a + " should be >= " + _b + ". Was " + _av + " (of type " +
typeof _av + ")."); |
| 309 else |
| 310 testPassed(_a + " is >= " + _b); |
| 311 } |
| 312 |
| 313 function shouldThrow(_a, _e) |
| 314 { |
| 315 var exception; |
| 316 var _av; |
| 317 try { |
| 318 _av = eval(_a); |
| 319 } catch (e) { |
| 320 exception = e; |
| 321 } |
| 322 |
| 323 var _ev; |
| 324 if (_e) |
| 325 _ev = eval(_e); |
| 326 |
| 327 if (exception) { |
| 328 if (typeof _e == "undefined" || exception == _ev) |
| 329 testPassed(_a + " threw exception " + exception + "."); |
| 330 else |
| 331 testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an excepti
on" : _ev) + ". Threw exception " + exception + "."); |
| 332 } else if (typeof _av == "undefined") |
| 333 testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception
" : _ev) + ". Was undefined."); |
| 334 else |
| 335 testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception
" : _ev) + ". Was " + _av + "."); |
| 336 } |
| 337 |
| 338 function assertMsg(assertion, msg) { |
| 339 if (assertion) { |
| 340 testPassed(msg); |
| 341 } else { |
| 342 testFailed(msg); |
| 343 } |
| 344 } |
| 345 |
| 346 function gc() { |
| 347 if (typeof GCController !== "undefined") |
| 348 GCController.collect(); |
| 349 else { |
| 350 function gcRec(n) { |
| 351 if (n < 1) |
| 352 return {}; |
| 353 var temp = {i: "ab" + i + (i / 100000)}; |
| 354 temp += "foo"; |
| 355 gcRec(n-1); |
| 356 } |
| 357 for (var i = 0; i < 1000; i++) |
| 358 gcRec(10) |
| 359 } |
| 360 } |
| 361 |
| 362 function finishTest() { |
| 363 successfullyParsed = true; |
| 364 var epilogue = document.createElement("script"); |
| 365 epilogue.onload = function() { |
| 366 if (window.nonKhronosFrameworkNotifyDone) { |
| 367 window.nonKhronosFrameworkNotifyDone(); |
| 368 } |
| 369 }; |
| 370 |
| 371 var basePath = ""; |
| 372 var expectedBase = "js-test-pre.js"; |
| 373 var scripts = document.getElementsByTagName('script'); |
| 374 for (var script, i = 0; script = scripts[i]; i++) { |
| 375 var src = script.src; |
| 376 var l = src.length; |
| 377 if (src.substr(l - expectedBase.length) == expectedBase) { |
| 378 basePath = src.substr(0, l - expectedBase.length); |
| 379 break; |
| 380 } |
| 381 } |
| 382 epilogue.src = basePath + "js-test-post.js"; |
| 383 document.body.appendChild(epilogue); |
| 384 } |
| 385 |
OLD | NEW |