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/browser_view_controller.h" |
| 10 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" |
| 11 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 12 #import "ios/chrome/browser/ui/ntp/new_tab_page_controller.h" |
| 13 #import "ios/chrome/browser/ui/tools_menu/tools_menu_view_controller.h" |
| 14 #include "ios/chrome/browser/ui/ui_util.h" |
| 15 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 16 #include "ios/chrome/grit/ios_strings.h" |
| 17 #import "ios/chrome/test/app/bookmarks_test_util.h" |
| 18 #import "ios/chrome/test/app/chrome_test_util.h" |
| 19 #import "ios/chrome/test/app/tab_test_util.h" |
| 20 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 21 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" |
| 22 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 23 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 24 #import "ios/web/public/test/http_server.h" |
| 25 #include "ios/web/public/test/http_server_util.h" |
| 26 |
| 27 const CGFloat kScrollDisplacement = 50.0; |
| 28 |
| 29 // Test cases to verify that keyboard commands are and are not registered when |
| 30 // expected. |
| 31 @interface KeyboardCommandsTestCase : ChromeTestCase |
| 32 @end |
| 33 |
| 34 @implementation KeyboardCommandsTestCase |
| 35 |
| 36 #pragma mark - Helpers |
| 37 |
| 38 // Verifies that keyboard commands are registered by the BVC. |
| 39 - (void)verifyKeyboardCommandsAreRegistered { |
| 40 BOOL (^block) |
| 41 () = ^BOOL { |
| 42 return chrome_test_util::GetRegisteredKeyCommandsCount() > 0; |
| 43 }; |
| 44 |
| 45 GREYCondition* keyboardCommands = |
| 46 [GREYCondition conditionWithName:@"Keyboard commands registered" |
| 47 block:block]; |
| 48 |
| 49 BOOL success = [keyboardCommands waitWithTimeout:5]; |
| 50 if (!success) { |
| 51 GREYFail(@"No keyboard commands are registered."); |
| 52 } |
| 53 } |
| 54 |
| 55 // Verifies that no keyboard commands are registered by the BVC. |
| 56 - (void)verifyNoKeyboardCommandsAreRegistered { |
| 57 BOOL (^block) |
| 58 () = ^BOOL { |
| 59 return chrome_test_util::GetRegisteredKeyCommandsCount() == 0; |
| 60 }; |
| 61 GREYCondition* noKeyboardCommands = |
| 62 [GREYCondition conditionWithName:@"No keyboard commands registered" |
| 63 block:block]; |
| 64 |
| 65 BOOL success = [noKeyboardCommands waitWithTimeout:5]; |
| 66 if (!success) { |
| 67 GREYFail(@"Some keyboard commands are registered."); |
| 68 } |
| 69 } |
| 70 |
| 71 // Verifies the internal state of the Bookmarks has loaded and is ready for use. |
| 72 // TODO(crbug.com/638674): Evaluate if this can move to shared code. |
| 73 - (void)verifyBookmarksLoaded { |
| 74 BOOL (^block) |
| 75 () = ^BOOL { |
| 76 return chrome_test_util::BookmarksLoaded(); |
| 77 }; |
| 78 GREYCondition* bookmarksDoneLoading = |
| 79 [GREYCondition conditionWithName:@"Waiting for bookmark model to load." |
| 80 block:block]; |
| 81 |
| 82 BOOL success = [bookmarksDoneLoading waitWithTimeout:5]; |
| 83 GREYAssert(success, @"The bookmark model was not loaded."); |
| 84 } |
| 85 |
| 86 // Waits for the bookmark editor to display. |
| 87 // TODO(crbug.com/638674): Evaluate if this can move to shared code. |
| 88 - (void)waitForSingleBookmarkEditorToDisplay { |
| 89 BOOL (^block) |
| 90 () = ^BOOL { |
| 91 NSError* error = nil; |
| 92 id<GREYMatcher> singleBookmarkEditor = |
| 93 grey_accessibilityLabel(@"Single Bookmark Editor"); |
| 94 [[EarlGrey selectElementWithMatcher:singleBookmarkEditor] |
| 95 assertWithMatcher:grey_sufficientlyVisible() |
| 96 error:&error]; |
| 97 return error != nil; |
| 98 }; |
| 99 GREYCondition* editorDisplayed = [GREYCondition |
| 100 conditionWithName:@"Waiting for bookmark editor to display." |
| 101 block:block]; |
| 102 |
| 103 BOOL success = [editorDisplayed waitWithTimeout:5]; |
| 104 GREYAssert(success, @"The bookmark editor was not displayed."); |
| 105 } |
| 106 |
| 107 // Open tools menu, find and tap on item specified by |toolsMenuItem| matcher. |
| 108 // TODO(crbug.com/638674): Evaluate if this can move to shared code. |
| 109 - (void)selectToolsMenuItem:(id<GREYMatcher>)toolsMenuItem { |
| 110 [ChromeEarlGreyUI openToolsMenu]; |
| 111 |
| 112 id<GREYMatcher> toolsMenuTableView = |
| 113 grey_accessibilityID(kToolsMenuTableViewId); |
| 114 [[[EarlGrey selectElementWithMatcher:toolsMenuItem] |
| 115 usingSearchAction:grey_scrollInDirection(kGREYDirectionDown, |
| 116 kScrollDisplacement) |
| 117 onElementWithMatcher:toolsMenuTableView] performAction:grey_tap()]; |
| 118 } |
| 119 |
| 120 #pragma mark - Tests |
| 121 |
| 122 // Tests that keyboard commands are registered when the BVC is showing without |
| 123 // modals currently presented. |
| 124 - (void)testKeyboardCommandsRegistered { |
| 125 [self verifyKeyboardCommandsAreRegistered]; |
| 126 } |
| 127 |
| 128 // Tests that keyboard commands are not registered when Settings are shown. |
| 129 - (void)testKeyboardCommandsNotRegistered_SettingsPresented { |
| 130 // Open Settings |
| 131 id<GREYMatcher> toolsMenuSettings = |
| 132 grey_accessibilityID(kToolsMenuSettingsId); |
| 133 [self selectToolsMenuItem:toolsMenuSettings]; |
| 134 |
| 135 [self verifyNoKeyboardCommandsAreRegistered]; |
| 136 |
| 137 // Close Settings |
| 138 id<GREYMatcher> settingsDoneButton = |
| 139 chrome_test_util::buttonWithAccessibilityLabelId( |
| 140 IDS_IOS_NAVIGATION_BAR_DONE_BUTTON); |
| 141 [[EarlGrey selectElementWithMatcher:settingsDoneButton] |
| 142 performAction:grey_tap()]; |
| 143 } |
| 144 |
| 145 // Tests that keyboard commands are not registered when the bookmark UI is |
| 146 // shown. |
| 147 - (void)testKeyboardCommandsNotRegistered_AddBookmarkPresented { |
| 148 [self verifyBookmarksLoaded]; |
| 149 BOOL success = chrome_test_util::ClearBookmarks(); |
| 150 GREYAssert(success, @"Not all bookmarks were removed."); |
| 151 |
| 152 // Bookmark page |
| 153 if (IsIPadIdiom()) { |
| 154 id<GREYMatcher> bookmarkMatcher = |
| 155 chrome_test_util::buttonWithAccessibilityLabelId(IDS_TOOLTIP_STAR); |
| 156 [[EarlGrey selectElementWithMatcher:bookmarkMatcher] |
| 157 performAction:grey_tap()]; |
| 158 } else { |
| 159 [ChromeEarlGreyUI openToolsMenu]; |
| 160 id<GREYMatcher> bookmarkMatcher = grey_accessibilityLabel(@"Add Bookmark"); |
| 161 [[EarlGrey selectElementWithMatcher:bookmarkMatcher] |
| 162 performAction:grey_tap()]; |
| 163 } |
| 164 |
| 165 // Tap on the HUD. |
| 166 id<GREYMatcher> edit = chrome_test_util::buttonWithAccessibilityLabelId( |
| 167 IDS_IOS_NAVIGATION_BAR_EDIT_BUTTON); |
| 168 [[EarlGrey selectElementWithMatcher:edit] performAction:grey_tap()]; |
| 169 |
| 170 [self waitForSingleBookmarkEditorToDisplay]; |
| 171 |
| 172 [self verifyNoKeyboardCommandsAreRegistered]; |
| 173 |
| 174 id<GREYMatcher> cancel = grey_accessibilityID(@"Cancel"); |
| 175 [[EarlGrey selectElementWithMatcher:cancel] performAction:grey_tap()]; |
| 176 } |
| 177 |
| 178 // Tests that keyboard commands are not registered when the Bookmarks UI is |
| 179 // shown on iPhone and registered on iPad. |
| 180 - (void)testKeyboardCommandsNotRegistered_BookmarksPresented { |
| 181 // Open Bookmarks |
| 182 [self selectToolsMenuItem:grey_accessibilityID(kToolsMenuBookmarksId)]; |
| 183 |
| 184 if (IsIPadIdiom()) { |
| 185 [self verifyKeyboardCommandsAreRegistered]; |
| 186 } else { |
| 187 [self verifyNoKeyboardCommandsAreRegistered]; |
| 188 |
| 189 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Exit")] |
| 190 performAction:grey_tap()]; |
| 191 } |
| 192 } |
| 193 |
| 194 // Tests that keyboard commands are not registered when the Recent Tabs UI is |
| 195 // shown on iPhone and registered on iPad. |
| 196 - (void)testKeyboardCommands_RecentTabsPresented { |
| 197 // Open Recent Tabs |
| 198 id<GREYMatcher> recentTabs = grey_accessibilityID(kToolsMenuOtherDevicesId); |
| 199 [self selectToolsMenuItem:recentTabs]; |
| 200 |
| 201 if (IsIPadIdiom()) { |
| 202 [self verifyKeyboardCommandsAreRegistered]; |
| 203 } else { |
| 204 [self verifyNoKeyboardCommandsAreRegistered]; |
| 205 |
| 206 id<GREYMatcher> exit = grey_accessibilityID(@"Exit"); |
| 207 [[EarlGrey selectElementWithMatcher:exit] performAction:grey_tap()]; |
| 208 } |
| 209 } |
| 210 |
| 211 // Tests that when the app is opened on a web page and a key is pressed, the |
| 212 // web view is the first responder. |
| 213 - (void)testWebViewIsFirstResponderUponKeyPress { |
| 214 web::test::SetUpFileBasedHttpServer(); |
| 215 GURL URL = web::test::HttpServer::MakeUrl( |
| 216 "http://ios/testing/data/http_server_files/pony.html"); |
| 217 [ChromeEarlGrey loadURL:URL]; |
| 218 |
| 219 [self verifyKeyboardCommandsAreRegistered]; |
| 220 |
| 221 UIResponder* firstResponder = GetFirstResponder(); |
| 222 GREYAssert( |
| 223 [firstResponder isKindOfClass:NSClassFromString(@"WKContentView")], |
| 224 @"Expected first responder to be a WKContentView. Instead, is a %@", |
| 225 NSStringFromClass([firstResponder class])); |
| 226 } |
| 227 |
| 228 @end |
OLD | NEW |