From 7a82863f488a3ba634ec4445a2700f75420152cd Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Thu, 4 Mar 2021 15:43:29 -0800 Subject: [PATCH 1/2] [web] Fix url updates when using Router --- lib/web_ui/lib/src/engine/window.dart | 30 +++++++++++++----- lib/web_ui/test/window_test.dart | 45 +++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/lib/web_ui/lib/src/engine/window.dart b/lib/web_ui/lib/src/engine/window.dart index c6fee6697e4ca..4ae0b7f09eaf1 100644 --- a/lib/web_ui/lib/src/engine/window.dart +++ b/lib/web_ui/lib/src/engine/window.dart @@ -44,13 +44,17 @@ class EngineFlutterWindow extends ui.SingletonFlutterWindow { /// button, etc. @visibleForTesting BrowserHistory get browserHistory { + return _browserHistory ??= + MultiEntriesBrowserHistory(urlStrategy: _urlStrategyForInitialization); + } + + UrlStrategy? get _urlStrategyForInitialization { final UrlStrategy? urlStrategy = _isUrlStrategySet ? _customUrlStrategy : _createDefaultUrlStrategy(); // Prevent any further customization of URL strategy. _isUrlStrategySet = true; - return _browserHistory ??= - MultiEntriesBrowserHistory(urlStrategy: urlStrategy); + return urlStrategy; } BrowserHistory? _browserHistory; @@ -59,8 +63,14 @@ class EngineFlutterWindow extends ui.SingletonFlutterWindow { if (_browserHistory is SingleEntryBrowserHistory) { return; } - final UrlStrategy? strategy = _browserHistory?.urlStrategy; - await _browserHistory?.tearDown(); + + final UrlStrategy? strategy; + if (_browserHistory == null) { + strategy = _urlStrategyForInitialization; + } else { + strategy = _browserHistory?.urlStrategy; + await _browserHistory?.tearDown(); + } _browserHistory = SingleEntryBrowserHistory(urlStrategy: strategy); } @@ -68,8 +78,14 @@ class EngineFlutterWindow extends ui.SingletonFlutterWindow { if (_browserHistory is MultiEntriesBrowserHistory) { return; } - final UrlStrategy? strategy = _browserHistory?.urlStrategy; - await _browserHistory?.tearDown(); + + final UrlStrategy? strategy; + if (_browserHistory == null) { + strategy = _urlStrategyForInitialization; + } else { + strategy = _browserHistory?.urlStrategy; + await _browserHistory?.tearDown(); + } _browserHistory = MultiEntriesBrowserHistory(urlStrategy: strategy); } @@ -261,7 +277,7 @@ external set _jsSetUrlStrategy(_JsSetUrlStrategy? newJsSetUrlStrategy); UrlStrategy? _createDefaultUrlStrategy() { return ui.debugEmulateFlutterTesterEnvironment - ? null + ? TestUrlStrategy.fromEntry(TestHistoryEntry('default', null, '/default')) : const HashUrlStrategy(); } diff --git a/lib/web_ui/test/window_test.dart b/lib/web_ui/test/window_test.dart index 3d49ec25002e0..ad65af7b81a77 100644 --- a/lib/web_ui/test/window_test.dart +++ b/lib/web_ui/test/window_test.dart @@ -128,6 +128,51 @@ void testMain() { expect(window.browserHistory.urlStrategy.getPath(), '/baz'); }); + 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. + + Completer callback = Completer(); + window.sendPlatformMessage( + 'flutter/navigation', + JSONMethodCodec().encodeMethodCall(MethodCall( + 'routeUpdated', + {'routeName': '/bar'}, + )), + (_) { callback.complete(); }, + ); + await callback.future; + expect(window.browserHistory is SingleEntryBrowserHistory, true); + // The url strategy should've been set to the default, and the path + // should've been correctly set to "/bar". + expect(window.browserHistory.urlStrategy, isNot(isNull)); + expect(window.browserHistory.urlStrategy.getPath(), '/bar'); + }); + + 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. + + Completer callback = Completer(); + window.sendPlatformMessage( + 'flutter/navigation', + JSONMethodCodec().encodeMethodCall(MethodCall( + 'routeInformationUpdated', + { + 'location': '/baz', + 'state': null, + }, + )), + (_) { callback.complete(); }, + ); + await callback.future; + expect(window.browserHistory is MultiEntriesBrowserHistory, true); + // The url strategy should've been set to the default, and the path + // should've been correctly set to "/baz". + expect(window.browserHistory.urlStrategy, isNot(isNull)); + expect(window.browserHistory.urlStrategy.getPath(), '/baz'); + }); + test('can disable location strategy', () async { // Disable URL strategy. expect(() => jsSetUrlStrategy(null), returnsNormally); From 1a26992c66d7efac72c990f951f3bbe13ae7f147 Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Fri, 5 Mar 2021 10:01:03 -0800 Subject: [PATCH 2/2] skip in Safari --- lib/web_ui/test/window_test.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/web_ui/test/window_test.dart b/lib/web_ui/test/window_test.dart index ad65af7b81a77..565796ea7625d 100644 --- a/lib/web_ui/test/window_test.dart +++ b/lib/web_ui/test/window_test.dart @@ -147,7 +147,7 @@ void testMain() { // should've been correctly set to "/bar". expect(window.browserHistory.urlStrategy, isNot(isNull)); expect(window.browserHistory.urlStrategy.getPath(), '/bar'); - }); + }, skip: browserEngine == BrowserEngine.webkit); // 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 @@ -171,7 +171,7 @@ void testMain() { // should've been correctly set to "/baz". expect(window.browserHistory.urlStrategy, isNot(isNull)); expect(window.browserHistory.urlStrategy.getPath(), '/baz'); - }); + }, skip: browserEngine == BrowserEngine.webkit); // https://github.com/flutter/flutter/issues/50836 test('can disable location strategy', () async { // Disable URL strategy.