OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import <XCTest/XCTest.h> |
| 6 |
| 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "components/strings/grit/components_strings.h" |
| 9 #import "ios/chrome/browser/find_in_page/find_in_page_controller.h" |
| 10 #import "ios/chrome/browser/ui/find_bar/find_bar_controller_ios.h" |
| 11 #import "ios/chrome/browser/ui/find_bar/find_bar_view.h" |
| 12 #import "ios/chrome/browser/ui/tools_menu/tools_menu_view_controller.h" |
| 13 #import "ios/chrome/test/app/tab_test_util.h" |
| 14 #import "ios/chrome/test/earl_grey/accessibility_util.h" |
| 15 #import "ios/chrome/test/earl_grey/chrome_assertions.h" |
| 16 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 17 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" |
| 18 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 19 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 20 #import "ios/testing/wait_util.h" |
| 21 #import "ios/web/public/test/http_server.h" |
| 22 #import "ios/web/public/test/http_server_util.h" |
| 23 #include "ui/base/l10n/l10n_util_mac.h" |
| 24 |
| 25 namespace { |
| 26 |
| 27 // Test web page content. |
| 28 const std::string kFindInPageResponse = "Find in page. Find in page."; |
| 29 |
| 30 } // namespace |
| 31 |
| 32 using chrome_test_util::buttonWithAccessibilityLabel; |
| 33 using chrome_test_util::webViewContainingText; |
| 34 |
| 35 // Tests for Find in Page. |
| 36 @interface FindInPageTestCase : ChromeTestCase |
| 37 |
| 38 // URL for a test page with |kFindInPageResponse|. |
| 39 @property(nonatomic, assign) GURL testURL; |
| 40 |
| 41 // Opens Find in Page. |
| 42 - (void)openFindInPage; |
| 43 // Closes Find in page. |
| 44 - (void)closeFindInPage; |
| 45 // Types text into Find in page textfield. |
| 46 - (void)typeFindInPageText:(NSString*)text; |
| 47 // Matcher for find in page textfield. |
| 48 - (id<GREYMatcher>)findInPageInputField; |
| 49 // Asserts that there is a string "|resultIndex| of |resultCount|" present on |
| 50 // screen. Waits for up to 2 seconds for this to happen. |
| 51 - (void)assertResultStringIsResult:(int)resultIndex outOfTotal:(int)resultCount; |
| 52 // Taps Next button in Find in page. |
| 53 - (void)advanceToNextResult; |
| 54 // Taps Previous button in Find in page. |
| 55 - (void)advanceToPreviousResult; |
| 56 // Navigates to |self.testURL| and waits for the page to load. |
| 57 - (void)navigateToTestPage; |
| 58 |
| 59 @end |
| 60 |
| 61 @implementation FindInPageTestCase |
| 62 @synthesize testURL = _testURL; |
| 63 |
| 64 #pragma mark - XCTest. |
| 65 |
| 66 // After setup, a page with |kFindInPageResponse| is displayed and Find In Page |
| 67 // bar is opened. |
| 68 - (void)setUp { |
| 69 [super setUp]; |
| 70 // Clear saved search term |
| 71 [FindInPageController setSearchTerm:nil]; |
| 72 |
| 73 // Setup find in page test URL. |
| 74 std::map<GURL, std::string> responses; |
| 75 self.testURL = web::test::HttpServer::MakeUrl("http://findinpage"); |
| 76 responses[self.testURL] = kFindInPageResponse; |
| 77 web::test::SetUpSimpleHttpServer(responses); |
| 78 |
| 79 [self navigateToTestPage]; |
| 80 |
| 81 // Open Find in Page view. |
| 82 [self openFindInPage]; |
| 83 } |
| 84 |
| 85 - (void)tearDown { |
| 86 // Close find in page view. |
| 87 [self closeFindInPage]; |
| 88 |
| 89 [super tearDown]; |
| 90 } |
| 91 |
| 92 #pragma mark - Tests. |
| 93 |
| 94 // Tests that find in page allows iteration between search results and displays |
| 95 // correct number of results. |
| 96 - (void)testFindInPage { |
| 97 // Type "find". |
| 98 [self typeFindInPageText:@"find"]; |
| 99 // Should be highlighting result 1 of 2. |
| 100 [self assertResultStringIsResult:1 outOfTotal:2]; |
| 101 // Tap Next. |
| 102 [self advanceToNextResult]; |
| 103 // Should now read "2 of 2". |
| 104 [self assertResultStringIsResult:2 outOfTotal:2]; |
| 105 // Go to previous. |
| 106 [self advanceToPreviousResult]; |
| 107 [self assertResultStringIsResult:1 outOfTotal:2]; |
| 108 } |
| 109 |
| 110 // Tests that Find In Page search term retention is working as expected, e.g. |
| 111 // the search term is persisted between FIP runs, but in incognito search term |
| 112 // is not retained and not autofilled. |
| 113 - (void)testFindInPageRetainsSearchTerm { |
| 114 // Type "find". |
| 115 [self typeFindInPageText:@"find"]; |
| 116 [self assertResultStringIsResult:1 outOfTotal:2]; |
| 117 [self closeFindInPage]; |
| 118 |
| 119 // Verify it's closed. |
| 120 ConditionBlock condition = ^{ |
| 121 NSError* error = nil; |
| 122 [[EarlGrey selectElementWithMatcher:grey_accessibilityID( |
| 123 kFindInPageContainerViewId)] |
| 124 assertWithMatcher:grey_nil() |
| 125 error:&error]; |
| 126 return (error == nil); |
| 127 }; |
| 128 GREYAssert(testing::WaitUntilConditionOrTimeout(2.0, condition), |
| 129 @"Timeout while waiting for Find Bar to close"); |
| 130 |
| 131 // Open incognito page. |
| 132 [ChromeEarlGreyUI openNewIncognitoTab]; |
| 133 [self navigateToTestPage]; |
| 134 [self openFindInPage]; |
| 135 // Check that no search term is prefilled. |
| 136 [[EarlGrey selectElementWithMatcher:[self findInPageInputField]] |
| 137 assertWithMatcher:grey_text(@"")]; |
| 138 [self typeFindInPageText:@"in"]; |
| 139 [self assertResultStringIsResult:1 outOfTotal:4]; |
| 140 [self closeFindInPage]; |
| 141 |
| 142 // Navigate to a new non-incognito tab. |
| 143 [ChromeEarlGreyUI openNewTab]; |
| 144 [self navigateToTestPage]; |
| 145 [self openFindInPage]; |
| 146 // Check that search term is retained from normal tab, not incognito tab. |
| 147 [[EarlGrey selectElementWithMatcher:[self findInPageInputField]] |
| 148 assertWithMatcher:grey_text(@"find")]; |
| 149 [self assertResultStringIsResult:1 outOfTotal:2]; |
| 150 } |
| 151 |
| 152 // Tests accessibility of the Find in Page screen. |
| 153 - (void)testAccessibilityOnFindInPage { |
| 154 [self typeFindInPageText:@"find"]; |
| 155 |
| 156 // Wait for UI to finish loading screen, before programatically verifying |
| 157 // accessibility. |
| 158 [[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; |
| 159 chrome_test_util::VerifyAccessibilityForCurrentScreen(); |
| 160 } |
| 161 |
| 162 #pragma mark - Steps. |
| 163 |
| 164 - (void)openFindInPage { |
| 165 [ChromeEarlGreyUI openToolsMenu]; |
| 166 [[EarlGrey |
| 167 selectElementWithMatcher:grey_accessibilityID(kToolsMenuFindInPageId)] |
| 168 performAction:grey_tap()]; |
| 169 } |
| 170 |
| 171 - (void)closeFindInPage { |
| 172 [[EarlGrey |
| 173 selectElementWithMatcher:grey_accessibilityID(kFindInPageCloseButtonId)] |
| 174 performAction:grey_tap()]; |
| 175 } |
| 176 |
| 177 - (void)typeFindInPageText:(NSString*)text { |
| 178 [[EarlGrey selectElementWithMatcher:[self findInPageInputField]] |
| 179 performAction:grey_typeText(text)]; |
| 180 } |
| 181 |
| 182 - (id<GREYMatcher>)findInPageInputField { |
| 183 return grey_accessibilityID(kFindInPageInputFieldId); |
| 184 } |
| 185 |
| 186 - (void)assertResultStringIsResult:(int)resultIndex |
| 187 outOfTotal:(int)resultCount { |
| 188 // Returns "<current> of <total>" search results label (e.g "1 of 5"). |
| 189 NSString* expectedResultsString = l10n_util::GetNSStringF( |
| 190 IDS_FIND_IN_PAGE_COUNT, base::IntToString16(resultIndex), |
| 191 base::IntToString16(resultCount)); |
| 192 |
| 193 ConditionBlock condition = ^{ |
| 194 NSError* error = nil; |
| 195 [[EarlGrey |
| 196 selectElementWithMatcher:grey_accessibilityLabel(expectedResultsString)] |
| 197 assertWithMatcher:grey_notNil() |
| 198 error:&error]; |
| 199 return (error == nil); |
| 200 }; |
| 201 GREYAssert( |
| 202 testing::WaitUntilConditionOrTimeout(2.0, condition), |
| 203 @"Timeout waiting for correct Find in Page results string to appear"); |
| 204 } |
| 205 |
| 206 - (void)advanceToNextResult { |
| 207 [[EarlGrey |
| 208 selectElementWithMatcher:grey_accessibilityID(kFindInPageNextButtonId)] |
| 209 performAction:grey_tap()]; |
| 210 } |
| 211 |
| 212 - (void)advanceToPreviousResult { |
| 213 [[EarlGrey selectElementWithMatcher:grey_accessibilityID( |
| 214 kFindInPagePreviousButtonId)] |
| 215 performAction:grey_tap()]; |
| 216 } |
| 217 |
| 218 - (void)navigateToTestPage { |
| 219 // Navigate to a page with some text. |
| 220 [ChromeEarlGrey loadURL:self.testURL]; |
| 221 |
| 222 // Verify web page finished loading. |
| 223 [[EarlGrey |
| 224 selectElementWithMatcher:webViewContainingText(kFindInPageResponse)] |
| 225 assertWithMatcher:grey_notNil()]; |
| 226 } |
| 227 |
| 228 @end |
OLD | NEW |