Skip to content

Commit deb623c

Browse files
feat: Expose device orientation via /wda/deviceOrientation (#1162)
1 parent bed8d1e commit deb623c

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

WebDriverAgentLib/Categories/XCUIDevice+FBRotation.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ NS_ASSUME_NONNULL_BEGIN
3232
/*! The UIDeviceOrientation to rotation mappings */
3333
@property (strong, nonatomic, readonly) NSDictionary *fb_rotationMapping;
3434

35+
/**
36+
The current physical device orientation as the raw UIDeviceOrientation name string,
37+
e.g. UIDeviceOrientationPortrait, UIDeviceOrientationLandscapeLeft,
38+
UIDeviceOrientationFaceUp. Returns UIDeviceOrientationUnknown if the orientation
39+
cannot be determined.
40+
*/
41+
@property (copy, nonatomic, readonly) NSString *fb_deviceOrientation;
42+
3543
@end
3644
#endif
3745

WebDriverAgentLib/Categories/XCUIDevice+FBRotation.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ - (BOOL)waitUntilInterfaceIsAtOrientation:(UIDeviceOrientation)orientation appli
6464
return application.interfaceOrientation == FBInterfaceOrientationFromDeviceOrientation(orientation);
6565
}
6666

67+
- (NSString *)fb_deviceOrientation
68+
{
69+
switch (self.orientation) {
70+
case UIDeviceOrientationPortrait:
71+
return @"UIDeviceOrientationPortrait";
72+
case UIDeviceOrientationPortraitUpsideDown:
73+
return @"UIDeviceOrientationPortraitUpsideDown";
74+
case UIDeviceOrientationLandscapeLeft:
75+
return @"UIDeviceOrientationLandscapeLeft";
76+
case UIDeviceOrientationLandscapeRight:
77+
return @"UIDeviceOrientationLandscapeRight";
78+
case UIDeviceOrientationFaceUp:
79+
return @"UIDeviceOrientationFaceUp";
80+
case UIDeviceOrientationFaceDown:
81+
return @"UIDeviceOrientationFaceDown";
82+
case UIDeviceOrientationUnknown:
83+
default:
84+
return @"UIDeviceOrientationUnknown";
85+
}
86+
}
87+
6788
- (NSDictionary *)fb_rotationMapping
6889
{
6990
static NSDictionary *rotationMap;

WebDriverAgentLib/Commands/FBOrientationCommands.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ + (NSArray *)routes
4747
[[FBRoute GET:@"/rotation"].withoutSession respondWithTarget:self action:@selector(handleGetRotation:)],
4848
[[FBRoute POST:@"/rotation"] respondWithTarget:self action:@selector(handleSetRotation:)],
4949
[[FBRoute POST:@"/rotation"].withoutSession respondWithTarget:self action:@selector(handleSetRotation:)],
50+
[[FBRoute GET:@"/wda/deviceOrientation"] respondWithTarget:self action:@selector(handleGetDeviceOrientation:)],
51+
[[FBRoute GET:@"/wda/deviceOrientation"].withoutSession respondWithTarget:self action:@selector(handleGetDeviceOrientation:)],
5052
];
5153
}
5254

@@ -113,6 +115,11 @@ + (NSArray *)routes
113115
return FBResponseWithOK();
114116
}
115117

118+
+ (id<FBResponsePayload>)handleGetDeviceOrientation:(FBRouteRequest *)request
119+
{
120+
return FBResponseWithObject(XCUIDevice.sharedDevice.fb_deviceOrientation);
121+
}
122+
116123

117124
#pragma mark - Helpers
118125

WebDriverAgentTests/IntegrationTests/XCUIDeviceRotationTests.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,27 @@ - (void)testLandscapeLeftRotation
6969
XCTAssertTrue(self.testedApplication.staticTexts[@"LandscapeRight"].exists);
7070
}
7171

72+
- (void)testGetDeviceOrientationInPortrait
73+
{
74+
BOOL success = [[XCUIDevice sharedDevice] fb_setDeviceInterfaceOrientation:UIDeviceOrientationPortrait];
75+
XCTAssertTrue(success, @"Device should support Portrait");
76+
XCTAssertEqualObjects([XCUIDevice sharedDevice].fb_deviceOrientation, @"UIDeviceOrientationPortrait");
77+
}
78+
79+
- (void)testGetDeviceOrientationInLandscapeLeft
80+
{
81+
BOOL success = [[XCUIDevice sharedDevice] fb_setDeviceInterfaceOrientation:UIDeviceOrientationLandscapeLeft];
82+
XCTAssertTrue(success, @"Device should support LandscapeLeft");
83+
XCTAssertEqualObjects([XCUIDevice sharedDevice].fb_deviceOrientation, @"UIDeviceOrientationLandscapeLeft");
84+
}
85+
86+
- (void)testGetDeviceOrientationInLandscapeRight
87+
{
88+
BOOL success = [[XCUIDevice sharedDevice] fb_setDeviceInterfaceOrientation:UIDeviceOrientationLandscapeRight];
89+
XCTAssertTrue(success, @"Device should support LandscapeRight");
90+
XCTAssertEqualObjects([XCUIDevice sharedDevice].fb_deviceOrientation, @"UIDeviceOrientationLandscapeRight");
91+
}
92+
7293
- (void)testRotationTiltRotation
7394
{
7495
UIDeviceOrientation currentRotation = [XCUIDevice sharedDevice].orientation;

0 commit comments

Comments
 (0)