-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathFBOrientationCommands.m
186 lines (161 loc) · 7.2 KB
/
FBOrientationCommands.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "FBOrientationCommands.h"
#import "XCUIDevice+FBRotation.h"
#import "FBRouteRequest.h"
#import "FBMacros.h"
#import "FBSession.h"
#import "XCUIApplication.h"
#import "XCUIApplication+FBHelpers.h"
#import "XCUIDevice.h"
extern const struct FBWDOrientationValues {
FBLiteralString portrait;
FBLiteralString landscapeLeft;
FBLiteralString landscapeRight;
FBLiteralString portraitUpsideDown;
} FBWDOrientationValues;
const struct FBWDOrientationValues FBWDOrientationValues = {
.portrait = @"PORTRAIT",
.landscapeLeft = @"LANDSCAPE",
.landscapeRight = @"UIA_DEVICE_ORIENTATION_LANDSCAPERIGHT",
.portraitUpsideDown = @"UIA_DEVICE_ORIENTATION_PORTRAIT_UPSIDEDOWN",
};
#if !TARGET_OS_TV
@implementation FBOrientationCommands
#pragma mark - <FBCommandHandler>
+ (NSArray *)routes
{
return
@[
[[FBRoute GET:@"/orientation"] respondWithTarget:self action:@selector(handleGetOrientation:)],
[[FBRoute GET:@"/orientation"].withoutSession respondWithTarget:self action:@selector(handleGetOrientation:)],
[[FBRoute POST:@"/orientation"] respondWithTarget:self action:@selector(handleSetOrientation:)],
[[FBRoute POST:@"/orientation"].withoutSession respondWithTarget:self action:@selector(handleSetOrientation:)],
[[FBRoute GET:@"/rotation"] respondWithTarget:self action:@selector(handleGetRotation:)],
[[FBRoute GET:@"/rotation"].withoutSession respondWithTarget:self action:@selector(handleGetRotation:)],
[[FBRoute POST:@"/rotation"] respondWithTarget:self action:@selector(handleSetRotation:)],
[[FBRoute POST:@"/rotation"].withoutSession respondWithTarget:self action:@selector(handleSetRotation:)],
];
}
#pragma mark - Commands
+ (id<FBResponsePayload>)handleGetOrientation:(FBRouteRequest *)request
{
XCUIApplication *application = request.session.activeApplication ?: XCUIApplication.fb_activeApplication;
NSString *orientation = [self.class interfaceOrientationForApplication:application];
return FBResponseWithObject([[self _wdOrientationsMapping] objectForKey:orientation]);
}
+ (id<FBResponsePayload>)handleSetOrientation:(FBRouteRequest *)request
{
XCUIApplication *application = request.session.activeApplication ?: XCUIApplication.fb_activeApplication;
if ([self.class setDeviceOrientation:request.arguments[@"orientation"] forApplication:application]) {
return FBResponseWithOK();
}
return FBResponseWithUnknownErrorFormat(@"Unable To Rotate Device");
}
+ (id<FBResponsePayload>)handleGetRotation:(FBRouteRequest *)request
{
XCUIDevice *device = [XCUIDevice sharedDevice];
XCUIApplication *application = request.session.activeApplication ?: XCUIApplication.fb_activeApplication;
UIInterfaceOrientation orientation = application.interfaceOrientation;
return FBResponseWithObject(device.fb_rotationMapping[@(orientation)]);
}
+ (id<FBResponsePayload>)handleSetRotation:(FBRouteRequest *)request
{
if (nil == request.arguments[@"x"] || nil == request.arguments[@"y"] || nil == request.arguments[@"z"]) {
NSString *errMessage = [NSString stringWithFormat:@"x, y and z arguments must exist in the request body: %@", request.arguments];
return FBResponseWithStatus([FBCommandStatus invalidArgumentErrorWithMessage:errMessage
traceback:nil]);
}
NSDictionary* rotation = @{
@"x": request.arguments[@"x"] ?: @0,
@"y": request.arguments[@"y"] ?: @0,
@"z": request.arguments[@"z"] ?: @0,
};
NSArray<NSDictionary *> *supportedRotations = XCUIDevice.sharedDevice.fb_rotationMapping.allValues;
if (![supportedRotations containsObject:rotation]) {
NSString *errMessage = [
NSString stringWithFormat:@"%@ rotation is not supported. Only the following values are supported: %@",
rotation, supportedRotations
];
return FBResponseWithStatus([FBCommandStatus invalidArgumentErrorWithMessage:errMessage
traceback:nil]);
}
XCUIApplication *application = request.session.activeApplication ?: XCUIApplication.fb_activeApplication;
if (![self.class setDeviceRotation:request.arguments forApplication:application]) {
NSString *errMessage = [
NSString stringWithFormat:@"The current rotation cannot be set to %@. Make sure the %@ application supports it",
rotation, application.bundleID
];
return FBResponseWithStatus([FBCommandStatus invalidElementStateErrorWithMessage:errMessage
traceback:nil]);
}
return FBResponseWithOK();
}
#pragma mark - Helpers
+ (NSString *)interfaceOrientationForApplication:(XCUIApplication *)application
{
NSNumber *orientation = @(application.interfaceOrientation);
NSSet *keys = [[self _orientationsMapping] keysOfEntriesPassingTest:^BOOL(id key, NSNumber *obj, BOOL *stop) {
return [obj isEqualToNumber:orientation];
}];
if (keys.count == 0) {
return @"Unknown orientation";
}
return keys.anyObject;
}
+ (BOOL)setDeviceRotation:(NSDictionary *)rotationObj forApplication:(XCUIApplication *)application
{
return [[XCUIDevice sharedDevice] fb_setDeviceRotation:rotationObj];
}
+ (BOOL)setDeviceOrientation:(NSString *)orientation forApplication:(XCUIApplication *)application
{
NSNumber *orientationValue = [[self _orientationsMapping] objectForKey:[orientation uppercaseString]];
if (orientationValue == nil) {
return NO;
}
return [[XCUIDevice sharedDevice] fb_setDeviceInterfaceOrientation:orientationValue.integerValue];
}
+ (NSDictionary *)_orientationsMapping
{
static NSDictionary *orientationMap;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
orientationMap =
@{
FBWDOrientationValues.portrait : @(UIDeviceOrientationPortrait),
FBWDOrientationValues.portraitUpsideDown : @(UIDeviceOrientationPortraitUpsideDown),
FBWDOrientationValues.landscapeLeft : @(UIDeviceOrientationLandscapeLeft),
FBWDOrientationValues.landscapeRight : @(UIDeviceOrientationLandscapeRight),
};
});
return orientationMap;
}
/*
We already have FBWDOrientationValues as orientation descriptions, however the strings are not valid
WebDriver responses. WebDriver can only receive 'portrait' or 'landscape'. So we can pass the keys
through this additional filter to ensure we get one of those. It's essentially a mapping from
FBWDOrientationValues to the valid subset of itself we can return to the client
*/
+ (NSDictionary *)_wdOrientationsMapping
{
static NSDictionary *orientationMap;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
orientationMap =
@{
FBWDOrientationValues.portrait : FBWDOrientationValues.portrait,
FBWDOrientationValues.portraitUpsideDown : FBWDOrientationValues.portrait,
FBWDOrientationValues.landscapeLeft : FBWDOrientationValues.landscapeLeft,
FBWDOrientationValues.landscapeRight : FBWDOrientationValues.landscapeLeft,
};
});
return orientationMap;
}
@end
#endif