implicit_animations_test.dart 15.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 8
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';

9 10 11 12 13 14 15 16 17 18 19
class MockOnEndFunction implements Function {
  int called = 0;

  void call() {
    called++;
  }
}

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

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

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

28
  testWidgets('BoxConstraintsTween control test', (WidgetTester tester) async {
29 30
    final BoxConstraintsTween tween = BoxConstraintsTween(
      begin: BoxConstraints.tight(const Size(20.0, 50.0)),
31
      end: BoxConstraints.tight(const Size(10.0, 30.0)),
32
    );
33
    final BoxConstraints result = tween.lerp(0.25);
34 35 36 37 38 39 40
    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 {
41
    final DecorationTween tween = DecorationTween(
42
      begin: const BoxDecoration(color: Color(0xFF00FF00)),
43
      end: const BoxDecoration(color: Color(0xFFFFFF00)),
44
    );
45
    final BoxDecoration result = tween.lerp(0.25) as BoxDecoration;
46
    expect(result.color, const Color(0xFF3FFF00));
47 48 49
  });

  testWidgets('EdgeInsetsTween control test', (WidgetTester tester) async {
50
    final EdgeInsetsTween tween = EdgeInsetsTween(
51
      begin: const EdgeInsets.symmetric(vertical: 50.0),
52
      end: const EdgeInsets.only(top: 10.0, bottom: 30.0),
53
    );
54
    final EdgeInsets result = tween.lerp(0.25);
55 56 57 58 59 60 61
    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 {
62 63
    final Matrix4Tween tween = Matrix4Tween(
      begin: Matrix4.translationValues(10.0, 20.0, 30.0),
64
      end: Matrix4.translationValues(14.0, 24.0, 34.0),
65
    );
66
    final Matrix4 result = tween.lerp(0.25);
67
    expect(result, equals(Matrix4.translationValues(11.0, 21.0, 31.0)));
68
  });
69 70 71

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

    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
93 94 95 96 97
      child: TestAnimatedWidget(
        callback: mockOnEndFunction,
        switchKey: switchKey,
        state: _TestAnimatedPaddingWidgetState(),
      )
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    ));

    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
114 115 116 117 118
      child: TestAnimatedWidget(
        callback: mockOnEndFunction,
        switchKey: switchKey,
        state: _TestAnimatedAlignWidgetState(),
      )
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    ));

    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
135 136 137 138 139
      child: TestAnimatedWidget(
        callback: mockOnEndFunction,
        switchKey: switchKey,
        state: _TestAnimatedPositionedWidgetState(),
      )
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    ));

    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
156 157 158 159 160
      child: TestAnimatedWidget(
        callback: mockOnEndFunction,
        switchKey: switchKey,
        state: _TestAnimatedPositionedDirectionalWidgetState(),
      )
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    ));

    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
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 226 227 228
      child: TestAnimatedWidget(
        callback: mockOnEndFunction,
        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(
      callback: mockOnEndFunction,
      switchKey: switchKey,
      state: _TestSliverAnimatedOpacityWidgetState(),
229 230 231 232 233 234 235 236 237 238 239 240 241 242
    ));

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

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

    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
294 295 296 297 298
      child: TestAnimatedWidget(
        callback: mockOnEndFunction,
        switchKey: switchKey,
        state: _TestAnimatedPhysicalModelWidgetState(),
      )
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    ));

    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
315 316 317 318 319
      child: TestAnimatedWidget(
        callback: mockOnEndFunction,
        switchKey: switchKey,
        state: _TestTweenAnimationBuilderWidgetState(),
      )
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    ));

    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
336 337 338 339 340
      child: TestAnimatedWidget(
        callback: mockOnEndFunction,
        switchKey: switchKey,
        state: _TestAnimatedThemeWidgetState(),
      )
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    ));

    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 {
366 367 368 369 370 371
  const TestAnimatedWidget({
    Key key,
    this.callback,
    this.switchKey,
    this.state,
  }) : super(key: key);
372 373 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
  @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
478 479 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
      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,
              ),
            ),
          ],
        ),
      ),
514 515 516 517 518 519 520 521
    );
  }
}

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

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
550 551 552 553 554 555 556 557 558 559 560
      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,
        );
      },
561 562 563 564 565 566 567 568 569 570 571 572 573 574
    );
  }
}

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