Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import <XCTest/XCTest.h> | 5 #import <XCTest/XCTest.h> |
| 6 | 6 |
| 7 #include "base/ios/block_types.h" | 7 #include "base/ios/block_types.h" |
| 8 #include "base/mac/foundation_util.h" | 8 #include "base/mac/foundation_util.h" |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #import "base/test/ios/wait_util.h" | 10 #import "base/test/ios/wait_util.h" |
| 11 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 11 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 12 #include "ios/chrome/browser/pref_names.h" | 12 #include "ios/chrome/browser/pref_names.h" |
| 13 #import "ios/chrome/browser/tabs/tab.h" | 13 #import "ios/chrome/browser/tabs/tab.h" |
| 14 #import "ios/chrome/browser/tabs/tab_model.h" | 14 #import "ios/chrome/browser/tabs/tab_model.h" |
| 15 #import "ios/chrome/browser/ui/browser_view_controller.h" | 15 #import "ios/chrome/browser/ui/browser_view_controller.h" |
| 16 #import "ios/chrome/browser/ui/stack_view/card_view.h" | 16 #import "ios/chrome/browser/ui/stack_view/card_view.h" |
| 17 #import "ios/chrome/browser/ui/stack_view/stack_view_controller.h" | 17 #import "ios/chrome/browser/ui/stack_view/stack_view_controller.h" |
| 18 #import "ios/chrome/browser/ui/stack_view/stack_view_controller_private.h" | 18 #import "ios/chrome/browser/ui/stack_view/stack_view_controller_private.h" |
| 19 #import "ios/chrome/browser/ui/toolbar/toolbar_controller.h" | 19 #import "ios/chrome/browser/ui/toolbar/toolbar_controller.h" |
| 20 #import "ios/chrome/browser/ui/tools_menu/tools_menu_view_controller.h" | 20 #import "ios/chrome/browser/ui/tools_menu/tools_menu_view_controller.h" |
| 21 #import "ios/chrome/test/app/stack_view_test_util.h" | 21 #import "ios/chrome/test/app/stack_view_test_util.h" |
| 22 #import "ios/chrome/test/app/tab_test_util.h" | 22 #import "ios/chrome/test/app/tab_test_util.h" |
| 23 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" | 23 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 24 #import "ios/chrome/test/earl_grey/chrome_matchers.h" | 24 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 25 #import "ios/chrome/test/earl_grey/chrome_test_case.h" | 25 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 26 #include "ios/testing/earl_grey/disabled_test_macros.h" | 26 #include "ios/testing/earl_grey/disabled_test_macros.h" |
| 27 #import "ios/testing/wait_util.h" | |
| 27 | 28 |
| 28 #if !defined(__has_feature) || !__has_feature(objc_arc) | 29 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 29 #error "This file requires ARC support." | 30 #error "This file requires ARC support." |
| 30 #endif | 31 #endif |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 // Returns a GREYMatcher that matches |view|. | 34 // Returns a GREYMatcher that matches |view|. |
| 34 // TODO(crbug.com/642619): Evaluate whether this should be shared code. | 35 // TODO(crbug.com/642619): Evaluate whether this should be shared code. |
| 35 id<GREYMatcher> ViewMatchingView(UIView* view) { | 36 id<GREYMatcher> ViewMatchingView(UIView* view) { |
| 36 MatchesBlock matches = ^BOOL(UIView* viewToMatch) { | 37 MatchesBlock matches = ^BOOL(UIView* viewToMatch) { |
| 37 return viewToMatch == view; | 38 return viewToMatch == view; |
| 38 }; | 39 }; |
| 39 DescribeToBlock describe = ^void(id<GREYDescription> description) { | 40 DescribeToBlock describe = ^void(id<GREYDescription> description) { |
| 40 NSString* matcherDescription = | 41 NSString* matcherDescription = |
| 41 [NSString stringWithFormat:@"View matching %@", view]; | 42 [NSString stringWithFormat:@"View matching %@", view]; |
| 42 [description appendText:matcherDescription]; | 43 [description appendText:matcherDescription]; |
| 43 }; | 44 }; |
| 44 return [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches | 45 return [[GREYElementMatcherBlock alloc] initWithMatchesBlock:matches |
| 45 descriptionBlock:describe]; | 46 descriptionBlock:describe]; |
| 46 } | 47 } |
| 47 | 48 |
| 48 // Returns a matcher for the StackViewController's view. | 49 // Returns a matcher for the StackViewController's view. |
| 49 id<GREYMatcher> StackView() { | 50 id<GREYMatcher> StackView() { |
| 50 return ViewMatchingView([chrome_test_util::GetStackViewController() view]); | 51 return ViewMatchingView([chrome_test_util::GetStackViewController() view]); |
| 51 } | 52 } |
| 52 | 53 |
| 54 // Waits for the Stack View to be active/inactive. | |
| 55 void WaitForStackViewActive(bool active) { | |
|
Eugene But (OOO till 7-30)
2017/03/07 16:08:15
Do you want to return bool from this method and as
kkhorimoto
2017/03/07 19:05:03
The condition we're checking below is dependent on
Eugene But (OOO till 7-30)
2017/03/07 19:21:12
I did a search for WaitFor methods in EG tests and
kkhorimoto
2017/03/07 21:19:50
Fixed
| |
| 56 // Wait for the tab switcher to be active or inactive. | |
| 57 NSString* activeTabSwitcherDescription = | |
| 58 [NSString stringWithFormat:@"Waiting for tab switcher to be %@.", | |
| 59 active ? @"active" : @"inactive"]; | |
| 60 BOOL (^activeTabSwitcherBlock) | |
| 61 () = ^BOOL { | |
| 62 BOOL isActive = chrome_test_util::GetStackViewController() && | |
| 63 chrome_test_util::IsTabSwitcherActive(); | |
| 64 return active ? isActive : !isActive; | |
| 65 }; | |
| 66 GREYCondition* activeTabSwitcherCondition = | |
| 67 [GREYCondition conditionWithName:activeTabSwitcherDescription | |
| 68 block:activeTabSwitcherBlock]; | |
| 69 [activeTabSwitcherCondition | |
| 70 waitWithTimeout:testing::kWaitForUIElementTimeout]; | |
| 71 } | |
| 72 | |
| 53 // Waits for the Stack View to be visible/hidden. | 73 // Waits for the Stack View to be visible/hidden. |
| 54 void CheckForStackViewVisibility(bool visible) { | 74 void CheckForStackViewVisibility(bool visible) { |
| 75 // Verify the visibility of the stack view. | |
|
Eugene But (OOO till 7-30)
2017/03/07 16:08:15
Could you please promote this comment to a functio
kkhorimoto
2017/03/07 19:05:03
Done.
| |
| 55 id<GREYMatcher> visibilityMatcher = | 76 id<GREYMatcher> visibilityMatcher = |
| 56 grey_allOf(visible ? grey_sufficientlyVisible() : grey_notVisible(), | 77 grey_allOf(visible ? grey_sufficientlyVisible() : grey_notVisible(), |
| 57 visible ? grey_notNil() : grey_nil(), nil); | 78 visible ? grey_notNil() : grey_nil(), nil); |
| 58 [[EarlGrey selectElementWithMatcher:StackView()] | 79 [[EarlGrey selectElementWithMatcher:StackView()] |
| 59 assertWithMatcher:visibilityMatcher]; | 80 assertWithMatcher:visibilityMatcher]; |
| 60 } | 81 } |
| 61 | 82 |
| 62 // Opens the StackViewController. | 83 // Opens the StackViewController. |
| 63 void OpenStackView() { | 84 void OpenStackView() { |
| 64 if (chrome_test_util::IsTabSwitcherActive()) | 85 if (chrome_test_util::IsTabSwitcherActive()) |
| 65 return; | 86 return; |
| 66 // Tap on the toolbar's tab switcher button. | 87 // Tap on the toolbar's tab switcher button. |
| 67 id<GREYMatcher> stackButtonMatcher = | 88 id<GREYMatcher> stackButtonMatcher = |
| 68 grey_allOf(grey_accessibilityID(kToolbarStackButtonIdentifier), | 89 grey_allOf(grey_accessibilityID(kToolbarStackButtonIdentifier), |
| 69 grey_sufficientlyVisible(), nil); | 90 grey_sufficientlyVisible(), nil); |
| 70 [[EarlGrey selectElementWithMatcher:stackButtonMatcher] | 91 [[EarlGrey selectElementWithMatcher:stackButtonMatcher] |
| 71 performAction:grey_tap()]; | 92 performAction:grey_tap()]; |
| 72 // Verify that a StackViewController was presented. | 93 // Verify that a StackViewController was presented. |
| 94 WaitForStackViewActive(true); | |
| 73 CheckForStackViewVisibility(true); | 95 CheckForStackViewVisibility(true); |
| 74 } | 96 } |
| 75 | 97 |
| 76 // Shows either the normal or incognito deck. | 98 // Shows either the normal or incognito deck. |
| 77 enum class DeckType : bool { NORMAL, INCOGNITO }; | 99 enum class DeckType : bool { NORMAL, INCOGNITO }; |
| 78 void ShowDeckWithType(DeckType type) { | 100 void ShowDeckWithType(DeckType type) { |
| 79 OpenStackView(); | 101 OpenStackView(); |
| 80 StackViewController* stackViewController = | 102 StackViewController* stackViewController = |
| 81 chrome_test_util::GetStackViewController(); | 103 chrome_test_util::GetStackViewController(); |
| 82 UIView* activeDisplayView = stackViewController.activeCardSet.displayView; | 104 UIView* activeDisplayView = stackViewController.activeCardSet.displayView; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 98 } | 120 } |
| 99 } | 121 } |
| 100 | 122 |
| 101 // Opens a new tab using the stack view button. | 123 // Opens a new tab using the stack view button. |
| 102 void OpenNewTabUsingStackView() { | 124 void OpenNewTabUsingStackView() { |
| 103 // Open the stack view, tap the New Tab button, and wait for the animation to | 125 // Open the stack view, tap the New Tab button, and wait for the animation to |
| 104 // finish. | 126 // finish. |
| 105 ShowDeckWithType(DeckType::NORMAL); | 127 ShowDeckWithType(DeckType::NORMAL); |
| 106 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"New Tab")] | 128 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"New Tab")] |
| 107 performAction:grey_tap()]; | 129 performAction:grey_tap()]; |
| 130 WaitForStackViewActive(false); | |
| 108 CheckForStackViewVisibility(false); | 131 CheckForStackViewVisibility(false); |
| 109 } | 132 } |
| 110 | 133 |
| 111 // Opens the tools menu from the stack view. | 134 // Opens the tools menu from the stack view. |
| 112 void OpenToolsMenu() { | 135 void OpenToolsMenu() { |
| 113 OpenStackView(); | 136 OpenStackView(); |
| 114 [[EarlGrey selectElementWithMatcher:chrome_test_util::ToolsMenuButton()] | 137 [[EarlGrey selectElementWithMatcher:chrome_test_util::ToolsMenuButton()] |
| 115 performAction:grey_tap()]; | 138 performAction:grey_tap()]; |
| 116 } | 139 } |
| 117 | 140 |
| 118 // Opens a new Incognito Tab using the stack view button. | 141 // Opens a new Incognito Tab using the stack view button. |
| 119 void OpenNewIncognitoTabUsingStackView() { | 142 void OpenNewIncognitoTabUsingStackView() { |
| 120 OpenToolsMenu(); | 143 OpenToolsMenu(); |
| 121 NSString* newIncognitoTabID = kToolsMenuNewIncognitoTabId; | 144 NSString* newIncognitoTabID = kToolsMenuNewIncognitoTabId; |
| 122 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(newIncognitoTabID)] | 145 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(newIncognitoTabID)] |
| 123 performAction:grey_tap()]; | 146 performAction:grey_tap()]; |
| 147 WaitForStackViewActive(false); | |
| 124 CheckForStackViewVisibility(false); | 148 CheckForStackViewVisibility(false); |
| 125 } | 149 } |
| 126 | 150 |
| 127 // Taps the CardView associated with |tab|. | 151 // Taps the CardView associated with |tab|. |
| 128 void SelectTabUsingStackView(Tab* tab) { | 152 void SelectTabUsingStackView(Tab* tab) { |
| 129 DCHECK(tab); | 153 DCHECK(tab); |
| 130 // Present the StackViewController. | 154 // Present the StackViewController. |
| 131 OpenStackView(); | 155 OpenStackView(); |
| 132 // Get the StackCard associated with |tab|. | 156 // Get the StackCard associated with |tab|. |
| 133 StackViewController* stackViewController = | 157 StackViewController* stackViewController = |
| 134 chrome_test_util::GetStackViewController(); | 158 chrome_test_util::GetStackViewController(); |
| 135 StackCard* nextCard = [[stackViewController activeCardSet] cardForTab:tab]; | 159 StackCard* nextCard = [[stackViewController activeCardSet] cardForTab:tab]; |
| 136 UIView* card_title_label = static_cast<UIView*>([[nextCard view] titleLabel]); | 160 UIView* card_title_label = static_cast<UIView*>([[nextCard view] titleLabel]); |
| 137 [[EarlGrey selectElementWithMatcher:ViewMatchingView(card_title_label)] | 161 [[EarlGrey selectElementWithMatcher:ViewMatchingView(card_title_label)] |
| 138 performAction:grey_tap()]; | 162 performAction:grey_tap()]; |
| 139 // Wait for the StackViewController to be dismissed. | 163 // Wait for the StackViewController to be dismissed. |
| 164 WaitForStackViewActive(false); | |
| 140 CheckForStackViewVisibility(false); | 165 CheckForStackViewVisibility(false); |
| 141 // Checks that the next Tab has been selected. | 166 // Checks that the next Tab has been selected. |
| 142 GREYAssertEqual(tab, chrome_test_util::GetCurrentTab(), | 167 GREYAssertEqual(tab, chrome_test_util::GetCurrentTab(), |
| 143 @"The next Tab was not selected"); | 168 @"The next Tab was not selected"); |
| 144 } | 169 } |
| 145 } | 170 } |
| 146 | 171 |
| 147 // Tests for interacting with the StackViewController. | 172 // Tests for interacting with the StackViewController. |
| 148 @interface StackViewTestCase : ChromeTestCase | 173 @interface StackViewTestCase : ChromeTestCase |
| 149 @end | 174 @end |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 180 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(identifier)] | 205 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(identifier)] |
| 181 performAction:grey_tap()]; | 206 performAction:grey_tap()]; |
| 182 // Verify that the CardView and its associated Tab were removed. | 207 // Verify that the CardView and its associated Tab were removed. |
| 183 [[EarlGrey selectElementWithMatcher:ViewMatchingView(cardView)] | 208 [[EarlGrey selectElementWithMatcher:ViewMatchingView(cardView)] |
| 184 assertWithMatcher:grey_notVisible()]; | 209 assertWithMatcher:grey_notVisible()]; |
| 185 GREYAssertEqual(chrome_test_util::GetMainTabCount(), 0, | 210 GREYAssertEqual(chrome_test_util::GetMainTabCount(), 0, |
| 186 @"All Tabs should be closed."); | 211 @"All Tabs should be closed."); |
| 187 } | 212 } |
| 188 | 213 |
| 189 // Tests closing all Tabs in the stack view. | 214 // Tests closing all Tabs in the stack view. |
| 190 // TODO(crbug.com/693517): Re-enable this test on simulator. | 215 - (void)testCloseAllTabs { |
| 191 #if TARGET_IPHONE_SIMULATOR | |
| 192 #define MAYBE_testCloseAllTabs FLAKY_testCloseAllTabs | |
| 193 #else | |
| 194 #define MAYBE_testCloseAllTabs testCloseAllTabs | |
| 195 #endif | |
| 196 - (void)MAYBE_testCloseAllTabs { | |
| 197 // The StackViewController is only used on iPhones. | 216 // The StackViewController is only used on iPhones. |
| 198 if (IsIPadIdiom()) | 217 if (IsIPadIdiom()) |
| 199 EARL_GREY_TEST_SKIPPED(@"Stack view is not used on iPads."); | 218 EARL_GREY_TEST_SKIPPED(@"Stack view is not used on iPads."); |
| 200 // Open an incognito Tab. | 219 // Open an incognito Tab. |
| 201 OpenNewIncognitoTabUsingStackView(); | 220 OpenNewIncognitoTabUsingStackView(); |
| 202 GREYAssertEqual(chrome_test_util::GetIncognitoTabCount(), 1, | 221 GREYAssertEqual(chrome_test_util::GetIncognitoTabCount(), 1, |
| 203 @"Incognito Tab was not opened."); | 222 @"Incognito Tab was not opened."); |
| 204 // Open two additional Tabs. | 223 // Open two additional Tabs. |
| 205 const NSUInteger kAdditionalTabCount = 2; | 224 const NSUInteger kAdditionalTabCount = 2; |
| 206 for (NSUInteger i = 0; i < kAdditionalTabCount; ++i) | 225 for (NSUInteger i = 0; i < kAdditionalTabCount; ++i) |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 250 chrome_test_util::GetStackViewController(); | 269 chrome_test_util::GetStackViewController(); |
| 251 GREYAssert([stackViewController isCurrentSetIncognito], | 270 GREYAssert([stackViewController isCurrentSetIncognito], |
| 252 @"Incognito deck not selected."); | 271 @"Incognito deck not selected."); |
| 253 // Switch back to the main CardSet and verify that is selected. | 272 // Switch back to the main CardSet and verify that is selected. |
| 254 ShowDeckWithType(DeckType::NORMAL); | 273 ShowDeckWithType(DeckType::NORMAL); |
| 255 GREYAssert(![stackViewController isCurrentSetIncognito], | 274 GREYAssert(![stackViewController isCurrentSetIncognito], |
| 256 @"Normal deck not selected."); | 275 @"Normal deck not selected."); |
| 257 } | 276 } |
| 258 | 277 |
| 259 @end | 278 @end |
| OLD | NEW |