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

Side by Side Diff: lib/unittest/html_enhanced_config.dart

Issue 10836241: Move unittest from lib to pkg. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 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 | « lib/unittest/html_config.dart ('k') | lib/unittest/html_print.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) 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('&', '&amp;')
202 .replaceAll('<','&lt;')
203 .replaceAll('>','&gt;');
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', sans-serif ;
245 background: WhiteSmoke;
246 }
247
248 .unittest-group
249 {
250 background: rgb(75,75,75);
251 width:98%;
252 color: WhiteSmoke;
253 font-weight: bold;
254 padding: 6px;
255 cursor: pointer;
256
257 /* Provide some visual separation between groups for IE */
258 ${_isIE ? "border-bottom:solid black 1px;": ""}
259 ${_isIE ? "border-top:solid #777777 1px;": ""}
260
261 background-image: -webkit-linear-gradient(bottom, rgb(50,50,50) 0%, rgb(100, 100,100) 100%);
262 background-image: -moz-linear-gradient(bottom, rgb(50,50,50) 0%, rgb(100,100 ,100) 100%);
263 background-image: -ms-linear-gradient(bottom, rgb(50,50,50) 0%, rgb(100,100, 100) 100%);
264 background-image: linear-gradient(bottom, rgb(50,50,50) 0%, rgb(100,100,100) 100%);
265
266 display: -webkit-box;
267 display: -moz-box;
268 display: -ms-box;
269 display: box;
270
271 -webkit-box-orient: horizontal;
272 -moz-box-orient: horizontal;
273 -ms-box-orient: horizontal;
274 box-orient: horizontal;
275
276 -webkit-box-align: center;
277 -moz-box-align: center;
278 -ms-box-align: center;
279 box-align: center;
280 }
281
282 .unittest-group-status
283 {
284 width: 20px;
285 height: 20px;
286 border-radius: 20px;
287 margin-left: 10px;
288 }
289
290 .unittest-group-status-pass{
291 background: Green;
292 background: -webkit-radial-gradient(center, ellipse cover, #AAFFAA 0%,Green 100%);
293 background: -moz-radial-gradient(center, ellipse cover, #AAFFAA 0%,Green 100 %);
294 background: -ms-radial-gradient(center, ellipse cover, #AAFFAA 0%,Green 100% );
295 background: radial-gradient(center, ellipse cover, #AAFFAA 0%,Green 100%);
296 }
297
298 .unittest-group-status-fail{
299 background: Red;
300 background: -webkit-radial-gradient(center, ellipse cover, #FFAAAA 0%,Red 10 0%);
301 background: -moz-radial-gradient(center, ellipse cover, #FFAAAA 0%,Red 100%) ;
302 background: -ms-radial-gradient(center, ellipse cover, #AAFFAA 0%,Green 100% );
303 background: radial-gradient(center, ellipse cover, #FFAAAA 0%,Red 100%);
304 }
305
306 .unittest-overall{
307 font-size: 20px;
308 }
309
310 .unittest-summary{
311 font-size: 18px;
312 }
313
314 .unittest-pass{
315 color: Green;
316 }
317
318 .unittest-fail, .unittest-error
319 {
320 color: Red;
321 }
322
323 .unittest-row
324 {
325 display: -webkit-box;
326 display: -moz-box;
327 display: -ms-box;
328 display: box;
329 -webkit-box-orient: horizontal;
330 -moz-box-orient: horizontal;
331 -ms-box-orient: horizontal;
332 box-orient: horizontal;
333 width: 100%;
334 }
335
336 .unittest-row-hidden
337 {
338 display: none;
339 }
340
341 .unittest-row-odd
342 {
343 background: WhiteSmoke;
344 }
345
346 .unittest-row-even
347 {
348 background: #E5E5E5;
349 }
350
351 .unittest-row-id
352 {
353 width: 3em;
354 }
355
356 .unittest-row-status
357 {
358 width: 4em;
359 }
360
361 .unittest-row-description
362 {
363 }
364
365 ''';
366 }
367
368 void useHtmlEnhancedConfiguration([bool isLayoutTest = false]) {
369 configure(new HtmlEnhancedConfiguration(isLayoutTest));
370 }
OLDNEW
« no previous file with comments | « lib/unittest/html_config.dart ('k') | lib/unittest/html_print.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698