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 <UIKit/UIKit.h> |
| 7 #import <XCTest/XCTest.h> |
| 8 |
| 9 #include "base/mac/scoped_nsobject.h" |
| 10 #include "base/test/scoped_command_line.h" |
| 11 #import "ios/chrome/app/main_controller_private.h" |
| 12 #include "ios/chrome/browser/chrome_switches.h" |
| 13 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" |
| 14 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 15 #import "ios/chrome/browser/ui/tools_menu/tools_menu_view_controller.h" |
| 16 #import "ios/chrome/browser/ui/ui_util.h" |
| 17 #include "ios/chrome/grit/ios_strings.h" |
| 18 #import "ios/chrome/test/app/chrome_test_util.h" |
| 19 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" |
| 20 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 21 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 22 #include "ui/base/l10n/l10n_util.h" |
| 23 |
| 24 using chrome_test_util::buttonWithAccessibilityLabel; |
| 25 using chrome_test_util::buttonWithAccessibilityLabelId; |
| 26 using chrome_test_util::staticTextWithAccessibilityLabelId; |
| 27 |
| 28 namespace { |
| 29 |
| 30 // Returns the GREYMatcher for the button that closes the tab switcher. |
| 31 id<GREYMatcher> tabSwitcherCloseButton() { |
| 32 return buttonWithAccessibilityLabelId(IDS_IOS_TAB_STRIP_LEAVE_TAB_SWITCHER); |
| 33 } |
| 34 |
| 35 // Returns the GREYMatcher for the button that creates new non incognito tabs |
| 36 // from within the tab switcher. |
| 37 id<GREYMatcher> tabSwitcherNewTabButton() { |
| 38 return grey_allOf( |
| 39 buttonWithAccessibilityLabelId(IDS_IOS_TAB_SWITCHER_CREATE_NEW_TAB), |
| 40 grey_sufficientlyVisible(), nil); |
| 41 } |
| 42 |
| 43 // Returns the GREYMatcher for the button that creates new incognito tabs from |
| 44 // within the tab switcher. |
| 45 id<GREYMatcher> tabSwitcherNewIncognitoTabButton() { |
| 46 return grey_allOf(buttonWithAccessibilityLabelId( |
| 47 IDS_IOS_TAB_SWITCHER_CREATE_NEW_INCOGNITO_TAB), |
| 48 grey_sufficientlyVisible(), nil); |
| 49 } |
| 50 |
| 51 // Returns the GREYMatcher for the button to go to the non incognito panel in |
| 52 // the tab switcher. |
| 53 id<GREYMatcher> tabSwitcherHeaderPanelButton() { |
| 54 NSString* accessibility_label = l10n_util::GetNSStringWithFixup( |
| 55 IDS_IOS_TAB_SWITCHER_HEADER_NON_INCOGNITO_TABS); |
| 56 return grey_accessibilityLabel(accessibility_label); |
| 57 } |
| 58 |
| 59 // Returns the GREYMatcher for the button that closes tabs on iPad. |
| 60 id<GREYMatcher> closeTabButton() { |
| 61 return buttonWithAccessibilityLabelId(IDS_IOS_TOOLS_MENU_CLOSE_TAB); |
| 62 } |
| 63 |
| 64 // Opens a new incognito tabs using the tools menu. |
| 65 void openNewIncognitoTabUsingUI() { |
| 66 [ChromeEarlGreyUI openToolsMenu]; |
| 67 id<GREYMatcher> newIncognitoTabButtonMatcher = |
| 68 grey_accessibilityID(kToolsMenuNewIncognitoTabId); |
| 69 [[EarlGrey selectElementWithMatcher:newIncognitoTabButtonMatcher] |
| 70 performAction:grey_tap()]; |
| 71 } |
| 72 |
| 73 // Triggers the opening of the tab switcher by launching a command. Should be |
| 74 // called only when the tab switcher is not presented. |
| 75 void enterTabSwitcherWithCommand() { |
| 76 base::scoped_nsobject<GenericChromeCommand> command( |
| 77 [[GenericChromeCommand alloc] initWithTag:IDC_TOGGLE_TAB_SWITCHER]); |
| 78 chrome_test_util::RunCommandWithActiveViewController(command); |
| 79 } |
| 80 |
| 81 } // namespace |
| 82 |
| 83 @interface TabSwitcherControllerTestCase : ChromeTestCase |
| 84 @end |
| 85 |
| 86 @implementation TabSwitcherControllerTestCase { |
| 87 std::unique_ptr<base::test::ScopedCommandLine> scoped_command_line_; |
| 88 } |
| 89 |
| 90 - (void)setUp { |
| 91 [super setUp]; |
| 92 scoped_command_line_.reset(new base::test::ScopedCommandLine()); |
| 93 scoped_command_line_->GetProcessCommandLine()->AppendSwitch( |
| 94 switches::kEnableTabSwitcher); |
| 95 } |
| 96 |
| 97 // Checks that the tab switcher is not presented. |
| 98 - (void)assertTabSwitcherIsInactive { |
| 99 [[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; |
| 100 MainController* main_controller = chrome_test_util::GetMainController(); |
| 101 GREYAssertTrue(![main_controller isTabSwitcherActive], |
| 102 @"Tab Switcher should be inactive"); |
| 103 } |
| 104 |
| 105 // Checks that the tab switcher is active. |
| 106 - (void)assertTabSwitcherIsActive { |
| 107 [[GREYUIThreadExecutor sharedInstance] drainUntilIdle]; |
| 108 MainController* main_controller = chrome_test_util::GetMainController(); |
| 109 GREYAssertTrue([main_controller isTabSwitcherActive], |
| 110 @"Tab Switcher should be active"); |
| 111 } |
| 112 |
| 113 // Checks that the text associated with |messageId| is somewhere on screen. |
| 114 - (void)assertMessageIsVisible:(int)messageId { |
| 115 id<GREYMatcher> messageMatcher = |
| 116 grey_allOf(staticTextWithAccessibilityLabelId(messageId), |
| 117 grey_sufficientlyVisible(), nil); |
| 118 [[EarlGrey selectElementWithMatcher:messageMatcher] |
| 119 assertWithMatcher:grey_notNil()]; |
| 120 } |
| 121 |
| 122 // Checks that the text associated with |messageId| is not visible. |
| 123 - (void)assertMessageIsNotVisible:(int)messageId { |
| 124 id<GREYMatcher> messageMatcher = |
| 125 grey_allOf(staticTextWithAccessibilityLabelId(messageId), |
| 126 grey_sufficientlyVisible(), nil); |
| 127 [[EarlGrey selectElementWithMatcher:messageMatcher] |
| 128 assertWithMatcher:grey_nil()]; |
| 129 } |
| 130 |
| 131 // Tests entering and leaving the tab switcher. |
| 132 - (void)testEnteringTabSwitcher { |
| 133 if (!IsIPadIdiom()) |
| 134 return; |
| 135 |
| 136 [self assertTabSwitcherIsInactive]; |
| 137 |
| 138 enterTabSwitcherWithCommand(); |
| 139 [self assertTabSwitcherIsActive]; |
| 140 |
| 141 // Check that the "No Open Tabs" message is not displayed. |
| 142 [self assertMessageIsNotVisible: |
| 143 IDS_IOS_TAB_SWITCHER_NO_LOCAL_NON_INCOGNITO_TABS_TITLE]; |
| 144 |
| 145 // Press the :: icon to exit the tab switcher. |
| 146 [[EarlGrey selectElementWithMatcher:tabSwitcherCloseButton()] |
| 147 performAction:grey_tap()]; |
| 148 |
| 149 [self assertTabSwitcherIsInactive]; |
| 150 } |
| 151 |
| 152 // Tests entering tab switcher by closing all tabs, and leaving the tab switcher |
| 153 // by creating a new tab. |
| 154 - (void)testClosingAllTabsAndCreatingNewTab { |
| 155 if (!IsIPadIdiom()) |
| 156 return; |
| 157 |
| 158 [self assertTabSwitcherIsInactive]; |
| 159 |
| 160 // Close the tab. |
| 161 [[EarlGrey selectElementWithMatcher:closeTabButton()] |
| 162 performAction:grey_tap()]; |
| 163 |
| 164 [self assertTabSwitcherIsActive]; |
| 165 |
| 166 // Check that the "No Open Tabs" message is displayed. |
| 167 [self assertMessageIsVisible: |
| 168 IDS_IOS_TAB_SWITCHER_NO_LOCAL_NON_INCOGNITO_TABS_TITLE]; |
| 169 |
| 170 // Create a new tab. |
| 171 [[EarlGrey selectElementWithMatcher:tabSwitcherNewTabButton()] |
| 172 performAction:grey_tap()]; |
| 173 |
| 174 [self assertTabSwitcherIsInactive]; |
| 175 } |
| 176 |
| 177 // Tests entering tab switcher from incognito mode. |
| 178 - (void)testIncognitoTabs { |
| 179 if (!IsIPadIdiom()) |
| 180 return; |
| 181 |
| 182 [self assertTabSwitcherIsInactive]; |
| 183 |
| 184 // Create new incognito tab from tools menu. |
| 185 openNewIncognitoTabUsingUI(); |
| 186 |
| 187 // Close the incognito tab and check that the we are entering the tab |
| 188 // switcher. |
| 189 [[EarlGrey selectElementWithMatcher:closeTabButton()] |
| 190 performAction:grey_tap()]; |
| 191 [self assertTabSwitcherIsActive]; |
| 192 |
| 193 // Check that the "No Incognito Tabs" message is shown. |
| 194 [self assertMessageIsVisible: |
| 195 IDS_IOS_TAB_SWITCHER_NO_LOCAL_INCOGNITO_TABS_PROMO]; |
| 196 |
| 197 // Create new incognito tab. |
| 198 [[EarlGrey selectElementWithMatcher:tabSwitcherNewIncognitoTabButton()] |
| 199 performAction:grey_tap()]; |
| 200 |
| 201 // Verify that we've left the tab switcher. |
| 202 [self assertTabSwitcherIsInactive]; |
| 203 |
| 204 // Close tab and verify we've entered the tab switcher again. |
| 205 [[EarlGrey selectElementWithMatcher:closeTabButton()] |
| 206 performAction:grey_tap()]; |
| 207 [self assertTabSwitcherIsActive]; |
| 208 |
| 209 // Switch to the non incognito panel. |
| 210 [[EarlGrey selectElementWithMatcher:tabSwitcherHeaderPanelButton()] |
| 211 performAction:grey_tap()]; |
| 212 |
| 213 // Press the :: icon to exit the tab switcher. |
| 214 [[EarlGrey selectElementWithMatcher:tabSwitcherCloseButton()] |
| 215 performAction:grey_tap()]; |
| 216 |
| 217 // Verify that we've left the tab switcher. |
| 218 [self assertTabSwitcherIsInactive]; |
| 219 } |
| 220 |
| 221 @end |
OLD | NEW |