Skip to content

Commit 8464890

Browse files
authored
[share] Migrate unit tests to null-safety. (flutter#3660)
1 parent 72feefd commit 8464890

File tree

3 files changed

+61
-36
lines changed

3 files changed

+61
-36
lines changed

packages/share/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.1
2+
3+
* Migrate unit tests to sound null safety.
4+
15
## 2.0.0
26

37
* Migrate to null safety.

packages/share/pubspec.yaml

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: share
22
description: Flutter plugin for sharing content via the platform share UI, using
33
the ACTION_SEND intent on Android and UIActivityViewController on iOS.
44
homepage: https://github.com/flutter/plugins/tree/master/packages/share
5-
version: 2.0.0
5+
version: 2.0.1
66

77
flutter:
88
plugin:
@@ -20,8 +20,6 @@ dependencies:
2020
sdk: flutter
2121

2222
dev_dependencies:
23-
test: ^1.16.3
24-
mockito: ^5.0.0-nullsafety.7
2523
flutter_test:
2624
sdk: flutter
2725
integration_test:

packages/share/test/share_test.dart

+56-33
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,33 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
import 'dart:io';
86
import 'dart:ui';
97

10-
import 'package:flutter_test/flutter_test.dart' show TestWidgetsFlutterBinding;
11-
import 'package:mockito/mockito.dart';
12-
import 'package:share/share.dart';
13-
import 'package:test/test.dart';
14-
158
import 'package:flutter/services.dart';
9+
import 'package:flutter_test/flutter_test.dart';
10+
import 'package:share/share.dart';
1611

1712
void main() {
18-
TestWidgetsFlutterBinding.ensureInitialized();
13+
TestWidgetsFlutterBinding.ensureInitialized(); // Required for MethodChannels
1914

20-
MockMethodChannel mockChannel;
15+
late FakeMethodChannel fakeChannel;
2116

2217
setUp(() {
23-
mockChannel = MockMethodChannel();
24-
// Re-pipe to mockito for easier verifies.
18+
fakeChannel = FakeMethodChannel();
19+
// Re-pipe to our fake to verify invocations.
2520
Share.channel.setMockMethodCallHandler((MethodCall call) async {
2621
// The explicit type can be void as the only method call has a return type of void.
27-
await mockChannel.invokeMethod<void>(call.method, call.arguments);
22+
await fakeChannel.invokeMethod<void>(call.method, call.arguments);
2823
});
2924
});
3025

3126
test('sharing empty fails', () {
3227
expect(
3328
() => Share.share(''),
34-
throwsA(const TypeMatcher<AssertionError>()),
29+
throwsA(isA<AssertionError>()),
3530
);
36-
verifyZeroInteractions(mockChannel);
31+
expect(fakeChannel.invocation, isNull);
3732
});
3833

3934
test('sharing origin sets the right params', () async {
@@ -42,34 +37,47 @@ void main() {
4237
subject: 'some subject to share',
4338
sharePositionOrigin: const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0),
4439
);
45-
verify(mockChannel.invokeMethod<void>('share', <String, dynamic>{
46-
'text': 'some text to share',
47-
'subject': 'some subject to share',
48-
'originX': 1.0,
49-
'originY': 2.0,
50-
'originWidth': 3.0,
51-
'originHeight': 4.0,
52-
}));
40+
41+
expect(
42+
fakeChannel.invocation,
43+
equals({
44+
'share': {
45+
'text': 'some text to share',
46+
'subject': 'some subject to share',
47+
'originX': 1.0,
48+
'originY': 2.0,
49+
'originWidth': 3.0,
50+
'originHeight': 4.0,
51+
}
52+
}),
53+
);
5354
});
5455

5556
test('sharing empty file fails', () {
5657
expect(
5758
() => Share.shareFiles(['']),
58-
throwsA(const TypeMatcher<AssertionError>()),
59+
throwsA(isA<AssertionError>()),
5960
);
60-
verifyZeroInteractions(mockChannel);
61+
expect(fakeChannel.invocation, isNull);
6162
});
6263

6364
test('sharing file sets correct mimeType', () async {
6465
final String path = 'tempfile-83649a.png';
6566
final File file = File(path);
6667
try {
6768
file.createSync();
69+
6870
await Share.shareFiles([path]);
69-
verify(mockChannel.invokeMethod('shareFiles', <String, dynamic>{
70-
'paths': [path],
71-
'mimeTypes': ['image/png'],
72-
}));
71+
72+
expect(
73+
fakeChannel.invocation,
74+
equals({
75+
'shareFiles': {
76+
'paths': [path],
77+
'mimeTypes': ['image/png'],
78+
}
79+
}),
80+
);
7381
} finally {
7482
file.deleteSync();
7583
}
@@ -80,15 +88,30 @@ void main() {
8088
final File file = File(path);
8189
try {
8290
file.createSync();
91+
8392
await Share.shareFiles([path], mimeTypes: ['*/*']);
84-
verify(mockChannel.invokeMethod('shareFiles', <String, dynamic>{
85-
'paths': [file.path],
86-
'mimeTypes': ['*/*'],
87-
}));
93+
94+
expect(
95+
fakeChannel.invocation,
96+
equals({
97+
'shareFiles': {
98+
'paths': [file.path],
99+
'mimeTypes': ['*/*'],
100+
}
101+
}),
102+
);
88103
} finally {
89104
file.deleteSync();
90105
}
91106
});
92107
}
93108

94-
class MockMethodChannel extends Mock implements MethodChannel {}
109+
class FakeMethodChannel extends Fake implements MethodChannel {
110+
Map<String, dynamic>? invocation;
111+
112+
@override
113+
Future<T?> invokeMethod<T>(String method, [dynamic arguments]) async {
114+
this.invocation = {method: arguments};
115+
return null;
116+
}
117+
}

0 commit comments

Comments
 (0)