| 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'); | 8 #library('unittest'); |
| 9 | 9 |
| 10 #import('dart:html'); | 10 #import('dart:html'); |
| 11 #import('unittest.dart'); | 11 #import('unittest.dart'); |
| 12 | 12 |
| 13 #source('html_print.dart'); | 13 #source('html_print.dart'); |
| 14 | 14 |
| 15 class HtmlConfiguration extends Configuration { | 15 class HtmlConfiguration extends Configuration { |
| 16 /** Whether this is run within dartium layout tests. */ | 16 /** Whether this is run within dartium layout tests. */ |
| 17 final bool _isLayoutTest; | 17 final bool _isLayoutTest; |
| 18 HtmlConfiguration(this._isLayoutTest); | 18 HtmlConfiguration(this._isLayoutTest); |
| 19 | 19 |
| 20 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. | 20 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. |
| 21 EventListener _onErrorClosure; | 21 EventListener _onErrorClosure; |
| 22 EventListener _onMessageClosure; |
| 22 | 23 |
| 23 void _installErrorHandler() { | 24 void _installHandlers() { |
| 24 if (_onErrorClosure == null) { | 25 if (_onErrorClosure == null) { |
| 25 _onErrorClosure = | 26 _onErrorClosure = |
| 26 (e) => handleExternalError(e, '(DOM callback has errors)'); | 27 (e) => handleExternalError(e, '(DOM callback has errors)'); |
| 27 // Listen for uncaught errors. | 28 // Listen for uncaught errors. |
| 28 window.on.error.add(_onErrorClosure); | 29 window.on.error.add(_onErrorClosure); |
| 29 } | 30 } |
| 31 if (_onMessageClosure == null) { |
| 32 _onMessageClosure = (e) => processMessage(e); |
| 33 // Listen for errors from JS. |
| 34 window.on.message.add(_onMessageClosure); |
| 35 } |
| 30 } | 36 } |
| 31 | 37 |
| 32 void _uninstallErrorHandler() { | 38 void _uninstallHandlers() { |
| 33 if (_onErrorClosure != null) { | 39 if (_onErrorClosure != null) { |
| 34 window.on.error.remove(_onErrorClosure); | 40 window.on.error.remove(_onErrorClosure); |
| 35 _onErrorClosure = null; | 41 _onErrorClosure = null; |
| 36 } | 42 } |
| 43 if (_onMessageClosure != null) { |
| 44 window.on.message.remove(_onMessageClosure); |
| 45 _onMessageClosure = null; |
| 46 } |
| 47 } |
| 48 |
| 49 void processMessage(e) { |
| 50 if ('unittest-suite-external-error' == e.data) { |
| 51 handleExternalError('<unknown>', '(external error detected)'); |
| 52 } |
| 37 } | 53 } |
| 38 | 54 |
| 39 void onInit() { | 55 void onInit() { |
| 40 _installErrorHandler(); | 56 _installHandlers(); |
| 41 } | 57 } |
| 42 | 58 |
| 43 void onStart() { | 59 void onStart() { |
| 44 window.postMessage('unittest-suite-wait-for-done', '*'); | 60 window.postMessage('unittest-suite-wait-for-done', '*'); |
| 45 } | 61 } |
| 46 | 62 |
| 47 void onTestResult(TestCase testCase) {} | 63 void onTestResult(TestCase testCase) {} |
| 48 | 64 |
| 49 void onDone(int passed, int failed, int errors, List<TestCase> results, | 65 void onDone(int passed, int failed, int errors, List<TestCase> results, |
| 50 String uncaughtError) { | 66 String uncaughtError) { |
| 51 _uninstallErrorHandler(); | 67 _uninstallHandlers(); |
| 52 _showResultsInPage(passed, failed, errors, results, _isLayoutTest, | 68 _showResultsInPage(passed, failed, errors, results, _isLayoutTest, |
| 53 uncaughtError); | 69 uncaughtError); |
| 54 window.postMessage('unittest-suite-done', '*'); | 70 window.postMessage('unittest-suite-done', '*'); |
| 55 } | 71 } |
| 56 } | 72 } |
| 57 | 73 |
| 58 void useHtmlConfiguration([bool isLayoutTest = false]) { | 74 void useHtmlConfiguration([bool isLayoutTest = false]) { |
| 59 configure(new HtmlConfiguration(isLayoutTest)); | 75 configure(new HtmlConfiguration(isLayoutTest)); |
| 60 } | 76 } |
| OLD | NEW |