animations_test.dart 9.36 KB
Newer Older
1 2 3 4 5 6 7 8
// 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';

9 10 11 12 13
class BogusCurve extends Curve {
  @override
  double transform(double t) => 100.0;
}

14 15 16 17 18 19 20
void main() {
  setUp(() {
    WidgetsFlutterBinding.ensureInitialized();
    WidgetsBinding.instance.resetEpoch();
  });

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

47
  test('ProxyAnimation.toString control test', () {
48
    final ProxyAnimation animation = new ProxyAnimation();
49 50
    expect(animation.value, 0.0);
    expect(animation.status, AnimationStatus.dismissed);
51
    expect(animation, hasOneLineDescription);
52
    animation.parent = kAlwaysDismissedAnimation;
53
    expect(animation, hasOneLineDescription);
54
  });
55 56

  test('ProxyAnimation set parent generates value changed', () {
57
    final AnimationController controller = new AnimationController(
58 59
      vsync: const TestVSync(),
    );
60 61
    controller.value = 0.5;
    bool didReceiveCallback = false;
62
    final ProxyAnimation animation = new ProxyAnimation()
63 64 65 66 67 68 69 70 71 72 73 74 75
      ..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', () {
76
    final AnimationController controller = new AnimationController(
77 78
      vsync: const TestVSync(),
    );
79 80 81 82 83
    controller.value = 0.5;
    bool didReceiveCallback = false;
    void listener() {
      didReceiveCallback = true;
    }
84
    final ReverseAnimation animation = new ReverseAnimation(controller)
85 86 87 88 89 90 91 92 93
      ..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);
94
    expect(animation, hasOneLineDescription);
95 96 97
  });

  test('TrainHoppingAnimation', () {
98
    final AnimationController currentTrain = new AnimationController(
99 100
      vsync: const TestVSync(),
    );
101
    final AnimationController nextTrain = new AnimationController(
102 103
      vsync: const TestVSync(),
    );
104 105 106
    currentTrain.value = 0.5;
    nextTrain.value = 0.75;
    bool didSwitchTrains = false;
107
    final TrainHoppingAnimation animation = new TrainHoppingAnimation(
108 109 110 111 112
      currentTrain, nextTrain, onSwitchedTrain: () {
        didSwitchTrains = true;
      });
    expect(didSwitchTrains, isFalse);
    expect(animation.value, 0.5);
113
    expect(animation, hasOneLineDescription);
114 115 116
    nextTrain.value = 0.25;
    expect(didSwitchTrains, isTrue);
    expect(animation.value, 0.25);
117
    expect(animation, hasOneLineDescription);
118 119
    expect(animation.toString(), contains('no next'));
  });
120 121

  test('AnimationMean control test', () {
122
    final AnimationController left = new AnimationController(
123 124 125
      value: 0.5,
      vsync: const TestVSync(),
    );
126
    final AnimationController right = new AnimationController(
127 128 129
      vsync: const TestVSync(),
    );

130
    final AnimationMean mean = new AnimationMean(left: left, right: right);
131 132 133 134

    expect(mean, hasOneLineDescription);
    expect(mean.value, equals(0.25));

135
    final List<double> log = <double>[];
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    void logValue() {
      log.add(mean.value);
    }

    mean.addListener(logValue);

    right.value = 1.0;

    expect(mean.value, equals(0.75));
    expect(log, equals(<double>[0.75]));
    log.clear();

    mean.removeListener(logValue);

    left.value = 0.0;

    expect(mean.value, equals(0.50));
    expect(log, isEmpty);
  });
155

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  test('AnimationMax control test', () {
    final AnimationController first = new AnimationController(
      value: 0.5,
      vsync: const TestVSync(),
    );
    final AnimationController second = new AnimationController(
      vsync: const TestVSync(),
    );

    final AnimationMax<double> max = new AnimationMax<double>(first, second);

    expect(max, hasOneLineDescription);
    expect(max.value, equals(0.5));

    final List<double> log = <double>[];
    void logValue() {
      log.add(max.value);
    }

    max.addListener(logValue);

    second.value = 1.0;

    expect(max.value, equals(1.0));
    expect(log, equals(<double>[1.0]));
    log.clear();

    max.removeListener(logValue);

    first.value = 0.0;

    expect(max.value, equals(1.0));
    expect(log, isEmpty);
  });

  test('AnimationMin control test', () {
    final AnimationController first = new AnimationController(
      value: 0.5,
      vsync: const TestVSync(),
    );
    final AnimationController second = new AnimationController(
      vsync: const TestVSync(),
    );

    final AnimationMin<double> min = new AnimationMin<double>(first, second);

    expect(min, hasOneLineDescription);
    expect(min.value, equals(0.0));

    final List<double> log = <double>[];
    void logValue() {
      log.add(min.value);
    }

    min.addListener(logValue);

    second.value = 1.0;

    expect(min.value, equals(0.5));
    expect(log, equals(<double>[0.5]));
    log.clear();

    min.removeListener(logValue);

    first.value = 0.25;

    expect(min.value, equals(0.25));
    expect(log, isEmpty);
  });

226
  test('CurvedAnimation with bogus curve', () {
227
    final AnimationController controller = new AnimationController(
228 229
      vsync: const TestVSync(),
    );
230
    final CurvedAnimation curved = new CurvedAnimation(parent: controller, curve: new BogusCurve());
231 232 233

    expect(() { curved.value; }, throwsFlutterError);
  });
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

  test('TweenSequence', () {
    final AnimationController controller = new AnimationController(
      vsync: const TestVSync(),
    );

    final Animation<double> animation = new TweenSequence<double>(
      <TweenSequenceItem<double>>[
        new TweenSequenceItem<double>(
          tween: new Tween<double>(begin: 5.0, end: 10.0),
          weight: 4.0,
        ),
        new TweenSequenceItem<double>(
          tween: new ConstantTween<double>(10.0),
          weight: 2.0,
        ),
        new TweenSequenceItem<double>(
          tween: new Tween<double>(begin: 10.0, end: 5.0),
          weight: 4.0,
        ),
      ],
    ).animate(controller);

    expect(animation.value, 5.0);

    controller.value = 0.2;
    expect(animation.value, 7.5);

    controller.value = 0.4;
    expect(animation.value, 10.0);

    controller.value = 0.6;
    expect(animation.value, 10.0);

    controller.value = 0.8;
    expect(animation.value, 7.5);

    controller.value = 1.0;
    expect(animation.value, 5.0);
  });

  test('TweenSequence with curves', () {
    final AnimationController controller = new AnimationController(
      vsync: const TestVSync(),
    );

    final Animation<double> animation = new TweenSequence<double>(
      <TweenSequenceItem<double>>[
        new TweenSequenceItem<double>(
          tween: new Tween<double>(begin: 5.0, end: 10.0)
            .chain(new CurveTween(curve: const Interval(0.5, 1.0))),
          weight: 4.0,
        ),
        new TweenSequenceItem<double>(
          tween: new ConstantTween<double>(10.0)
            .chain(new CurveTween(curve: Curves.linear)), // linear is a no-op
          weight: 2.0,
        ),
        new TweenSequenceItem<double>(
          tween: new Tween<double>(begin: 10.0, end: 5.0)
            .chain(new CurveTween(curve: const Interval(0.0, 0.5))),
          weight: 4.0,
        ),
      ],
    ).animate(controller);

    expect(animation.value, 5.0);

    controller.value = 0.2;
    expect(animation.value, 5.0);

    controller.value = 0.4;
    expect(animation.value, 10.0);

    controller.value = 0.6;
    expect(animation.value, 10.0);

    controller.value = 0.8;
    expect(animation.value, 5.0);

    controller.value = 1.0;
    expect(animation.value, 5.0);
  });

  test('TweenSequence, one tween', () {
    final AnimationController controller = new AnimationController(
      vsync: const TestVSync(),
    );

    final Animation<double> animation = new TweenSequence<double>(
      <TweenSequenceItem<double>>[
        new TweenSequenceItem<double>(
          tween: new Tween<double>(begin: 5.0, end: 10.0),
          weight: 1.0,
        ),
      ],
    ).animate(controller);

    expect(animation.value, 5.0);

    controller.value = 0.5;
    expect(animation.value, 7.5);

    controller.value = 1.0;
    expect(animation.value, 10.0);
  });

341
}