| 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 * DumpRenderTree. | 7 * DumpRenderTree. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 if (navigator.webkitStartDart) { | 10 if (navigator.webkitStartDart) { |
| 11 navigator.webkitStartDart(); | 11 navigator.webkitStartDart(); |
| 12 } | 12 } |
| 13 | 13 |
| 14 var testRunner = window.testRunner || window.layoutTestController; | 14 var testRunner = window.testRunner || window.layoutTestController; |
| 15 | 15 |
| 16 var waitForDone = false; | |
| 17 | |
| 18 function processMessage(msg) { | 16 function processMessage(msg) { |
| 19 if (typeof msg != 'string') return; | 17 if (testRunner) { |
| 20 if (msg == 'unittest-suite-done') { | 18 if (msg == 'unittest-suite-done') { |
| 21 if (testRunner) testRunner.notifyDone(); | 19 testRunner.notifyDone(); |
| 22 } else if (msg == 'unittest-suite-wait-for-done') { | 20 } else if (msg == 'unittest-suite-wait-for-done') { |
| 23 waitForDone = true; | 21 testRunner.startedDartTest = true; |
| 24 if (testRunner) testRunner.startedDartTest = true; | |
| 25 } else if (msg == 'dart-calling-main') { | |
| 26 if (testRunner) testRunner.startedDartTest = true; | |
| 27 } else if (msg == 'dart-main-done') { | |
| 28 if (!waitForDone) { | |
| 29 window.postMessage('unittest-suite-success', '*'); | |
| 30 } | 22 } |
| 31 } else if (msg == 'unittest-suite-success') { | |
| 32 document.body.innerHTML += '<pre>PASS</pre>'; | |
| 33 if (testRunner) testRunner.notifyDone(); | |
| 34 } else if (msg == 'unittest-suite-fail') { | |
| 35 showErrorAndExit('Some tests failed.'); | |
| 36 } | 23 } |
| 37 } | 24 } |
| 38 | 25 |
| 39 function onReceive(e) { | 26 function onReceive(e) { |
| 40 processMessage(e.data); | 27 processMessage(e.data); |
| 41 } | 28 } |
| 42 | 29 |
| 43 if (testRunner) { | 30 if (testRunner) { |
| 44 testRunner.dumpAsText(); | 31 testRunner.dumpAsText(); |
| 45 testRunner.waitUntilDone(); | 32 testRunner.waitUntilDone(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 65 } | 52 } |
| 66 | 53 |
| 67 window.addEventListener("DOMContentLoaded", onLoad, false); | 54 window.addEventListener("DOMContentLoaded", onLoad, false); |
| 68 | 55 |
| 69 // If nobody intercepts the error, finish the test. | 56 // If nobody intercepts the error, finish the test. |
| 70 window.addEventListener("error", function(e) { | 57 window.addEventListener("error", function(e) { |
| 71 // needed for dartium compilation errors. | 58 // needed for dartium compilation errors. |
| 72 showErrorAndExit(e && e.message); | 59 showErrorAndExit(e && e.message); |
| 73 }, false); | 60 }, false); |
| 74 | 61 |
| 75 document.addEventListener('readystatechange', function () { | 62 document.onreadystatechange = function() { |
| 76 if (document.readyState != "loaded") return; | 63 if (document.readyState != "loaded") return; |
| 77 // If 'startedDartTest' is not set, that means that the test did not have | 64 // If 'startedDartTest' is not set, that means that the test did not have |
| 78 // a chance to load. This will happen when a load error occurs in the VM. | 65 // a chance to load. This will happen when a load error occurs in the VM. |
| 79 // Give the machine time to start up. | 66 // Give the machine time to start up. |
| 80 setTimeout(function() { | 67 setTimeout(function() { |
| 81 // A window.postMessage might have been enqueued after this timeout. | 68 // A window.postMessage might have been enqueued after this timeout. |
| 82 // Just sleep another time to give the browser the time to process the | 69 // Just sleep another time to give the browser the time to process the |
| 83 // posted message. | 70 // posted message. |
| 84 setTimeout(function() { | 71 setTimeout(function() { |
| 85 if (testRunner && !testRunner.startedDartTest) { | 72 if (testRunner && !testRunner.startedDartTest) { |
| 86 testRunner.notifyDone(); | 73 testRunner.notifyDone(); |
| 87 } | 74 } |
| 88 }, 0); | 75 }, 0); |
| 89 }, 50); | 76 }, 50); |
| 90 }); | 77 }; |
| 91 | |
| 92 // dart2js will generate code to call this function to handle the Dart | |
| 93 // [print] method. The base [Configuration] (config.html) calls | |
| 94 // [print] with the secret messages "unittest-suite-success" and | |
| 95 // "unittest-suite-wait-for-done". These messages are then posted so | |
| 96 // processMessage above will see them. | |
| 97 function dartPrint(msg) { | |
| 98 if ((msg === 'unittest-suite-success') | |
| 99 || (msg === 'unittest-suite-wait-for-done')) { | |
| 100 window.postMessage(msg, '*'); | |
| 101 return; | |
| 102 } | |
| 103 var pre = document.createElement("pre"); | |
| 104 pre.appendChild(document.createTextNode(String(msg))); | |
| 105 document.body.appendChild(pre); | |
| 106 } | |
| 107 | |
| 108 // dart2js will generate code to call this function instead of calling | |
| 109 // Dart [main] directly. The argument is a closure that invokes main. | |
| 110 function dartMainRunner(main) { | |
| 111 window.postMessage('dart-calling-main', '*'); | |
| 112 try { | |
| 113 main(); | |
| 114 } catch (e) { | |
| 115 window.postMessage('unittest-suite-fail', '*'); | |
| 116 return; // Posting 'dart-main-done' signals success. | |
| 117 } | |
| 118 window.postMessage('dart-main-done', '*'); | |
| 119 } | |
| OLD | NEW |