From 18965437cda755b970b28c947dd052f2f3f0daf6 Mon Sep 17 00:00:00 2001
From: Peter Trost
Date: Wed, 27 Nov 2024 21:02:20 +0100
Subject: [PATCH] test: add test helpers for system navigation (#124)
---
lib/flow_builder.dart | 9 -----
test/flow_builder_test.dart | 70 ++++++++++++++++++++-----------------
2 files changed, 38 insertions(+), 41 deletions(-)
diff --git a/lib/flow_builder.dart b/lib/flow_builder.dart
index c79a6ec..7fceda7 100644
--- a/lib/flow_builder.dart
+++ b/lib/flow_builder.dart
@@ -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 handleSystemNavigation(MethodCall methodCall) {
- return _SystemNavigationObserver._handleSystemNavigation(methodCall);
- }
-}
diff --git a/test/flow_builder_test.dart b/test/flow_builder_test.dart
index a63596a..230f626 100644
--- a/test/flow_builder_test.dart
+++ b/test/flow_builder_test.dart
@@ -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);
@@ -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));
@@ -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));
@@ -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));
@@ -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);
@@ -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);
@@ -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);
@@ -1404,9 +1393,7 @@ void main() {
child: TextButton(
key: targetKey,
onPressed: () {
- TestSystemNavigationObserver.handleSystemNavigation(
- const MethodCall('popRoute'),
- );
+ tester.sendPlatformPop();
},
child: const SizedBox(),
),
@@ -1459,9 +1446,7 @@ void main() {
child: TextButton(
key: targetKey,
onPressed: () {
- TestSystemNavigationObserver.handleSystemNavigation(
- const MethodCall('popRoute'),
- );
+ tester.sendPlatformPop();
},
child: const SizedBox(),
),
@@ -1563,3 +1548,24 @@ class _TestPushWidgetsBindingObserver with WidgetsBindingObserver {
return true;
}
}
+
+extension WidgetTesterX on WidgetTester {
+ Future sendPlatformPop() async {
+ final message = const JSONMethodCodec().encodeMethodCall(
+ const MethodCall('popRoute'),
+ );
+ await _sendSystemNavigationMessage(message);
+ }
+
+ Future sendPlatformPush([String? route]) async {
+ final message = const JSONMethodCodec().encodeMethodCall(
+ MethodCall('pushRoute', route),
+ );
+ await _sendSystemNavigationMessage(message);
+ }
+
+ Future _sendSystemNavigationMessage(ByteData message) async {
+ await binding.defaultBinaryMessenger
+ .handlePlatformMessage('flutter/navigation', message, (_) {});
+ }
+}