PerfControllerTests.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. // Non-google3 relative import to support building with Xcode.
  16. #import "../Source/ViewControllers/NetworkConnectionViewController+Accessibility.h"
  17. #import "../Source/ViewControllers/NetworkRequestsViewController.h"
  18. #import "../Source/ViewControllers/TracesViewController+Accessibility.h"
  19. #import "../Source/ViewControllers/TracesViewController.h"
  20. #import "../Source/Views/PerfTraceView+Accessibility.h"
  21. #import "third_party/objective_c/EarlGreyV2/TestLib/EarlGreyImpl/EarlGrey.h"
  22. const NSUInteger kStagesCount = 10;
  23. const NSUInteger kCounterTaps = 10;
  24. const NSUInteger kRequestsCount = 5;
  25. static NSString *const kTraceName = @"Trace 1";
  26. @interface PerfControllerTests : XCTestCase
  27. - (void)tapStageButtonNTimes:(NSUInteger)tapsCount;
  28. - (void)tapCountButtonsNTimes:(NSUInteger)tapsCount;
  29. @end
  30. @implementation PerfControllerTests {
  31. XCUIApplication *_application;
  32. }
  33. #pragma mark - Test cases
  34. - (void)setUp {
  35. [super setUp];
  36. static dispatch_once_t onceToken;
  37. dispatch_once(&onceToken, ^{
  38. _application = [[XCUIApplication alloc] init];
  39. [_application launch];
  40. });
  41. }
  42. - (void)testKeyWindow {
  43. [[EarlGrey selectElementWithMatcher:grey_keyWindow()]
  44. assertWithMatcher:grey_sufficientlyVisible()];
  45. }
  46. - (void)testTraceStages {
  47. [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"TracesTab")] performAction:grey_tap()];
  48. NSString *addTraceAccessibilityId =
  49. [GREY_REMOTE_CLASS_IN_APP(TracesViewController) addTraceAccessibilityItem].accessibilityID;
  50. [self tapElementWithMatcher:grey_accessibilityID(addTraceAccessibilityId)];
  51. [self tapStageButtonNTimes:kStagesCount];
  52. [self tapCountButtonsNTimes:kCounterTaps];
  53. NSString *stopAccessibilityId =
  54. [GREY_REMOTE_CLASS_IN_APP(PerfTraceView) stopAccessibilityItemWithTraceName:kTraceName]
  55. .accessibilityID;
  56. [self tapElementWithMatcher:grey_accessibilityID(stopAccessibilityId)];
  57. }
  58. - (void)testPerfURLConnectionWithDelegateClassInit {
  59. [self tapAndCheckConnectionTypeWithName:@"PerfURLConnectionWithDelegateClassInit"];
  60. }
  61. - (void)testPerfURLConnectionWithDelegate {
  62. [self tapAndCheckConnectionTypeWithName:@"PerfURLConnectionWithDelegate"];
  63. }
  64. - (void)testPerfURLConnectionWithDelegateStartImmediately {
  65. [self tapAndCheckConnectionTypeWithName:@"PerfURLConnectionWithDelegateStartImmediately"];
  66. }
  67. - (void)testPerfURLConnectionAsyncRequest {
  68. [self tapAndCheckConnectionTypeWithName:@"PerfURLConnectionAsyncRequest"];
  69. }
  70. - (void)testPerfURLSessionDownloadTaskWithDelegate {
  71. [self tapAndCheckConnectionTypeWithName:@"PerfURLSessionDownloadTaskWithDelegate"];
  72. }
  73. - (void)testPerfURLSessionDataTaskWithDelegate {
  74. [self tapAndCheckConnectionTypeWithName:@"PerfURLSessionDataTaskWithDelegate"];
  75. }
  76. - (void)testPerfURLSessionDownloadTask {
  77. [self tapAndCheckConnectionTypeWithName:@"PerfURLSessionDownloadTask"];
  78. }
  79. - (void)testPerfURLSessionDataTask {
  80. [self tapAndCheckConnectionTypeWithName:@"PerfURLSessionDataTask"];
  81. }
  82. #pragma mark - Private methods
  83. - (void)tapAndCheckConnectionTypeWithName:(NSString *)connectionName {
  84. [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"RequestsTab")]
  85. performAction:grey_tap()];
  86. NSString *conditionName =
  87. [NSString stringWithFormat:@"Check if %@ button visible", connectionName];
  88. GREYCondition *conditionForElement = [GREYCondition
  89. conditionWithName:conditionName
  90. block:^BOOL {
  91. NSError *error;
  92. [[EarlGrey selectElementWithMatcher:grey_buttonTitle(connectionName)]
  93. assertWithMatcher:grey_sufficientlyVisible()
  94. error:&error];
  95. return (error == nil);
  96. }];
  97. BOOL elementAppeared = [conditionForElement waitWithTimeout:1];
  98. if (!elementAppeared) {
  99. GREYElementInteraction *scrollViewInteractor =
  100. [EarlGrey selectElementWithMatcher:grey_accessibilityID(@"RequestsScrollView")];
  101. [scrollViewInteractor performAction:grey_swipeFastInDirection(kGREYDirectionUp)];
  102. }
  103. [self tapElementWithMatcher:grey_buttonTitle(connectionName)];
  104. AccessibilityItem *item = [GREY_REMOTE_CLASS_IN_APP(NetworkConnectionViewController)
  105. statusLabelAccessibilityItemWithConnectionName:connectionName];
  106. BOOL (^conditionBlock)(void) = ^BOOL {
  107. NSError *error = nil;
  108. [[EarlGrey selectElementWithMatcher:grey_accessibilityID(item.accessibilityID)]
  109. assertWithMatcher:grey_anyOf(grey_text(@"Success"), grey_text(@"Fail"), nil)
  110. error:&error];
  111. if (error) {
  112. NSLog(@"EarlGrey synchronization: status label hasn't been updated yet");
  113. }
  114. return error == nil;
  115. };
  116. BOOL succeeded = [[GREYCondition conditionWithName:@"Wait for status label to change text"
  117. block:conditionBlock] waitWithTimeout:30];
  118. XCTAssertTrue(succeeded);
  119. [[EarlGrey selectElementWithMatcher:grey_accessibilityID(item.accessibilityID)]
  120. assertWithMatcher:grey_text(@"Success")];
  121. }
  122. - (void)tapCountButtonsNTimes:(NSUInteger)tapsCount {
  123. for (int counterTapNumber = 0; counterTapNumber < tapsCount; counterTapNumber++) {
  124. NSString *counterAccessibilityID = counterTapNumber % 2 == 1
  125. ? [GREY_REMOTE_CLASS_IN_APP(PerfTraceView)
  126. metricOneAccessibilityItemWithTraceName:kTraceName]
  127. .accessibilityID
  128. : [GREY_REMOTE_CLASS_IN_APP(PerfTraceView)
  129. metricTwoAccessibilityItemWithTraceName:kTraceName]
  130. .accessibilityID;
  131. [self tapElementWithMatcher:grey_accessibilityID(counterAccessibilityID)];
  132. }
  133. }
  134. - (void)tapElementWithMatcher:(id<GREYMatcher>)matcher {
  135. [[EarlGrey selectElementWithMatcher:matcher] performAction:grey_tap()];
  136. }
  137. - (void)tapStageButtonNTimes:(NSUInteger)tapsCount {
  138. NSString *stageAccessibilityId =
  139. [GREY_REMOTE_CLASS_IN_APP(PerfTraceView) stageAccessibilityItemWithTraceName:kTraceName]
  140. .accessibilityID;
  141. for (int stageNumber = 0; stageNumber < tapsCount; stageNumber++) {
  142. [self tapElementWithMatcher:grey_accessibilityID(stageAccessibilityId)];
  143. }
  144. }
  145. @end