implicit_animations_test.dart 16 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 6
// @dart = 2.8

7
import 'package:flutter/material.dart';
8 9 10
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';

11
class MockOnEndFunction {
12 13
  int called = 0;

14
  void handler() {
15 16 17 18 19 20 21
    called++;
  }
}

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

22
void main() {
23 24 25 26 27 28 29
  MockOnEndFunction mockOnEndFunction;
  const Key switchKey = Key('switchKey');

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

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

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

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

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

  testWidgets('AnimatedContainer onEnd callback test', (WidgetTester tester) async {
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
74
      child: TestAnimatedWidget(
75
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
76 77 78
        switchKey: switchKey,
        state: _TestAnimatedContainerWidgetState(),
      )
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    ));

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

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

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

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

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

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

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

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

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

  testWidgets('AnimatedOpacity onEnd callback test', (WidgetTester tester) async {
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
179
      child: TestAnimatedWidget(
180
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
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 226 227
        switchKey: switchKey,
        state: _TestAnimatedOpacityWidgetState(),
      )
    ));

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

  testWidgets('AnimatedOpacity transition test', (WidgetTester tester) async {
    await tester.pumpWidget(wrap(
      child: TestAnimatedWidget(
        switchKey: switchKey,
        state: _TestAnimatedOpacityWidgetState(),
      )
    ));

    final Finder switchFinder = find.byKey(switchKey);
    final FadeTransition opacityWidget = tester.widget<FadeTransition>(
      find.ancestor(
        of: find.byType(Placeholder),
        matching: find.byType(FadeTransition),
      ).first,
    );

    await tester.tap(switchFinder);
    await tester.pump();
    expect(opacityWidget.opacity.value, equals(0.0));

    await tester.pump(const Duration(milliseconds: 500));
    expect(opacityWidget.opacity.value, equals(0.5));
    await tester.pump(const Duration(milliseconds: 250));
    expect(opacityWidget.opacity.value, equals(0.75));
    await tester.pump(const Duration(milliseconds: 250));
    expect(opacityWidget.opacity.value, equals(1.0));
  });


  testWidgets('SliverAnimatedOpacity onEnd callback test', (WidgetTester tester) async {
    await tester.pumpWidget(TestAnimatedWidget(
228
      callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
229 230
      switchKey: switchKey,
      state: _TestSliverAnimatedOpacityWidgetState(),
231 232 233 234 235 236 237 238 239 240 241 242 243 244
    ));

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

Kate Lovett's avatar
Kate Lovett committed
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
  testWidgets('SliverAnimatedOpacity transition test', (WidgetTester tester) async {
    await tester.pumpWidget(wrap(
      child: TestAnimatedWidget(
        switchKey: switchKey,
        state: _TestSliverAnimatedOpacityWidgetState(),
      )
    ));

    final Finder switchFinder = find.byKey(switchKey);
    final SliverFadeTransition opacityWidget = tester.widget<SliverFadeTransition>(
      find.ancestor(
        of: find.byType(Placeholder),
        matching: find.byType(SliverFadeTransition),
      ).first,
    );

    await tester.tap(switchFinder);
    await tester.pump();
    expect(opacityWidget.opacity.value, equals(0.0));

    await tester.pump(const Duration(milliseconds: 500));
    expect(opacityWidget.opacity.value, equals(0.5));
    await tester.pump(const Duration(milliseconds: 250));
    expect(opacityWidget.opacity.value, equals(0.75));
    await tester.pump(const Duration(milliseconds: 250));
    expect(opacityWidget.opacity.value, equals(1.0));
  });

273 274
  testWidgets('AnimatedDefaultTextStyle onEnd callback test', (WidgetTester tester) async {
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
275
      child: TestAnimatedWidget(
276
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
277 278 279
        switchKey: switchKey,
        state: _TestAnimatedDefaultTextStyleWidgetState(),
      )
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    ));

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

  testWidgets('AnimatedPhysicalModel onEnd callback test', (WidgetTester tester) async {
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
296
      child: TestAnimatedWidget(
297
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
298 299 300
        switchKey: switchKey,
        state: _TestAnimatedPhysicalModelWidgetState(),
      )
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    ));

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

  testWidgets('TweenAnimationBuilder onEnd callback test', (WidgetTester tester) async {
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
317
      child: TestAnimatedWidget(
318
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
319 320 321
        switchKey: switchKey,
        state: _TestTweenAnimationBuilderWidgetState(),
      )
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    ));

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

  testWidgets('AnimatedTheme onEnd callback test', (WidgetTester tester) async {
    await tester.pumpWidget(wrap(
Kate Lovett's avatar
Kate Lovett committed
338
      child: TestAnimatedWidget(
339
        callback: mockOnEndFunction.handler,
Kate Lovett's avatar
Kate Lovett committed
340 341 342
        switchKey: switchKey,
        state: _TestAnimatedThemeWidgetState(),
      )
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    ));

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

Widget wrap({Widget child}) {
  return Directionality(
    textDirection: TextDirection.ltr,
    child: Material(
      child: Center(child: child),
    ),
  );
}

class TestAnimatedWidget extends StatefulWidget {
368 369 370 371 372 373
  const TestAnimatedWidget({
    Key key,
    this.callback,
    this.switchKey,
    this.state,
  }) : super(key: key);
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
  @required
  final VoidCallback callback;
  @required
  final Key switchKey;
  @required
  final State<StatefulWidget> state;

  @override
  State<StatefulWidget> createState() => state;
}

abstract class _TestAnimatedWidgetState extends State<TestAnimatedWidget> {
  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) {
    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(
      child: child,
      duration: duration,
      onEnd: widget.callback,
      width: toggle ? 10 : 20,
    );
  }
}

class _TestAnimatedPaddingWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedPadding(
      child: child,
      duration: duration,
      onEnd: widget.callback,
      padding:
      toggle ? const EdgeInsets.all(8.0) : const EdgeInsets.all(16.0),
    );
  }
}

class _TestAnimatedAlignWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedAlign(
      child: child,
      duration: duration,
      onEnd: widget.callback,
      alignment: toggle ? Alignment.topLeft : Alignment.bottomRight,
    );
  }
}

class _TestAnimatedPositionedWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedPositioned(
      child: child,
      duration: duration,
      onEnd: widget.callback,
      left: toggle ? 10 : 20,
    );
  }
}

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

class _TestAnimatedOpacityWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedOpacity(
      child: child,
      duration: duration,
      onEnd: widget.callback,
Kate Lovett's avatar
Kate Lovett committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
      opacity: toggle ? 1.0 : 0.0,
    );
  }
}

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) {
    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,
              ),
            ),
          ],
        ),
      ),
516 517 518 519 520 521 522 523
    );
  }
}

class _TestAnimatedDefaultTextStyleWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return AnimatedDefaultTextStyle(
Kate Lovett's avatar
Kate Lovett committed
524 525 526 527 528 529
      child: child,
      duration: duration,
      onEnd: widget.callback,
      style: toggle
        ? const TextStyle(fontStyle: FontStyle.italic)
        : const TextStyle(fontStyle: FontStyle.normal));
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
  }
}

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

class _TestTweenAnimationBuilderWidgetState extends _TestAnimatedWidgetState {
  @override
  Widget getAnimatedWidget() {
    return TweenAnimationBuilder<double>(
Kate Lovett's avatar
Kate Lovett committed
552 553 554 555 556 557 558 559 560 561 562
      child: child,
      tween: Tween<double>(begin: 1, end: 2),
      duration: duration,
      onEnd: widget.callback,
      builder: (BuildContext context, double size, Widget child) {
        return Container(
          child: child,
          width: size,
          height: size,
        );
      },
563 564 565 566 567 568 569 570 571 572 573 574 575 576
    );
  }
}

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