animations_test.dart 3.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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_test/flutter_test.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/widgets.dart';

void main() {
  setUp(() {
    WidgetsFlutterBinding.ensureInitialized();
    WidgetsBinding.instance.resetEpoch();
  });

  test('toString control test', () {
16 17 18
    expect(kAlwaysCompleteAnimation.toString(), hasOneLineDescription);
    expect(kAlwaysDismissedAnimation.toString(), hasOneLineDescription);
    expect(new AlwaysStoppedAnimation<double>(0.5).toString(), hasOneLineDescription);
19 20 21 22
    CurvedAnimation curvedAnimation = new CurvedAnimation(
      parent: kAlwaysDismissedAnimation,
      curve: Curves.ease
    );
23
    expect(curvedAnimation.toString(), hasOneLineDescription);
24
    curvedAnimation.reverseCurve = Curves.elasticOut;
25
    expect(curvedAnimation.toString(), hasOneLineDescription);
26 27 28 29 30 31 32 33 34 35 36
    AnimationController controller = new AnimationController(
      duration: const Duration(milliseconds: 500)
    );
    controller
      ..value = 0.5
      ..reverse();
    curvedAnimation = new CurvedAnimation(
      parent: controller,
      curve: Curves.ease,
      reverseCurve: Curves.elasticOut
    );
37
    expect(curvedAnimation.toString(), hasOneLineDescription);
38
    controller.stop();
39 40
  });

41
  test('ProxyAnimation.toString control test', () {
42 43 44
    ProxyAnimation animation = new ProxyAnimation();
    expect(animation.value, 0.0);
    expect(animation.status, AnimationStatus.dismissed);
45
    expect(animation.toString(), hasOneLineDescription);
46
    animation.parent = kAlwaysDismissedAnimation;
47
    expect(animation.toString(), hasOneLineDescription);
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

  test('ProxyAnimation set parent generates value changed', () {
    AnimationController controller = new AnimationController();
    controller.value = 0.5;
    bool didReceiveCallback = false;
    ProxyAnimation animation = new ProxyAnimation()
      ..addListener(() {
        didReceiveCallback = true;
      });
    expect(didReceiveCallback, isFalse);
    animation.parent = controller;
    expect(didReceiveCallback, isTrue);
    didReceiveCallback = false;
    expect(didReceiveCallback, isFalse);
    controller.value = 0.6;
    expect(didReceiveCallback, isTrue);
  });

  test('ReverseAnimation calls listeners', () {
    AnimationController controller = new AnimationController();
    controller.value = 0.5;
    bool didReceiveCallback = false;
    void listener() {
      didReceiveCallback = true;
    }
    ReverseAnimation animation = new ReverseAnimation(controller)
      ..addListener(listener);
    expect(didReceiveCallback, isFalse);
    controller.value = 0.6;
    expect(didReceiveCallback, isTrue);
    didReceiveCallback = false;
    animation.removeListener(listener);
    expect(didReceiveCallback, isFalse);
    controller.value = 0.7;
    expect(didReceiveCallback, isFalse);
84
    expect(animation.toString(), hasOneLineDescription);
85 86 87 88 89 90 91 92 93 94 95 96 97 98
  });

  test('TrainHoppingAnimation', () {
    AnimationController currentTrain = new AnimationController();
    AnimationController nextTrain = new AnimationController();
    currentTrain.value = 0.5;
    nextTrain.value = 0.75;
    bool didSwitchTrains = false;
    TrainHoppingAnimation animation = new TrainHoppingAnimation(
      currentTrain, nextTrain, onSwitchedTrain: () {
        didSwitchTrains = true;
      });
    expect(didSwitchTrains, isFalse);
    expect(animation.value, 0.5);
99
    expect(animation.toString(), hasOneLineDescription);
100 101 102
    nextTrain.value = 0.25;
    expect(didSwitchTrains, isTrue);
    expect(animation.value, 0.25);
103
    expect(animation.toString(), hasOneLineDescription);
104 105
    expect(animation.toString(), contains('no next'));
  });
106
}