Skip to content

Commit 22e0502

Browse files
committed
home test: Make tapOpenMenu stronger; rename with "AndAwait" to clarify
This is robust to changes in the entrance-animation duration, and it checks the state more thoroughly.
1 parent 177ad1c commit 22e0502

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

test/test_navigation.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ import 'package:flutter/widgets.dart';
66

77
/// A trivial observer for testing the navigator.
88
class TestNavigatorObserver extends NavigatorObserver {
9+
void Function(Route<dynamic> topRoute, Route<dynamic>? previousTopRoute)? onChangedTop;
910
void Function(Route<dynamic> route, Route<dynamic>? previousRoute)? onPushed;
1011
void Function(Route<dynamic> route, Route<dynamic>? previousRoute)? onPopped;
1112
void Function(Route<dynamic> route, Route<dynamic>? previousRoute)? onRemoved;
1213
void Function(Route<dynamic>? route, Route<dynamic>? previousRoute)? onReplaced;
1314
void Function(Route<dynamic> route, Route<dynamic>? previousRoute)? onStartUserGesture;
1415
void Function()? onStopUserGesture;
1516

17+
@override
18+
void didChangeTop(Route<dynamic> topRoute, Route<dynamic>? previousTopRoute) {
19+
onChangedTop?.call(topRoute, previousTopRoute);
20+
}
21+
1622
@override
1723
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
1824
onPushed?.call(route, previousRoute);

test/widgets/home_test.dart

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,22 @@ void main () {
3333

3434
late PerAccountStore store;
3535
late FakeApiConnection connection;
36+
37+
late Route<dynamic>? topRoute;
38+
late Route<dynamic>? previousTopRoute;
3639
late List<Route<dynamic>> pushedRoutes;
3740

3841
final testNavObserver = TestNavigatorObserver()
39-
..onPushed = (route, prevRoute) => pushedRoutes.add(route);
42+
..onChangedTop = ((current, previous) {
43+
topRoute = current;
44+
previousTopRoute = previous;
45+
})
46+
..onPushed = ((route, prevRoute) => pushedRoutes.add(route));
4047

4148
Future<void> prepare(WidgetTester tester) async {
4249
addTearDown(testBinding.reset);
50+
topRoute = null;
51+
previousTopRoute = null;
4352
pushedRoutes = [];
4453
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
4554
store = await testBinding.globalStore.perAccount(eg.selfAccount.id);
@@ -142,10 +151,20 @@ void main () {
142151
final channelsMenuIconFinder = find.byIcon(ZulipIcons.hash_italic);
143152
final combinedFeedMenuIconFinder = find.byIcon(ZulipIcons.message_feed);
144153

145-
Future<void> tapOpenMenu(WidgetTester tester) async {
154+
Future<void> tapOpenMenuAndAwait(WidgetTester tester) async {
155+
final topRouteBeforePress = topRoute;
146156
await tester.tap(find.byIcon(ZulipIcons.menu));
147-
await tester.pump(Duration.zero); // tap the button
148-
await tester.pump(const Duration(milliseconds: 250)); // wait for animation
157+
await tester.pump();
158+
final topRouteAfterPress = topRoute;
159+
check(topRouteAfterPress).isA<ModalBottomSheetRoute<void>>();
160+
await tester.pump((topRouteAfterPress as ModalBottomSheetRoute<void>).transitionDuration);
161+
162+
// This was the only change during the interaction.
163+
check(topRouteBeforePress).identicalTo(previousTopRoute);
164+
165+
// We got to the sheet by pushing, not popping or something else.
166+
check(pushedRoutes.last).identicalTo(topRouteAfterPress);
167+
149168
check(find.byType(BottomSheet)).findsOne();
150169
}
151170

@@ -168,7 +187,7 @@ void main () {
168187
testWidgets('navigation states reflect on navigation bar menu buttons', (tester) async {
169188
await prepare(tester);
170189

171-
await tapOpenMenu(tester);
190+
await tapOpenMenuAndAwait(tester);
172191
checkIconSelected(tester, inboxMenuIconFinder);
173192
checkIconNotSelected(tester, channelsMenuIconFinder);
174193
await tester.tap(find.text('Cancel'));
@@ -178,15 +197,15 @@ void main () {
178197
await tester.tap(find.byIcon(ZulipIcons.hash_italic));
179198
await tester.pump();
180199

181-
await tapOpenMenu(tester);
200+
await tapOpenMenuAndAwait(tester);
182201
checkIconNotSelected(tester, inboxMenuIconFinder);
183202
checkIconSelected(tester, channelsMenuIconFinder);
184203
});
185204

186205
testWidgets('navigation bar menu buttons control navigation states', (tester) async {
187206
await prepare(tester);
188207

189-
await tapOpenMenu(tester);
208+
await tapOpenMenuAndAwait(tester);
190209
checkIconSelected(tester, inboxMenuIconFinder);
191210
checkIconNotSelected(tester, channelsMenuIconFinder);
192211
check(find.byType(InboxPageBody)).findsOne();
@@ -201,14 +220,14 @@ void main () {
201220
check(find.byType(InboxPageBody)).findsNothing();
202221
check(find.byType(SubscriptionListPageBody)).findsOne();
203222

204-
await tapOpenMenu(tester);
223+
await tapOpenMenuAndAwait(tester);
205224
checkIconNotSelected(tester, inboxMenuIconFinder);
206225
checkIconSelected(tester, channelsMenuIconFinder);
207226
});
208227

209228
testWidgets('navigation bar menu buttons dismiss the menu', (tester) async {
210229
await prepare(tester);
211-
await tapOpenMenu(tester);
230+
await tapOpenMenuAndAwait(tester);
212231

213232
await tester.tap(find.descendant(
214233
of: find.byType(BottomSheet),
@@ -220,7 +239,7 @@ void main () {
220239

221240
testWidgets('cancel button dismisses the menu', (tester) async {
222241
await prepare(tester);
223-
await tapOpenMenu(tester);
242+
await tapOpenMenuAndAwait(tester);
224243

225244
await tester.tap(find.text('Cancel'));
226245
await tester.pump(Duration.zero); // tap the button
@@ -230,14 +249,17 @@ void main () {
230249

231250
testWidgets('menu buttons dismiss the menu', (tester) async {
232251
addTearDown(testBinding.reset);
252+
topRoute = null;
253+
previousTopRoute = null;
254+
pushedRoutes = [];
233255
await testBinding.globalStore.add(eg.selfAccount, eg.initialSnapshot());
234256

235-
await tester.pumpWidget(const ZulipApp());
257+
await tester.pumpWidget(ZulipApp(navigatorObservers: [testNavObserver]));
236258
final store = await testBinding.globalStore.perAccount(eg.selfAccount.id);
237259
final connection = store.connection as FakeApiConnection;
238260
await tester.pump();
239261

240-
await tapOpenMenu(tester);
262+
await tapOpenMenuAndAwait(tester);
241263

242264
connection.prepare(json: eg.newestGetMessagesResult(
243265
foundOldest: true, messages: [eg.streamMessage()]).toJson());
@@ -255,7 +277,7 @@ void main () {
255277

256278
testWidgets('_MyProfileButton', (tester) async {
257279
await prepare(tester);
258-
await tapOpenMenu(tester);
280+
await tapOpenMenuAndAwait(tester);
259281

260282
await tester.tap(find.text('My profile'));
261283
await tester.pump(Duration.zero); // tap the button
@@ -266,7 +288,7 @@ void main () {
266288

267289
testWidgets('_AboutZulipButton', (tester) async {
268290
await prepare(tester);
269-
await tapOpenMenu(tester);
291+
await tapOpenMenuAndAwait(tester);
270292

271293
await tester.tap(find.byIcon(ZulipIcons.info));
272294
await tester.pump(Duration.zero); // tap the button

0 commit comments

Comments
 (0)