From 315ebafea37e67e08c630faa8c1bb56143c791c6 Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Thu, 13 Jul 2023 11:41:56 -0700 Subject: [PATCH] =?UTF-8?q?PlatformRouteInformationProvider=20does=20not?= =?UTF-8?q?=20push=20new=20entry=20if=20query=20par=E2=80=A6=20(#130457)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …ameter is semanticsally the same The URI compare does not taking into account that query parameter may or may not be encoded, or the parameters' order can be different. However, they are all semantically the same. This pr makes PlatformRouteInformationProvider to take those into account when deciding whether it should push/replace the browser history entry. --- packages/flutter/lib/src/widgets/router.dart | 9 ++- .../flutter/test/widgets/router_test.dart | 70 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/router.dart b/packages/flutter/lib/src/widgets/router.dart index 918d36b526..63db13e365 100644 --- a/packages/flutter/lib/src/widgets/router.dart +++ b/packages/flutter/lib/src/widgets/router.dart @@ -5,6 +5,7 @@ import 'dart:async'; import 'dart:collection'; +import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/scheduler.dart'; import 'package:flutter/services.dart'; @@ -1466,12 +1467,18 @@ class PlatformRouteInformationProvider extends RouteInformationProvider with Wid required RouteInformation initialRouteInformation, }) : _value = initialRouteInformation; + static bool _equals(Uri a, Uri b) { + return a.path == b.path + && a.fragment == b.fragment + && const DeepCollectionEquality.unordered().equals(a.queryParametersAll, b.queryParametersAll); + } + @override void routerReportsNewRouteInformation(RouteInformation routeInformation, {RouteInformationReportingType type = RouteInformationReportingType.none}) { final bool replace = type == RouteInformationReportingType.neglect || (type == RouteInformationReportingType.none && - _valueInEngine.uri == routeInformation.uri); + _equals(_valueInEngine.uri, routeInformation.uri)); SystemNavigator.selectMultiEntryHistory(); SystemNavigator.routeInformationUpdated( uri: routeInformation.uri, diff --git a/packages/flutter/test/widgets/router_test.dart b/packages/flutter/test/widgets/router_test.dart index f07d59245b..cf46e0fc3d 100644 --- a/packages/flutter/test/widgets/router_test.dart +++ b/packages/flutter/test/widgets/router_test.dart @@ -867,6 +867,76 @@ testWidgets('ChildBackButtonDispatcher take priority recursively', (WidgetTester ]); }); + testWidgets('PlatformRouteInformationProvider does not push new entry if query parameters are semantically the same', (WidgetTester tester) async { + final List log = []; + TestDefaultBinaryMessengerBinding + .instance.defaultBinaryMessenger + .setMockMethodCallHandler( + SystemChannels.navigation, + (MethodCall methodCall) async { + log.add(methodCall); + return null; + } + ); + final RouteInformation initial = RouteInformation( + uri: Uri.parse('initial?a=ws/abcd'), + ); + final RouteInformationProvider provider = PlatformRouteInformationProvider( + initialRouteInformation: initial + ); + // Make sure engine is updated with initial route + provider.routerReportsNewRouteInformation(initial); + log.clear(); + + provider.routerReportsNewRouteInformation( + RouteInformation( + uri: Uri( + path: 'initial', + queryParameters: {'a': 'ws/abcd'}, // This will be escaped. + ), + ), + ); + expect(provider.value.uri.toString(), 'initial?a=ws%2Fabcd'); + // should use `replace: true` + expect(log, [ + isMethodCall('selectMultiEntryHistory', arguments: null), + isMethodCall('routeInformationUpdated', arguments: { 'uri': 'initial?a=ws%2Fabcd', 'state': null, 'replace': true }), + ]); + log.clear(); + + provider.routerReportsNewRouteInformation( + RouteInformation(uri: Uri.parse('initial?a=1&b=2')), + ); + log.clear(); + + // Change query parameters order + provider.routerReportsNewRouteInformation( + RouteInformation(uri: Uri.parse('initial?b=2&a=1')), + ); + // should use `replace: true` + expect(log, [ + isMethodCall('selectMultiEntryHistory', arguments: null), + isMethodCall('routeInformationUpdated', arguments: { 'uri': 'initial?b=2&a=1', 'state': null, 'replace': true }), + ]); + log.clear(); + + provider.routerReportsNewRouteInformation( + RouteInformation(uri: Uri.parse('initial?a=1&a=2')), + ); + log.clear(); + + // Change query parameters order for same key + provider.routerReportsNewRouteInformation( + RouteInformation(uri: Uri.parse('initial?a=2&a=1')), + ); + // should use `replace: true` + expect(log, [ + isMethodCall('selectMultiEntryHistory', arguments: null), + isMethodCall('routeInformationUpdated', arguments: { 'uri': 'initial?a=2&a=1', 'state': null, 'replace': true }), + ]); + log.clear(); + }); + testWidgets('RootBackButtonDispatcher works', (WidgetTester tester) async { final BackButtonDispatcher outerDispatcher = RootBackButtonDispatcher(); final RouteInformationProvider provider = PlatformRouteInformationProvider(