| 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 new Future.immediate(null).then((_) => _onTickController.add(tickTime)); | 60 new Future.immediate(null).then((_) => _onTickController.add(tickTime)); |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 | 63 |
| 64 /// Automatically progresses forward in time as long as there are still | 64 /// Automatically progresses forward in time as long as there are still |
| 65 /// subscribers to [onTick] (that is, [Timer]s waiting to fire). After each | 65 /// subscribers to [onTick] (that is, [Timer]s waiting to fire). After each |
| 66 /// tick, this pumps the event loop repeatedly so that all non-clock-dependent | 66 /// tick, this pumps the event loop repeatedly so that all non-clock-dependent |
| 67 /// code runs before the next tick. | 67 /// code runs before the next tick. |
| 68 void run() { | 68 void run() { |
| 69 pumpEventQueue().then((_) { | 69 pumpEventQueue().then((_) { |
| 70 if (!_onTickController.hasSubscribers) return; | 70 if (!_onTickController.hasListener) return; |
| 71 tick(); | 71 tick(); |
| 72 return run(); | 72 return run(); |
| 73 }); | 73 }); |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 /// A mock implementation of [Timer] that uses [Clock] to keep time, rather than | 77 /// A mock implementation of [Timer] that uses [Clock] to keep time, rather than |
| 78 /// the system clock. | 78 /// the system clock. |
| 79 class _MockTimer implements Timer { | 79 class _MockTimer implements Timer { |
| 80 /// The time at which the timer should fire. | 80 /// The time at which the timer should fire. |
| 81 final int _time; | 81 final int _time; |
| 82 | 82 |
| 83 /// The callback to run when the timer fires. | 83 /// The callback to run when the timer fires. |
| 84 final TimerCallback _callback; | 84 final TimerCallback _callback; |
| 85 | 85 |
| 86 /// The subscription to the [Clock.onTick] stream. | 86 /// The subscription to the [Clock.onTick] stream. |
| 87 StreamSubscription _subscription; | 87 StreamSubscription _subscription; |
| 88 | 88 |
| 89 _MockTimer(Duration duration, this._callback) | 89 _MockTimer(Duration duration, this._callback) |
| 90 : _time = _clock.time + duration.inMilliseconds { | 90 : _time = _clock.time + duration.inMilliseconds { |
| 91 _subscription = _clock.onTick.listen((time) { | 91 _subscription = _clock.onTick.listen((time) { |
| 92 if (time < _time) return; | 92 if (time < _time) return; |
| 93 _subscription.cancel(); | 93 _subscription.cancel(); |
| 94 _callback(); | 94 _callback(); |
| 95 }); | 95 }); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void cancel() => _subscription.cancel(); | 98 void cancel() => _subscription.cancel(); |
| 99 } | 99 } |
| OLD | NEW |