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