| 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 library that wraps [Timer] in a way that can be mocked out in test code. | 5 /// A library that wraps [Timer] in a way that can be mocked out in test code. |
| 6 /// Application code only needs to use [newTimer] to get an instance of [Timer]. | 6 /// Application code only needs to use [newTimer] to get an instance of [Timer]. |
| 7 /// Then test code can call [mock] to mock out all new [Timer] instances so that | 7 /// Then test code can call [mock] to mock out all new [Timer] instances so that |
| 8 /// they're controllable by a returned [Clock] object. | 8 /// they're controllable by a returned [Clock] object. |
| 9 library mock_clock; | 9 library mock_clock; |
| 10 | 10 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 _mocked ? new _MockTimer(duration, callback) : new Timer(duration, callback); | 37 _mocked ? new _MockTimer(duration, callback) : new Timer(duration, callback); |
| 38 | 38 |
| 39 /// A clock that controls when mocked [Timer]s move forward in time. It starts | 39 /// A clock that controls when mocked [Timer]s move forward in time. It starts |
| 40 /// at time 0 and advances forward millisecond-by-millisecond, broadcasting each | 40 /// at time 0 and advances forward millisecond-by-millisecond, broadcasting each |
| 41 /// tick on the [onTick] stream. | 41 /// tick on the [onTick] stream. |
| 42 class Clock { | 42 class Clock { |
| 43 /// The current time of the clock, in milliseconds. Starts at 0. | 43 /// The current time of the clock, in milliseconds. Starts at 0. |
| 44 int get time => _time; | 44 int get time => _time; |
| 45 int _time = 0; | 45 int _time = 0; |
| 46 | 46 |
| 47 /// Collection of controllers of all subscribed listeners. |
| 48 /// |
| 49 /// [StreamController] is not overriding [Object.operator==], so this is |
| 50 /// effectively an identity map. |
| 51 Set<StreamController> _subscriptions = new Set<StreamController>(); |
| 52 |
| 53 Clock._(); |
| 54 |
| 47 /// The stream of millisecond ticks of the clock. | 55 /// The stream of millisecond ticks of the clock. |
| 48 Stream<int> get onTick { | 56 Stream<int> get onTick { |
| 49 if (_onTickControllerStream == null) { | 57 StreamController<int> controller; |
| 50 _onTickControllerStream = _onTickController.stream.asBroadcastStream(); | 58 controller = new StreamController<int>( |
| 51 } | 59 onListen: () { |
| 52 return _onTickControllerStream; | 60 _subscriptions.add(controller); |
| 61 }, |
| 62 onCancel: () { |
| 63 _subscriptions.remove(controller); |
| 64 }); |
| 65 return controller.stream; |
| 53 } | 66 } |
| 54 | 67 |
| 55 final _onTickController = new StreamController<int>(); | |
| 56 Stream<int> _onTickControllerStream; | |
| 57 | |
| 58 Clock._(); | |
| 59 | 68 |
| 60 /// Advances the clock forward by [milliseconds]. This works like synchronous | 69 /// Advances the clock forward by [milliseconds]. This works like synchronous |
| 61 /// code that takes [milliseconds] to execute; any [Timer]s that are scheduled | 70 /// code that takes [milliseconds] to execute; any [Timer]s that are scheduled |
| 62 /// to fire during the interval will do so asynchronously once control returns | 71 /// to fire during the interval will do so asynchronously once control returns |
| 63 /// to the event loop. | 72 /// to the event loop. |
| 64 void tick([int milliseconds=1]) { | 73 void tick([int milliseconds = 1]) { |
| 65 for (var i = 0; i < milliseconds; i++) { | 74 for (var i = 0; i < milliseconds; i++) { |
| 66 var tickTime = ++_time; | 75 var tickTime = ++_time; |
| 67 new Future.value().then((_) => _onTickController.add(tickTime)); | 76 runAsync(() { |
| 77 List<StreamController> controllers = _subscriptions.toList(); |
| 78 for (StreamController controller in controllers) { |
| 79 if (_subscriptions.contains(controller)) { |
| 80 controller.add(tickTime); |
| 81 } |
| 82 } |
| 83 }); |
| 68 } | 84 } |
| 69 } | 85 } |
| 70 | 86 |
| 71 /// Automatically progresses forward in time as long as there are still | 87 /// Automatically progresses forward in time as long as there are still |
| 72 /// subscribers to [onTick] (that is, [Timer]s waiting to fire). After each | 88 /// subscribers to [onTick] (that is, [Timer]s waiting to fire). After each |
| 73 /// tick, this pumps the event loop repeatedly so that all non-clock-dependent | 89 /// tick, this pumps the event loop repeatedly so that all non-clock-dependent |
| 74 /// code runs before the next tick. | 90 /// code runs before the next tick. |
| 75 void run() { | 91 void run() { |
| 76 pumpEventQueue().then((_) { | 92 pumpEventQueue().then((_) { |
| 77 if (!_onTickController.hasListener) return; | 93 if (_subscriptions.isEmpty) return; |
| 78 tick(); | 94 tick(); |
| 79 return run(); | 95 return run(); |
| 80 }); | 96 }); |
| 81 } | 97 } |
| 82 } | 98 } |
| 83 | 99 |
| 84 /// A mock implementation of [Timer] that uses [Clock] to keep time, rather than | 100 /// A mock implementation of [Timer] that uses [Clock] to keep time, rather than |
| 85 /// the system clock. | 101 /// the system clock. |
| 86 class _MockTimer implements Timer { | 102 class _MockTimer implements Timer { |
| 87 /// The time at which the timer should fire. | 103 /// The time at which the timer should fire. |
| 88 final int _time; | 104 final int _time; |
| 89 | 105 |
| 90 /// The callback to run when the timer fires. | 106 /// The callback to run when the timer fires. |
| 91 final TimerCallback _callback; | 107 final TimerCallback _callback; |
| 92 | 108 |
| 93 /// The subscription to the [Clock.onTick] stream. | 109 /// The subscription to the [Clock.onTick] stream. |
| 94 StreamSubscription _subscription; | 110 StreamSubscription _subscription; |
| 95 | 111 |
| 96 _MockTimer(Duration duration, this._callback) | 112 _MockTimer(Duration duration, this._callback) |
| 97 : _time = _clock.time + duration.inMilliseconds { | 113 : _time = _clock.time + duration.inMilliseconds { |
| 98 _subscription = _clock.onTick.listen((time) { | 114 _subscription = _clock.onTick.listen((time) { |
| 99 if (time < _time) return; | 115 if (time < _time) return; |
| 100 _subscription.cancel(); | 116 _subscription.cancel(); |
| 101 _callback(); | 117 _callback(); |
| 102 }); | 118 }); |
| 103 } | 119 } |
| 104 | 120 |
| 105 void cancel() => _subscription.cancel(); | 121 void cancel() => _subscription.cancel(); |
| 106 } | 122 } |
| OLD | NEW |