image_picker_windows_test.dart
6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:file_selector_platform_interface/file_selector_platform_interface.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart';
import 'package:image_picker_windows/image_picker_windows.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'image_picker_windows_test.mocks.dart';
@GenerateMocks(<Type>[FileSelectorPlatform])
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
// Returns the captured type groups from a mock call result, assuming that
// exactly one call was made and only the type groups were captured.
List<XTypeGroup> capturedTypeGroups(VerificationResult result) {
return result.captured.single as List<XTypeGroup>;
}
group('ImagePickerWindows', () {
late ImagePickerWindows plugin;
late MockFileSelectorPlatform mockFileSelectorPlatform;
setUp(() {
plugin = ImagePickerWindows();
mockFileSelectorPlatform = MockFileSelectorPlatform();
when(mockFileSelectorPlatform.openFile(
acceptedTypeGroups: anyNamed('acceptedTypeGroups')))
.thenAnswer((_) async => null);
when(mockFileSelectorPlatform.openFiles(
acceptedTypeGroups: anyNamed('acceptedTypeGroups')))
.thenAnswer((_) async => List<XFile>.empty());
ImagePickerWindows.fileSelector = mockFileSelectorPlatform;
});
test('registered instance', () {
ImagePickerWindows.registerWith();
expect(ImagePickerPlatform.instance, isA<ImagePickerWindows>());
});
group('images', () {
test('pickImage passes the accepted type groups correctly', () async {
await plugin.pickImage(source: ImageSource.gallery);
final VerificationResult result = verify(
mockFileSelectorPlatform.openFile(
acceptedTypeGroups: captureAnyNamed('acceptedTypeGroups')));
expect(capturedTypeGroups(result)[0].extensions,
ImagePickerWindows.imageFormats);
});
test('getImage passes the accepted type groups correctly', () async {
await plugin.getImage(source: ImageSource.gallery);
final VerificationResult result = verify(
mockFileSelectorPlatform.openFile(
acceptedTypeGroups: captureAnyNamed('acceptedTypeGroups')));
expect(capturedTypeGroups(result)[0].extensions,
ImagePickerWindows.imageFormats);
});
test('getMultiImage passes the accepted type groups correctly', () async {
await plugin.getMultiImage();
final VerificationResult result = verify(
mockFileSelectorPlatform.openFiles(
acceptedTypeGroups: captureAnyNamed('acceptedTypeGroups')));
expect(capturedTypeGroups(result)[0].extensions,
ImagePickerWindows.imageFormats);
});
test(
'getImageFromSource throws StateError when source is camera with no delegate',
() async {
await expectLater(plugin.getImageFromSource(source: ImageSource.camera),
throwsStateError);
});
test('getMultiImage passes the accepted type groups correctly', () async {
await plugin.getMultiImage();
final VerificationResult result = verify(
mockFileSelectorPlatform.openFiles(
acceptedTypeGroups: captureAnyNamed('acceptedTypeGroups')));
expect(capturedTypeGroups(result)[0].extensions,
ImagePickerWindows.imageFormats);
});
});
group('videos', () {
test('pickVideo passes the accepted type groups correctly', () async {
await plugin.pickVideo(source: ImageSource.gallery);
final VerificationResult result = verify(
mockFileSelectorPlatform.openFile(
acceptedTypeGroups: captureAnyNamed('acceptedTypeGroups')));
expect(capturedTypeGroups(result)[0].extensions,
ImagePickerWindows.videoFormats);
});
test('getVideo passes the accepted type groups correctly', () async {
await plugin.getVideo(source: ImageSource.gallery);
final VerificationResult result = verify(
mockFileSelectorPlatform.openFile(
acceptedTypeGroups: captureAnyNamed('acceptedTypeGroups')));
expect(capturedTypeGroups(result)[0].extensions,
ImagePickerWindows.videoFormats);
});
test('getVideo calls delegate when source is camera', () async {
const String fakePath = '/tmp/foo';
plugin.cameraDelegate = FakeCameraDelegate(result: XFile(fakePath));
expect((await plugin.getVideo(source: ImageSource.camera))!.path,
fakePath);
});
test('getVideo throws StateError when source is camera with no delegate',
() async {
await expectLater(
plugin.getVideo(source: ImageSource.camera), throwsStateError);
});
});
group('media', () {
test('getMedia passes the accepted type groups correctly', () async {
await plugin.getMedia(options: const MediaOptions(allowMultiple: true));
final VerificationResult result = verify(
mockFileSelectorPlatform.openFiles(
acceptedTypeGroups: captureAnyNamed('acceptedTypeGroups')));
expect(capturedTypeGroups(result)[0].extensions, <String>[
...ImagePickerWindows.imageFormats,
...ImagePickerWindows.videoFormats
]);
});
test('multiple media handles an empty path response gracefully',
() async {
expect(
await plugin.getMedia(
options: const MediaOptions(
allowMultiple: true,
),
),
<String>[]);
});
test('single media handles an empty path response gracefully', () async {
expect(
await plugin.getMedia(
options: const MediaOptions(
allowMultiple: false,
),
),
<String>[]);
});
});
});
}
class FakeCameraDelegate extends ImagePickerCameraDelegate {
FakeCameraDelegate({this.result});
XFile? result;
@override
Future<XFile?> takePhoto(
{ImagePickerCameraDelegateOptions options =
const ImagePickerCameraDelegateOptions()}) async {
return result;
}
@override
Future<XFile?> takeVideo(
{ImagePickerCameraDelegateOptions options =
const ImagePickerCameraDelegateOptions()}) async {
return result;
}
}