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

import 'package:flutter/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart';
7
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
8

9 10
import '../scheduler/scheduler_tester.dart';

11
class BogusCurve extends Curve {
12 13
  const BogusCurve();

14 15 16 17
  @override
  double transform(double t) => 100.0;
}

18 19 20
void main() {
  setUp(() {
    WidgetsFlutterBinding.ensureInitialized();
21 22 23 24
    WidgetsBinding.instance
      ..resetEpoch()
      ..platformDispatcher.onBeginFrame = null
      ..platformDispatcher.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 132 133 134 135 136 137 138 139 140 141 142 143
  test('TrainHoppingAnimation dispatches memory events', () async {
    await expectLater(
      await memoryEvents(
        () => TrainHoppingAnimation(
          const AlwaysStoppedAnimation<double>(1),
          const AlwaysStoppedAnimation<double>(1),
        ).dispose(),
        TrainHoppingAnimation,
      ),
      areCreateAndDispose,
    );
  });

144
  test('AnimationMean control test', () {
145
    final AnimationController left = AnimationController(
146 147 148
      value: 0.5,
      vsync: const TestVSync(),
    );
149
    final AnimationController right = AnimationController(
150 151 152
      vsync: const TestVSync(),
    );

153
    final AnimationMean mean = AnimationMean(left: left, right: right);
154 155 156 157

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

158
    final List<double> log = <double>[];
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    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);
  });
178

179
  test('AnimationMax control test', () {
180
    final AnimationController first = AnimationController(
181 182 183
      value: 0.5,
      vsync: const TestVSync(),
    );
184
    final AnimationController second = AnimationController(
185 186 187
      vsync: const TestVSync(),
    );

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

    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', () {
215
    final AnimationController first = AnimationController(
216 217 218
      value: 0.5,
      vsync: const TestVSync(),
    );
219
    final AnimationController second = AnimationController(
220 221 222
      vsync: const TestVSync(),
    );

223
    final AnimationMin<double> min = AnimationMin<double>(first, second);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

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

249
  test('CurvedAnimation with bogus curve', () {
250
    final AnimationController controller = AnimationController(
251 252
      vsync: const TestVSync(),
    );
253
    final CurvedAnimation curved = CurvedAnimation(parent: controller, curve: const BogusCurve());
254
    FlutterError? error;
255 256 257 258 259 260
    try {
      curved.value;
    } on FlutterError catch (e) {
      error = e;
    }
    expect(error, isNotNull);
261 262
    expect(
      error!.toStringDeep(),
263 264 265
      // RegExp matcher is required here due to flutter web and flutter mobile generating
      // slightly different floating point numbers
      // in Flutter web 0.0 sometimes just appears as 0. or 0
266
      matches(RegExp(r'''
267
FlutterError
268 269 270
   Invalid curve endpoint at \d+(\.\d*)?\.
   Curves must map 0\.0 to near zero and 1\.0 to near one but
   BogusCurve mapped \d+(\.\d*)? to \d+(\.\d*)?, which is near \d+(\.\d*)?\.
271 272
''', multiLine: true)),
    );
273
  });
274

275 276 277 278 279 280 281 282 283
  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();
284
    tick(Duration.zero);
285
    tick(const Duration(milliseconds: 10));
286
    expect(curved.value, moreOrLessEquals(0.1));
287
    tick(const Duration(milliseconds: 20));
288
    expect(curved.value, moreOrLessEquals(0.2));
289
    tick(const Duration(milliseconds: 30));
290
    expect(curved.value, moreOrLessEquals(0.3));
291
    tick(const Duration(milliseconds: 40));
292
    expect(curved.value, moreOrLessEquals(0.4));
293
    tick(const Duration(milliseconds: 50));
294
    expect(curved.value, moreOrLessEquals(0.5));
295
    tick(const Duration(milliseconds: 60));
296
    expect(curved.value, moreOrLessEquals(0.6));
297
    tick(const Duration(milliseconds: 70));
298
    expect(curved.value, moreOrLessEquals(0.7));
299
    tick(const Duration(milliseconds: 80));
300
    expect(curved.value, moreOrLessEquals(0.8));
301
    tick(const Duration(milliseconds: 90));
302
    expect(curved.value, moreOrLessEquals(0.9));
303
    tick(const Duration(milliseconds: 100));
304
    expect(curved.value, moreOrLessEquals(1.0));
305 306
    controller.reverse();
    tick(const Duration(milliseconds: 110));
307
    expect(curved.value, moreOrLessEquals(1.0));
308
    tick(const Duration(milliseconds: 120));
309
    expect(curved.value, moreOrLessEquals(0.8));
310
    tick(const Duration(milliseconds: 130));
311
    expect(curved.value, moreOrLessEquals(0.6));
312
    tick(const Duration(milliseconds: 140));
313
    expect(curved.value, moreOrLessEquals(0.4));
314
    tick(const Duration(milliseconds: 150));
315
    expect(curved.value, moreOrLessEquals(0.2));
316
    tick(const Duration(milliseconds: 160));
317
    expect(curved.value, moreOrLessEquals(0.0));
318 319
  });

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
  test('CurvedAnimation stops listening to parent when disposed.', () async {
    const Interval forwardCurve = Interval(0.0, 0.5);
    const Interval reverseCurve = Interval(0.5, 1.0);

    final AnimationController controller = AnimationController(
      duration: const Duration(milliseconds: 100),
      reverseDuration: const Duration(milliseconds: 100),
      vsync: const TestVSync(),
    );
    final CurvedAnimation curved = CurvedAnimation(
        parent: controller, curve: forwardCurve, reverseCurve: reverseCurve);

    expect(forwardCurve.transform(0.5), 1.0);
    expect(reverseCurve.transform(0.5), 0.0);

    controller.forward(from: 0.5);
    expect(controller.status, equals(AnimationStatus.forward));
    expect(curved.value, equals(1.0));

    controller.value = 1.0;
    expect(controller.status, equals(AnimationStatus.completed));

    controller.reverse(from: 0.5);
    expect(controller.status, equals(AnimationStatus.reverse));
    expect(curved.value, equals(0.0));

    expect(curved.isDisposed, isFalse);
    curved.dispose();
    expect(curved.isDisposed, isTrue);

    controller.value = 0.0;
    expect(controller.status, equals(AnimationStatus.dismissed));

    controller.forward(from: 0.5);
    expect(controller.status, equals(AnimationStatus.forward));
    expect(curved.value, equals(0.0));
  });

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
  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();
373
    tick(Duration.zero);
374
    tick(const Duration(milliseconds: 10));
375
    expect(reversed.value, moreOrLessEquals(0.9));
376
    tick(const Duration(milliseconds: 20));
377
    expect(reversed.value, moreOrLessEquals(0.8));
378
    tick(const Duration(milliseconds: 30));
379
    expect(reversed.value, moreOrLessEquals(0.7));
380
    tick(const Duration(milliseconds: 40));
381
    expect(reversed.value, moreOrLessEquals(0.6));
382
    tick(const Duration(milliseconds: 50));
383
    expect(reversed.value, moreOrLessEquals(0.5));
384
    tick(const Duration(milliseconds: 60));
385
    expect(reversed.value, moreOrLessEquals(0.4));
386
    tick(const Duration(milliseconds: 70));
387
    expect(reversed.value, moreOrLessEquals(0.3));
388
    tick(const Duration(milliseconds: 80));
389
    expect(reversed.value, moreOrLessEquals(0.2));
390
    tick(const Duration(milliseconds: 90));
391
    expect(reversed.value, moreOrLessEquals(0.1));
392
    tick(const Duration(milliseconds: 100));
393
    expect(reversed.value, moreOrLessEquals(0.0));
394 395
    controller.reverse();
    tick(const Duration(milliseconds: 110));
396
    expect(reversed.value, moreOrLessEquals(0.0));
397
    tick(const Duration(milliseconds: 120));
398
    expect(reversed.value, moreOrLessEquals(0.2));
399
    tick(const Duration(milliseconds: 130));
400
    expect(reversed.value, moreOrLessEquals(0.4));
401
    tick(const Duration(milliseconds: 140));
402
    expect(reversed.value, moreOrLessEquals(0.6));
403
    tick(const Duration(milliseconds: 150));
404
    expect(reversed.value, moreOrLessEquals(0.8));
405
    tick(const Duration(milliseconds: 160));
406
    expect(reversed.value, moreOrLessEquals(1.0));
407 408
  });

409
  test('TweenSequence', () {
410
    final AnimationController controller = AnimationController(
411 412 413
      vsync: const TestVSync(),
    );

414
    final Animation<double> animation = TweenSequence<double>(
415
      <TweenSequenceItem<double>>[
416 417
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 5.0, end: 10.0),
418 419
          weight: 4.0,
        ),
420 421
        TweenSequenceItem<double>(
          tween: ConstantTween<double>(10.0),
422 423
          weight: 2.0,
        ),
424 425
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 10.0, end: 5.0),
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
          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', () {
450
    final AnimationController controller = AnimationController(
451 452 453
      vsync: const TestVSync(),
    );

454
    final Animation<double> animation = TweenSequence<double>(
455
      <TweenSequenceItem<double>>[
456 457 458
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 5.0, end: 10.0)
            .chain(CurveTween(curve: const Interval(0.5, 1.0))),
459 460
          weight: 4.0,
        ),
461 462 463
        TweenSequenceItem<double>(
          tween: ConstantTween<double>(10.0)
            .chain(CurveTween(curve: Curves.linear)), // linear is a no-op
464 465
          weight: 2.0,
        ),
466 467 468
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 10.0, end: 5.0)
            .chain(CurveTween(curve: const Interval(0.0, 0.5))),
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
          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', () {
493
    final AnimationController controller = AnimationController(
494 495 496
      vsync: const TestVSync(),
    );

497
    final Animation<double> animation = TweenSequence<double>(
498
      <TweenSequenceItem<double>>[
499 500
        TweenSequenceItem<double>(
          tween: Tween<double>(begin: 5.0, end: 10.0),
501 502 503 504 505 506 507 508 509 510 511 512 513 514
          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);
  });

515
}