animation_controller_test.dart 4.01 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 27 28 29 30 31 32 33 34 35 36 37 38
// 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/animation.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
import 'package:test/test.dart';

void main() {
  test("Can set value during status callback", () {
    WidgetFlutterBinding.ensureInitialized();
    AnimationController controller = new AnimationController(
      duration: const Duration(milliseconds: 100)
    );
    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);
    Scheduler.instance.handleBeginFrame(const Duration(seconds: 1));
    expect(didComplete, isFalse);
    expect(didDismiss, isFalse);
    Scheduler.instance.handleBeginFrame(const Duration(seconds: 2));
    expect(didComplete, isTrue);
    expect(didDismiss, isTrue);
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

    controller.stop();
  });

  test("Receives status callbacks for forward and reverse", () {
    WidgetFlutterBinding.ensureInitialized();
    AnimationController controller = new AnimationController(
      duration: const Duration(milliseconds: 100)
    );
    List<double> valueLog = <double>[];
    List<AnimationStatus> log = <AnimationStatus>[];
    controller
      ..addStatusListener((AnimationStatus status) {
        log.add(status);
      })
      ..addListener(() {
        valueLog.add(controller.value);
      });

    expect(log, equals([]));
    expect(valueLog, equals([]));

    controller.forward();

    expect(log, equals([AnimationStatus.forward]));
    expect(valueLog, equals([]));

    controller.reverse();

    expect(log, equals([AnimationStatus.forward, AnimationStatus.dismissed]));
    expect(valueLog, equals([]));

    controller.reverse();

    expect(log, equals([AnimationStatus.forward, AnimationStatus.dismissed]));
    expect(valueLog, equals([]));

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

    expect(log, equals([AnimationStatus.forward]));
    expect(valueLog, equals([]));

    controller.forward();

    expect(log, equals([AnimationStatus.forward]));
    expect(valueLog, equals([]));

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

    Scheduler.instance.handleBeginFrame(const Duration(seconds: 10));
    expect(log, equals([]));
    expect(valueLog, equals([]));
    Scheduler.instance.handleBeginFrame(const Duration(seconds: 20));
    expect(log, equals([]));
    expect(valueLog, equals([]));
    Scheduler.instance.handleBeginFrame(const Duration(seconds: 30));
    expect(log, equals([]));
    expect(valueLog, equals([]));
    Scheduler.instance.handleBeginFrame(const Duration(seconds: 40));
    expect(log, equals([]));
    expect(valueLog, equals([]));

    controller.stop();
104
  });
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

  test("Forward and reverse from values", () {
    WidgetFlutterBinding.ensureInitialized();
    AnimationController controller = new AnimationController(
      duration: const Duration(milliseconds: 100)
    );
    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);
    expect(statusLog, equals([ AnimationStatus.reverse ]));
    expect(valueLog, equals([ 0.2 ]));
    expect(controller.value, equals(0.2));
    statusLog.clear();
    valueLog.clear();

    controller.forward(from: 0.0);
    expect(statusLog, equals([ AnimationStatus.dismissed, AnimationStatus.forward ]));
    expect(valueLog, equals([ 0.0 ]));
    expect(controller.value, equals(0.0));
  });
133
}