[image_picker] Add native tests for photo-library access and UIImagePicker completion - #12540
Conversation
…agePicker completion.
There was a problem hiding this comment.
Code Review
This pull request adds comprehensive unit tests to ImagePickerPluginTests.m for the iOS image picker plugin, covering permission handling, image/video picking, and scaling scenarios. The review feedback identifies a potential memory corruption or crash issue under ARC in three tests where a block is retrieved from NSInvocation using getArgument:atIndex: into a __strong variable, and suggests declaring these block variables as __unsafe_unretained to prevent over-releasing.
| void (^handler)(PHAuthorizationStatus status); | ||
| [invocation getArgument:&handler atIndex:2]; |
There was a problem hiding this comment.
Under ARC, retrieving a block from NSInvocation using getArgument:atIndex: directly into a __strong block variable can cause memory corruption or crashes due to over-releasing. Since getArgument:atIndex: does not retain the block, the implicit release by ARC when the __strong variable goes out of scope will result in an over-release. To prevent this, declare the block variable as __unsafe_unretained.
| void (^handler)(PHAuthorizationStatus status); | |
| [invocation getArgument:&handler atIndex:2]; | |
| __unsafe_unretained void (^handler)(PHAuthorizationStatus status); | |
| [invocation getArgument:&handler atIndex:2]; |
There was a problem hiding this comment.
Agreed and fixed. The authorization handler is now retrieved as __unsafe_unretained so ARC doesn’t over-release the NSInvocation argument.
| void (^handler)(PHAuthorizationStatus status); | ||
| [invocation getArgument:&handler atIndex:2]; |
There was a problem hiding this comment.
Under ARC, retrieving a block from NSInvocation using getArgument:atIndex: directly into a __strong block variable can cause memory corruption or crashes due to over-releasing. Since getArgument:atIndex: does not retain the block, the implicit release by ARC when the __strong variable goes out of scope will result in an over-release. To prevent this, declare the block variable as __unsafe_unretained.
| void (^handler)(PHAuthorizationStatus status); | |
| [invocation getArgument:&handler atIndex:2]; | |
| __unsafe_unretained void (^handler)(PHAuthorizationStatus status); | |
| [invocation getArgument:&handler atIndex:2]; |
There was a problem hiding this comment.
Agreed and fixed. Same fix here: the authorization handler is now __unsafe_unretained.
| void (^handler)(NSData *, NSString *, CGImagePropertyOrientation, NSDictionary *); | ||
| [invocation getArgument:&handler atIndex:4]; |
There was a problem hiding this comment.
Under ARC, retrieving a block from NSInvocation using getArgument:atIndex: directly into a __strong block variable can cause memory corruption or crashes due to over-releasing. Since getArgument:atIndex: does not retain the block, the implicit release by ARC when the __strong variable goes out of scope will result in an over-release. To prevent this, declare the block variable as __unsafe_unretained.
| void (^handler)(NSData *, NSString *, CGImagePropertyOrientation, NSDictionary *); | |
| [invocation getArgument:&handler atIndex:4]; | |
| __unsafe_unretained void (^handler)(NSData *, NSString *, CGImagePropertyOrientation, NSDictionary *); | |
| [invocation getArgument:&handler atIndex:4]; |
There was a problem hiding this comment.
Agreed and fixed. The PHImageManager result handler is now retrieved as __unsafe_unretained.
…picker_coverage_3_gallery_and_finish_picking
…variables to prevent retain cycles in mock callbacks.
Stacked on #12539.
Adds native unit tests for previously untested paths in
FLTImagePickerPlugin.m(photo-library authorization,UIImagePickerlaunch, anddidFinishPickingMediaWithInfo:) before the Objective-C → Swift migration. Production code is unchanged.This is a tests-only change, so it does not bump the package version or CHANGELOG.
ImagePickerPluginTestsgo from 52 tests to 70 tests (+18).New test cases
Photo-library access:
testLaunchUIImagePickerGalleryWithoutFullMetadataSkipsAuthorization— gallery without full metadata skips the authorization checktestPhotoAccessDeniedReturnsError— denied photo access returns an errortestPhotoAccessRestrictedReturnsError— restricted photo access returns an errortestPhotoAccessAuthorizedShowsLibrary— authorized access presents the librarytestPhotoAccessNotDeterminedGrantedShowsLibrary— prompt granted presents the librarytestPhotoAccessNotDeterminedDeniedReturnsError— prompt denied returns an errortestPhotoAccessLimitedReturnsDeniedError— Limited access falls through to deniedtestPhotoAccessUnknownStatusTreatedAsDenied— unknown status is treated as deniedlaunchUIImagePickerWithSource::testLaunchUIImagePickerInvalidSourceReturnsError— an invalid source returns an errortestLaunchUIImagePickerSetsImageAndVideoMediaTypes— image/video media types and duration are set on the pickerimagePickerController:didFinishPickingMediaWithInfo::testImagePickerDidFinishPickingOriginalImage— original image is savedtestImagePickerDidFinishPickingPrefersEditedImage— edited image is preferred over originaltestImagePickerDidFinishPickingScalesImage— the image is scaled when requestedtestImagePickerDidFinishPickingWithFullMetadataAsset— full-metadata path usesPHImageManagerwhen an asset is presenttestImagePickerDidFinishPickingFullMetadataWithoutAsset— full metadata without an asset still completestestImagePickerDidFinishPickingVideo— video is copied successfullytestImagePickerDidFinishPickingVideoCopyFailure— a video copy failure is reportedtestImagePickerDidFinishPickingIgnoredWhenNoCallContext— nocallContextis a no-opThird part of
image_picker_ioscoverage backfill before the Obj-C → Swift migration for flutter/flutter#119107Pre-Review Checklist
[shared_preferences]///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2