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
- // @dart = 2.9
6
-
7
5
import 'dart:io' ;
8
6
import 'dart:ui' ;
9
7
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
-
15
8
import 'package:flutter/services.dart' ;
9
+ import 'package:flutter_test/flutter_test.dart' ;
10
+ import 'package:share/share.dart' ;
16
11
17
12
void main () {
18
- TestWidgetsFlutterBinding .ensureInitialized ();
13
+ TestWidgetsFlutterBinding .ensureInitialized (); // Required for MethodChannels
19
14
20
- MockMethodChannel mockChannel ;
15
+ late FakeMethodChannel fakeChannel ;
21
16
22
17
setUp (() {
23
- mockChannel = MockMethodChannel ();
24
- // Re-pipe to mockito for easier verifies .
18
+ fakeChannel = FakeMethodChannel ();
19
+ // Re-pipe to our fake to verify invocations .
25
20
Share .channel.setMockMethodCallHandler ((MethodCall call) async {
26
21
// 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);
28
23
});
29
24
});
30
25
31
26
test ('sharing empty fails' , () {
32
27
expect (
33
28
() => Share .share ('' ),
34
- throwsA (const TypeMatcher <AssertionError >()),
29
+ throwsA (isA <AssertionError >()),
35
30
);
36
- verifyZeroInteractions (mockChannel );
31
+ expect (fakeChannel.invocation, isNull );
37
32
});
38
33
39
34
test ('sharing origin sets the right params' , () async {
@@ -42,34 +37,47 @@ void main() {
42
37
subject: 'some subject to share' ,
43
38
sharePositionOrigin: const Rect .fromLTWH (1.0 , 2.0 , 3.0 , 4.0 ),
44
39
);
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
+ );
53
54
});
54
55
55
56
test ('sharing empty file fails' , () {
56
57
expect (
57
58
() => Share .shareFiles (['' ]),
58
- throwsA (const TypeMatcher <AssertionError >()),
59
+ throwsA (isA <AssertionError >()),
59
60
);
60
- verifyZeroInteractions (mockChannel );
61
+ expect (fakeChannel.invocation, isNull );
61
62
});
62
63
63
64
test ('sharing file sets correct mimeType' , () async {
64
65
final String path = 'tempfile-83649a.png' ;
65
66
final File file = File (path);
66
67
try {
67
68
file.createSync ();
69
+
68
70
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
+ );
73
81
} finally {
74
82
file.deleteSync ();
75
83
}
@@ -80,15 +88,30 @@ void main() {
80
88
final File file = File (path);
81
89
try {
82
90
file.createSync ();
91
+
83
92
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
+ );
88
103
} finally {
89
104
file.deleteSync ();
90
105
}
91
106
});
92
107
}
93
108
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