FBOrientationCommands.m 7.2 KB

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