FBOrientationCommands.m 7.1 KB

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