XCUIDeviceHelperTests.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 <XCTest/XCTest.h>
  10. #import "FBIntegrationTestCase.h"
  11. #import "FBImageUtils.h"
  12. #import "FBMacros.h"
  13. #import "FBTestMacros.h"
  14. #import "XCUIApplication.h"
  15. #import "XCUIApplication+FBHelpers.h"
  16. #import "XCUIDevice+FBHelpers.h"
  17. #import "XCUIDevice+FBRotation.h"
  18. #import "XCUIScreen.h"
  19. @interface XCUIDeviceHelperTests : FBIntegrationTestCase
  20. @end
  21. @implementation XCUIDeviceHelperTests
  22. - (void)restorePortraitOrientation
  23. {
  24. if ([XCUIDevice sharedDevice].orientation != UIDeviceOrientationPortrait) {
  25. [[XCUIDevice sharedDevice] fb_setDeviceInterfaceOrientation:UIDeviceOrientationPortrait];
  26. }
  27. }
  28. - (void)setUp
  29. {
  30. [super setUp];
  31. [self launchApplication];
  32. [self restorePortraitOrientation];
  33. }
  34. - (void)tearDown
  35. {
  36. [self restorePortraitOrientation];
  37. [super tearDown];
  38. }
  39. - (void)testScreenshot
  40. {
  41. NSError *error = nil;
  42. NSData *screenshotData = [[XCUIDevice sharedDevice] fb_screenshotWithError:&error];
  43. XCTAssertNotNil(screenshotData);
  44. XCTAssertNil(error);
  45. XCTAssertTrue(FBIsPngImage(screenshotData));
  46. UIImage *screenshot = [UIImage imageWithData:screenshotData];
  47. XCTAssertNotNil(screenshot);
  48. XCUIScreen *mainScreen = XCUIScreen.mainScreen;
  49. UIImage *screenshotExact = ((XCUIScreenshot *)mainScreen.screenshot).image;
  50. XCTAssertEqualWithAccuracy(screenshotExact.size.height * mainScreen.scale,
  51. screenshot.size.height,
  52. FLT_EPSILON);
  53. XCTAssertEqualWithAccuracy(screenshotExact.size.width * mainScreen.scale,
  54. screenshot.size.width,
  55. FLT_EPSILON);
  56. }
  57. - (void)testLandscapeScreenshot
  58. {
  59. XCTAssertTrue([[XCUIDevice sharedDevice] fb_setDeviceInterfaceOrientation:UIDeviceOrientationLandscapeLeft]);
  60. NSError *error = nil;
  61. NSData *screenshotData = [[XCUIDevice sharedDevice] fb_screenshotWithError:&error];
  62. XCTAssertNotNil(screenshotData);
  63. XCTAssertTrue(FBIsPngImage(screenshotData));
  64. XCTAssertNil(error);
  65. UIImage *screenshot = [UIImage imageWithData:screenshotData];
  66. XCTAssertNotNil(screenshot);
  67. XCTAssertTrue(screenshot.size.width > screenshot.size.height);
  68. XCUIScreen *mainScreen = XCUIScreen.mainScreen;
  69. UIImage *screenshotExact = ((XCUIScreenshot *)mainScreen.screenshot).image;
  70. CGSize realMainScreenSize = screenshotExact.size.height > screenshot.size.width
  71. ? CGSizeMake(screenshotExact.size.height * mainScreen.scale, screenshotExact.size.width * mainScreen.scale)
  72. : CGSizeMake(screenshotExact.size.width * mainScreen.scale, screenshotExact.size.height * mainScreen.scale);
  73. XCTAssertEqualWithAccuracy(realMainScreenSize.height, screenshot.size.height, FLT_EPSILON);
  74. XCTAssertEqualWithAccuracy(realMainScreenSize.width, screenshot.size.width, FLT_EPSILON);
  75. }
  76. - (void)testWifiAddress
  77. {
  78. NSString *adderss = [XCUIDevice sharedDevice].fb_wifiIPAddress;
  79. if (!adderss) {
  80. return;
  81. }
  82. NSRange range = [adderss rangeOfString:@"^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})" options:NSRegularExpressionSearch];
  83. XCTAssertTrue(range.location != NSNotFound);
  84. }
  85. - (void)testGoToHomeScreen
  86. {
  87. NSError *error;
  88. XCTAssertTrue([[XCUIDevice sharedDevice] fb_goToHomescreenWithError:&error]);
  89. XCTAssertNil(error);
  90. XCTAssertTrue([XCUIApplication fb_activeApplication].icons[@"Safari"].exists);
  91. }
  92. - (void)testLockUnlockScreen
  93. {
  94. XCTAssertFalse([[XCUIDevice sharedDevice] fb_isScreenLocked]);
  95. NSError *error;
  96. XCTAssertTrue([[XCUIDevice sharedDevice] fb_lockScreen:&error]);
  97. XCTAssertTrue([[XCUIDevice sharedDevice] fb_isScreenLocked]);
  98. XCTAssertNil(error);
  99. XCTAssertTrue([[XCUIDevice sharedDevice] fb_unlockScreen:&error]);
  100. XCTAssertFalse([[XCUIDevice sharedDevice] fb_isScreenLocked]);
  101. XCTAssertNil(error);
  102. }
  103. - (void)testUrlSchemeActivation
  104. {
  105. if (SYSTEM_VERSION_LESS_THAN(@"16.4")) {
  106. return;
  107. }
  108. NSError *error;
  109. XCTAssertTrue([XCUIDevice.sharedDevice fb_openUrl:@"https://apple.com" error:&error]);
  110. FBAssertWaitTillBecomesTrue([XCUIApplication.fb_activeApplication.bundleID isEqualToString:@"com.apple.mobilesafari"]);
  111. XCTAssertNil(error);
  112. }
  113. - (void)testUrlSchemeActivationWithApp
  114. {
  115. if (SYSTEM_VERSION_LESS_THAN(@"16.4")) {
  116. return;
  117. }
  118. NSError *error;
  119. XCTAssertTrue([XCUIDevice.sharedDevice fb_openUrl:@"https://apple.com"
  120. withApplication:@"com.apple.mobilesafari"
  121. error:&error]);
  122. FBAssertWaitTillBecomesTrue([XCUIApplication.fb_activeApplication.bundleID isEqualToString:@"com.apple.mobilesafari"]);
  123. XCTAssertNil(error);
  124. }
  125. #if !TARGET_OS_TV
  126. - (void)testSimulatedLocationSetup
  127. {
  128. if (SYSTEM_VERSION_LESS_THAN(@"16.4")) {
  129. return;
  130. }
  131. CLLocation *simulatedLocation = [[CLLocation alloc] initWithLatitude:50 longitude:50];
  132. NSError *error;
  133. XCTAssertTrue([XCUIDevice.sharedDevice fb_setSimulatedLocation:simulatedLocation error:&error]);
  134. XCTAssertNil(error);
  135. CLLocation *currentLocation = [XCUIDevice.sharedDevice fb_getSimulatedLocation:&error];
  136. XCTAssertNil(error);
  137. XCTAssertNotNil(currentLocation);
  138. XCTAssertEqualWithAccuracy(simulatedLocation.coordinate.latitude, currentLocation.coordinate.latitude, 0.1);
  139. XCTAssertEqualWithAccuracy(simulatedLocation.coordinate.longitude, currentLocation.coordinate.longitude, 0.1);
  140. XCTAssertTrue([XCUIDevice.sharedDevice fb_clearSimulatedLocation:&error]);
  141. XCTAssertNil(error);
  142. currentLocation = [XCUIDevice.sharedDevice fb_getSimulatedLocation:&error];
  143. XCTAssertNil(error);
  144. XCTAssertNotEqualWithAccuracy(simulatedLocation.coordinate.latitude, currentLocation.coordinate.latitude, 0.1);
  145. XCTAssertNotEqualWithAccuracy(simulatedLocation.coordinate.longitude, currentLocation.coordinate.longitude, 0.1);
  146. }
  147. #endif
  148. - (void)testPressingUnsupportedButton
  149. {
  150. NSError *error;
  151. NSNumber *duration = nil;
  152. XCTAssertFalse([XCUIDevice.sharedDevice fb_pressButton:@"volumeUpp"
  153. forDuration:duration
  154. error:&error]);
  155. XCTAssertNotNil(error);
  156. }
  157. - (void)testPressingSupportedButton
  158. {
  159. NSError *error;
  160. XCTAssertTrue([XCUIDevice.sharedDevice fb_pressButton:@"home"
  161. forDuration:nil
  162. error:&error]);
  163. XCTAssertNil(error);
  164. }
  165. - (void)testPressingSupportedButtonNumber
  166. {
  167. NSError *error;
  168. XCTAssertTrue([XCUIDevice.sharedDevice fb_pressButton:@"home"
  169. forDuration:[NSNumber numberWithDouble:1.0]
  170. error:&error]);
  171. XCTAssertNil(error);
  172. }
  173. - (void)testLongPressHomeButton
  174. {
  175. NSError *error;
  176. // kHIDPage_Consumer = 0x0C
  177. // kHIDUsage_Csmr_Menu = 0x40
  178. XCTAssertTrue([XCUIDevice.sharedDevice fb_performIOHIDEventWithPage:0x0C
  179. usage:0x40
  180. duration:1.0
  181. error:&error]);
  182. XCTAssertNil(error);
  183. }
  184. - (void)testAppearance
  185. {
  186. if (SYSTEM_VERSION_LESS_THAN(@"15.0")) {
  187. return;
  188. }
  189. NSError *error;
  190. XCTAssertTrue([XCUIDevice.sharedDevice fb_setAppearance:FBUIInterfaceAppearanceDark error:&error]);
  191. XCTAssertNil(error);
  192. }
  193. @end