diff --git a/lib/web_ui/lib/src/engine/window.dart b/lib/web_ui/lib/src/engine/window.dart index 8a13cf4e136d82da18e35020a660ea2803a03fbd..8a995f0ca03ee0c9d7fae528e2af773dfd4dd2a6 100644 --- a/lib/web_ui/lib/src/engine/window.dart +++ b/lib/web_ui/lib/src/engine/window.dart @@ -77,7 +77,7 @@ class EngineWindow extends ui.Window { /// Setting this member will automatically update [_browserHistory]. /// /// By setting this to null, the browser history will be disabled. - set webOnlyLocationStrategy(LocationStrategy strategy) { + set locationStrategy(LocationStrategy strategy) { _browserHistory.locationStrategy = strategy; } @@ -147,6 +147,20 @@ class EngineWindow extends ui.Window { // In widget tests we want to bypass processing of platform messages. accessibilityAnnouncements.handleMessage(data); return; + + case 'flutter/navigation': + const MethodCodec codec = JSONMethodCodec(); + final MethodCall decoded = codec.decodeMethodCall(data); + final Map message = decoded.arguments; + switch (decoded.method) { + case 'routePushed': + _browserHistory.setRouteName(message['routeName']); + break; + case 'routePopped': + _browserHistory.setRouteName(message['previousRouteName']); + break; + } + return; } if (pluginMessageCallHandler != null) { diff --git a/lib/web_ui/lib/src/ui/initialization.dart b/lib/web_ui/lib/src/ui/initialization.dart index 50bda446735134ba4424bd9e7da58b19cac3b064..041590bfebe262d41e8af8ae98fdb1099222592d 100644 --- a/lib/web_ui/lib/src/ui/initialization.dart +++ b/lib/web_ui/lib/src/ui/initialization.dart @@ -9,7 +9,7 @@ Future webOnlyInitializePlatform({ engine.AssetManager assetManager, }) async { if (!debugEmulateFlutterTesterEnvironment) { - engine.window.webOnlyLocationStrategy = const engine.HashLocationStrategy(); + engine.window.locationStrategy = const engine.HashLocationStrategy(); } engine.webOnlyInitializeEngine(); diff --git a/lib/web_ui/lib/src/ui/test_embedding.dart b/lib/web_ui/lib/src/ui/test_embedding.dart index 592d15d2b1b1a7695076fd7fbc016dbf4639779b..988feb454e39b22c774d234d13c8570f19c236f3 100644 --- a/lib/web_ui/lib/src/ui/test_embedding.dart +++ b/lib/web_ui/lib/src/ui/test_embedding.dart @@ -24,12 +24,6 @@ Future ensureTestPlatformInitializedThenRunTest( return _testPlatformInitializedFuture.then((_) => body()); } -/// This setter is used by [WebNavigatorObserver] to update the url to -/// reflect the [Navigator]'s current route name. -set webOnlyRouteName(String routeName) { - engine.window.webOnlyRouteName = routeName; -} - /// Used to track when the platform is initialized. This ensures the test fonts /// are available. Future _platformInitializedFuture; diff --git a/lib/web_ui/test/engine/navigation_test.dart b/lib/web_ui/test/engine/navigation_test.dart new file mode 100644 index 0000000000000000000000000000000000000000..7258af4ce018d4a2d9538fc71cadd9ed4295993b --- /dev/null +++ b/lib/web_ui/test/engine/navigation_test.dart @@ -0,0 +1,66 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:typed_data'; + +import 'package:test/test.dart'; +import 'package:ui/src/engine.dart' as engine; + +engine.TestLocationStrategy _strategy; + +const engine.MethodCodec codec = engine.JSONMethodCodec(); + +void emptyCallback(ByteData date) {} + +void main() { + setUp(() { + engine.window.locationStrategy = _strategy = engine.TestLocationStrategy(); + }); + + tearDown(() { + engine.window.locationStrategy = _strategy = null; + }); + + test('Tracks pushed and popped routes', () { + engine.window.sendPlatformMessage( + 'flutter/navigation', + codec.encodeMethodCall(const engine.MethodCall( + 'routePushed', + {'previousRouteName': '/', 'routeName': '/foo'}, + )), + emptyCallback, + ); + expect(_strategy.path, '/foo'); + + engine.window.sendPlatformMessage( + 'flutter/navigation', + codec.encodeMethodCall(const engine.MethodCall( + 'routePushed', + {'previousRouteName': '/foo', 'routeName': '/bar'}, + )), + emptyCallback, + ); + expect(_strategy.path, '/bar'); + + engine.window.sendPlatformMessage( + 'flutter/navigation', + codec.encodeMethodCall(const engine.MethodCall( + 'routePopped', + {'previousRouteName': '/foo', 'routeName': '/bar'}, + )), + emptyCallback, + ); + expect(_strategy.path, '/foo'); + + engine.window.sendPlatformMessage( + 'flutter/navigation', + codec.encodeMethodCall(const engine.MethodCall( + 'routePushed', + {'previousRouteName': '/foo', 'routeName': '/bar/baz'}, + )), + emptyCallback, + ); + expect(_strategy.path, '/bar/baz'); + }); +}