Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add test helpers for system navigation #124

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions lib/flow_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,3 @@ abstract class _SystemNavigationObserver implements WidgetsBinding {
}
}
}

/// Visible for testing system navigation.
abstract class TestSystemNavigationObserver {
/// Visible for testing system pop navigation.
@visibleForTesting
static Future<dynamic> handleSystemNavigation(MethodCall methodCall) {
return _SystemNavigationObserver._handleSystemNavigation(methodCall);
}
}
70 changes: 38 additions & 32 deletions test/flow_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,10 @@ void main() {
expect(numBuilds, 2);
expect(find.byKey(buttonKey), findsNothing);
expect(find.byKey(scaffoldKey), findsOneWidget);
await TestSystemNavigationObserver.handleSystemNavigation(
const MethodCall('pushRoute'),
);
await TestSystemNavigationObserver.handleSystemNavigation(
const MethodCall('popRoute'),
);

await tester.sendPlatformPush();
await tester.sendPlatformPop();

await tester.pumpAndSettle();

expect(numBuilds, 2);
Expand Down Expand Up @@ -571,9 +569,7 @@ void main() {
expect(find.byKey(buttonKey), findsOneWidget);
expect(find.byKey(scaffoldKey), findsNothing);

await TestSystemNavigationObserver.handleSystemNavigation(
const MethodCall('popRoute'),
);
await tester.sendPlatformPop();
await tester.pumpAndSettle();

expect(systemPopCallCount, equals(1));
Expand Down Expand Up @@ -634,9 +630,7 @@ void main() {
expect(find.byKey(buttonKey), findsNothing);
expect(find.byKey(scaffoldKey), findsOneWidget);

await TestSystemNavigationObserver.handleSystemNavigation(
const MethodCall('popRoute'),
);
await tester.sendPlatformPop();
await tester.pumpAndSettle();

expect(systemPopCallCount, equals(0));
Expand Down Expand Up @@ -699,9 +693,7 @@ void main() {
expect(find.byKey(buttonKey), findsNothing);
expect(find.byKey(scaffoldKey), findsOneWidget);

await TestSystemNavigationObserver.handleSystemNavigation(
const MethodCall('popRoute'),
);
await tester.sendPlatformPop();
await tester.pumpAndSettle();

expect(systemPopCallCount, equals(0));
Expand Down Expand Up @@ -922,12 +914,7 @@ void main() {
);
expect(numBuilds, 1);

await TestSystemNavigationObserver.handleSystemNavigation(
const MethodCall(
'pushRoute',
path,
),
);
await tester.sendPlatformPush(path);
await tester.pumpAndSettle();
expect(observer.lastRoute, path);
expect(observer.pushCount, 1);
Expand Down Expand Up @@ -959,9 +946,7 @@ void main() {
);
expect(numBuilds, 1);

await TestSystemNavigationObserver.handleSystemNavigation(
const MethodCall('pushRoute'),
);
await tester.sendPlatformPush();
await tester.pumpAndSettle();
expect(observer.lastRoute, isNull);
expect(observer.pushCount, 0);
Expand Down Expand Up @@ -992,8 +977,12 @@ void main() {
);
expect(numBuilds, 1);

await TestSystemNavigationObserver.handleSystemNavigation(
const MethodCall('randomMethod'),
await tester.binding.defaultBinaryMessenger.handlePlatformMessage(
'flutter/navigation',
const JSONMethodCodec().encodeMethodCall(
const MethodCall('randomMethod'),
),
(_) {},
);
await tester.pumpAndSettle();
expect(observer.lastRoute, isNull);
Expand Down Expand Up @@ -1404,9 +1393,7 @@ void main() {
child: TextButton(
key: targetKey,
onPressed: () {
TestSystemNavigationObserver.handleSystemNavigation(
const MethodCall('popRoute'),
);
tester.sendPlatformPop();
},
child: const SizedBox(),
),
Expand Down Expand Up @@ -1459,9 +1446,7 @@ void main() {
child: TextButton(
key: targetKey,
onPressed: () {
TestSystemNavigationObserver.handleSystemNavigation(
const MethodCall('popRoute'),
);
tester.sendPlatformPop();
},
child: const SizedBox(),
),
Expand Down Expand Up @@ -1563,3 +1548,24 @@ class _TestPushWidgetsBindingObserver with WidgetsBindingObserver {
return true;
}
}

extension WidgetTesterX on WidgetTester {
Future<void> sendPlatformPop() async {
final message = const JSONMethodCodec().encodeMethodCall(
const MethodCall('popRoute'),
);
await _sendSystemNavigationMessage(message);
}

Future<void> sendPlatformPush([String? route]) async {
final message = const JSONMethodCodec().encodeMethodCall(
MethodCall('pushRoute', route),
);
await _sendSystemNavigationMessage(message);
}

Future<void> _sendSystemNavigationMessage(ByteData message) async {
await binding.defaultBinaryMessenger
.handlePlatformMessage('flutter/navigation', message, (_) {});
}
}