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

Commit 176cfb8

Browse files
authored
[camera] Update [CameraOrientationTests testOrientationNotifications] unit test to work on Xcode 13 (#4426)
1 parent 6716d75 commit 176cfb8

File tree

8 files changed

+70
-20
lines changed

8 files changed

+70
-20
lines changed

packages/camera/camera/CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## NEXT
1+
## 0.9.4+2
22

3-
* Updated package description.
3+
* Updated package description;
4+
* Refactor unit test on iOS to make it compatible with new restrictions in Xcode 13 which only supports the use of the `XCUIDevice` in Xcode UI tests.
45

56
## 0.9.4+1
67

packages/camera/camera/example/ios/RunnerTests/CameraOrientationTests.m

+24-16
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,73 @@
33
// found in the LICENSE file.
44

55
@import camera;
6+
@import camera.Test;
67
@import XCTest;
8+
@import Flutter;
79

810
#import <OCMock/OCMock.h>
911

1012
@interface CameraOrientationTests : XCTestCase
11-
@property(strong, nonatomic) id mockRegistrar;
1213
@property(strong, nonatomic) id mockMessenger;
14+
@property(strong, nonatomic) CameraPlugin *cameraPlugin;
1315
@end
1416

1517
@implementation CameraOrientationTests
1618

1719
- (void)setUp {
1820
[super setUp];
19-
self.mockRegistrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar));
21+
2022
self.mockMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
21-
OCMStub([self.mockRegistrar messenger]).andReturn(self.mockMessenger);
23+
self.cameraPlugin = [[CameraPlugin alloc] initWithRegistry:nil messenger:self.mockMessenger];
2224
}
2325

2426
- (void)testOrientationNotifications {
2527
id mockMessenger = self.mockMessenger;
2628
[mockMessenger setExpectationOrderMatters:YES];
27-
XCUIDevice.sharedDevice.orientation = UIDeviceOrientationPortrait;
28-
29-
[CameraPlugin registerWithRegistrar:self.mockRegistrar];
3029

3130
[self rotate:UIDeviceOrientationPortraitUpsideDown expectedChannelOrientation:@"portraitDown"];
3231
[self rotate:UIDeviceOrientationPortrait expectedChannelOrientation:@"portraitUp"];
3332
[self rotate:UIDeviceOrientationLandscapeRight expectedChannelOrientation:@"landscapeLeft"];
3433
[self rotate:UIDeviceOrientationLandscapeLeft expectedChannelOrientation:@"landscapeRight"];
3534

3635
OCMReject([mockMessenger sendOnChannel:[OCMArg any] message:[OCMArg any]]);
37-
// No notification when orientation doesn't change.
38-
XCUIDevice.sharedDevice.orientation = UIDeviceOrientationLandscapeLeft;
36+
3937
// No notification when flat.
40-
XCUIDevice.sharedDevice.orientation = UIDeviceOrientationFaceUp;
38+
[self.cameraPlugin
39+
orientationChanged:[self createMockNotificationForOrientation:UIDeviceOrientationFaceUp]];
4140
// No notification when facedown.
42-
XCUIDevice.sharedDevice.orientation = UIDeviceOrientationFaceDown;
41+
[self.cameraPlugin
42+
orientationChanged:[self createMockNotificationForOrientation:UIDeviceOrientationFaceDown]];
4343

4444
OCMVerifyAll(mockMessenger);
4545
}
4646

4747
- (void)rotate:(UIDeviceOrientation)deviceOrientation
48-
expectedChannelOrientation:(NSString*)channelOrientation {
48+
expectedChannelOrientation:(NSString *)channelOrientation {
4949
id mockMessenger = self.mockMessenger;
50-
XCTestExpectation* orientationExpectation = [self expectationWithDescription:channelOrientation];
50+
XCTestExpectation *orientationExpectation = [self expectationWithDescription:channelOrientation];
5151

5252
OCMExpect([mockMessenger
5353
sendOnChannel:[OCMArg any]
54-
message:[OCMArg checkWithBlock:^BOOL(NSData* data) {
55-
NSObject<FlutterMethodCodec>* codec = [FlutterStandardMethodCodec sharedInstance];
56-
FlutterMethodCall* methodCall = [codec decodeMethodCall:data];
54+
message:[OCMArg checkWithBlock:^BOOL(NSData *data) {
55+
NSObject<FlutterMethodCodec> *codec = [FlutterStandardMethodCodec sharedInstance];
56+
FlutterMethodCall *methodCall = [codec decodeMethodCall:data];
5757
[orientationExpectation fulfill];
5858
return
5959
[methodCall.method isEqualToString:@"orientation_changed"] &&
6060
[methodCall.arguments isEqualToDictionary:@{@"orientation" : channelOrientation}];
6161
}]]);
6262

63-
XCUIDevice.sharedDevice.orientation = deviceOrientation;
63+
[self.cameraPlugin
64+
orientationChanged:[self createMockNotificationForOrientation:deviceOrientation]];
6465
[self waitForExpectationsWithTimeout:30.0 handler:nil];
6566
}
6667

68+
- (NSNotification *)createMockNotificationForOrientation:(UIDeviceOrientation)deviceOrientation {
69+
UIDevice *mockDevice = OCMClassMock([UIDevice class]);
70+
OCMStub([mockDevice orientation]).andReturn(deviceOrientation);
71+
72+
return [NSNotification notificationWithName:@"orientation_test" object:mockDevice];
73+
}
74+
6775
@end

packages/camera/camera/ios/Classes/CameraPlugin.m

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// found in the LICENSE file.
44

55
#import "CameraPlugin.h"
6+
#import "CameraPlugin_Test.h"
7+
68
#import <AVFoundation/AVFoundation.h>
79
#import <Accelerate/Accelerate.h>
810
#import <CoreMotion/CoreMotion.h>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
framework module camera {
2+
umbrella header "camera-umbrella.h"
3+
4+
export *
5+
module * { export * }
6+
7+
explicit module Test {
8+
header "CameraPlugin_Test.h"
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// This header is available in the Test module. Import via "@import camera.Test;"
6+
7+
#import <camera/CameraPlugin.h>
8+
9+
/// Methods exposed for unit testing.
10+
@interface CameraPlugin ()
11+
12+
- (instancetype)initWithRegistry:(NSObject<FlutterTextureRegistry> *)registry
13+
messenger:(NSObject<FlutterBinaryMessenger> *)messenger
14+
NS_DESIGNATED_INITIALIZER;
15+
- (instancetype)init NS_UNAVAILABLE;
16+
17+
- (void)orientationChanged:(NSNotification *)notification;
18+
19+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#import <Foundation/Foundation.h>
6+
#import <camera/CameraPlugin.h>
7+
8+
FOUNDATION_EXPORT double cameraVersionNumber;
9+
FOUNDATION_EXPORT const unsigned char cameraVersionString[];

packages/camera/camera/ios/camera.podspec

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ A Flutter plugin to use the camera from your Flutter app.
1313
s.author = { 'Flutter Dev Team' => '[email protected]' }
1414
s.source = { :http => 'https://github.com/flutter/plugins/tree/master/packages/camera' }
1515
s.documentation_url = 'https://pub.dev/packages/camera'
16-
s.source_files = 'Classes/**/*'
16+
s.source_files = 'Classes/**/*.{h,m}'
1717
s.public_header_files = 'Classes/**/*.h'
18+
s.module_map = 'Classes/CameraPlugin.modulemap'
1819
s.dependency 'Flutter'
1920

2021
s.platform = :ios, '9.0'

packages/camera/camera/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: A Flutter plugin for controlling the camera. Supports previewing
44
Dart.
55
repository: https://github.com/flutter/plugins/tree/master/packages/camera/camera
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
7-
version: 0.9.4+1
7+
version: 0.9.4+2
88

99
environment:
1010
sdk: ">=2.14.0 <3.0.0"

0 commit comments

Comments
 (0)