tween_test.dart 2.78 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 6
import 'dart:io';

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 = new Tween<double>(begin: 0.30, end: 0.50);
15
    expect(tween, hasOneLineDescription);
16 17
    final Animatable<double> chain = tween.chain(new Tween<double>(begin: 0.50, end: 1.0));
    final AnimationController controller = new AnimationController(
18 19
      vsync: const TestVSync(),
    );
20 21 22 23 24
    expect(chain.evaluate(controller), 0.40);
    expect(chain, hasOneLineDescription);
  });

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

  test('SizeTween', () {
37
    final SizeTween tween = new SizeTween(begin: Size.zero, end: const Size(20.0, 30.0));
38
    expect(tween.lerp(0.5), equals(const Size(10.0, 15.0)));
39 40 41 42
    expect(tween, hasOneLineDescription);
  });

  test('IntTween', () {
43
    final IntTween tween = new IntTween(begin: 5, end: 9);
44 45 46
    expect(tween.lerp(0.5), 7);
    expect(tween.lerp(0.7), 8);
  });
47 48

  test('RectTween', () {
49 50 51
    final Rect a = new Rect.fromLTWH(5.0, 3.0, 7.0, 11.0);
    final Rect b = new Rect.fromLTWH(8.0, 12.0, 14.0, 18.0);
    final RectTween tween = new RectTween(begin: a, end: b);
52 53 54
    expect(tween.lerp(0.5), equals(Rect.lerp(a, b, 0.5)));
    expect(tween, hasOneLineDescription);
  });
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

  test('Matrix4Tween', () {
    final Matrix4 a = new Matrix4.identity();
    final Matrix4 b = a.clone()..translate(6.0, -8.0, 0.0)..scale(0.5, 1.0, 5.0);
    final Matrix4Tween tween = new Matrix4Tween(begin: a, end: b);
    expect(tween.lerp(0.0), equals(a));
    expect(tween.lerp(1.0), equals(b));
    expect(
      tween.lerp(0.5),
      equals(a.clone()..translate(3.0, -4.0, 0.0)..scale(0.75, 1.0, 3.0))
    );
    final Matrix4 c = a.clone()..rotateZ(1.0);
    final Matrix4Tween rotationTween = new Matrix4Tween(begin: a, end: c);
    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)
      ),
      moreOrLessEquals(0.0)
    );
76
  }, skip: Platform.isWindows); // floating point math not quite deterministic on Windows?
77
}