OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012, 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 * This configuration is similar to the interactive_html_config | |
7 * as it runs each test in its own IFrame. However, where the former | |
8 * recreated the IFrame for each test, here the IFrames are preserved. | |
9 * Furthermore we post a message on completion. The aim is for this | |
10 * config to be used by testrunner for running layout tests. | |
Siggi Cherem (dart-lang)
2012/09/05 17:45:41
I'd rephrase and move this last sentence to the to
gram
2012/09/06 18:25:25
Done.
| |
11 */ | |
12 #library('html_layout_config'); | |
13 | |
14 #import('dart:html'); | |
15 #import('dart:math'); | |
16 #import('unittest.dart'); | |
17 | |
18 /** The messages exchanged between parent and child. */ | |
19 | |
Siggi Cherem (dart-lang)
2012/09/05 17:45:41
nit: remove empty line
gram
2012/09/06 18:25:25
Done.
| |
20 class _Message { | |
21 static final START = 'start'; | |
22 static final LOG = 'log'; | |
23 static final STACK = 'stack'; | |
24 static final PASS = 'pass'; | |
25 static final FAIL = 'fail'; | |
26 static final ERROR = 'error'; | |
27 | |
28 String messageType; | |
29 int elapsed; | |
30 String body; | |
31 | |
32 static String text(String messageType, | |
33 [int elapsed = 0, String body = '']) => | |
34 '$messageType $elapsed $body'; | |
35 | |
36 _Message(this.messageType, [this.elapsed = 0, this.body = '']); | |
37 | |
38 _Message.fromString(String msg) { | |
39 int idx = msg.indexOf(' '); | |
40 if (idx < 0) { // Not supposed to happen | |
41 messageType = 'log'; | |
42 elapsed = 0; | |
43 body = msg; | |
44 return; | |
45 } | |
46 messageType = msg.substring(0, idx); | |
47 ++idx; | |
48 int idx2 = msg.indexOf(' ', idx); | |
49 elapsed = parseInt(msg.substring(idx, idx2)); | |
50 ++idx2; | |
51 body = msg.substring(idx2); | |
Siggi Cherem (dart-lang)
2012/09/05 17:45:41
can we use a regexp instead of these index/substri
gram
2012/09/06 18:25:25
Done.
| |
52 } | |
53 | |
54 String toString() => text(messageType, elapsed, body); | |
55 } | |
56 | |
57 /** | |
58 * The child configuration that is used to run individual tests in | |
59 * an IFrame and post the results back to the parent. In principle | |
60 * this can run more than one test in the IFrame but currently only | |
61 * one is used. | |
62 */ | |
63 class ChildHtmlConfiguration extends Configuration { | |
64 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. | |
65 EventListener _onErrorClosure; | |
66 | |
67 /** The window to which results must be posted. */ | |
68 Window parentWindow; | |
69 | |
70 /** The time at which tests start. */ | |
71 Map<int,Date> _testStarts; | |
72 | |
73 ChildHtmlConfiguration() : | |
74 _testStarts = new Map<int,Date>(); | |
75 | |
76 /** Don't start running tests automatically. */ | |
77 get autoStart() => false; | |
78 | |
79 void onInit() { | |
80 _onErrorClosure = | |
81 (e) => handleExternalError(e, '(DOM callback has errors)'); | |
82 | |
83 /** | |
84 * The parent posts a 'start' message to kick things off, | |
85 * which is handled by this handler. It saves the parent | |
86 * window, gets the test ID from the query parameter in the | |
87 * IFrame URL, sets that as a solo test and starts test execution. | |
88 */ | |
89 window.on.message.add((MessageEvent e) { | |
90 // Get the result, do any logging, then do a pass/fail. | |
91 var m = new _Message.fromString(e.data); | |
92 if (m.messageType == _Message.START) { | |
93 parentWindow = e.source; | |
94 String search = window.location.search; | |
95 int pos = search.indexOf('t='); | |
96 String ids = search.substring(pos+2); | |
97 int id = parseInt(ids); | |
98 setSoloTest(id); | |
99 runTests(); | |
100 } | |
101 }); | |
102 } | |
103 | |
104 void onStart() { | |
105 // Listen for uncaught errors. | |
106 window.on.error.add(_onErrorClosure); | |
107 } | |
108 | |
109 /** Record the start time of the test. */ | |
110 void onTestStart(TestCase testCase) { | |
111 super.onTestStart(testCase); | |
112 _testStarts[testCase.id]= new Date.now(); | |
113 } | |
114 | |
115 /** | |
116 * Tests can call [log] for diagnostic output. These log | |
117 * messages in turn get passed to this method, which adds | |
118 * a timestamp and posts them back to the parent window. | |
119 */ | |
120 void logMessage(TestCase testCase, String message) { | |
121 int elapsed; | |
122 if (testCase == null) { | |
123 elapsed = -1; | |
124 } else { | |
125 Date end = new Date.now(); | |
126 elapsed = end.difference(_testStarts[testCase.id]).inMilliseconds; | |
127 } | |
128 parentWindow.postMessage( | |
129 _Message.text(_Message.LOG, elapsed, message).toString(), '*'); | |
Siggi Cherem (dart-lang)
2012/09/05 17:45:41
btw - at some point postMessage was supposed to su
gram
2012/09/06 18:25:25
I'll add a TODO for this.
| |
130 } | |
131 | |
132 /** | |
133 * Get the elapsed time for the test, anbd post the test result | |
134 * back to the parent window. If the test failed due to an exception | |
135 * the stack is posted back too (before the test result). | |
136 */ | |
137 void onTestResult(TestCase testCase) { | |
138 super.onTestResult(testCase); | |
139 Date end = new Date.now(); | |
140 int elapsed = end.difference(_testStarts[testCase.id]).inMilliseconds; | |
141 if (testCase.stackTrace != null) { | |
142 parentWindow.postMessage( | |
143 _Message.text(_Message.STACK, elapsed, testCase.stackTrace), '*'); | |
144 } | |
145 parentWindow.postMessage( | |
146 _Message.text(testCase.result, elapsed, testCase.message), '*'); | |
147 } | |
148 | |
149 void onDone(int passed, int failed, int errors, List<TestCase> results, | |
150 String uncaughtError) { | |
151 window.on.error.remove(_onErrorClosure); | |
152 } | |
153 } | |
154 | |
155 /** | |
156 * The parent configuration runs in the top-level window; it wraps the tests | |
157 * in new functions that create child IFrames and run the real tests. | |
158 */ | |
159 class ParentHtmlConfiguration extends Configuration { | |
160 get autoStart => false; | |
161 | |
162 Map<int,Date> _testStarts; | |
163 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. | |
164 EventListener _onErrorClosure; | |
165 | |
166 /** The stack that was posted back from the child, if any. */ | |
167 String _stack; | |
168 | |
169 int _testTime; | |
170 /** | |
171 * Whether or not we have already wrapped the TestCase test functions | |
172 * in new closures that instead create an IFrame and get it to run the | |
173 * test. | |
174 */ | |
175 bool _doneWrap = false; | |
176 | |
177 /** | |
178 * We use this to make a single closure from _handleMessage so we | |
179 * can remove the handler later. | |
180 */ | |
181 Function _messageHandler; | |
182 | |
183 ParentHtmlConfiguration() : | |
184 _testStarts = new Map<int,Date>(); | |
185 | |
186 // We need to block until the test is done, so we make a | |
187 // dummy async callback that we will use to flag completion. | |
188 Function completeTest = null; | |
189 | |
190 wrapTest(TestCase testCase) { | |
191 String baseUrl = window.location.toString(); | |
192 String url = '${baseUrl}?t=${testCase.id}'; | |
193 return () { | |
194 // Add the child IFrame. | |
195 Element childDiv = document.query('#child'); | |
196 var label = new Element.html( | |
197 "<pre id='result${testCase.id}'>${testCase.description}</pre>"); | |
198 IFrameElement child = new Element.html(""" | |
199 <iframe id='childFrame${testCase.id}' src='$url'> | |
200 </iframe>"""); | |
201 childDiv.nodes.add(label); | |
202 childDiv.nodes.add(child); | |
203 completeTest = expectAsync0((){ }); | |
204 // Kick off the test when the IFrame is loaded. | |
205 child.on.load.add((e) { | |
206 child.contentWindow.postMessage(_Message.text(_Message.START), '*'); | |
207 }); | |
208 }; | |
209 } | |
210 | |
211 void _handleMessage(MessageEvent e) { | |
212 // Get the result, do any logging, then do a pass/fail. | |
213 var msg = new _Message.fromString(e.data); | |
214 if (msg.messageType == _Message.LOG) { | |
215 trace(e.data); | |
216 } else if (msg.messageType == _Message.STACK) { | |
217 _stack = msg.body; | |
218 } else { | |
219 _testTime = msg.elapsed; | |
220 if (msg.messageType == _Message.PASS) { | |
221 currentTestCase.pass(); | |
222 } else if (msg.messageType == _Message.FAIL) { | |
223 currentTestCase.fail(msg.body, _stack); | |
224 } else if (msg.messageType == _Message.ERROR) { | |
225 currentTestCase.error(msg.body, _stack); | |
226 } | |
227 completeTest(); | |
228 } | |
229 } | |
230 | |
231 void onInit() { | |
232 _messageHandler = _handleMessage; // We need to make just one closure. | |
233 _onErrorClosure = | |
234 (e) => handleExternalError(e, '(DOM callback has errors)'); | |
235 } | |
236 | |
237 void onStart() { | |
238 // Listen for uncaught errors. | |
239 window.on.error.add(_onErrorClosure); | |
240 if (!_doneWrap) { | |
241 _doneWrap = true; | |
242 for (int i = 0; i < testCases.length; i++) { | |
243 testCases[i].test = wrapTest(testCases[i]); | |
244 testCases[i].setUp = null; | |
245 testCases[i].tearDown = null; | |
246 } | |
247 } | |
248 window.on.message.add(_messageHandler); | |
249 } | |
250 | |
251 void onTestStart(TestCase testCase) { | |
252 var id = testCase.id; | |
253 _testStarts[testCase.id]= new Date.now(); | |
254 super.onTestStart(testCase); | |
255 _stack = null; | |
256 } | |
257 | |
258 // Actually test logging is handled by the child, then posted | |
259 // back to the parent. So here we know that the [message] argument | |
260 // is in the format used by [_Message]. | |
261 void logMessage(TestCase testCase, String message) { | |
262 var msg = new _Message.fromString(message); | |
263 document.query('#otherlogs').nodes.add( | |
264 new Element.html('<p>${msg.body}</p>')); | |
265 } | |
266 | |
267 void onTestResult(TestCase testCase) { | |
268 if (!testCase.enabled) return; | |
269 super.onTestResult(testCase); | |
270 document.query('#result${testCase.id}').text = | |
271 '${testCase.result}:${testCase.runningTime.inMilliseconds}:' | |
272 '${testCase.description}//${testCase.message}'; | |
273 } | |
274 | |
275 void onDone(int passed, int failed, int errors, List<TestCase> results, | |
276 String uncaughtError) { | |
277 window.on.message.remove(_messageHandler); | |
278 window.on.error.remove(_onErrorClosure); | |
279 window.postMessage('done', '*'); // Unblock DRT | |
280 } | |
281 } | |
282 | |
283 /** | |
284 * Add the divs to the DOM if they are not present. | |
285 */ | |
286 void _prepareDom() { | |
287 if (document.query('#otherlogs') == null) { | |
288 document.body.nodes.add(new Element.html( | |
289 "<div id='otherlogs'></div>")); | |
290 } | |
291 if (document.query('#child') == null) { | |
292 document.body.nodes.add(new Element.html("<div id='child'></div>")); | |
293 } | |
294 } | |
295 | |
296 /** | |
297 * Allocate a Configuration. We allocate either a parent or | |
298 * child, depending on whether the URL has a search part. | |
299 */ | |
300 void useHtmlLayoutConfiguration() { | |
301 if (window.location.search == '') { // This is the parent. | |
302 _prepareDom(); | |
303 configure(new ParentHtmlConfiguration()); | |
304 } else { | |
305 configure(new ChildHtmlConfiguration()); | |
306 } | |
307 } | |
308 | |
OLD | NEW |