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