| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Test controller logic - used by unit test harness to embed tests in | 6 * Test controller logic - used by unit test harness to embed tests in |
| 7 * conent shell. | 7 * conent shell. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 // Clear the console before every test run - this is Firebug specific code. | 10 // Clear the console before every test run - this is Firebug specific code. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 // We call notifyStart here to notify the encapsulating browser. | 62 // We call notifyStart here to notify the encapsulating browser. |
| 63 notifyStart(); | 63 notifyStart(); |
| 64 | 64 |
| 65 function notifyDone() { | 65 function notifyDone() { |
| 66 if (testRunner) testRunner.notifyDone(); | 66 if (testRunner) testRunner.notifyDone(); |
| 67 | 67 |
| 68 // TODO(ricow): REMOVE, debug info, see issue 13292 | 68 // TODO(ricow): REMOVE, debug info, see issue 13292 |
| 69 if (!testRunner) { | 69 if (!testRunner) { |
| 70 dartPrint('Calling notifyDone()'); | 70 printMessage('Calling notifyDone()'); |
| 71 } | 71 } |
| 72 // To support in browser launching of tests we post back start and result | 72 // To support in browser launching of tests we post back start and result |
| 73 // messages to the window.opener. | 73 // messages to the window.opener. |
| 74 var driver = getDriverWindow(); | 74 var driver = getDriverWindow(); |
| 75 if (driver) { | 75 if (driver) { |
| 76 driver.postMessage(window.document.body.innerHTML, "*"); | 76 driver.postMessage(window.document.body.innerHTML, "*"); |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 | 79 |
| 80 function processMessage(msg) { | 80 function processMessage(msg) { |
| 81 // TODO(ricow): REMOVE, debug info, see issue 13292 | 81 // TODO(ricow): REMOVE, debug info, see issue 13292 |
| 82 if (!testRunner) { | 82 if (!testRunner) { |
| 83 dartPrint('processMessage(): ' + msg); | 83 printMessage('processMessage(): ' + msg); |
| 84 } | 84 } |
| 85 if (typeof msg != 'string') return; | 85 if (typeof msg != 'string') return; |
| 86 if (msg == 'unittest-suite-done') { | 86 if (msg == 'unittest-suite-done') { |
| 87 notifyDone(); | 87 notifyDone(); |
| 88 } else if (msg == 'unittest-suite-wait-for-done') { | 88 } else if (msg == 'unittest-suite-wait-for-done') { |
| 89 waitForDone = true; | 89 waitForDone = true; |
| 90 if (testRunner) testRunner.startedDartTest = true; | 90 if (testRunner) testRunner.startedDartTest = true; |
| 91 } else if (msg == 'dart-calling-main') { | 91 } else if (msg == 'dart-calling-main') { |
| 92 if (testRunner) testRunner.startedDartTest = true; | 92 if (testRunner) testRunner.startedDartTest = true; |
| 93 } else if (msg == 'dart-main-done') { | 93 } else if (msg == 'dart-main-done') { |
| 94 if (!waitForDone) { | 94 if (!waitForDone) { |
| 95 window.postMessage('unittest-suite-success', '*'); | 95 window.postMessage('unittest-suite-success', '*'); |
| 96 } | 96 } |
| 97 } else if (msg == 'unittest-suite-success') { | 97 } else if (msg == 'unittest-suite-success') { |
| 98 dartPrint('PASS'); | 98 printMessage('PASS'); |
| 99 notifyDone(); | 99 notifyDone(); |
| 100 } else if (msg == 'unittest-suite-fail') { | 100 } else if (msg == 'unittest-suite-fail') { |
| 101 showErrorAndExit('Some tests failed.'); | 101 showErrorAndExit('Some tests failed.'); |
| 102 } | 102 } |
| 103 } | 103 } |
| 104 | 104 |
| 105 function onReceive(e) { | 105 function onReceive(e) { |
| 106 processMessage(e.data); | 106 processMessage(e.data); |
| 107 } | 107 } |
| 108 | 108 |
| 109 if (testRunner) { | 109 if (testRunner) { |
| 110 testRunner.dumpAsText(); | 110 testRunner.dumpAsText(); |
| 111 testRunner.waitUntilDone(); | 111 testRunner.waitUntilDone(); |
| 112 } | 112 } |
| 113 window.addEventListener("message", onReceive, false); | 113 window.addEventListener("message", onReceive, false); |
| 114 | 114 |
| 115 function showErrorAndExit(message) { | 115 function showErrorAndExit(message) { |
| 116 if (message) { | 116 if (message) { |
| 117 dartPrint('Error: ' + String(message)); | 117 printMessage('Error: ' + String(message)); |
| 118 } | 118 } |
| 119 // dart/tools/testing/run_selenium.py is looking for either PASS or | 119 // dart/tools/testing/run_selenium.py is looking for either PASS or |
| 120 // FAIL and will continue polling until one of these words show up. | 120 // FAIL and will continue polling until one of these words show up. |
| 121 dartPrint('FAIL'); | 121 printMessage('FAIL'); |
| 122 notifyDone(); | 122 notifyDone(); |
| 123 } | 123 } |
| 124 | 124 |
| 125 function onLoad(e) { | 125 function onLoad(e) { |
| 126 // needed for dartium compilation errors. | 126 // needed for dartium compilation errors. |
| 127 if (window.compilationError) { | 127 if (window.compilationError) { |
| 128 showErrorAndExit(window.compilationError); | 128 showErrorAndExit(window.compilationError); |
| 129 } | 129 } |
| 130 } | 130 } |
| 131 | 131 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 151 // posted message. | 151 // posted message. |
| 152 setTimeout(function() { | 152 setTimeout(function() { |
| 153 if (testRunner && !testRunner.startedDartTest) { | 153 if (testRunner && !testRunner.startedDartTest) { |
| 154 notifyDone(); | 154 notifyDone(); |
| 155 } | 155 } |
| 156 }, 0); | 156 }, 0); |
| 157 }, 50); | 157 }, 50); |
| 158 }); | 158 }); |
| 159 | 159 |
| 160 // dart2js will generate code to call this function to handle the Dart | 160 // dart2js will generate code to call this function to handle the Dart |
| 161 // [print] method. The base [Configuration] (config.html) calls | 161 // [print] method. |
| 162 // [print] with the secret messages "unittest-suite-success" and | 162 // |
| 163 // "unittest-suite-wait-for-done". These messages are then posted so | 163 // dartium will invoke this method for [print] calls if the environment variable |
| 164 // processMessage above will see them. | 164 // "DART_FORWARDING_PRINT" was set when launching dartium. |
| 165 // |
| 166 // Our tests will be wrapped, so we can detect when [main] is called and when |
| 167 // it has ended. |
| 168 // The wrapping happens either via "dartMainRunner" (for dart2js) or wrapped |
| 169 // tests for dartium. |
| 170 // |
| 171 // The following messages are handled specially: |
| 172 // dart-calling-main: signals that the dart [main] function will be invoked |
| 173 // dart-main-done: signals that the dart [main] function has finished |
| 174 // unittest-suite-wait-for-done: signals the start of an asynchronous test |
| 175 // unittest-suite-success: signals the end of an asynchrounous test |
| 176 // |
| 177 // These messages are used to communicate with the test and will be posted so |
| 178 // [processMessage] above can see it. |
| 165 function dartPrint(msg) { | 179 function dartPrint(msg) { |
| 166 if ((msg === 'unittest-suite-success') | 180 if ((msg === 'unittest-suite-success') |
| 167 || (msg === 'unittest-suite-done') | 181 || (msg === 'unittest-suite-done') |
| 168 || (msg === 'unittest-suite-wait-for-done')) { | 182 || (msg === 'unittest-suite-wait-for-done') |
| 183 || (msg === 'dart-calling-main') |
| 184 || (msg === 'dart-main-done')) { |
| 169 window.postMessage(msg, '*'); | 185 window.postMessage(msg, '*'); |
| 170 return; | 186 return; |
| 171 } | 187 } |
| 188 printMessage(msg); |
| 189 } |
| 190 |
| 191 // Prints 'msg' to the console (if available) and to the body of the html |
| 192 // document. |
| 193 function printMessage(msg) { |
| 172 if (typeof console === 'object') console.warn(msg); | 194 if (typeof console === 'object') console.warn(msg); |
| 173 var pre = document.createElement("pre"); | 195 var pre = document.createElement('pre'); |
| 174 pre.appendChild(document.createTextNode(String(msg))); | 196 pre.appendChild(document.createTextNode(String(msg))); |
| 175 document.body.appendChild(pre); | 197 document.body.appendChild(pre); |
| 198 document.body.appendChild(document.createTextNode('\n')); |
| 176 } | 199 } |
| 177 | 200 |
| 178 // dart2js will generate code to call this function instead of calling | 201 // dart2js will generate code to call this function instead of calling |
| 179 // Dart [main] directly. The argument is a closure that invokes main. | 202 // Dart [main] directly. The argument is a closure that invokes main. |
| 180 function dartMainRunner(main) { | 203 function dartMainRunner(main) { |
| 181 window.postMessage('dart-calling-main', '*'); | 204 dartPrint('dart-calling-main'); |
| 182 try { | 205 try { |
| 183 main(); | 206 main(); |
| 184 } catch (e) { | 207 } catch (e) { |
| 185 dartPrint(e); | 208 dartPrint(e); |
| 186 if (e.stack) dartPrint(e.stack); | 209 if (e.stack) dartPrint(e.stack); |
| 187 window.postMessage('unittest-suite-fail', '*'); | 210 window.postMessage('unittest-suite-fail', '*'); |
| 188 return; | 211 return; |
| 189 } | 212 } |
| 190 window.postMessage('dart-main-done', '*'); | 213 dartPrint('dart-main-done'); |
| 191 } | 214 } |
| OLD | NEW |