| 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/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "components/strings/grit/components_strings.h" | 10 #include "components/strings/grit/components_strings.h" |
| 11 #include "ios/chrome/browser/ui/ui_util.h" | 11 #include "ios/chrome/browser/ui/ui_util.h" |
| 12 #import "ios/chrome/test/app/chrome_test_util.h" | 12 #import "ios/chrome/test/app/chrome_test_util.h" |
| 13 #include "ios/chrome/test/app/navigation_test_util.h" | 13 #include "ios/chrome/test/app/navigation_test_util.h" |
| 14 #include "ios/chrome/test/app/web_view_interaction_test_util.h" | 14 #include "ios/chrome/test/app/web_view_interaction_test_util.h" |
| 15 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" | 15 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 16 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" | 16 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" |
| 17 #import "ios/chrome/test/earl_grey/chrome_matchers.h" | 17 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 18 #import "ios/chrome/test/earl_grey/chrome_test_case.h" | 18 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 19 #import "ios/testing/wait_util.h" | 19 #import "ios/testing/wait_util.h" |
| 20 #import "ios/web/public/test/earl_grey/web_view_actions.h" |
| 21 #import "ios/web/public/test/earl_grey/web_view_matchers.h" |
| 20 #include "ios/web/public/test/http_server/data_response_provider.h" | 22 #include "ios/web/public/test/http_server/data_response_provider.h" |
| 21 #import "ios/web/public/test/http_server/http_server.h" | 23 #import "ios/web/public/test/http_server/http_server.h" |
| 22 #include "ios/web/public/test/http_server/http_server_util.h" | 24 #include "ios/web/public/test/http_server/http_server_util.h" |
| 23 #include "ios/web/public/test/url_test_util.h" | 25 #include "ios/web/public/test/url_test_util.h" |
| 24 | 26 |
| 25 #if !defined(__has_feature) || !__has_feature(objc_arc) | 27 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 26 #error "This file requires ARC support." | 28 #error "This file requires ARC support." |
| 27 #endif | 29 #endif |
| 28 | 30 |
| 29 using chrome_test_util::ButtonWithAccessibilityLabelId; | 31 using chrome_test_util::ButtonWithAccessibilityLabelId; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 48 // GURL of a generic website in the user navigation flow. | 50 // GURL of a generic website in the user navigation flow. |
| 49 const GURL GetGenericUrl() { | 51 const GURL GetGenericUrl() { |
| 50 return web::test::HttpServer::MakeUrl("http://generic"); | 52 return web::test::HttpServer::MakeUrl("http://generic"); |
| 51 } | 53 } |
| 52 | 54 |
| 53 // GURL of a page with a form that posts data to |GetDestinationUrl|. | 55 // GURL of a page with a form that posts data to |GetDestinationUrl|. |
| 54 const GURL GetFormUrl() { | 56 const GURL GetFormUrl() { |
| 55 return web::test::HttpServer::MakeUrl("http://form"); | 57 return web::test::HttpServer::MakeUrl("http://form"); |
| 56 } | 58 } |
| 57 | 59 |
| 60 // GURL of a page with a form that posts data to |GetDestinationUrl|. |
| 61 const GURL GetFormPostOnSamePageUrl() { |
| 62 return web::test::HttpServer::MakeUrl("http://form"); |
| 63 } |
| 64 |
| 58 // GURL of the page to which the |GetFormUrl| posts data to. | 65 // GURL of the page to which the |GetFormUrl| posts data to. |
| 59 const GURL GetDestinationUrl() { | 66 const GURL GetDestinationUrl() { |
| 60 return web::test::HttpServer::MakeUrl("http://destination"); | 67 return web::test::HttpServer::MakeUrl("http://destination"); |
| 61 } | 68 } |
| 62 | 69 |
| 63 #pragma mark - TestFormRedirectResponseProvider | 70 #pragma mark - TestFormResponseProvider |
| 64 | 71 |
| 65 // URL that redirects to |GetDestinationUrl| with a 302. | 72 // URL that redirects to |GetDestinationUrl| with a 302. |
| 66 const GURL GetRedirectUrl() { | 73 const GURL GetRedirectUrl() { |
| 67 return web::test::HttpServer::MakeUrl("http://redirect"); | 74 return web::test::HttpServer::MakeUrl("http://redirect"); |
| 68 } | 75 } |
| 69 | 76 |
| 70 // URL to return a page that posts to |GetRedirectUrl|. | 77 // URL to return a page that posts to |GetRedirectUrl|. |
| 71 const GURL GetRedirectFormUrl() { | 78 const GURL GetRedirectFormUrl() { |
| 72 return web::test::HttpServer::MakeUrl("http://formRedirect"); | 79 return web::test::HttpServer::MakeUrl("http://formRedirect"); |
| 73 } | 80 } |
| 74 | 81 |
| 75 // A ResponseProvider that provides html response or a redirect. | 82 // A ResponseProvider that provides html response, post response or a redirect. |
| 76 class TestFormRedirectResponseProvider : public web::DataResponseProvider { | 83 class TestFormResponseProvider : public web::DataResponseProvider { |
| 77 public: | 84 public: |
| 78 // TestResponseProvider implementation. | 85 // TestResponseProvider implementation. |
| 79 bool CanHandleRequest(const Request& request) override; | 86 bool CanHandleRequest(const Request& request) override; |
| 80 void GetResponseHeadersAndBody( | 87 void GetResponseHeadersAndBody( |
| 81 const Request& request, | 88 const Request& request, |
| 82 scoped_refptr<net::HttpResponseHeaders>* headers, | 89 scoped_refptr<net::HttpResponseHeaders>* headers, |
| 83 std::string* response_body) override; | 90 std::string* response_body) override; |
| 84 }; | 91 }; |
| 85 | 92 |
| 86 bool TestFormRedirectResponseProvider::CanHandleRequest( | 93 bool TestFormResponseProvider::CanHandleRequest(const Request& request) { |
| 87 const Request& request) { | |
| 88 const GURL& url = request.url; | 94 const GURL& url = request.url; |
| 89 return url == GetDestinationUrl() || url == GetRedirectUrl() || | 95 return url == GetDestinationUrl() || url == GetRedirectUrl() || |
| 90 url == GetRedirectFormUrl(); | 96 url == GetRedirectFormUrl() || url == GetFormPostOnSamePageUrl() || |
| 97 url == GetGenericUrl(); |
| 91 } | 98 } |
| 92 | 99 |
| 93 void TestFormRedirectResponseProvider::GetResponseHeadersAndBody( | 100 void TestFormResponseProvider::GetResponseHeadersAndBody( |
| 94 const Request& request, | 101 const Request& request, |
| 95 scoped_refptr<net::HttpResponseHeaders>* headers, | 102 scoped_refptr<net::HttpResponseHeaders>* headers, |
| 96 std::string* response_body) { | 103 std::string* response_body) { |
| 97 const GURL& url = request.url; | 104 const GURL& url = request.url; |
| 98 if (url == GetRedirectUrl()) { | 105 if (url == GetRedirectUrl()) { |
| 99 *headers = web::ResponseProvider::GetRedirectResponseHeaders( | 106 *headers = web::ResponseProvider::GetRedirectResponseHeaders( |
| 100 GetDestinationUrl().spec(), net::HTTP_FOUND); | 107 GetDestinationUrl().spec(), net::HTTP_FOUND); |
| 101 return; | 108 return; |
| 102 } | 109 } |
| 103 | 110 |
| 104 *headers = web::ResponseProvider::GetDefaultResponseHeaders(); | 111 *headers = web::ResponseProvider::GetDefaultResponseHeaders(); |
| 112 if (url == GetGenericUrl()) { |
| 113 *response_body = "A generic page"; |
| 114 return; |
| 115 } |
| 116 if (url == GetFormPostOnSamePageUrl()) { |
| 117 if (request.method == "POST") { |
| 118 *response_body = request.method + std::string(" ") + request.body; |
| 119 } else { |
| 120 *response_body = |
| 121 "<form method='post'>" |
| 122 "<input value='button' type='submit' id='button'></form>"; |
| 123 } |
| 124 return; |
| 125 } |
| 126 |
| 105 if (url == GetRedirectFormUrl()) { | 127 if (url == GetRedirectFormUrl()) { |
| 106 *response_body = | 128 *response_body = |
| 107 base::StringPrintf(kFormHtmlTemplate, GetRedirectUrl().spec().c_str()); | 129 base::StringPrintf(kFormHtmlTemplate, GetRedirectUrl().spec().c_str()); |
| 108 return; | 130 return; |
| 109 } else if (url == GetDestinationUrl()) { | 131 } else if (url == GetDestinationUrl()) { |
| 110 *response_body = request.method + std::string(" ") + request.body; | 132 *response_body = request.method + std::string(" ") + request.body; |
| 111 return; | 133 return; |
| 112 } | 134 } |
| 113 NOTREACHED(); | 135 NOTREACHED(); |
| 114 } | 136 } |
| 115 | 137 |
| 116 } // namespace | 138 } // namespace |
| 117 | 139 |
| 118 // Tests submition of HTTP forms POST data including cases involving navigation. | 140 // Tests submition of HTTP forms POST data including cases involving navigation. |
| 119 @interface FormsTestCase : ChromeTestCase | 141 @interface FormsTestCase : ChromeTestCase |
| 120 @end | 142 @end |
| 121 | 143 |
| 122 @implementation FormsTestCase | 144 @implementation FormsTestCase |
| 123 | 145 |
| 146 // Matcher for a Go button that is interactable. |
| 147 id<GREYMatcher> GoButtonMatcher() { |
| 148 return grey_allOf(grey_accessibilityID(@"Go"), grey_interactable(), nil); |
| 149 } |
| 150 |
| 124 // Waits for view with Tab History accessibility ID. | 151 // Waits for view with Tab History accessibility ID. |
| 125 - (void)waitForTabHistoryView { | 152 - (void)waitForTabHistoryView { |
| 126 GREYCondition* condition = [GREYCondition | 153 GREYCondition* condition = [GREYCondition |
| 127 conditionWithName:@"Waiting for Tab History to display." | 154 conditionWithName:@"Waiting for Tab History to display." |
| 128 block:^BOOL { | 155 block:^BOOL { |
| 129 NSError* error = nil; | 156 NSError* error = nil; |
| 130 id<GREYMatcher> tabHistory = | 157 id<GREYMatcher> tabHistory = |
| 131 grey_accessibilityID(@"Tab History"); | 158 grey_accessibilityID(@"Tab History"); |
| 132 [[EarlGrey selectElementWithMatcher:tabHistory] | 159 [[EarlGrey selectElementWithMatcher:tabHistory] |
| 133 assertWithMatcher:grey_notNil() | 160 assertWithMatcher:grey_notNil() |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 // Go back and verify the browser navigates to the original URL. | 347 // Go back and verify the browser navigates to the original URL. |
| 321 [ChromeEarlGrey goBack]; | 348 [ChromeEarlGrey goBack]; |
| 322 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kSubmitButtonLabel)] | 349 [[EarlGrey selectElementWithMatcher:WebViewContainingText(kSubmitButtonLabel)] |
| 323 assertWithMatcher:grey_notNil()]; | 350 assertWithMatcher:grey_notNil()]; |
| 324 [[EarlGrey selectElementWithMatcher:OmniboxText(GetFormUrl().GetContent())] | 351 [[EarlGrey selectElementWithMatcher:OmniboxText(GetFormUrl().GetContent())] |
| 325 assertWithMatcher:grey_notNil()]; | 352 assertWithMatcher:grey_notNil()]; |
| 326 } | 353 } |
| 327 | 354 |
| 328 // Tests that a POST followed by a redirect does not show the popup. | 355 // Tests that a POST followed by a redirect does not show the popup. |
| 329 - (void)testRepostFormCancellingAfterRedirect { | 356 - (void)testRepostFormCancellingAfterRedirect { |
| 330 web::test::SetUpHttpServer( | 357 web::test::SetUpHttpServer(base::MakeUnique<TestFormResponseProvider>()); |
| 331 base::MakeUnique<TestFormRedirectResponseProvider>()); | |
| 332 const GURL destinationURL = GetDestinationUrl(); | 358 const GURL destinationURL = GetDestinationUrl(); |
| 333 | 359 |
| 334 [ChromeEarlGrey loadURL:GetRedirectFormUrl()]; | 360 [ChromeEarlGrey loadURL:GetRedirectFormUrl()]; |
| 335 | 361 |
| 336 // Submit the form, which redirects before printing the data. | 362 // Submit the form, which redirects before printing the data. |
| 337 chrome_test_util::TapWebViewElementWithId(kSubmitButtonLabel); | 363 chrome_test_util::TapWebViewElementWithId(kSubmitButtonLabel); |
| 338 | 364 |
| 339 // Check that the redirect changes the POST to a GET. | 365 // Check that the redirect changes the POST to a GET. |
| 340 [[EarlGrey selectElementWithMatcher:WebViewContainingText("GET")] | 366 [[EarlGrey selectElementWithMatcher:WebViewContainingText("GET")] |
| 341 assertWithMatcher:grey_notNil()]; | 367 assertWithMatcher:grey_notNil()]; |
| 342 [[EarlGrey selectElementWithMatcher:OmniboxText(destinationURL.GetContent())] | 368 [[EarlGrey selectElementWithMatcher:OmniboxText(destinationURL.GetContent())] |
| 343 assertWithMatcher:grey_notNil()]; | 369 assertWithMatcher:grey_notNil()]; |
| 344 | 370 |
| 345 [ChromeEarlGrey reload]; | 371 [ChromeEarlGrey reload]; |
| 346 | 372 |
| 347 // Check that the popup did not show | 373 // Check that the popup did not show |
| 348 id<GREYMatcher> resendWarning = | 374 id<GREYMatcher> resendWarning = |
| 349 ButtonWithAccessibilityLabelId(IDS_HTTP_POST_WARNING_RESEND); | 375 ButtonWithAccessibilityLabelId(IDS_HTTP_POST_WARNING_RESEND); |
| 350 [[EarlGrey selectElementWithMatcher:resendWarning] | 376 [[EarlGrey selectElementWithMatcher:resendWarning] |
| 351 assertWithMatcher:grey_nil()]; | 377 assertWithMatcher:grey_nil()]; |
| 352 [[EarlGrey selectElementWithMatcher:WebViewContainingText("GET")] | 378 [[EarlGrey selectElementWithMatcher:WebViewContainingText("GET")] |
| 353 assertWithMatcher:grey_notNil()]; | 379 assertWithMatcher:grey_notNil()]; |
| 354 [[EarlGrey selectElementWithMatcher:OmniboxText(destinationURL.GetContent())] | 380 [[EarlGrey selectElementWithMatcher:OmniboxText(destinationURL.GetContent())] |
| 355 assertWithMatcher:grey_notNil()]; | 381 assertWithMatcher:grey_notNil()]; |
| 356 } | 382 } |
| 357 | 383 |
| 384 // Tests that pressing the button on a POST-based form with same-page action |
| 385 // does not change the page URL and that the back button works as expected |
| 386 // afterwards. |
| 387 - (void)testPostFormToSamePage { |
| 388 // TODO(crbug.com/714303): Re-enable this test on devices. |
| 389 #if !TARGET_IPHONE_SIMULATOR |
| 390 EARL_GREY_TEST_DISABLED(@"Test disabled on device."); |
| 391 #endif |
| 392 |
| 393 web::test::SetUpHttpServer(base::MakeUnique<TestFormResponseProvider>()); |
| 394 const GURL formURL = GetFormPostOnSamePageUrl(); |
| 395 |
| 396 // Open the first URL so it's in history. |
| 397 [ChromeEarlGrey loadURL:GetGenericUrl()]; |
| 398 |
| 399 // Open the second URL, tap the button, and verify the browser navigates to |
| 400 // the expected URL. |
| 401 [ChromeEarlGrey loadURL:formURL]; |
| 402 chrome_test_util::TapWebViewElementWithId("button"); |
| 403 [ChromeEarlGrey waitForWebViewContainingText:"POST"]; |
| 404 [[EarlGrey selectElementWithMatcher:OmniboxText(formURL.GetContent())] |
| 405 assertWithMatcher:grey_notNil()]; |
| 406 |
| 407 // Go back once and verify the browser navigates to the form URL. |
| 408 [ChromeEarlGrey goBack]; |
| 409 [[EarlGrey selectElementWithMatcher:OmniboxText(formURL.GetContent())] |
| 410 assertWithMatcher:grey_notNil()]; |
| 411 |
| 412 // Go back a second time and verify the browser navigates to the first URL. |
| 413 [ChromeEarlGrey goBack]; |
| 414 [[EarlGrey selectElementWithMatcher:OmniboxText(GetGenericUrl().GetContent())] |
| 415 assertWithMatcher:grey_notNil()]; |
| 416 } |
| 417 |
| 418 // Tests that submitting a POST-based form by tapping the 'Go' button on the |
| 419 // keyboard navigates to the correct URL and the back button works as expected |
| 420 // afterwards. |
| 421 - (void)testPostFormEntryWithKeyboard { |
| 422 // TODO(crbug.com/704618): Re-enable this test on devices. |
| 423 #if !TARGET_IPHONE_SIMULATOR |
| 424 EARL_GREY_TEST_DISABLED(@"Test disabled on device."); |
| 425 #endif |
| 426 |
| 427 [self setUpFormTestSimpleHttpServer]; |
| 428 const GURL destinationURL = GetDestinationUrl(); |
| 429 |
| 430 [ChromeEarlGrey loadURL:GetFormUrl()]; |
| 431 [self submitFormUsingKeyboardGoButtonWithInputID:"textfield"]; |
| 432 |
| 433 // Verify that the browser navigates to the expected URL. |
| 434 [ChromeEarlGrey waitForWebViewContainingText:"bar!"]; |
| 435 [[EarlGrey selectElementWithMatcher:OmniboxText(destinationURL.GetContent())] |
| 436 assertWithMatcher:grey_notNil()]; |
| 437 |
| 438 // Go back and verify that the browser navigates to the original URL. |
| 439 [ChromeEarlGrey goBack]; |
| 440 [[EarlGrey selectElementWithMatcher:OmniboxText(GetFormUrl().GetContent())] |
| 441 assertWithMatcher:grey_notNil()]; |
| 442 } |
| 443 |
| 444 // Tap the text field indicated by |ID| to open the keyboard, and then |
| 445 // press the keyboard's "Go" button to submit the form. |
| 446 - (void)submitFormUsingKeyboardGoButtonWithInputID:(const std::string&)ID { |
| 447 // Disable EarlGrey's synchronization since it is blocked by opening the |
| 448 // keyboard from a web view. |
| 449 [[GREYConfiguration sharedInstance] |
| 450 setValue:@NO |
| 451 forConfigKey:kGREYConfigKeySynchronizationEnabled]; |
| 452 |
| 453 // Wait for web view to be interactable before tapping. |
| 454 GREYCondition* interactableCondition = [GREYCondition |
| 455 conditionWithName:@"Wait for web view to be interactable." |
| 456 block:^BOOL { |
| 457 NSError* error = nil; |
| 458 id<GREYMatcher> webViewMatcher = WebViewInWebState( |
| 459 chrome_test_util::GetCurrentWebState()); |
| 460 [[EarlGrey selectElementWithMatcher:webViewMatcher] |
| 461 assertWithMatcher:grey_interactable() |
| 462 error:&error]; |
| 463 return !error; |
| 464 }]; |
| 465 GREYAssert( |
| 466 [interactableCondition waitWithTimeout:testing::kWaitForUIElementTimeout], |
| 467 @"Web view did not become interactable."); |
| 468 |
| 469 web::WebState* currentWebState = chrome_test_util::GetCurrentWebState(); |
| 470 [[EarlGrey selectElementWithMatcher:web::WebViewInWebState(currentWebState)] |
| 471 performAction:web::WebViewTapElement(currentWebState, ID)]; |
| 472 |
| 473 // Wait until the keyboard shows up before tapping. |
| 474 GREYCondition* condition = [GREYCondition |
| 475 conditionWithName:@"Wait for the keyboard to show up." |
| 476 block:^BOOL { |
| 477 NSError* error = nil; |
| 478 [[EarlGrey selectElementWithMatcher:GoButtonMatcher()] |
| 479 assertWithMatcher:grey_notNil() |
| 480 error:&error]; |
| 481 return (error == nil); |
| 482 }]; |
| 483 GREYAssert([condition waitWithTimeout:testing::kWaitForUIElementTimeout], |
| 484 @"No keyboard with 'Go' button showed up."); |
| 485 |
| 486 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Go")] |
| 487 performAction:grey_tap()]; |
| 488 |
| 489 // Reenable synchronization now that the keyboard has been closed. |
| 490 [[GREYConfiguration sharedInstance] |
| 491 setValue:@YES |
| 492 forConfigKey:kGREYConfigKeySynchronizationEnabled]; |
| 493 } |
| 494 |
| 358 @end | 495 @end |
| OLD | NEW |