change_notifier_test.dart 5.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 // Copyright 2016 The Chromium Authors. All rights reserved.
// 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();
  }
}

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'); };
    final VoidCallback badListener = () { log.add('badListener'); throw null; };

    final TestNotifier test = new TestNotifier();

    test.addListener(listener);
    test.addListener(listener);
    test.notify();
27
    expect(log, <String>['listener', 'listener']);
28 29 30 31
    log.clear();

    test.removeListener(listener);
    test.notify();
32
    expect(log, <String>['listener']);
33 34 35 36
    log.clear();

    test.removeListener(listener);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
37
    expect(log, <String>[]);
38 39 40 41
    log.clear();

    test.removeListener(listener);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
42
    expect(log, <String>[]);
43 44 45 46
    log.clear();

    test.addListener(listener);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
47
    expect(log, <String>['listener']);
48 49 50 51
    log.clear();

    test.addListener(listener1);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
52
    expect(log, <String>['listener', 'listener1']);
53 54 55 56
    log.clear();

    test.addListener(listener2);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
57
    expect(log, <String>['listener', 'listener1', 'listener2']);
58 59 60 61
    log.clear();

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

    test.addListener(listener1);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
67
    expect(log, <String>['listener', 'listener2', 'listener1']);
68 69 70 71
    log.clear();

    test.addListener(badListener);
    test.notify();
Ian Hickson's avatar
Ian Hickson committed
72
    expect(log, <String>['listener', 'listener2', 'listener1', 'badListener']);
73 74 75 76 77 78 79 80 81
    expect(tester.takeException(), isNullThrownError);
    log.clear();

    test.addListener(listener1);
    test.removeListener(listener);
    test.removeListener(listener1);
    test.removeListener(listener2);
    test.addListener(listener2);
    test.notify();
82
    expect(log, <String>['badListener', 'listener1', 'listener2']);
83 84 85 86
    expect(tester.takeException(), isNullThrownError);
    log.clear();
  });

87
  test('ChangeNotifier with mutating listener', () {
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    final TestNotifier test = new TestNotifier();
    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
105
    expect(log, <String>['listener1', 'listener2']);
106 107 108
    log.clear();

    test.notify();
Ian Hickson's avatar
Ian Hickson committed
109
    expect(log, <String>['listener2', 'listener4']);
110 111 112
    log.clear();

    test.notify();
113
    expect(log, <String>['listener2', 'listener4', 'listener4']);
Ian Hickson's avatar
Ian Hickson committed
114 115 116
    log.clear();
  });

117
  test('Merging change notifiers', () {
Ian Hickson's avatar
Ian Hickson committed
118 119 120 121 122 123
    final TestNotifier source1 = new TestNotifier();
    final TestNotifier source2 = new TestNotifier();
    final TestNotifier source3 = new TestNotifier();
    final List<String> log = <String>[];

    final Listenable merged = new Listenable.merge(<Listenable>[source1, source2]);
124 125
    final VoidCallback listener1 = () { log.add('listener1'); };
    final VoidCallback listener2 = () { log.add('listener2'); };
Ian Hickson's avatar
Ian Hickson committed
126

127
    merged.addListener(listener1);
Ian Hickson's avatar
Ian Hickson committed
128 129 130
    source1.notify();
    source2.notify();
    source3.notify();
131
    expect(log, <String>['listener1', 'listener1']);
Ian Hickson's avatar
Ian Hickson committed
132 133
    log.clear();

134
    merged.removeListener(listener1);
Ian Hickson's avatar
Ian Hickson committed
135 136 137 138
    source1.notify();
    source2.notify();
    source3.notify();
    expect(log, isEmpty);
139
    log.clear();
140 141 142 143 144 145 146 147 148 149

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

150
  test('Merging change notifiers ignores null', () {
151 152 153 154 155 156 157 158 159 160 161 162
    final TestNotifier source1 = new TestNotifier();
    final TestNotifier source2 = new TestNotifier();
    final List<String> log = <String>[];

    final Listenable merged = new Listenable.merge(<Listenable>[null, source1, null, source2, null]);
    final VoidCallback listener = () { log.add('listener'); };

    merged.addListener(listener);
    source1.notify();
    source2.notify();
    expect(log, <String>['listener', 'listener']);
    log.clear();
163
  });
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

  test('Can dispose merged notifier', () {
    final TestNotifier source1 = new TestNotifier();
    final TestNotifier source2 = new TestNotifier();
    final List<String> log = <String>[];

    final ChangeNotifier merged = new Listenable.merge(<Listenable>[source1, source2]);
    final VoidCallback listener = () { log.add('listener'); };

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

    source1.notify();
    source2.notify();
    expect(log, isEmpty);
  });
184 185 186 187 188 189 190 191 192

  test('Cannot use a disposed ChangeNotifier', () {
    final TestNotifier source = new TestNotifier();
    source.dispose();
    expect(() { source.addListener(null); }, throwsFlutterError);
    expect(() { source.removeListener(null); }, throwsFlutterError);
    expect(() { source.dispose(); }, throwsFlutterError);
    expect(() { source.notify(); }, throwsFlutterError);
  });
193
}