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 26 matching lines...) Expand all Loading... |
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; |
46 | 46 |
47 /// Controller providing streams for listening. | 47 /// The stream of millisecond ticks of the clock. |
48 StreamController<int> _multiplexController = | 48 Stream<int> get onTick { |
49 new StreamController<int>.multiplex(); | 49 if (_onTickControllerStream == null) { |
| 50 _onTickControllerStream = _onTickController.stream.asBroadcastStream(); |
| 51 } |
| 52 return _onTickControllerStream; |
| 53 } |
| 54 |
| 55 final _onTickController = new StreamController<int>(); |
| 56 Stream<int> _onTickControllerStream; |
50 | 57 |
51 Clock._(); | 58 Clock._(); |
52 | 59 |
53 /// The stream of millisecond ticks of the clock. | |
54 Stream<int> get onTick => _multiplexController.stream; | |
55 | |
56 /// Advances the clock forward by [milliseconds]. This works like synchronous | 60 /// Advances the clock forward by [milliseconds]. This works like synchronous |
57 /// 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 |
58 /// 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 |
59 /// to the event loop. | 63 /// to the event loop. |
60 void tick([int milliseconds = 1]) { | 64 void tick([int milliseconds=1]) { |
61 for (var i = 0; i < milliseconds; i++) { | 65 for (var i = 0; i < milliseconds; i++) { |
62 var tickTime = ++_time; | 66 var tickTime = ++_time; |
63 runAsync(() { | 67 new Future.value().then((_) => _onTickController.add(tickTime)); |
64 _multiplexController.add(tickTime); | |
65 }); | |
66 } | 68 } |
67 } | 69 } |
68 | 70 |
69 /// Automatically progresses forward in time as long as there are still | 71 /// Automatically progresses forward in time as long as there are still |
70 /// 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 |
71 /// 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 |
72 /// code runs before the next tick. | 74 /// code runs before the next tick. |
73 void run() { | 75 void run() { |
74 pumpEventQueue().then((_) { | 76 pumpEventQueue().then((_) { |
75 if (!_multiplexController.hasListener) return; | 77 if (!_onTickController.hasListener) return; |
76 tick(); | 78 tick(); |
77 return run(); | 79 return run(); |
78 }); | 80 }); |
79 } | 81 } |
80 } | 82 } |
81 | 83 |
82 /// A mock implementation of [Timer] that uses [Clock] to keep time, rather than | 84 /// A mock implementation of [Timer] that uses [Clock] to keep time, rather than |
83 /// the system clock. | 85 /// the system clock. |
84 class _MockTimer implements Timer { | 86 class _MockTimer implements Timer { |
85 /// The time at which the timer should fire. | 87 /// The time at which the timer should fire. |
86 final int _time; | 88 final int _time; |
87 | 89 |
88 /// The callback to run when the timer fires. | 90 /// The callback to run when the timer fires. |
89 final TimerCallback _callback; | 91 final TimerCallback _callback; |
90 | 92 |
91 /// The subscription to the [Clock.onTick] stream. | 93 /// The subscription to the [Clock.onTick] stream. |
92 StreamSubscription _subscription; | 94 StreamSubscription _subscription; |
93 | 95 |
94 _MockTimer(Duration duration, this._callback) | 96 _MockTimer(Duration duration, this._callback) |
95 : _time = _clock.time + duration.inMilliseconds { | 97 : _time = _clock.time + duration.inMilliseconds { |
96 _subscription = _clock.onTick.listen((time) { | 98 _subscription = _clock.onTick.listen((time) { |
97 if (time < _time) return; | 99 if (time < _time) return; |
98 _subscription.cancel(); | 100 _subscription.cancel(); |
99 _callback(); | 101 _callback(); |
100 }); | 102 }); |
101 } | 103 } |
102 | 104 |
103 void cancel() => _subscription.cancel(); | 105 void cancel() => _subscription.cancel(); |
104 } | 106 } |
OLD | NEW |