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..0f481b540d2288fc09e71b5c06807d5b21eb3a01 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,31 @@ |
#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://"; |
Eugene But (OOO till 7-30)
2017/02/14 21:44:43
kHttpScheme
michaeldo
2017/02/15 00:56:34
Done.
|
-// URL for the server to print the HTTP method and the request body. |
-const char kPrintFormDataUrl[] = "http://printFormData"; |
+// Test server domain. |
+const char kHttpServerDomain[] = "localhost"; |
Eugene But (OOO till 7-30)
2017/02/14 21:44:43
nit: kHttpServerHost ?
michaeldo
2017/02/15 00:56:34
Done.
|
-// 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 for the server 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 for the server that redirects to |kPrintFormDataPath| with a 302. |
+const char kRedirectPath[] = "/redirect/"; |
+ |
+// Path for the server to return a page that posts a form with some data to |
+// |kPrintFormDataPath|. |
+const char kFormPath[] = "/form/"; |
+ |
+// Path for the server to return a page that posts to |kRedirectPath|. |
+const char kRedirectFormPath[] = "/redirectform/"; |
// Label for the button in the form. |
const char kSubmitButton[] = "Submit"; |
@@ -47,6 +53,67 @@ 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: |
+ // 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() == kHttpServerDomain && |
+ (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) { |
+ GURL printFormDataUrl = web::test::HttpServer::MakeUrl( |
Eugene But (OOO till 7-30)
2017/02/14 21:44:43
print_form_data_url
michaeldo
2017/02/15 00:56:34
Done.
|
+ std::string(kHTTPScheme) + kPrintFormDataPath); |
Eugene But (OOO till 7-30)
2017/02/14 21:44:43
How about using url::SchemeHostPort instead of str
michaeldo
2017/02/15 00:56:34
I agree that url::SchemeHostPort is more ideal, bu
Eugene But (OOO till 7-30)
2017/02/15 01:28:04
I suggested to use url::SchemeHostPort to avoid st
|
+ |
+ const GURL& url = request.url; |
+ if (url.path() == kRedirectPath) { |
+ *headers = web::ResponseProvider::GetRedirectResponseHeaders( |
+ printFormDataUrl.spec(), net::HTTP_FOUND); |
+ return; |
+ } |
+ |
+ const char* formHtml = |
Eugene But (OOO till 7-30)
2017/02/14 21:44:43
form_html
michaeldo
2017/02/15 00:56:34
Done.
|
+ "<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(formHtml, printFormDataUrl.spec().c_str(), |
+ kSubmitButton, kSubmitButton); |
+ return; |
+ } else if (url.path() == kRedirectFormPath) { |
+ *response_body = base::StringPrintf( |
+ formHtml, |
+ 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 +125,16 @@ 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 = web::test::HttpServer::MakeUrl(std::string(kHTTPScheme) + |
Eugene But (OOO till 7-30)
2017/02/14 21:44:43
Optional nit: Do you want to create TestResponsePr
michaeldo
2017/02/15 00:56:34
Done. This was a great cleanup point so I similarl
|
+ kPrintFormDataPath); |
id<GREYMatcher> URLMatcher = chrome_test_util::OmniboxText(url.GetContent()); |
[[EarlGrey selectElementWithMatcher:URLMatcher] |
assertWithMatcher:grey_notNil()]; |
@@ -100,7 +142,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 +153,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 +219,8 @@ 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:web::test::HttpServer::MakeUrl( |
+ std::string(kHTTPScheme) + kFormPath)]; |
[self submitForm]; |
[self waitForExpectedResponse:kExpectedPostData]; |
@@ -188,12 +234,14 @@ 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:web::test::HttpServer::MakeUrl( |
+ std::string(kHTTPScheme) + kFormPath)]; |
[self submitForm]; |
// Go to a new page. |
- [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kGenericUrl)]; |
+ [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl( |
+ std::string(kHTTPScheme) + kGenericPath)]; |
// Go back and check that the data is reposted. |
[self goBack]; |
@@ -204,7 +252,8 @@ 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:web::test::HttpServer::MakeUrl( |
+ std::string(kHTTPScheme) + kFormPath)]; |
[self submitForm]; |
[self goBack]; |
@@ -216,16 +265,19 @@ 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:web::test::HttpServer::MakeUrl( |
+ std::string(kHTTPScheme) + kFormPath)]; |
[self submitForm]; |
// Go to a new page. |
- [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kGenericUrl)]; |
+ [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl( |
+ std::string(kHTTPScheme) + kGenericPath)]; |
[self openBackHistory]; |
[self waitForTabHistoryView]; |
- const GURL printURL = web::test::HttpServer::MakeUrl(kPrintFormDataUrl); |
+ const GURL printURL = web::test::HttpServer::MakeUrl( |
+ std::string(kHTTPScheme) + kPrintFormDataPath); |
id<GREYMatcher> historyItem = |
grey_text(base::SysUTF8ToNSString(printURL.spec())); |
[[EarlGrey selectElementWithMatcher:historyItem] performAction:grey_tap()]; |
@@ -238,7 +290,8 @@ 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:web::test::HttpServer::MakeUrl( |
+ std::string(kHTTPScheme) + kFormPath)]; |
[self submitForm]; |
[self reloadPage]; |
@@ -267,7 +320,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( |
+ 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. |