change_notifier_test.dart 11.7 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';

class TestNotifier extends ChangeNotifier {
  void notify() {
    notifyListeners();
  }
12 13

  bool get isListenedTo => hasListeners;
14 15
}

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
class HasListenersTester<T> extends ValueNotifier<T> {
  HasListenersTester(T value) : super(value);
  bool get testHasListeners => hasListeners;
}

class A {
  bool result = false;
  void test() { result = true; }
}

class B extends A with ChangeNotifier {
  @override
  void test() {
    notifyListeners();
    super.test();
  }
}

34 35 36 37 38 39
void main() {
  testWidgets('ChangeNotifier', (WidgetTester tester) async {
    final List<String> log = <String>[];
    final VoidCallback listener = () { log.add('listener'); };
    final VoidCallback listener1 = () { log.add('listener1'); };
    final VoidCallback listener2 = () { log.add('listener2'); };
40 41
    final VoidCallback badListener = () {
      log.add('badListener');
42
      throw ArgumentError();
43
    };
44

45
    final TestNotifier test = TestNotifier();
46 47 48 49

    test.addListener(listener);
    test.addListener(listener);
    test.notify();
50
    expect(log, <String>['listener', 'listener']);
51 52 53 54
    log.clear();

    test.removeListener(listener);
    test.notify();
55
    expect(log, <String>['listener']);
56 57 58 59
    log.clear();

    test.removeListener(listener);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
60
    expect(log, <String>[]);
61 62 63 64
    log.clear();

    test.removeListener(listener);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
65
    expect(log, <String>[]);
66 67 68 69
    log.clear();

    test.addListener(listener);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
70
    expect(log, <String>['listener']);
71 72 73 74
    log.clear();

    test.addListener(listener1);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
75
    expect(log, <String>['listener', 'listener1']);
76 77 78 79
    log.clear();

    test.addListener(listener2);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
80
    expect(log, <String>['listener', 'listener1', 'listener2']);
81 82 83 84
    log.clear();

    test.removeListener(listener1);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
85
    expect(log, <String>['listener', 'listener2']);
86 87 88 89
    log.clear();

    test.addListener(listener1);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
90
    expect(log, <String>['listener', 'listener2', 'listener1']);
91 92 93 94
    log.clear();

    test.addListener(badListener);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
95
    expect(log, <String>['listener', 'listener2', 'listener1', 'badListener']);
96
    expect(tester.takeException(), isArgumentError);
97 98 99 100 101 102 103 104
    log.clear();

    test.addListener(listener1);
    test.removeListener(listener);
    test.removeListener(listener1);
    test.removeListener(listener2);
    test.addListener(listener2);
    test.notify();
105
    expect(log, <String>['badListener', 'listener1', 'listener2']);
106
    expect(tester.takeException(), isArgumentError);
107
    log.clear();
Kate Lovett's avatar
Kate Lovett committed
108
  });
109

110
  test('ChangeNotifier with mutating listener', () {
111
    final TestNotifier test = TestNotifier();
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    final List<String> log = <String>[];

    final VoidCallback listener1 = () { log.add('listener1'); };
    final VoidCallback listener3 = () { log.add('listener3'); };
    final VoidCallback listener4 = () { log.add('listener4'); };
    final VoidCallback listener2 = () {
      log.add('listener2');
      test.removeListener(listener1);
      test.removeListener(listener3);
      test.addListener(listener4);
    };

    test.addListener(listener1);
    test.addListener(listener2);
    test.addListener(listener3);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
128
    expect(log, <String>['listener1', 'listener2']);
129 130 131
    log.clear();

    test.notify();
Ian Hickson's avatar
Ian Hickson committed
132
    expect(log, <String>['listener2', 'listener4']);
133 134 135
    log.clear();

    test.notify();
136
    expect(log, <String>['listener2', 'listener4', 'listener4']);
Ian Hickson's avatar
Ian Hickson committed
137 138 139
    log.clear();
  });

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  test('During notifyListeners, a listener was added and removed immediately', () {
    final TestNotifier source = TestNotifier();
    final List<String> log = <String>[];

    final VoidCallback listener3 = () { log.add('listener3'); };
    final VoidCallback listener2 = () { log.add('listener2'); };
    void listener1() {
      log.add('listener1');
      source.addListener(listener2);
      source.removeListener(listener2);
      source.addListener(listener3);
    }

    source.addListener(listener1);

    source.notify();

    expect(log, <String>['listener1']);
  });

  test(
      'If a listener in the middle of the list of listeners removes itself, '
      'notifyListeners still notifies all listeners', () {
    final TestNotifier source = TestNotifier();
    final List<String> log = <String>[];

    void selfRemovingListener() {
      log.add('selfRemovingListener');
      source.removeListener(selfRemovingListener);
    }
    final VoidCallback listener1 = () { log.add('listener1'); };

    source.addListener(listener1);
    source.addListener(selfRemovingListener);
    source.addListener(listener1);

    source.notify();

    expect(log, <String>['listener1', 'selfRemovingListener', 'listener1']);
  });

  test('If the first listener removes itself, notifyListeners still notify all listeners', () {
    final TestNotifier source = TestNotifier();
    final List<String> log = <String>[];

    void selfRemovingListener() {
      log.add('selfRemovingListener');
      source.removeListener(selfRemovingListener);
    }
    void listener1() {
      log.add('listener1');
    }

    source.addListener(selfRemovingListener);
    source.addListener(listener1);

    source.notifyListeners();

    expect(log, <String>['selfRemovingListener', 'listener1']);
  });

201
  test('Merging change notifiers', () {
202 203 204
    final TestNotifier source1 = TestNotifier();
    final TestNotifier source2 = TestNotifier();
    final TestNotifier source3 = TestNotifier();
Ian Hickson's avatar
Ian Hickson committed
205 206
    final List<String> log = <String>[];

207
    final Listenable merged = Listenable.merge(<Listenable>[source1, source2]);
208 209
    final VoidCallback listener1 = () { log.add('listener1'); };
    final VoidCallback listener2 = () { log.add('listener2'); };
Ian Hickson's avatar
Ian Hickson committed
210

211
    merged.addListener(listener1);
Ian Hickson's avatar
Ian Hickson committed
212 213 214
    source1.notify();
    source2.notify();
    source3.notify();
215
    expect(log, <String>['listener1', 'listener1']);
Ian Hickson's avatar
Ian Hickson committed
216 217
    log.clear();

218
    merged.removeListener(listener1);
Ian Hickson's avatar
Ian Hickson committed
219 220 221 222
    source1.notify();
    source2.notify();
    source3.notify();
    expect(log, isEmpty);
223
    log.clear();
224 225 226 227 228 229 230 231 232 233

    merged.addListener(listener1);
    merged.addListener(listener2);
    source1.notify();
    source2.notify();
    source3.notify();
    expect(log, <String>['listener1', 'listener2', 'listener1', 'listener2']);
    log.clear();
  });

234
  test('Merging change notifiers ignores null', () {
235 236
    final TestNotifier source1 = TestNotifier();
    final TestNotifier source2 = TestNotifier();
237 238
    final List<String> log = <String>[];

239
    final Listenable merged = Listenable.merge(<Listenable?>[null, source1, null, source2, null]);
240 241 242 243 244 245 246
    final VoidCallback listener = () { log.add('listener'); };

    merged.addListener(listener);
    source1.notify();
    source2.notify();
    expect(log, <String>['listener', 'listener']);
    log.clear();
247
  });
248

249
  test('Can remove from merged notifier', () {
250 251
    final TestNotifier source1 = TestNotifier();
    final TestNotifier source2 = TestNotifier();
252 253
    final List<String> log = <String>[];

254
    final Listenable merged = Listenable.merge(<Listenable>[source1, source2]);
255 256 257 258 259 260 261 262
    final VoidCallback listener = () { log.add('listener'); };

    merged.addListener(listener);
    source1.notify();
    source2.notify();
    expect(log, <String>['listener', 'listener']);
    log.clear();

263
    merged.removeListener(listener);
264 265 266 267
    source1.notify();
    source2.notify();
    expect(log, isEmpty);
  });
268 269

  test('Cannot use a disposed ChangeNotifier', () {
270
    final TestNotifier source = TestNotifier();
271
    source.dispose();
272 273
    expect(() { source.addListener(() { }); }, throwsFlutterError);
    expect(() { source.removeListener(() { }); }, throwsFlutterError);
274 275 276
    expect(() { source.dispose(); }, throwsFlutterError);
    expect(() { source.notify(); }, throwsFlutterError);
  });
Adam Barth's avatar
Adam Barth committed
277 278

  test('Value notifier', () {
279
    final ValueNotifier<double> notifier = ValueNotifier<double>(2.0);
Adam Barth's avatar
Adam Barth committed
280 281 282 283 284 285 286 287 288 289 290 291 292

    final List<double> log = <double>[];
    final VoidCallback listener = () { log.add(notifier.value); };

    notifier.addListener(listener);
    notifier.value = 3.0;

    expect(log, equals(<double>[ 3.0 ]));
    log.clear();

    notifier.value = 3.0;
    expect(log, isEmpty);
  });
293 294

  test('Listenable.merge toString', () {
295 296
    final TestNotifier source1 = TestNotifier();
    final TestNotifier source2 = TestNotifier();
297

298
    Listenable listenableUnderTest = Listenable.merge(<Listenable>[]);
299 300
    expect(listenableUnderTest.toString(), 'Listenable.merge([])');

301
    listenableUnderTest = Listenable.merge(<Listenable?>[null]);
302 303
    expect(listenableUnderTest.toString(), 'Listenable.merge([null])');

304
    listenableUnderTest = Listenable.merge(<Listenable>[source1]);
305
    expect(
306
      listenableUnderTest.toString(),
307 308 309
      "Listenable.merge([Instance of 'TestNotifier'])",
    );

310
    listenableUnderTest = Listenable.merge(<Listenable>[source1, source2]);
311
    expect(
312
      listenableUnderTest.toString(),
313 314 315
      "Listenable.merge([Instance of 'TestNotifier', Instance of 'TestNotifier'])",
    );

316
    listenableUnderTest = Listenable.merge(<Listenable?>[null, source2]);
317
    expect(
318
      listenableUnderTest.toString(),
319 320 321
      "Listenable.merge([null, Instance of 'TestNotifier'])",
    );
  });
322

323 324 325 326 327
  test('Listenable.merge does not leak', () {
    // Regression test for https://github.com/flutter/flutter/issues/25163.

    final TestNotifier source1 = TestNotifier();
    final TestNotifier source2 = TestNotifier();
328
    final VoidCallback fakeListener = () { };
329 330 331 332 333 334 335 336 337 338 339 340 341 342

    final Listenable listenableUnderTest = Listenable.merge(<Listenable>[source1, source2]);
    expect(source1.isListenedTo, isFalse);
    expect(source2.isListenedTo, isFalse);
    listenableUnderTest.addListener(fakeListener);
    expect(source1.isListenedTo, isTrue);
    expect(source2.isListenedTo, isTrue);

    listenableUnderTest.removeListener(fakeListener);
    expect(source1.isListenedTo, isFalse);
    expect(source2.isListenedTo, isFalse);
  });


343
  test('hasListeners', () {
344
    final HasListenersTester<bool> notifier = HasListenersTester<bool>(true);
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    expect(notifier.testHasListeners, isFalse);
    void test1() { }
    void test2() { }
    notifier.addListener(test1);
    expect(notifier.testHasListeners, isTrue);
    notifier.addListener(test1);
    expect(notifier.testHasListeners, isTrue);
    notifier.removeListener(test1);
    expect(notifier.testHasListeners, isTrue);
    notifier.removeListener(test1);
    expect(notifier.testHasListeners, isFalse);
    notifier.addListener(test1);
    expect(notifier.testHasListeners, isTrue);
    notifier.addListener(test2);
    expect(notifier.testHasListeners, isTrue);
    notifier.removeListener(test1);
    expect(notifier.testHasListeners, isTrue);
    notifier.removeListener(test2);
    expect(notifier.testHasListeners, isFalse);
  });

366 367 368 369 370 371 372 373 374 375 376 377 378
  test('ChangeNotifier as a mixin', () {
    // We document that this is a valid way to use this class.
    final B b = B();
    int notifications = 0;
    b.addListener(() {
      notifications += 1;
    });
    expect(b.result, isFalse);
    expect(notifications, 0);
    b.test();
    expect(b.result, isTrue);
    expect(notifications, 1);
  });
379 380 381 382

  test('Throws FlutterError when disposed and called', () {
    final TestNotifier testNotifier = TestNotifier();
    testNotifier.dispose();
383
    FlutterError? error;
384 385 386 387 388 389
    try {
      testNotifier.dispose();
    } on FlutterError catch (e) {
      error = e;
    }
    expect(error, isNotNull);
390
    expect(error!, isFlutterError);
391 392 393 394 395 396 397 398
    expect(error.toStringDeep(), equalsIgnoringHashCodes(
      'FlutterError\n'
      '   A TestNotifier was used after being disposed.\n'
      '   Once you have called dispose() on a TestNotifier, it can no\n'
      '   longer be used.\n'
    ));
  });

399
}