-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[camera] Update [CameraOrientationTests testOrientationNotifications] unit test to work on Xcode 13 #4426
[camera] Update [CameraOrientationTests testOrientationNotifications] unit test to work on Xcode 13 #4426
Changes from 2 commits
165b15c
d9292ba
6083b93
8af03a6
30f7c8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,66 +2,78 @@ | |||||
| // Use of this source code is governed by a BSD-style license that can be | ||||||
| // found in the LICENSE file. | ||||||
|
|
||||||
| @import camera; | ||||||
| @import XCTest; | ||||||
|
|
||||||
| #import <Flutter/Flutter.h> | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
| #import <OCMock/OCMock.h> | ||||||
|
|
||||||
| @interface CameraPlugin : NSObject | ||||||
| - (instancetype)initWithRegistry:(NSObject<FlutterTextureRegistry> *)registry | ||||||
| messenger:(NSObject<FlutterBinaryMessenger> *)messenger; | ||||||
|
|
||||||
| - (void)orientationChanged:(NSNotification *)note; | ||||||
| @end | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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".)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
|
|
||||||
| @interface CameraOrientationTests : XCTestCase | ||||||
| @property(strong, nonatomic) id mockRegistrar; | ||||||
| @property(strong, nonatomic) id mockMessenger; | ||||||
| @property(strong, nonatomic) CameraPlugin *cameraPlugin; | ||||||
| @end | ||||||
|
|
||||||
| @implementation CameraOrientationTests | ||||||
|
|
||||||
| - (void)setUp { | ||||||
| [super setUp]; | ||||||
| self.mockRegistrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar)); | ||||||
|
|
||||||
| self.mockMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger)); | ||||||
| OCMStub([self.mockRegistrar messenger]).andReturn(self.mockMessenger); | ||||||
| self.cameraPlugin = [[CameraPlugin alloc] initWithRegistry:nil messenger:_mockMessenger]; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed, thanks for educating. |
||||||
| } | ||||||
|
|
||||||
| - (void)testOrientationNotifications { | ||||||
| id mockMessenger = self.mockMessenger; | ||||||
| [mockMessenger setExpectationOrderMatters:YES]; | ||||||
| XCUIDevice.sharedDevice.orientation = UIDeviceOrientationPortrait; | ||||||
|
|
||||||
| [CameraPlugin registerWithRegistrar:self.mockRegistrar]; | ||||||
|
|
||||||
| [self rotate:UIDeviceOrientationPortraitUpsideDown expectedChannelOrientation:@"portraitDown"]; | ||||||
| [self rotate:UIDeviceOrientationPortrait expectedChannelOrientation:@"portraitUp"]; | ||||||
| [self rotate:UIDeviceOrientationLandscapeRight expectedChannelOrientation:@"landscapeLeft"]; | ||||||
| [self rotate:UIDeviceOrientationLandscapeLeft expectedChannelOrientation:@"landscapeRight"]; | ||||||
|
|
||||||
| OCMReject([mockMessenger sendOnChannel:[OCMArg any] message:[OCMArg any]]); | ||||||
| // No notification when orientation doesn't change. | ||||||
| XCUIDevice.sharedDevice.orientation = UIDeviceOrientationLandscapeLeft; | ||||||
|
|
||||||
| // No notification when flat. | ||||||
| XCUIDevice.sharedDevice.orientation = UIDeviceOrientationFaceUp; | ||||||
| [self.cameraPlugin | ||||||
| orientationChanged:[self createMockNotificationForOrientation:UIDeviceOrientationFaceUp]]; | ||||||
| // No notification when facedown. | ||||||
| XCUIDevice.sharedDevice.orientation = UIDeviceOrientationFaceDown; | ||||||
| [self.cameraPlugin | ||||||
| orientationChanged:[self createMockNotificationForOrientation:UIDeviceOrientationFaceDown]]; | ||||||
|
|
||||||
| OCMVerifyAll(mockMessenger); | ||||||
| } | ||||||
|
|
||||||
| - (void)rotate:(UIDeviceOrientation)deviceOrientation | ||||||
| expectedChannelOrientation:(NSString*)channelOrientation { | ||||||
| expectedChannelOrientation:(NSString *)channelOrientation { | ||||||
| id mockMessenger = self.mockMessenger; | ||||||
| XCTestExpectation* orientationExpectation = [self expectationWithDescription:channelOrientation]; | ||||||
| XCTestExpectation *orientationExpectation = [self expectationWithDescription:channelOrientation]; | ||||||
|
|
||||||
| OCMExpect([mockMessenger | ||||||
| sendOnChannel:[OCMArg any] | ||||||
| message:[OCMArg checkWithBlock:^BOOL(NSData* data) { | ||||||
| NSObject<FlutterMethodCodec>* codec = [FlutterStandardMethodCodec sharedInstance]; | ||||||
| FlutterMethodCall* methodCall = [codec decodeMethodCall:data]; | ||||||
| message:[OCMArg checkWithBlock:^BOOL(NSData *data) { | ||||||
| NSObject<FlutterMethodCodec> *codec = [FlutterStandardMethodCodec sharedInstance]; | ||||||
| FlutterMethodCall *methodCall = [codec decodeMethodCall:data]; | ||||||
| [orientationExpectation fulfill]; | ||||||
| return | ||||||
| [methodCall.method isEqualToString:@"orientation_changed"] && | ||||||
| [methodCall.arguments isEqualToDictionary:@{@"orientation" : channelOrientation}]; | ||||||
| }]]); | ||||||
|
|
||||||
| XCUIDevice.sharedDevice.orientation = deviceOrientation; | ||||||
| [self.cameraPlugin | ||||||
| orientationChanged:[self createMockNotificationForOrientation:deviceOrientation]]; | ||||||
| [self waitForExpectationsWithTimeout:30.0 handler:nil]; | ||||||
| } | ||||||
|
|
||||||
| - (NSNotification *)createMockNotificationForOrientation:(UIDeviceOrientation)deviceOrientation { | ||||||
| UIDevice *mockDevice = OCMClassMock([UIDevice class]); | ||||||
| OCMStub([mockDevice orientation]).andReturn(deviceOrientation); | ||||||
|
|
||||||
| return [[NSNotification alloc] initWithName:@"orientation_test" object:mockDevice userInfo:nil]; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||||||
| } | ||||||
|
|
||||||
| @end | ||||||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.