Skip to content

Commit 32ea4b3

Browse files
authored
Add test for animated_fractionally_sized_box.0.dart API example. (#146721)
This PR contributes to flutter/flutter#130459 ### Description - Adds `examples/api/test/widgets/implicit_animations/animated_fractionally_sized_box.0_test.dart` test
1 parent a1cb040 commit 32ea4b3

File tree

3 files changed

+150
-8
lines changed

3 files changed

+150
-8
lines changed

dev/bots/check_code_samples.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,6 @@ final Set<String> _knownMissingTests = <String>{
466466
'examples/api/test/widgets/focus_scope/focus.0_test.dart',
467467
'examples/api/test/widgets/focus_scope/focus.1_test.dart',
468468
'examples/api/test/widgets/focus_scope/focus_scope.0_test.dart',
469-
'examples/api/test/widgets/implicit_animations/animated_fractionally_sized_box.0_test.dart',
470469
'examples/api/test/widgets/scroll_view/custom_scroll_view.1_test.dart',
471470
'examples/api/test/widgets/inherited_notifier/inherited_notifier.0_test.dart',
472471
'examples/api/test/animation/curves/curve2_d.0_test.dart',

examples/api/lib/widgets/implicit_animations/animated_fractionally_sized_box.0.dart

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,43 @@ void main() => runApp(const AnimatedFractionallySizedBoxExampleApp());
1111
class AnimatedFractionallySizedBoxExampleApp extends StatelessWidget {
1212
const AnimatedFractionallySizedBoxExampleApp({super.key});
1313

14+
static const Duration duration = Duration(seconds: 1);
15+
static const Curve curve = Curves.fastOutSlowIn;
16+
1417
@override
1518
Widget build(BuildContext context) {
1619
return MaterialApp(
1720
home: Scaffold(
18-
appBar: AppBar(title: const Text('AnimatedFractionallySizedBox Sample')),
19-
body: const AnimatedFractionallySizedBoxExample(),
21+
appBar: AppBar(
22+
title: const Text('AnimatedFractionallySizedBox Sample'),
23+
),
24+
body: const AnimatedFractionallySizedBoxExample(
25+
duration: duration,
26+
curve: curve,
27+
),
2028
),
2129
);
2230
}
2331
}
2432

2533
class AnimatedFractionallySizedBoxExample extends StatefulWidget {
26-
const AnimatedFractionallySizedBoxExample({super.key});
34+
const AnimatedFractionallySizedBoxExample({
35+
required this.duration,
36+
required this.curve,
37+
super.key,
38+
});
39+
40+
final Duration duration;
41+
42+
final Curve curve;
2743

2844
@override
29-
State<AnimatedFractionallySizedBoxExample> createState() => _AnimatedFractionallySizedBoxExampleState();
45+
State<AnimatedFractionallySizedBoxExample> createState() =>
46+
_AnimatedFractionallySizedBoxExampleState();
3047
}
3148

32-
class _AnimatedFractionallySizedBoxExampleState extends State<AnimatedFractionallySizedBoxExample> {
49+
class _AnimatedFractionallySizedBoxExampleState
50+
extends State<AnimatedFractionallySizedBoxExample> {
3351
bool selected = false;
3452

3553
@override
@@ -50,8 +68,8 @@ class _AnimatedFractionallySizedBoxExampleState extends State<AnimatedFractional
5068
widthFactor: selected ? 0.25 : 0.75,
5169
heightFactor: selected ? 0.75 : 0.25,
5270
alignment: selected ? Alignment.topLeft : Alignment.bottomRight,
53-
duration: const Duration(seconds: 1),
54-
curve: Curves.fastOutSlowIn,
71+
duration: widget.duration,
72+
curve: widget.curve,
5573
child: const ColoredBox(
5674
color: Colors.blue,
5775
child: FlutterLogo(size: 75),
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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/implicit_animations/animated_fractionally_sized_box.0.dart'
9+
as example;
10+
import 'package:flutter_test/flutter_test.dart';
11+
12+
void main() {
13+
testWidgets(
14+
'AnimatedFractionallySizedBox animates on tap',
15+
(WidgetTester tester) async {
16+
await tester.pumpWidget(
17+
const example.AnimatedFractionallySizedBoxExampleApp(),
18+
);
19+
20+
final Finder fractionallySizedBoxFinder = find.descendant(
21+
of: find.byType(AnimatedFractionallySizedBox),
22+
matching: find.byType(FractionallySizedBox),
23+
);
24+
25+
const double beginWidthFactor = 0.75;
26+
const double endWidthFactor = 0.25;
27+
const double beginHeightFactor = 0.25;
28+
const double endHeightFactor = 0.75;
29+
const Alignment beginAlignment = Alignment.bottomRight;
30+
const Alignment endAlignment = Alignment.topLeft;
31+
32+
FractionallySizedBox fractionallySizedBox = tester.widget(
33+
fractionallySizedBoxFinder,
34+
);
35+
expect(fractionallySizedBox.widthFactor, beginWidthFactor);
36+
expect(fractionallySizedBox.heightFactor, beginHeightFactor);
37+
expect(fractionallySizedBox.alignment, beginAlignment);
38+
39+
// Tap on the AnimatedFractionallySizedBoxExample to start the forward
40+
// animation.
41+
await tester.tap(
42+
find.byType(example.AnimatedFractionallySizedBoxExample),
43+
);
44+
await tester.pump();
45+
46+
fractionallySizedBox = tester.widget(fractionallySizedBoxFinder);
47+
expect(fractionallySizedBox.widthFactor, beginWidthFactor);
48+
expect(fractionallySizedBox.heightFactor, beginHeightFactor);
49+
expect(fractionallySizedBox.alignment, beginAlignment);
50+
51+
// Advance animation to the middle.
52+
await tester.pump(
53+
example.AnimatedFractionallySizedBoxExampleApp.duration ~/ 2,
54+
);
55+
56+
final double t =
57+
example.AnimatedFractionallySizedBoxExampleApp.curve.transform(0.5);
58+
59+
fractionallySizedBox = tester.widget(fractionallySizedBoxFinder);
60+
expect(
61+
fractionallySizedBox.widthFactor,
62+
lerpDouble(beginWidthFactor, endWidthFactor, t),
63+
);
64+
expect(
65+
fractionallySizedBox.heightFactor,
66+
lerpDouble(beginHeightFactor, endHeightFactor, t),
67+
);
68+
expect(
69+
fractionallySizedBox.alignment,
70+
Alignment.lerp(beginAlignment, endAlignment, t),
71+
);
72+
73+
// Advance animation to the end.
74+
await tester.pump(
75+
example.AnimatedFractionallySizedBoxExampleApp.duration ~/ 2,
76+
);
77+
78+
fractionallySizedBox = tester.widget(fractionallySizedBoxFinder);
79+
expect(fractionallySizedBox.widthFactor, endWidthFactor);
80+
expect(fractionallySizedBox.heightFactor, endHeightFactor);
81+
expect(fractionallySizedBox.alignment, endAlignment);
82+
83+
// Tap on the AnimatedFractionallySizedBoxExample again to start the
84+
// reverse animation.
85+
await tester.tap(
86+
find.byType(example.AnimatedFractionallySizedBoxExample),
87+
);
88+
await tester.pump();
89+
90+
fractionallySizedBox = tester.widget(fractionallySizedBoxFinder);
91+
expect(fractionallySizedBox.widthFactor, endWidthFactor);
92+
expect(fractionallySizedBox.heightFactor, endHeightFactor);
93+
expect(fractionallySizedBox.alignment, endAlignment);
94+
95+
// Advance animation to the middle.
96+
await tester.pump(
97+
example.AnimatedFractionallySizedBoxExampleApp.duration ~/ 2,
98+
);
99+
100+
fractionallySizedBox = tester.widget(fractionallySizedBoxFinder);
101+
expect(
102+
fractionallySizedBox.widthFactor,
103+
lerpDouble(endWidthFactor, beginWidthFactor, t),
104+
);
105+
expect(
106+
fractionallySizedBox.heightFactor,
107+
lerpDouble(endHeightFactor, beginHeightFactor, t),
108+
);
109+
expect(
110+
fractionallySizedBox.alignment,
111+
Alignment.lerp(endAlignment, beginAlignment, t),
112+
);
113+
114+
// Advance animation to the end.
115+
await tester.pump(
116+
example.AnimatedFractionallySizedBoxExampleApp.duration ~/ 2,
117+
);
118+
119+
fractionallySizedBox = tester.widget(fractionallySizedBoxFinder);
120+
expect(fractionallySizedBox.widthFactor, beginWidthFactor);
121+
expect(fractionallySizedBox.heightFactor, beginHeightFactor);
122+
expect(fractionallySizedBox.alignment, beginAlignment);
123+
},
124+
);
125+
}

0 commit comments

Comments
 (0)