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