animation_controller_test.dart 9.43 KB
Newer Older
1 2 3 4
// 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.

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

9 10
import 'animation_tester.dart';

11
void main() {
12
  setUp(() {
13
    WidgetsFlutterBinding.ensureInitialized();
14 15 16 17
    WidgetsBinding.instance.resetEpoch();
  });

  test('Can set value during status callback', () {
18
    AnimationController controller = new AnimationController(
19 20
      duration: const Duration(milliseconds: 100),
      vsync: const TestVSync(),
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    );
    bool didComplete = false;
    bool didDismiss = false;
    controller.addStatusListener((AnimationStatus status) {
      if (status == AnimationStatus.completed) {
        didComplete = true;
        controller.value = 0.0;
        controller.forward();
      } else if (status == AnimationStatus.dismissed) {
        didDismiss = true;
        controller.value = 0.0;
        controller.forward();
      }
    });

    controller.forward();
    expect(didComplete, isFalse);
    expect(didDismiss, isFalse);
39
    WidgetsBinding.instance.handleBeginFrame(const Duration(seconds: 1));
40 41
    expect(didComplete, isFalse);
    expect(didDismiss, isFalse);
42
    WidgetsBinding.instance.handleBeginFrame(const Duration(seconds: 2));
43 44
    expect(didComplete, isTrue);
    expect(didDismiss, isTrue);
45 46 47 48

    controller.stop();
  });

49
  test('Receives status callbacks for forward and reverse', () {
50
    AnimationController controller = new AnimationController(
51 52
      duration: const Duration(milliseconds: 100),
      vsync: const TestVSync(),
53 54 55 56 57 58 59 60 61 62 63
    );
    List<double> valueLog = <double>[];
    List<AnimationStatus> log = <AnimationStatus>[];
    controller
      ..addStatusListener((AnimationStatus status) {
        log.add(status);
      })
      ..addListener(() {
        valueLog.add(controller.value);
      });

64 65
    expect(log, equals(<AnimationStatus>[]));
    expect(valueLog, equals(<AnimationStatus>[]));
66 67 68

    controller.forward();

69 70
    expect(log, equals(<AnimationStatus>[AnimationStatus.forward]));
    expect(valueLog, equals(<AnimationStatus>[]));
71 72 73

    controller.reverse();

74 75
    expect(log, equals(<AnimationStatus>[AnimationStatus.forward, AnimationStatus.dismissed]));
    expect(valueLog, equals(<AnimationStatus>[]));
76 77 78

    controller.reverse();

79 80
    expect(log, equals(<AnimationStatus>[AnimationStatus.forward, AnimationStatus.dismissed]));
    expect(valueLog, equals(<AnimationStatus>[]));
81 82 83 84

    log.clear();
    controller.forward();

85 86
    expect(log, equals(<AnimationStatus>[AnimationStatus.forward]));
    expect(valueLog, equals(<AnimationStatus>[]));
87 88 89

    controller.forward();

90 91
    expect(log, equals(<AnimationStatus>[AnimationStatus.forward]));
    expect(valueLog, equals(<AnimationStatus>[]));
92 93 94 95

    controller.reverse();
    log.clear();

96
    WidgetsBinding.instance.handleBeginFrame(const Duration(seconds: 10));
97 98
    expect(log, equals(<AnimationStatus>[]));
    expect(valueLog, equals(<AnimationStatus>[]));
99
    WidgetsBinding.instance.handleBeginFrame(const Duration(seconds: 20));
100 101
    expect(log, equals(<AnimationStatus>[]));
    expect(valueLog, equals(<AnimationStatus>[]));
102
    WidgetsBinding.instance.handleBeginFrame(const Duration(seconds: 30));
103 104
    expect(log, equals(<AnimationStatus>[]));
    expect(valueLog, equals(<AnimationStatus>[]));
105
    WidgetsBinding.instance.handleBeginFrame(const Duration(seconds: 40));
106 107
    expect(log, equals(<AnimationStatus>[]));
    expect(valueLog, equals(<AnimationStatus>[]));
108 109

    controller.stop();
110
  });
111

112
  test('Forward and reverse from values', () {
113
    AnimationController controller = new AnimationController(
114 115
      duration: const Duration(milliseconds: 100),
      vsync: const TestVSync(),
116 117 118 119 120 121 122 123 124 125 126 127
    );
    List<double> valueLog = <double>[];
    List<AnimationStatus> statusLog = <AnimationStatus>[];
    controller
      ..addStatusListener((AnimationStatus status) {
        statusLog.add(status);
      })
      ..addListener(() {
        valueLog.add(controller.value);
      });

    controller.reverse(from: 0.2);
128 129
    expect(statusLog, equals(<AnimationStatus>[ AnimationStatus.reverse ]));
    expect(valueLog, equals(<double>[ 0.2 ]));
130 131 132 133 134
    expect(controller.value, equals(0.2));
    statusLog.clear();
    valueLog.clear();

    controller.forward(from: 0.0);
135 136
    expect(statusLog, equals(<AnimationStatus>[ AnimationStatus.dismissed, AnimationStatus.forward ]));
    expect(valueLog, equals(<double>[ 0.0 ]));
137 138
    expect(controller.value, equals(0.0));
  });
139

140 141
  test('Forward only from value', () {
    AnimationController controller = new AnimationController(
142 143
      duration: const Duration(milliseconds: 100),
      vsync: const TestVSync(),
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    );
    List<double> valueLog = <double>[];
    List<AnimationStatus> statusLog = <AnimationStatus>[];
    controller
      ..addStatusListener((AnimationStatus status) {
        statusLog.add(status);
      })
      ..addListener(() {
        valueLog.add(controller.value);
      });

    controller.forward(from: 0.2);
    expect(statusLog, equals(<AnimationStatus>[ AnimationStatus.forward ]));
    expect(valueLog, equals(<double>[ 0.2 ]));
    expect(controller.value, equals(0.2));
  });

161 162
  test('Can fling to upper and lower bounds', () {
    AnimationController controller = new AnimationController(
163 164
      duration: const Duration(milliseconds: 100),
      vsync: const TestVSync(),
165 166 167 168 169 170 171 172 173 174 175
    );

    controller.fling();
    WidgetsBinding.instance.handleBeginFrame(const Duration(seconds: 1));
    WidgetsBinding.instance.handleBeginFrame(const Duration(seconds: 2));
    expect(controller.value, 1.0);
    controller.stop();

    AnimationController largeRangeController = new AnimationController(
      duration: const Duration(milliseconds: 100),
      lowerBound: -30.0,
176 177
      upperBound: 45.0,
      vsync: const TestVSync(),
178 179 180 181 182 183 184 185 186 187 188 189
    );

    largeRangeController.fling();
    WidgetsBinding.instance.handleBeginFrame(const Duration(seconds: 3));
    WidgetsBinding.instance.handleBeginFrame(const Duration(seconds: 4));
    expect(largeRangeController.value, 45.0);
    largeRangeController.fling(velocity: -1.0);
    WidgetsBinding.instance.handleBeginFrame(const Duration(seconds: 5));
    WidgetsBinding.instance.handleBeginFrame(const Duration(seconds: 6));
    expect(largeRangeController.value, -30.0);
    largeRangeController.stop();
  });
190 191 192

  test('lastElapsedDuration control test', () {
    AnimationController controller = new AnimationController(
193 194
      duration: const Duration(milliseconds: 100),
      vsync: const TestVSync(),
195 196 197 198 199 200 201 202
    );
    controller.forward();
    WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 20));
    WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 30));
    WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 40));
    expect(controller.lastElapsedDuration, equals(const Duration(milliseconds: 20)));
    controller.stop();
  });
203 204 205

  test('toString control test', () {
    AnimationController controller = new AnimationController(
206 207
      duration: const Duration(milliseconds: 100),
      vsync: const TestVSync(),
208
    );
209
    expect(controller.toString(), hasOneLineDescription);
210
    controller.forward();
211
    WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 10));
212
    WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 20));
213
    expect(controller.toString(), hasOneLineDescription);
214
    WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 30));
215
    expect(controller.toString(), hasOneLineDescription);
216
    controller.reverse();
217 218
    WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 40));
    WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 50));
219
    expect(controller.toString(), hasOneLineDescription);
220 221
    controller.stop();
  });
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

  test('velocity test - linear', () {
    AnimationController controller = new AnimationController(
      duration: const Duration(milliseconds: 1000),
      vsync: const TestVSync(),
    );

    // mid-flight
    controller.forward();
    WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 0));
    WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 500));
    expect(controller.velocity, inInclusiveRange(0.9, 1.1));

    // edges
    controller.forward();
    expect(controller.velocity, inInclusiveRange(0.4, 0.6));
    WidgetsBinding.instance.handleBeginFrame(Duration.ZERO);
    expect(controller.velocity, inInclusiveRange(0.4, 0.6));
    WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 5));
    expect(controller.velocity, inInclusiveRange(0.9, 1.1));

    controller.forward(from: 0.5);
    expect(controller.velocity, inInclusiveRange(0.4, 0.6));
    WidgetsBinding.instance.handleBeginFrame(Duration.ZERO);
    expect(controller.velocity, inInclusiveRange(0.4, 0.6));
    WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 5));
    expect(controller.velocity, inInclusiveRange(0.9, 1.1));

    // stopped
    controller.forward(from: 1.0);
    expect(controller.velocity, 0.0);
    WidgetsBinding.instance.handleBeginFrame(Duration.ZERO);
    expect(controller.velocity, 0.0);
    WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 500));
    expect(controller.velocity, 0.0);

    controller.forward();
    WidgetsBinding.instance.handleBeginFrame(Duration.ZERO);
    WidgetsBinding.instance.handleBeginFrame(const Duration(milliseconds: 1000));
    expect(controller.velocity, 0.0);

    controller.stop();
  });
265
}