2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
- // TODO(egarciad): Remove once Mockito has been migrated to null safety.
6
- // @dart = 2.9
7
-
8
5
import 'dart:ui' ;
6
+
9
7
import 'package:flutter/material.dart' ;
10
8
import 'package:flutter_test/flutter_test.dart' ;
11
9
import 'package:mockito/mockito.dart' ;
12
10
import 'package:flutter/services.dart' ;
13
- import 'package:plugin_platform_interface/plugin_platform_interface.dart' ;
14
11
import 'package:url_launcher/link.dart' ;
15
12
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart' ;
16
13
14
+ import 'mock_url_launcher_platform.dart' ;
15
+
17
16
final MethodCodec _codec = const JSONMethodCodec ();
18
17
19
18
void main () {
20
- final MockUrlLauncher mock = MockUrlLauncher ();
21
- UrlLauncherPlatform .instance = mock;
19
+ late MockUrlLauncher mock;
22
20
23
- PlatformMessageCallback realOnPlatformMessage;
21
+ PlatformMessageCallback ? realOnPlatformMessage;
24
22
setUp (() {
23
+ mock = MockUrlLauncher ();
24
+ UrlLauncherPlatform .instance = mock;
25
25
realOnPlatformMessage = window.onPlatformMessage;
26
26
});
27
27
tearDown (() {
@@ -31,11 +31,11 @@ void main() {
31
31
group ('$Link ' , () {
32
32
testWidgets ('handles null uri correctly' , (WidgetTester tester) async {
33
33
bool isBuilt = false ;
34
- FollowLink followLink;
34
+ FollowLink ? followLink;
35
35
36
36
final Link link = Link (
37
37
uri: null ,
38
- builder: (BuildContext context, FollowLink followLink2) {
38
+ builder: (BuildContext context, FollowLink ? followLink2) {
39
39
isBuilt = true ;
40
40
followLink = followLink2;
41
41
return Container ();
@@ -50,66 +50,62 @@ void main() {
50
50
51
51
testWidgets ('calls url_launcher for external URLs with blank target' ,
52
52
(WidgetTester tester) async {
53
- FollowLink followLink;
53
+ FollowLink ? followLink;
54
54
55
55
await tester.pumpWidget (Link (
56
56
uri: Uri .parse ('http://example.com/foobar' ),
57
57
target: LinkTarget .blank,
58
- builder: (BuildContext context, FollowLink followLink2) {
58
+ builder: (BuildContext context, FollowLink ? followLink2) {
59
59
followLink = followLink2;
60
60
return Container ();
61
61
},
62
62
));
63
63
64
- when (mock.canLaunch ('http://example.com/foobar' ))
65
- .thenAnswer ((realInvocation) => Future <bool >.value (true ));
66
- clearInteractions (mock);
67
- await followLink ();
68
-
69
- verifyInOrder ([
70
- mock.canLaunch ('http://example.com/foobar' ),
71
- mock.launch (
72
- 'http://example.com/foobar' ,
64
+ mock
65
+ ..setLaunchExpectations (
66
+ url: 'http://example.com/foobar' ,
73
67
useSafariVC: false ,
74
68
useWebView: false ,
75
69
universalLinksOnly: false ,
76
70
enableJavaScript: false ,
77
71
enableDomStorage: false ,
78
72
headers: < String , String > {},
73
+ webOnlyWindowName: null ,
79
74
)
80
- ]);
75
+ ..setResponse (true );
76
+ await followLink !();
77
+ expect (mock.canLaunchCalled, isTrue);
78
+ expect (mock.launchCalled, isTrue);
81
79
});
82
80
83
81
testWidgets ('calls url_launcher for external URLs with self target' ,
84
82
(WidgetTester tester) async {
85
- FollowLink followLink;
83
+ FollowLink ? followLink;
86
84
87
85
await tester.pumpWidget (Link (
88
86
uri: Uri .parse ('http://example.com/foobar' ),
89
87
target: LinkTarget .self,
90
- builder: (BuildContext context, FollowLink followLink2) {
88
+ builder: (BuildContext context, FollowLink ? followLink2) {
91
89
followLink = followLink2;
92
90
return Container ();
93
91
},
94
92
));
95
93
96
- when (mock.canLaunch ('http://example.com/foobar' ))
97
- .thenAnswer ((realInvocation) => Future <bool >.value (true ));
98
- clearInteractions (mock);
99
- await followLink ();
100
-
101
- verifyInOrder ([
102
- mock.canLaunch ('http://example.com/foobar' ),
103
- mock.launch (
104
- 'http://example.com/foobar' ,
94
+ mock
95
+ ..setLaunchExpectations (
96
+ url: 'http://example.com/foobar' ,
105
97
useSafariVC: true ,
106
98
useWebView: true ,
107
99
universalLinksOnly: false ,
108
100
enableJavaScript: false ,
109
101
enableDomStorage: false ,
110
102
headers: < String , String > {},
103
+ webOnlyWindowName: null ,
111
104
)
112
- ]);
105
+ ..setResponse (true );
106
+ await followLink !();
107
+ expect (mock.canLaunchCalled, isTrue);
108
+ expect (mock.launchCalled, isTrue);
113
109
});
114
110
115
111
testWidgets ('sends navigation platform messages for internal route names' ,
@@ -125,21 +121,21 @@ void main() {
125
121
final List <MethodCall > frameworkCalls = < MethodCall > [];
126
122
window.onPlatformMessage = (
127
123
String name,
128
- ByteData data,
129
- PlatformMessageResponseCallback callback,
124
+ ByteData ? data,
125
+ PlatformMessageResponseCallback ? callback,
130
126
) {
131
127
frameworkCalls.add (_codec.decodeMethodCall (data));
132
- realOnPlatformMessage (name, data, callback);
128
+ realOnPlatformMessage ! (name, data, callback);
133
129
};
134
130
135
131
final Uri uri = Uri .parse ('/foo/bar' );
136
- FollowLink followLink;
132
+ FollowLink ? followLink;
137
133
138
134
await tester.pumpWidget (MaterialApp (
139
135
routes: < String , WidgetBuilder > {
140
136
'/' : (BuildContext context) => Link (
141
137
uri: uri,
142
- builder: (BuildContext context, FollowLink followLink2) {
138
+ builder: (BuildContext context, FollowLink ? followLink2) {
143
139
followLink = followLink2;
144
140
return Container ();
145
141
},
@@ -150,11 +146,11 @@ void main() {
150
146
151
147
engineCalls.clear ();
152
148
frameworkCalls.clear ();
153
- clearInteractions (mock);
154
- await followLink ();
149
+ await followLink !();
155
150
156
151
// Shouldn't use url_launcher when uri is an internal route name.
157
- verifyZeroInteractions (mock);
152
+ expect (mock.canLaunchCalled, isFalse);
153
+ expect (mock.launchCalled, isFalse);
158
154
159
155
// A message should've been sent to the engine (by the Navigator, not by
160
156
// the Link widget).
@@ -191,19 +187,19 @@ void main() {
191
187
final List <MethodCall > frameworkCalls = < MethodCall > [];
192
188
window.onPlatformMessage = (
193
189
String name,
194
- ByteData data,
195
- PlatformMessageResponseCallback callback,
190
+ ByteData ? data,
191
+ PlatformMessageResponseCallback ? callback,
196
192
) {
197
193
frameworkCalls.add (_codec.decodeMethodCall (data));
198
- realOnPlatformMessage (name, data, callback);
194
+ realOnPlatformMessage ! (name, data, callback);
199
195
};
200
196
201
197
final Uri uri = Uri .parse ('/foo/bar' );
202
- FollowLink followLink;
198
+ FollowLink ? followLink;
203
199
204
200
final Link link = Link (
205
201
uri: uri,
206
- builder: (BuildContext context, FollowLink followLink2) {
202
+ builder: (BuildContext context, FollowLink ? followLink2) {
207
203
followLink = followLink2;
208
204
return Container ();
209
205
},
@@ -217,11 +213,11 @@ void main() {
217
213
218
214
engineCalls.clear ();
219
215
frameworkCalls.clear ();
220
- clearInteractions (mock);
221
- await followLink ();
216
+ await followLink !();
222
217
223
218
// Shouldn't use url_launcher when uri is an internal route name.
224
- verifyZeroInteractions (mock);
219
+ expect (mock.canLaunchCalled, isFalse);
220
+ expect (mock.launchCalled, isFalse);
225
221
226
222
// Sends route information update to the engine.
227
223
expect (engineCalls, hasLength (1 ));
@@ -249,10 +245,6 @@ void main() {
249
245
});
250
246
}
251
247
252
- class MockUrlLauncher extends Mock
253
- with MockPlatformInterfaceMixin
254
- implements UrlLauncherPlatform {}
255
-
256
248
class MockRouteInformationParser extends Mock
257
249
implements RouteInformationParser <bool > {
258
250
@override
@@ -261,13 +253,19 @@ class MockRouteInformationParser extends Mock
261
253
}
262
254
}
263
255
264
- class MockRouterDelegate extends Mock implements RouterDelegate {
265
- MockRouterDelegate ({@ required this .builder});
256
+ class MockRouterDelegate extends Mock implements RouterDelegate < Object > {
257
+ MockRouterDelegate ({required this .builder});
266
258
267
259
final WidgetBuilder builder;
268
260
269
261
@override
270
262
Widget build (BuildContext context) {
271
263
return builder (context);
272
264
}
265
+
266
+ @override
267
+ Future <void > setInitialRoutePath (Object configuration) async {}
268
+
269
+ @override
270
+ Future <void > setNewRoutePath (Object configuration) async {}
273
271
}
0 commit comments