OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// A simple unit test library for running tests in a browser. | 5 @deprecated |
6 library unittest.html_config; | 6 library unittest.html_config; |
7 | 7 |
8 import 'dart:async'; | 8 import 'src/simple_configuration.dart'; |
9 import 'dart:convert'; | |
10 import 'dart:html'; | |
11 import 'dart:js' as js; | |
12 import 'unittest.dart'; | |
13 | 9 |
14 /// Creates a table showing tests results in HTML. | 10 /// This is a stub class used to preserve compatibility with unittest 0.11.*. |
15 void _showResultsInPage(int passed, int failed, int errors, | 11 /// |
16 List<TestCase> results, bool isLayoutTest, String uncaughtError) { | 12 /// It will be removed before the next version is released. |
17 if (isLayoutTest && (passed == results.length) && uncaughtError == null) { | 13 @deprecated |
18 document.body.innerHtml = "PASS"; | 14 class HtmlConfiguration extends SimpleConfiguration { |
19 } else { | 15 HtmlConfiguration(bool isLayoutTest); |
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 | 16 |
26 for (final test_ in results) { | 17 void processMessage(e) {} |
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 } | 18 } |
64 | 19 |
65 String _toHtml(TestCase test_) { | 20 @deprecated |
66 if (!test_.isComplete) { | 21 void useHtmlConfiguration([bool isLayoutTest]) {} |
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((e) { | |
102 // Some tests may expect this and have no way to suppress the error. | |
103 if (js.context['testExpectsGlobalError'] != true) { | |
104 handleExternalError(e, '(DOM callback has errors)'); | |
105 } | |
106 }); | |
107 } | |
108 if (_onMessageSubscription == null) { | |
109 _onMessageSubscription = | |
110 window.onMessage.listen((e) => processMessage(e)); | |
111 } | |
112 } | |
113 | |
114 void _uninstallHandlers() { | |
115 if (_onErrorSubscription != null) { | |
116 _onErrorSubscription.cancel(); | |
117 _onErrorSubscription = null; | |
118 } | |
119 if (_onMessageSubscription != null) { | |
120 _onMessageSubscription.cancel(); | |
121 _onMessageSubscription = null; | |
122 } | |
123 } | |
124 | |
125 void processMessage(e) { | |
126 if ('unittest-suite-external-error' == e.data) { | |
127 handleExternalError('<unknown>', '(external error detected)'); | |
128 } | |
129 } | |
130 | |
131 void onInit() { | |
132 // For Dart internal tests, we want to turn off stack frame | |
133 // filtering, which we do with this meta-header. | |
134 var meta = querySelector('meta[name="dart.unittest"]'); | |
135 filterStacks = | |
136 meta == null ? true : !meta.content.contains('full-stack-traces'); | |
137 _installHandlers(); | |
138 window.postMessage('unittest-suite-wait-for-done', '*'); | |
139 } | |
140 | |
141 void onStart() { | |
142 // If the URL has a #testFilter=testName then filter tests to that. | |
143 // This is used to make it easy to run a single test- but is only intended | |
144 // for interactive debugging scenarios. | |
145 var hash = window.location.hash; | |
146 if (hash != null && hash.length > 1) { | |
147 var params = hash.substring(1).split('&'); | |
148 for (var param in params) { | |
149 var parts = param.split('='); | |
150 if (parts.length == 2 && parts[0] == 'testFilter') { | |
151 filterTests('^${parts[1]}'); | |
152 } | |
153 } | |
154 } | |
155 super.onStart(); | |
156 } | |
157 | |
158 void onSummary(int passed, int failed, int errors, List<TestCase> results, | |
159 String uncaughtError) { | |
160 _showResultsInPage( | |
161 passed, failed, errors, results, _isLayoutTest, uncaughtError); | |
162 } | |
163 | |
164 void onDone(bool success) { | |
165 _uninstallHandlers(); | |
166 window.postMessage('unittest-suite-done', '*'); | |
167 } | |
168 } | |
169 | |
170 void useHtmlConfiguration([bool isLayoutTest = false]) { | |
171 unittestConfiguration = isLayoutTest ? _singletonLayout : _singletonNotLayout; | |
172 } | |
173 | |
174 final _singletonLayout = new HtmlConfiguration(true); | |
175 final _singletonNotLayout = new HtmlConfiguration(false); | |
OLD | NEW |