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 <EarlGrey/EarlGrey.h> |
| 6 #import <XCTest/XCTest.h> |
| 7 |
| 8 #include "components/strings/grit/components_strings.h" |
| 9 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" |
| 10 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 11 #import "ios/chrome/browser/ui/ntp/new_tab_page_controller.h" |
| 12 #include "ios/chrome/browser/ui/ui_util.h" |
| 13 #include "ios/chrome/grit/ios_strings.h" |
| 14 #import "ios/chrome/test/app/chrome_test_util.h" |
| 15 #import "ios/chrome/test/app/tab_test_util.h" |
| 16 #import "ios/chrome/test/earl_grey/accessibility_util.h" |
| 17 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 18 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 19 #import "ios/testing/earl_grey/disabled_test_macros.h" |
| 20 #import "ios/testing/wait_util.h" |
| 21 #include "ui/base/l10n/l10n_util.h" |
| 22 |
| 23 @implementation NewTabPageController (ExposedForTesting) |
| 24 - (GoogleLandingController*)googleLandingController { |
| 25 return googleLandingController_; |
| 26 } |
| 27 @end |
| 28 |
| 29 @interface GoogleLandingController (ExposedForTesting) |
| 30 - (BOOL)scrolledToTop; |
| 31 @end |
| 32 |
| 33 namespace { |
| 34 |
| 35 void DismissNewTabPagePanel() { |
| 36 if (!IsIPadIdiom()) { |
| 37 id<GREYMatcher> matcher = grey_allOf(grey_accessibilityID(@"Exit"), |
| 38 grey_sufficientlyVisible(), nil); |
| 39 [[EarlGrey selectElementWithMatcher:matcher] performAction:grey_tap()]; |
| 40 } |
| 41 } |
| 42 |
| 43 // Pauses until the history label has disappeared. History should not show on |
| 44 // incognito. |
| 45 void WaitForHistoryToDisappear() { |
| 46 [[GREYCondition |
| 47 conditionWithName:@"Wait for history to disappear" |
| 48 block:^BOOL { |
| 49 NSError* error = nil; |
| 50 NSString* history = |
| 51 l10n_util::GetNSString(IDS_HISTORY_SHOW_HISTORY); |
| 52 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel( |
| 53 history)] |
| 54 assertWithMatcher:grey_notVisible() |
| 55 error:&error]; |
| 56 return error == nil; |
| 57 }] waitWithTimeout:testing::kWaitForUIElementTimeout]; |
| 58 } |
| 59 |
| 60 // Displays the |panel_type| new tab page. An a phone this will send a command |
| 61 // to display a dialog, on tablet this calls -selectPanel to slide the NTP. |
| 62 void SelectNewTabPagePanel(NewTabPage::PanelIdentifier panel_type) { |
| 63 NewTabPageController* ntp_controller = |
| 64 chrome_test_util::GetCurrentNewTabPageController(); |
| 65 if (IsIPadIdiom()) { |
| 66 [ntp_controller selectPanel:panel_type]; |
| 67 } else { |
| 68 NSUInteger tag = 0; |
| 69 if (panel_type == NewTabPage::PanelIdentifier::kBookmarksPanel) { |
| 70 tag = IDC_SHOW_BOOKMARK_MANAGER; |
| 71 } else if (panel_type == NewTabPage::PanelIdentifier::kOpenTabsPanel) { |
| 72 tag = IDC_SHOW_OTHER_DEVICES; |
| 73 } |
| 74 if (tag) { |
| 75 base::scoped_nsobject<GenericChromeCommand> command( |
| 76 [[GenericChromeCommand alloc] initWithTag:tag]); |
| 77 chrome_test_util::RunCommandWithActiveViewController(command); |
| 78 } |
| 79 } |
| 80 [[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; |
| 81 } |
| 82 |
| 83 void AssertNTPScrolledToTop(bool scrolledToTop) { |
| 84 [[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; |
| 85 NewTabPageController* ntp_controller = |
| 86 chrome_test_util::GetCurrentNewTabPageController(); |
| 87 GoogleLandingController* google_landing_controller = |
| 88 [ntp_controller googleLandingController]; |
| 89 GREYAssertTrue([google_landing_controller scrolledToTop] == scrolledToTop, |
| 90 @"scrolledToTop_ does not match expected value"); |
| 91 } |
| 92 |
| 93 } // namespace |
| 94 |
| 95 @interface UIWindow (Hidden) |
| 96 - (UIResponder*)firstResponder; |
| 97 @end |
| 98 |
| 99 @interface NewTabPageTestCase : ChromeTestCase |
| 100 @end |
| 101 |
| 102 @implementation NewTabPageTestCase |
| 103 |
| 104 // Tests that all items are accessible on the most visited page. |
| 105 - (void)testAccessibilityOnMostVisited { |
| 106 SelectNewTabPagePanel(NewTabPage::kMostVisitedPanel); |
| 107 // TODO(crbug.com/640179): Implement and call |
| 108 chrome_test_util::VerifyAccessibilityForCurrentScreen(); |
| 109 } |
| 110 |
| 111 // Tests that all items are accessible on the open tabs page. |
| 112 - (void)testAccessibilityOnOpenTabs { |
| 113 SelectNewTabPagePanel(NewTabPage::kOpenTabsPanel); |
| 114 // TODO(crbug.com/640179): Implement and call |
| 115 chrome_test_util::VerifyAccessibilityForCurrentScreen(); |
| 116 DismissNewTabPagePanel(); |
| 117 } |
| 118 |
| 119 // Tests that all items are accessible on the bookmarks page. |
| 120 - (void)testAccessibilityOnBookmarks { |
| 121 SelectNewTabPagePanel(NewTabPage::kBookmarksPanel); |
| 122 // TODO(crbug.com/640179): Implement and call |
| 123 chrome_test_util::VerifyAccessibilityForCurrentScreen(); |
| 124 DismissNewTabPagePanel(); |
| 125 } |
| 126 |
| 127 // Tests that all items are accessible on the incognito page. |
| 128 - (void)testAccessibilityOnIncognitoTab { |
| 129 chrome_test_util::OpenNewIncognitoTab(); |
| 130 SelectNewTabPagePanel(NewTabPage::kIncognitoPanel); |
| 131 WaitForHistoryToDisappear(); |
| 132 // TODO(crbug.com/640179): Implement and call |
| 133 chrome_test_util::VerifyAccessibilityForCurrentScreen(); |
| 134 chrome_test_util::CloseAllIncognitoTabs(); |
| 135 } |
| 136 |
| 137 // Tests rotating the device when the NTP's omnibox is pinned to the top of the |
| 138 // screen. |
| 139 - (void)testRotation { |
| 140 // TODO(crbug.com/652465): Enable the test for iPad when rotation bug is |
| 141 // fixed. |
| 142 if (IsIPadIdiom()) { |
| 143 EARL_GREY_TEST_DISABLED(@"Disabled for iPad due to device rotation bug."); |
| 144 } |
| 145 |
| 146 NSString* ntpOmniboxLabel = l10n_util::GetNSString(IDS_OMNIBOX_EMPTY_HINT); |
| 147 NSString* focusedOmniboxLabel = l10n_util::GetNSString(IDS_ACCNAME_LOCATION); |
| 148 SelectNewTabPagePanel(NewTabPage::kMostVisitedPanel); |
| 149 AssertNTPScrolledToTop(NO); |
| 150 |
| 151 if (IsIPadIdiom()) { |
| 152 // Tap in omnibox to scroll it to the top of the screen. |
| 153 id<GREYMatcher> matcher = grey_accessibilityLabel(ntpOmniboxLabel); |
| 154 [[EarlGrey selectElementWithMatcher:matcher] performAction:grey_tap()]; |
| 155 } else { |
| 156 // Swipe up the NTP. The omnibox should be pinned at the top of the screen. |
| 157 id<GREYMatcher> matcher = grey_accessibilityID(@"Google Landing"); |
| 158 [[EarlGrey selectElementWithMatcher:matcher] |
| 159 performAction:grey_swipeFastInDirection(kGREYDirectionUp)]; |
| 160 } |
| 161 AssertNTPScrolledToTop(YES); |
| 162 |
| 163 // Restore the orientation of the device to portrait in Teardown. |
| 164 [self setTearDownHandler:^{ |
| 165 if ([UIDevice currentDevice].orientation != UIDeviceOrientationPortrait) { |
| 166 [EarlGrey rotateDeviceToOrientation:UIDeviceOrientationPortrait |
| 167 errorOrNil:nil]; |
| 168 } |
| 169 }]; |
| 170 |
| 171 // Rotate to landscape and check that the device and the status bar are |
| 172 // rotated properly and the omnibox is still scrolled up. |
| 173 [EarlGrey rotateDeviceToOrientation:UIDeviceOrientationLandscapeLeft |
| 174 errorOrNil:nil]; |
| 175 GREYAssertTrue( |
| 176 [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft, |
| 177 @"Device orientation should now be left landscape"); |
| 178 GREYAssertTrue([[UIApplication sharedApplication] statusBarOrientation] == |
| 179 UIInterfaceOrientationLandscapeRight, |
| 180 @"Status bar orientation should now be right landscape"); |
| 181 |
| 182 AssertNTPScrolledToTop(YES); |
| 183 |
| 184 // Tap in the omnibox. |
| 185 NSString* omniboxLabel = |
| 186 IsIPadIdiom() ? focusedOmniboxLabel : ntpOmniboxLabel; |
| 187 id<GREYMatcher> matcher = grey_allOf(grey_accessibilityLabel(omniboxLabel), |
| 188 grey_minimumVisiblePercent(0.2), nil); |
| 189 [[EarlGrey selectElementWithMatcher:matcher] performAction:grey_tap()]; |
| 190 |
| 191 // Rotate to portrait and check that the device and the status bar are rotated |
| 192 // properly and the omnibox is still scrolled up. |
| 193 [EarlGrey rotateDeviceToOrientation:UIDeviceOrientationPortrait |
| 194 errorOrNil:nil]; |
| 195 GREYAssertTrue( |
| 196 [UIDevice currentDevice].orientation == UIDeviceOrientationPortrait, |
| 197 @"Device orientation should now be portrait"); |
| 198 GREYAssertTrue([[UIApplication sharedApplication] statusBarOrientation] == |
| 199 UIInterfaceOrientationPortrait, |
| 200 @"Status bar orientation should now be portrait"); |
| 201 |
| 202 AssertNTPScrolledToTop(YES); |
| 203 |
| 204 // Check that omnibox is still focused. |
| 205 UIResponder* firstResponder = |
| 206 [[UIApplication sharedApplication].keyWindow firstResponder]; |
| 207 BOOL equal = |
| 208 [[firstResponder accessibilityLabel] isEqualToString:focusedOmniboxLabel]; |
| 209 GREYAssertTrue( |
| 210 equal, |
| 211 @"Expected accessibility label for first responder to be '%@', got '%@'", |
| 212 focusedOmniboxLabel, [firstResponder accessibilityLabel]); |
| 213 } |
| 214 |
| 215 // Tests focusing and defocusing the NTP's omnibox. |
| 216 - (void)testOmnibox { |
| 217 // Empty the pasteboard: if it contains a link the Google Landing will not be |
| 218 // interactable. |
| 219 [UIPasteboard generalPasteboard].string = @""; |
| 220 |
| 221 NSString* omniboxLabel = l10n_util::GetNSString(IDS_OMNIBOX_EMPTY_HINT); |
| 222 NSString* cancelLabel = l10n_util::GetNSString(IDS_CANCEL); |
| 223 if (IsIPadIdiom()) { |
| 224 SelectNewTabPagePanel(NewTabPage::kMostVisitedPanel); |
| 225 } |
| 226 |
| 227 // Check that the NTP is in its normal state. |
| 228 id<GREYMatcher> omnibox_matcher = |
| 229 grey_allOf(grey_accessibilityLabel(omniboxLabel), |
| 230 grey_minimumVisiblePercent(0.2), nil); |
| 231 [[EarlGrey selectElementWithMatcher:omnibox_matcher] |
| 232 assertWithMatcher:grey_notNil()]; |
| 233 AssertNTPScrolledToTop(NO); |
| 234 |
| 235 // Tap in omnibox to scroll it to the top of the screen. |
| 236 [[EarlGrey selectElementWithMatcher:omnibox_matcher] |
| 237 performAction:grey_tap()]; |
| 238 |
| 239 // Check that the omnibox is scrolled to the top of the screen and its cancel |
| 240 // button is visible. |
| 241 AssertNTPScrolledToTop(YES); |
| 242 |
| 243 if (!IsIPadIdiom()) { |
| 244 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(cancelLabel)] |
| 245 assertWithMatcher:grey_notNil()]; |
| 246 } |
| 247 |
| 248 // Swipe down in the NTP to return to the normal state. |
| 249 id<GREYMatcher> landing_matcher = grey_accessibilityID(@"Google Landing"); |
| 250 [[EarlGrey selectElementWithMatcher:landing_matcher] |
| 251 performAction:grey_swipeFastInDirection(kGREYDirectionDown)]; |
| 252 AssertNTPScrolledToTop(NO); |
| 253 if (!IsIPadIdiom()) { |
| 254 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(cancelLabel)] |
| 255 assertWithMatcher:grey_notVisible()]; |
| 256 } |
| 257 |
| 258 // Tap in omnibox and check that it's scrolled to the top of the screen and |
| 259 // its cancel button is visible. |
| 260 [[EarlGrey selectElementWithMatcher:omnibox_matcher] |
| 261 performAction:grey_tap()]; |
| 262 AssertNTPScrolledToTop(YES); |
| 263 |
| 264 if (!IsIPadIdiom()) { |
| 265 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(cancelLabel)] |
| 266 assertWithMatcher:grey_sufficientlyVisible()]; |
| 267 } |
| 268 // Tap below the omnibox to cancel editing and return to the normal state. |
| 269 [[EarlGrey selectElementWithMatcher:landing_matcher] |
| 270 performAction:grey_tap()]; |
| 271 AssertNTPScrolledToTop(NO); |
| 272 |
| 273 if (!IsIPadIdiom()) { |
| 274 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(cancelLabel)] |
| 275 assertWithMatcher:grey_notVisible()]; |
| 276 } |
| 277 } |
| 278 |
| 279 // Tests that the NTP's toolbar is not visible when the NTP is scrolled up. |
| 280 - (void)testScrollToolbar { |
| 281 if (IsIPadIdiom()) { |
| 282 EARL_GREY_TEST_SKIPPED(@"Skipped for iPad (no hidden toolbar in tablet)"); |
| 283 } |
| 284 |
| 285 NSString* tabSwitcherLabel = |
| 286 l10n_util::GetNSString(IDS_IOS_TOOLBAR_SHOW_TABS); |
| 287 NSString* toolsMenuLabel = l10n_util::GetNSString(IDS_IOS_TOOLBAR_SETTINGS); |
| 288 |
| 289 // Check that the toolbar's tab switcher and tools menu buttons are visible. |
| 290 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(tabSwitcherLabel)] |
| 291 assertWithMatcher:grey_sufficientlyVisible()]; |
| 292 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(toolsMenuLabel)] |
| 293 assertWithMatcher:grey_sufficientlyVisible()]; |
| 294 AssertNTPScrolledToTop(NO); |
| 295 |
| 296 // Swipe up the NTP. The omnibox should be fixed at the top of the screen. |
| 297 id<GREYMatcher> matcher = grey_accessibilityID(@"Google Landing"); |
| 298 [[EarlGrey selectElementWithMatcher:matcher] |
| 299 performAction:grey_swipeFastInDirection(kGREYDirectionUp)]; |
| 300 AssertNTPScrolledToTop(YES); |
| 301 |
| 302 // Check that tab switcher and tools menu buttons are not on screen. |
| 303 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(tabSwitcherLabel)] |
| 304 assertWithMatcher:grey_notVisible()]; |
| 305 [[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(toolsMenuLabel)] |
| 306 assertWithMatcher:grey_notVisible()]; |
| 307 } |
| 308 |
| 309 @end |
OLD | NEW |