XCUIApplicationHelperTests.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <mach/mach_time.h>
  11. #import "FBIntegrationTestCase.h"
  12. #import "FBElement.h"
  13. #import "FBMacros.h"
  14. #import "FBTestMacros.h"
  15. #import "XCUIApplication.h"
  16. #import "XCUIApplication+FBHelpers.h"
  17. #import "XCUIElement+FBIsVisible.h"
  18. #import "FBXCodeCompatibility.h"
  19. void calculateMaxTreeDepth(NSDictionary *tree, NSNumber *currentDepth, NSNumber** maxDepth) {
  20. if (nil == maxDepth) {
  21. return;
  22. }
  23. NSArray *children = tree[@"children"];
  24. if (nil == children || 0 == children.count) {
  25. return;
  26. }
  27. for (NSDictionary *child in children) {
  28. if (currentDepth.integerValue > [*maxDepth integerValue]) {
  29. *maxDepth = currentDepth;
  30. }
  31. calculateMaxTreeDepth(child, @(currentDepth.integerValue + 1), maxDepth);
  32. }
  33. }
  34. @interface XCUIApplicationHelperTests : FBIntegrationTestCase
  35. @end
  36. @implementation XCUIApplicationHelperTests
  37. - (void)setUp
  38. {
  39. [super setUp];
  40. [self launchApplication];
  41. }
  42. - (void)testQueringSpringboard
  43. {
  44. [self goToSpringBoardFirstPage];
  45. XCTAssertTrue(XCUIApplication.fb_systemApplication.icons[@"Safari"].exists);
  46. XCTAssertTrue(XCUIApplication.fb_systemApplication.icons[@"Calendar"].firstMatch.exists);
  47. }
  48. - (void)testApplicationTree
  49. {
  50. NSDictionary *tree = self.testedApplication.fb_tree;
  51. XCTAssertNotNil(tree);
  52. NSNumber *maxDepth;
  53. calculateMaxTreeDepth(tree, @0, &maxDepth);
  54. XCTAssertGreaterThan(maxDepth.integerValue, 3);
  55. XCTAssertNotNil(self.testedApplication.fb_accessibilityTree);
  56. }
  57. - (void)testApplicationTreeAttributesFiltering
  58. {
  59. NSDictionary *applicationTree = [self.testedApplication fb_tree:[NSSet setWithArray:@[@"visible"]]];
  60. XCTAssertNotNil(applicationTree);
  61. XCTAssertNil([applicationTree objectForKey:@"isVisible"], @"'isVisible' key should not be present in the application tree");
  62. }
  63. - (void)testDeactivateApplication
  64. {
  65. NSError *error;
  66. uint64_t timeStarted = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
  67. NSTimeInterval backgroundDuration = 5.0;
  68. XCTAssertTrue([self.testedApplication fb_deactivateWithDuration:backgroundDuration error:&error]);
  69. NSTimeInterval timeElapsed = (clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) - timeStarted) / NSEC_PER_SEC;
  70. XCTAssertNil(error);
  71. XCTAssertEqualWithAccuracy(timeElapsed, backgroundDuration, 3.0);
  72. XCTAssertTrue(self.testedApplication.buttons[@"Alerts"].exists);
  73. }
  74. - (void)testActiveApplication
  75. {
  76. XCUIApplication *systemApp = XCUIApplication.fb_systemApplication;
  77. XCTAssertTrue([XCUIApplication fb_activeApplication].buttons[@"Alerts"].fb_isVisible);
  78. [self goToSpringBoardFirstPage];
  79. XCTAssertEqualObjects([XCUIApplication fb_activeApplication].bundleID, systemApp.bundleID);
  80. XCTAssertTrue(systemApp.icons[@"Safari"].fb_isVisible);
  81. }
  82. - (void)testActiveElement
  83. {
  84. [self goToAttributesPage];
  85. XCTAssertNil(self.testedApplication.fb_activeElement);
  86. XCUIElement *textField = self.testedApplication.textFields[@"aIdentifier"];
  87. [textField tap];
  88. FBAssertWaitTillBecomesTrue(nil != self.testedApplication.fb_activeElement);
  89. XCTAssertEqualObjects(((id<FBElement>)self.testedApplication.fb_activeElement).wdUID,
  90. ((id<FBElement>)textField).wdUID);
  91. }
  92. - (void)testActiveApplicationsInfo
  93. {
  94. NSArray *appsInfo = [XCUIApplication fb_activeAppsInfo];
  95. XCTAssertTrue(appsInfo.count > 0);
  96. BOOL isAppActive = NO;
  97. for (NSDictionary *appInfo in appsInfo) {
  98. if ([appInfo[@"bundleId"] isEqualToString:self.testedApplication.bundleID]) {
  99. isAppActive = YES;
  100. break;
  101. }
  102. }
  103. XCTAssertTrue(isAppActive);
  104. }
  105. - (void)testTestmanagerdVersion
  106. {
  107. XCTAssertGreaterThan(FBTestmanagerdVersion(), 0);
  108. }
  109. - (void)testAccessbilityAudit
  110. {
  111. if (SYSTEM_VERSION_LESS_THAN(@"17.0")) {
  112. return;
  113. }
  114. NSError *error;
  115. NSArray *auditIssues1 = [XCUIApplication.fb_activeApplication fb_performAccessibilityAuditWithAuditTypes:~0UL
  116. error:&error];
  117. XCTAssertNotNil(auditIssues1);
  118. XCTAssertNil(error);
  119. NSMutableSet *set = [NSMutableSet new];
  120. [set addObject:@"XCUIAccessibilityAuditTypeAll"];
  121. NSArray *auditIssues2 = [XCUIApplication.fb_activeApplication fb_performAccessibilityAuditWithAuditTypesSet:set.copy
  122. error:&error];
  123. // 'elementDescription' is not in this list because it could have
  124. // different object id's debug description in XCTest.
  125. NSArray *checkKeys = @[
  126. @"auditType",
  127. @"compactDescription",
  128. @"detailedDescription",
  129. @"element",
  130. @"elementAttributes"
  131. ];
  132. XCTAssertEqual([auditIssues1 count], [auditIssues2 count]);
  133. for (int i = 1; i < [auditIssues1 count]; i++) {
  134. for (NSString *k in checkKeys) {
  135. XCTAssertEqualObjects(
  136. [auditIssues1[i] objectForKey:k],
  137. [auditIssues2[i] objectForKey:k]
  138. );
  139. }
  140. }
  141. XCTAssertNil(error);
  142. }
  143. @end