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