implicit_animations_test.dart 27.9 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
import 'package:flutter/material.dart';
6 7
import 'package:flutter_test/flutter_test.dart';

8
class MockOnEndFunction {
9 10
  int called = 0;

11
  void handler() {
12 13 14 15 16 17 18
    called++;
  }
}

const Duration animationDuration = Duration(milliseconds:1000);
const Duration additionalDelay = Duration(milliseconds:1);

19
void main() {
20
  late MockOnEndFunction mockOnEndFunction;
21 22 23 24 25 26
  const Key switchKey = Key('switchKey');

  setUp(() {
    mockOnEndFunction = MockOnEndFunction();
  });

27
  testWidgets('BoxConstraintsTween control test', (WidgetTester tester) async {
28 29
    final BoxConstraintsTween tween = BoxConstraintsTween(
      begin: BoxConstraints.tight(const Size(20.0, 50.0)),
30
      end: BoxConstraints.tight(const Size(10.0, 30.0)),
31
    );
32
    final BoxConstraints result = tween.lerp(0.25);
33 34 35 36 37 38
    expect(result.minWidth, 17.5);
    expect(result.maxWidth, 17.5);
    expect(result.minHeight, 45.0);
    expect(result.maxHeight, 45.0);
  });

39
  testWidgets('DecorationTween control test', (WidgetTester tester) async {
40
    final DecorationTween tween = DecorationTween(
41
      begin: const BoxDecoration(color: Color(0xFF00FF00)),
42
      end: const BoxDecoration(color: Color(0xFFFFFF00)),
43
    );
44
    final BoxDecoration result = tween.lerp(0.25) as BoxDecoration;
45
    expect(result.color, const Color(0xFF3FFF00));
46 47
  });

48
  testWidgets('EdgeInsetsTween control test', (WidgetTester tester) async {
49
    final EdgeInsetsTween tween = EdgeInsetsTween(
50
      begin: const EdgeInsets.symmetric(vertical: 50.0),
51
      end: const EdgeInsets.only(top: 10.0, bottom: 30.0),
52
    );
53
    final EdgeInsets result = tween.lerp(0.25);
54 55 56 57 58 59
    expect(result.left, 0.0);
    expect(result.right, 0.0);
    expect(result.top, 40.0);
    expect(result.bottom, 45.0);
  });

60
  testWidgets('Matrix4Tween control test', (WidgetTester tester) async {
61 62
    final Matrix4Tween tween = Matrix4Tween(
      begin: Matrix4.translationValues(10.0, 20.0, 30.0),
63
      end: Matrix4.translationValues(14.0, 24.0, 34.0),
64
    );
65
    final Matrix4 result = tween.lerp(0.25);
66
    expect(result, equals(Matrix4.translationValues(11.0, 21.0, 31.0)));
67
  });
68

69
  testWidgets('AnimatedContainer onEnd callback test', (WidgetTester tester) async {
70
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
71
      child: TestAnimatedWidget(
72
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
73 74
        switchKey: switchKey,
        state: _TestAnimatedContainerWidgetState(),
75
      ),
76 77 78 79 80 81 82 83 84 85 86 87
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);

    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);
88 89

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
90 91
  });

92
  testWidgets('AnimatedPadding onEnd callback test', (WidgetTester tester) async {
93
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
94
      child: TestAnimatedWidget(
95
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
96 97
        switchKey: switchKey,
        state: _TestAnimatedPaddingWidgetState(),
98
      ),
99 100 101 102 103 104 105 106 107 108 109 110
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);

    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);
111 112

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
113 114
  });

115
  testWidgets('AnimatedAlign onEnd callback test', (WidgetTester tester) async {
116
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
117
      child: TestAnimatedWidget(
118
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
119 120
        switchKey: switchKey,
        state: _TestAnimatedAlignWidgetState(),
121
      ),
122 123 124 125 126 127 128 129 130 131 132 133
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);

    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);
134 135

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
136 137
  });

138
  testWidgets('AnimatedPositioned onEnd callback test', (WidgetTester tester) async {
139
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
140
      child: TestAnimatedWidget(
141
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
142 143
        switchKey: switchKey,
        state: _TestAnimatedPositionedWidgetState(),
144
      ),
145 146 147 148 149 150 151 152 153 154 155 156
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);

    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);
157 158

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
159 160
  });

161
  testWidgets('AnimatedPositionedDirectional onEnd callback test', (WidgetTester tester) async {
162
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
163
      child: TestAnimatedWidget(
164
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
165 166
        switchKey: switchKey,
        state: _TestAnimatedPositionedDirectionalWidgetState(),
167
      ),
168 169 170 171 172 173 174 175 176 177 178 179
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);

    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);
180 181

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
182 183
  });

184
   testWidgets('AnimatedSlide onEnd callback test', (WidgetTester tester) async {
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    await tester.pumpWidget(wrap(
      child: TestAnimatedWidget(
        callback: mockOnEndFunction.handler,
        switchKey: switchKey,
        state: _TestAnimatedSlideWidgetState(),
      ),
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);
    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);
202 203

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
204 205
  });

206
  testWidgets('AnimatedSlide transition test', (WidgetTester tester) async {
207 208 209 210 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 236 237 238 239 240 241 242 243
    await tester.pumpWidget(wrap(
      child: TestAnimatedWidget(
        switchKey: switchKey,
        state: _TestAnimatedSlideWidgetState(),
      ),
    ));

    final RebuildCountingState<StatefulWidget> state = tester.widget<TestAnimatedWidget>(
      find.byType(TestAnimatedWidget)
    ).rebuildState!;
    final Finder switchFinder = find.byKey(switchKey);
    final SlideTransition slideWidget = tester.widget<SlideTransition>(
      find.ancestor(
        of: find.byType(Placeholder),
        matching: find.byType(SlideTransition),
      ).first,
    );

    expect(state.builds, equals(1));

    await tester.tap(switchFinder);
    expect(state.builds, equals(1));
    await tester.pump();
    expect(slideWidget.position.value, equals(Offset.zero));
    expect(state.builds, equals(2));

    await tester.pump(const Duration(milliseconds: 500));
    expect(slideWidget.position.value, equals(const Offset(0.5,0.5)));
    expect(state.builds, equals(2));
    await tester.pump(const Duration(milliseconds: 250));
    expect(slideWidget.position.value, equals(const Offset(0.75,0.75)));
    expect(state.builds, equals(2));
    await tester.pump(const Duration(milliseconds: 250));
    expect(slideWidget.position.value, equals(const Offset(1,1)));
    expect(state.builds, equals(2));
  });

244
  testWidgets('AnimatedScale onEnd callback test', (WidgetTester tester) async {
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    await tester.pumpWidget(wrap(
      child: TestAnimatedWidget(
        callback: mockOnEndFunction.handler,
        switchKey: switchKey,
        state: _TestAnimatedScaleWidgetState(),
      ),
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);
    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);
262 263

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
264 265
  });

266
  testWidgets('AnimatedScale transition test', (WidgetTester tester) async {
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
    await tester.pumpWidget(wrap(
      child: TestAnimatedWidget(
        switchKey: switchKey,
        state: _TestAnimatedScaleWidgetState(),
      ),
    ));

    final RebuildCountingState<StatefulWidget> state = tester.widget<TestAnimatedWidget>(
      find.byType(TestAnimatedWidget)
    ).rebuildState!;
    final Finder switchFinder = find.byKey(switchKey);
    final ScaleTransition scaleWidget = tester.widget<ScaleTransition>(
      find.ancestor(
        of: find.byType(Placeholder),
        matching: find.byType(ScaleTransition),
      ).first,
    );

    expect(state.builds, equals(1));

    await tester.tap(switchFinder);
    expect(state.builds, equals(1));
    await tester.pump();
    expect(scaleWidget.scale.value, equals(1.0));
    expect(state.builds, equals(2));

    await tester.pump(const Duration(milliseconds: 500));
    expect(scaleWidget.scale.value, equals(1.5));
    expect(state.builds, equals(2));
    await tester.pump(const Duration(milliseconds: 250));
    expect(scaleWidget.scale.value, equals(1.75));
    expect(state.builds, equals(2));
    await tester.pump(const Duration(milliseconds: 250));
    expect(scaleWidget.scale.value, equals(2.0));
    expect(state.builds, equals(2));
  });

304
  testWidgets('AnimatedRotation onEnd callback test', (WidgetTester tester) async {
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    await tester.pumpWidget(wrap(
      child: TestAnimatedWidget(
        callback: mockOnEndFunction.handler,
        switchKey: switchKey,
        state: _TestAnimatedRotationWidgetState(),
      ),
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);
    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);
322 323

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
324 325
  });

326
  testWidgets('AnimatedRotation transition test', (WidgetTester tester) async {
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 358 359 360 361 362 363
    await tester.pumpWidget(wrap(
      child: TestAnimatedWidget(
        switchKey: switchKey,
        state: _TestAnimatedRotationWidgetState(),
      ),
    ));

    final RebuildCountingState<StatefulWidget> state = tester.widget<TestAnimatedWidget>(
        find.byType(TestAnimatedWidget)
    ).rebuildState!;
    final Finder switchFinder = find.byKey(switchKey);
    final RotationTransition rotationWidget = tester.widget<RotationTransition>(
      find.ancestor(
        of: find.byType(Placeholder),
        matching: find.byType(RotationTransition),
      ).first,
    );

    expect(state.builds, equals(1));

    await tester.tap(switchFinder);
    expect(state.builds, equals(1));
    await tester.pump();
    expect(rotationWidget.turns.value, equals(0.0));
    expect(state.builds, equals(2));

    await tester.pump(const Duration(milliseconds: 500));
    expect(rotationWidget.turns.value, equals(0.75));
    expect(state.builds, equals(2));
    await tester.pump(const Duration(milliseconds: 250));
    expect(rotationWidget.turns.value, equals(1.125));
    expect(state.builds, equals(2));
    await tester.pump(const Duration(milliseconds: 250));
    expect(rotationWidget.turns.value, equals(1.5));
    expect(state.builds, equals(2));
  });

364
  testWidgets('AnimatedOpacity onEnd callback test', (WidgetTester tester) async {
365
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
366
      child: TestAnimatedWidget(
367
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
368 369
        switchKey: switchKey,
        state: _TestAnimatedOpacityWidgetState(),
370
      ),
Kate Lovett's avatar
Kate Lovett committed
371 372 373 374 375 376 377 378 379 380 381
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);
    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);
382 383

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
Kate Lovett's avatar
Kate Lovett committed
384 385
  });

386
  testWidgets('AnimatedOpacity transition test', (WidgetTester tester) async {
Kate Lovett's avatar
Kate Lovett committed
387 388 389 390
    await tester.pumpWidget(wrap(
      child: TestAnimatedWidget(
        switchKey: switchKey,
        state: _TestAnimatedOpacityWidgetState(),
391
      ),
Kate Lovett's avatar
Kate Lovett committed
392 393
    ));

394 395 396
    final RebuildCountingState<StatefulWidget> state = tester.widget<TestAnimatedWidget>(
        find.byType(TestAnimatedWidget)
    ).rebuildState!;
Kate Lovett's avatar
Kate Lovett committed
397 398 399 400 401 402 403 404
    final Finder switchFinder = find.byKey(switchKey);
    final FadeTransition opacityWidget = tester.widget<FadeTransition>(
      find.ancestor(
        of: find.byType(Placeholder),
        matching: find.byType(FadeTransition),
      ).first,
    );

405 406
    expect(state.builds, equals(1));

Kate Lovett's avatar
Kate Lovett committed
407
    await tester.tap(switchFinder);
408
    expect(state.builds, equals(1));
Kate Lovett's avatar
Kate Lovett committed
409 410
    await tester.pump();
    expect(opacityWidget.opacity.value, equals(0.0));
411
    expect(state.builds, equals(2));
Kate Lovett's avatar
Kate Lovett committed
412 413 414

    await tester.pump(const Duration(milliseconds: 500));
    expect(opacityWidget.opacity.value, equals(0.5));
415
    expect(state.builds, equals(2));
Kate Lovett's avatar
Kate Lovett committed
416 417
    await tester.pump(const Duration(milliseconds: 250));
    expect(opacityWidget.opacity.value, equals(0.75));
418
    expect(state.builds, equals(2));
Kate Lovett's avatar
Kate Lovett committed
419 420
    await tester.pump(const Duration(milliseconds: 250));
    expect(opacityWidget.opacity.value, equals(1.0));
421
    expect(state.builds, equals(2));
Kate Lovett's avatar
Kate Lovett committed
422 423
  });

424
  testWidgets('AnimatedFractionallySizedBox onEnd callback test', (WidgetTester tester) async {
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    await tester.pumpWidget(wrap(
      child: TestAnimatedWidget(
        callback: mockOnEndFunction.handler,
        switchKey: switchKey,
        state: _TestAnimatedFractionallySizedBoxWidgetState(),
      ),
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);
    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
  });
Kate Lovett's avatar
Kate Lovett committed
445

446
  testWidgets('SliverAnimatedOpacity onEnd callback test', (WidgetTester tester) async {
Kate Lovett's avatar
Kate Lovett committed
447
    await tester.pumpWidget(TestAnimatedWidget(
448
      callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
449 450
      switchKey: switchKey,
      state: _TestSliverAnimatedOpacityWidgetState(),
451 452 453 454 455 456 457 458 459 460 461 462
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);

    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);
463 464

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
465 466
  });

467
  testWidgets('SliverAnimatedOpacity transition test', (WidgetTester tester) async {
Kate Lovett's avatar
Kate Lovett committed
468 469 470 471
    await tester.pumpWidget(wrap(
      child: TestAnimatedWidget(
        switchKey: switchKey,
        state: _TestSliverAnimatedOpacityWidgetState(),
472
      ),
Kate Lovett's avatar
Kate Lovett committed
473 474
    ));

475 476 477
    final RebuildCountingState<StatefulWidget> state = tester.widget<TestAnimatedWidget>(
        find.byType(TestAnimatedWidget)
    ).rebuildState!;
Kate Lovett's avatar
Kate Lovett committed
478 479 480 481 482 483 484 485
    final Finder switchFinder = find.byKey(switchKey);
    final SliverFadeTransition opacityWidget = tester.widget<SliverFadeTransition>(
      find.ancestor(
        of: find.byType(Placeholder),
        matching: find.byType(SliverFadeTransition),
      ).first,
    );

486 487
    expect(state.builds, equals(1));

Kate Lovett's avatar
Kate Lovett committed
488
    await tester.tap(switchFinder);
489
    expect(state.builds, equals(1));
Kate Lovett's avatar
Kate Lovett committed
490 491
    await tester.pump();
    expect(opacityWidget.opacity.value, equals(0.0));
492
    expect(state.builds, equals(2));
Kate Lovett's avatar
Kate Lovett committed
493 494 495

    await tester.pump(const Duration(milliseconds: 500));
    expect(opacityWidget.opacity.value, equals(0.5));
496
    expect(state.builds, equals(2));
Kate Lovett's avatar
Kate Lovett committed
497 498
    await tester.pump(const Duration(milliseconds: 250));
    expect(opacityWidget.opacity.value, equals(0.75));
499
    expect(state.builds, equals(2));
Kate Lovett's avatar
Kate Lovett committed
500 501
    await tester.pump(const Duration(milliseconds: 250));
    expect(opacityWidget.opacity.value, equals(1.0));
502
    expect(state.builds, equals(2));
Kate Lovett's avatar
Kate Lovett committed
503 504
  });

505
  testWidgets('AnimatedDefaultTextStyle onEnd callback test', (WidgetTester tester) async {
506
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
507
      child: TestAnimatedWidget(
508
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
509 510
        switchKey: switchKey,
        state: _TestAnimatedDefaultTextStyleWidgetState(),
511
      ),
512 513 514 515 516 517 518 519 520 521 522 523
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);

    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);
524 525

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
526 527
  });

528
  testWidgets('AnimatedPhysicalModel onEnd callback test', (WidgetTester tester) async {
529
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
530
      child: TestAnimatedWidget(
531
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
532 533
        switchKey: switchKey,
        state: _TestAnimatedPhysicalModelWidgetState(),
534
      ),
535 536 537 538 539 540 541 542 543 544 545 546
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);

    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);
547 548

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
549 550
  });

551
  testWidgets('TweenAnimationBuilder onEnd callback test', (WidgetTester tester) async {
552
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
553
      child: TestAnimatedWidget(
554
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
555 556
        switchKey: switchKey,
        state: _TestTweenAnimationBuilderWidgetState(),
557
      ),
558 559 560 561 562 563 564 565 566 567 568 569
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);

    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);
570 571

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
572 573
  });

574
  testWidgets('AnimatedTheme onEnd callback test', (WidgetTester tester) async {
575
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
576
      child: TestAnimatedWidget(
577
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
578 579
        switchKey: switchKey,
        state: _TestAnimatedThemeWidgetState(),
580
      ),
581 582 583 584 585 586 587 588 589 590 591 592
    ));

    final Finder widgetFinder = find.byKey(switchKey);

    await tester.tap(widgetFinder);

    await tester.pump();
    expect(mockOnEndFunction.called, 0);
    await tester.pump(animationDuration);
    expect(mockOnEndFunction.called, 0);
    await tester.pump(additionalDelay);
    expect(mockOnEndFunction.called, 1);
593 594

    await tapTest2and3(tester, widgetFinder, mockOnEndFunction);
595
  });
596

597
  testWidgets('Ensure CurvedAnimations are disposed on widget change',
598 599 600 601
      (WidgetTester tester) async {
    final GlobalKey<ImplicitlyAnimatedWidgetState<AnimatedOpacity>> key =
        GlobalKey<ImplicitlyAnimatedWidgetState<AnimatedOpacity>>();
    final ValueNotifier<Curve> curve = ValueNotifier<Curve>(const Interval(0.0, 0.5));
602
    addTearDown(curve.dispose);
603 604 605 606 607 608 609 610 611 612 613 614 615 616
    await tester.pumpWidget(wrap(
      child: ValueListenableBuilder<Curve>(
        valueListenable: curve,
        builder: (_, Curve c, __) => AnimatedOpacity(
            key: key,
            opacity: 1.0,
            duration: const Duration(seconds: 1),
            curve: c,
            child: Container(color: Colors.green)),
      ),
    ));

    final ImplicitlyAnimatedWidgetState<AnimatedOpacity>? firstState = key.currentState;
    final Animation<double>? firstAnimation = firstState?.animation;
617
    if (firstAnimation == null) {
618
      fail('animation was null!');
619
    }
620 621 622 623 624 625 626 627 628 629 630

    final CurvedAnimation firstCurvedAnimation =
        firstAnimation as CurvedAnimation;

    expect(firstCurvedAnimation.isDisposed, isFalse);

    curve.value = const Interval(0.0, 0.6);
    await tester.pumpAndSettle();

    final ImplicitlyAnimatedWidgetState<AnimatedOpacity>? secondState = key.currentState;
    final Animation<double>? secondAnimation = secondState?.animation;
631
    if (secondAnimation == null) {
632
      fail('animation was null!');
633
    }
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651

    final CurvedAnimation secondCurvedAnimation = secondAnimation as CurvedAnimation;

    expect(firstState, equals(secondState));
    expect(firstAnimation, isNot(equals(secondAnimation)));

    expect(firstCurvedAnimation.isDisposed, isTrue);
    expect(secondCurvedAnimation.isDisposed, isFalse);

    await tester.pumpWidget(
      wrap(
        child: const Offstage(),
      ),
    );
    await tester.pumpAndSettle();

    expect(secondCurvedAnimation.isDisposed, isTrue);
  });
652 653
}

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
Future<void> tapTest2and3(WidgetTester tester, Finder widgetFinder,
    MockOnEndFunction mockOnEndFunction) async {
  await tester.tap(widgetFinder);

  await tester.pump();
  await tester.pump(animationDuration + additionalDelay);
  expect(mockOnEndFunction.called, 2);

  await tester.tap(widgetFinder);

  await tester.pump();
  await tester.pump(animationDuration + additionalDelay);
  expect(mockOnEndFunction.called, 3);
}

669
Widget wrap({required Widget child}) {
670 671 672 673 674 675 676 677
  return Directionality(
    textDirection: TextDirection.ltr,
    child: Material(
      child: Center(child: child),
    ),
  );
}

678 679 680 681
abstract class RebuildCountingState<T extends StatefulWidget> extends State<T> {
  int builds = 0;
}

682
class TestAnimatedWidget extends StatefulWidget {
683
  const TestAnimatedWidget({
684
    super.key,
685
    this.callback,
686 687
    required this.switchKey,
    required this.state,
688
  });
689
  final VoidCallback? callback;
690 691 692
  final Key switchKey;
  final State<StatefulWidget> state;

693 694 695
  RebuildCountingState<StatefulWidget>? get rebuildState =>
    state is RebuildCountingState<StatefulWidget> ? state as RebuildCountingState<StatefulWidget> : null;

696
  @override
Ian Hickson's avatar
Ian Hickson committed
697
  State<StatefulWidget> createState() => state; // ignore: no_logic_in_create_state, this test predates the lint
698 699
}

700
abstract class _TestAnimatedWidgetState extends RebuildCountingState<TestAnimatedWidget> {
701 702 703 704 705 706 707 708 709 710 711 712 713 714
  bool toggle = false;
  final Widget child = const Placeholder();
  final Duration duration = animationDuration;

  void onChanged(bool v) {
    setState(() {
      toggle = v;
    });
  }

  Widget getAnimatedWidget();

  @override
  Widget build(BuildContext context) {
715
    builds++;
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
    final Widget animatedWidget = getAnimatedWidget();

    return Stack(
      children: <Widget>[
        animatedWidget,
        Switch(key: widget.switchKey, value: toggle, onChanged: onChanged),
      ],
    );
  }
}

class _TestAnimatedContainerWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedContainer(
      duration: duration,
      onEnd: widget.callback,
      width: toggle ? 10 : 20,
734
      foregroundDecoration: toggle ? const BoxDecoration() : null,
735
      child: child,
736 737 738 739 740 741 742 743 744 745 746 747
    );
  }
}

class _TestAnimatedPaddingWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedPadding(
      duration: duration,
      onEnd: widget.callback,
      padding:
      toggle ? const EdgeInsets.all(8.0) : const EdgeInsets.all(16.0),
748
      child: child,
749 750 751 752 753 754 755 756 757 758 759
    );
  }
}

class _TestAnimatedAlignWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedAlign(
      duration: duration,
      onEnd: widget.callback,
      alignment: toggle ? Alignment.topLeft : Alignment.bottomRight,
760
      child: child,
761 762 763 764 765 766 767 768 769 770 771
    );
  }
}

class _TestAnimatedPositionedWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedPositioned(
      duration: duration,
      onEnd: widget.callback,
      left: toggle ? 10 : 20,
772
      child: child,
773 774 775 776 777 778 779 780 781 782 783
    );
  }
}

class _TestAnimatedPositionedDirectionalWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedPositionedDirectional(
      duration: duration,
      onEnd: widget.callback,
      start: toggle ? 10 : 20,
784
      child: child,
785 786 787 788
    );
  }
}

789 790 791 792 793 794 795 796 797 798 799 800
class _TestAnimatedSlideWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedSlide(
      duration: duration,
      onEnd: widget.callback,
      offset: toggle ? const Offset(1,1) : Offset.zero,
      child: child,
    );
  }
}

801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
class _TestAnimatedScaleWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedScale(
      duration: duration,
      onEnd: widget.callback,
      scale: toggle ? 2.0 : 1.0,
      child: child,
    );
  }
}

class _TestAnimatedRotationWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedRotation(
      duration: duration,
      onEnd: widget.callback,
      turns: toggle ? 1.5 : 0.0,
      child: child,
    );
  }
}

825 826 827 828 829 830
class _TestAnimatedOpacityWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedOpacity(
      duration: duration,
      onEnd: widget.callback,
Kate Lovett's avatar
Kate Lovett committed
831
      opacity: toggle ? 1.0 : 0.0,
832
      child: child,
Kate Lovett's avatar
Kate Lovett committed
833 834 835 836
    );
  }
}

837 838 839 840 841 842 843 844 845 846 847 848 849
class _TestAnimatedFractionallySizedBoxWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedFractionallySizedBox(
      duration: duration,
      onEnd: widget.callback,
      heightFactor: toggle ? 0.25 : 0.75,
      widthFactor: toggle ? 0.25 : 0.75,
      child: child,
    );
  }
}

Kate Lovett's avatar
Kate Lovett committed
850 851 852 853 854 855 856 857 858 859 860 861 862
class _TestSliverAnimatedOpacityWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return SliverAnimatedOpacity(
      sliver: SliverToBoxAdapter(child: child),
      duration: duration,
      onEnd: widget.callback,
      opacity: toggle ? 1.0 : 0.0,
    );
  }

  @override
  Widget build(BuildContext context) {
863
    builds++;
Kate Lovett's avatar
Kate Lovett committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
    final Widget animatedWidget = getAnimatedWidget();

    return Material(
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: CustomScrollView(
          slivers: <Widget>[
            animatedWidget,
            SliverToBoxAdapter(
              child: Switch(
                key: widget.switchKey,
                value: toggle,
                onChanged: onChanged,
              ),
            ),
          ],
        ),
      ),
882 883 884 885 886 887 888 889
    );
  }
}

class _TestAnimatedDefaultTextStyleWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedDefaultTextStyle(
Kate Lovett's avatar
Kate Lovett committed
890 891 892 893
      duration: duration,
      onEnd: widget.callback,
      style: toggle
        ? const TextStyle(fontStyle: FontStyle.italic)
894
        : const TextStyle(fontStyle: FontStyle.normal),
895
      child: child,
896
    );
897 898 899 900 901 902 903 904 905 906 907 908 909
  }
}

class _TestAnimatedPhysicalModelWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedPhysicalModel(
      duration: duration,
      onEnd: widget.callback,
      color: toggle ? Colors.red : Colors.green,
      elevation: 0,
      shadowColor: Colors.blue,
      shape: BoxShape.rectangle,
910
      child: child,
911 912 913 914 915 916 917 918
    );
  }
}

class _TestTweenAnimationBuilderWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return TweenAnimationBuilder<double>(
919
      tween: toggle ? Tween<double>(begin: 1, end: 2) : Tween<double>(begin: 2, end: 1),
Kate Lovett's avatar
Kate Lovett committed
920 921
      duration: duration,
      onEnd: widget.callback,
922
      child: child,
923
      builder: (BuildContext context, double? size, Widget? child) {
924
        return SizedBox(
Kate Lovett's avatar
Kate Lovett committed
925 926
          width: size,
          height: size,
927
          child: child,
Kate Lovett's avatar
Kate Lovett committed
928 929
        );
      },
930 931 932 933 934 935 936 937 938 939 940
    );
  }
}

class _TestAnimatedThemeWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedTheme(
      data: toggle ? ThemeData.dark() : ThemeData.light(),
      duration: duration,
      onEnd: widget.callback,
941
      child: child,
942 943
    );
  }
944
}