FBApplication.m 6.7 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 "FBApplication.h"
  10. #import "FBXCAccessibilityElement.h"
  11. #import "FBLogger.h"
  12. #import "FBExceptions.h"
  13. #import "FBRunLoopSpinner.h"
  14. #import "FBMacros.h"
  15. #import "FBActiveAppDetectionPoint.h"
  16. #import "FBXCodeCompatibility.h"
  17. #import "FBXCTestDaemonsProxy.h"
  18. #import "XCUIApplication.h"
  19. #import "XCUIApplication+FBHelpers.h"
  20. #import "XCUIApplicationImpl.h"
  21. #import "XCUIApplicationProcess.h"
  22. #import "XCUIElement.h"
  23. #import "XCUIElementQuery.h"
  24. #import "FBXCAXClientProxy.h"
  25. static const NSTimeInterval APP_STATE_CHANGE_TIMEOUT = 5.0;
  26. @interface FBApplication ()
  27. @end
  28. @implementation FBApplication
  29. + (instancetype)fb_activeApplication
  30. {
  31. return [self fb_activeApplicationWithDefaultBundleId:nil];
  32. }
  33. + (NSArray<FBApplication *> *)fb_activeApplications
  34. {
  35. NSArray<id<FBXCAccessibilityElement>> *activeApplicationElements = [FBXCAXClientProxy.sharedClient activeApplications];
  36. NSMutableArray<FBApplication *> *result = [NSMutableArray array];
  37. if (activeApplicationElements.count > 0) {
  38. for (id<FBXCAccessibilityElement> applicationElement in activeApplicationElements) {
  39. FBApplication *app = [FBApplication fb_applicationWithPID:applicationElement.processIdentifier];
  40. if (nil != app) {
  41. [result addObject:app];
  42. }
  43. }
  44. }
  45. return result.count > 0 ? result.copy : @[self.class.fb_systemApplication];
  46. }
  47. + (instancetype)fb_activeApplicationWithDefaultBundleId:(nullable NSString *)bundleId
  48. {
  49. NSArray<id<FBXCAccessibilityElement>> *activeApplicationElements = [FBXCAXClientProxy.sharedClient activeApplications];
  50. id<FBXCAccessibilityElement> activeApplicationElement = nil;
  51. id<FBXCAccessibilityElement> currentElement = nil;
  52. if (nil != bundleId) {
  53. currentElement = FBActiveAppDetectionPoint.sharedInstance.axElement;
  54. if (nil != currentElement) {
  55. NSArray<NSDictionary *> *appInfos = [self fb_appsInfoWithAxElements:@[currentElement]];
  56. [FBLogger logFmt:@"Detected on-screen application: %@", appInfos.firstObject[@"bundleId"]];
  57. if ([[appInfos.firstObject objectForKey:@"bundleId"] isEqualToString:(id)bundleId]) {
  58. activeApplicationElement = currentElement;
  59. }
  60. }
  61. }
  62. if (nil == activeApplicationElement && activeApplicationElements.count > 1) {
  63. if (nil != bundleId) {
  64. NSArray<NSDictionary *> *appInfos = [self fb_appsInfoWithAxElements:activeApplicationElements];
  65. NSMutableArray<NSString *> *bundleIds = [NSMutableArray array];
  66. for (NSDictionary *appInfo in appInfos) {
  67. [bundleIds addObject:(NSString *)appInfo[@"bundleId"]];
  68. }
  69. [FBLogger logFmt:@"Detected system active application(s): %@", bundleIds];
  70. // Try to select the desired application first
  71. for (NSUInteger appIdx = 0; appIdx < appInfos.count; appIdx++) {
  72. if ([[[appInfos objectAtIndex:appIdx] objectForKey:@"bundleId"] isEqualToString:(id)bundleId]) {
  73. activeApplicationElement = [activeApplicationElements objectAtIndex:appIdx];
  74. break;
  75. }
  76. }
  77. }
  78. // Fall back to the "normal" algorithm if the desired application is either
  79. // not set or is not active
  80. if (nil == activeApplicationElement) {
  81. if (nil == currentElement) {
  82. currentElement = FBActiveAppDetectionPoint.sharedInstance.axElement;
  83. }
  84. if (nil == currentElement) {
  85. [FBLogger log:@"Cannot precisely detect the current application. Will use the system's recently active one"];
  86. if (nil == bundleId) {
  87. [FBLogger log:@"Consider changing the 'defaultActiveApplication' setting to the bundle identifier of the desired application under test"];
  88. }
  89. } else {
  90. for (id<FBXCAccessibilityElement> appElement in activeApplicationElements) {
  91. if (appElement.processIdentifier == currentElement.processIdentifier) {
  92. activeApplicationElement = appElement;
  93. break;
  94. }
  95. }
  96. }
  97. }
  98. }
  99. if (nil != activeApplicationElement) {
  100. FBApplication *application = [FBApplication fb_applicationWithPID:activeApplicationElement.processIdentifier];
  101. if (nil != application) {
  102. return application;
  103. }
  104. [FBLogger log:@"Cannot translate the active process identifier into an application object"];
  105. }
  106. if (activeApplicationElements.count > 0) {
  107. [FBLogger logFmt:@"Getting the most recent active application (out of %@ total items)", @(activeApplicationElements.count)];
  108. for (id<FBXCAccessibilityElement> appElement in activeApplicationElements) {
  109. FBApplication *application = [FBApplication fb_applicationWithPID:appElement.processIdentifier];
  110. if (nil != application) {
  111. return application;
  112. }
  113. }
  114. }
  115. [FBLogger log:@"Cannot retrieve any active applications. Assuming the system application is the active one"];
  116. return [self fb_systemApplication];
  117. }
  118. + (instancetype)fb_systemApplication
  119. {
  120. return [self fb_applicationWithPID:
  121. [[FBXCAXClientProxy.sharedClient systemApplication] processIdentifier]];
  122. }
  123. + (instancetype)applicationWithPID:(pid_t)processID
  124. {
  125. if ([NSProcessInfo processInfo].processIdentifier == processID) {
  126. return nil;
  127. }
  128. return (FBApplication *)[FBXCAXClientProxy.sharedClient monitoredApplicationWithProcessIdentifier:processID];
  129. }
  130. - (void)launch
  131. {
  132. [super launch];
  133. if (![self fb_waitForAppElement:APP_STATE_CHANGE_TIMEOUT]) {
  134. [FBLogger logFmt:@"The application '%@' is not running in foreground after %.2f seconds", self.bundleID, APP_STATE_CHANGE_TIMEOUT];
  135. }
  136. }
  137. - (void)terminate
  138. {
  139. [super terminate];
  140. if (![self waitForState:XCUIApplicationStateNotRunning timeout:APP_STATE_CHANGE_TIMEOUT]) {
  141. [FBLogger logFmt:@"The active application is still '%@' after %.2f seconds timeout", self.bundleID, APP_STATE_CHANGE_TIMEOUT];
  142. }
  143. }
  144. + (BOOL)fb_switchToSystemApplicationWithError:(NSError **)error
  145. {
  146. FBApplication *systemApp = self.fb_systemApplication;
  147. @try {
  148. if ([systemApp fb_state] < 2) {
  149. [systemApp launch];
  150. } else {
  151. [systemApp fb_activate];
  152. }
  153. } @catch (NSException *e) {
  154. return [[[FBErrorBuilder alloc]
  155. withDescription:nil == e ? @"Cannot open the home screen" : e.reason]
  156. buildError:error];
  157. }
  158. return [[[[FBRunLoopSpinner new]
  159. timeout:5]
  160. timeoutErrorMessage:@"Timeout waiting until the home screen is visible"]
  161. spinUntilTrue:^BOOL{
  162. FBApplication *activeApp = self.fb_activeApplication;
  163. return nil != activeApp && [activeApp.bundleID isEqualToString:systemApp.bundleID];
  164. }
  165. error:error];
  166. }
  167. @end