| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 library test.runner.runner_suite; | 5 library test.runner.runner_suite; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:async/async.dart'; | 9 import 'package:async/async.dart'; |
| 10 | 10 |
| 11 import '../backend/operating_system.dart'; | 11 import '../backend/operating_system.dart'; |
| 12 import '../backend/suite.dart'; | 12 import '../backend/suite.dart'; |
| 13 import '../backend/group.dart'; | 13 import '../backend/group.dart'; |
| 14 import '../backend/test.dart'; | 14 import '../backend/test.dart'; |
| 15 import '../backend/test_platform.dart'; | 15 import '../backend/test_platform.dart'; |
| 16 import '../utils.dart'; | 16 import '../utils.dart'; |
| 17 import 'environment.dart'; | 17 import 'environment.dart'; |
| 18 | 18 |
| 19 /// A suite produced and consumed by the test runner that has runner-specific | 19 /// A suite produced and consumed by the test runner that has runner-specific |
| 20 /// logic and lifecycle management. | 20 /// logic and lifecycle management. |
| 21 /// | 21 /// |
| 22 /// This is separated from [Suite] because the backend library (which will | 22 /// This is separated from [Suite] because the backend library (which will |
| 23 /// eventually become its own package) is primarily for test code itself to use, | 23 /// eventually become its own package) is primarily for test code itself to use, |
| 24 /// for which the [RunnerSuite] APIs don't make sense. | 24 /// for which the [RunnerSuite] APIs don't make sense. |
| 25 /// | |
| 26 /// A [RunnerSuite] can be produced and controlled using a | |
| 27 /// [RunnerSuiteController]. | |
| 28 class RunnerSuite extends Suite { | 25 class RunnerSuite extends Suite { |
| 29 final RunnerSuiteController _controller; | 26 final Environment environment; |
| 30 | 27 |
| 31 /// The environment in which this suite runs. | 28 /// The memoizer for running [close] exactly once. |
| 32 Environment get environment => _controller._environment; | 29 final _closeMemo = new AsyncMemoizer(); |
| 33 | 30 |
| 34 /// Whether the suite is paused for debugging. | 31 /// The function to call when the suite is closed. |
| 35 /// | 32 final AsyncFunction _onClose; |
| 36 /// When using a dev inspector, this may also mean that the entire browser is | |
| 37 /// paused. | |
| 38 bool get isDebugging => _controller._isDebugging; | |
| 39 | 33 |
| 40 /// A broadcast stream that emits an event whenever the suite is paused for | 34 RunnerSuite(this.environment, Group group, {String path, |
| 41 /// debugging or resumed afterwards. | 35 TestPlatform platform, OperatingSystem os, AsyncFunction onClose}) |
| 42 /// | 36 : _onClose = onClose, |
| 43 /// The event is `true` when debugging starts and `false` when it ends. | 37 super(group, path: path, platform: platform, os: os); |
| 44 Stream<bool> get onDebugging => _controller._onDebuggingController.stream; | |
| 45 | |
| 46 /// A shortcut constructor for creating a [RunnerSuite] that never goes into | |
| 47 /// debugging mode. | |
| 48 factory RunnerSuite(Environment environment, Group group, {String path, | |
| 49 TestPlatform platform, OperatingSystem os, AsyncFunction onClose}) { | |
| 50 var controller = new RunnerSuiteController(environment, group, | |
| 51 path: path, platform: platform, os: os, onClose: onClose); | |
| 52 return controller.suite; | |
| 53 } | |
| 54 | |
| 55 RunnerSuite._(this._controller, Group group, String path, | |
| 56 TestPlatform platform, OperatingSystem os) | |
| 57 : super(group, path: path, platform: platform, os: os); | |
| 58 | 38 |
| 59 RunnerSuite filter(bool callback(Test test)) { | 39 RunnerSuite filter(bool callback(Test test)) { |
| 60 var filtered = group.filter(callback); | 40 var filtered = group.filter(callback); |
| 61 filtered ??= new Group.root([], metadata: metadata); | 41 filtered ??= new Group.root([], metadata: metadata); |
| 62 return new RunnerSuite._(_controller, filtered, path, platform, os); | 42 return new RunnerSuite(environment, filtered, |
| 43 platform: platform, os: os, path: path); |
| 63 } | 44 } |
| 64 | 45 |
| 65 /// Closes the suite and releases any resources associated with it. | 46 /// Closes the suite and releases any resources associated with it. |
| 66 Future close() => _controller._close(); | 47 Future close() { |
| 48 return _closeMemo.runOnce(() async { |
| 49 if (_onClose != null) await _onClose(); |
| 50 }); |
| 51 } |
| 67 } | 52 } |
| 68 | |
| 69 /// A class that exposes and controls a [RunnerSuite]. | |
| 70 class RunnerSuiteController { | |
| 71 /// The suite controlled by this controller. | |
| 72 RunnerSuite get suite => _suite; | |
| 73 RunnerSuite _suite; | |
| 74 | |
| 75 /// The backing value for [suite.environment]. | |
| 76 final Environment _environment; | |
| 77 | |
| 78 /// The function to call when the suite is closed. | |
| 79 final AsyncFunction _onClose; | |
| 80 | |
| 81 /// The backing value for [suite.isDebugging]. | |
| 82 bool _isDebugging = false; | |
| 83 | |
| 84 /// The controller for [suite.onDebugging]. | |
| 85 final _onDebuggingController = new StreamController<bool>.broadcast(); | |
| 86 | |
| 87 RunnerSuiteController(this._environment, Group group, {String path, | |
| 88 TestPlatform platform, OperatingSystem os, AsyncFunction onClose}) | |
| 89 : _onClose = onClose { | |
| 90 _suite = new RunnerSuite._(this, group, path, platform, os); | |
| 91 } | |
| 92 | |
| 93 /// Sets whether the suite is paused for debugging. | |
| 94 /// | |
| 95 /// If this is different than [suite.isDebugging], this will automatically | |
| 96 /// send out an event along [suite.onDebugging]. | |
| 97 void setDebugging(bool debugging) { | |
| 98 if (debugging == _isDebugging) return; | |
| 99 _isDebugging = debugging; | |
| 100 _onDebuggingController.add(debugging); | |
| 101 } | |
| 102 | |
| 103 /// The backing function for [suite.close]. | |
| 104 Future _close() => _closeMemo.runOnce(() async { | |
| 105 _onDebuggingController.close(); | |
| 106 if (_onClose != null) await _onClose(); | |
| 107 }); | |
| 108 final _closeMemo = new AsyncMemoizer(); | |
| 109 } | |
| OLD | NEW |