|
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
5 | 5 | @import camera; |
| 6 | +@import camera.Test; |
6 | 7 | @import XCTest; |
| 8 | +@import Flutter; |
7 | 9 |
|
8 | 10 | #import <OCMock/OCMock.h> |
9 | 11 |
|
10 | 12 | @interface CameraOrientationTests : XCTestCase |
11 | | -@property(strong, nonatomic) id mockRegistrar; |
12 | 13 | @property(strong, nonatomic) id mockMessenger; |
| 14 | +@property(strong, nonatomic) CameraPlugin *cameraPlugin; |
13 | 15 | @end |
14 | 16 |
|
15 | 17 | @implementation CameraOrientationTests |
16 | 18 |
|
17 | 19 | - (void)setUp { |
18 | 20 | [super setUp]; |
19 | | - self.mockRegistrar = OCMProtocolMock(@protocol(FlutterPluginRegistrar)); |
| 21 | + |
20 | 22 | self.mockMessenger = OCMProtocolMock(@protocol(FlutterBinaryMessenger)); |
21 | | - OCMStub([self.mockRegistrar messenger]).andReturn(self.mockMessenger); |
| 23 | + self.cameraPlugin = [[CameraPlugin alloc] initWithRegistry:nil messenger:self.mockMessenger]; |
22 | 24 | } |
23 | 25 |
|
24 | 26 | - (void)testOrientationNotifications { |
25 | 27 | id mockMessenger = self.mockMessenger; |
26 | 28 | [mockMessenger setExpectationOrderMatters:YES]; |
27 | | - XCUIDevice.sharedDevice.orientation = UIDeviceOrientationPortrait; |
28 | | - |
29 | | - [CameraPlugin registerWithRegistrar:self.mockRegistrar]; |
30 | 29 |
|
31 | 30 | [self rotate:UIDeviceOrientationPortraitUpsideDown expectedChannelOrientation:@"portraitDown"]; |
32 | 31 | [self rotate:UIDeviceOrientationPortrait expectedChannelOrientation:@"portraitUp"]; |
33 | 32 | [self rotate:UIDeviceOrientationLandscapeRight expectedChannelOrientation:@"landscapeLeft"]; |
34 | 33 | [self rotate:UIDeviceOrientationLandscapeLeft expectedChannelOrientation:@"landscapeRight"]; |
35 | 34 |
|
36 | 35 | OCMReject([mockMessenger sendOnChannel:[OCMArg any] message:[OCMArg any]]); |
37 | | - // No notification when orientation doesn't change. |
38 | | - XCUIDevice.sharedDevice.orientation = UIDeviceOrientationLandscapeLeft; |
| 36 | + |
39 | 37 | // No notification when flat. |
40 | | - XCUIDevice.sharedDevice.orientation = UIDeviceOrientationFaceUp; |
| 38 | + [self.cameraPlugin |
| 39 | + orientationChanged:[self createMockNotificationForOrientation:UIDeviceOrientationFaceUp]]; |
41 | 40 | // No notification when facedown. |
42 | | - XCUIDevice.sharedDevice.orientation = UIDeviceOrientationFaceDown; |
| 41 | + [self.cameraPlugin |
| 42 | + orientationChanged:[self createMockNotificationForOrientation:UIDeviceOrientationFaceDown]]; |
43 | 43 |
|
44 | 44 | OCMVerifyAll(mockMessenger); |
45 | 45 | } |
46 | 46 |
|
47 | 47 | - (void)rotate:(UIDeviceOrientation)deviceOrientation |
48 | | - expectedChannelOrientation:(NSString*)channelOrientation { |
| 48 | + expectedChannelOrientation:(NSString *)channelOrientation { |
49 | 49 | id mockMessenger = self.mockMessenger; |
50 | | - XCTestExpectation* orientationExpectation = [self expectationWithDescription:channelOrientation]; |
| 50 | + XCTestExpectation *orientationExpectation = [self expectationWithDescription:channelOrientation]; |
51 | 51 |
|
52 | 52 | OCMExpect([mockMessenger |
53 | 53 | 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]; |
57 | 57 | [orientationExpectation fulfill]; |
58 | 58 | return |
59 | 59 | [methodCall.method isEqualToString:@"orientation_changed"] && |
60 | 60 | [methodCall.arguments isEqualToDictionary:@{@"orientation" : channelOrientation}]; |
61 | 61 | }]]); |
62 | 62 |
|
63 | | - XCUIDevice.sharedDevice.orientation = deviceOrientation; |
| 63 | + [self.cameraPlugin |
| 64 | + orientationChanged:[self createMockNotificationForOrientation:deviceOrientation]]; |
64 | 65 | [self waitForExpectationsWithTimeout:30.0 handler:nil]; |
65 | 66 | } |
66 | 67 |
|
| 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 | + |
67 | 75 | @end |
0 commit comments