mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Update _handlePushRouteInformation to Future<bool> to indicate whether any of the observer has handled the route or not (#147901)
follow up on comments on https://github.com/flutter/engine/pull/52350
This commit is contained in:
parent
8b129b9b89
commit
6826fc08fa
@ -830,13 +830,14 @@ mixin WidgetsBinding on BindingBase, ServicesBinding, SchedulerBinding, GestureB
|
||||
/// {@endtemplate}
|
||||
@protected
|
||||
@visibleForTesting
|
||||
Future<void> handlePopRoute() async {
|
||||
Future<bool> handlePopRoute() async {
|
||||
for (final WidgetsBindingObserver observer in List<WidgetsBindingObserver>.of(_observers)) {
|
||||
if (await observer.didPopRoute()) {
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
SystemNavigator.pop();
|
||||
return false;
|
||||
}
|
||||
|
||||
// The observer that is currently handling an active predictive back gesture.
|
||||
@ -870,7 +871,8 @@ mixin WidgetsBinding on BindingBase, ServicesBinding, SchedulerBinding, GestureB
|
||||
// back gesture occurs but no predictive back route transition exists to
|
||||
// handle it. The back gesture should still cause normal pop even if it
|
||||
// doesn't cause a predictive transition.
|
||||
return handlePopRoute();
|
||||
await handlePopRoute();
|
||||
return;
|
||||
}
|
||||
_backGestureObserver?.handleCommitBackGesture();
|
||||
}
|
||||
@ -896,33 +898,36 @@ mixin WidgetsBinding on BindingBase, ServicesBinding, SchedulerBinding, GestureB
|
||||
@protected
|
||||
@mustCallSuper
|
||||
@visibleForTesting
|
||||
Future<void> handlePushRoute(String route) async {
|
||||
Future<bool> handlePushRoute(String route) async {
|
||||
final RouteInformation routeInformation = RouteInformation(uri: Uri.parse(route));
|
||||
for (final WidgetsBindingObserver observer in List<WidgetsBindingObserver>.of(_observers)) {
|
||||
if (await observer.didPushRouteInformation(routeInformation)) {
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<void> _handlePushRouteInformation(Map<dynamic, dynamic> routeArguments) async {
|
||||
Future<bool> _handlePushRouteInformation(Map<dynamic, dynamic> routeArguments) async {
|
||||
final RouteInformation routeInformation = RouteInformation(
|
||||
uri: Uri.parse(routeArguments['location'] as String),
|
||||
state: routeArguments['state'] as Object?,
|
||||
);
|
||||
for (final WidgetsBindingObserver observer in List<WidgetsBindingObserver>.of(_observers)) {
|
||||
if (await observer.didPushRouteInformation(routeInformation)) {
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<dynamic> _handleNavigationInvocation(MethodCall methodCall) {
|
||||
Future<bool> _handleNavigationInvocation(MethodCall methodCall) {
|
||||
return switch (methodCall.method) {
|
||||
'popRoute' => handlePopRoute(),
|
||||
'pushRoute' => handlePushRoute(methodCall.arguments as String),
|
||||
'pushRouteInformation' => _handlePushRouteInformation(methodCall.arguments as Map<dynamic, dynamic>),
|
||||
_ => Future<dynamic>.value(),
|
||||
// Return false for unhandled method.
|
||||
_ => Future<bool>.value(false),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -268,7 +268,11 @@ void main() {
|
||||
|
||||
const String testRouteName = 'testRouteName';
|
||||
final ByteData message = const JSONMethodCodec().encodeMethodCall(const MethodCall('pushRoute', testRouteName));
|
||||
await tester.binding.defaultBinaryMessenger.handlePlatformMessage('flutter/navigation', message, (_) {});
|
||||
final ByteData result = (await tester.binding.defaultBinaryMessenger
|
||||
.handlePlatformMessage('flutter/navigation', message, (_) {}))!;
|
||||
final bool decodedResult = const JSONMethodCodec().decodeEnvelope(result) as bool;
|
||||
|
||||
expect(decodedResult, true);
|
||||
expect(observer.pushedRoute, testRouteName);
|
||||
|
||||
WidgetsBinding.instance.removeObserver(observer);
|
||||
@ -286,8 +290,11 @@ void main() {
|
||||
final ByteData message = const JSONMethodCodec().encodeMethodCall(
|
||||
const MethodCall('pushRouteInformation', testRouteInformation),
|
||||
);
|
||||
await tester.binding.defaultBinaryMessenger
|
||||
.handlePlatformMessage('flutter/navigation', message, (_) {});
|
||||
final ByteData result = (await tester.binding.defaultBinaryMessenger
|
||||
.handlePlatformMessage('flutter/navigation', message, (_) {}))!;
|
||||
final bool decodedResult = const JSONMethodCodec().decodeEnvelope(result) as bool;
|
||||
|
||||
expect(decodedResult, true);
|
||||
expect(observer.pushedRoute, 'testRouteName');
|
||||
WidgetsBinding.instance.removeObserver(observer);
|
||||
});
|
||||
@ -377,6 +384,49 @@ void main() {
|
||||
WidgetsBinding.instance.removeObserver(observer);
|
||||
});
|
||||
|
||||
testWidgets('pushRouteInformation not handled by observer returns false', (WidgetTester tester) async {
|
||||
|
||||
const Map<String, dynamic> testRouteInformation = <String, dynamic>{
|
||||
'location': 'testRouteName',
|
||||
'state': null,
|
||||
};
|
||||
final ByteData message = const JSONMethodCodec().encodeMethodCall(
|
||||
const MethodCall('pushRouteInformation', testRouteInformation),
|
||||
);
|
||||
|
||||
final ByteData result = (await tester.binding.defaultBinaryMessenger
|
||||
.handlePlatformMessage('flutter/navigation', message, (_) {}))!;
|
||||
final bool decodedResult = const JSONMethodCodec().decodeEnvelope(result) as bool;
|
||||
|
||||
expect(decodedResult, false);
|
||||
});
|
||||
|
||||
testWidgets('pushRoute not handled by observer returns false', (WidgetTester tester) async {
|
||||
|
||||
const String testRoute = 'testRouteName';
|
||||
final ByteData message = const JSONMethodCodec().encodeMethodCall(
|
||||
const MethodCall('pushRoute', testRoute),
|
||||
);
|
||||
|
||||
final ByteData result = (await tester.binding.defaultBinaryMessenger
|
||||
.handlePlatformMessage('flutter/navigation', message, (_) {}))!;
|
||||
final bool decodedResult = const JSONMethodCodec().decodeEnvelope(result) as bool;
|
||||
|
||||
expect(decodedResult, false);
|
||||
});
|
||||
|
||||
|
||||
testWidgets('popRoute not handled by observer returns false', (WidgetTester tester) async {
|
||||
final ByteData message = const JSONMethodCodec().encodeMethodCall(
|
||||
const MethodCall('popRoute'),
|
||||
);
|
||||
|
||||
final ByteData result = (await tester.binding.defaultBinaryMessenger
|
||||
.handlePlatformMessage('flutter/navigation', message, (_) {}))!;
|
||||
final bool decodedResult = const JSONMethodCodec().decodeEnvelope(result) as bool;
|
||||
|
||||
expect(decodedResult, false);
|
||||
});
|
||||
testWidgets('Application lifecycle affects frame scheduling', (WidgetTester tester) async {
|
||||
expect(tester.binding.hasScheduledFrame, isFalse);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user