| 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 // TODO(nweiz): Remove this when issue 8512 is fixed. | |
| 90 var _cancelled = false; | |
| 91 | |
| 92 _MockTimer(Duration duration, this._callback) | 89 _MockTimer(Duration duration, this._callback) |
| 93 : _time = _clock.time + duration.inMilliseconds { | 90 : _time = _clock.time + duration.inMilliseconds { |
| 94 _subscription = _clock.onTick.listen((time) { | 91 _subscription = _clock.onTick.listen((time) { |
| 95 if (_cancelled || time < _time) return; | 92 if (time < _time) return; |
| 96 _subscription.cancel(); | 93 _subscription.cancel(); |
| 97 _callback(); | 94 _callback(); |
| 98 }); | 95 }); |
| 99 } | 96 } |
| 100 | 97 |
| 101 void cancel() { | 98 void cancel() => _subscription.cancel(); |
| 102 _cancelled = true; | |
| 103 _subscription.cancel(); | |
| 104 } | |
| 105 } | 99 } |
| OLD | NEW |