3
3
// found in the LICENSE file.
4
4
5
5
// @dart = 2.6
6
+ import 'dart:async' ;
7
+ import 'dart:html' as html;
8
+ import 'dart:js_util' as js_util;
6
9
import 'dart:typed_data' ;
7
10
8
11
import 'package:test/test.dart' ;
@@ -12,24 +15,18 @@ const MethodCodec codec = JSONMethodCodec();
12
15
13
16
void emptyCallback (ByteData date) {}
14
17
15
- TestLocationStrategy _strategy;
16
- TestLocationStrategy get strategy => _strategy;
17
- set strategy (TestLocationStrategy newStrategy) {
18
- window.locationStrategy = _strategy = newStrategy;
19
- }
20
-
21
18
void main () {
22
19
test ('window.defaultRouteName should not change' , () {
23
- strategy = TestLocationStrategy .fromEntry (TestHistoryEntry ('initial state' , null , '/initial' ));
20
+ window.locationStrategy = TestLocationStrategy .fromEntry (TestHistoryEntry ('initial state' , null , '/initial' ));
24
21
expect (window.defaultRouteName, '/initial' );
25
22
26
23
// Changing the URL in the address bar later shouldn't affect [window.defaultRouteName].
27
- strategy .replaceState (null , null , '/newpath' );
24
+ window.locationStrategy .replaceState (null , null , '/newpath' );
28
25
expect (window.defaultRouteName, '/initial' );
29
26
});
30
27
31
28
test ('window.defaultRouteName should reset after navigation platform message' , () {
32
- strategy = TestLocationStrategy .fromEntry (TestHistoryEntry ('initial state' , null , '/initial' ));
29
+ window.locationStrategy = TestLocationStrategy .fromEntry (TestHistoryEntry ('initial state' , null , '/initial' ));
33
30
// Reading it multiple times should return the same value.
34
31
expect (window.defaultRouteName, '/initial' );
35
32
expect (window.defaultRouteName, '/initial' );
@@ -46,4 +43,47 @@ void main() {
46
43
// reset to "/".
47
44
expect (window.defaultRouteName, '/' );
48
45
});
46
+
47
+ test ('can disable location strategy' , () async {
48
+ final testStrategy = TestLocationStrategy .fromEntry (
49
+ TestHistoryEntry (null , null , '/' ),
50
+ );
51
+ window.locationStrategy = testStrategy;
52
+
53
+ expect (window.locationStrategy, testStrategy);
54
+ // A single listener should've been setup.
55
+ expect (testStrategy.listeners, hasLength (1 ));
56
+ // The initial entry should be there, plus another "flutter" entry.
57
+ expect (testStrategy.history, hasLength (2 ));
58
+ expect (testStrategy.history[0 ].state, < String , bool > {'origin' : true });
59
+ expect (testStrategy.history[1 ].state, < String , bool > {'flutter' : true });
60
+ expect (testStrategy.currentEntry, testStrategy.history[1 ]);
61
+
62
+ // Now, let's disable location strategy and make sure things get cleaned up.
63
+ expect (() => jsSetLocationStrategy (null ), returnsNormally);
64
+ expect (window.locationStrategy, isNull);
65
+
66
+ // The listener is removed asynchronously.
67
+ await Future <void >.delayed (const Duration (milliseconds: 10 ));
68
+
69
+ // No more listeners.
70
+ expect (testStrategy.listeners, isEmpty);
71
+ // History should've moved back to the initial entry.
72
+ expect (testStrategy.history[0 ].state, < String , bool > {'origin' : true });
73
+ expect (testStrategy.currentEntry, testStrategy.history[0 ]);
74
+ });
75
+
76
+ test ('js interop throws on wrong type' , () {
77
+ expect (() => jsSetLocationStrategy (123 ), throwsA (anything));
78
+ expect (() => jsSetLocationStrategy ('foo' ), throwsA (anything));
79
+ expect (() => jsSetLocationStrategy (false ), throwsA (anything));
80
+ });
81
+ }
82
+
83
+ void jsSetLocationStrategy (dynamic strategy) {
84
+ js_util.callMethod (
85
+ html.window,
86
+ '_flutter_web_set_location_strategy' ,
87
+ < dynamic > [strategy],
88
+ );
49
89
}
0 commit comments