Skip to content

Commit 488fb09

Browse files
authored
Add tests for tween_animation_builder.0.dart API example. (#148902)
This PR contributes to flutter/flutter#130459 ### Description - Adds tests for `examples/api/lib/widgets/tween_animation_builder/tween_animation_builder.0.dart`
1 parent 5933e99 commit 488fb09

File tree

3 files changed

+170
-7
lines changed

3 files changed

+170
-7
lines changed

dev/bots/check_code_samples.dart

-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,6 @@ final Set<String> _knownMissingTests = <String>{
393393
'examples/api/test/widgets/notification_listener/notification.0_test.dart',
394394
'examples/api/test/widgets/overscroll_indicator/glowing_overscroll_indicator.1_test.dart',
395395
'examples/api/test/widgets/overscroll_indicator/glowing_overscroll_indicator.0_test.dart',
396-
'examples/api/test/widgets/tween_animation_builder/tween_animation_builder.0_test.dart',
397396
'examples/api/test/widgets/single_child_scroll_view/single_child_scroll_view.1_test.dart',
398397
'examples/api/test/widgets/single_child_scroll_view/single_child_scroll_view.0_test.dart',
399398
'examples/api/test/widgets/restoration/restoration_mixin.0_test.dart',

examples/api/lib/widgets/tween_animation_builder/tween_animation_builder.0.dart

+10-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class TweenAnimationBuilderExampleApp extends StatelessWidget {
1515
Widget build(BuildContext context) {
1616
return MaterialApp(
1717
home: Scaffold(
18-
appBar: AppBar(title: const Text('TweenAnimationBuilder Sample')),
18+
appBar: AppBar(
19+
title: const Text('TweenAnimationBuilder Sample'),
20+
),
1921
body: const Center(
2022
child: TweenAnimationBuilderExample(),
2123
),
@@ -28,16 +30,18 @@ class TweenAnimationBuilderExample extends StatefulWidget {
2830
const TweenAnimationBuilderExample({super.key});
2931

3032
@override
31-
State<TweenAnimationBuilderExample> createState() => _TweenAnimationBuilderExampleState();
33+
State<TweenAnimationBuilderExample> createState() =>
34+
_TweenAnimationBuilderExampleState();
3235
}
3336

34-
class _TweenAnimationBuilderExampleState extends State<TweenAnimationBuilderExample> {
35-
double targetValue = 24.0;
37+
class _TweenAnimationBuilderExampleState
38+
extends State<TweenAnimationBuilderExample> {
39+
double _targetValue = 24.0;
3640

3741
@override
3842
Widget build(BuildContext context) {
3943
return TweenAnimationBuilder<double>(
40-
tween: Tween<double>(begin: 0, end: targetValue),
44+
tween: Tween<double>(begin: 0, end: _targetValue),
4145
duration: const Duration(seconds: 1),
4246
builder: (BuildContext context, double size, Widget? child) {
4347
return IconButton(
@@ -46,7 +50,7 @@ class _TweenAnimationBuilderExampleState extends State<TweenAnimationBuilderExam
4650
icon: child!,
4751
onPressed: () {
4852
setState(() {
49-
targetValue = targetValue == 24.0 ? 48.0 : 24.0;
53+
_targetValue = _targetValue == 24.0 ? 48.0 : 24.0;
5054
});
5155
},
5256
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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

Comments
 (0)