| 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 <EarlGrey/EarlGrey.h> | 5 #import <EarlGrey/EarlGrey.h> |
| 6 #import <UIKit/UIKit.h> | 6 #import <UIKit/UIKit.h> |
| 7 #import <XCTest/XCTest.h> | 7 #import <XCTest/XCTest.h> |
| 8 | 8 |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "ios/chrome/browser/ui/ui_util.h" | 10 #include "ios/chrome/browser/ui/ui_util.h" |
| 11 #include "ios/chrome/grit/ios_strings.h" | 11 #include "ios/chrome/grit/ios_strings.h" |
| 12 #import "ios/chrome/test/app/chrome_test_util.h" | 12 #import "ios/chrome/test/app/chrome_test_util.h" |
| 13 #import "ios/chrome/test/app/settings_test_util.h" | 13 #import "ios/chrome/test/app/settings_test_util.h" |
| 14 #import "ios/chrome/test/app/tab_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" | 15 #include "ios/chrome/test/app/web_view_interaction_test_util.h" |
| 16 #import "ios/chrome/test/earl_grey/chrome_actions.h" | 16 #import "ios/chrome/test/earl_grey/chrome_actions.h" |
| 17 #import "ios/chrome/test/earl_grey/chrome_assertions.h" | 17 #import "ios/chrome/test/earl_grey/chrome_assertions.h" |
| 18 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" | 18 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 19 #import "ios/chrome/test/earl_grey/chrome_matchers.h" | 19 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 20 #import "ios/chrome/test/earl_grey/chrome_test_case.h" | 20 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 21 #include "ios/chrome/test/earl_grey/chrome_util.h" | 21 #include "ios/chrome/test/earl_grey/chrome_util.h" |
| 22 #import "ios/testing/wait_util.h" | 22 #import "ios/testing/wait_util.h" |
| 23 #import "ios/web/public/test/earl_grey/web_view_matchers.h" | 23 #import "ios/web/public/test/earl_grey/web_view_matchers.h" |
| 24 #import "ios/web/public/test/http_server.h" | 24 #import "ios/web/public/test/http_server.h" |
| 25 #import "ios/web/public/test/http_server_util.h" | 25 #import "ios/web/public/test/http_server_util.h" |
| 26 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 27 | 27 |
| 28 using chrome_test_util::buttonWithAccessibilityLabelId; | 28 using chrome_test_util::ButtonWithAccessibilityLabelId; |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 const char kUrlChromiumLogoPage[] = | 31 const char kUrlChromiumLogoPage[] = |
| 32 "http://ios/testing/data/http_server_files/chromium_logo_page.html"; | 32 "http://ios/testing/data/http_server_files/chromium_logo_page.html"; |
| 33 const char kUrlChromiumLogoImg[] = | 33 const char kUrlChromiumLogoImg[] = |
| 34 "http://ios/testing/data/http_server_files/chromium_logo.png"; | 34 "http://ios/testing/data/http_server_files/chromium_logo.png"; |
| 35 const char kUrlInitialPage[] = "http://scenarioContextMenuOpenInNewTab"; | 35 const char kUrlInitialPage[] = "http://scenarioContextMenuOpenInNewTab"; |
| 36 const char kUrlDestinationPage[] = "http://destination"; | 36 const char kUrlDestinationPage[] = "http://destination"; |
| 37 const char kChromiumImageID[] = "chromium_image"; | 37 const char kChromiumImageID[] = "chromium_image"; |
| 38 const char kDestinationLinkID[] = "link"; | 38 const char kDestinationLinkID[] = "link"; |
| 39 | 39 |
| 40 // HTML content of the destination page that sets the page title. | 40 // HTML content of the destination page that sets the page title. |
| 41 const char kDestinationHtml[] = | 41 const char kDestinationHtml[] = |
| 42 "<script>document.title='new doc'</script>You made it!"; | 42 "<script>document.title='new doc'</script>You made it!"; |
| 43 | 43 |
| 44 // Matcher for the open image button in the context menu. | 44 // Matcher for the open image button in the context menu. |
| 45 id<GREYMatcher> openImageButton() { | 45 id<GREYMatcher> OpenImageButton() { |
| 46 return buttonWithAccessibilityLabelId(IDS_IOS_CONTENT_CONTEXT_OPENIMAGE); | 46 return ButtonWithAccessibilityLabelId(IDS_IOS_CONTENT_CONTEXT_OPENIMAGE); |
| 47 } | 47 } |
| 48 | 48 |
| 49 // Matcher for the open image in new tab button in the context menu. | 49 // Matcher for the open image in new tab button in the context menu. |
| 50 id<GREYMatcher> openImageInNewTabButton() { | 50 id<GREYMatcher> OpenImageInNewTabButton() { |
| 51 return buttonWithAccessibilityLabelId( | 51 return ButtonWithAccessibilityLabelId( |
| 52 IDS_IOS_CONTENT_CONTEXT_OPENIMAGENEWTAB); | 52 IDS_IOS_CONTENT_CONTEXT_OPENIMAGENEWTAB); |
| 53 } | 53 } |
| 54 | 54 |
| 55 // Matcher for the open link in new tab button in the context menu. | 55 // Matcher for the open link in new tab button in the context menu. |
| 56 id<GREYMatcher> openLinkInNewTabButton() { | 56 // TODO(crbug.com/638674): Clean up code duplication. |
| 57 return buttonWithAccessibilityLabelId(IDS_IOS_CONTENT_CONTEXT_OPENLINKNEWTAB); | 57 id<GREYMatcher> OpenLinkInNewTabButton() { |
| 58 return ButtonWithAccessibilityLabelId(IDS_IOS_CONTENT_CONTEXT_OPENLINKNEWTAB); |
| 58 } | 59 } |
| 59 | 60 |
| 60 // Waits for the context menu item to disappear. TODO(crbug.com/682871): Remove | 61 // Waits for the context menu item to disappear. TODO(crbug.com/682871): Remove |
| 61 // this once EarlGrey is synchronized with context menu. | 62 // this once EarlGrey is synchronized with context menu. |
| 62 void WaitForContextMenuItemDisappeared(id<GREYMatcher> contextMenuItemButton) { | 63 void WaitForContextMenuItemDisappeared(id<GREYMatcher> contextMenuItemButton) { |
| 63 ConditionBlock condition = ^{ | 64 ConditionBlock condition = ^{ |
| 64 NSError* error = nil; | 65 NSError* error = nil; |
| 65 [[EarlGrey selectElementWithMatcher:contextMenuItemButton] | 66 [[EarlGrey selectElementWithMatcher:contextMenuItemButton] |
| 66 assertWithMatcher:grey_nil() | 67 assertWithMatcher:grey_nil() |
| 67 error:&error]; | 68 error:&error]; |
| 68 return error == nil; | 69 return error == nil; |
| 69 }; | 70 }; |
| 70 GREYAssert(testing::WaitUntilConditionOrTimeout( | 71 GREYAssert(testing::WaitUntilConditionOrTimeout( |
| 71 testing::kWaitForUIElementTimeout, condition), | 72 testing::kWaitForUIElementTimeout, condition), |
| 72 [NSString stringWithFormat:@"Waiting for matcher %@ failed.", | 73 [NSString stringWithFormat:@"Waiting for matcher %@ failed.", |
| 73 contextMenuItemButton]); | 74 contextMenuItemButton]); |
| 74 } | 75 } |
| 75 | 76 |
| 76 // Long press on |elementId| to trigger context menu and then tap on | 77 // Long press on |elementId| to trigger context menu and then tap on |
| 77 // |contextMenuItemButton| item. | 78 // |contextMenuItemButton| item. |
| 78 void LongPressElementAndTapOnButton(const char* elementId, | 79 void LongPressElementAndTapOnButton(const char* elementId, |
| 79 id<GREYMatcher> contextMenuItemButton) { | 80 id<GREYMatcher> contextMenuItemButton) { |
| 80 [[EarlGrey selectElementWithMatcher:chrome_test_util:: | 81 id<GREYMatcher> webViewMatcher = |
| 81 webViewBelongingToWebController()] | 82 web::WebViewInWebState(chrome_test_util::GetCurrentWebState()); |
| 83 [[EarlGrey selectElementWithMatcher:webViewMatcher] |
| 82 performAction:chrome_test_util::longPressElementForContextMenu(elementId, | 84 performAction:chrome_test_util::longPressElementForContextMenu(elementId, |
| 83 true)]; | 85 true)]; |
| 84 | 86 |
| 85 [[EarlGrey selectElementWithMatcher:contextMenuItemButton] | 87 [[EarlGrey selectElementWithMatcher:contextMenuItemButton] |
| 86 assertWithMatcher:grey_notNil()]; | 88 assertWithMatcher:grey_notNil()]; |
| 87 [[EarlGrey selectElementWithMatcher:contextMenuItemButton] | 89 [[EarlGrey selectElementWithMatcher:contextMenuItemButton] |
| 88 performAction:grey_tap()]; | 90 performAction:grey_tap()]; |
| 89 WaitForContextMenuItemDisappeared(contextMenuItemButton); | 91 WaitForContextMenuItemDisappeared(contextMenuItemButton); |
| 90 } | 92 } |
| 91 | 93 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 | 125 |
| 124 // Tests that selecting "Open Image" from the context menu properly opens the | 126 // Tests that selecting "Open Image" from the context menu properly opens the |
| 125 // image in the current tab. | 127 // image in the current tab. |
| 126 - (void)testOpenImageInCurrentTabFromContextMenu { | 128 - (void)testOpenImageInCurrentTabFromContextMenu { |
| 127 GURL pageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoPage); | 129 GURL pageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoPage); |
| 128 GURL imageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoImg); | 130 GURL imageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoImg); |
| 129 web::test::SetUpFileBasedHttpServer(); | 131 web::test::SetUpFileBasedHttpServer(); |
| 130 [ChromeEarlGrey loadURL:pageURL]; | 132 [ChromeEarlGrey loadURL:pageURL]; |
| 131 chrome_test_util::AssertMainTabCount(1U); | 133 chrome_test_util::AssertMainTabCount(1U); |
| 132 | 134 |
| 133 LongPressElementAndTapOnButton(kChromiumImageID, openImageButton()); | 135 LongPressElementAndTapOnButton(kChromiumImageID, OpenImageButton()); |
| 134 | 136 |
| 135 // Verify url and tab count. | 137 // Verify url and tab count. |
| 136 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText( | 138 [[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText( |
| 137 imageURL.GetContent())] | 139 imageURL.GetContent())] |
| 138 assertWithMatcher:grey_notNil()]; | 140 assertWithMatcher:grey_notNil()]; |
| 139 chrome_test_util::AssertMainTabCount(1U); | 141 chrome_test_util::AssertMainTabCount(1U); |
| 140 } | 142 } |
| 141 | 143 |
| 142 // Tests that selecting "Open Image in New Tab" from the context menu properly | 144 // Tests that selecting "Open Image in New Tab" from the context menu properly |
| 143 // opens the image in a new background tab. | 145 // opens the image in a new background tab. |
| 144 - (void)testOpenImageInNewTabFromContextMenu { | 146 - (void)testOpenImageInNewTabFromContextMenu { |
| 145 GURL pageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoPage); | 147 GURL pageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoPage); |
| 146 GURL imageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoImg); | 148 GURL imageURL = web::test::HttpServer::MakeUrl(kUrlChromiumLogoImg); |
| 147 web::test::SetUpFileBasedHttpServer(); | 149 web::test::SetUpFileBasedHttpServer(); |
| 148 [ChromeEarlGrey loadURL:pageURL]; | 150 [ChromeEarlGrey loadURL:pageURL]; |
| 149 chrome_test_util::AssertMainTabCount(1U); | 151 chrome_test_util::AssertMainTabCount(1U); |
| 150 | 152 |
| 151 LongPressElementAndTapOnButton(kChromiumImageID, openImageInNewTabButton()); | 153 LongPressElementAndTapOnButton(kChromiumImageID, OpenImageInNewTabButton()); |
| 152 | 154 |
| 153 SelectTabAtIndexInCurrentMode(1U); | 155 SelectTabAtIndexInCurrentMode(1U); |
| 154 | 156 |
| 155 // Verify url and tab count. | 157 // Verify url and tab count. |
| 156 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText( | 158 [[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText( |
| 157 imageURL.GetContent())] | 159 imageURL.GetContent())] |
| 158 assertWithMatcher:grey_notNil()]; | 160 assertWithMatcher:grey_notNil()]; |
| 159 chrome_test_util::AssertMainTabCount(2U); | 161 chrome_test_util::AssertMainTabCount(2U); |
| 160 } | 162 } |
| 161 | 163 |
| 162 // Tests "Open in New Tab" on context menu. | 164 // Tests "Open in New Tab" on context menu. |
| 163 - (void)testContextMenuOpenInNewTab { | 165 - (void)testContextMenuOpenInNewTab { |
| 164 // Set up test simple http server. | 166 // Set up test simple http server. |
| 165 std::map<GURL, std::string> responses; | 167 std::map<GURL, std::string> responses; |
| 166 GURL initialURL = web::test::HttpServer::MakeUrl(kUrlInitialPage); | 168 GURL initialURL = web::test::HttpServer::MakeUrl(kUrlInitialPage); |
| 167 GURL destinationURL = web::test::HttpServer::MakeUrl(kUrlDestinationPage); | 169 GURL destinationURL = web::test::HttpServer::MakeUrl(kUrlDestinationPage); |
| 168 | 170 |
| 169 // The initial page contains a link to the destination page. | 171 // The initial page contains a link to the destination page. |
| 170 responses[initialURL] = "<a style='margin-left:50px' href='" + | 172 responses[initialURL] = "<a style='margin-left:50px' href='" + |
| 171 destinationURL.spec() + "' id='link'>link</a>"; | 173 destinationURL.spec() + "' id='link'>link</a>"; |
| 172 responses[destinationURL] = kDestinationHtml; | 174 responses[destinationURL] = kDestinationHtml; |
| 173 | 175 |
| 174 web::test::SetUpSimpleHttpServer(responses); | 176 web::test::SetUpSimpleHttpServer(responses); |
| 175 [ChromeEarlGrey loadURL:initialURL]; | 177 [ChromeEarlGrey loadURL:initialURL]; |
| 176 chrome_test_util::AssertMainTabCount(1U); | 178 chrome_test_util::AssertMainTabCount(1U); |
| 177 | 179 |
| 178 LongPressElementAndTapOnButton(kDestinationLinkID, openLinkInNewTabButton()); | 180 LongPressElementAndTapOnButton(kDestinationLinkID, OpenLinkInNewTabButton()); |
| 179 | 181 |
| 180 SelectTabAtIndexInCurrentMode(1U); | 182 SelectTabAtIndexInCurrentMode(1U); |
| 181 | 183 |
| 182 // Verify url and tab count. | 184 // Verify url and tab count. |
| 183 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText( | 185 [[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText( |
| 184 destinationURL.GetContent())] | 186 destinationURL.GetContent())] |
| 185 assertWithMatcher:grey_notNil()]; | 187 assertWithMatcher:grey_notNil()]; |
| 186 chrome_test_util::AssertMainTabCount(2U); | 188 chrome_test_util::AssertMainTabCount(2U); |
| 187 } | 189 } |
| 188 | 190 |
| 189 // Tests "Open in New Tab" on context menu on a link that requires scrolling | 191 // Tests "Open in New Tab" on context menu on a link that requires scrolling |
| 190 // on the page to verify that context menu can be properly triggered in the | 192 // on the page to verify that context menu can be properly triggered in the |
| 191 // current screen view. | 193 // current screen view. |
| 192 - (void)testContextMenuOpenInNewTabFromTallPage { | 194 - (void)testContextMenuOpenInNewTabFromTallPage { |
| 193 // Set up test simple http server. | 195 // Set up test simple http server. |
| 194 std::map<GURL, std::string> responses; | 196 std::map<GURL, std::string> responses; |
| 195 GURL initialURL = | 197 GURL initialURL = |
| 196 web::test::HttpServer::MakeUrl("http://scenarioContextMenuOpenInNewTab"); | 198 web::test::HttpServer::MakeUrl("http://scenarioContextMenuOpenInNewTab"); |
| 197 GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination"); | 199 GURL destinationURL = web::test::HttpServer::MakeUrl("http://destination"); |
| 198 | 200 |
| 199 // The initial page contains a link to the destination page that is below a | 201 // The initial page contains a link to the destination page that is below a |
| 200 // really tall div so that scrolling is required. | 202 // really tall div so that scrolling is required. |
| 201 responses[initialURL] = | 203 responses[initialURL] = |
| 202 "<div style='height:4000px'></div>" | 204 "<div style='height:4000px'></div>" |
| 203 "<a style='margin-left:50px' href='" + | 205 "<a style='margin-left:50px' href='" + |
| 204 destinationURL.spec() + "' id='link'>link</a>"; | 206 destinationURL.spec() + "' id='link'>link</a>"; |
| 205 responses[destinationURL] = kDestinationHtml; | 207 responses[destinationURL] = kDestinationHtml; |
| 206 | 208 |
| 207 web::test::SetUpSimpleHttpServer(responses); | 209 web::test::SetUpSimpleHttpServer(responses); |
| 208 [ChromeEarlGrey loadURL:initialURL]; | 210 [ChromeEarlGrey loadURL:initialURL]; |
| 209 chrome_test_util::AssertMainTabCount(1U); | 211 chrome_test_util::AssertMainTabCount(1U); |
| 210 | 212 |
| 211 // Scroll down on the web view to make the link visible. | 213 // Scroll down on the web view to make the link visible. |
| 212 [[EarlGrey | 214 [[EarlGrey |
| 213 selectElementWithMatcher:webViewScrollView( | 215 selectElementWithMatcher:WebViewScrollView( |
| 214 chrome_test_util::GetCurrentWebState())] | 216 chrome_test_util::GetCurrentWebState())] |
| 215 performAction:grey_swipeFastInDirection(kGREYDirectionUp)]; | 217 performAction:grey_swipeFastInDirection(kGREYDirectionUp)]; |
| 216 [[EarlGrey selectElementWithMatcher:chrome_test_util::webViewContainingText( | 218 [[EarlGrey selectElementWithMatcher:chrome_test_util::WebViewContainingText( |
| 217 kDestinationLinkID)] | 219 kDestinationLinkID)] |
| 218 assertWithMatcher:grey_notNil()]; | 220 assertWithMatcher:grey_notNil()]; |
| 219 | 221 |
| 220 LongPressElementAndTapOnButton(kDestinationLinkID, openLinkInNewTabButton()); | 222 LongPressElementAndTapOnButton(kDestinationLinkID, OpenLinkInNewTabButton()); |
| 221 | 223 |
| 222 // Earl Grey cannot preperly synchronize some animations, so adding a | 224 // Earl Grey cannot preperly synchronize some animations, so adding a |
| 223 // WaitUntilCondition to wait for the new tab opening animation to finish | 225 // WaitUntilCondition to wait for the new tab opening animation to finish |
| 224 // and the scroll view to become interactable. | 226 // and the scroll view to become interactable. |
| 225 ConditionBlock condition = ^{ | 227 ConditionBlock condition = ^{ |
| 226 NSError* error = nil; | 228 NSError* error = nil; |
| 227 [[EarlGrey | 229 [[EarlGrey |
| 228 selectElementWithMatcher:webViewScrollView( | 230 selectElementWithMatcher:WebViewScrollView( |
| 229 chrome_test_util::GetCurrentWebState())] | 231 chrome_test_util::GetCurrentWebState())] |
| 230 assertWithMatcher:grey_interactable() | 232 assertWithMatcher:grey_interactable() |
| 231 error:&error]; | 233 error:&error]; |
| 232 return !error; | 234 return !error; |
| 233 }; | 235 }; |
| 234 GREYAssert(testing::WaitUntilConditionOrTimeout( | 236 GREYAssert(testing::WaitUntilConditionOrTimeout( |
| 235 testing::kWaitForUIElementTimeout, condition), | 237 testing::kWaitForUIElementTimeout, condition), |
| 236 @"Web view did not become interactable"); | 238 @"Web view did not become interactable"); |
| 237 | 239 |
| 238 // Make the toolbar visible by scrolling up on the web view to select the | 240 // Make the toolbar visible by scrolling up on the web view to select the |
| 239 // newly opened tab. | 241 // newly opened tab. |
| 240 [[EarlGrey | 242 [[EarlGrey |
| 241 selectElementWithMatcher:webViewScrollView( | 243 selectElementWithMatcher:WebViewScrollView( |
| 242 chrome_test_util::GetCurrentWebState())] | 244 chrome_test_util::GetCurrentWebState())] |
| 243 performAction:grey_swipeFastInDirection(kGREYDirectionDown)]; | 245 performAction:grey_swipeFastInDirection(kGREYDirectionDown)]; |
| 244 chrome_test_util::AssertToolbarVisible(); | 246 chrome_test_util::AssertToolbarVisible(); |
| 245 | 247 |
| 246 SelectTabAtIndexInCurrentMode(1U); | 248 SelectTabAtIndexInCurrentMode(1U); |
| 247 | 249 |
| 248 // Verify url and tab count. | 250 // Verify url and tab count. |
| 249 [[EarlGrey selectElementWithMatcher:chrome_test_util::omniboxText( | 251 [[EarlGrey selectElementWithMatcher:chrome_test_util::OmniboxText( |
| 250 destinationURL.GetContent())] | 252 destinationURL.GetContent())] |
| 251 assertWithMatcher:grey_notNil()]; | 253 assertWithMatcher:grey_notNil()]; |
| 252 chrome_test_util::AssertMainTabCount(2U); | 254 chrome_test_util::AssertMainTabCount(2U); |
| 253 } | 255 } |
| 254 | 256 |
| 255 @end | 257 @end |
| OLD | NEW |