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

[camera] Update [CameraOrientationTests testOrientationNotifications] unit test to work on Xcode 13 #4426

Merged
merged 5 commits into from
Oct 14, 2021

Conversation

mvanbeusekom
Copy link
Contributor

@mvanbeusekom mvanbeusekom commented Oct 12, 2021

Refactored the iOS native [CameraOrientationTests testOrientationNotifications] unit test to remove the dependency on the XCUIDevice class. The XCUIDevice class can no longer be used in unit tests starting from Xcode 13 (the class will only be available in Xcode UI tests).

The updates made in this PR isolates the [CameraPlugin orientationChanged] adhering to the principles of a unit test. This however has two consequences that would be good to verify:

  1. The previous version of this test functioned as an integration test, also running the [CameraPlugin startOrientationListener] method and relying on the NSNotificationCenter to deliver orientation updates. This version directly calls the [CameraPlugin orientationChanged] method with a mocked NSNotification object and thus no longer tests the integration between the NSNotificationCenter and the CameraPlugin class. Since this is only simple initialisation logic I decided it wouldn't be a big problem, but it is worth mentioning here to make sure everybody agrees;
  2. I have removed the test which makes sure that no message is send when the [CameraPlugin orientationChanged] method is called with the same orientation the device is already in. The reason is that the [CameraPlugin orientationChanged] method doesn't contain any logic which would prevent this. The reason this wasn't a problem earlier is that the NSNotificationCenter will not deliver a notification if the orientation isn't physically different. Since we are now testing the [CameraPlugin orientationChanged] method in isolation and no longer the integration with NSNotificationCenter this test would fail. If it is necessary to ensure this behaviour in the [CameraPlugin orientationChanged] method, the implementation needs to change and I didn't want to do so unless everybody agrees it needs to be changed.

Resolves flutter/flutter#84162

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 relevant style guides and ran the auto-formatter. (Note that unlike the flutter/flutter repo, the flutter/plugins repo does use dart 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.
    • As this is only a change in a unit test (not implementation) I think this doesn't need to be released immediately but can be released in the next version.
  • 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.

Refactored the unit test so it no longer relies on the XCUIDevice class
which can only be used by UI tests starting from Xcode 13 and higher.
Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

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

Thanks for working on this!

self.mockMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
OCMStub([self.mockRegistrar messenger]).andReturn(self.mockMessenger);
self.cameraPlugin = [[CameraPlugin alloc] initWithRegistry:nil messenger:_mockMessenger];
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Don't use ivars (except in getters, initializers, and dealloc), use the synthesized property.

Suggested change
self.cameraPlugin = [[CameraPlugin alloc] initWithRegistry:nil messenger:_mockMessenger];
self.cameraPlugin = [[CameraPlugin alloc] initWithRegistry:nil messenger:self.mockMessenger];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, thanks for educating.

Comment on lines 9 to 14
@interface CameraPlugin : NSObject
- (instancetype)initWithRegistry:(NSObject<FlutterTextureRegistry> *)registry
messenger:(NSObject<FlutterBinaryMessenger> *)messenger;

- (void)orientationChanged:(NSNotification *)note;
@end
Copy link
Member

Choose a reason for hiding this comment

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

We don't generally want to expose private interfaces like this, and instead expose in a test header like FLTGoogleSignInPlugin_Test.h introduced in #4157. Though that caused a lot of changes, I had to add a module map and umbrella header.
@stuartmorgan what's your take on this? Would you prefer the a CameraPlugin_Test.h header and explicit module map, or is a little SPI for an API under test (and so would fail in the same commit if something changed) preferred?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Having a look at the changes you made in #4157, I feel confident I can also do the same here (have to read up a bit on the .modulemap but it doesn't look to complicated). However I will wait for @stuartmorgan's take on this, but I do see why it would be useful, thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

I always prefer explicit test headers; it makes it much easier for someone looking at the non-test code to understand what parts of the internals are being used by tests. (I also like to hope that maybe having to be very deliberate about the fact that the internals of the class are being exposed and used gives people more pause than just tossing a couple of lines at the top of a test file where "it's just test code".)

Copy link
Member

Choose a reason for hiding this comment

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

@mvanbeusekom There were a few gotchas with modulemaps, the podspec, and designated initializers, so I took the liberty of getting it all working in #4430. You can cherry pick that change into this PR, or I can update the pubspec/CHANGELOG and check it in first, up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jmagman thank you for doing all this work for me ;). I cherry picked your change into my branch and removed the inline interface declaration from the CameraOrientationTests.m file and all looks to be fine. Could you have another look at the PR and see if you agree with the changes?

Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

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

I think this needs a version bump and pubspec change, but other than that, a few very minor nits.

@import XCTest;

#import <Flutter/Flutter.h>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#import <Flutter/Flutter.h>
@import Flutter;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -1,6 +1,7 @@
## NEXT
Copy link
Member

Choose a reason for hiding this comment

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

I think the modulemap change deserves a patch version bump since it's a change in the framework, not just the example tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

UIDevice *mockDevice = OCMClassMock([UIDevice class]);
OCMStub([mockDevice orientation]).andReturn(deviceOrientation);

return [[NSNotification alloc] initWithName:@"orientation_test" object:mockDevice userInfo:nil];
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return [[NSNotification alloc] initWithName:@"orientation_test" object:mockDevice userInfo:nil];
return [NSNotification notificationWithName:@"orientation_test" object:mockDevice];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@mvanbeusekom mvanbeusekom requested a review from jmagman October 13, 2021 17:26
Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

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

The test failures look like a GitHub connection issue? Might pass on a re-run.

fatal: unable to access 'https://github.com/flutter/plugins.git/': Failed to connect to github.com port 443: Connection refused

LGTM, thanks again!

@jmagman
Copy link
Member

jmagman commented Oct 13, 2021

Might pass on a re-run.

And they did.

@jmagman jmagman 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 Oct 13, 2021
@jmagman jmagman merged commit 176cfb8 into flutter:master Oct 14, 2021
@mvanbeusekom mvanbeusekom deleted the 84162 branch October 14, 2021 05:29
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Oct 14, 2021
camsim99 pushed a commit to camsim99/plugins that referenced this pull request Oct 18, 2021
NickalasB added a commit to NickalasB/plugins that referenced this pull request Oct 21, 2021
* master:
  [webview] Fix typos in the README (flutter#4249)
  [google_sign_in] add serverAuthCode to GoogleSignInAccount (flutter#4180)
  [ci] Update macOS Cirrus image to Xcode 13 (flutter#4429)
  [shared_preferences] Switch to new analysis options (flutter#4384)
  [flutter_plugin_android_lifecycle] remove placeholder dart file (flutter#4413)
  [camera] Run iOS methods on UI thread by default (flutter#4140)
  [ci] Always run all `format` steps (flutter#4427)
  [flutter_plugin_tools] Fix license-check on Windows (flutter#4425)
  [google_maps_flutter] Clean Java test, consolidate Marker example. (flutter#4400)
  [image_picker][android] suppress unchecked warning (flutter#4408)
  [ci] Replace Firebase Test Lab deprecated Pixel 4 device with Pixel 5 (flutter#4436)
  [image_picker_for_web] Added support for maxWidth, maxHeight and imageQuality  (flutter#4389)
  Bump compileSdkVersion to 31 (flutter#4432)
  [camera] Update [CameraOrientationTests testOrientationNotifications] unit test to work on Xcode 13 (flutter#4426)
  Update integration_test README  (flutter#3824)
  [webview_flutter] Adjust test URLs again (flutter#4407)
  [google_sign_in] Add serverAuthCode attribute to google_sign_in_platform_interface user data (flutter#4179)
  [camera] Add filter for unsupported cameras on Android (flutter#4418)
NickalasB added a commit to NickalasB/plugins that referenced this pull request Oct 21, 2021
* master:
  [webview] Fix typos in the README (flutter#4249)
  [google_sign_in] add serverAuthCode to GoogleSignInAccount (flutter#4180)
  [ci] Update macOS Cirrus image to Xcode 13 (flutter#4429)
  [shared_preferences] Switch to new analysis options (flutter#4384)
  [flutter_plugin_android_lifecycle] remove placeholder dart file (flutter#4413)
  [camera] Run iOS methods on UI thread by default (flutter#4140)
  [ci] Always run all `format` steps (flutter#4427)
  [flutter_plugin_tools] Fix license-check on Windows (flutter#4425)
  [google_maps_flutter] Clean Java test, consolidate Marker example. (flutter#4400)
  [image_picker][android] suppress unchecked warning (flutter#4408)
  [ci] Replace Firebase Test Lab deprecated Pixel 4 device with Pixel 5 (flutter#4436)
  [image_picker_for_web] Added support for maxWidth, maxHeight and imageQuality  (flutter#4389)
  Bump compileSdkVersion to 31 (flutter#4432)
  [camera] Update [CameraOrientationTests testOrientationNotifications] unit test to work on Xcode 13 (flutter#4426)
  Update integration_test README  (flutter#3824)
  [webview_flutter] Adjust test URLs again (flutter#4407)
  [google_sign_in] Add serverAuthCode attribute to google_sign_in_platform_interface user data (flutter#4179)
  [camera] Add filter for unsupported cameras on Android (flutter#4418)

# Conflicts:
#	packages/webview_flutter/webview_flutter/CHANGELOG.md
#	packages/webview_flutter/webview_flutter/pubspec.yaml
yasargil added a commit to yasargil/plugins that referenced this pull request Oct 25, 2021
* master: (364 commits)
  Use OpenJDK 11 in CI jobs  (flutter#4419)
  [google_sign_in] remove the commented out code in tests (flutter#4442)
  [webview] Fix typos in the README (flutter#4249)
  [google_sign_in] add serverAuthCode to GoogleSignInAccount (flutter#4180)
  [ci] Update macOS Cirrus image to Xcode 13 (flutter#4429)
  [shared_preferences] Switch to new analysis options (flutter#4384)
  [flutter_plugin_android_lifecycle] remove placeholder dart file (flutter#4413)
  [camera] Run iOS methods on UI thread by default (flutter#4140)
  [ci] Always run all `format` steps (flutter#4427)
  [flutter_plugin_tools] Fix license-check on Windows (flutter#4425)
  [google_maps_flutter] Clean Java test, consolidate Marker example. (flutter#4400)
  [image_picker][android] suppress unchecked warning (flutter#4408)
  [ci] Replace Firebase Test Lab deprecated Pixel 4 device with Pixel 5 (flutter#4436)
  [image_picker_for_web] Added support for maxWidth, maxHeight and imageQuality  (flutter#4389)
  Bump compileSdkVersion to 31 (flutter#4432)
  [camera] Update [CameraOrientationTests testOrientationNotifications] unit test to work on Xcode 13 (flutter#4426)
  Update integration_test README  (flutter#3824)
  [webview_flutter] Adjust test URLs again (flutter#4407)
  [google_sign_in] Add serverAuthCode attribute to google_sign_in_platform_interface user data (flutter#4179)
  [camera] Add filter for unsupported cameras on Android (flutter#4418)
  ...

# Conflicts:
#	packages/google_maps_flutter/google_maps_flutter/android/src/main/java/io/flutter/plugins/googlemaps/GoogleMapController.java
amantoux pushed a commit to amantoux/plugins that referenced this pull request Dec 11, 2021
KyleFin pushed a commit to KyleFin/plugins that referenced this pull request Dec 21, 2021
mvanbeusekom added a commit to Baseflow/flutter-plugins that referenced this pull request Feb 2, 2022
Refactored the existing unit tests to expose private interfaces in a
separate test header instead of an inline interface. This was first
introduced in the google_sign_in plugin (flutter#4157) and after that also
applied in the camera plugin (flutter#4426) and the webview_flutter plugin
(flutter#4480).

This commit also removed several obsolete `OCMStub` declarations.
mvanbeusekom added a commit to Baseflow/flutter-plugins that referenced this pull request Feb 2, 2022
Refactored the existing unit tests to expose private interfaces in a
separate test header instead of an inline interface. This was first
introduced in the google_sign_in plugin (flutter#4157) and after that also
applied in the camera plugin (flutter#4426) and the webview_flutter plugin
(flutter#4480).

This commit also removed several obsolete `OCMStub` declarations.
mvanbeusekom added a commit to Baseflow/flutter-plugins that referenced this pull request Feb 2, 2022
Refactored the existing unit tests to expose private interfaces in a
separate test header instead of an inline interface. This was first
introduced in the google_sign_in plugin (flutter#4157) and after that also
applied in the camera plugin (flutter#4426) and the webview_flutter plugin
(flutter#4480).

This commit also removed several obsolete `OCMStub` declarations.
mvanbeusekom added a commit to Baseflow/flutter-plugins that referenced this pull request Feb 2, 2022
Refactored the existing unit tests to expose private interfaces in a
separate test header instead of an inline interface. This was first
introduced in the google_sign_in plugin (flutter#4157) and after that also
applied in the camera plugin (flutter#4426) and the webview_flutter plugin
(flutter#4480).
mvanbeusekom added a commit to Baseflow/flutter-plugins that referenced this pull request Feb 2, 2022
Refactored the existing unit tests to expose private interfaces in a
separate test header instead of an inline interface. This was first
introduced in the google_sign_in plugin (flutter#4157) and after that also
applied in the camera plugin (flutter#4426) and the webview_flutter plugin
(flutter#4480).
mvanbeusekom added a commit to Baseflow/flutter-plugins that referenced this pull request Feb 2, 2022
Refactored the existing unit tests to expose private interfaces in a
separate test header instead of an inline interface. This was first
introduced in the google_sign_in plugin (flutter#4157) and after that also
applied in the camera plugin (flutter#4426) and the webview_flutter plugin
(flutter#4480).
mvanbeusekom added a commit to Baseflow/flutter-plugins that referenced this pull request Feb 7, 2022
Refactored the existing unit tests to expose private interfaces in a
separate test header instead of an inline interface. This was first
introduced in the google_sign_in plugin (flutter#4157) and after that also
applied in the camera plugin (flutter#4426) and the webview_flutter plugin
(flutter#4480).
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes p: camera 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
3 participants