OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, 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 /// A simple unit test library for running tests in a browser. | |
6 library unittest.html_config; | |
7 | |
8 import 'dart:async'; | |
9 import 'dart:convert'; | |
10 import 'dart:html'; | |
11 import 'dart:js' as js; | |
12 import 'unittest.dart'; | |
13 | |
14 /// Creates a table showing tests results in HTML. | |
15 void _showResultsInPage(int passed, int failed, int errors, | |
16 List<TestCase> results, bool isLayoutTest, String uncaughtError) { | |
17 if (isLayoutTest && (passed == results.length) && uncaughtError == null) { | |
18 document.body.innerHtml = "PASS"; | |
19 } else { | |
20 var newBody = new StringBuffer(); | |
21 newBody.write("<table class='unittest-table'><tbody>"); | |
22 newBody.write(passed == results.length && uncaughtError == null | |
23 ? "<tr><td colspan='3' class='unittest-pass'>PASS</td></tr>" | |
24 : "<tr><td colspan='3' class='unittest-fail'>FAIL</td></tr>"); | |
25 | |
26 for (final test_ in results) { | |
27 newBody.write(_toHtml(test_)); | |
28 } | |
29 | |
30 if (uncaughtError != null) { | |
31 newBody.write('''<tr> | |
32 <td>--</td> | |
33 <td class="unittest-error">ERROR</td> | |
34 <td>Uncaught error: $uncaughtError</td> | |
35 </tr>'''); | |
36 } | |
37 | |
38 if (passed == results.length && uncaughtError == null) { | |
39 newBody.write(""" | |
40 <tr><td colspan='3' class='unittest-pass'> | |
41 All ${passed} tests passed | |
42 </td></tr>"""); | |
43 } else { | |
44 newBody.write(""" | |
45 <tr><td colspan='3'>Total | |
46 <span class='unittest-pass'>${passed} passed</span>, | |
47 <span class='unittest-fail'>${failed} failed</span> | |
48 <span class='unittest-error'> | |
49 ${errors + (uncaughtError == null ? 0 : 1)} errors</span> | |
50 </td></tr>"""); | |
51 } | |
52 newBody.write("</tbody></table>"); | |
53 document.body.innerHtml = newBody.toString(); | |
54 | |
55 window.onHashChange.listen((_) { | |
56 // Location may change from individual tests setting the hash tag. | |
57 if (window.location.hash != null && | |
58 window.location.hash.contains('testFilter')) { | |
59 window.location.reload(); | |
60 } | |
61 }); | |
62 } | |
63 } | |
64 | |
65 String _toHtml(TestCase test_) { | |
66 if (!test_.isComplete) { | |
67 return ''' | |
68 <tr> | |
69 <td>${test_.id}</td> | |
70 <td class="unittest-error">NO STATUS</td> | |
71 <td>Test did not complete</td> | |
72 </tr>'''; | |
73 } | |
74 | |
75 var html = ''' | |
76 <tr> | |
77 <td>${test_.id}</td> | |
78 <td class="unittest-${test_.result}">${test_.result.toUpperCase()}</td> | |
79 <td>Expectation: <a href="#testFilter=${test_.description}">${test_.desc
ription}</a>. ${HTML_ESCAPE.convert(test_.message)}</td> | |
80 </tr>'''; | |
81 | |
82 if (test_.stackTrace != null) { | |
83 html = '$html<tr><td></td><td colspan="2"><pre>' + | |
84 HTML_ESCAPE.convert(test_.stackTrace.toString()) + | |
85 '</pre></td></tr>'; | |
86 } | |
87 | |
88 return html; | |
89 } | |
90 | |
91 class HtmlConfiguration extends SimpleConfiguration { | |
92 /// Whether this is run within dartium layout tests. | |
93 final bool _isLayoutTest; | |
94 HtmlConfiguration(this._isLayoutTest); | |
95 | |
96 StreamSubscription<Event> _onErrorSubscription; | |
97 StreamSubscription<Event> _onMessageSubscription; | |
98 | |
99 void _installHandlers() { | |
100 if (_onErrorSubscription == null) { | |
101 _onErrorSubscription = window.onError.listen( | |
102 (e) { | |
103 // Some tests may expect this and have no way to suppress the error. | |
104 if (js.context['testExpectsGlobalError'] != true) { | |
105 handleExternalError(e, '(DOM callback has errors)'); | |
106 } | |
107 }); | |
108 } | |
109 if (_onMessageSubscription == null) { | |
110 _onMessageSubscription = window.onMessage.listen( | |
111 (e) => processMessage(e)); | |
112 } | |
113 } | |
114 | |
115 void _uninstallHandlers() { | |
116 if (_onErrorSubscription != null) { | |
117 _onErrorSubscription.cancel(); | |
118 _onErrorSubscription = null; | |
119 } | |
120 if (_onMessageSubscription != null) { | |
121 _onMessageSubscription.cancel(); | |
122 _onMessageSubscription = null; | |
123 } | |
124 } | |
125 | |
126 void processMessage(e) { | |
127 if ('unittest-suite-external-error' == e.data) { | |
128 handleExternalError('<unknown>', '(external error detected)'); | |
129 } | |
130 } | |
131 | |
132 void onInit() { | |
133 // For Dart internal tests, we want to turn off stack frame | |
134 // filtering, which we do with this meta-header. | |
135 var meta = querySelector('meta[name="dart.unittest"]'); | |
136 filterStacks = meta == null ? true : | |
137 !meta.content.contains('full-stack-traces'); | |
138 _installHandlers(); | |
139 window.postMessage('unittest-suite-wait-for-done', '*'); | |
140 } | |
141 | |
142 void onStart() { | |
143 // If the URL has a #testFilter=testName then filter tests to that. | |
144 // This is used to make it easy to run a single test- but is only intended | |
145 // for interactive debugging scenarios. | |
146 var hash = window.location.hash; | |
147 if (hash != null && hash.length > 1) { | |
148 var params = hash.substring(1).split('&'); | |
149 for (var param in params) { | |
150 var parts = param.split('='); | |
151 if (parts.length == 2 && parts[0] == 'testFilter') { | |
152 filterTests('^${parts[1]}'); | |
153 } | |
154 } | |
155 } | |
156 super.onStart(); | |
157 } | |
158 | |
159 void onSummary(int passed, int failed, int errors, List<TestCase> results, | |
160 String uncaughtError) { | |
161 _showResultsInPage(passed, failed, errors, results, _isLayoutTest, | |
162 uncaughtError); | |
163 } | |
164 | |
165 void onDone(bool success) { | |
166 _uninstallHandlers(); | |
167 window.postMessage('unittest-suite-done', '*'); | |
168 } | |
169 } | |
170 | |
171 void useHtmlConfiguration([bool isLayoutTest = false]) { | |
172 unittestConfiguration = isLayoutTest ? _singletonLayout : _singletonNotLayout; | |
173 } | |
174 | |
175 final _singletonLayout = new HtmlConfiguration(true); | |
176 final _singletonNotLayout = new HtmlConfiguration(false); | |
OLD | NEW |