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 * A simple unit test library for running tests in a browser. | |
7 * | |
8 * Provides enhanced HTML output with collapsible group headers | |
9 * and other at-a-glance information about the test results. | |
10 */ | |
11 #library('unittest'); | |
12 | |
13 #import('dart:html'); | |
14 #import('unittest.dart'); | |
15 | |
16 | |
17 class HtmlEnhancedConfiguration extends Configuration { | |
18 /** Whether this is run within dartium layout tests. */ | |
19 final bool _isLayoutTest; | |
20 HtmlEnhancedConfiguration(this._isLayoutTest); | |
21 | |
22 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. | |
23 EventListener _onErrorClosure; | |
24 | |
25 void onInit() { | |
26 //initialize and load CSS | |
27 final String _CSSID = '_unittestcss_'; | |
28 | |
29 var cssElement = document.head.query('#${_CSSID}'); | |
30 if (cssElement == null){ | |
31 document.head.elements.add(new Element.html( | |
32 '<style id="${_CSSID}"></style>')); | |
33 cssElement = document.head.query('#${_CSSID}'); | |
34 } | |
35 | |
36 cssElement.innerHTML = _htmlTestCSS; | |
37 | |
38 _onErrorClosure = | |
39 (e) => handleExternalError(e, '(DOM callback has errors)'); | |
40 } | |
41 | |
42 void onStart() { | |
43 window.postMessage('unittest-suite-wait-for-done', '*'); | |
44 // Listen for uncaught errors. | |
45 window.on.error.add(_onErrorClosure); | |
46 } | |
47 | |
48 void onTestResult(TestCase testCase) {} | |
49 | |
50 void onDone(int passed, int failed, int errors, List<TestCase> results, | |
51 String uncaughtError) { | |
52 window.on.error.remove(_onErrorClosure); | |
53 | |
54 _showInteractiveResultsInPage(passed, failed, errors, results, | |
55 _isLayoutTest, uncaughtError); | |
56 | |
57 window.postMessage('unittest-suite-done', '*'); | |
58 } | |
59 | |
60 void _showInteractiveResultsInPage(int passed, int failed, int errors, | |
61 List<TestCase> results, bool isLayoutTest, String uncaughtError) { | |
62 if (isLayoutTest && passed == results.length) { | |
63 document.body.innerHTML = "PASS"; | |
64 } else { | |
65 // changed the StringBuffer to an Element fragment | |
66 Element te = new Element.html('<div class="unittest-table"></div>'); | |
67 | |
68 te.elements.add(new Element.html(passed == results.length | |
69 ? "<div class='unittest-overall unittest-pass'>PASS</div>" | |
70 : "<div class='unittest-overall unittest-fail'>FAIL</div>")); | |
71 | |
72 // moved summary to the top since web browsers | |
73 // don't auto-scroll to the bottom like consoles typically do. | |
74 if (passed == results.length && uncaughtError == null) { | |
75 te.elements.add(new Element.html(""" | |
76 <div class='unittest-pass'>All ${passed} tests passed</div>""")); | |
77 } else { | |
78 | |
79 if (uncaughtError != null) { | |
80 te.elements.add(new Element.html(""" | |
81 <div class='unittest-summary'> | |
82 <span class='unittest-error'>Uncaught error: $uncaughtError</span> | |
83 </div>""")); | |
84 } | |
85 | |
86 te.elements.add(new Element.html(""" | |
87 <div class='unittest-summary'> | |
88 <span class='unittest-pass'>Total ${passed} passed</span>, | |
89 <span class='unittest-fail'>${failed} failed</span>, | |
90 <span class='unittest-error'> | |
91 ${errors + (uncaughtError == null ? 0 : 1)} errors</span> | |
92 </div>""")); | |
93 } | |
94 | |
95 te.elements.add(new Element.html(""" | |
96 <div><button id='btnCollapseAll'>Collapse All</button></div> | |
97 """)); | |
98 | |
99 // handle the click event for the collapse all button | |
100 te.query('#btnCollapseAll').on.click.add((_){ | |
101 document | |
102 .queryAll('.unittest-row') | |
103 .forEach((el) => el.attributes['class'] = el.attributes['class'] | |
104 .replaceAll('unittest-row ', 'unittest-row-hidden ')); | |
105 }); | |
106 | |
107 var previousGroup = ''; | |
108 var groupPassFail = true; | |
109 final indentAmount = 50; | |
110 | |
111 // order by group and sort numerically within each group | |
112 var groupedBy = new LinkedHashMap<String, List<TestCase>>(); | |
113 | |
114 for (final t in results){ | |
115 if (!groupedBy.containsKey(t.currentGroup)){ | |
116 groupedBy[t.currentGroup] = new List<TestCase>(); | |
117 } | |
118 | |
119 groupedBy[t.currentGroup].add(t); | |
120 } | |
121 | |
122 // flatten the list again with tests ordered | |
123 List<TestCase> flattened = new List<TestCase>(); | |
124 | |
125 groupedBy | |
126 .getValues() | |
127 .forEach((tList){ | |
128 tList.sort((tcA, tcB) => tcA.id - tcB.id); | |
129 flattened.addAll(tList); | |
130 } | |
131 ); | |
132 | |
133 var nonAlphanumeric = new RegExp('[^a-z0-9A-Z]'); | |
134 | |
135 // output group headers and test rows | |
136 for (final test_ in flattened) { | |
137 | |
138 // replace everything but numbers and letters from the group name with | |
139 // '_' so we can use in id and class properties. | |
140 var safeGroup = test_.currentGroup.replaceAll(nonAlphanumeric,'_'); | |
141 | |
142 if (test_.currentGroup != previousGroup){ | |
143 | |
144 previousGroup = test_.currentGroup; | |
145 | |
146 var testsInGroup = results.filter( | |
147 (TestCase t) => t.currentGroup == previousGroup); | |
148 var groupTotalTestCount = testsInGroup.length; | |
149 var groupTestPassedCount = testsInGroup.filter( | |
150 (TestCase t) => t.result == 'pass').length; | |
151 groupPassFail = groupTotalTestCount == groupTestPassedCount; | |
152 | |
153 te.elements.add(new Element.html(""" | |
154 <div> | |
155 <div id='${safeGroup}' | |
156 class='unittest-group ${safeGroup} test${safeGroup}'> | |
157 <div ${_isIE ? "style='display:inline-block' ": ""} | |
158 class='unittest-row-status'> | |
159 <div class='unittest-group-status unittest-group-status- | |
160 ${groupPassFail ? 'pass' : 'fail'}'></div> | |
161 </div> | |
162 <div ${_isIE ? "style='display:inline-block' ": ""}> | |
163 ${test_.currentGroup}</div> | |
164 <div ${_isIE ? "style='display:inline-block' ": ""}> | |
165 (${groupTestPassedCount}/${groupTotalTestCount})</div> | |
166 </div> | |
167 </div>""")); | |
168 | |
169 var grp = te.query('#${safeGroup}'); | |
170 if (grp != null){ | |
171 grp.on.click.add((_){ | |
172 var row = document.query('.unittest-row-${safeGroup}'); | |
173 if (row.attributes['class'].contains('unittest-row ')){ | |
174 document.queryAll('.unittest-row-${safeGroup}').forEach( | |
175 (e) => e.attributes['class'] = e.attributes['class'] | |
176 .replaceAll('unittest-row ', 'unittest-row-hidden ')); | |
177 }else{ | |
178 document.queryAll('.unittest-row-${safeGroup}').forEach( | |
179 (e) => e.attributes['class'] = e.attributes['class'] | |
180 .replaceAll('unittest-row-hidden', 'unittest-row')); | |
181 } | |
182 }); | |
183 } | |
184 } | |
185 | |
186 _buildRow(test_, te, safeGroup, !groupPassFail); | |
187 } | |
188 | |
189 document.body.elements.clear(); | |
190 document.body.elements.add(te); | |
191 } | |
192 } | |
193 | |
194 void _buildRow(TestCase test_, Element te, String groupID, bool isVisible) { | |
195 var background = 'unittest-row-${test_.id % 2 == 0 ? "even" : "odd"}'; | |
196 var display = '${isVisible ? "unittest-row" : "unittest-row-hidden"}'; | |
197 | |
198 // TODO (prujohn@gmail.com) I had to borrow this from html_print.dart | |
199 // Probably should put it in some more common location. | |
200 String _htmlEscape(String string) { | |
201 return string.replaceAll('&', '&') | |
202 .replaceAll('<','<') | |
203 .replaceAll('>','>'); | |
204 } | |
205 | |
206 addRowElement(id, status, description){ | |
207 te.elements.add( | |
208 new Element.html( | |
209 ''' <div> | |
210 <div class='$display unittest-row-${groupID} $background'> | |
211 <div ${_isIE ? "style='display:inline-block' ": ""} | |
212 class='unittest-row-id'>$id</div> | |
213 <div ${_isIE ? "style='display:inline-block' ": ""} | |
214 class="unittest-row-status unittest-${test_.result}"> | |
215 $status</div> | |
216 <div ${_isIE ? "style='display:inline-block' ": ""} | |
217 class='unittest-row-description'>$description</div> | |
218 </div> | |
219 </div>''' | |
220 ) | |
221 ); | |
222 } | |
223 | |
224 if (!test_.isComplete) { | |
225 addRowElement('${test_.id}', 'NO STATUS', 'Test did not complete.'); | |
226 return; | |
227 } | |
228 | |
229 addRowElement('${test_.id}', '${test_.result.toUpperCase()}', | |
230 '${test_.description}. ${_htmlEscape(test_.message)}'); | |
231 | |
232 if (test_.stackTrace != null) { | |
233 addRowElement('', '', '<pre>${_htmlEscape(test_.stackTrace)}</pre>'); | |
234 } | |
235 } | |
236 | |
237 | |
238 static bool get _isIE => document.window.navigator.userAgent.contains('MSIE'); | |
239 | |
240 String get _htmlTestCSS => | |
241 ''' | |
242 body{ | |
243 font-size: 14px; | |
244 font-family: 'Open Sans', 'Lucida Sans Unicode', 'Lucida Grande',''' | |
245 ''' sans-serif; | |
246 background: WhiteSmoke; | |
247 } | |
248 | |
249 .unittest-group | |
250 { | |
251 background: rgb(75,75,75); | |
252 width:98%; | |
253 color: WhiteSmoke; | |
254 font-weight: bold; | |
255 padding: 6px; | |
256 cursor: pointer; | |
257 | |
258 /* Provide some visual separation between groups for IE */ | |
259 ${_isIE ? "border-bottom:solid black 1px;": ""} | |
260 ${_isIE ? "border-top:solid #777777 1px;": ""} | |
261 | |
262 background-image: -webkit-linear-gradient(bottom, rgb(50,50,50) 0%, ''' | |
263 '''rgb(100,100,100) 100%); | |
264 background-image: -moz-linear-gradient(bottom, rgb(50,50,50) 0%, ''' | |
265 '''rgb(100,100,100) 100%); | |
266 background-image: -ms-linear-gradient(bottom, rgb(50,50,50) 0%, ''' | |
267 '''rgb(100,100,100) 100%); | |
268 background-image: linear-gradient(bottom, rgb(50,50,50) 0%, ''' | |
269 '''rgb(100,100,100) 100%); | |
270 | |
271 display: -webkit-box; | |
272 display: -moz-box; | |
273 display: -ms-box; | |
274 display: box; | |
275 | |
276 -webkit-box-orient: horizontal; | |
277 -moz-box-orient: horizontal; | |
278 -ms-box-orient: horizontal; | |
279 box-orient: horizontal; | |
280 | |
281 -webkit-box-align: center; | |
282 -moz-box-align: center; | |
283 -ms-box-align: center; | |
284 box-align: center; | |
285 } | |
286 | |
287 .unittest-group-status | |
288 { | |
289 width: 20px; | |
290 height: 20px; | |
291 border-radius: 20px; | |
292 margin-left: 10px; | |
293 } | |
294 | |
295 .unittest-group-status-pass{ | |
296 background: Green; | |
297 background: ''' | |
298 '''-webkit-radial-gradient(center, ellipse cover, #AAFFAA 0%,Green 100%); | |
299 background: ''' | |
300 '''-moz-radial-gradient(center, ellipse cover, #AAFFAA 0%,Green 100%); | |
301 background: ''' | |
302 '''-ms-radial-gradient(center, ellipse cover, #AAFFAA 0%,Green 100%); | |
303 background: ''' | |
304 '''radial-gradient(center, ellipse cover, #AAFFAA 0%,Green 100%); | |
305 } | |
306 | |
307 .unittest-group-status-fail{ | |
308 background: Red; | |
309 background: ''' | |
310 '''-webkit-radial-gradient(center, ellipse cover, #FFAAAA 0%,Red 100%); | |
311 background: ''' | |
312 '''-moz-radial-gradient(center, ellipse cover, #FFAAAA 0%,Red 100%); | |
313 background: ''' | |
314 '''-ms-radial-gradient(center, ellipse cover, #AAFFAA 0%,Green 100%); | |
315 background: radial-gradient(center, ellipse cover, #FFAAAA 0%,Red 100%); | |
316 } | |
317 | |
318 .unittest-overall{ | |
319 font-size: 20px; | |
320 } | |
321 | |
322 .unittest-summary{ | |
323 font-size: 18px; | |
324 } | |
325 | |
326 .unittest-pass{ | |
327 color: Green; | |
328 } | |
329 | |
330 .unittest-fail, .unittest-error | |
331 { | |
332 color: Red; | |
333 } | |
334 | |
335 .unittest-row | |
336 { | |
337 display: -webkit-box; | |
338 display: -moz-box; | |
339 display: -ms-box; | |
340 display: box; | |
341 -webkit-box-orient: horizontal; | |
342 -moz-box-orient: horizontal; | |
343 -ms-box-orient: horizontal; | |
344 box-orient: horizontal; | |
345 width: 100%; | |
346 } | |
347 | |
348 .unittest-row-hidden | |
349 { | |
350 display: none; | |
351 } | |
352 | |
353 .unittest-row-odd | |
354 { | |
355 background: WhiteSmoke; | |
356 } | |
357 | |
358 .unittest-row-even | |
359 { | |
360 background: #E5E5E5; | |
361 } | |
362 | |
363 .unittest-row-id | |
364 { | |
365 width: 3em; | |
366 } | |
367 | |
368 .unittest-row-status | |
369 { | |
370 width: 4em; | |
371 } | |
372 | |
373 .unittest-row-description | |
374 { | |
375 } | |
376 | |
377 '''; | |
378 } | |
379 | |
380 void useHtmlEnhancedConfiguration([bool isLayoutTest = false]) { | |
381 configure(new HtmlEnhancedConfiguration(isLayoutTest)); | |
382 } | |
OLD | NEW |