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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 | 50 |
51 Clock._(); | 51 Clock._(); |
52 | 52 |
53 /// Advances the clock forward by [milliseconds]. This works like synchronous | 53 /// Advances the clock forward by [milliseconds]. This works like synchronous |
54 /// code that takes [milliseconds] to execute; any [Timer]s that are scheduled | 54 /// code that takes [milliseconds] to execute; any [Timer]s that are scheduled |
55 /// to fire during the interval will do so asynchronously once control returns | 55 /// to fire during the interval will do so asynchronously once control returns |
56 /// to the event loop. | 56 /// to the event loop. |
57 void tick([int milliseconds=1]) { | 57 void tick([int milliseconds=1]) { |
58 for (var i = 0; i < milliseconds; i++) { | 58 for (var i = 0; i < milliseconds; i++) { |
59 var tickTime = ++_time; | 59 var tickTime = ++_time; |
60 new Future.immediate(null).then((_) => _onTickController.add(tickTime)); | 60 new Future.value().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.hasListener) return; | 70 if (!_onTickController.hasListener) return; |
(...skipping 19 matching lines...) Expand all Loading... |
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 |