Skip to content

Commit bfbbe1f

Browse files
committed
Add verifyPoppedHeroCurve function to enhance Hero widget tests
- Introduced a new test utility function, `verifyPoppedHeroCurve`, to validate the animation behavior of the Hero widget with customizable curves. - The function tests the size transitions of the Hero widget during push and pop operations, ensuring that the specified curves are applied correctly throughout the animation. - Removed the previous implementation of `verifyPoppedHeroCurve` to streamline the test code.
1 parent 6377019 commit bfbbe1f

1 file changed

Lines changed: 109 additions & 109 deletions

File tree

‎packages/flutter/test/widgets/heroes_test.dart‎

Lines changed: 109 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,115 @@ Future<ui.Image> createTestImage() {
2424
return picture.toImage(300, 300);
2525
}
2626

27+
Future<void> verifyPoppedHeroCurve({
28+
required WidgetTester tester,
29+
required Curve curve,
30+
required Curve? reverseCurve,
31+
}) async {
32+
final Key container1 = UniqueKey();
33+
final Key container2 = UniqueKey();
34+
final navigator = GlobalKey<NavigatorState>();
35+
final observer = TransitionDurationObserver();
36+
37+
await tester.pumpWidget(
38+
MaterialApp(
39+
key: UniqueKey(),
40+
navigatorKey: navigator,
41+
navigatorObservers: <NavigatorObserver>[observer],
42+
home: Scaffold(
43+
body: Center(
44+
child: Hero(
45+
tag: 'test',
46+
createRectTween: (Rect? begin, Rect? end) {
47+
return RectTween(begin: begin, end: end);
48+
},
49+
curve: curve,
50+
reverseCurve: reverseCurve?.flipped,
51+
child: SizedBox(key: container1, height: 100, width: 100),
52+
),
53+
),
54+
),
55+
),
56+
);
57+
final Size originalSize = tester.getSize(find.byKey(container1));
58+
expect(originalSize, const Size(100, 100));
59+
60+
navigator.currentState!.push(
61+
MaterialPageRoute<void>(
62+
builder: (BuildContext context) {
63+
return Scaffold(
64+
body: Center(
65+
child: Hero(
66+
tag: 'test',
67+
createRectTween: (Rect? begin, Rect? end) {
68+
return RectTween(begin: begin, end: end);
69+
},
70+
curve: curve,
71+
reverseCurve: reverseCurve?.flipped,
72+
child: SizedBox(key: container2, height: 200, width: 200),
73+
),
74+
),
75+
);
76+
},
77+
),
78+
);
79+
await tester.pumpAndSettle();
80+
final Size newSize = tester.getSize(find.byKey(container2));
81+
expect(newSize, const Size(200, 200));
82+
83+
navigator.currentState!.pop();
84+
await tester.pump();
85+
86+
final Duration duration = observer.transitionDuration;
87+
final Curve expectedCurve = reverseCurve ?? curve;
88+
final expectedCurveName = expectedCurve.toString();
89+
const epsilon = 0.001;
90+
final Animatable<Size?> tween = SizeTween(
91+
begin: newSize,
92+
end: originalSize,
93+
).chain(CurveTween(curve: expectedCurve));
94+
95+
// Jump 25% into the transition.
96+
await tester.pump(duration ~/ 4);
97+
Size heroSize = tester.getSize(find.byKey(container1));
98+
Size expectedSize = tween.transform(0.25)!;
99+
expect(
100+
heroSize,
101+
within<Size>(distance: epsilon, from: expectedSize),
102+
reason: 'curve=$expectedCurveName, t=0.25, duration=$duration',
103+
);
104+
105+
// Jump to 50% into the transition.
106+
await tester.pump(duration ~/ 4);
107+
heroSize = tester.getSize(find.byKey(container1));
108+
expectedSize = tween.transform(0.50)!;
109+
expect(
110+
heroSize,
111+
within<Size>(distance: epsilon, from: expectedSize),
112+
reason: 'curve=$expectedCurveName, t=0.50, duration=$duration',
113+
);
114+
115+
// Jump to 75% into the transition.
116+
await tester.pump(duration ~/ 4);
117+
heroSize = tester.getSize(find.byKey(container1));
118+
expectedSize = tween.transform(0.75)!;
119+
expect(
120+
heroSize,
121+
within<Size>(distance: epsilon, from: expectedSize),
122+
reason: 'curve=$expectedCurveName, t=0.75, duration=$duration',
123+
);
124+
125+
// Jump to 100% into the transition.
126+
await tester.pump(duration ~/ 4);
127+
heroSize = tester.getSize(find.byKey(container1));
128+
expectedSize = tween.transform(1.0)!;
129+
expect(
130+
heroSize,
131+
within<Size>(distance: epsilon, from: expectedSize),
132+
reason: 'curve=$expectedCurveName, t=1.00, duration=$duration',
133+
);
134+
}
135+
27136
Key firstKey = const Key('first');
28137
Key secondKey = const Key('second');
29138
Key thirdKey = const Key('third');
@@ -3365,115 +3474,6 @@ Future<void> main() async {
33653474
expect(heroSize, tween.transform(1.0));
33663475
});
33673476

3368-
Future<void> verifyPoppedHeroCurve({
3369-
required WidgetTester tester,
3370-
required Curve curve,
3371-
required Curve? reverseCurve,
3372-
}) async {
3373-
final Key container1 = UniqueKey();
3374-
final Key container2 = UniqueKey();
3375-
final navigator = GlobalKey<NavigatorState>();
3376-
final observer = TransitionDurationObserver();
3377-
3378-
await tester.pumpWidget(
3379-
MaterialApp(
3380-
key: UniqueKey(),
3381-
navigatorKey: navigator,
3382-
navigatorObservers: <NavigatorObserver>[observer],
3383-
home: Scaffold(
3384-
body: Center(
3385-
child: Hero(
3386-
tag: 'test',
3387-
createRectTween: (Rect? begin, Rect? end) {
3388-
return RectTween(begin: begin, end: end);
3389-
},
3390-
curve: curve,
3391-
reverseCurve: reverseCurve?.flipped,
3392-
child: SizedBox(key: container1, height: 100, width: 100),
3393-
),
3394-
),
3395-
),
3396-
),
3397-
);
3398-
final Size originalSize = tester.getSize(find.byKey(container1));
3399-
expect(originalSize, const Size(100, 100));
3400-
3401-
navigator.currentState!.push(
3402-
MaterialPageRoute<void>(
3403-
builder: (BuildContext context) {
3404-
return Scaffold(
3405-
body: Center(
3406-
child: Hero(
3407-
tag: 'test',
3408-
createRectTween: (Rect? begin, Rect? end) {
3409-
return RectTween(begin: begin, end: end);
3410-
},
3411-
curve: curve,
3412-
reverseCurve: reverseCurve?.flipped,
3413-
child: SizedBox(key: container2, height: 200, width: 200),
3414-
),
3415-
),
3416-
);
3417-
},
3418-
),
3419-
);
3420-
await tester.pumpAndSettle();
3421-
final Size newSize = tester.getSize(find.byKey(container2));
3422-
expect(newSize, const Size(200, 200));
3423-
3424-
navigator.currentState!.pop();
3425-
await tester.pump();
3426-
3427-
final Duration duration = observer.transitionDuration;
3428-
final Curve expectedCurve = reverseCurve ?? curve;
3429-
final expectedCurveName = expectedCurve.toString();
3430-
const epsilon = 0.001;
3431-
final Animatable<Size?> tween = SizeTween(
3432-
begin: newSize,
3433-
end: originalSize,
3434-
).chain(CurveTween(curve: expectedCurve));
3435-
3436-
// Jump 25% into the transition.
3437-
await tester.pump(duration ~/ 4);
3438-
Size heroSize = tester.getSize(find.byKey(container1));
3439-
Size expectedSize = tween.transform(0.25)!;
3440-
expect(
3441-
heroSize,
3442-
within<Size>(distance: epsilon, from: expectedSize),
3443-
reason: 'curve=$expectedCurveName, t=0.25, duration=$duration',
3444-
);
3445-
3446-
// Jump to 50% into the transition.
3447-
await tester.pump(duration ~/ 4);
3448-
heroSize = tester.getSize(find.byKey(container1));
3449-
expectedSize = tween.transform(0.50)!;
3450-
expect(
3451-
heroSize,
3452-
within<Size>(distance: epsilon, from: expectedSize),
3453-
reason: 'curve=$expectedCurveName, t=0.50, duration=$duration',
3454-
);
3455-
3456-
// Jump to 75% into the transition.
3457-
await tester.pump(duration ~/ 4);
3458-
heroSize = tester.getSize(find.byKey(container1));
3459-
expectedSize = tween.transform(0.75)!;
3460-
expect(
3461-
heroSize,
3462-
within<Size>(distance: epsilon, from: expectedSize),
3463-
reason: 'curve=$expectedCurveName, t=0.75, duration=$duration',
3464-
);
3465-
3466-
// Jump to 100% into the transition.
3467-
await tester.pump(duration ~/ 4);
3468-
heroSize = tester.getSize(find.byKey(container1));
3469-
expectedSize = tween.transform(1.0)!;
3470-
expect(
3471-
heroSize,
3472-
within<Size>(distance: epsilon, from: expectedSize),
3473-
reason: 'curve=$expectedCurveName, t=1.00, duration=$duration',
3474-
);
3475-
}
3476-
34773477
testWidgets('popped hero curve is customizable', (WidgetTester tester) async {
34783478
final testReverseCurvesList = <Curve>[
34793479
Curves.linear,

0 commit comments

Comments
 (0)