66
77@import image_picker;
88@import XCTest;
9+ #import < OCMock/OCMock.h>
910
1011@interface MockViewController : UIViewController
1112@property (nonatomic , retain ) UIViewController *mockPresented;
@@ -27,90 +28,152 @@ - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
2728@end
2829
2930@interface ImagePickerPluginTests : XCTestCase
31+ @property (readonly , nonatomic ) id mockUIImagePicker;
32+ @property (readonly , nonatomic ) id mockAVCaptureDevice;
3033@end
3134
3235@implementation ImagePickerPluginTests
3336
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+
3542- (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
3958 FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new ];
4059 FlutterMethodCall *call =
4160 [FlutterMethodCall methodCallWithMethodName: @" pickImage"
4261 arguments: @{@" source" : @(0 ), @" cameraDevice" : @(0 )}];
4362 [plugin handleMethodCall: call
4463 result: ^(id _Nullable r){
4564 }];
65+
4666 XCTAssertEqual ([plugin getImagePickerController ].cameraDevice ,
4767 UIImagePickerControllerCameraDeviceRear);
4868}
4969
5070- (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
5486 FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new ];
5587 FlutterMethodCall *call =
5688 [FlutterMethodCall methodCallWithMethodName: @" pickImage"
5789 arguments: @{@" source" : @(0 ), @" cameraDevice" : @(1 )}];
5890 [plugin handleMethodCall: call
5991 result: ^(id _Nullable r){
6092 }];
93+
6194 XCTAssertEqual ([plugin getImagePickerController ].cameraDevice ,
6295 UIImagePickerControllerCameraDeviceFront);
6396}
6497
6598- (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
69114 FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new ];
70115 FlutterMethodCall *call =
71116 [FlutterMethodCall methodCallWithMethodName: @" pickVideo"
72117 arguments: @{@" source" : @(0 ), @" cameraDevice" : @(0 )}];
73118 [plugin handleMethodCall: call
74119 result: ^(id _Nullable r){
75120 }];
121+
76122 XCTAssertEqual ([plugin getImagePickerController ].cameraDevice ,
77123 UIImagePickerControllerCameraDeviceRear);
78124}
79125
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
84142 FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new ];
85143 FlutterMethodCall *call =
86- [FlutterMethodCall methodCallWithMethodName: @" pickImage "
144+ [FlutterMethodCall methodCallWithMethodName: @" pickVideo "
87145 arguments: @{@" source" : @(0 ), @" cameraDevice" : @(1 )}];
88146 [plugin handleMethodCall: call
89147 result: ^(id _Nullable r){
90148 }];
91- plugin.result = ^(id result) {
92149
93- };
94- [plugin imagePickerControllerDidCancel: [plugin getImagePickerController ]];
95- [plugin imagePickerControllerDidCancel: [plugin getImagePickerController ]];
150+ XCTAssertEqual ([plugin getImagePickerController ].cameraDevice ,
151+ UIImagePickerControllerCameraDeviceFront);
96152}
97153
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]) {
100158 return ;
101159 }
102160 FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new ];
103161 FlutterMethodCall *call =
104- [FlutterMethodCall methodCallWithMethodName: @" pickVideo "
162+ [FlutterMethodCall methodCallWithMethodName: @" pickImage "
105163 arguments: @{@" source" : @(0 ), @" cameraDevice" : @(1 )}];
106164 [plugin handleMethodCall: call
107165 result: ^(id _Nullable r){
108166 }];
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 ]];
111173}
112174
113175#pragma mark - Test video duration
176+
114177- (void )testPickingVideoWithDuration {
115178 FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new ];
116179 FlutterMethodCall *call = [FlutterMethodCall
0 commit comments