@@ -17,6 +17,10 @@ @interface FLTImagePickerPlugin (Testing)
1717@property (nonatomic , strong ) UIWindow *interactionBlockerWindow;
1818@property (nonatomic , weak ) UIWindow *previousKeyWindow;
1919- (void )removeInteractionBlocker ;
20+ - (void )showCamera : (UIImagePickerControllerCameraDevice)device
21+ withImagePicker : (UIImagePickerController *)imagePickerController ;
22+ - (UIViewController *)presentingViewControllerForImagePickerInNewWindow ;
23+ - (NSNumber *)getDesiredImageQuality : (NSNumber *)imageQuality ;
2024@end
2125
2226@interface MockViewController : UIViewController
@@ -815,4 +819,357 @@ - (void)testPickVideoSetsCurrentRepresentationMode API_AVAILABLE(ios(14)) {
815819 OCMVerifyAll (mockPickerViewController);
816820}
817821
822+ - (void )testPickImageInvalidResultWhenMultiplePathsReturned {
823+ FLTImagePickerPlugin *plugin =
824+ [[FLTImagePickerPlugin alloc ] initWithViewProvider: [[StubViewProvider alloc ] init ]];
825+ XCTestExpectation *resultExpectation = [self expectationWithDescription: @" invalid_result" ];
826+ __block NSInteger completionCount = 0 ;
827+ [plugin pickImageWithSource: [FLTSourceSpecification makeWithType: FLTSourceTypeGallery
828+ camera: FLTSourceCameraRear]
829+ maxSize: [[FLTMaxSize alloc ] init ]
830+ quality: nil
831+ fullMetadata: NO
832+ completion: ^(NSString *result, FlutterError *error) {
833+ completionCount += 1 ;
834+ if (completionCount == 1 ) {
835+ // Current behavior: the invalid_result error is sent, then the adapter
836+ // also forwards paths.firstObject.
837+ XCTAssertNil (result);
838+ XCTAssertEqualObjects (error.code , @" invalid_result" );
839+ } else {
840+ XCTAssertEqualObjects (result, @" a" );
841+ [resultExpectation fulfill ];
842+ }
843+ }];
844+ [plugin sendCallResultWithSavedPathList: @[ @" a" , @" b" ]];
845+ [self waitForExpectationsWithTimeout: 30 handler: nil ];
846+ XCTAssertEqual (completionCount, 2 );
847+ }
848+
849+ - (void )testPickVideoInvalidResultWhenMultiplePathsReturned {
850+ FLTImagePickerPlugin *plugin =
851+ [[FLTImagePickerPlugin alloc ] initWithViewProvider: [[StubViewProvider alloc ] init ]];
852+ XCTestExpectation *resultExpectation = [self expectationWithDescription: @" invalid_result" ];
853+ __block NSInteger completionCount = 0 ;
854+ [plugin pickVideoWithSource: [FLTSourceSpecification makeWithType: FLTSourceTypeGallery
855+ camera: FLTSourceCameraRear]
856+ maxDuration: nil
857+ completion: ^(NSString *result, FlutterError *error) {
858+ completionCount += 1 ;
859+ if (completionCount == 1 ) {
860+ XCTAssertNil (result);
861+ XCTAssertEqualObjects (error.code , @" invalid_result" );
862+ } else {
863+ XCTAssertEqualObjects (result, @" a" );
864+ [resultExpectation fulfill ];
865+ }
866+ }];
867+ [plugin sendCallResultWithSavedPathList: @[ @" a" , @" b" ]];
868+ [self waitForExpectationsWithTimeout: 30 handler: nil ];
869+ XCTAssertEqual (completionCount, 2 );
870+ }
871+
872+ - (void )testPHPickerCancelSendsEmptyPathList API_AVAILABLE(ios(14 )) {
873+ id mockPickerViewController = OCMClassMock ([PHPickerViewController class ]);
874+ FLTImagePickerPlugin *plugin =
875+ [[FLTImagePickerPlugin alloc ] initWithViewProvider: [[StubViewProvider alloc ] init ]];
876+ XCTestExpectation *resultExpectation = [self expectationWithDescription: @" cancelled" ];
877+ plugin.callContext = [[FLTImagePickerMethodCallContext alloc ]
878+ initWithResult: ^(NSArray <NSString *> *result, FlutterError *error) {
879+ XCTAssertEqualObjects (result, @[]);
880+ XCTAssertNil (error);
881+ [resultExpectation fulfill ];
882+ }];
883+ [plugin picker: mockPickerViewController didFinishPicking: @[]];
884+ [self waitForExpectationsWithTimeout: 30 handler: nil ];
885+ }
886+
887+ - (void )testPresentationControllerDidDismissSendsEmptyPathList {
888+ FLTImagePickerPlugin *plugin =
889+ [[FLTImagePickerPlugin alloc ] initWithViewProvider: [[StubViewProvider alloc ] init ]];
890+ XCTestExpectation *resultExpectation = [self expectationWithDescription: @" dismissed" ];
891+ plugin.callContext = [[FLTImagePickerMethodCallContext alloc ]
892+ initWithResult: ^(NSArray <NSString *> *result, FlutterError *error) {
893+ XCTAssertEqualObjects (result, @[]);
894+ XCTAssertNil (error);
895+ [resultExpectation fulfill ];
896+ }];
897+ id mockPresentationController = OCMClassMock ([UIPresentationController class ]);
898+ [plugin presentationControllerDidDismiss: mockPresentationController];
899+ [self waitForExpectationsWithTimeout: 30 handler: nil ];
900+ }
901+
902+ - (void )testDesiredImageQualityClampsOutOfRangeValues {
903+ FLTImagePickerPlugin *plugin =
904+ [[FLTImagePickerPlugin alloc ] initWithViewProvider: [[StubViewProvider alloc ] init ]];
905+ XCTAssertEqualWithAccuracy ([plugin getDesiredImageQuality: @(-1 )].doubleValue , 1.0 , 0.0001 );
906+ }
907+
908+ - (void )testDesiredImageQualityScalesValidPercent {
909+ FLTImagePickerPlugin *plugin =
910+ [[FLTImagePickerPlugin alloc ] initWithViewProvider: [[StubViewProvider alloc ] init ]];
911+ XCTAssertEqualWithAccuracy ([plugin getDesiredImageQuality: @50 ].doubleValue , 0.5 , 0.0001 );
912+ }
913+
914+ - (void )testDesiredImageQualityOver100IsClamped {
915+ FLTImagePickerPlugin *plugin =
916+ [[FLTImagePickerPlugin alloc ] initWithViewProvider: [[StubViewProvider alloc ] init ]];
917+ XCTAssertEqualWithAccuracy ([plugin getDesiredImageQuality: @150 ].doubleValue , 1.0 , 0.0001 );
918+ }
919+
920+ - (void )testCameraAccessDeniedReturnsError {
921+ id mockAVCaptureDevice = OCMClassMock ([AVCaptureDevice class ]);
922+ OCMStub ([mockAVCaptureDevice authorizationStatusForMediaType: AVMediaTypeVideo])
923+ .andReturn (AVAuthorizationStatusDenied);
924+
925+ FLTImagePickerPlugin *plugin =
926+ [[FLTImagePickerPlugin alloc ] initWithViewProvider: [[StubViewProvider alloc ] init ]];
927+ [plugin setImagePickerControllerOverrides: @[ [[UIImagePickerController alloc ] init ] ]];
928+
929+ XCTestExpectation *resultExpectation = [self expectationWithDescription: @" denied" ];
930+ [plugin pickImageWithSource: [FLTSourceSpecification makeWithType: FLTSourceTypeCamera
931+ camera: FLTSourceCameraRear]
932+ maxSize: [[FLTMaxSize alloc ] init ]
933+ quality: nil
934+ fullMetadata: YES
935+ completion: ^(NSString *result, FlutterError *error) {
936+ XCTAssertEqualObjects (error.code , @" camera_access_denied" );
937+ [resultExpectation fulfill ];
938+ }];
939+ [self waitForExpectationsWithTimeout: 30 handler: nil ];
940+ }
941+
942+ - (void )testCameraAccessRestrictedReturnsError {
943+ id mockAVCaptureDevice = OCMClassMock ([AVCaptureDevice class ]);
944+ OCMStub ([mockAVCaptureDevice authorizationStatusForMediaType: AVMediaTypeVideo])
945+ .andReturn (AVAuthorizationStatusRestricted);
946+
947+ FLTImagePickerPlugin *plugin =
948+ [[FLTImagePickerPlugin alloc ] initWithViewProvider: [[StubViewProvider alloc ] init ]];
949+ [plugin setImagePickerControllerOverrides: @[ [[UIImagePickerController alloc ] init ] ]];
950+
951+ XCTestExpectation *resultExpectation = [self expectationWithDescription: @" restricted" ];
952+ [plugin pickImageWithSource: [FLTSourceSpecification makeWithType: FLTSourceTypeCamera
953+ camera: FLTSourceCameraRear]
954+ maxSize: [[FLTMaxSize alloc ] init ]
955+ quality: nil
956+ fullMetadata: YES
957+ completion: ^(NSString *result, FlutterError *error) {
958+ XCTAssertEqualObjects (error.code , @" camera_access_restricted" );
959+ [resultExpectation fulfill ];
960+ }];
961+ [self waitForExpectationsWithTimeout: 30 handler: nil ];
962+ }
963+
964+ - (void )testCameraAccessNotDeterminedDenied {
965+ id mockAVCaptureDevice = OCMClassMock ([AVCaptureDevice class ]);
966+ OCMStub ([mockAVCaptureDevice authorizationStatusForMediaType: AVMediaTypeVideo])
967+ .andReturn (AVAuthorizationStatusNotDetermined);
968+ OCMStub ([mockAVCaptureDevice requestAccessForMediaType: AVMediaTypeVideo
969+ completionHandler: OCMOCK_ANY ])
970+ .andDo (^(NSInvocation *invocation) {
971+ void (^handler)(BOOL granted);
972+ [invocation getArgument: &handler atIndex: 3 ];
973+ handler (NO );
974+ });
975+
976+ FLTImagePickerPlugin *plugin =
977+ [[FLTImagePickerPlugin alloc ] initWithViewProvider: [[StubViewProvider alloc ] init ]];
978+ [plugin setImagePickerControllerOverrides: @[ [[UIImagePickerController alloc ] init ] ]];
979+
980+ XCTestExpectation *resultExpectation = [self expectationWithDescription: @" request denied" ];
981+ [plugin pickImageWithSource: [FLTSourceSpecification makeWithType: FLTSourceTypeCamera
982+ camera: FLTSourceCameraRear]
983+ maxSize: [[FLTMaxSize alloc ] init ]
984+ quality: nil
985+ fullMetadata: YES
986+ completion: ^(NSString *result, FlutterError *error) {
987+ XCTAssertEqualObjects (error.code , @" camera_access_denied" );
988+ [resultExpectation fulfill ];
989+ }];
990+ [self waitForExpectationsWithTimeout: 30 handler: nil ];
991+ }
992+
993+ - (void )testCameraAccessNotDeterminedGrantedPresentsCamera {
994+ id mockUIImagePicker = OCMClassMock ([UIImagePickerController class ]);
995+ id mockAVCaptureDevice = OCMClassMock ([AVCaptureDevice class ]);
996+ OCMStub (ClassMethod (
997+ [mockUIImagePicker isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]))
998+ .andReturn (YES );
999+ OCMStub (ClassMethod (
1000+ [mockUIImagePicker isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceRear]))
1001+ .andReturn (YES );
1002+ OCMStub ([mockAVCaptureDevice authorizationStatusForMediaType: AVMediaTypeVideo])
1003+ .andReturn (AVAuthorizationStatusNotDetermined);
1004+ OCMStub ([mockAVCaptureDevice requestAccessForMediaType: AVMediaTypeVideo
1005+ completionHandler: OCMOCK_ANY ])
1006+ .andDo (^(NSInvocation *invocation) {
1007+ void (^handler)(BOOL granted);
1008+ [invocation getArgument: &handler atIndex: 3 ];
1009+ handler (YES );
1010+ });
1011+
1012+ FLTImagePickerPlugin *plugin =
1013+ [[FLTImagePickerPlugin alloc ] initWithViewProvider: [[StubViewProvider alloc ] init ]];
1014+ UIImagePickerController *controller = [[UIImagePickerController alloc ] init ];
1015+ [plugin setImagePickerControllerOverrides: @[ controller ]];
1016+
1017+ XCTestExpectation *presented = [self expectationWithDescription: @" camera presented" ];
1018+ [plugin pickImageWithSource: [FLTSourceSpecification makeWithType: FLTSourceTypeCamera
1019+ camera: FLTSourceCameraRear]
1020+ maxSize: [[FLTMaxSize alloc ] init ]
1021+ quality: nil
1022+ fullMetadata: YES
1023+ completion: ^(NSString *result, FlutterError *error){
1024+ }];
1025+ dispatch_async (dispatch_get_main_queue (), ^{
1026+ XCTAssertEqual (controller.sourceType , UIImagePickerControllerSourceTypeCamera);
1027+ [presented fulfill ];
1028+ });
1029+ [self waitForExpectationsWithTimeout: 30 handler: nil ];
1030+ }
1031+
1032+ - (void )testShowCameraWhenUnavailableSendsNilPathList {
1033+ id mockUIImagePicker = OCMClassMock ([UIImagePickerController class ]);
1034+ id mockAVCaptureDevice = OCMClassMock ([AVCaptureDevice class ]);
1035+ OCMStub (ClassMethod (
1036+ [mockUIImagePicker isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]))
1037+ .andReturn (NO );
1038+ OCMStub ([mockAVCaptureDevice authorizationStatusForMediaType: AVMediaTypeVideo])
1039+ .andReturn (AVAuthorizationStatusAuthorized);
1040+
1041+ FLTImagePickerPlugin *plugin =
1042+ [[FLTImagePickerPlugin alloc ] initWithViewProvider: [[StubViewProvider alloc ] init ]];
1043+ [plugin setImagePickerControllerOverrides: @[ [[UIImagePickerController alloc ] init ] ]];
1044+
1045+ XCTestExpectation *resultExpectation = [self expectationWithDescription: @" unavailable" ];
1046+ [plugin pickImageWithSource: [FLTSourceSpecification makeWithType: FLTSourceTypeCamera
1047+ camera: FLTSourceCameraRear]
1048+ maxSize: [[FLTMaxSize alloc ] init ]
1049+ quality: nil
1050+ fullMetadata: YES
1051+ completion: ^(NSString *result, FlutterError *error) {
1052+ XCTAssertNil (result);
1053+ XCTAssertNil (error);
1054+ [resultExpectation fulfill ];
1055+ }];
1056+ [self waitForExpectationsWithTimeout: 30 handler: nil ];
1057+ }
1058+
1059+ - (void )testShowCameraReturnsEarlyWhenAlreadyBeingPresented {
1060+ id mockPicker = OCMClassMock ([UIImagePickerController class ]);
1061+ OCMStub ([mockPicker isBeingPresented ]).andReturn (YES );
1062+
1063+ FLTImagePickerPlugin *plugin =
1064+ [[FLTImagePickerPlugin alloc ] initWithViewProvider: [[StubViewProvider alloc ] init ]];
1065+ [plugin showCamera: UIImagePickerControllerCameraDeviceRear withImagePicker: mockPicker];
1066+ OCMVerify (times (0 ), [mockPicker setSourceType: UIImagePickerControllerSourceTypeCamera]);
1067+ }
1068+
1069+ - (void )testCameraAccessUnknownStatusTreatedAsDenied {
1070+ id mockAVCaptureDevice = OCMClassMock ([AVCaptureDevice class ]);
1071+ OCMStub ([mockAVCaptureDevice authorizationStatusForMediaType: AVMediaTypeVideo])
1072+ .andReturn ((AVAuthorizationStatus)999 );
1073+
1074+ FLTImagePickerPlugin *plugin =
1075+ [[FLTImagePickerPlugin alloc ] initWithViewProvider: [[StubViewProvider alloc ] init ]];
1076+ [plugin setImagePickerControllerOverrides: @[ [[UIImagePickerController alloc ] init ] ]];
1077+
1078+ XCTestExpectation *resultExpectation = [self expectationWithDescription: @" unknown denied" ];
1079+ [plugin pickImageWithSource: [FLTSourceSpecification makeWithType: FLTSourceTypeCamera
1080+ camera: FLTSourceCameraRear]
1081+ maxSize: [[FLTMaxSize alloc ] init ]
1082+ quality: nil
1083+ fullMetadata: YES
1084+ completion: ^(NSString *result, FlutterError *error) {
1085+ XCTAssertEqualObjects (error.code , @" camera_access_denied" );
1086+ [resultExpectation fulfill ];
1087+ }];
1088+ [self waitForExpectationsWithTimeout: 30 handler: nil ];
1089+ }
1090+
1091+ - (void )testShowCameraUnavailableAlertOKHandler {
1092+ id mockUIImagePicker = OCMClassMock ([UIImagePickerController class ]);
1093+ id mockAVCaptureDevice = OCMClassMock ([AVCaptureDevice class ]);
1094+ OCMStub (ClassMethod (
1095+ [mockUIImagePicker isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]))
1096+ .andReturn (NO );
1097+ OCMStub ([mockAVCaptureDevice authorizationStatusForMediaType: AVMediaTypeVideo])
1098+ .andReturn (AVAuthorizationStatusAuthorized);
1099+
1100+ id mockViewController = OCMClassMock ([UIViewController class ]);
1101+ __block UIAlertController *alert = nil ;
1102+ OCMStub ([mockViewController presentViewController: [OCMArg any ] animated: YES completion: nil ])
1103+ .andDo (^(NSInvocation *invocation) {
1104+ __unsafe_unretained UIAlertController *presented = nil ;
1105+ [invocation getArgument: &presented atIndex: 2 ];
1106+ alert = presented;
1107+ });
1108+
1109+ FLTImagePickerPlugin *plugin = [[FLTImagePickerPlugin alloc ]
1110+ initWithViewProvider: [[StubViewProvider alloc ] initWithViewController: mockViewController]];
1111+ [plugin setImagePickerControllerOverrides: @[ [[UIImagePickerController alloc ] init ] ]];
1112+
1113+ XCTestExpectation *resultExpectation = [self expectationWithDescription: @" unavailable" ];
1114+ [plugin pickImageWithSource: [FLTSourceSpecification makeWithType: FLTSourceTypeCamera
1115+ camera: FLTSourceCameraRear]
1116+ maxSize: [[FLTMaxSize alloc ] init ]
1117+ quality: nil
1118+ fullMetadata: YES
1119+ completion: ^(NSString *result, FlutterError *error) {
1120+ XCTAssertNil (result);
1121+ XCTAssertNil (error);
1122+ [resultExpectation fulfill ];
1123+ }];
1124+ [self waitForExpectationsWithTimeout: 30 handler: nil ];
1125+
1126+ XCTAssertTrue ([alert isKindOfClass: [UIAlertController class ]]);
1127+ void (^handler)(UIAlertAction *) = [alert.actions.firstObject valueForKey: @" handler" ];
1128+ if (handler) {
1129+ handler (alert.actions .firstObject );
1130+ }
1131+ }
1132+
1133+ - (void )testPresentingViewControllerWithoutWindowReturnsHostController {
1134+ UIViewController *host = [[UIViewController alloc ] init ];
1135+ FLTImagePickerPlugin *plugin = [[FLTImagePickerPlugin alloc ]
1136+ initWithViewProvider: [[StubViewProvider alloc ] initWithViewController: host]];
1137+ UIViewController *presented = [plugin presentingViewControllerForImagePickerInNewWindow ];
1138+ XCTAssertEqual (presented, host);
1139+ }
1140+
1141+ - (void )testPresentingViewControllerReusesExistingBlockerWindow {
1142+ UIWindow *window = [[UIWindow alloc ] initWithFrame: CGRectMake (0 , 0 , 320 , 480 )];
1143+ UIViewController *rootViewController = [[UIViewController alloc ] init ];
1144+ window.rootViewController = rootViewController;
1145+ [rootViewController loadViewIfNeeded ];
1146+
1147+ FLTImagePickerPlugin *plugin = [[FLTImagePickerPlugin alloc ]
1148+ initWithViewProvider: [[StubViewProvider alloc ] initWithViewController: rootViewController]];
1149+ UIViewController *first = [plugin presentingViewControllerForImagePickerInNewWindow ];
1150+ UIViewController *second = [plugin presentingViewControllerForImagePickerInNewWindow ];
1151+ XCTAssertEqual (first, second);
1152+ [plugin removeInteractionBlocker ];
1153+ }
1154+
1155+ - (void )testPresentingViewControllerWithoutWindowSceneUsesFrame {
1156+ UIWindow *window = [[UIWindow alloc ] initWithFrame: CGRectMake (0 , 0 , 320 , 480 )];
1157+ UIViewController *rootViewController = [[UIViewController alloc ] init ];
1158+ window.rootViewController = rootViewController;
1159+ [rootViewController loadViewIfNeeded ];
1160+
1161+ FLTImagePickerPlugin *plugin = [[FLTImagePickerPlugin alloc ]
1162+ initWithViewProvider: [[StubViewProvider alloc ] initWithViewController: rootViewController]];
1163+ XCTAssertNotNil ([plugin presentingViewControllerForImagePickerInNewWindow ]);
1164+ [plugin removeInteractionBlocker ];
1165+ }
1166+
1167+ - (void )testDefaultViewProviderReturnsRegistrarViewController {
1168+ UIViewController *host = [[UIViewController alloc ] init ];
1169+ id registrar = OCMProtocolMock (@protocol (FlutterPluginRegistrar));
1170+ OCMStub ([registrar viewController ]).andReturn (host);
1171+ FIPDefaultViewProvider *provider = [[FIPDefaultViewProvider alloc ] initWithRegistrar: registrar];
1172+ XCTAssertEqual (provider.viewController , host);
1173+ }
1174+
8181175@end
0 commit comments