Chromium Code Reviews| 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 14 matching lines...) Expand all Loading... | |
| 25 if (_mocked) { | 25 if (_mocked) { |
| 26 throw new StateError("mock_clock.mock() has already been called."); | 26 throw new StateError("mock_clock.mock() has already been called."); |
| 27 } | 27 } |
| 28 | 28 |
| 29 _clock = new Clock._(); | 29 _clock = new Clock._(); |
| 30 return _clock; | 30 return _clock; |
| 31 } | 31 } |
| 32 | 32 |
| 33 typedef void TimerCallback(); | 33 typedef void TimerCallback(); |
| 34 | 34 |
| 35 /// Returns a new (possibly mocked) [Timer]. Works the same as [new Timer]. | 35 /// Returns a new (possibly mocked) [Timer]. Works the same as `new Timer`. |
|
nweiz
2013/10/30 19:28:39
Also reference the issue number here.
| |
| 36 Timer newTimer(Duration duration, TimerCallback callback) => | 36 Timer newTimer(Duration duration, TimerCallback callback) => |
| 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; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 65 }); | 65 }); |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 | 68 |
| 69 /// Automatically progresses forward in time as long as there are still | 69 /// Automatically progresses forward in time as long as there are still |
| 70 /// subscribers to [onTick] (that is, [Timer]s waiting to fire). After each | 70 /// subscribers to [onTick] (that is, [Timer]s waiting to fire). After each |
| 71 /// tick, this pumps the event loop repeatedly so that all non-clock-dependent | 71 /// tick, this pumps the event loop repeatedly so that all non-clock-dependent |
| 72 /// code runs before the next tick. | 72 /// code runs before the next tick. |
| 73 void run() { | 73 void run() { |
| 74 pumpEventQueue().then((_) { | 74 pumpEventQueue().then((_) { |
| 75 if (!_broadcastController.hasListener) return; | 75 if (!_broadcastController.hasListener) return null; |
| 76 tick(); | 76 tick(); |
| 77 return run(); | 77 return run(); |
| 78 }); | 78 }); |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 | 81 |
| 82 /// A mock implementation of [Timer] that uses [Clock] to keep time, rather than | 82 /// A mock implementation of [Timer] that uses [Clock] to keep time, rather than |
| 83 /// the system clock. | 83 /// the system clock. |
| 84 class _MockTimer implements Timer { | 84 class _MockTimer implements Timer { |
| 85 /// The time at which the timer should fire. | 85 /// The time at which the timer should fire. |
| 86 final int _time; | 86 final int _time; |
| 87 | 87 |
| 88 /// The callback to run when the timer fires. | 88 /// The callback to run when the timer fires. |
| 89 final TimerCallback _callback; | 89 final TimerCallback _callback; |
| 90 | 90 |
| 91 /// The subscription to the [Clock.onTick] stream. | 91 /// The subscription to the [Clock.onTick] stream. |
| 92 StreamSubscription _subscription; | 92 StreamSubscription _subscription; |
| 93 | 93 |
| 94 _MockTimer(Duration duration, this._callback) | 94 _MockTimer(Duration duration, this._callback) |
| 95 : _time = _clock.time + duration.inMilliseconds { | 95 : _time = _clock.time + duration.inMilliseconds { |
| 96 _subscription = _clock.onTick.listen((time) { | 96 _subscription = _clock.onTick.listen((time) { |
| 97 if (time < _time) return; | 97 if (time < _time) return; |
| 98 _subscription.cancel(); | 98 _subscription.cancel(); |
| 99 _callback(); | 99 _callback(); |
| 100 }); | 100 }); |
| 101 } | 101 } |
| 102 | 102 |
| 103 void cancel() => _subscription.cancel(); | 103 bool get isActive => _subscription != null; |
| 104 | |
| 105 void cancel() { | |
| 106 _subscription.cancel(); | |
| 107 _subscription = null; | |
| 108 } | |
| 104 } | 109 } |
| OLD | NEW |