| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 /** | 5 /** |
| 6 * A simple unit test library for running tests in a browser. | 6 * A simple unit test library for running tests in a browser. |
| 7 */ | 7 */ |
| 8 library unittest_html_config; | 8 library unittest_html_config; |
| 9 | 9 |
| 10 import 'dart:async'; |
| 10 import 'dart:html'; | 11 import 'dart:html'; |
| 11 import 'unittest.dart'; | 12 import 'unittest.dart'; |
| 12 | 13 |
| 13 /** Creates a table showing tests results in HTML. */ | 14 /** Creates a table showing tests results in HTML. */ |
| 14 void _showResultsInPage(int passed, int failed, int errors, | 15 void _showResultsInPage(int passed, int failed, int errors, |
| 15 List<TestCase> results, bool isLayoutTest, String uncaughtError) { | 16 List<TestCase> results, bool isLayoutTest, String uncaughtError) { |
| 16 if (isLayoutTest && (passed == results.length) && uncaughtError == null) { | 17 if (isLayoutTest && (passed == results.length) && uncaughtError == null) { |
| 17 document.body.innerHtml = "PASS"; | 18 document.body.innerHtml = "PASS"; |
| 18 } else { | 19 } else { |
| 19 var newBody = new StringBuffer(); | 20 var newBody = new StringBuffer(); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 return string.replaceAll('&', '&') | 83 return string.replaceAll('&', '&') |
| 83 .replaceAll('<','<') | 84 .replaceAll('<','<') |
| 84 .replaceAll('>','>'); | 85 .replaceAll('>','>'); |
| 85 } | 86 } |
| 86 | 87 |
| 87 class HtmlConfiguration extends Configuration { | 88 class HtmlConfiguration extends Configuration { |
| 88 /** Whether this is run within dartium layout tests. */ | 89 /** Whether this is run within dartium layout tests. */ |
| 89 final bool _isLayoutTest; | 90 final bool _isLayoutTest; |
| 90 HtmlConfiguration(this._isLayoutTest); | 91 HtmlConfiguration(this._isLayoutTest); |
| 91 | 92 |
| 92 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. | 93 StreamSubscription<Event> _onErrorSubscription; |
| 93 EventListener _onErrorClosure; | 94 StreamSubscription<Event> _onMessageSubscription; |
| 94 EventListener _onMessageClosure; | |
| 95 | 95 |
| 96 void _installHandlers() { | 96 void _installHandlers() { |
| 97 if (_onErrorClosure == null) { | 97 if (_onErrorSubscription == null) { |
| 98 _onErrorClosure = | 98 _onErrorSubscription = window.onError.listen( |
| 99 (e) => handleExternalError(e, '(DOM callback has errors)'); | 99 (e) => handleExternalError(e, '(DOM callback has errors)')); |
| 100 // Listen for uncaught errors. | |
| 101 window.on.error.add(_onErrorClosure); | |
| 102 } | 100 } |
| 103 if (_onMessageClosure == null) { | 101 if (_onMessageSubscription == null) { |
| 104 _onMessageClosure = (e) => processMessage(e); | 102 _onMessageSubscription = window.onMessage.listen( |
| 105 // Listen for errors from JS. | 103 (e) => processMessage(e)); |
| 106 window.on.message.add(_onMessageClosure); | |
| 107 } | 104 } |
| 108 } | 105 } |
| 109 | 106 |
| 110 void _uninstallHandlers() { | 107 void _uninstallHandlers() { |
| 111 if (_onErrorClosure != null) { | 108 if (_onErrorSubscription != null) { |
| 112 window.on.error.remove(_onErrorClosure); | 109 _onErrorSubscription.cancel(); |
| 113 _onErrorClosure = null; | 110 _onErrorSubscription = null; |
| 114 } | 111 } |
| 115 if (_onMessageClosure != null) { | 112 if (_onMessageSubscription != null) { |
| 116 window.on.message.remove(_onMessageClosure); | 113 _onMessageSubscription.cancel(); |
| 117 _onMessageClosure = null; | 114 _onMessageSubscription = null; |
| 118 } | 115 } |
| 119 } | 116 } |
| 120 | 117 |
| 121 void processMessage(e) { | 118 void processMessage(e) { |
| 122 if ('unittest-suite-external-error' == e.data) { | 119 if ('unittest-suite-external-error' == e.data) { |
| 123 handleExternalError('<unknown>', '(external error detected)'); | 120 handleExternalError('<unknown>', '(external error detected)'); |
| 124 } | 121 } |
| 125 } | 122 } |
| 126 | 123 |
| 127 void onInit() { | 124 void onInit() { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 140 void onDone(bool success) { | 137 void onDone(bool success) { |
| 141 _uninstallHandlers(); | 138 _uninstallHandlers(); |
| 142 window.postMessage('unittest-suite-done', '*'); | 139 window.postMessage('unittest-suite-done', '*'); |
| 143 } | 140 } |
| 144 } | 141 } |
| 145 | 142 |
| 146 void useHtmlConfiguration([bool isLayoutTest = false]) { | 143 void useHtmlConfiguration([bool isLayoutTest = false]) { |
| 147 if (config != null) return; | 144 if (config != null) return; |
| 148 configure(new HtmlConfiguration(isLayoutTest)); | 145 configure(new HtmlConfiguration(isLayoutTest)); |
| 149 } | 146 } |
| OLD | NEW |