| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * Test controller logic - used by unit test harness to embed tests in | |
| 7 * DumpRenderTree. | |
| 8 */ | |
| 9 | |
| 10 // Clear the console before every test run - this is Firebug specific code. | |
| 11 if (typeof console == "object" && typeof console.clear == "function") { | |
| 12 console.clear(); | |
| 13 } | |
| 14 // Set window onerror to make sure that we catch test harness errors across all | |
| 15 // browsers. | |
| 16 window.onerror = function (message, url, lineNumber) { | |
| 17 if (url) { | |
| 18 showErrorAndExit( | |
| 19 "\n\n" + url + ":" + lineNumber + ":\n" + message + "\n\n"); | |
| 20 } else { | |
| 21 showErrorAndExit(message); | |
| 22 } | |
| 23 window.postMessage('unittest-suite-external-error', '*'); | |
| 24 }; | |
| 25 | |
| 26 if (navigator.webkitStartDart) { | |
| 27 navigator.webkitStartDart(); | |
| 28 } | |
| 29 | |
| 30 // testRunner is provided by DRT or WebKit's layout tests. | |
| 31 // It is not available in selenium tests. | |
| 32 var testRunner = window.testRunner || window.layoutTestController; | |
| 33 | |
| 34 var waitForDone = false; | |
| 35 | |
| 36 function processMessage(msg) { | |
| 37 if (typeof msg != 'string') return; | |
| 38 if (msg == 'unittest-suite-done') { | |
| 39 if (testRunner) testRunner.notifyDone(); | |
| 40 } else if (msg == 'unittest-suite-wait-for-done') { | |
| 41 waitForDone = true; | |
| 42 if (testRunner) testRunner.startedDartTest = true; | |
| 43 } else if (msg == 'dart-calling-main') { | |
| 44 if (testRunner) testRunner.startedDartTest = true; | |
| 45 } else if (msg == 'dart-main-done') { | |
| 46 if (!waitForDone) { | |
| 47 window.postMessage('unittest-suite-success', '*'); | |
| 48 } | |
| 49 } else if (msg == 'unittest-suite-success') { | |
| 50 dartPrint('PASS'); | |
| 51 if (testRunner) testRunner.notifyDone(); | |
| 52 } else if (msg == 'unittest-suite-fail') { | |
| 53 showErrorAndExit('Some tests failed.'); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 function onReceive(e) { | |
| 58 processMessage(e.data); | |
| 59 } | |
| 60 | |
| 61 if (testRunner) { | |
| 62 testRunner.dumpAsText(); | |
| 63 testRunner.waitUntilDone(); | |
| 64 } | |
| 65 window.addEventListener("message", onReceive, false); | |
| 66 | |
| 67 function showErrorAndExit(message) { | |
| 68 if (message) { | |
| 69 dartPrint('Error: ' + String(message)); | |
| 70 } | |
| 71 // dart/tools/testing/run_selenium.py is looking for either PASS or | |
| 72 // FAIL and will continue polling until one of these words show up. | |
| 73 dartPrint('FAIL'); | |
| 74 if (testRunner) testRunner.notifyDone(); | |
| 75 } | |
| 76 | |
| 77 function onLoad(e) { | |
| 78 // needed for dartium compilation errors. | |
| 79 if (window.compilationError) { | |
| 80 showErrorAndExit(window.compilationError); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 window.addEventListener("DOMContentLoaded", onLoad, false); | |
| 85 | |
| 86 // Note: before renaming this function, note that it is also included in an | |
| 87 // inlined error handler in the HTML files that wrap DRT tests. | |
| 88 // See: tools/testing/dart/browser_test.dart | |
| 89 function externalError(e) { | |
| 90 // needed for dartium compilation errors. | |
| 91 showErrorAndExit(e && e.message); | |
| 92 window.postMessage('unittest-suite-external-error', '*'); | |
| 93 } | |
| 94 | |
| 95 // If nobody intercepts the error, finish the test. | |
| 96 window.addEventListener("error", externalError, false); | |
| 97 | |
| 98 document.addEventListener('readystatechange', function () { | |
| 99 if (document.readyState != "loaded") return; | |
| 100 // If 'startedDartTest' is not set, that means that the test did not have | |
| 101 // a chance to load. This will happen when a load error occurs in the VM. | |
| 102 // Give the machine time to start up. | |
| 103 setTimeout(function() { | |
| 104 // A window.postMessage might have been enqueued after this timeout. | |
| 105 // Just sleep another time to give the browser the time to process the | |
| 106 // posted message. | |
| 107 setTimeout(function() { | |
| 108 if (testRunner && !testRunner.startedDartTest) { | |
| 109 testRunner.notifyDone(); | |
| 110 } | |
| 111 }, 0); | |
| 112 }, 50); | |
| 113 }); | |
| 114 | |
| 115 // dart2js will generate code to call this function to handle the Dart | |
| 116 // [print] method. The base [Configuration] (config.html) calls | |
| 117 // [print] with the secret messages "unittest-suite-success" and | |
| 118 // "unittest-suite-wait-for-done". These messages are then posted so | |
| 119 // processMessage above will see them. | |
| 120 function dartPrint(msg) { | |
| 121 if ((msg === 'unittest-suite-success') | |
| 122 || (msg === 'unittest-suite-wait-for-done')) { | |
| 123 window.postMessage(msg, '*'); | |
| 124 return; | |
| 125 } | |
| 126 var pre = document.createElement("pre"); | |
| 127 pre.appendChild(document.createTextNode(String(msg))); | |
| 128 document.body.appendChild(pre); | |
| 129 } | |
| 130 | |
| 131 // dart2js will generate code to call this function instead of calling | |
| 132 // Dart [main] directly. The argument is a closure that invokes main. | |
| 133 function dartMainRunner(main) { | |
| 134 window.postMessage('dart-calling-main', '*'); | |
| 135 try { | |
| 136 main(); | |
| 137 } catch (e) { | |
| 138 window.postMessage('unittest-suite-fail', '*'); | |
| 139 return; | |
| 140 } | |
| 141 window.postMessage('dart-main-done', '*'); | |
| 142 } | |
| OLD | NEW |