Index: ios/chrome/browser/web/forms_egtest.mm |
diff --git a/ios/chrome/browser/web/forms_egtest.mm b/ios/chrome/browser/web/forms_egtest.mm |
index bb88c6ad7bafba855d37ec402f50c6613bec02f9..1165ff62665e4691bf22b57255942cda8b78b8ee 100644 |
--- a/ios/chrome/browser/web/forms_egtest.mm |
+++ b/ios/chrome/browser/web/forms_egtest.mm |
@@ -5,6 +5,7 @@ |
#import <XCTest/XCTest.h> |
#include "base/mac/scoped_nsobject.h" |
+#include "base/memory/ptr_util.h" |
#include "base/strings/stringprintf.h" |
#include "base/strings/sys_string_conversions.h" |
#include "components/strings/grit/components_strings.h" |
@@ -20,26 +21,30 @@ |
#import "ios/testing/wait_util.h" |
#import "ios/web/public/test/http_server.h" |
#import "ios/web/public/test/http_server_util.h" |
-#include "ios/web/public/test/response_providers/html_response_provider.h" |
-#include "ios/web/public/test/response_providers/html_response_provider_impl.h" |
+#include "ios/web/public/test/response_providers/data_response_provider.h" |
namespace { |
-// URL for a generic website in the user navigation flow. |
-const char kGenericUrl[] = "http://generic"; |
+// Test Server scheme. |
+const char kHttpScheme[] = "http://"; |
-// URL for the server to print the HTTP method and the request body. |
-const char kPrintFormDataUrl[] = "http://printFormData"; |
+// Test server host. |
+const char kHttpServerHost[] = "localhost"; |
-// URL for the server that redirects to kPrintPostData with a 302. |
-const char kRedirectUrl[] = "http://redirect"; |
+// Path for a generic website in the user navigation flow. |
+const char kGenericPath[] = "/generic/"; |
-// URL for the server to return a page that posts a form with some data to |
-// |kPrintPostData|. |
-const char kFormUrl[] = "http://formURL"; |
+// Path to print the HTTP method and the request body. |
+const char kPrintFormDataPath[] = "/printformdata/"; |
-// URL for the server to return a page that posts to |kRedirect|. |
-const char kRedirectFormUrl[] = "http://redirectFormURL"; |
+// Path that redirects to |kPrintFormDataPath| with a 302. |
+const char kRedirectPath[] = "/redirect/"; |
+ |
+// Path to a page that posts a form with some data to |kPrintFormDataPath|. |
+const char kFormPath[] = "/form/"; |
+ |
+// Path to a page that posts to |kRedirectPath|. |
+const char kRedirectFormPath[] = "/redirectform/"; |
// Label for the button in the form. |
const char kSubmitButton[] = "Submit"; |
@@ -47,6 +52,73 @@ const char kSubmitButton[] = "Submit"; |
// Expected response from the server. |
const char kExpectedPostData[] = "POST Data=Unicorn"; |
+#pragma mark - TestResponseProvider |
+ |
+// A ResponseProvider that provides html response or a redirect. |
+class TestResponseProvider : public web::DataResponseProvider { |
+ public: |
+ // URL for the server at |kFormPath|. |
+ static GURL GetFormURL() { |
Eugene But (OOO till 7-30)
2017/02/15 01:28:04
GetFormUrl
michaeldo
2017/02/15 06:22:52
Done.
|
+ return web::test::HttpServer::MakeUrl(std::string(kHttpScheme) + kFormPath); |
+ } |
+ // URL for the server at |kPrintFormDataPath|. |
+ static GURL GetPrintFormDataURL() { |
Eugene But (OOO till 7-30)
2017/02/15 01:28:04
GetPrintFormDataUrl
michaeldo
2017/02/15 06:22:52
Done.
|
+ return web::test::HttpServer::MakeUrl(std::string(kHttpScheme) + |
+ kPrintFormDataPath); |
+ } |
+ // TestResponseProvider implementation. |
+ bool CanHandleRequest(const Request& request) override; |
+ void GetResponseHeadersAndBody( |
+ const Request& request, |
+ scoped_refptr<net::HttpResponseHeaders>* headers, |
+ std::string* response_body) override; |
+}; |
+ |
+bool TestResponseProvider::CanHandleRequest(const Request& request) { |
+ const GURL& url = request.url; |
+ return url.host() == kHttpServerHost && |
Eugene But (OOO till 7-30)
2017/02/15 01:28:04
Now when you have static methods which return form
michaeldo
2017/02/15 06:22:52
Definitely, I agree that is much nicer.
|
+ (url.path() == kFormPath || url.path() == kRedirectFormPath || |
+ url.path() == kRedirectPath || url.path() == kPrintFormDataPath); |
+} |
+ |
+void TestResponseProvider::GetResponseHeadersAndBody( |
+ const Request& request, |
+ scoped_refptr<net::HttpResponseHeaders>* headers, |
+ std::string* response_body) { |
+ const GURL& url = request.url; |
+ if (url.path() == kRedirectPath) { |
+ *headers = web::ResponseProvider::GetRedirectResponseHeaders( |
+ TestResponseProvider::GetPrintFormDataURL().spec(), net::HTTP_FOUND); |
+ return; |
+ } |
+ |
+ const char* form_html = |
+ "<form method=\"post\" action=\"%s\">" |
+ "<textarea rows=\"1\" name=\"Data\">Unicorn</textarea>" |
+ "<input type=\"submit\" value=\"%s\" id=\"%s\">" |
+ "</form>"; |
+ |
+ *headers = web::ResponseProvider::GetDefaultResponseHeaders(); |
+ if (url.path() == kFormPath) { |
+ *response_body = base::StringPrintf( |
+ form_html, TestResponseProvider::GetPrintFormDataURL().spec().c_str(), |
+ kSubmitButton, kSubmitButton); |
+ return; |
+ } else if (url.path() == kRedirectFormPath) { |
+ *response_body = base::StringPrintf( |
+ form_html, |
+ web::test::HttpServer::MakeUrl(std::string(kHttpScheme) + kRedirectPath) |
+ .spec() |
+ .c_str(), |
+ kSubmitButton, kSubmitButton); |
+ return; |
+ } else if (url.path() == kPrintFormDataPath) { |
+ *response_body = request.method + std::string(" ") + request.body; |
+ return; |
+ } |
+ NOTREACHED(); |
+} |
+ |
} // namespace |
// Tests submition of HTTP forms POST data including cases involving navigation. |
@@ -58,41 +130,15 @@ const char kExpectedPostData[] = "POST Data=Unicorn"; |
// Sets up server urls and responses. |
- (void)setUp { |
[super setUp]; |
- std::map<GURL, HtmlResponseProviderImpl::Response> responses; |
- const char* formHtml = |
- "<form method=\"post\" action=\"%s\">" |
- "<textarea rows=\"1\" name=\"Data\">Unicorn</textarea>" |
- "<input type=\"submit\" value=\"%s\" id=\"%s\">" |
- "</form>"; |
- GURL printFormDataUrl = web::test::HttpServer::MakeUrl(kPrintFormDataUrl); |
- |
- const GURL formUrl = web::test::HttpServer::MakeUrl(kFormUrl); |
- responses[formUrl] = HtmlResponseProviderImpl::GetSimpleResponse( |
- base::StringPrintf(formHtml, printFormDataUrl.spec().c_str(), |
- kSubmitButton, kSubmitButton)); |
- |
- const GURL redirectFormUrl = web::test::HttpServer::MakeUrl(kRedirectFormUrl); |
- const std::string redirectFormResponse = base::StringPrintf( |
- formHtml, web::test::HttpServer::MakeUrl(kRedirectUrl).spec().c_str(), |
- kSubmitButton, kSubmitButton); |
- responses[redirectFormUrl] = |
- HtmlResponseProviderImpl::GetSimpleResponse(redirectFormResponse); |
- |
- const GURL redirectUrl = web::test::HttpServer::MakeUrl(kRedirectUrl); |
- responses[redirectUrl] = HtmlResponseProviderImpl::GetRedirectResponse( |
- printFormDataUrl, net::HTTP_FOUND); |
- |
- std::unique_ptr<web::DataResponseProvider> provider( |
- new HtmlResponseProvider(responses)); |
- web::test::SetUpHttpServer(std::move(provider)); |
+ web::test::SetUpHttpServer(base::MakeUnique<TestResponseProvider>()); |
} |
// Submits the html form and verifies the destination url. |
- (void)submitForm { |
chrome_test_util::TapWebViewElementWithId(kSubmitButton); |
- GURL url = web::test::HttpServer::MakeUrl(kPrintFormDataUrl); |
+ GURL url = TestResponseProvider::GetPrintFormDataURL(); |
id<GREYMatcher> URLMatcher = chrome_test_util::OmniboxText(url.GetContent()); |
[[EarlGrey selectElementWithMatcher:URLMatcher] |
assertWithMatcher:grey_notNil()]; |
@@ -100,7 +146,7 @@ const char kExpectedPostData[] = "POST Data=Unicorn"; |
// Waits for the |expectedResponse| within the web view. |
- (void)waitForExpectedResponse:(std::string)expectedResponse { |
- [[GREYCondition |
+ GREYCondition* condition = [GREYCondition |
conditionWithName:@"Waiting for webview to display resulting text." |
block:^BOOL { |
id<GREYMatcher> webViewMatcher = |
@@ -111,21 +157,24 @@ const char kExpectedPostData[] = "POST Data=Unicorn"; |
assertWithMatcher:grey_notNil() |
error:&error]; |
return error == nil; |
- }] waitWithTimeout:5]; |
+ }]; |
+ GREYAssert([condition waitWithTimeout:5], @"Webview text was not displayed."); |
} |
// Waits for view with Tab History accessibility ID. |
- (void)waitForTabHistoryView { |
- [[GREYCondition conditionWithName:@"Waiting for Tab History to display." |
- block:^BOOL { |
- NSError* error = nil; |
- id<GREYMatcher> tabHistory = |
- grey_accessibilityID(@"Tab History"); |
- [[EarlGrey selectElementWithMatcher:tabHistory] |
- assertWithMatcher:grey_notNil() |
- error:&error]; |
- return error == nil; |
- }] waitWithTimeout:5]; |
+ GREYCondition* condition = [GREYCondition |
+ conditionWithName:@"Waiting for Tab History to display." |
+ block:^BOOL { |
+ NSError* error = nil; |
+ id<GREYMatcher> tabHistory = |
+ grey_accessibilityID(@"Tab History"); |
+ [[EarlGrey selectElementWithMatcher:tabHistory] |
+ assertWithMatcher:grey_notNil() |
+ error:&error]; |
+ return error == nil; |
+ }]; |
+ GREYAssert([condition waitWithTimeout:5], @"Tab History View not displayed."); |
} |
// Reloads the web view and waits for the loading to complete. |
@@ -174,7 +223,7 @@ const char kExpectedPostData[] = "POST Data=Unicorn"; |
// Tests whether the request data is reposted correctly. |
- (void)testRepostForm { |
- [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kFormUrl)]; |
+ [ChromeEarlGrey loadURL:TestResponseProvider::GetFormURL()]; |
[self submitForm]; |
[self waitForExpectedResponse:kExpectedPostData]; |
@@ -188,12 +237,13 @@ const char kExpectedPostData[] = "POST Data=Unicorn"; |
// Tests that a POST followed by navigating to a new page and then tapping back |
// to the form result page resends data. |
- (void)testRepostFormAfterTappingBack { |
- [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kFormUrl)]; |
+ [ChromeEarlGrey loadURL:TestResponseProvider::GetFormURL()]; |
[self submitForm]; |
// Go to a new page. |
- [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kGenericUrl)]; |
+ [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl( |
+ std::string(kHttpScheme) + kGenericPath)]; |
Eugene But (OOO till 7-30)
2017/02/15 01:28:04
Could you please create static method for this URL
michaeldo
2017/02/15 06:22:52
Done.
|
// Go back and check that the data is reposted. |
[self goBack]; |
@@ -204,7 +254,7 @@ const char kExpectedPostData[] = "POST Data=Unicorn"; |
// Tests that a POST followed by tapping back to the form page and then tapping |
// forward to the result page resends data. |
- (void)testRepostFormAfterTappingBackAndForward { |
- [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kFormUrl)]; |
+ [ChromeEarlGrey loadURL:TestResponseProvider::GetFormURL()]; |
[self submitForm]; |
[self goBack]; |
@@ -216,18 +266,18 @@ const char kExpectedPostData[] = "POST Data=Unicorn"; |
// Tests that a POST followed by a new request and then index navigation to get |
// back to the result page resends data. |
- (void)testRepostFormAfterIndexNavigation { |
- [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kFormUrl)]; |
+ [ChromeEarlGrey loadURL:TestResponseProvider::GetFormURL()]; |
[self submitForm]; |
// Go to a new page. |
- [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kGenericUrl)]; |
+ [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl( |
+ std::string(kHttpScheme) + kGenericPath)]; |
Eugene But (OOO till 7-30)
2017/02/15 01:28:04
ditto
michaeldo
2017/02/15 06:22:52
Done.
|
[self openBackHistory]; |
[self waitForTabHistoryView]; |
- const GURL printURL = web::test::HttpServer::MakeUrl(kPrintFormDataUrl); |
- id<GREYMatcher> historyItem = |
- grey_text(base::SysUTF8ToNSString(printURL.spec())); |
+ id<GREYMatcher> historyItem = grey_text(base::SysUTF8ToNSString( |
Eugene But (OOO till 7-30)
2017/02/15 01:28:04
Optional nit: How about creating a function for th
michaeldo
2017/02/15 06:22:52
The matcher is only used once, so I'll leave it al
|
+ TestResponseProvider::GetPrintFormDataURL().spec())); |
[[EarlGrey selectElementWithMatcher:historyItem] performAction:grey_tap()]; |
[ChromeEarlGrey waitForPageToFinishLoading]; |
@@ -238,7 +288,7 @@ const char kExpectedPostData[] = "POST Data=Unicorn"; |
// When data is not re-sent, the request is done with a GET method. |
- (void)testRepostFormCancelling { |
- [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kFormUrl)]; |
+ [ChromeEarlGrey loadURL:TestResponseProvider::GetFormURL()]; |
[self submitForm]; |
[self reloadPage]; |
@@ -267,7 +317,8 @@ const char kExpectedPostData[] = "POST Data=Unicorn"; |
// Tests that a POST followed by a redirect does not show the popup. |
- (void)testRepostFormCancellingAfterRedirect { |
- [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kRedirectFormUrl)]; |
+ [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl( |
Eugene But (OOO till 7-30)
2017/02/15 01:28:04
Could you please create static method for this URL
michaeldo
2017/02/15 06:22:52
Done.
|
+ std::string(kHttpScheme) + kRedirectFormPath)]; |
// Submit the form, which redirects before printing the data. |
[self submitForm]; |
// Check that the redirect changes the POST to a GET. |