| 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'package:logging/logging.dart'; | 6 import 'package:logging/logging.dart'; |
| 7 import 'package:observe/observe.dart'; | 7 import 'package:observe/observe.dart'; |
| 8 import 'package:observe/src/dirty_check.dart' as dirty_check; | 8 import 'package:observe/src/dirty_check.dart' as dirty_check; |
| 9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
| 10 import 'observe_test_utils.dart'; | 10 import 'observe_test_utils.dart'; |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 runAsync(expectAsync0(() { | 186 runAsync(expectAsync0(() { |
| 187 subs.add(t.changes.listen(expectAsync1((records) { | 187 subs.add(t.changes.listen(expectAsync1((records) { |
| 188 expectChanges(records, _changedValue(1)); | 188 expectChanges(records, _changedValue(1)); |
| 189 }))); | 189 }))); |
| 190 t.value = 777; | 190 t.value = 777; |
| 191 runAsync(Observable.dirtyCheck); | 191 runAsync(Observable.dirtyCheck); |
| 192 })); | 192 })); |
| 193 })); | 193 })); |
| 194 t.value = 42; | 194 t.value = 42; |
| 195 }); | 195 }); |
| 196 |
| 197 observeTest('cannot modify changes list', () { |
| 198 var t = createModel(123); |
| 199 var records = null; |
| 200 subs.add(t.changes.listen((r) { records = r; })); |
| 201 t.value = 42; |
| 202 |
| 203 performMicrotaskCheckpoint(); |
| 204 expectChanges(records, _changedValue(1)); |
| 205 |
| 206 // Verify that mutation operations on the list fail: |
| 207 |
| 208 expect(() { |
| 209 records[0] = new PropertyChangeRecord(_VALUE); |
| 210 }, throwsUnsupportedError); |
| 211 |
| 212 expect(() { records.clear(); }, throwsUnsupportedError); |
| 213 |
| 214 expect(() { records.length = 0; }, throwsUnsupportedError); |
| 215 }); |
| 216 |
| 217 observeTest('notifyChange', () { |
| 218 var t = createModel(123); |
| 219 var records = []; |
| 220 subs.add(t.changes.listen((r) { records.addAll(r); })); |
| 221 t.notifyChange(new PropertyChangeRecord(_VALUE)); |
| 222 |
| 223 performMicrotaskCheckpoint(); |
| 224 expectChanges(records, _changedValue(1)); |
| 225 expect(t.value, 123, reason: 'value did not actually change.'); |
| 226 }); |
| 227 |
| 228 observeTest('notifyPropertyChange', () { |
| 229 var t = createModel(123); |
| 230 var records = null; |
| 231 subs.add(t.changes.listen((r) { records = r; })); |
| 232 expect(t.notifyPropertyChange(_VALUE, t.value, 42), 42, |
| 233 reason: 'notifyPropertyChange returns newValue'); |
| 234 |
| 235 performMicrotaskCheckpoint(); |
| 236 expectChanges(records, _changedValue(1)); |
| 237 expect(t.value, 123, reason: 'value did not actually change.'); |
| 238 }); |
| 196 } | 239 } |
| 197 | 240 |
| 198 _changedValue(len) => new List.filled(len, new PropertyChangeRecord(_VALUE)); | 241 _changedValue(len) => new List.filled(len, new PropertyChangeRecord(_VALUE)); |
| 199 | 242 |
| 200 // A test model based on dirty checking. | 243 // A test model based on dirty checking. |
| 201 class WatcherModel<T> extends ObservableBase { | 244 class WatcherModel<T> extends ObservableBase { |
| 202 @observable T value; | 245 @observable T value; |
| 203 | 246 |
| 204 WatcherModel([T initialValue]) : value = initialValue; | 247 WatcherModel([T initialValue]) : value = initialValue; |
| 205 | 248 |
| 206 String toString() => '#<$runtimeType value: $value>'; | 249 String toString() => '#<$runtimeType value: $value>'; |
| 207 } | 250 } |
| OLD | NEW |