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/strings/sys_string_conversions.h" |
| 10 #include "ios/chrome/browser/ui/ui_util.h" |
| 11 #include "ios/chrome/grit/ios_strings.h" |
| 12 #import "ios/chrome/test/app/chrome_test_util.h" |
| 13 #import "ios/chrome/test/app/settings_test_util.h" |
| 14 #import "ios/chrome/test/app/tab_test_util.h" |
| 15 #include "ios/chrome/test/app/web_view_interaction_test_util.h" |
| 16 #import "ios/chrome/test/earl_grey/chrome_actions.h" |
| 17 #import "ios/chrome/test/earl_grey/chrome_assertions.h" |
| 18 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 19 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 20 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 21 #include "ios/chrome/test/earl_grey/chrome_util.h" |
| 22 #import "ios/testing/wait_util.h" |
| 23 #import "ios/web/public/test/earl_grey/web_view_matchers.h" |
| 24 #import "ios/web/public/test/http_server.h" |
| 25 #import "ios/web/public/test/http_server_util.h" |
| 26 #include "url/gurl.h" |
| 27 |
| 28 using chrome_test_util::buttonWithAccessibilityLabelId; |
| 29 |
| 30 namespace { |
| 31 const char kUrlChromiumLogoPage[] = |
| 32 "http://ios/testing/data/http_server_files/chromium_logo_page.html"; |
| 33 const char kUrlChromiumLogoImg[] = |
| 34 "http://ios/testing/data/http_server_files/chromium_logo.png"; |
| 35 const char kUrlInitialPage[] = "http://scenarioContextMenuOpenInNewTab"; |
| 36 const char kUrlDestinationPage[] = "http://destination"; |
| 37 const char kChromiumImageID[] = "chromium_image"; |
| 38 const char kDestinationLinkID[] = "link"; |
| 39 |
| 40 // HTML content of the destination page that sets the page title. |
| 41 const char kDestinationHtml[] = |
| 42 "<script>document.title='new doc'</script>You made it!"; |
| 43 |
| 44 // Matcher for the open image button in the context menu. |
| 45 id<GREYMatcher> openImageButton() { |
| 46 return buttonWithAccessibilityLabelId(IDS_IOS_CONTENT_CONTEXT_OPENIMAGE); |
| 47 } |
| 48 |
| 49 // Matcher for the open image in new tab button in the context menu. |
| 50 id<GREYMatcher> openImageInNewTabButton() { |
| 51 return buttonWithAccessibilityLabelId( |
| 52 IDS_IOS_CONTENT_CONTEXT_OPENIMAGENEWTAB); |
| 53 } |
| 54 |
| 55 // Matcher for the open link in new tab button in the context menu. |
| 56 id<GREYMatcher> openLinkInNewTabButton() { |
| 57 return buttonWithAccessibilityLabelId(IDS_IOS_CONTENT_CONTEXT_OPENLINKNEWTAB); |
| 58 } |
| 59 |
| 60 // Long press on |elementId| to trigger context menu and then tap on |
| 61 // |contextMenuItemButton| item. |
| 62 void LongPressElementAndTapOnButton(const char* elementId, |
| 63 id<GREYMatcher> contextMenuItemButton) { |
| 64 [[EarlGrey selectElementWithMatcher:chrome_test_util:: |
| 65 webViewBelongingToWebController()] |
| 66 performAction:chrome_test_util::longPressElementForContextMenu(elementId, |
| 67 true)]; |
| 68 |
| 69 [[EarlGrey selectElementWithMatcher:contextMenuItemButton] |
| 70 assertWithMatcher:grey_notNil()]; |
| 71 [[EarlGrey selectElementWithMatcher:contextMenuItemButton] |
| 72 performAction:grey_tap()]; |
| 73 [[EarlGrey selectElementWithMatcher:contextMenuItemButton] |
| 74 assertWithMatcher:grey_nil()]; |
| 75 } |
| 76 |
| 77 // A simple wrapper that sleeps for 1s to wait for the animation, triggered from |
| 78 // opening a new tab through context menu, to finish before selecting tab. |
| 79 // TODO(crbug.com/643792): Remove this function when the bug is fixed. |
| 80 void SelectTabAtIndexInCurrentMode(NSUInteger index) { |
| 81 // Delay for 1 second. |
| 82 GREYCondition* myCondition = [GREYCondition conditionWithName:@"delay" |
| 83 block:^BOOL { |
| 84 return NO; |
| 85 }]; |
| 86 [myCondition waitWithTimeout:1U]; |
| 87 |
| 88 chrome_test_util::SelectTabAtIndexInCurrentMode(index); |
| 89 } |
| 90 |
| 91 } // namespace |
| 92 |
| 93 // Context menu tests for Chrome. |
| 94 @interface ContextMenuTestCase : ChromeTestCase |
| 95 @end |
| 96 |
| 97 @implementation ContextMenuTestCase |
| 98 |
| 99 + (void)setUp { |
| 100 [super setUp]; |
| 101 chrome_test_util::SetContentSettingsBlockPopups(CONTENT_SETTING_ALLOW); |
| 102 } |
| 103 |
| 104 + (void)tearDown { |
| 105 chrome_test_util::SetContentSettingsBlockPopups(CONTENT_SETTING_DEFAULT); |
| 106 [super tearDown]; |
| 107 } |
| 108 |
| 109 // Tests that selecting "Open Image" from the context menu properly opens the |
| 110 // image in the current tab. |
| 111 - (void)testOpenImageInCurrentTabFromContextMenu { |
| 112 GURL pageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoPage); |
| 113 GURL imageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoImg); |
| 114 web::test::SetUpFileBasedHttpServer(); |
| 115 [ChromeEarlGrey loadURL:pageURL]; |
| 116 chrome_test_util::AssertMainTabCount(1U); |
| 117 |
| 118 LongPressElementAndTapOnButton(kChromiumImageID, openImageButton()); |
| 119 |
| 120 // Verify url and tab count. |
| 121 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText( |
| 122 imageURL.GetContent())] |
| 123 assertWithMatcher:grey_notNil()]; |
| 124 chrome_test_util::AssertMainTabCount(1U); |
| 125 } |
| 126 |
| 127 // Tests that selecting "Open Image in New Tab" from the context menu properly |
| 128 // opens the image in a new background tab. |
| 129 - (void)testOpenImageInNewTabFromContextMenu { |
| 130 GURL pageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoPage); |
| 131 GURL imageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoImg); |
| 132 web::test::SetUpFileBasedHttpServer(); |
| 133 [ChromeEarlGrey loadURL:pageURL]; |
| 134 chrome_test_util::AssertMainTabCount(1U); |
| 135 |
| 136 LongPressElementAndTapOnButton(kChromiumImageID, openImageInNewTabButton()); |
| 137 |
| 138 SelectTabAtIndexInCurrentMode(1U); |
| 139 |
| 140 // Verify url and tab count. |
| 141 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText( |
| 142 imageURL.GetContent())] |
| 143 assertWithMatcher:grey_notNil()]; |
| 144 chrome_test_util::AssertMainTabCount(2U); |
| 145 } |
| 146 |
| 147 // Tests "Open in New Tab" on context menu. |
| 148 - (void)testContextMenuOpenInNewTab { |
| 149 // Set up test simple http server. |
| 150 std::map<GURL, std::string> responses; |
| 151 GURL initialURL = web::test::HttpServer::MakeUrl(kUrlInitialPage); |
| 152 GURL destinationURL = web::test::HttpServer::MakeUrl(kUrlDestinationPage); |
| 153 |
| 154 // The initial page contains a link to the destination page. |
| 155 responses[initialURL] = "<a style='margin-left:50px' href='" + |
| 156 destinationURL.spec() + "' id='link'>link</a>"; |
| 157 responses[destinationURL] = kDestinationHtml; |
| 158 |
| 159 web::test::SetUpSimpleHttpServer(responses); |
| 160 [ChromeEarlGrey loadURL:initialURL]; |
| 161 chrome_test_util::AssertMainTabCount(1U); |
| 162 |
| 163 LongPressElementAndTapOnButton(kDestinationLinkID, openLinkInNewTabButton()); |
| 164 |
| 165 SelectTabAtIndexInCurrentMode(1U); |
| 166 |
| 167 // Verify url and tab count. |
| 168 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText( |
| 169 destinationURL.GetContent())] |
| 170 assertWithMatcher:grey_notNil()]; |
| 171 chrome_test_util::AssertMainTabCount(2U); |
| 172 } |
| 173 |
| 174 // Tests "Open in New Tab" on context menu on a link that requires scrolling |
| 175 // on the page to verify that context menu can be properly triggered in the |
| 176 // current screen view. |
| 177 - (void)testContextMenuOpenInNewTabFromTallPage { |
| 178 // Set up test simple http server. |
| 179 std::map<GURL, std::string> responses; |
| 180 GURL initialURL = |
| 181 web::test::HttpServer::MakeUrl("http://scenarioContextMenuOpenInNewTab"); |
| 182 GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination"); |
| 183 |
| 184 // The initial page contains a link to the destination page that is below a |
| 185 // really tall div so that scrolling is required. |
| 186 responses[initialURL] = |
| 187 "<div style='height:4000px'></div>" |
| 188 "<a style='margin-left:50px' href='" + |
| 189 destinationURL.spec() + "' id='link'>link</a>"; |
| 190 responses[destinationURL] = kDestinationHtml; |
| 191 |
| 192 web::test::SetUpSimpleHttpServer(responses); |
| 193 [ChromeEarlGrey loadURL:initialURL]; |
| 194 chrome_test_util::AssertMainTabCount(1U); |
| 195 |
| 196 // Scroll down on the web view to make the link visible. |
| 197 [[EarlGrey |
| 198 selectElementWithMatcher:webViewScrollView( |
| 199 chrome_test_util::GetCurrentWebState())] |
| 200 performAction:grey_swipeFastInDirection(kGREYDirectionUp)]; |
| 201 [[EarlGrey selectElementWithMatcher:chrome_test_util::webViewContainingText( |
| 202 kDestinationLinkID)] |
| 203 assertWithMatcher:grey_notNil()]; |
| 204 |
| 205 LongPressElementAndTapOnButton(kDestinationLinkID, openLinkInNewTabButton()); |
| 206 |
| 207 // Earl Grey cannot preperly synchronize some animations, so adding a |
| 208 // WaitUntilCondition to wait for the new tab opening animation to finish |
| 209 // and the scroll view to become interactable. |
| 210 ConditionBlock condition = ^{ |
| 211 NSError* error = nil; |
| 212 [[EarlGrey |
| 213 selectElementWithMatcher:webViewScrollView( |
| 214 chrome_test_util::GetCurrentWebState())] |
| 215 assertWithMatcher:grey_interactable() |
| 216 error:&error]; |
| 217 return !error; |
| 218 }; |
| 219 GREYAssert(testing::WaitUntilConditionOrTimeout( |
| 220 testing::kWaitForUIElementTimeout, condition), |
| 221 @"Web view did not become interactable"); |
| 222 |
| 223 // Make the toolbar visible by scrolling up on the web view to select the |
| 224 // newly opened tab. |
| 225 [[EarlGrey |
| 226 selectElementWithMatcher:webViewScrollView( |
| 227 chrome_test_util::GetCurrentWebState())] |
| 228 performAction:grey_swipeFastInDirection(kGREYDirectionDown)]; |
| 229 chrome_test_util::AssertToolbarVisible(); |
| 230 |
| 231 SelectTabAtIndexInCurrentMode(1U); |
| 232 |
| 233 // Verify url and tab count. |
| 234 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText( |
| 235 destinationURL.GetContent())] |
| 236 assertWithMatcher:grey_notNil()]; |
| 237 chrome_test_util::AssertMainTabCount(2U); |
| 238 } |
| 239 |
| 240 @end |
OLD | NEW |