Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 09d29ea

Browse files
committed
Applied feedback from PR
1 parent 2b5555a commit 09d29ea

File tree

3 files changed

+63
-58
lines changed

3 files changed

+63
-58
lines changed

packages/image_picker/image_picker/example/ios/RunnerTests/ImagePickerPluginTests.m

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ - (UIViewController *)presentedViewController {
2222

2323
@end
2424

25+
// Set a long timeout to avoid flake due to slow CI.
26+
static const NSTimeInterval kTimeout = 30.0;
27+
2528
@interface ImagePickerPluginTests : XCTestCase
2629
@property(readonly, nonatomic) id mockUIImagePicker;
2730
@property(readonly, nonatomic) id mockAVCaptureDevice;
@@ -146,6 +149,33 @@ - (void)testPluginPickVideoDeviceFront {
146149
UIImagePickerControllerCameraDeviceFront);
147150
}
148151

152+
- (void)testPickMultiImageShouldUseUIImagePickerControllerOnPreiOS14 {
153+
if (@available(iOS 14, *)) {
154+
return;
155+
}
156+
157+
id photoLibrary = OCMClassMock([PHPhotoLibrary class]);
158+
OCMStub(ClassMethod([photoLibrary authorizationStatus]))
159+
.andReturn(PHAuthorizationStatusAuthorized);
160+
161+
FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new];
162+
[plugin setImagePickerControllerOverride: _mockUIImagePicker];
163+
FlutterMethodCall *call =
164+
[FlutterMethodCall methodCallWithMethodName:@"pickMultiImage"
165+
arguments:@{
166+
@"maxWidth" : @(100),
167+
@"maxHeight" : @(200),
168+
@"imageQuality" : @(50),
169+
}];
170+
171+
[plugin handleMethodCall:call
172+
result:^(id _Nullable r){
173+
}];
174+
175+
OCMVerify(times(1),
176+
[self->_mockUIImagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]);
177+
}
178+
149179
#pragma mark - Test camera devices, no op on simulators
150180

151181
- (void)testPluginPickImageDeviceCancelClickMultipleTimes {
@@ -167,61 +197,6 @@ - (void)testPluginPickImageDeviceCancelClickMultipleTimes {
167197
[plugin imagePickerControllerDidCancel:[plugin getImagePickerController]];
168198
}
169199

170-
#pragma mark - Test that arguments and results are set for all method calls
171-
- (void)testPickImageShouldSetArgumentsAndResult {
172-
FlutterResult expectedResult = ^(id _Nullable r) {
173-
};
174-
NSDictionary *expectedArguments =
175-
@{@"source" : @(1), @"maxWidth" : @(200), @"maxHeight" : @(200), @"imageQuality" : @(50)};
176-
177-
// Run test
178-
FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new];
179-
FlutterMethodCall *call = [FlutterMethodCall methodCallWithMethodName:@"pickImage"
180-
arguments:expectedArguments];
181-
182-
[plugin handleMethodCall:call result:expectedResult];
183-
184-
XCTAssertEqual(plugin.result, expectedResult);
185-
XCTAssertEqual(plugin.arguments, expectedArguments);
186-
}
187-
188-
- (void)testPickMultiImageShouldSetArgumentsAndResult {
189-
FlutterResult expectedResult = ^(id _Nullable r) {
190-
};
191-
NSDictionary *expectedArguments =
192-
@{@"maxWidth" : @(200), @"maxHeight" : @(200), @"imageQuality" : @(50)};
193-
194-
// Run test
195-
FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new];
196-
FlutterMethodCall *call = [FlutterMethodCall methodCallWithMethodName:@"pickMultiImage"
197-
arguments:expectedArguments];
198-
199-
[plugin handleMethodCall:call result:expectedResult];
200-
201-
XCTAssertEqual(plugin.result, expectedResult);
202-
XCTAssertEqual(plugin.arguments, expectedArguments);
203-
}
204-
205-
- (void)testPickVideoShouldSetArgumentsAndResult {
206-
// AVAuthorizationStatusAuthorized is supported
207-
OCMStub([_mockAVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo])
208-
.andReturn(AVAuthorizationStatusAuthorized);
209-
210-
FlutterResult expectedResult = ^(id _Nullable r) {
211-
};
212-
NSDictionary *expectedArguments = @{@"source" : @(1), @"maxDuration" : @(200)};
213-
214-
// Run test
215-
FLTImagePickerPlugin *plugin = [FLTImagePickerPlugin new];
216-
FlutterMethodCall *call = [FlutterMethodCall methodCallWithMethodName:@"pickVideo"
217-
arguments:expectedArguments];
218-
219-
[plugin handleMethodCall:call result:expectedResult];
220-
221-
XCTAssertEqual(plugin.result, expectedResult);
222-
XCTAssertEqual(plugin.arguments, expectedArguments);
223-
}
224-
225200
#pragma mark - Test video duration
226201

227202
- (void)testPickingVideoWithDuration {

packages/image_picker/image_picker/ios/Classes/FLTImagePickerPlugin.m

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#import "FLTPHPickerSaveImageToPathOperation.h"
1919

2020
@interface FLTImagePickerPlugin () <UINavigationControllerDelegate,
21-
UIImagePickerControllerDelegate,
21+
UIImagePickerControllerDelegate,
2222
PHPickerViewControllerDelegate,
2323
UIAdaptivePresentationControllerDelegate>
2424

@@ -36,6 +36,7 @@ @interface FLTImagePickerPlugin () <UINavigationControllerDelegate,
3636
typedef NS_ENUM(NSInteger, ImagePickerClassType) { UIImagePickerClassType, PHPickerClassType };
3737

3838
@implementation FLTImagePickerPlugin {
39+
UIImagePickerController *_imagePickerControllerOverride;
3940
UIImagePickerController *_imagePickerController;
4041
}
4142

@@ -47,10 +48,32 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
4748
[registrar addMethodCallDelegate:instance channel:channel];
4849
}
4950

51+
/**
52+
* Initializes the _imagePickerController member with a new instance of the
53+
* UIImagePickerController class.
54+
*
55+
* A new instance of the UIImagePickerController is created every time the
56+
* initImagePickerController method is called. For testing purposes this can
57+
* be overriden using the setImagePickerControllerOverride method, in which
58+
* case the set instance of the UIImagePickerController is used to initialize
59+
* the _imagePickerController member.
60+
*/
61+
- (void)initImagePickerController {
62+
if (_imagePickerControllerOverride) {
63+
_imagePickerController = _imagePickerControllerOverride;
64+
} else {
65+
_imagePickerController = [[UIImagePickerController alloc] init];
66+
}
67+
}
68+
5069
- (UIImagePickerController *)getImagePickerController {
5170
return _imagePickerController;
5271
}
5372

73+
- (void)setImagePickerControllerOverride:(UIImagePickerController *)imagePickerController {
74+
_imagePickerControllerOverride = imagePickerController;
75+
}
76+
5477
- (UIViewController *)viewControllerWithWindow:(UIWindow *)window {
5578
UIWindow *windowToUse = window;
5679
if (windowToUse == nil) {
@@ -106,7 +129,7 @@ - (void)pickImageWithUIImagePicker {
106129
}
107130

108131
- (void)launchUIImagePickerWithSource:(int)imageSource {
109-
_imagePickerController = [[UIImagePickerController alloc] init];
132+
[self initImagePickerController];
110133
_imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
111134
_imagePickerController.delegate = self;
112135
_imagePickerController.mediaTypes = @[ (NSString *)kUTTypeImage ];
@@ -160,7 +183,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
160183
[self launchUIImagePickerWithSource:SOURCE_GALLERY];
161184
}
162185
} else if ([@"pickVideo" isEqualToString:call.method]) {
163-
_imagePickerController = [[UIImagePickerController alloc] init];
186+
[self initImagePickerController];
164187
_imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
165188
_imagePickerController.delegate = self;
166189
_imagePickerController.mediaTypes = @[

packages/image_picker/image_picker/ios/Classes/FLTImagePickerPlugin_Test.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,11 @@
4040
*/
4141
-(void)imagePickerControllerDidCancel : (UIImagePickerController *)picker;
4242

43+
/**
44+
* Sets the UIImagePickerController instance that should be used by the
45+
* image_picker plugin.
46+
*
47+
* Should be used for testing purposes only.
48+
*/
49+
- (void)setImagePickerControllerOverride:(UIImagePickerController *)imagePickerController;
4350
@end

0 commit comments

Comments
 (0)