@@ -44,10 +44,15 @@ void testMain() {
4444 expect (window.defaultRouteName, '/initial' );
4545 });
4646
47+ // window.defaultRouteName is now permanently decoupled from the history,
48+ // even in subsequent tests, because the PlatformDispatcher caches it.
49+
4750 test ('window.defaultRouteName should reset after navigation platform message' ,
4851 () async {
4952 await window.debugInitializeHistory (TestUrlStrategy .fromEntry (
50- TestHistoryEntry ('initial state' , null , '/initial' ),
53+ // The URL here does not set the PlatformDispatcher's defaultRouteName,
54+ // since it got cached as soon as we read it above.
55+ TestHistoryEntry ('initial state' , null , '/not-really-inital/THIS_IS_IGNORED' ),
5156 ), useSingle: true );
5257 // Reading it multiple times should return the same value.
5358 expect (window.defaultRouteName, '/initial' );
@@ -62,17 +67,48 @@ void testMain() {
6267 )),
6368 (_) { callback.complete (); },
6469 );
65- // After a navigation platform message, [window.defaultRouteName] should
66- // reset to "/".
70+ await callback.future;
71+ // After a navigation platform message, the PlatformDispatcher's
72+ // defaultRouteName resets to "/".
6773 expect (window.defaultRouteName, '/' );
6874 });
6975
70- test ('should throw when using nav1 and nav2 together' ,
76+ // window.defaultRouteName is now '/'.
77+
78+ test ('can switch history mode' , () async {
79+ Completer <void > callback;
80+ await window.debugInitializeHistory (TestUrlStrategy .fromEntry (
81+ TestHistoryEntry ('initial state' , null , '/initial' ),
82+ ), useSingle: false );
83+ expect (window.browserHistory, isA <MultiEntriesBrowserHistory >());
84+
85+ Future <void > check <T >(String method, Map <String , Object ?> arguments) async {
86+ callback = Completer <void >();
87+ window.sendPlatformMessage (
88+ 'flutter/navigation' ,
89+ JSONMethodCodec ().encodeMethodCall (MethodCall (method, arguments)),
90+ (_) { callback.complete (); },
91+ );
92+ await callback.future;
93+ expect (window.browserHistory, isA <T >());
94+ }
95+
96+ await check <SingleEntryBrowserHistory >('selectSingleEntryHistory' , < String , dynamic > {}); // -> single
97+ await check <MultiEntriesBrowserHistory >('selectMultiEntryHistory' , < String , dynamic > {}); // -> multi
98+ await check <SingleEntryBrowserHistory >('routeUpdated' , < String , dynamic > {'routeName' : '/bar' }); // -> single
99+ await check <SingleEntryBrowserHistory >('routeInformationUpdated' , < String , dynamic > {'location' : '/bar' }); // does not change mode
100+ await check <MultiEntriesBrowserHistory >('selectMultiEntryHistory' , < String , dynamic > {}); // -> multi
101+ await check <MultiEntriesBrowserHistory >('routeInformationUpdated' , < String , dynamic > {'location' : '/bar' }); // does not change mode
102+ });
103+
104+ test ('should not throw when using nav1 and nav2 together' ,
71105 () async {
72106 await window.debugInitializeHistory (TestUrlStrategy .fromEntry (
73107 TestHistoryEntry ('initial state' , null , '/initial' ),
74108 ), useSingle: false );
75- // Receive nav1 update first.
109+ expect (window.browserHistory, isA <MultiEntriesBrowserHistory >());
110+
111+ // routeUpdated resets the history type
76112 Completer <void > callback = Completer <void >();
77113 window.sendPlatformMessage (
78114 'flutter/navigation' ,
@@ -83,10 +119,10 @@ void testMain() {
83119 (_) { callback.complete (); },
84120 );
85121 await callback.future;
86- expect (window.browserHistory is SingleEntryBrowserHistory , true );
122+ expect (window.browserHistory, isA < SingleEntryBrowserHistory >() );
87123 expect (window.browserHistory.urlStrategy! .getPath (), '/bar' );
88124
89- // We can still receive nav2 update.
125+ // routeInformationUpdated does not
90126 callback = Completer <void >();
91127 window.sendPlatformMessage (
92128 'flutter/navigation' ,
@@ -100,29 +136,18 @@ void testMain() {
100136 (_) { callback.complete (); },
101137 );
102138 await callback.future;
103- expect (window.browserHistory is MultiEntriesBrowserHistory , true );
139+ expect (window.browserHistory, isA < SingleEntryBrowserHistory >() );
104140 expect (window.browserHistory.urlStrategy! .getPath (), '/baz' );
105141
106- // Throws assertion error if it receives nav1 update after nav2 update.
107- late AssertionError caughtAssertion;
142+ // they can be interleaved safely
108143 await window.handleNavigationMessage (
109144 JSONMethodCodec ().encodeMethodCall (MethodCall (
110145 'routeUpdated' ,
111146 < String , dynamic > {'routeName' : '/foo' },
112147 ))
113- ).catchError ((Object e) {
114- caughtAssertion = e as AssertionError ;
115- });
116-
117- expect (
118- caughtAssertion.message,
119- 'Receives old navigator update in a router application. This can '
120- 'happen if you use non-router versions of '
121- 'MaterialApp/CupertinoApp/WidgetsApp together with the router versions of them.'
122148 );
123- // The history does not change.
124- expect (window.browserHistory is MultiEntriesBrowserHistory , true );
125- expect (window.browserHistory.urlStrategy! .getPath (), '/baz' );
149+ expect (window.browserHistory, isA <SingleEntryBrowserHistory >());
150+ expect (window.browserHistory.urlStrategy! .getPath (), '/foo' );
126151 });
127152
128153 test ('initialize browser history with default url strategy (single)' , () async {
@@ -143,7 +168,7 @@ void testMain() {
143168 (_) { callback.complete (); },
144169 );
145170 await callback.future;
146- expect (window.browserHistory is SingleEntryBrowserHistory , true );
171+ expect (window.browserHistory, isA < SingleEntryBrowserHistory >() );
147172 // The url strategy should've been set to the default, and the path
148173 // should've been correctly set to "/bar".
149174 expect (window.browserHistory.urlStrategy, isNot (isNull));
@@ -171,7 +196,7 @@ void testMain() {
171196 (_) { callback.complete (); },
172197 );
173198 await callback.future;
174- expect (window.browserHistory is MultiEntriesBrowserHistory , true );
199+ expect (window.browserHistory, isA < MultiEntriesBrowserHistory >() );
175200 // The url strategy should've been set to the default, and the path
176201 // should've been correctly set to "/baz".
177202 expect (window.browserHistory.urlStrategy, isNot (isNull));
0 commit comments