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 <XCTest/XCTest.h> |
| 6 |
| 7 #include "base/mac/scoped_nsobject.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "components/strings/grit/components_strings.h" |
| 11 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" |
| 12 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 13 #include "ios/chrome/browser/ui/ui_util.h" |
| 14 #import "ios/chrome/test/app/chrome_test_util.h" |
| 15 #include "ios/chrome/test/app/navigation_test_util.h" |
| 16 #include "ios/chrome/test/app/web_view_interaction_test_util.h" |
| 17 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 18 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 19 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 20 #import "ios/testing/wait_util.h" |
| 21 #import "ios/web/public/test/http_server.h" |
| 22 #import "ios/web/public/test/http_server_util.h" |
| 23 #include "ios/web/public/test/response_providers/html_response_provider.h" |
| 24 #include "ios/web/public/test/response_providers/html_response_provider_impl.h" |
| 25 |
| 26 namespace { |
| 27 |
| 28 // URL for a generic website in the user navigation flow. |
| 29 const char kGenericUrl[] = "http://generic"; |
| 30 |
| 31 // URL for the server to print the HTTP method and the request body. |
| 32 const char kPrintFormDataUrl[] = "http://printFormData"; |
| 33 |
| 34 // URL for the server that redirects to kPrintPostData with a 302. |
| 35 const char kRedirectUrl[] = "http://redirect"; |
| 36 |
| 37 // URL for the server to return a page that posts a form with some data to |
| 38 // |kPrintPostData|. |
| 39 const char kFormUrl[] = "http://formURL"; |
| 40 |
| 41 // URL for the server to return a page that posts to |kRedirect|. |
| 42 const char kRedirectFormUrl[] = "http://redirectFormURL"; |
| 43 |
| 44 // Label for the button in the form. |
| 45 const char kSubmitButton[] = "Submit"; |
| 46 |
| 47 // Expected response from the server. |
| 48 const char kExpectedPostData[] = "POST Data=Unicorn"; |
| 49 |
| 50 } // namespace |
| 51 |
| 52 // Tests submition of HTTP forms POST data including cases involving navigation. |
| 53 @interface FormsTestCase : ChromeTestCase |
| 54 @end |
| 55 |
| 56 @implementation FormsTestCase |
| 57 |
| 58 // Sets up server urls and responses. |
| 59 - (void)setUp { |
| 60 [super setUp]; |
| 61 std::map<GURL, HtmlResponseProviderImpl::Response> responses; |
| 62 |
| 63 const char* formHtml = |
| 64 "<form method=\"post\" action=\"%s\">" |
| 65 "<textarea rows=\"1\" name=\"Data\">Unicorn</textarea>" |
| 66 "<input type=\"submit\" value=\"%s\" id=\"%s\">" |
| 67 "</form>"; |
| 68 GURL printFormDataUrl = web::test::HttpServer::MakeUrl(kPrintFormDataUrl); |
| 69 |
| 70 const GURL formUrl = web::test::HttpServer::MakeUrl(kFormUrl); |
| 71 responses[formUrl] = HtmlResponseProviderImpl::GetSimpleResponse( |
| 72 base::StringPrintf(formHtml, printFormDataUrl.spec().c_str(), |
| 73 kSubmitButton, kSubmitButton)); |
| 74 |
| 75 const GURL redirectFormUrl = web::test::HttpServer::MakeUrl(kRedirectFormUrl); |
| 76 const std::string redirectFormResponse = base::StringPrintf( |
| 77 formHtml, web::test::HttpServer::MakeUrl(kRedirectUrl).spec().c_str(), |
| 78 kSubmitButton, kSubmitButton); |
| 79 responses[redirectFormUrl] = |
| 80 HtmlResponseProviderImpl::GetSimpleResponse(redirectFormResponse); |
| 81 |
| 82 const GURL redirectUrl = web::test::HttpServer::MakeUrl(kRedirectUrl); |
| 83 responses[redirectUrl] = HtmlResponseProviderImpl::GetRedirectResponse( |
| 84 printFormDataUrl, net::HTTP_FOUND); |
| 85 |
| 86 std::unique_ptr<web::DataResponseProvider> provider( |
| 87 new HtmlResponseProvider(responses)); |
| 88 web::test::SetUpHttpServer(std::move(provider)); |
| 89 } |
| 90 |
| 91 // Submits the html form and verifies the destination url. |
| 92 - (void)submitForm { |
| 93 chrome_test_util::TapWebViewElementWithId(kSubmitButton); |
| 94 |
| 95 GURL url = web::test::HttpServer::MakeUrl(kPrintFormDataUrl); |
| 96 id<GREYMatcher> URLMatcher = chrome_test_util::omniboxText(url.GetContent()); |
| 97 [[EarlGrey selectElementWithMatcher:URLMatcher] |
| 98 assertWithMatcher:grey_notNil()]; |
| 99 } |
| 100 |
| 101 // Waits for the |expectedResponse| within the web view. |
| 102 - (void)waitForExpectedResponse:(std::string)expectedResponse { |
| 103 [[GREYCondition |
| 104 conditionWithName:@"Waiting for webview to display resulting text." |
| 105 block:^BOOL { |
| 106 id<GREYMatcher> webViewMatcher = |
| 107 chrome_test_util::webViewContainingText( |
| 108 expectedResponse); |
| 109 NSError* error = nil; |
| 110 [[EarlGrey selectElementWithMatcher:webViewMatcher] |
| 111 assertWithMatcher:grey_notNil() |
| 112 error:&error]; |
| 113 return error == nil; |
| 114 }] waitWithTimeout:5]; |
| 115 } |
| 116 |
| 117 // Waits for view with Tab History accessibility ID. |
| 118 - (void)waitForTabHistoryView { |
| 119 [[GREYCondition conditionWithName:@"Waiting for Tab History to display." |
| 120 block:^BOOL { |
| 121 NSError* error = nil; |
| 122 id<GREYMatcher> tabHistory = |
| 123 grey_accessibilityID(@"Tab History"); |
| 124 [[EarlGrey selectElementWithMatcher:tabHistory] |
| 125 assertWithMatcher:grey_notNil() |
| 126 error:&error]; |
| 127 return error == nil; |
| 128 }] waitWithTimeout:5]; |
| 129 } |
| 130 |
| 131 // Reloads the web view and waits for the loading to complete. |
| 132 // TODO(crbug.com/638674): Evaluate if this can move to shared code |
| 133 - (void)reloadPage { |
| 134 base::scoped_nsobject<GenericChromeCommand> reloadCommand( |
| 135 [[GenericChromeCommand alloc] initWithTag:IDC_RELOAD]); |
| 136 chrome_test_util::RunCommandWithActiveViewController(reloadCommand); |
| 137 |
| 138 [ChromeEarlGrey waitForPageToFinishLoading]; |
| 139 } |
| 140 |
| 141 // Navigates back to the previous webpage. |
| 142 - (void)goBack { |
| 143 base::scoped_nsobject<GenericChromeCommand> backCommand( |
| 144 [[GenericChromeCommand alloc] initWithTag:IDC_BACK]); |
| 145 chrome_test_util::RunCommandWithActiveViewController(backCommand); |
| 146 |
| 147 [ChromeEarlGrey waitForPageToFinishLoading]; |
| 148 } |
| 149 |
| 150 // Open back navigation history. |
| 151 - (void)openBackHistory { |
| 152 id<GREYMatcher> back = |
| 153 chrome_test_util::buttonWithAccessibilityLabelId(IDS_ACCNAME_BACK); |
| 154 [[EarlGrey selectElementWithMatcher:back] performAction:grey_longPress()]; |
| 155 } |
| 156 |
| 157 // Navigates forward to a previous webpage. |
| 158 // TODO(crbug.com/638674): Evaluate if this can move to shared code |
| 159 - (void)goForward { |
| 160 base::scoped_nsobject<GenericChromeCommand> forwardCommand( |
| 161 [[GenericChromeCommand alloc] initWithTag:IDC_FORWARD]); |
| 162 chrome_test_util::RunCommandWithActiveViewController(forwardCommand); |
| 163 |
| 164 [ChromeEarlGrey waitForPageToFinishLoading]; |
| 165 } |
| 166 |
| 167 // Accepts the warning that the form POST data will be resent. |
| 168 - (void)confirmResendWarning { |
| 169 id<GREYMatcher> resendWarning = |
| 170 chrome_test_util::buttonWithAccessibilityLabelId( |
| 171 IDS_HTTP_POST_WARNING_RESEND); |
| 172 [[EarlGrey selectElementWithMatcher:resendWarning] |
| 173 performAction:grey_longPress()]; |
| 174 } |
| 175 |
| 176 // Tests whether the request data is resent correctly. |
| 177 - (void)testFormsResendPostData { |
| 178 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kFormUrl)]; |
| 179 |
| 180 [self submitForm]; |
| 181 [self waitForExpectedResponse:kExpectedPostData]; |
| 182 |
| 183 [self reloadPage]; |
| 184 [self confirmResendWarning]; |
| 185 |
| 186 [self waitForExpectedResponse:kExpectedPostData]; |
| 187 } |
| 188 |
| 189 // Tests that a POST followed by navigating to a new page and then tapping back |
| 190 // to the form result page resends data. |
| 191 - (void)testFormsResendPostDataAfterTappingBack { |
| 192 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kFormUrl)]; |
| 193 |
| 194 [self submitForm]; |
| 195 |
| 196 // Go to a new page. |
| 197 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kGenericUrl)]; |
| 198 |
| 199 // Go back and check that the data is resent. |
| 200 [self goBack]; |
| 201 [self confirmResendWarning]; |
| 202 [self waitForExpectedResponse:kExpectedPostData]; |
| 203 } |
| 204 |
| 205 // Tests that a POST followed by tapping back to the form page and then tapping |
| 206 // forward to the result page resends data. |
| 207 - (void)testFormsResendPostDataAfterTappingBackAndForward { |
| 208 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kFormUrl)]; |
| 209 [self submitForm]; |
| 210 |
| 211 [self goBack]; |
| 212 [self goForward]; |
| 213 [self confirmResendWarning]; |
| 214 [self waitForExpectedResponse:kExpectedPostData]; |
| 215 } |
| 216 |
| 217 // Tests that a POST followed by a new request and then index navigation to get |
| 218 // back to the result page resends data. |
| 219 - (void)testFormsResendPostDataAfterIndexNavigation { |
| 220 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kFormUrl)]; |
| 221 [self submitForm]; |
| 222 |
| 223 // Go to a new page. |
| 224 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kGenericUrl)]; |
| 225 |
| 226 [self openBackHistory]; |
| 227 [self waitForTabHistoryView]; |
| 228 |
| 229 const GURL printURL = web::test::HttpServer::MakeUrl(kPrintFormDataUrl); |
| 230 id<GREYMatcher> historyItem = |
| 231 grey_text(base::SysUTF8ToNSString(printURL.spec())); |
| 232 [[EarlGrey selectElementWithMatcher:historyItem] performAction:grey_tap()]; |
| 233 |
| 234 [ChromeEarlGrey waitForPageToFinishLoading]; |
| 235 |
| 236 [self confirmResendWarning]; |
| 237 [self waitForExpectedResponse:kExpectedPostData]; |
| 238 } |
| 239 |
| 240 // When data is not re-sent, the request is done with a GET method. |
| 241 - (void)testFormsDontResendPostData { |
| 242 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kFormUrl)]; |
| 243 [self submitForm]; |
| 244 |
| 245 [self reloadPage]; |
| 246 |
| 247 // Abort the reload. |
| 248 if (IsIPadIdiom()) { |
| 249 // On tablet, dismiss the popover. |
| 250 base::scoped_nsobject<GREYElementMatcherBlock> matcher([ |
| 251 [GREYElementMatcherBlock alloc] |
| 252 initWithMatchesBlock:^BOOL(UIView* view) { |
| 253 return [NSStringFromClass([view class]) hasPrefix:@"UIDimmingView"]; |
| 254 } |
| 255 descriptionBlock:^(id<GREYDescription> description) { |
| 256 [description appendText:@"class prefixed with UIDimmingView"]; |
| 257 }]); |
| 258 [[EarlGrey selectElementWithMatcher:matcher] |
| 259 performAction:grey_tapAtPoint(CGPointMake(50.0f, 50.0f))]; |
| 260 } else { |
| 261 // On handset, dismiss via the cancel button. |
| 262 id<GREYMatcher> cancel = |
| 263 chrome_test_util::buttonWithAccessibilityLabelId(IDS_CANCEL); |
| 264 [[EarlGrey selectElementWithMatcher:cancel] performAction:grey_tap()]; |
| 265 } |
| 266 // Check that the POST is changed to a GET |
| 267 [self waitForExpectedResponse:"GET"]; |
| 268 } |
| 269 |
| 270 // Tests that a POST followed by a redirect does not show the popup. |
| 271 - (void)testFormsDontResendPostDataAfterRedirect { |
| 272 [ChromeEarlGrey loadURL:web::test::HttpServer::MakeUrl(kRedirectFormUrl)]; |
| 273 // Submit the form, which redirects before printing the data. |
| 274 [self submitForm]; |
| 275 // Check that the redirect changes the POST to a GET. |
| 276 [self waitForExpectedResponse:"GET"]; |
| 277 [self reloadPage]; |
| 278 |
| 279 // Check that the popup did not show |
| 280 id<GREYMatcher> resendWarning = |
| 281 chrome_test_util::buttonWithAccessibilityLabelId( |
| 282 IDS_HTTP_POST_WARNING_RESEND); |
| 283 [[EarlGrey selectElementWithMatcher:resendWarning] |
| 284 assertWithMatcher:grey_nil()]; |
| 285 |
| 286 [self waitForExpectedResponse:"GET"]; |
| 287 } |
| 288 |
| 289 @end |
OLD | NEW |