[web] Cleanup usages of deprecated routeUpdated message (#173782)

This is a follow up to https://github.com/flutter/flutter/pull/173652

Closes https://github.com/flutter/flutter/issues/50836
This commit is contained in:
Mouad Debbar 2025-08-14 18:51:28 -04:00 committed by GitHub
parent 667da94d3c
commit 62904ba72a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 71 deletions

View File

@ -625,11 +625,6 @@ final class EngineFlutterWindow extends EngineFlutterView implements ui.Singleto
await _useSingleEntryBrowserHistory();
return true;
// the following cases assert that arguments are not null
case 'routeUpdated': // deprecated
assert(arguments != null);
await _useSingleEntryBrowserHistory();
browserHistory.setRouteName(arguments!.tryString('routeName'));
return true;
case 'routeInformationUpdated':
assert(arguments != null);
final String? uriString = arguments!.tryString('uri');

View File

@ -186,7 +186,7 @@ void testMain() {
expect(strategy.history, hasLength(2));
expect(strategy.currentEntry.state, flutterState);
expect(strategy.currentEntry.url, '/home');
await routeUpdated('/page1');
await routeInformationUpdated('/page1', null);
// The number of entries shouldn't change.
expect(strategy.history, hasLength(2));
expect(strategy.currentEntryIndex, 1);
@ -204,7 +204,7 @@ void testMain() {
expect(spy.messages[0].methodName, 'popRoute');
expect(spy.messages[0].methodArguments, isNull);
// The framework responds by updating to the most current route name.
await routeUpdated('/home');
await routeInformationUpdated('/home', null);
// We still have 2 entries.
expect(strategy.history, hasLength(2));
expect(strategy.currentEntryIndex, 1);
@ -219,8 +219,8 @@ void testMain() {
);
await implicitView.debugInitializeHistory(strategy, useSingle: true);
await routeUpdated('/page1');
await routeUpdated('/page2');
await routeInformationUpdated('/page1', null);
await routeInformationUpdated('/page2', null);
// Make sure we are on page2.
expect(strategy.history, hasLength(2));
@ -237,7 +237,7 @@ void testMain() {
expect(spy.messages[0].methodArguments, isNull);
spy.messages.clear();
// 2. The framework sends a `routePopped` platform message.
await routeUpdated('/page1');
await routeInformationUpdated('/page1', null);
// 3. The history state should reflect that /page1 is currently active.
expect(strategy.history, hasLength(2));
expect(strategy.currentEntryIndex, 1);
@ -253,7 +253,7 @@ void testMain() {
expect(spy.messages[0].methodArguments, isNull);
spy.messages.clear();
// 2. The framework sends a `routePopped` platform message.
await routeUpdated('/home');
await routeInformationUpdated('/home', null);
// 3. The history state should reflect that /page1 is currently active.
expect(strategy.history, hasLength(2));
expect(strategy.currentEntryIndex, 1);
@ -296,7 +296,7 @@ void testMain() {
expect(spy.messages[0].methodArguments, '/page3');
spy.messages.clear();
// 2. The framework sends a `routeUpdated` platform message.
await routeUpdated('/page3');
await routeInformationUpdated('/page3', null);
// 3. The history state should reflect that /page3 is currently active.
expect(strategy.history, hasLength(3));
expect(strategy.currentEntryIndex, 1);
@ -312,7 +312,7 @@ void testMain() {
expect(spy.messages[0].methodArguments, isNull);
spy.messages.clear();
// 2. The framework sends a `routeUpdated` platform message.
await routeUpdated('/home');
await routeInformationUpdated('/home', null);
// 3. The history state should reflect that /home is currently active.
expect(strategy.history, hasLength(2));
expect(strategy.currentEntryIndex, 1);
@ -351,7 +351,7 @@ void testMain() {
await implicitView.debugInitializeHistory(strategy, useSingle: true);
// Go to a named route.
await routeUpdated('/named-route');
await routeInformationUpdated('/named-route', null);
expect(strategy.currentEntry.url, '/named-route');
// Now, push a nameless route. The url shouldn't change.
@ -761,16 +761,6 @@ void testMain() {
});
}
Future<void> routeUpdated(String routeName) {
final Completer<void> completer = Completer<void>();
EnginePlatformDispatcher.instance.sendPlatformMessage(
'flutter/navigation',
codec.encodeMethodCall(MethodCall('routeUpdated', <String, dynamic>{'routeName': routeName})),
(_) => completer.complete(),
);
return completer.future;
}
Future<void> routeInformationUpdated(String location, dynamic state) {
final Completer<void> completer = Completer<void>();
EnginePlatformDispatcher.instance.sendPlatformMessage(

View File

@ -29,7 +29,7 @@ void testMain() {
ui.PlatformDispatcher.instance.sendPlatformMessage(
'flutter/navigation',
codec.encodeMethodCall(
const MethodCall('routeUpdated', <String, dynamic>{'routeName': '/foo'}),
const MethodCall('routeInformationUpdated', <String, dynamic>{'location': '/foo'}),
),
(ByteData? response) => completer.complete(response),
);
@ -53,7 +53,7 @@ void testMain() {
ui.PlatformDispatcher.instance.sendPlatformMessage(
'flutter/navigation',
codec.encodeMethodCall(
const MethodCall('routeUpdated', <String, dynamic>{'routeName': '/foo'}),
const MethodCall('routeInformationUpdated', <String, dynamic>{'location': '/foo'}),
),
(_) => completer.complete(),
);

View File

@ -78,7 +78,7 @@ void testMain() {
myWindow.sendPlatformMessage(
'flutter/navigation',
const JSONMethodCodec().encodeMethodCall(
const MethodCall('routeUpdated', <String, dynamic>{'routeName': '/bar'}),
const MethodCall('routeInformationUpdated', <String, dynamic>{'location': '/bar'}),
),
(_) {
callback.complete();
@ -125,17 +125,14 @@ void testMain() {
'selectMultiEntryHistory',
<String, dynamic>{},
); // -> multi
await check<SingleEntryBrowserHistory>('routeUpdated', <String, dynamic>{
'routeName': '/bar',
}); // -> single
await check<SingleEntryBrowserHistory>('routeInformationUpdated', <String, dynamic>{
await check<MultiEntriesBrowserHistory>('routeInformationUpdated', <String, dynamic>{
'location': '/bar',
}); // does not change mode
await check<MultiEntriesBrowserHistory>(
'selectMultiEntryHistory',
await check<SingleEntryBrowserHistory>(
'selectSingleEntryHistory',
<String, dynamic>{},
); // -> multi
await check<MultiEntriesBrowserHistory>('routeInformationUpdated', <String, dynamic>{
); // -> single
await check<SingleEntryBrowserHistory>('routeInformationUpdated', <String, dynamic>{
'location': '/bar',
}); // does not change mode
});
@ -143,12 +140,6 @@ void testMain() {
test(
'handleNavigationMessage throws for route update methods called with null arguments',
() async {
expect(() async {
await myWindow.handleNavigationMessage(
const JSONMethodCodec().encodeMethodCall(const MethodCall('routeUpdated')),
);
}, throwsAssertionError);
expect(() async {
await myWindow.handleNavigationMessage(
const JSONMethodCodec().encodeMethodCall(const MethodCall('routeInformationUpdated')),
@ -214,12 +205,21 @@ void testMain() {
);
expect(myWindow.browserHistory, isA<MultiEntriesBrowserHistory>());
// routeUpdated resets the history type
// change the history type
Completer<void> callback = Completer<void>();
myWindow.sendPlatformMessage(
'flutter/navigation',
const JSONMethodCodec().encodeMethodCall(const MethodCall('selectSingleEntryHistory')),
(_) {
callback.complete();
},
);
await callback.future;
callback = Completer<void>();
myWindow.sendPlatformMessage(
'flutter/navigation',
const JSONMethodCodec().encodeMethodCall(
const MethodCall('routeUpdated', <String, dynamic>{'routeName': '/bar'}),
const MethodCall('routeInformationUpdated', <String, dynamic>{'location': '/bar'}),
),
(_) {
callback.complete();
@ -250,7 +250,7 @@ void testMain() {
// they can be interleaved safely
await myWindow.handleNavigationMessage(
const JSONMethodCodec().encodeMethodCall(
const MethodCall('routeUpdated', <String, dynamic>{'routeName': '/foo'}),
const MethodCall('routeInformationUpdated', <String, dynamic>{'location': '/foo'}),
),
);
expect(myWindow.browserHistory, isA<SingleEntryBrowserHistory>());
@ -390,32 +390,6 @@ void testMain() {
expect(myWindow.browserHistory.urlStrategy!.getState(), _tagStateWithSerialCount('/state1', 1));
});
test('initialize browser history with default url strategy (single)', () async {
// On purpose, we don't initialize history on the window. We want to let the
// window to self-initialize when it receives a navigation message.
// Without initializing history, the default route name should be
// initialized to "/" in tests.
expect(myWindow.defaultRouteName, '/');
final Completer<void> callback = Completer<void>();
myWindow.sendPlatformMessage(
'flutter/navigation',
const JSONMethodCodec().encodeMethodCall(
const MethodCall('routeUpdated', <String, dynamic>{'routeName': '/bar'}),
),
(_) {
callback.complete();
},
);
await callback.future;
expect(myWindow.browserHistory, isA<SingleEntryBrowserHistory>());
// The url strategy should've been set to the default, and the path
// should've been correctly set to "/bar".
expect(myWindow.browserHistory.urlStrategy, isNot(isNull));
expect(myWindow.browserHistory.urlStrategy!.getPath(), '/bar');
}, skip: isSafari); // https://github.com/flutter/flutter/issues/50836
test('initialize browser history with default url strategy (multiple)', () async {
// On purpose, we don't initialize history on the window. We want to let the
// window to self-initialize when it receives a navigation message.
@ -443,7 +417,7 @@ void testMain() {
// should've been correctly set to "/baz".
expect(myWindow.browserHistory.urlStrategy, isNot(isNull));
expect(myWindow.browserHistory.urlStrategy!.getPath(), '/baz');
}, skip: isSafari); // https://github.com/flutter/flutter/issues/50836
});
test('can disable location strategy', () async {
// Disable URL strategy.