| 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 * Provides enhanced HTML output with collapsible group headers | 8 * Provides enhanced HTML output with collapsible group headers |
| 9 * and other at-a-glance information about the test results. | 9 * and other at-a-glance information about the test results. |
| 10 */ | 10 */ |
| 11 #library('unittest'); | 11 #library('unittest'); |
| 12 | 12 |
| 13 #import('dart:html'); | 13 #import('dart:html'); |
| 14 #import('unittest.dart'); | 14 #import('unittest.dart'); |
| 15 | 15 |
| 16 | 16 |
| 17 class HtmlEnhancedConfiguration extends Configuration { | 17 class HtmlEnhancedConfiguration extends Configuration { |
| 18 /** Whether this is run within dartium layout tests. */ | 18 /** Whether this is run within dartium layout tests. */ |
| 19 final bool _isLayoutTest; | 19 final bool _isLayoutTest; |
| 20 HtmlEnhancedConfiguration(this._isLayoutTest); | 20 HtmlEnhancedConfiguration(this._isLayoutTest); |
| 21 | 21 |
| 22 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. | 22 // TODO(rnystrom): Get rid of this if we get canonical closures for methods. |
| 23 EventListener _onErrorClosure; | 23 EventListener _onErrorClosure; |
| 24 EventListener _onMessageClosure; |
| 24 | 25 |
| 25 void _installErrorHandler() { | 26 void _installHandlers() { |
| 26 if (_onErrorClosure == null) { | 27 if (_onErrorClosure == null) { |
| 27 _onErrorClosure = | 28 _onErrorClosure = |
| 28 (e) => handleExternalError(e, '(DOM callback has errors)'); | 29 (e) => handleExternalError(e, '(DOM callback has errors)'); |
| 29 // Listen for uncaught errors. | 30 // Listen for uncaught errors. |
| 30 window.on.error.add(_onErrorClosure); | 31 window.on.error.add(_onErrorClosure); |
| 31 } | 32 } |
| 33 if (_onMessageClosure == null) { |
| 34 _onMessageClosure = (e) => processMessage(e); |
| 35 // Listen for errors from JS. |
| 36 window.on.message.add(_onMessageClosure); |
| 37 } |
| 32 } | 38 } |
| 33 | 39 |
| 34 void _uninstallErrorHandler() { | 40 void _uninstallHandlers() { |
| 35 if (_onErrorClosure != null) { | 41 if (_onErrorClosure != null) { |
| 36 window.on.error.remove(_onErrorClosure); | 42 window.on.error.remove(_onErrorClosure); |
| 37 _onErrorClosure = null; | 43 _onErrorClosure = null; |
| 38 } | 44 } |
| 45 if (_onMessageClosure != null) { |
| 46 window.on.message.remove(_onMessageClosure); |
| 47 _onMessageClosure = null; |
| 48 } |
| 39 } | 49 } |
| 40 | 50 |
| 41 void onInit() { | 51 void onInit() { |
| 42 _installErrorHandler(); | 52 _installHandlers(); |
| 43 //initialize and load CSS | 53 //initialize and load CSS |
| 44 final String _CSSID = '_unittestcss_'; | 54 final String _CSSID = '_unittestcss_'; |
| 45 | 55 |
| 46 var cssElement = document.head.query('#${_CSSID}'); | 56 var cssElement = document.head.query('#${_CSSID}'); |
| 47 if (cssElement == null){ | 57 if (cssElement == null){ |
| 48 document.head.elements.add(new Element.html( | 58 document.head.elements.add(new Element.html( |
| 49 '<style id="${_CSSID}"></style>')); | 59 '<style id="${_CSSID}"></style>')); |
| 50 cssElement = document.head.query('#${_CSSID}'); | 60 cssElement = document.head.query('#${_CSSID}'); |
| 51 } | 61 } |
| 52 | 62 |
| 53 cssElement.innerHTML = _htmlTestCSS; | 63 cssElement.innerHTML = _htmlTestCSS; |
| 54 } | 64 } |
| 55 | 65 |
| 56 void onStart() { | 66 void onStart() { |
| 57 window.postMessage('unittest-suite-wait-for-done', '*'); | 67 window.postMessage('unittest-suite-wait-for-done', '*'); |
| 58 // Listen for uncaught errors. | 68 // Listen for uncaught errors. |
| 59 window.on.error.add(_onErrorClosure); | 69 window.on.error.add(_onErrorClosure); |
| 60 } | 70 } |
| 61 | 71 |
| 62 void onTestResult(TestCase testCase) {} | 72 void onTestResult(TestCase testCase) {} |
| 63 | 73 |
| 64 void onDone(int passed, int failed, int errors, List<TestCase> results, | 74 void onDone(int passed, int failed, int errors, List<TestCase> results, |
| 65 String uncaughtError) { | 75 String uncaughtError) { |
| 66 _uninstallErrorHandler(); | 76 _uninstallHandlers(); |
| 67 | 77 |
| 68 _showInteractiveResultsInPage(passed, failed, errors, results, | 78 _showInteractiveResultsInPage(passed, failed, errors, results, |
| 69 _isLayoutTest, uncaughtError); | 79 _isLayoutTest, uncaughtError); |
| 70 | 80 |
| 71 window.postMessage('unittest-suite-done', '*'); | 81 window.postMessage('unittest-suite-done', '*'); |
| 72 } | 82 } |
| 73 | 83 |
| 74 void _showInteractiveResultsInPage(int passed, int failed, int errors, | 84 void _showInteractiveResultsInPage(int passed, int failed, int errors, |
| 75 List<TestCase> results, bool isLayoutTest, String uncaughtError) { | 85 List<TestCase> results, bool isLayoutTest, String uncaughtError) { |
| 76 if (isLayoutTest && passed == results.length) { | 86 if (isLayoutTest && passed == results.length) { |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 .unittest-row-description | 397 .unittest-row-description |
| 388 { | 398 { |
| 389 } | 399 } |
| 390 | 400 |
| 391 '''; | 401 '''; |
| 392 } | 402 } |
| 393 | 403 |
| 394 void useHtmlEnhancedConfiguration([bool isLayoutTest = false]) { | 404 void useHtmlEnhancedConfiguration([bool isLayoutTest = false]) { |
| 395 configure(new HtmlEnhancedConfiguration(isLayoutTest)); | 405 configure(new HtmlEnhancedConfiguration(isLayoutTest)); |
| 396 } | 406 } |
| OLD | NEW |