animations_test.dart 13.9 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 7
import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';
8 9 10 11
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/widgets.dart';

12 13
import '../scheduler/scheduler_tester.dart';

14 15 16 17 18
class BogusCurve extends Curve {
  @override
  double transform(double t) => 100.0;
}

19 20 21 22
void main() {
  setUp(() {
    WidgetsFlutterBinding.ensureInitialized();
    WidgetsBinding.instance.resetEpoch();
23 24
    ui.window.onBeginFrame = null;
    ui.window.onDrawFrame = null;
25 26 27
  });

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

54
  test('ProxyAnimation.toString control test', () {
55
    final ProxyAnimation animation = ProxyAnimation();
56 57
    expect(animation.value, 0.0);
    expect(animation.status, AnimationStatus.dismissed);
58
    expect(animation, hasOneLineDescription);
59
    animation.parent = kAlwaysDismissedAnimation;
60
    expect(animation, hasOneLineDescription);
61
  });
62 63

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

  test('TrainHoppingAnimation', () {
105
    final AnimationController currentTrain = AnimationController(
106 107
      vsync: const TestVSync(),
    );
108
    final AnimationController nextTrain = AnimationController(
109 110
      vsync: const TestVSync(),
    );
111 112 113
    currentTrain.value = 0.5;
    nextTrain.value = 0.75;
    bool didSwitchTrains = false;
114
    final TrainHoppingAnimation animation = TrainHoppingAnimation(
115 116 117
      currentTrain,
      nextTrain,
      onSwitchedTrain: () {
118
        didSwitchTrains = true;
119 120
      },
    );
121 122
    expect(didSwitchTrains, isFalse);
    expect(animation.value, 0.5);
123
    expect(animation, hasOneLineDescription);
124 125 126
    nextTrain.value = 0.25;
    expect(didSwitchTrains, isTrue);
    expect(animation.value, 0.25);
127
    expect(animation, hasOneLineDescription);
128 129
    expect(animation.toString(), contains('no next'));
  });
130 131

  test('AnimationMean control test', () {
132
    final AnimationController left = AnimationController(
133 134 135
      value: 0.5,
      vsync: const TestVSync(),
    );
136
    final AnimationController right = AnimationController(
137 138 139
      vsync: const TestVSync(),
    );

140
    final AnimationMean mean = AnimationMean(left: left, right: right);
141 142 143 144

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

145
    final List<double> log = <double>[];
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    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);
  });
165

166
  test('AnimationMax control test', () {
167
    final AnimationController first = AnimationController(
168 169 170
      value: 0.5,
      vsync: const TestVSync(),
    );
171
    final AnimationController second = AnimationController(
172 173 174
      vsync: const TestVSync(),
    );

175
    final AnimationMax<double> max = AnimationMax<double>(first, second);
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

    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', () {
202
    final AnimationController first = AnimationController(
203 204 205
      value: 0.5,
      vsync: const TestVSync(),
    );
206
    final AnimationController second = AnimationController(
207 208 209
      vsync: const TestVSync(),
    );

210
    final AnimationMin<double> min = AnimationMin<double>(first, second);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

    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);
  });

236
  test('CurvedAnimation with bogus curve', () {
237
    final AnimationController controller = AnimationController(
238 239
      vsync: const TestVSync(),
    );
240
    final CurvedAnimation curved = CurvedAnimation(parent: controller, curve: BogusCurve());
241 242 243

    expect(() { curved.value; }, throwsFlutterError);
  });
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('CurvedAnimation running with different forward and reverse durations.', () {
    final AnimationController controller = AnimationController(
      duration: const Duration(milliseconds: 100),
      reverseDuration: const Duration(milliseconds: 50),
      vsync: const TestVSync(),
    );
    final CurvedAnimation curved = CurvedAnimation(parent: controller, curve: Curves.linear, reverseCurve: Curves.linear);

    controller.forward();
    tick(const Duration(milliseconds: 0));
    tick(const Duration(milliseconds: 10));
    expect(curved.value, closeTo(0.1, precisionErrorTolerance));
    tick(const Duration(milliseconds: 20));
    expect(curved.value, closeTo(0.2, precisionErrorTolerance));
    tick(const Duration(milliseconds: 30));
    expect(curved.value, closeTo(0.3, precisionErrorTolerance));
    tick(const Duration(milliseconds: 40));
    expect(curved.value, closeTo(0.4, precisionErrorTolerance));
    tick(const Duration(milliseconds: 50));
    expect(curved.value, closeTo(0.5, precisionErrorTolerance));
    tick(const Duration(milliseconds: 60));
    expect(curved.value, closeTo(0.6, precisionErrorTolerance));
    tick(const Duration(milliseconds: 70));
    expect(curved.value, closeTo(0.7, precisionErrorTolerance));
    tick(const Duration(milliseconds: 80));
    expect(curved.value, closeTo(0.8, precisionErrorTolerance));
    tick(const Duration(milliseconds: 90));
    expect(curved.value, closeTo(0.9, precisionErrorTolerance));
    tick(const Duration(milliseconds: 100));
    expect(curved.value, closeTo(1.0, precisionErrorTolerance));
    controller.reverse();
    tick(const Duration(milliseconds: 110));
    expect(curved.value, closeTo(1.0, precisionErrorTolerance));
    tick(const Duration(milliseconds: 120));
    expect(curved.value, closeTo(0.8, precisionErrorTolerance));
    tick(const Duration(milliseconds: 130));
    expect(curved.value, closeTo(0.6, precisionErrorTolerance));
    tick(const Duration(milliseconds: 140));
    expect(curved.value, closeTo(0.4, precisionErrorTolerance));
    tick(const Duration(milliseconds: 150));
    expect(curved.value, closeTo(0.2, precisionErrorTolerance));
    tick(const Duration(milliseconds: 160));
    expect(curved.value, closeTo(0.0, precisionErrorTolerance));
  });

  test('ReverseAnimation running with different forward and reverse durations.', () {
    final AnimationController controller = AnimationController(
      duration: const Duration(milliseconds: 100),
      reverseDuration: const Duration(milliseconds: 50),
      vsync: const TestVSync(),
    );
    final ReverseAnimation reversed = ReverseAnimation(
      CurvedAnimation(
        parent: controller,
        curve: Curves.linear,
        reverseCurve: Curves.linear,
      ),
    );

    controller.forward();
    tick(const Duration(milliseconds: 0));
    tick(const Duration(milliseconds: 10));
    expect(reversed.value, closeTo(0.9, precisionErrorTolerance));
    tick(const Duration(milliseconds: 20));
    expect(reversed.value, closeTo(0.8, precisionErrorTolerance));
    tick(const Duration(milliseconds: 30));
    expect(reversed.value, closeTo(0.7, precisionErrorTolerance));
    tick(const Duration(milliseconds: 40));
    expect(reversed.value, closeTo(0.6, precisionErrorTolerance));
    tick(const Duration(milliseconds: 50));
    expect(reversed.value, closeTo(0.5, precisionErrorTolerance));
    tick(const Duration(milliseconds: 60));
    expect(reversed.value, closeTo(0.4, precisionErrorTolerance));
    tick(const Duration(milliseconds: 70));
    expect(reversed.value, closeTo(0.3, precisionErrorTolerance));
    tick(const Duration(milliseconds: 80));
    expect(reversed.value, closeTo(0.2, precisionErrorTolerance));
    tick(const Duration(milliseconds: 90));
    expect(reversed.value, closeTo(0.1, precisionErrorTolerance));
    tick(const Duration(milliseconds: 100));
    expect(reversed.value, closeTo(0.0, precisionErrorTolerance));
    controller.reverse();
    tick(const Duration(milliseconds: 110));
    expect(reversed.value, closeTo(0.0, precisionErrorTolerance));
    tick(const Duration(milliseconds: 120));
    expect(reversed.value, closeTo(0.2, precisionErrorTolerance));
    tick(const Duration(milliseconds: 130));
    expect(reversed.value, closeTo(0.4, precisionErrorTolerance));
    tick(const Duration(milliseconds: 140));
    expect(reversed.value, closeTo(0.6, precisionErrorTolerance));
    tick(const Duration(milliseconds: 150));
    expect(reversed.value, closeTo(0.8, precisionErrorTolerance));
    tick(const Duration(milliseconds: 160));
    expect(reversed.value, closeTo(1.0, precisionErrorTolerance));
  });

341
  test('TweenSequence', () {
342
    final AnimationController controller = AnimationController(
343 344 345
      vsync: const TestVSync(),
    );

346
    final Animation<double> animation = TweenSequence<double>(
347
      <TweenSequenceItem<double>>[
348 349
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 5.0, end: 10.0),
350 351
          weight: 4.0,
        ),
352 353
        TweenSequenceItem<double>(
          tween: ConstantTween<double>(10.0),
354 355
          weight: 2.0,
        ),
356 357
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 10.0, end: 5.0),
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
          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', () {
382
    final AnimationController controller = AnimationController(
383 384 385
      vsync: const TestVSync(),
    );

386
    final Animation<double> animation = TweenSequence<double>(
387
      <TweenSequenceItem<double>>[
388 389 390
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 5.0, end: 10.0)
            .chain(CurveTween(curve: const Interval(0.5, 1.0))),
391 392
          weight: 4.0,
        ),
393 394 395
        TweenSequenceItem<double>(
          tween: ConstantTween<double>(10.0)
            .chain(CurveTween(curve: Curves.linear)), // linear is a no-op
396 397
          weight: 2.0,
        ),
398 399 400
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 10.0, end: 5.0)
            .chain(CurveTween(curve: const Interval(0.0, 0.5))),
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
          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', () {
425
    final AnimationController controller = AnimationController(
426 427 428
      vsync: const TestVSync(),
    );

429
    final Animation<double> animation = TweenSequence<double>(
430
      <TweenSequenceItem<double>>[
431 432
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 5.0, end: 10.0),
433 434 435 436 437 438 439 440 441 442 443 444 445 446
          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);
  });

447
}