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 * conent shell. | |
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 | |
15 // Some tests may expect and have no way to suppress global errors. | |
16 var testExpectsGlobalError = false; | |
17 var testSuppressedGlobalErrors = []; | |
18 | |
19 // Set window onerror to make sure that we catch test harness errors across all | |
20 // browsers. | |
21 window.onerror = function (message, url, lineNumber) { | |
22 if (testExpectsGlobalError) { | |
23 testSuppressedGlobalErrors.push({ | |
24 message: message | |
25 }); | |
26 return; | |
27 } | |
28 if (url) { | |
29 showErrorAndExit( | |
30 "\n\n" + url + ":" + lineNumber + ":\n" + message + "\n\n"); | |
31 } else { | |
32 showErrorAndExit(message); | |
33 } | |
34 window.postMessage('unittest-suite-external-error', '*'); | |
35 }; | |
36 | |
37 // Start Dartium/content_shell, unless we are waiting for HTML Imports to load. | |
38 // HTML Imports allows a document to link to other HTMLs documents via | |
39 // <link rel=import>. It also allows for those other documents to contain | |
40 // <script> tags, which must be run before scripts on the main page. | |
41 // We have package:web_components to polyfill this feature, and it will handle | |
42 // starting Dartium/content_shell in that case. HTML Imports is used by Polymer, | |
43 // but it could be used by itself too. See the specification: | |
44 // https://dvcs.w3.org/hg/webcomponents/raw-file/tip/spec/imports/index.html | |
45 if (navigator.webkitStartDart && !window.HTMLImports) { | |
46 navigator.webkitStartDart(); | |
47 } | |
48 | |
49 // testRunner is provided by content shell. | |
50 // It is not available in browser tests. | |
51 var testRunner = window.testRunner || window.layoutTestController; | |
52 | |
53 var waitForDone = false; | |
54 | |
55 // Returns the driving window object if available | |
56 function getDriverWindow() { | |
57 if (window != window.parent) { | |
58 // We're running in an iframe. | |
59 return window.parent; | |
60 } else if (window.opener) { | |
61 // We were opened by another window. | |
62 return window.opener; | |
63 } | |
64 return null; | |
65 } | |
66 | |
67 function notifyStart() { | |
68 var driver = getDriverWindow(); | |
69 if (driver) { | |
70 driver.postMessage("STARTING", "*"); | |
71 } | |
72 } | |
73 // We call notifyStart here to notify the encapsulating browser. | |
74 notifyStart(); | |
75 | |
76 function notifyDone() { | |
77 if (testRunner) testRunner.notifyDone(); | |
78 | |
79 // TODO(ricow): REMOVE, debug info, see issue 13292 | |
80 if (!testRunner) { | |
81 printMessage('Calling notifyDone()'); | |
82 } | |
83 // To support in browser launching of tests we post back start and result | |
84 // messages to the window.opener. | |
85 var driver = getDriverWindow(); | |
86 if (driver) { | |
87 driver.postMessage(window.document.body.innerHTML, "*"); | |
88 } | |
89 } | |
90 | |
91 function processMessage(msg) { | |
92 if (typeof msg != 'string') return; | |
93 // TODO(ricow): REMOVE, debug info, see issue 13292 | |
94 if (!testRunner) { | |
95 // Filter out ShadowDOM polyfill messages which are random floats. | |
96 if (msg != parseFloat(msg)) { | |
97 printMessage('processMessage(): ' + msg); | |
98 } | |
99 } | |
100 if (msg == 'unittest-suite-done') { | |
101 notifyDone(); | |
102 } else if (msg == 'unittest-suite-wait-for-done') { | |
103 waitForDone = true; | |
104 if (testRunner) { | |
105 testRunner.startedDartTest = true; | |
106 } | |
107 } else if (msg == 'dart-calling-main') { | |
108 if (testRunner) { | |
109 testRunner.startedDartTest = true; | |
110 } | |
111 } else if (msg == 'dart-main-done') { | |
112 if (!waitForDone) { | |
113 printMessage('PASS'); | |
114 notifyDone(); | |
115 } | |
116 } else if (msg == 'unittest-suite-success') { | |
117 printMessage('PASS'); | |
118 notifyDone(); | |
119 } else if (msg == 'unittest-suite-fail') { | |
120 showErrorAndExit('Some tests failed.'); | |
121 } | |
122 } | |
123 | |
124 function onReceive(e) { | |
125 processMessage(e.data); | |
126 } | |
127 | |
128 if (testRunner) { | |
129 testRunner.dumpAsText(); | |
130 testRunner.waitUntilDone(); | |
131 } | |
132 window.addEventListener("message", onReceive, false); | |
133 | |
134 function showErrorAndExit(message) { | |
135 if (message) { | |
136 printMessage('Error: ' + String(message)); | |
137 } | |
138 // dart/tools/testing/test_runner.dart is looking for either PASS or | |
139 // FAIL in a browser test's output. | |
140 printMessage('FAIL'); | |
141 notifyDone(); | |
142 } | |
143 | |
144 function onLoad(e) { | |
145 // needed for dartium compilation errors. | |
146 if (window.compilationError) { | |
147 showErrorAndExit(window.compilationError); | |
148 } | |
149 } | |
150 | |
151 window.addEventListener("DOMContentLoaded", onLoad, false); | |
152 | |
153 // Note: before renaming this function, note that it is also included in an | |
154 // inlined error handler in the HTML files that wrap DRT tests. | |
155 // See: tools/testing/dart/browser_test.dart | |
156 function externalError(e) { | |
157 // needed for dartium compilation errors. | |
158 showErrorAndExit(e && e.message); | |
159 window.postMessage('unittest-suite-external-error', '*'); | |
160 } | |
161 | |
162 document.addEventListener('readystatechange', function () { | |
163 if (document.readyState != "loaded") return; | |
164 // If 'startedDartTest' is not set, that means that the test did not have | |
165 // a chance to load. This will happen when a load error occurs in the VM. | |
166 // Give the machine time to start up. | |
167 setTimeout(function() { | |
168 // A window.postMessage might have been enqueued after this timeout. | |
169 // Just sleep another time to give the browser the time to process the | |
170 // posted message. | |
171 setTimeout(function() { | |
172 if (testRunner && !testRunner.startedDartTest) { | |
173 notifyDone(); | |
174 } | |
175 }, 0); | |
176 }, 50); | |
177 }); | |
178 | |
179 // dart2js will generate code to call this function to handle the Dart | |
180 // [print] method. | |
181 // | |
182 // dartium will invoke this method for [print] calls if the environment variable | |
183 // "DART_FORWARDING_PRINT" was set when launching dartium. | |
184 // | |
185 // Our tests will be wrapped, so we can detect when [main] is called and when | |
186 // it has ended. | |
187 // The wrapping happens either via "dartMainRunner" (for dart2js) or wrapped | |
188 // tests for dartium. | |
189 // | |
190 // The following messages are handled specially: | |
191 // dart-calling-main: signals that the dart [main] function will be invoked | |
192 // dart-main-done: signals that the dart [main] function has finished | |
193 // unittest-suite-wait-for-done: signals the start of an asynchronous test | |
194 // unittest-suite-success: signals the end of an asynchrounous test | |
195 // | |
196 // These messages are used to communicate with the test and will be posted so | |
197 // [processMessage] above can see it. | |
198 function dartPrint(msg) { | |
199 if ((msg === 'unittest-suite-success') | |
200 || (msg === 'unittest-suite-done') | |
201 || (msg === 'unittest-suite-wait-for-done') | |
202 || (msg === 'dart-calling-main') | |
203 || (msg === 'dart-main-done')) { | |
204 window.postMessage(msg, '*'); | |
205 return; | |
206 } | |
207 printMessage(msg); | |
208 } | |
209 | |
210 // Prints 'msg' to the console (if available) and to the body of the html | |
211 // document. | |
212 function printMessage(msg) { | |
213 if (typeof console === 'object') console.warn(msg); | |
214 var pre = document.createElement('pre'); | |
215 pre.appendChild(document.createTextNode(String(msg))); | |
216 document.body.appendChild(pre); | |
217 document.body.appendChild(document.createTextNode('\n')); | |
218 } | |
219 | |
220 // dart2js will generate code to call this function instead of calling | |
221 // Dart [main] directly. The argument is a closure that invokes main. | |
222 function dartMainRunner(main) { | |
223 dartPrint('dart-calling-main'); | |
224 try { | |
225 main(); | |
226 } catch (e) { | |
227 dartPrint(e); | |
228 if (e.stack) dartPrint(e.stack); | |
229 window.postMessage('unittest-suite-fail', '*'); | |
230 return; | |
231 } | |
232 dartPrint('dart-main-done'); | |
233 } | |
OLD | NEW |