Skip to content

[image_picker] Add native tests for photo-library access and UIImagePicker completion - #12540

Open
victogomez-cs wants to merge 3 commits into
image_picker_coverage_2_camera_and_resultsfrom
image_picker_coverage_3_gallery_and_finish_picking
Open

[image_picker] Add native tests for photo-library access and UIImagePicker completion#12540
victogomez-cs wants to merge 3 commits into
image_picker_coverage_2_camera_and_resultsfrom
image_picker_coverage_3_gallery_and_finish_picking

Conversation

@victogomez-cs

@victogomez-cs victogomez-cs commented Aug 21, 2026

Copy link
Copy Markdown

Stacked on #12539.

Adds native unit tests for previously untested paths in FLTImagePickerPlugin.m (photo-library authorization, UIImagePicker launch, and didFinishPickingMediaWithInfo:) 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.

ImagePickerPluginTests go from 52 tests to 70 tests (+18).

New test cases

Photo-library access:

  • testLaunchUIImagePickerGalleryWithoutFullMetadataSkipsAuthorization — gallery without full metadata skips the authorization check
  • testPhotoAccessDeniedReturnsError — denied photo access returns an error
  • testPhotoAccessRestrictedReturnsError — restricted photo access returns an error
  • testPhotoAccessAuthorizedShowsLibrary — authorized access presents the library
  • testPhotoAccessNotDeterminedGrantedShowsLibrary — prompt granted presents the library
  • testPhotoAccessNotDeterminedDeniedReturnsError — prompt denied returns an error
  • testPhotoAccessLimitedReturnsDeniedError — Limited access falls through to denied
  • testPhotoAccessUnknownStatusTreatedAsDenied — unknown status is treated as denied

launchUIImagePickerWithSource::

  • testLaunchUIImagePickerInvalidSourceReturnsError — an invalid source returns an error
  • testLaunchUIImagePickerSetsImageAndVideoMediaTypes — image/video media types and duration are set on the picker

imagePickerController:didFinishPickingMediaWithInfo::

  • testImagePickerDidFinishPickingOriginalImage — original image is saved
  • testImagePickerDidFinishPickingPrefersEditedImage — edited image is preferred over original
  • testImagePickerDidFinishPickingScalesImage — the image is scaled when requested
  • testImagePickerDidFinishPickingWithFullMetadataAsset — full-metadata path uses PHImageManager when an asset is present
  • testImagePickerDidFinishPickingFullMetadataWithoutAsset — full metadata without an asset still completes
  • testImagePickerDidFinishPickingVideo — video is copied successfully
  • testImagePickerDidFinishPickingVideoCopyFailure — a video copy failure is reported
  • testImagePickerDidFinishPickingIgnoredWhenNoCallContext — no callContext is a no-op

Third part of image_picker_ios coverage backfill before the Obj-C → Swift migration for flutter/flutter#119107

Pre-Review Checklist

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-assist bot 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

  1. 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

@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Aug 21, 2026
@victogomez-cs victogomez-cs changed the title [image_picker_ios] Add native tests for photo-library access and UIIm… [image_picker] Add native tests for photo-library access and UIImagePicker completion Aug 21, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +1329 to +1330
void (^handler)(PHAuthorizationStatus status);
[invocation getArgument:&handler atIndex:2];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
void (^handler)(PHAuthorizationStatus status);
[invocation getArgument:&handler atIndex:2];
__unsafe_unretained void (^handler)(PHAuthorizationStatus status);
[invocation getArgument:&handler atIndex:2];

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed and fixed. The authorization handler is now retrieved as __unsafe_unretained so ARC doesn’t over-release the NSInvocation argument.

Comment on lines +1361 to +1362
void (^handler)(PHAuthorizationStatus status);
[invocation getArgument:&handler atIndex:2];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
void (^handler)(PHAuthorizationStatus status);
[invocation getArgument:&handler atIndex:2];
__unsafe_unretained void (^handler)(PHAuthorizationStatus status);
[invocation getArgument:&handler atIndex:2];

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed and fixed. Same fix here: the authorization handler is now __unsafe_unretained.

Comment on lines +1545 to +1546
void (^handler)(NSData *, NSString *, CGImagePropertyOrientation, NSDictionary *);
[invocation getArgument:&handler atIndex:4];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
void (^handler)(NSData *, NSString *, CGImagePropertyOrientation, NSDictionary *);
[invocation getArgument:&handler atIndex:4];
__unsafe_unretained void (^handler)(NSData *, NSString *, CGImagePropertyOrientation, NSDictionary *);
[invocation getArgument:&handler atIndex:4];

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant