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

import 'package:flutter/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart';
7

8 9 10
void main() {
  setUp(() {
    WidgetsFlutterBinding.ensureInitialized();
11
    WidgetsBinding.instance.resetEpoch();
12 13 14
  });

  test('AnimationController with mutating listener', () {
15
    final AnimationController controller = AnimationController(
16 17 18 19 20
      duration: const Duration(milliseconds: 100),
      vsync: const TestVSync(),
    );
    final List<String> log = <String>[];

21 22 23 24
    void listener1() { log.add('listener1'); }
    void listener3() { log.add('listener3'); }
    void listener4() { log.add('listener4'); }
    void listener2() {
25 26 27 28
      log.add('listener2');
      controller.removeListener(listener1);
      controller.removeListener(listener3);
      controller.addListener(listener4);
29
    }
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

    controller.addListener(listener1);
    controller.addListener(listener2);
    controller.addListener(listener3);
    controller.value = 0.2;
    expect(log, <String>['listener1', 'listener2']);
    log.clear();

    controller.value = 0.3;
    expect(log, <String>['listener2', 'listener4']);
    log.clear();

    controller.value = 0.4;
    expect(log, <String>['listener2', 'listener4', 'listener4']);
    log.clear();
  });

  test('AnimationController with mutating status listener', () {
48
    final AnimationController controller = AnimationController(
49 50 51 52 53
      duration: const Duration(milliseconds: 100),
      vsync: const TestVSync(),
    );
    final List<String> log = <String>[];

54 55 56 57
    void listener1(AnimationStatus status) { log.add('listener1'); }
    void listener3(AnimationStatus status) { log.add('listener3'); }
    void listener4(AnimationStatus status) { log.add('listener4'); }
    void listener2(AnimationStatus status) {
58 59 60 61
      log.add('listener2');
      controller.removeStatusListener(listener1);
      controller.removeStatusListener(listener3);
      controller.addStatusListener(listener4);
62
    }
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    controller.addStatusListener(listener1);
    controller.addStatusListener(listener2);
    controller.addStatusListener(listener3);
    controller.forward();
    expect(log, <String>['listener1', 'listener2']);
    log.clear();

    controller.reverse();
    expect(log, <String>['listener2', 'listener4']);
    log.clear();

    controller.forward();
    expect(log, <String>['listener2', 'listener4', 'listener4']);
    log.clear();

    controller.dispose();
  });

82
  testWidgets('AnimationController with throwing listener', (WidgetTester tester) async {
83
    final AnimationController controller = AnimationController(
84 85 86
      duration: const Duration(milliseconds: 100),
      vsync: const TestVSync(),
    );
87
    addTearDown(controller.dispose);
88 89
    final List<String> log = <String>[];

90 91
    void listener1() { log.add('listener1'); }
    void badListener() {
92
      log.add('badListener');
93
      throw ArgumentError();
94 95
    }
    void listener2() { log.add('listener2'); }
96 97 98 99 100 101

    controller.addListener(listener1);
    controller.addListener(badListener);
    controller.addListener(listener2);
    controller.value = 0.2;
    expect(log, <String>['listener1', 'badListener', 'listener2']);
102
    expect(tester.takeException(), isArgumentError);
103 104 105
    log.clear();
  });

106
  testWidgets('AnimationController with throwing status listener', (WidgetTester tester) async {
107
    final AnimationController controller = AnimationController(
108 109 110 111 112
      duration: const Duration(milliseconds: 100),
      vsync: const TestVSync(),
    );
    final List<String> log = <String>[];

113 114
    void listener1(AnimationStatus status) { log.add('listener1'); }
    void badListener(AnimationStatus status) {
115
      log.add('badListener');
116
      throw ArgumentError();
117 118
    }
    void listener2(AnimationStatus status) { log.add('listener2'); }
119 120 121 122 123 124

    controller.addStatusListener(listener1);
    controller.addStatusListener(badListener);
    controller.addStatusListener(listener2);
    controller.forward();
    expect(log, <String>['listener1', 'badListener', 'listener2']);
125
    expect(tester.takeException(), isArgumentError);
126 127 128 129
    log.clear();
    controller.dispose();
  });
}