XCUIDevice+FBRotation.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 "XCUIDevice+FBRotation.h"
  10. #import "FBConfiguration.h"
  11. #import "XCUIApplication.h"
  12. #import "XCUIApplication+FBHelpers.h"
  13. #import "XCUIElement+FBUtilities.h"
  14. # if !TARGET_OS_TV
  15. @implementation XCUIDevice (FBRotation)
  16. - (BOOL)fb_setDeviceInterfaceOrientation:(UIDeviceOrientation)orientation
  17. {
  18. XCUIApplication *application = XCUIApplication.fb_activeApplication;
  19. [XCUIDevice sharedDevice].orientation = orientation;
  20. return [self waitUntilInterfaceIsAtOrientation:orientation application:application];
  21. }
  22. - (BOOL)fb_setDeviceRotation:(NSDictionary *)rotationObj
  23. {
  24. NSArray<NSNumber *> *keysForRotationObj = [self.fb_rotationMapping allKeysForObject:rotationObj];
  25. if (keysForRotationObj.count == 0) {
  26. return NO;
  27. }
  28. NSInteger orientation = keysForRotationObj.firstObject.integerValue;
  29. XCUIApplication *application = XCUIApplication.fb_activeApplication;
  30. [XCUIDevice sharedDevice].orientation = orientation;
  31. return [self waitUntilInterfaceIsAtOrientation:orientation application:application];
  32. }
  33. - (BOOL)waitUntilInterfaceIsAtOrientation:(NSInteger)orientation application:(XCUIApplication *)application
  34. {
  35. // Tapping elements immediately after rotation may fail due to way UIKit is handling touches.
  36. // We should wait till UI cools off, before continuing
  37. [application fb_waitUntilStableWithTimeout:FBConfiguration.animationCoolOffTimeout];
  38. return application.interfaceOrientation == orientation;
  39. }
  40. - (NSDictionary *)fb_rotationMapping
  41. {
  42. static NSDictionary *rotationMap;
  43. static dispatch_once_t onceToken;
  44. dispatch_once(&onceToken, ^{
  45. rotationMap =
  46. @{
  47. @(UIDeviceOrientationUnknown) : @{@"x" : @(-1), @"y" : @(-1), @"z" : @(-1)},
  48. @(UIDeviceOrientationPortrait) : @{@"x" : @(0), @"y" : @(0), @"z" : @(0)},
  49. @(UIDeviceOrientationPortraitUpsideDown) : @{@"x" : @(0), @"y" : @(0), @"z" : @(180)},
  50. @(UIDeviceOrientationLandscapeLeft) : @{@"x" : @(0), @"y" : @(0), @"z" : @(270)},
  51. @(UIDeviceOrientationLandscapeRight) : @{@"x" : @(0), @"y" : @(0), @"z" : @(90)},
  52. @(UIDeviceOrientationFaceUp) : @{@"x" : @(90), @"y" : @(0), @"z" : @(0)},
  53. @(UIDeviceOrientationFaceDown) : @{@"x" : @(270), @"y" : @(0), @"z" : @(0)},
  54. };
  55. });
  56. return rotationMap;
  57. }
  58. @end
  59. #endif