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 testCase) { | |
66 if (!testCase.isComplete) { | |
67 return ''' | |
68 <tr> | |
69 <td>${testCase.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>${testCase.id}</td> | |
78 <td class="unittest-${testCase.result}"> | |
79 ${testCase.result.toUpperCase()} | |
80 </td> | |
81 <td> | |
82 <p>Expectation: | |
83 <a href="#testFilter=${testCase.description}"> | |
84 ${testCase.description} | |
85 </a>. | |
86 </p> | |
87 <pre>${HTML_ESCAPE.convert(testCase.message)}</pre> | |
88 </td> | |
89 </tr>'''; | |
90 | |
91 if (testCase.stackTrace != null) { | |
92 html = '$html<tr><td></td><td colspan="2"><pre>' + | |
93 HTML_ESCAPE.convert(testCase.stackTrace.toString()) + | |
94 '</pre></td></tr>'; | |
95 } | |
96 | |
97 return html; | |
98 } | |
99 | |
100 class HtmlConfiguration extends SimpleConfiguration { | |
101 /// Whether this is run within dartium layout tests. | |
102 final bool _isLayoutTest; | |
103 HtmlConfiguration(this._isLayoutTest); | |
104 | |
105 StreamSubscription<Event> _onErrorSubscription; | |
106 StreamSubscription<Event> _onMessageSubscription; | |
107 | |
108 void _installHandlers() { | |
109 if (_onErrorSubscription == null) { | |
110 _onErrorSubscription = window.onError.listen((e) { | |
111 // Some tests may expect this and have no way to suppress the error. | |
112 if (js.context['testExpectsGlobalError'] != true) { | |
113 handleExternalError(e, '(DOM callback has errors)'); | |
114 } | |
115 }); | |
116 } | |
117 if (_onMessageSubscription == null) { | |
118 _onMessageSubscription = | |
119 window.onMessage.listen((e) => processMessage(e)); | |
120 } | |
121 } | |
122 | |
123 void _uninstallHandlers() { | |
124 if (_onErrorSubscription != null) { | |
125 _onErrorSubscription.cancel(); | |
126 _onErrorSubscription = null; | |
127 } | |
128 if (_onMessageSubscription != null) { | |
129 _onMessageSubscription.cancel(); | |
130 _onMessageSubscription = null; | |
131 } | |
132 } | |
133 | |
134 void processMessage(e) { | |
135 if ('unittest-suite-external-error' == e.data) { | |
136 handleExternalError('<unknown>', '(external error detected)'); | |
137 } | |
138 } | |
139 | |
140 void onInit() { | |
141 // For Dart internal tests, we want to turn off stack frame | |
142 // filtering, which we do with this meta-header. | |
143 var meta = querySelector('meta[name="dart.unittest"]'); | |
144 filterStacks = | |
145 meta == null ? true : !meta.content.contains('full-stack-traces'); | |
146 _installHandlers(); | |
147 window.postMessage('unittest-suite-wait-for-done', '*'); | |
148 } | |
149 | |
150 void onStart() { | |
151 // If the URL has a #testFilter=testName then filter tests to that. | |
152 // This is used to make it easy to run a single test- but is only intended | |
153 // for interactive debugging scenarios. | |
154 var hash = window.location.hash; | |
155 if (hash != null && hash.length > 1) { | |
156 var params = hash.substring(1).split('&'); | |
157 for (var param in params) { | |
158 var parts = param.split('='); | |
159 if (parts.length == 2 && parts[0] == 'testFilter') { | |
160 filterTests('^${parts[1]}'); | |
161 } | |
162 } | |
163 } | |
164 super.onStart(); | |
165 } | |
166 | |
167 void onSummary(int passed, int failed, int errors, List<TestCase> results, | |
168 String uncaughtError) { | |
169 _showResultsInPage( | |
170 passed, failed, errors, results, _isLayoutTest, uncaughtError); | |
171 } | |
172 | |
173 void onDone(bool success) { | |
174 _uninstallHandlers(); | |
175 window.postMessage('unittest-suite-done', '*'); | |
176 } | |
177 } | |
178 | |
179 void useHtmlConfiguration([bool isLayoutTest = false]) { | |
180 unittestConfiguration = isLayoutTest ? _singletonLayout : _singletonNotLayout; | |
181 } | |
182 | |
183 final _singletonLayout = new HtmlConfiguration(true); | |
184 final _singletonNotLayout = new HtmlConfiguration(false); | |
OLD | NEW |