OLD | NEW |
| (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:observe/observe.dart'; | |
7 import 'package:unittest/unittest.dart'; | |
8 import 'observe_test_utils.dart'; | |
9 | |
10 main() => dirtyCheckZone().run(_runTests); | |
11 | |
12 _runTests() { | |
13 var list; | |
14 var obs; | |
15 var o1, o2, o3; | |
16 var sub; | |
17 int changes; | |
18 | |
19 setUp(() { | |
20 list = toObservable([ | |
21 o1 = new TestModel()..a = (new TestModel()..b = 1), | |
22 o2 = new TestModel()..a = (new TestModel()..b = 2), | |
23 o3 = new TestModel()..a = (new TestModel()..b = 3)]); | |
24 obs = new ListPathObserver(list, 'a.b'); | |
25 changes = 0; | |
26 sub = obs.changes.listen((e) { changes++; }); | |
27 }); | |
28 | |
29 tearDown(() { | |
30 sub.cancel(); | |
31 list = obs = o1 = o2 = o3 = null; | |
32 }); | |
33 | |
34 test('list path observer noticed length changes', () { | |
35 expect(o2.a.b, 2); | |
36 expect(list[1].a.b, 2); | |
37 return _nextMicrotask(null).then((_) { | |
38 expect(changes, 0); | |
39 list.removeAt(1); | |
40 }).then(_nextMicrotask).then((_) { | |
41 expect(changes, 1); | |
42 expect(list[1].a.b, 3); | |
43 }); | |
44 }); | |
45 | |
46 test('list path observer delivers deep change', () { | |
47 expect(o2.a.b, 2); | |
48 expect(list[1].a.b, 2); | |
49 int changes = 0; | |
50 obs.changes.listen((e) { changes++; }); | |
51 return _nextMicrotask(null).then((_) { | |
52 expect(changes, 0); | |
53 o2.a.b = 4; | |
54 }).then(_nextMicrotask).then((_) { | |
55 expect(changes, 1); | |
56 expect(list[1].a.b, 4); | |
57 o1.a = new TestModel()..b = 5; | |
58 }).then(_nextMicrotask).then((_) { | |
59 expect(changes, 2); | |
60 expect(list[0].a.b, 5); | |
61 }); | |
62 }); | |
63 } | |
64 | |
65 _nextMicrotask(_) => new Future(() {}); | |
66 | |
67 @reflectable | |
68 class TestModel extends ChangeNotifier { | |
69 var _a, _b; | |
70 TestModel(); | |
71 | |
72 get a => _a; | |
73 void set a(newValue) { _a = notifyPropertyChange(#a, _a, newValue); } | |
74 | |
75 get b => _b; | |
76 void set b(newValue) { _b = notifyPropertyChange(#b, _b, newValue); } | |
77 } | |
OLD | NEW |