Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(209)

Side by Side Diff: pkg/unittest/html_layout_config.dart

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

Powered by Google App Engine
This is Rietveld 408576698