| 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 12 matching lines...) Expand all Loading... |
| 23 /// are controlled by the returned [Clock]. This may only be called once. | 23 /// are controlled by the returned [Clock]. This may only be called once. |
| 24 Clock mock() { | 24 Clock mock() { |
| 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(Timer timer); | 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]. |
| 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. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 StreamSubscription _subscription; | 87 StreamSubscription _subscription; |
| 88 | 88 |
| 89 // TODO(nweiz): Remove this when issue 8512 is fixed. | 89 // TODO(nweiz): Remove this when issue 8512 is fixed. |
| 90 var _cancelled = false; | 90 var _cancelled = false; |
| 91 | 91 |
| 92 _MockTimer(Duration duration, this._callback) | 92 _MockTimer(Duration duration, this._callback) |
| 93 : _time = _clock.time + duration.inMilliseconds { | 93 : _time = _clock.time + duration.inMilliseconds { |
| 94 _subscription = _clock.onTick.listen((time) { | 94 _subscription = _clock.onTick.listen((time) { |
| 95 if (_cancelled || time < _time) return; | 95 if (_cancelled || time < _time) return; |
| 96 _subscription.cancel(); | 96 _subscription.cancel(); |
| 97 _callback(this); | 97 _callback(); |
| 98 }); | 98 }); |
| 99 } | 99 } |
| 100 | 100 |
| 101 void cancel() { | 101 void cancel() { |
| 102 _cancelled = true; | 102 _cancelled = true; |
| 103 _subscription.cancel(); | 103 _subscription.cancel(); |
| 104 } | 104 } |
| 105 } | 105 } |
| OLD | NEW |