tween_test.dart 3.41 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// 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';
8
import 'package:vector_math/vector_math_64.dart';
9 10 11

void main() {
  test('Can chain tweens', () {
12
    final Tween<double> tween = Tween<double>(begin: 0.30, end: 0.50);
13
    expect(tween, hasOneLineDescription);
14 15
    final Animatable<double> chain = tween.chain(Tween<double>(begin: 0.50, end: 1.0));
    final AnimationController controller = AnimationController(
16 17
      vsync: const TestVSync(),
    );
18 19 20 21
    expect(chain.evaluate(controller), 0.40);
    expect(chain, hasOneLineDescription);
  });

22
  test('Can animate tweens', () {
23 24
    final Tween<double> tween = Tween<double>(begin: 0.30, end: 0.50);
    final AnimationController controller = AnimationController(
25 26
      vsync: const TestVSync(),
    );
27
    final Animation<double> animation = tween.animate(controller);
28 29 30 31 32 33
    controller.value = 0.50;
    expect(animation.value, 0.40);
    expect(animation, hasOneLineDescription);
    expect(animation.toStringDetails(), hasOneLineDescription);
  });

34 35 36 37 38 39 40 41 42 43 44 45
  test('Can drive tweens', () {
    final Tween<double> tween = Tween<double>(begin: 0.30, end: 0.50);
    final AnimationController controller = AnimationController(
      vsync: const TestVSync(),
    );
    final Animation<double> animation = controller.drive(tween);
    controller.value = 0.50;
    expect(animation.value, 0.40);
    expect(animation, hasOneLineDescription);
    expect(animation.toStringDetails(), hasOneLineDescription);
  });

46
  test('SizeTween', () {
47
    final SizeTween tween = SizeTween(begin: Size.zero, end: const Size(20.0, 30.0));
48
    expect(tween.lerp(0.5), equals(const Size(10.0, 15.0)));
49 50 51 52
    expect(tween, hasOneLineDescription);
  });

  test('IntTween', () {
53
    final IntTween tween = IntTween(begin: 5, end: 9);
54 55 56
    expect(tween.lerp(0.5), 7);
    expect(tween.lerp(0.7), 8);
  });
57 58

  test('RectTween', () {
Dan Field's avatar
Dan Field committed
59 60
    const Rect a = Rect.fromLTWH(5.0, 3.0, 7.0, 11.0);
    const Rect b = Rect.fromLTWH(8.0, 12.0, 14.0, 18.0);
61
    final RectTween tween = RectTween(begin: a, end: b);
62 63 64
    expect(tween.lerp(0.5), equals(Rect.lerp(a, b, 0.5)));
    expect(tween, hasOneLineDescription);
  });
65 66

  test('Matrix4Tween', () {
67
    final Matrix4 a = Matrix4.identity();
68
    final Matrix4 b = a.clone()..translate(6.0, -8.0, 0.0)..scale(0.5, 1.0, 5.0);
69
    final Matrix4Tween tween = Matrix4Tween(begin: a, end: b);
70 71 72 73
    expect(tween.lerp(0.0), equals(a));
    expect(tween.lerp(1.0), equals(b));
    expect(
      tween.lerp(0.5),
74
      equals(a.clone()..translate(3.0, -4.0, 0.0)..scale(0.75, 1.0, 3.0)),
75 76
    );
    final Matrix4 c = a.clone()..rotateZ(1.0);
77
    final Matrix4Tween rotationTween = Matrix4Tween(begin: a, end: c);
78 79 80 81 82 83
    expect(rotationTween.lerp(0.0), equals(a));
    expect(rotationTween.lerp(1.0), equals(c));
    expect(
      rotationTween.lerp(0.5).absoluteError(
        a.clone()..rotateZ(0.5)
      ),
84
      moreOrLessEquals(0.0),
85
    );
86
  }, skip: isWindows); // floating point math not quite deterministic on Windows?
87 88

  test('ConstantTween', () {
89
    final ConstantTween<double> tween = ConstantTween<double>(100.0);
90 91 92 93 94 95
    expect(tween.begin, 100.0);
    expect(tween.end, 100.0);
    expect(tween.lerp(0.0), 100.0);
    expect(tween.lerp(0.5), 100.0);
    expect(tween.lerp(1.0), 100.0);
  });
96
}