Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit f09b300

Browse files
[url_launcher] Migrate unit tests to NNBD
Replaces the problematic Mockito mock with a manual mock that handles null and non-null types correctly.
1 parent 98a90d6 commit f09b300

File tree

4 files changed

+269
-169
lines changed

4 files changed

+269
-169
lines changed

packages/url_launcher/url_launcher/example/test/url_launcher_example_test.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// TODO(egarciad): Remove once mockito is migrated to null safety.
6-
// @dart = 2.9
7-
85
import 'package:flutter_test/flutter_test.dart';
96
import 'package:flutter/material.dart';
107
import 'package:mockito/mockito.dart';

packages/url_launcher/url_launcher/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ dev_dependencies:
3939
flutter_test:
4040
sdk: flutter
4141
test: ^1.16.3
42-
mockito: ^5.0.0-nullsafety.7
42+
mockito: ^5.0.0
4343
plugin_platform_interface: ^2.0.0
4444
pedantic: ^1.10.0
4545

packages/url_launcher/url_launcher/test/link_test.dart

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// TODO(egarciad): Remove once Mockito has been migrated to null safety.
6-
// @dart = 2.9
7-
85
import 'dart:ui';
96
import 'package:flutter/material.dart';
107
import 'package:flutter_test/flutter_test.dart';
@@ -20,7 +17,7 @@ void main() {
2017
final MockUrlLauncher mock = MockUrlLauncher();
2118
UrlLauncherPlatform.instance = mock;
2219

23-
PlatformMessageCallback realOnPlatformMessage;
20+
PlatformMessageCallback? realOnPlatformMessage;
2421
setUp(() {
2522
realOnPlatformMessage = window.onPlatformMessage;
2623
});
@@ -31,11 +28,11 @@ void main() {
3128
group('$Link', () {
3229
testWidgets('handles null uri correctly', (WidgetTester tester) async {
3330
bool isBuilt = false;
34-
FollowLink followLink;
31+
FollowLink? followLink;
3532

3633
final Link link = Link(
3734
uri: null,
38-
builder: (BuildContext context, FollowLink followLink2) {
35+
builder: (BuildContext context, FollowLink? followLink2) {
3936
isBuilt = true;
4037
followLink = followLink2;
4138
return Container();
@@ -50,12 +47,12 @@ void main() {
5047

5148
testWidgets('calls url_launcher for external URLs with blank target',
5249
(WidgetTester tester) async {
53-
FollowLink followLink;
50+
FollowLink? followLink;
5451

5552
await tester.pumpWidget(Link(
5653
uri: Uri.parse('http://example.com/foobar'),
5754
target: LinkTarget.blank,
58-
builder: (BuildContext context, FollowLink followLink2) {
55+
builder: (BuildContext context, FollowLink? followLink2) {
5956
followLink = followLink2;
6057
return Container();
6158
},
@@ -64,7 +61,7 @@ void main() {
6461
when(mock.canLaunch('http://example.com/foobar'))
6562
.thenAnswer((realInvocation) => Future<bool>.value(true));
6663
clearInteractions(mock);
67-
await followLink();
64+
await followLink!();
6865

6966
verifyInOrder([
7067
mock.canLaunch('http://example.com/foobar'),
@@ -82,12 +79,12 @@ void main() {
8279

8380
testWidgets('calls url_launcher for external URLs with self target',
8481
(WidgetTester tester) async {
85-
FollowLink followLink;
82+
FollowLink? followLink;
8683

8784
await tester.pumpWidget(Link(
8885
uri: Uri.parse('http://example.com/foobar'),
8986
target: LinkTarget.self,
90-
builder: (BuildContext context, FollowLink followLink2) {
87+
builder: (BuildContext context, FollowLink? followLink2) {
9188
followLink = followLink2;
9289
return Container();
9390
},
@@ -96,7 +93,7 @@ void main() {
9693
when(mock.canLaunch('http://example.com/foobar'))
9794
.thenAnswer((realInvocation) => Future<bool>.value(true));
9895
clearInteractions(mock);
99-
await followLink();
96+
await followLink!();
10097

10198
verifyInOrder([
10299
mock.canLaunch('http://example.com/foobar'),
@@ -125,21 +122,21 @@ void main() {
125122
final List<MethodCall> frameworkCalls = <MethodCall>[];
126123
window.onPlatformMessage = (
127124
String name,
128-
ByteData data,
129-
PlatformMessageResponseCallback callback,
125+
ByteData? data,
126+
PlatformMessageResponseCallback? callback,
130127
) {
131128
frameworkCalls.add(_codec.decodeMethodCall(data));
132-
realOnPlatformMessage(name, data, callback);
129+
realOnPlatformMessage!(name, data, callback);
133130
};
134131

135132
final Uri uri = Uri.parse('/foo/bar');
136-
FollowLink followLink;
133+
FollowLink? followLink;
137134

138135
await tester.pumpWidget(MaterialApp(
139136
routes: <String, WidgetBuilder>{
140137
'/': (BuildContext context) => Link(
141138
uri: uri,
142-
builder: (BuildContext context, FollowLink followLink2) {
139+
builder: (BuildContext context, FollowLink? followLink2) {
143140
followLink = followLink2;
144141
return Container();
145142
},
@@ -151,7 +148,7 @@ void main() {
151148
engineCalls.clear();
152149
frameworkCalls.clear();
153150
clearInteractions(mock);
154-
await followLink();
151+
await followLink!();
155152

156153
// Shouldn't use url_launcher when uri is an internal route name.
157154
verifyZeroInteractions(mock);
@@ -191,19 +188,19 @@ void main() {
191188
final List<MethodCall> frameworkCalls = <MethodCall>[];
192189
window.onPlatformMessage = (
193190
String name,
194-
ByteData data,
195-
PlatformMessageResponseCallback callback,
191+
ByteData? data,
192+
PlatformMessageResponseCallback? callback,
196193
) {
197194
frameworkCalls.add(_codec.decodeMethodCall(data));
198-
realOnPlatformMessage(name, data, callback);
195+
realOnPlatformMessage!(name, data, callback);
199196
};
200197

201198
final Uri uri = Uri.parse('/foo/bar');
202-
FollowLink followLink;
199+
FollowLink? followLink;
203200

204201
final Link link = Link(
205202
uri: uri,
206-
builder: (BuildContext context, FollowLink followLink2) {
203+
builder: (BuildContext context, FollowLink? followLink2) {
207204
followLink = followLink2;
208205
return Container();
209206
},
@@ -218,7 +215,7 @@ void main() {
218215
engineCalls.clear();
219216
frameworkCalls.clear();
220217
clearInteractions(mock);
221-
await followLink();
218+
await followLink!();
222219

223220
// Shouldn't use url_launcher when uri is an internal route name.
224221
verifyZeroInteractions(mock);
@@ -261,8 +258,8 @@ class MockRouteInformationParser extends Mock
261258
}
262259
}
263260

264-
class MockRouterDelegate extends Mock implements RouterDelegate {
265-
MockRouterDelegate({@required this.builder});
261+
class MockRouterDelegate extends Mock implements RouterDelegate<Object> {
262+
MockRouterDelegate({required this.builder});
266263

267264
final WidgetBuilder builder;
268265

0 commit comments

Comments
 (0)