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

Side by Side Diff: packages/observe/test/observe_test.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 4 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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'dart:async';
6 import 'package:logging/logging.dart';
7 import 'package:observe/observe.dart';
8 import 'package:observe/src/dirty_check.dart' as dirty_check;
9 import 'package:unittest/unittest.dart';
10 import 'observe_test_utils.dart';
11
12 import 'package:observe/mirrors_used.dart'; // make test smaller.
13 import 'package:smoke/mirrors.dart';
14
15 main() {
16 useMirrors();
17 dirtyCheckZone().run(_tests);
18 }
19
20 void _tests() {
21 // Note: to test the basic Observable system, we use ObservableBox due to its
22 // simplicity. We also test a variant that is based on dirty-checking.
23
24 test('no observers at the start', () {
25 expect(dirty_check.allObservablesCount, 0);
26 });
27
28 group('WatcherModel', () => _observeTests((x) => new WatcherModel(x)));
29
30 group('ObservableBox', () => _observeTests((x) => new ObservableBox(x)));
31
32 group('ModelSubclass', () => _observeTests((x) => new ModelSubclass(x)));
33
34 group('dirtyCheck loops can be debugged', () {
35 var messages;
36 var subscription;
37 setUp(() {
38 messages = [];
39 subscription = Logger.root.onRecord.listen((record) {
40 messages.add(record.message);
41 });
42 });
43
44 tearDown(() {
45 subscription.cancel();
46 });
47
48 test('logs debug information', () {
49 var maxNumIterations = dirty_check.MAX_DIRTY_CHECK_CYCLES;
50
51 var x = new WatcherModel(0);
52 var sub = x.changes.listen(expectAsync((_) { x.value++; },
53 count: maxNumIterations));
54 x.value = 1;
55 Observable.dirtyCheck();
56 expect(x.value, maxNumIterations + 1);
57 expect(messages.length, 2);
58
59 expect(messages[0], contains('Possible loop'));
60 expect(messages[1], contains('index 0'));
61 expect(messages[1], contains('object: $x'));
62
63 sub.cancel();
64 });
65 });
66 }
67
68 void _observeTests(createModel(x)) {
69 final watch = createModel(null) is! ChangeNotifier;
70
71 // Track the subscriptions so we can clean them up in tearDown.
72 List subs;
73
74 int initialObservers;
75 setUp(() {
76 initialObservers = dirty_check.allObservablesCount;
77 subs = [];
78
79 if (watch) scheduleMicrotask(Observable.dirtyCheck);
80 });
81
82 tearDown(() {
83 for (var sub in subs) sub.cancel();
84 return new Future(() {
85 expect(dirty_check.allObservablesCount, initialObservers,
86 reason: 'Observable object leaked');
87 });
88 });
89
90 test('handle future result', () {
91 var callback = expectAsync((){});
92 return new Future(callback);
93 });
94
95 test('no observers', () {
96 var t = createModel(123);
97 expect(t.value, 123);
98 t.value = 42;
99 expect(t.value, 42);
100 expect(t.hasObservers, false);
101 });
102
103 test('listen adds an observer', () {
104 var t = createModel(123);
105 expect(t.hasObservers, false);
106
107 subs.add(t.changes.listen((n) {}));
108 expect(t.hasObservers, true);
109 });
110
111 test('changes delived async', () {
112 var t = createModel(123);
113 int called = 0;
114
115 subs.add(t.changes.listen(expectAsync((records) {
116 called++;
117 expectPropertyChanges(records, watch ? 1 : 2);
118 })));
119
120 t.value = 41;
121 t.value = 42;
122 expect(called, 0);
123 });
124
125 test('cause changes in handler', () {
126 var t = createModel(123);
127 int called = 0;
128
129 subs.add(t.changes.listen(expectAsync((records) {
130 called++;
131 expectPropertyChanges(records, 1);
132 if (called == 1) {
133 // Cause another change
134 t.value = 777;
135 }
136 }, count: 2)));
137
138 t.value = 42;
139 });
140
141 test('multiple observers', () {
142 var t = createModel(123);
143
144 verifyRecords(records) {
145 expectPropertyChanges(records, watch ? 1 : 2);
146 };
147
148 subs.add(t.changes.listen(expectAsync(verifyRecords)));
149 subs.add(t.changes.listen(expectAsync(verifyRecords)));
150
151 t.value = 41;
152 t.value = 42;
153 });
154
155 test('async processing model', () {
156 var t = createModel(123);
157 var records = [];
158 subs.add(t.changes.listen((r) { records.addAll(r); }));
159 t.value = 41;
160 t.value = 42;
161 expectChanges(records, [], reason: 'changes delived async');
162
163 return new Future(() {
164 expectPropertyChanges(records, watch ? 1 : 2);
165 records.clear();
166
167 t.value = 777;
168 expectChanges(records, [], reason: 'changes delived async');
169
170 }).then(newMicrotask).then((_) {
171 expectPropertyChanges(records, 1);
172
173 // Has no effect if there are no changes
174 Observable.dirtyCheck();
175 expectPropertyChanges(records, 1);
176 });
177 });
178
179 test('cancel listening', () {
180 var t = createModel(123);
181 var sub;
182 sub = t.changes.listen(expectAsync((records) {
183 expectPropertyChanges(records, 1);
184 sub.cancel();
185 t.value = 777;
186 scheduleMicrotask(Observable.dirtyCheck);
187 }));
188 t.value = 42;
189 });
190
191 test('cancel and reobserve', () {
192 var t = createModel(123);
193 var sub;
194 sub = t.changes.listen(expectAsync((records) {
195 expectPropertyChanges(records, 1);
196 sub.cancel();
197
198 scheduleMicrotask(expectAsync(() {
199 subs.add(t.changes.listen(expectAsync((records) {
200 expectPropertyChanges(records, 1);
201 })));
202 t.value = 777;
203 scheduleMicrotask(Observable.dirtyCheck);
204 }));
205 }));
206 t.value = 42;
207 });
208
209 test('cannot modify changes list', () {
210 var t = createModel(123);
211 var records = null;
212 subs.add(t.changes.listen((r) { records = r; }));
213 t.value = 42;
214
215 return new Future(() {
216 expectPropertyChanges(records, 1);
217
218 // Verify that mutation operations on the list fail:
219
220 expect(() {
221 records[0] = new PropertyChangeRecord(t, #value, 0, 1);
222 }, throwsUnsupportedError);
223
224 expect(() { records.clear(); }, throwsUnsupportedError);
225
226 expect(() { records.length = 0; }, throwsUnsupportedError);
227 });
228 });
229
230 test('notifyChange', () {
231 var t = createModel(123);
232 var records = [];
233 subs.add(t.changes.listen((r) { records.addAll(r); }));
234 t.notifyChange(new PropertyChangeRecord(t, #value, 123, 42));
235
236 return new Future(() {
237 expectPropertyChanges(records, 1);
238 expect(t.value, 123, reason: 'value did not actually change.');
239 });
240 });
241
242 test('notifyPropertyChange', () {
243 var t = createModel(123);
244 var records = null;
245 subs.add(t.changes.listen((r) { records = r; }));
246 expect(t.notifyPropertyChange(#value, t.value, 42), 42,
247 reason: 'notifyPropertyChange returns newValue');
248
249 return new Future(() {
250 expectPropertyChanges(records, 1);
251 expect(t.value, 123, reason: 'value did not actually change.');
252 });
253 });
254 }
255
256 expectPropertyChanges(records, int number) {
257 expect(records.length, number, reason: 'expected $number change records');
258 for (var record in records) {
259 expect(record is PropertyChangeRecord, true, reason:
260 'record should be PropertyChangeRecord');
261 expect((record as PropertyChangeRecord).name, #value, reason:
262 'record should indicate a change to the "value" property');
263 }
264 }
265
266 // A test model based on dirty checking.
267 class WatcherModel<T> extends Observable {
268 @observable T value;
269
270 WatcherModel([T initialValue]) : value = initialValue;
271
272 String toString() => '#<$runtimeType value: $value>';
273 }
274
275 class ModelSubclass<T> extends WatcherModel<T> {
276 ModelSubclass([T initialValue]) : super(initialValue);
277 }
OLDNEW
« no previous file with comments | « packages/observe/test/observable_map_test.dart ('k') | packages/observe/test/observe_test_utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698