Skip to content

Commit

Permalink
test: add test helpers for system navigation (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peetee06 authored Nov 27, 2024
1 parent 838f92c commit 1896543
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 41 deletions.
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, (_) {});
}
}

0 comments on commit 1896543

Please sign in to comment.