Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: third_party/pkg/angular/lib/mock/zone.dart

Issue 176943008: Update the Angular/DI tests to latest from github. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 library angular.mock_zone; 1 library angular.mock_zone;
2 2
3 import 'dart:async' as dart_async; 3 import 'dart:async' as dart_async;
4 4
5 List<Function> _asyncQueue = []; 5 final _asyncQueue = <Function>[];
6 List<_TimerSpec> _timerQueue = []; 6 final _timerQueue = <_TimerSpec>[];
7 List _asyncErrors = []; 7 final _asyncErrors = [];
8 bool _noMoreAsync = false; 8 bool _noMoreAsync = false;
9 9
10 /** 10 /**
11 * Runs any queued up async calls and any async calls queued with 11 * Runs any queued up async calls and any async calls queued with
12 * running microLeap. Example: 12 * running microLeap. Example:
13 * 13 *
14 * it('should run async code', async(() { 14 * it('should run async code', async(() {
15 * var thenRan = false; 15 * var thenRan = false;
16 * new Future.value('s').then((_) { thenRan = true; }); 16 * new Future.value('s').then((_) { thenRan = true; });
17 * expect(thenRan).toBe(false); 17 * expect(thenRan).toBe(false);
18 * microLeap(); 18 * microLeap();
19 * expect(thenRan).toBe(true); 19 * expect(thenRan).toBe(true);
20 * })); 20 * }));
21 * 21 *
22 * it('should run chained thens', async(() { 22 * it('should run chained thens', async(() {
23 * var log = []; 23 * var log = [];
24 * new Future.value('s') 24 * new Future.value('s')
25 * .then((_) { log.add('firstThen'); }) 25 * .then((_) { log.add('firstThen'); })
26 * .then((_) { log.add('2ndThen'); }); 26 * .then((_) { log.add('2ndThen'); });
27 * expect(log.join(' ')).toEqual(''); 27 * expect(log.join(' ')).toEqual('');
28 * microLeap(); 28 * microLeap();
29 * expect(log.join(' ')).toEqual('firstThen 2ndThen'); 29 * expect(log.join(' ')).toEqual('firstThen 2ndThen');
30 * })); 30 * }));
31 * 31 *
32 */ 32 */
33 microLeap() { 33 microLeap() {
34 while (!_asyncQueue.isEmpty) { 34 while (!_asyncQueue.isEmpty) {
35 // copy the queue as it may change. 35 // copy the queue as it may change.
36 var toRun = new List.from(_asyncQueue); 36 var toRun = new List.from(_asyncQueue);
37 _asyncQueue = []; 37 _asyncQueue.clear();
38 // TODO: Support the case where multiple exceptions are thrown. 38 // TODO: Support the case where multiple exceptions are thrown.
39 // e.g. with a throwNextException() method. 39 // e.g. with a throwNextException() method.
40 assert(_asyncErrors.isEmpty); 40 assert(_asyncErrors.isEmpty);
41 toRun.forEach((fn) => fn()); 41 toRun.forEach((fn) => fn());
42 if (!_asyncErrors.isEmpty) { 42 if (_asyncErrors.isNotEmpty) {
43 var e = _asyncErrors.removeAt(0); 43 var e = _asyncErrors.removeAt(0);
44 throw ['Async error', e[0], e[1]]; 44 throw ['Async error', e[0], e[1]];
45 } 45 }
46 } 46 }
47 } 47 }
48 48
49 /** 49 /**
50 * Simulates a clock tick by running any scheduled timers. Can only be used 50 * Simulates a clock tick by running any scheduled timers. Can only be used
51 * in [async] tests.Clock tick will call [microLeap] to process the microtask 51 * in [async] tests.Clock tick will call [microLeap] to process the microtask
52 * queue before each timer callback. 52 * queue before each timer callback.
(...skipping 17 matching lines...) Expand all
70 * new Timer.periodic(new Duration(milliseconds: 10), (_) => timerRan++); 70 * new Timer.periodic(new Duration(milliseconds: 10), (_) => timerRan++);
71 * 71 *
72 * clockTick(milliseconds: 9); 72 * clockTick(milliseconds: 9);
73 * expect(timerRan).toBe(0); 73 * expect(timerRan).toBe(0);
74 * clockTick(milliseconds: 1); 74 * clockTick(milliseconds: 1);
75 * expect(timerRan).toBe(1); 75 * expect(timerRan).toBe(1);
76 * clockTick(milliseconds: 30); 76 * clockTick(milliseconds: 30);
77 * expect(timerRan).toBe(4); 77 * expect(timerRan).toBe(4);
78 * })); 78 * }));
79 */ 79 */
80 clockTick({int days: 0, 80 void clockTick({int days: 0,
81 int hours: 0, 81 int hours: 0,
82 int minutes: 0, 82 int minutes: 0,
83 int seconds: 0, 83 int seconds: 0,
84 int milliseconds: 0, 84 int milliseconds: 0,
85 int microseconds: 0}) { 85 int microseconds: 0}) {
86 var tickDuration = new Duration(days: days, hours: hours, minutes: minutes, 86 var tickDuration = new Duration(days: days, hours: hours, minutes: minutes,
87 seconds: seconds, milliseconds: milliseconds, microseconds: microseconds); 87 seconds: seconds, milliseconds: milliseconds, microseconds: microseconds);
88 88
89 var queue = _timerQueue;
90 var remainingTimers = []; 89 var remainingTimers = [];
91 _timerQueue = []; 90 var queue = new List.from(_timerQueue);
92 queue.forEach((_TimerSpec spec) { 91 _timerQueue.clear();
93 if (!spec.isActive) return; // Skip over inactive timers. 92 queue
94 if (spec.periodic) { 93 .where((_TimerSpec spec) => spec.isActive)
95 // We always add back the periodic timer unless it's cancelled. 94 .forEach((_TimerSpec spec) {
96 remainingTimers.add(spec); 95 if (spec.periodic) {
96 // We always add back the periodic timer unless it's cancelled.
97 remainingTimers.add(spec);
97 98
98 // Ignore ZERO duration ticks for periodic timers. 99 // Ignore ZERO duration ticks for periodic timers.
99 if (tickDuration == Duration.ZERO) return; 100 if (tickDuration == Duration.ZERO) return;
100 101
101 spec.elapsed += tickDuration; 102 spec.elapsed += tickDuration;
102 // Run the timer as many times as the timer priod fits into the tick. 103 // Run the timer as many times as the timer priod fits into the tick.
103 while (spec.elapsed >= spec.duration) { 104 while (spec.elapsed >= spec.duration) {
104 spec.elapsed -= spec.duration; 105 spec.elapsed -= spec.duration;
105 microLeap(); 106 microLeap();
106 spec.fn(spec); 107 spec.fn(spec);
108 }
109 } else {
110 spec.duration -= tickDuration;
111 if (spec.duration <= Duration.ZERO) {
112 microLeap();
113 spec.fn();
114 } else {
115 remainingTimers.add(spec);
116 }
107 } 117 }
108 } else { 118 });
109 spec.duration -= tickDuration;
110 if (spec.duration <= Duration.ZERO) {
111 microLeap();
112 spec.fn();
113 } else {
114 remainingTimers.add(spec);
115 }
116 }
117 });
118 // Remaining timers should come before anything else scheduled after them. 119 // Remaining timers should come before anything else scheduled after them.
119 _timerQueue.insertAll(0, remainingTimers); 120 _timerQueue.insertAll(0, remainingTimers);
120 } 121 }
121 122
122 /** 123 /**
123 * Causes scheduleMicrotask calls to throw exceptions. 124 * Causes scheduleMicrotask calls to throw exceptions.
124 * 125 *
125 * This function is useful while debugging async tests: the exception 126 * This function is useful while debugging async tests: the exception
126 * is thrown from the scheduleMicrotask call-site instead later in the test. 127 * is thrown from the scheduleMicrotask call-site instead later in the test.
127 */ 128 */
128 noMoreAsync() { 129 noMoreAsync() {
129 _noMoreAsync = true; 130 _noMoreAsync = true;
130 } 131 }
131 132
132 /** 133 /**
133 * Captures all scheduleMicrotask calls inside of a function. 134 * Captures all scheduleMicrotask calls inside of a function.
134 * 135 *
135 * Typically used within a test: 136 * Typically used within a test:
136 * 137 *
137 * it('should be async', async(() { 138 * it('should be async', async(() {
138 * ... 139 * ...
139 * })); 140 * }));
140 */ 141 */
141 async(Function fn) => 142 async(Function fn) => () {
142 () {
143 _noMoreAsync = false; 143 _noMoreAsync = false;
144 _asyncErrors = []; 144 _asyncErrors.clear();
145 _timerQueue = []; 145 _timerQueue.clear();
146 var zoneSpec = new dart_async.ZoneSpecification( 146 var zoneSpec = new dart_async.ZoneSpecification(
147 scheduleMicrotask: (_, __, ___, asyncFn) { 147 scheduleMicrotask: (_, __, ___, asyncFn) {
148 if (_noMoreAsync) { 148 if (_noMoreAsync) {
149 throw ['scheduleMicrotask called after noMoreAsync()']; 149 throw ['scheduleMicrotask called after noMoreAsync()'];
150 } else { 150 } else {
151 _asyncQueue.add(asyncFn); 151 _asyncQueue.add(asyncFn);
152 } 152 }
153 }, 153 },
154 createTimer: (_, __, ____, Duration duration, void f()) => 154 createTimer: (_, __, ____, Duration duration, void f()) =>
155 _createTimer(f, duration, false), 155 _createTimer(f, duration, false),
156 createPeriodicTimer: 156 createPeriodicTimer:
157 (_, __, ___, Duration period, void f(dart_async.Timer timer)) => 157 (_, __, ___, Duration period, void f(dart_async.Timer timer)) =>
158 _createTimer(f, period, true), 158 _createTimer(f, period, true),
159 handleUncaughtError: (_, __, ___, e, s) => _asyncErrors.add([e, s]) 159 handleUncaughtError: (_, __, ___, e, s) => _asyncErrors.add([e, s])
160 ); 160 );
161 dart_async.runZoned(() { 161 dart_async.runZoned(() {
162 fn(); 162 fn();
163 microLeap(); 163 microLeap();
164 }, zoneSpecification: zoneSpec); 164 }, zoneSpecification: zoneSpec);
165 165
166 _asyncErrors.forEach((e) { 166 _asyncErrors.forEach((e) {
167 throw "During runZoned: ${e[0]}. Stack:\n${e[1]}"; 167 throw "During runZoned: ${e[0]}. Stack:\n${e[1]}";
168 }); 168 });
169 169
170 if (!_timerQueue.isEmpty && _timerQueue.any((_TimerSpec spec) => spec.isActive )) { 170 var activeTimers = _timerQueue.fold(0, (nb, _TimerSpec spec) {
171 throw ["${_timerQueue.where((_TimerSpec spec) => spec.isActive).length} " 171 return spec.isActive ? nb + 1 : nb;
172 "active timer(s) are still in the queue."]; 172 });
173
174 if (activeTimers > 0) {
175 throw ["$activeTimers active timer(s) are still in the queue."];
173 } 176 }
174 }; 177 };
175 178
176 _createTimer(Function fn, Duration duration, bool periodic) { 179 _createTimer(Function fn, Duration duration, bool periodic) {
177 var timer = new _TimerSpec(fn, duration, periodic); 180 var timer = new _TimerSpec(fn, duration, periodic);
178 _timerQueue.add(timer); 181 _timerQueue.add(timer);
179 return timer; 182 return timer;
180 } 183 }
181 184
182 /** 185 /**
(...skipping 21 matching lines...) Expand all
204 Duration elapsed = Duration.ZERO; 207 Duration elapsed = Duration.ZERO;
205 bool periodic; 208 bool periodic;
206 bool isActive = true; 209 bool isActive = true;
207 210
208 _TimerSpec(this.fn, this.duration, this.periodic); 211 _TimerSpec(this.fn, this.duration, this.periodic);
209 212
210 void cancel() { 213 void cancel() {
211 isActive = false; 214 isActive = false;
212 } 215 }
213 } 216 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698