|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:ui'; |
| 6 | + |
| 7 | +import 'package:flutter/material.dart'; |
| 8 | +import 'package:flutter_api_samples/widgets/tween_animation_builder/tween_animation_builder.0.dart' |
| 9 | + as example; |
| 10 | +import 'package:flutter_test/flutter_test.dart'; |
| 11 | + |
| 12 | +void main() { |
| 13 | + testWidgets('Animates icon size on first build', (WidgetTester tester) async { |
| 14 | + await tester.pumpWidget( |
| 15 | + const example.TweenAnimationBuilderExampleApp(), |
| 16 | + ); |
| 17 | + |
| 18 | + // The animation duration defined in the example app. |
| 19 | + const Duration animationDuration = Duration(seconds: 1); |
| 20 | + |
| 21 | + const double beginSize = 0.0; |
| 22 | + const double endSize = 24.0; |
| 23 | + |
| 24 | + final Finder iconButtonFinder = find.byType(IconButton); |
| 25 | + |
| 26 | + IconButton iconButton = tester.widget(iconButtonFinder); |
| 27 | + expect(iconButton.iconSize, equals(beginSize)); |
| 28 | + |
| 29 | + // Advance animation to the middle. |
| 30 | + await tester.pump(animationDuration ~/ 2); |
| 31 | + |
| 32 | + iconButton = tester.widget(iconButtonFinder); |
| 33 | + expect( |
| 34 | + iconButton.iconSize, |
| 35 | + equals(lerpDouble(beginSize, endSize, 0.5)), |
| 36 | + ); |
| 37 | + |
| 38 | + // Advance animation to the end. |
| 39 | + await tester.pump(animationDuration ~/ 2); |
| 40 | + |
| 41 | + iconButton = tester.widget(iconButtonFinder); |
| 42 | + expect(iconButton.iconSize, equals(endSize)); |
| 43 | + }); |
| 44 | + |
| 45 | + testWidgets('Animates icon size on IconButton tap', (WidgetTester tester) async { |
| 46 | + await tester.pumpWidget( |
| 47 | + const example.TweenAnimationBuilderExampleApp(), |
| 48 | + ); |
| 49 | + await tester.pumpAndSettle(); |
| 50 | + |
| 51 | + // The animation duration defined in the example app. |
| 52 | + const Duration animationDuration = Duration(seconds: 1); |
| 53 | + |
| 54 | + const double beginSize = 24.0; |
| 55 | + const double endSize = 48.0; |
| 56 | + |
| 57 | + final Finder iconButtonFinder = find.byType(IconButton); |
| 58 | + |
| 59 | + IconButton iconButton = tester.widget(iconButtonFinder); |
| 60 | + expect(iconButton.iconSize, equals(beginSize)); |
| 61 | + |
| 62 | + // Tap on the IconButton to start the forward animation. |
| 63 | + await tester.tap(iconButtonFinder); |
| 64 | + await tester.pump(); |
| 65 | + |
| 66 | + iconButton = tester.widget(iconButtonFinder); |
| 67 | + expect(iconButton.iconSize, equals(beginSize)); |
| 68 | + |
| 69 | + // Advance animation to the middle. |
| 70 | + await tester.pump(animationDuration ~/ 2); |
| 71 | + |
| 72 | + iconButton = tester.widget(iconButtonFinder); |
| 73 | + expect( |
| 74 | + iconButton.iconSize, |
| 75 | + equals(lerpDouble(beginSize, endSize, 0.5)), |
| 76 | + ); |
| 77 | + |
| 78 | + // Advance animation to the end. |
| 79 | + await tester.pump(animationDuration ~/ 2); |
| 80 | + |
| 81 | + iconButton = tester.widget(iconButtonFinder); |
| 82 | + expect(iconButton.iconSize, equals(endSize)); |
| 83 | + |
| 84 | + // Tap on the IconButton to start the reverse animation. |
| 85 | + await tester.tap(iconButtonFinder); |
| 86 | + await tester.pump(); |
| 87 | + |
| 88 | + iconButton = tester.widget(iconButtonFinder); |
| 89 | + expect(iconButton.iconSize, equals(endSize)); |
| 90 | + |
| 91 | + // Advance animation to the middle. |
| 92 | + await tester.pump(animationDuration ~/ 2); |
| 93 | + |
| 94 | + iconButton = tester.widget(iconButtonFinder); |
| 95 | + expect( |
| 96 | + iconButton.iconSize, |
| 97 | + equals(lerpDouble(endSize, beginSize, 0.5)), |
| 98 | + ); |
| 99 | + |
| 100 | + // Advance animation to the end. |
| 101 | + await tester.pump(animationDuration ~/ 2); |
| 102 | + |
| 103 | + iconButton = tester.widget(iconButtonFinder); |
| 104 | + expect(iconButton.iconSize, equals(beginSize)); |
| 105 | + }); |
| 106 | + |
| 107 | + testWidgets('Animation target can be updated during the animation', (WidgetTester tester) async { |
| 108 | + await tester.pumpWidget( |
| 109 | + const example.TweenAnimationBuilderExampleApp(), |
| 110 | + ); |
| 111 | + await tester.pumpAndSettle(); |
| 112 | + |
| 113 | + // The animation duration defined in the example app. |
| 114 | + const Duration animationDuration = Duration(seconds: 1); |
| 115 | + |
| 116 | + const double beginSize = 24.0; |
| 117 | + const double endSize = 48.0; |
| 118 | + final double middleSize = lerpDouble(beginSize, endSize, 0.5)!; |
| 119 | + |
| 120 | + final Finder iconButtonFinder = find.byType(IconButton); |
| 121 | + |
| 122 | + IconButton iconButton = tester.widget(iconButtonFinder); |
| 123 | + expect(iconButton.iconSize, equals(beginSize)); |
| 124 | + |
| 125 | + // Tap on the IconButton to start the forward animation. |
| 126 | + await tester.tap(iconButtonFinder); |
| 127 | + await tester.pump(); |
| 128 | + |
| 129 | + iconButton = tester.widget(iconButtonFinder); |
| 130 | + expect(iconButton.iconSize, equals(beginSize)); |
| 131 | + |
| 132 | + // Advance animation to the middle. |
| 133 | + await tester.pump(animationDuration ~/ 2); |
| 134 | + |
| 135 | + iconButton = tester.widget(iconButtonFinder); |
| 136 | + expect(iconButton.iconSize, equals(middleSize)); |
| 137 | + |
| 138 | + // Tap on the IconButton to start the backward animation. |
| 139 | + await tester.tap(iconButtonFinder); |
| 140 | + await tester.pump(); |
| 141 | + |
| 142 | + iconButton = tester.widget(iconButtonFinder); |
| 143 | + expect(iconButton.iconSize, equals(middleSize)); |
| 144 | + |
| 145 | + // Advance animation to the middle. |
| 146 | + await tester.pump(animationDuration ~/ 2); |
| 147 | + |
| 148 | + iconButton = tester.widget(iconButtonFinder); |
| 149 | + expect( |
| 150 | + iconButton.iconSize, |
| 151 | + equals(lerpDouble(middleSize, beginSize, 0.5)), |
| 152 | + ); |
| 153 | + |
| 154 | + // Advance animation to the end. |
| 155 | + await tester.pump(animationDuration ~/ 2); |
| 156 | + |
| 157 | + iconButton = tester.widget(iconButtonFinder); |
| 158 | + expect(iconButton.iconSize, equals(beginSize)); |
| 159 | + }); |
| 160 | +} |
0 commit comments