Skip to content

Commit cd92039

Browse files
authored
Handle new navigation platform messages (#11880)
1 parent 898480f commit cd92039

File tree

4 files changed

+82
-8
lines changed

4 files changed

+82
-8
lines changed

lib/web_ui/lib/src/engine/window.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class EngineWindow extends ui.Window {
7777
/// Setting this member will automatically update [_browserHistory].
7878
///
7979
/// By setting this to null, the browser history will be disabled.
80-
set webOnlyLocationStrategy(LocationStrategy strategy) {
80+
set locationStrategy(LocationStrategy strategy) {
8181
_browserHistory.locationStrategy = strategy;
8282
}
8383

@@ -147,6 +147,20 @@ class EngineWindow extends ui.Window {
147147
// In widget tests we want to bypass processing of platform messages.
148148
accessibilityAnnouncements.handleMessage(data);
149149
return;
150+
151+
case 'flutter/navigation':
152+
const MethodCodec codec = JSONMethodCodec();
153+
final MethodCall decoded = codec.decodeMethodCall(data);
154+
final Map<String, dynamic> message = decoded.arguments;
155+
switch (decoded.method) {
156+
case 'routePushed':
157+
_browserHistory.setRouteName(message['routeName']);
158+
break;
159+
case 'routePopped':
160+
_browserHistory.setRouteName(message['previousRouteName']);
161+
break;
162+
}
163+
return;
150164
}
151165

152166
if (pluginMessageCallHandler != null) {

lib/web_ui/lib/src/ui/initialization.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Future<void> webOnlyInitializePlatform({
99
engine.AssetManager assetManager,
1010
}) async {
1111
if (!debugEmulateFlutterTesterEnvironment) {
12-
engine.window.webOnlyLocationStrategy = const engine.HashLocationStrategy();
12+
engine.window.locationStrategy = const engine.HashLocationStrategy();
1313
}
1414

1515
engine.webOnlyInitializeEngine();

lib/web_ui/lib/src/ui/test_embedding.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ Future<dynamic> ensureTestPlatformInitializedThenRunTest(
2424
return _testPlatformInitializedFuture.then<dynamic>((_) => body());
2525
}
2626

27-
/// This setter is used by [WebNavigatorObserver] to update the url to
28-
/// reflect the [Navigator]'s current route name.
29-
set webOnlyRouteName(String routeName) {
30-
engine.window.webOnlyRouteName = routeName;
31-
}
32-
3327
/// Used to track when the platform is initialized. This ensures the test fonts
3428
/// are available.
3529
Future<void> _platformInitializedFuture;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:typed_data';
6+
7+
import 'package:test/test.dart';
8+
import 'package:ui/src/engine.dart' as engine;
9+
10+
engine.TestLocationStrategy _strategy;
11+
12+
const engine.MethodCodec codec = engine.JSONMethodCodec();
13+
14+
void emptyCallback(ByteData date) {}
15+
16+
void main() {
17+
setUp(() {
18+
engine.window.locationStrategy = _strategy = engine.TestLocationStrategy();
19+
});
20+
21+
tearDown(() {
22+
engine.window.locationStrategy = _strategy = null;
23+
});
24+
25+
test('Tracks pushed and popped routes', () {
26+
engine.window.sendPlatformMessage(
27+
'flutter/navigation',
28+
codec.encodeMethodCall(const engine.MethodCall(
29+
'routePushed',
30+
<String, dynamic>{'previousRouteName': '/', 'routeName': '/foo'},
31+
)),
32+
emptyCallback,
33+
);
34+
expect(_strategy.path, '/foo');
35+
36+
engine.window.sendPlatformMessage(
37+
'flutter/navigation',
38+
codec.encodeMethodCall(const engine.MethodCall(
39+
'routePushed',
40+
<String, dynamic>{'previousRouteName': '/foo', 'routeName': '/bar'},
41+
)),
42+
emptyCallback,
43+
);
44+
expect(_strategy.path, '/bar');
45+
46+
engine.window.sendPlatformMessage(
47+
'flutter/navigation',
48+
codec.encodeMethodCall(const engine.MethodCall(
49+
'routePopped',
50+
<String, dynamic>{'previousRouteName': '/foo', 'routeName': '/bar'},
51+
)),
52+
emptyCallback,
53+
);
54+
expect(_strategy.path, '/foo');
55+
56+
engine.window.sendPlatformMessage(
57+
'flutter/navigation',
58+
codec.encodeMethodCall(const engine.MethodCall(
59+
'routePushed',
60+
<String, dynamic>{'previousRouteName': '/foo', 'routeName': '/bar/baz'},
61+
)),
62+
emptyCallback,
63+
);
64+
expect(_strategy.path, '/bar/baz');
65+
});
66+
}

0 commit comments

Comments
 (0)