6
6
7
7
@import image_picker;
8
8
@import XCTest;
9
+ #import < OCMock/OCMock.h>
9
10
10
11
@interface MockViewController : UIViewController
11
12
@property (nonatomic , retain ) UIViewController *mockPresented;
@@ -27,90 +28,152 @@ - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
27
28
@end
28
29
29
30
@interface ImagePickerPluginTests : XCTestCase
31
+ @property (readonly , nonatomic ) id mockUIImagePicker;
32
+ @property (readonly , nonatomic ) id mockAVCaptureDevice;
30
33
@end
31
34
32
35
@implementation ImagePickerPluginTests
33
36
34
- #pragma mark - Test camera devices, no op on simulators
37
+ - (void )setUp {
38
+ _mockUIImagePicker = OCMClassMock ([UIImagePickerController class ]);
39
+ _mockAVCaptureDevice = OCMClassMock ([AVCaptureDevice class ]);
40
+ }
41
+
35
42
- (void )testPluginPickImageDeviceBack {
36
- if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
37
- return ;
38
- }
43
+ // UIImagePickerControllerSourceTypeCamera is supported
44
+ OCMStub (ClassMethod (
45
+ [_mockUIImagePicker isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]))
46
+ .andReturn (YES );
47
+
48
+ // UIImagePickerControllerCameraDeviceRear is supported
49
+ OCMStub (ClassMethod (
50
+ [_mockUIImagePicker isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceRear]))
51
+ .andReturn (YES );
52
+
53
+ // AVAuthorizationStatusAuthorized is supported
54
+ OCMStub ([_mockAVCaptureDevice authorizationStatusForMediaType: AVMediaTypeVideo])
55
+ .andReturn (AVAuthorizationStatusAuthorized);
56
+
57
+ // Run test
39
58
FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new ];
40
59
FlutterMethodCall *call =
41
60
[FlutterMethodCall methodCallWithMethodName: @" pickImage"
42
61
arguments: @{@" source" : @(0 ), @" cameraDevice" : @(0 )}];
43
62
[plugin handleMethodCall: call
44
63
result: ^(id _Nullable r){
45
64
}];
65
+
46
66
XCTAssertEqual ([plugin getImagePickerController ].cameraDevice ,
47
67
UIImagePickerControllerCameraDeviceRear);
48
68
}
49
69
50
70
- (void )testPluginPickImageDeviceFront {
51
- if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
52
- return ;
53
- }
71
+ // UIImagePickerControllerSourceTypeCamera is supported
72
+ OCMStub (ClassMethod (
73
+ [_mockUIImagePicker isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]))
74
+ .andReturn (YES );
75
+
76
+ // UIImagePickerControllerCameraDeviceFront is supported
77
+ OCMStub (ClassMethod ([_mockUIImagePicker
78
+ isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceFront]))
79
+ .andReturn (YES );
80
+
81
+ // AVAuthorizationStatusAuthorized is supported
82
+ OCMStub ([_mockAVCaptureDevice authorizationStatusForMediaType: AVMediaTypeVideo])
83
+ .andReturn (AVAuthorizationStatusAuthorized);
84
+
85
+ // Run test
54
86
FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new ];
55
87
FlutterMethodCall *call =
56
88
[FlutterMethodCall methodCallWithMethodName: @" pickImage"
57
89
arguments: @{@" source" : @(0 ), @" cameraDevice" : @(1 )}];
58
90
[plugin handleMethodCall: call
59
91
result: ^(id _Nullable r){
60
92
}];
93
+
61
94
XCTAssertEqual ([plugin getImagePickerController ].cameraDevice ,
62
95
UIImagePickerControllerCameraDeviceFront);
63
96
}
64
97
65
98
- (void )testPluginPickVideoDeviceBack {
66
- if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
67
- return ;
68
- }
99
+ // UIImagePickerControllerSourceTypeCamera is supported
100
+ OCMStub (ClassMethod (
101
+ [_mockUIImagePicker isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]))
102
+ .andReturn (YES );
103
+
104
+ // UIImagePickerControllerCameraDeviceRear is supported
105
+ OCMStub (ClassMethod (
106
+ [_mockUIImagePicker isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceRear]))
107
+ .andReturn (YES );
108
+
109
+ // AVAuthorizationStatusAuthorized is supported
110
+ OCMStub ([_mockAVCaptureDevice authorizationStatusForMediaType: AVMediaTypeVideo])
111
+ .andReturn (AVAuthorizationStatusAuthorized);
112
+
113
+ // Run test
69
114
FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new ];
70
115
FlutterMethodCall *call =
71
116
[FlutterMethodCall methodCallWithMethodName: @" pickVideo"
72
117
arguments: @{@" source" : @(0 ), @" cameraDevice" : @(0 )}];
73
118
[plugin handleMethodCall: call
74
119
result: ^(id _Nullable r){
75
120
}];
121
+
76
122
XCTAssertEqual ([plugin getImagePickerController ].cameraDevice ,
77
123
UIImagePickerControllerCameraDeviceRear);
78
124
}
79
125
80
- - (void )testPluginPickImageDeviceCancelClickMultipleTimes {
81
- if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
82
- return ;
83
- }
126
+ - (void )testPluginPickVideoDeviceFront {
127
+ // UIImagePickerControllerSourceTypeCamera is supported
128
+ OCMStub (ClassMethod (
129
+ [_mockUIImagePicker isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]))
130
+ .andReturn (YES );
131
+
132
+ // UIImagePickerControllerCameraDeviceFront is supported
133
+ OCMStub (ClassMethod ([_mockUIImagePicker
134
+ isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceFront]))
135
+ .andReturn (YES );
136
+
137
+ // AVAuthorizationStatusAuthorized is supported
138
+ OCMStub ([_mockAVCaptureDevice authorizationStatusForMediaType: AVMediaTypeVideo])
139
+ .andReturn (AVAuthorizationStatusAuthorized);
140
+
141
+ // Run test
84
142
FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new ];
85
143
FlutterMethodCall *call =
86
- [FlutterMethodCall methodCallWithMethodName: @" pickImage "
144
+ [FlutterMethodCall methodCallWithMethodName: @" pickVideo "
87
145
arguments: @{@" source" : @(0 ), @" cameraDevice" : @(1 )}];
88
146
[plugin handleMethodCall: call
89
147
result: ^(id _Nullable r){
90
148
}];
91
- plugin.result = ^(id result) {
92
149
93
- };
94
- [plugin imagePickerControllerDidCancel: [plugin getImagePickerController ]];
95
- [plugin imagePickerControllerDidCancel: [plugin getImagePickerController ]];
150
+ XCTAssertEqual ([plugin getImagePickerController ].cameraDevice ,
151
+ UIImagePickerControllerCameraDeviceFront);
96
152
}
97
153
98
- - (void )testPluginPickVideoDeviceFront {
99
- if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
154
+ #pragma mark - Test camera devices, no op on simulators
155
+
156
+ - (void )testPluginPickImageDeviceCancelClickMultipleTimes {
157
+ if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
100
158
return ;
101
159
}
102
160
FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new ];
103
161
FlutterMethodCall *call =
104
- [FlutterMethodCall methodCallWithMethodName: @" pickVideo "
162
+ [FlutterMethodCall methodCallWithMethodName: @" pickImage "
105
163
arguments: @{@" source" : @(0 ), @" cameraDevice" : @(1 )}];
106
164
[plugin handleMethodCall: call
107
165
result: ^(id _Nullable r){
108
166
}];
109
- XCTAssertEqual ([plugin getImagePickerController ].cameraDevice ,
110
- UIImagePickerControllerCameraDeviceFront);
167
+ plugin.result = ^(id result) {
168
+
169
+ };
170
+ // To ensure the flow does not crash by multiple cancel call
171
+ [plugin imagePickerControllerDidCancel: [plugin getImagePickerController ]];
172
+ [plugin imagePickerControllerDidCancel: [plugin getImagePickerController ]];
111
173
}
112
174
113
175
#pragma mark - Test video duration
176
+
114
177
- (void )testPickingVideoWithDuration {
115
178
FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new ];
116
179
FlutterMethodCall *call = [FlutterMethodCall
0 commit comments