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

5 6
// @dart = 2.8

7 8 9
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/widgets.dart';
10
import 'package:vector_math/vector_math_64.dart';
11 12 13

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

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

36 37 38 39 40 41 42 43 44 45 46 47
  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);
  });

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

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

  test('RectTween', () {
Dan Field's avatar
Dan Field committed
61 62
    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);
63
    final RectTween tween = RectTween(begin: a, end: b);
64 65 66
    expect(tween.lerp(0.5), equals(Rect.lerp(a, b, 0.5)));
    expect(tween, hasOneLineDescription);
  });
67 68

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

  test('ConstantTween', () {
91
    final ConstantTween<double> tween = ConstantTween<double>(100.0);
92 93 94 95 96 97
    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);
  });
98
}