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

[image_picker] Image picker fix camera device #3898

Merged

Conversation

ydag
Copy link
Contributor

@ydag ydag commented May 17, 2021

This PR fixes an issue where preferredCameraDevice is not working as expected for getVideo method. I also refactor device-only unit tests to make sure we can test camera devices on the CI as well.

Related issues :

If you had to change anything in the flutter/tests repo, include a link to the migration guide as per the breaking change policy.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides. (Note that unlike the flutter/flutter repo, the flutter/plugins repo does use dart format. See plugin_tool format)
  • I signed the CLA.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

ydag added 4 commits May 17, 2021 16:15
# Conflicts:
#	packages/image_picker/image_picker/pubspec.yaml
# Conflicts:
#	packages/image_picker/image_picker/CHANGELOG.md
When camera is chosen the originalAsset will be nil which leads wrong orientation settings. This can be YES all the time because limited access only available with iOS 14+ and it will be handled with PHPicker implementation.
@ydag ydag force-pushed the image_picker_fix_camera_device branch from 9df8716 to 877b0d8 Compare May 17, 2021 14:20
ydag added 2 commits May 18, 2021 09:18
# Conflicts:
#	packages/image_picker/image_picker/CHANGELOG.md
Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

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

We should have tests covering this. Long-term that will probably require some refactoring for testability, but short term: was this test failing and we just didn't know because it's device-only?

@ydag
Copy link
Contributor Author

ydag commented May 19, 2021

We should have tests covering this. Long-term that will probably require some refactoring for testability, but short term: was this test failing and we just didn't know because it's device-only?

I think the problem is not this test. Since UIImagePickerControllerCameraDevice _device is a global variable we need to re-assign before we call the showCamera method from both pickImage and pickVideo in handleMethodCall.

But I double-check and I think we even don't need a UIImagePickerControllerCameraDevice _device variable at all if we call the getCameraDevice helper method inside the showCamera method.

Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

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

We should have tests covering this. Long-term that will probably require some refactoring for testability, but short term: was this test failing and we just didn't know because it's device-only?

I think the problem is not this test. Since UIImagePickerControllerCameraDevice _device is a global variable we need to re-assign before we call the showCamera method from both pickImage and pickVideo in handleMethodCall.

Sorry, I'm not seeing how this answers my question. To clarify, there are two parts:

  • Where are the test changes that this PR needs to verify the fix, per Flutter policy?
  • What is the status of the test referenced above, which sounds like it should already have been testing this scenario, but had not alerted us to the failure previously given that the tree is green?

@ydag
Copy link
Contributor Author

ydag commented May 27, 2021

Sorry, I'm not seeing how this answers my question. To clarify, there are two parts:

  • Where are the test changes that this PR needs to verify the fix, per Flutter policy?
  • What is the status of the test referenced above, which sounds like it should already have been testing this scenario, but had not alerted us to the failure previously given that the tree is green?

After some tests, I realized that this test is failing as you said and we didn't know because it's device-only.

I was not able to run this test on the real device (because of some weird errors when I was trying to run unit tests of the image_picker on a real device) but after some manipulation on the test and the code, I confirmed that it is failing and this PR fixes the problem.

@stuartmorgan-g
Copy link
Contributor

That means we don't have any meaningful test coverage here, since a regression would (as evidenced by this PR) never show up in our CI.

It looks like this should be testable via native unit tests. It would require class mocking of AVCaptureDevice and UIImagePickerController unfortunately, since there are class methods called on them, but class mocks are still better than no test coverage at all. Alternately, you could wrap those calls in a helper that can be instantiated and mocked less invasively via DI.

ydag added 3 commits June 1, 2021 08:40
# Conflicts:
#	packages/image_picker/image_picker/CHANGELOG.md
#	packages/image_picker/image_picker/pubspec.yaml
# Conflicts:
#	packages/image_picker/image_picker/CHANGELOG.md
#	packages/image_picker/image_picker/pubspec.yaml
# Conflicts:
#	packages/image_picker/image_picker/CHANGELOG.md
#	packages/image_picker/image_picker/ios/Classes/FLTImagePickerPlugin.m
#	packages/image_picker/image_picker/pubspec.yaml
@mvanbeusekom
Copy link
Contributor

That means we don't have any meaningful test coverage here, since a regression would (as evidenced by this PR) never show up in our CI.

It looks like this should be testable via native unit tests. It would require class mocking of AVCaptureDevice and UIImagePickerController unfortunately, since there are class methods called on them, but class mocks are still better than no test coverage at all. Alternately, you could wrap those calls in a helper that can be instantiated and mocked less invasively via DI.

@stuartmorgan could you have another look at this PR? @ydag updated the tests so the device specific features are mocked using OCMock and unit-tests will run correctly on the simulator. Chris already approved but the PR is blocked on your requested changes.

ydag added 2 commits June 25, 2021 08:54
# Conflicts:
#	packages/image_picker/image_picker/CHANGELOG.md
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
#pragma mark - Test camera devices, no op on simulators
- (void)testPluginPickImageDeviceCancelClickMultipleTimes {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you aborting the test if the camera is available?

Copy link
Contributor Author

@ydag ydag Jun 28, 2021

Choose a reason for hiding this comment

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

Hi Stuart,
This change made by this commit and it already exists in master as well and got merged in my PR. Do you think we should remove it? Or do you think we should ask what was this test about and try to fix it?

Copy link
Contributor

Choose a reason for hiding this comment

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

@cyanglaz Was this a bug, or is this intentionally only supposed to run on simulator? That seems bizarre.

@ydag ydag requested a review from stuartmorgan-g June 30, 2021 08:28
Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

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

LGTM with one comment nit (no need to get another review once fixed)

@mvanbeusekom mvanbeusekom added the waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land. label Jul 14, 2021
@fluttergithubbot fluttergithubbot merged commit cf80430 into flutter:master Jul 14, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jul 14, 2021
Ralph-Li added a commit to Insight-Timer/plugins that referenced this pull request Jul 15, 2021
* upstream_master: (40 commits)
  [image_picker] Image picker fix camera device (flutter#3898)
  [flutter_plugin_tools] Improve license-check output (flutter#4154)
  [webview_flutter] Fix broken keyboard issue link (flutter#3266)
  [flutter_plugin_tools] Support format on Windows (flutter#4150)
  [flutter_plugin_tools] Make unit tests pass on Windows (flutter#4149)
  [image_picker_for_web] Migrate image_picker to package:cross_file (flutter#4083)
  [various] Prepare plugin repo for binding API improvements (flutter#4148)
  [quick_actions] Add const constructor (flutter#4131)
  [in_app_purchase] Add iOS currency symbol to ProductDetails (flutter#4144)
  [in_app_purchase] Added priceCurrencySymbol to SkuDetailsWrapper (flutter#4114)
  [image_picker_platform_interface] Add methods that return package:cross_file (flutter#4072)
  [flutter_plugin_tools] Improve and test 'format' (flutter#4145)
  [flutter_plugin_tools] Only check target packages in analyze (flutter#4146)
  [in_app_purchase] Fix crash when retrieveReceiptWithError gives an error. (flutter#4138)
  [video_player] Pause video when it completes (flutter#3727)
  [in_app_purchase] Add currencySymbol to ProductDetails (flutter#4115)
  [in_app_purchase] Add documentation for price change confirmations (flutter#4092)
  [camera] android-rework part 8: Supporting modules for final implementation (flutter#4054)
  [plugin_platform_interface] Fix README broken link (flutter#4143)
  [various] Prepare plugin repo for binding API improvements (flutter#4137)
  ...
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jul 16, 2021
fotiDim pushed a commit to fotiDim/plugins that referenced this pull request Sep 13, 2021
jolmiracle pushed a commit to miracle-as/webview_flutter that referenced this pull request Sep 24, 2021
# Conflicts:
#	packages/image_picker/image_picker/CHANGELOG.md
#	packages/image_picker/image_picker/example/ios/Podfile
#	packages/image_picker/image_picker/pubspec.yaml
amantoux pushed a commit to amantoux/plugins that referenced this pull request Sep 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes p: image_picker platform-ios waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[image_picker] preferredCameraDevice: CameraDevice.front does not work in getVideo
5 participants