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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 Clock._(); | 58 Clock._(); |
59 | 59 |
60 /// Advances the clock forward by [milliseconds]. This works like synchronous | 60 /// Advances the clock forward by [milliseconds]. This works like synchronous |
61 /// code that takes [milliseconds] to execute; any [Timer]s that are scheduled | 61 /// code that takes [milliseconds] to execute; any [Timer]s that are scheduled |
62 /// to fire during the interval will do so asynchronously once control returns | 62 /// to fire during the interval will do so asynchronously once control returns |
63 /// to the event loop. | 63 /// to the event loop. |
64 void tick([int milliseconds=1]) { | 64 void tick([int milliseconds=1]) { |
65 for (var i = 0; i < milliseconds; i++) { | 65 for (var i = 0; i < milliseconds; i++) { |
66 var tickTime = ++_time; | 66 var tickTime = ++_time; |
67 new Future.immediate(null).then((_) => _onTickController.add(tickTime)); | 67 new Future.value().then((_) => _onTickController.add(tickTime)); |
68 } | 68 } |
69 } | 69 } |
70 | 70 |
71 /// Automatically progresses forward in time as long as there are still | 71 /// Automatically progresses forward in time as long as there are still |
72 /// subscribers to [onTick] (that is, [Timer]s waiting to fire). After each | 72 /// subscribers to [onTick] (that is, [Timer]s waiting to fire). After each |
73 /// tick, this pumps the event loop repeatedly so that all non-clock-dependent | 73 /// tick, this pumps the event loop repeatedly so that all non-clock-dependent |
74 /// code runs before the next tick. | 74 /// code runs before the next tick. |
75 void run() { | 75 void run() { |
76 pumpEventQueue().then((_) { | 76 pumpEventQueue().then((_) { |
77 if (!_onTickController.hasListener) return; | 77 if (!_onTickController.hasListener) return; |
(...skipping 19 matching lines...) Expand all Loading... |
97 : _time = _clock.time + duration.inMilliseconds { | 97 : _time = _clock.time + duration.inMilliseconds { |
98 _subscription = _clock.onTick.listen((time) { | 98 _subscription = _clock.onTick.listen((time) { |
99 if (time < _time) return; | 99 if (time < _time) return; |
100 _subscription.cancel(); | 100 _subscription.cancel(); |
101 _callback(); | 101 _callback(); |
102 }); | 102 }); |
103 } | 103 } |
104 | 104 |
105 void cancel() => _subscription.cancel(); | 105 void cancel() => _subscription.cancel(); |
106 } | 106 } |
OLD | NEW |