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 #include <map> |
| 7 #include <memory> |
| 8 #include <string> |
| 9 |
| 10 #include "base/ios/ios_util.h" |
| 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/strings/sys_string_conversions.h" |
| 13 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 14 #include "components/content_settings/core/common/content_settings.h" |
| 15 #include "ios/chrome/browser/content_settings/host_content_settings_map_factory.
h" |
| 16 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" |
| 17 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 18 #include "ios/chrome/browser/ui/ui_util.h" |
| 19 #include "ios/chrome/grit/ios_strings.h" |
| 20 #import "ios/chrome/test/app/chrome_test_util.h" |
| 21 #include "ios/chrome/test/app/navigation_test_util.h" |
| 22 #include "ios/chrome/test/app/web_view_interaction_test_util.h" |
| 23 #import "ios/chrome/test/earl_grey/chrome_assertions.h" |
| 24 #import "ios/chrome/test/earl_grey/chrome_earl_grey.h" |
| 25 #import "ios/chrome/test/earl_grey/chrome_earl_grey_ui.h" |
| 26 #import "ios/chrome/test/earl_grey/chrome_matchers.h" |
| 27 #import "ios/chrome/test/earl_grey/chrome_test_case.h" |
| 28 #import "ios/testing/earl_grey/disabled_test_macros.h" |
| 29 #import "ios/testing/wait_util.h" |
| 30 #import "ios/web/public/test/earl_grey/web_view_actions.h" |
| 31 #import "ios/web/public/test/earl_grey/web_view_matchers.h" |
| 32 #import "ios/web/public/test/http_server.h" |
| 33 #include "ios/web/public/test/http_server_util.h" |
| 34 #include "ios/web/public/test/response_providers/data_response_provider.h" |
| 35 #include "net/http/http_response_headers.h" |
| 36 #include "ui/base/l10n/l10n_util.h" |
| 37 #include "url/gurl.h" |
| 38 |
| 39 using chrome_test_util::omniboxText; |
| 40 using chrome_test_util::webViewContainingText; |
| 41 |
| 42 namespace { |
| 43 |
| 44 // URL used for the reload test. |
| 45 const char kReloadTestUrl[] = "http://mock/reloadTest"; |
| 46 |
| 47 // Returns the number of serviced requests in HTTP body. |
| 48 class ReloadResponseProvider : public web::DataResponseProvider { |
| 49 public: |
| 50 ReloadResponseProvider() : request_number_(0) {} |
| 51 |
| 52 // URL used for the reload test. |
| 53 static GURL GetReloadTestUrl() { |
| 54 return web::test::HttpServer::MakeUrl(kReloadTestUrl); |
| 55 } |
| 56 |
| 57 bool CanHandleRequest(const Request& request) override { |
| 58 return request.url == ReloadResponseProvider::GetReloadTestUrl(); |
| 59 } |
| 60 |
| 61 void GetResponseHeadersAndBody( |
| 62 const Request& request, |
| 63 scoped_refptr<net::HttpResponseHeaders>* headers, |
| 64 std::string* response_body) override { |
| 65 DCHECK_EQ(ReloadResponseProvider::GetReloadTestUrl(), request.url); |
| 66 *headers = GetDefaultResponseHeaders(); |
| 67 *response_body = GetResponseBody(request_number_++); |
| 68 } |
| 69 |
| 70 // static |
| 71 static std::string GetResponseBody(int request_number) { |
| 72 return base::StringPrintf("Load request %d", request_number); |
| 73 } |
| 74 |
| 75 private: |
| 76 int request_number_; // Count of requests received by the response provider. |
| 77 }; |
| 78 |
| 79 // ScopedBlockPopupsPref modifies the block popups preference and resets the |
| 80 // preference to its original value when this object goes out of scope. |
| 81 // TODO(crbug.com/638674): Evaluate if this can move to shared code |
| 82 class ScopedBlockPopupsPref { |
| 83 public: |
| 84 ScopedBlockPopupsPref(ContentSetting setting) { |
| 85 original_setting_ = GetPrefValue(); |
| 86 SetPrefValue(setting); |
| 87 } |
| 88 ~ScopedBlockPopupsPref() { SetPrefValue(original_setting_); } |
| 89 |
| 90 private: |
| 91 // Gets the current value of the preference. |
| 92 ContentSetting GetPrefValue() { |
| 93 ContentSetting popupSetting = |
| 94 ios::HostContentSettingsMapFactory::GetForBrowserState( |
| 95 chrome_test_util::GetOriginalBrowserState()) |
| 96 ->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, NULL); |
| 97 return popupSetting; |
| 98 } |
| 99 |
| 100 // Sets the preference to the given value. |
| 101 void SetPrefValue(ContentSetting setting) { |
| 102 DCHECK(setting == CONTENT_SETTING_BLOCK || |
| 103 setting == CONTENT_SETTING_ALLOW); |
| 104 ios::ChromeBrowserState* state = |
| 105 chrome_test_util::GetOriginalBrowserState(); |
| 106 ios::HostContentSettingsMapFactory::GetForBrowserState(state) |
| 107 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, setting); |
| 108 } |
| 109 |
| 110 // Saves the original pref setting so that it can be restored when the scoper |
| 111 // is destroyed. |
| 112 ContentSetting original_setting_; |
| 113 |
| 114 DISALLOW_COPY_AND_ASSIGN(ScopedBlockPopupsPref); |
| 115 }; |
| 116 } // namespace |
| 117 |
| 118 // Tests web browsing scenarios. |
| 119 @interface BrowsingTestCase : ChromeTestCase |
| 120 @end |
| 121 |
| 122 @implementation BrowsingTestCase |
| 123 |
| 124 // Matcher for the title of the current tab (on tablet only). |
| 125 id<GREYMatcher> tabWithTitle(const std::string& tab_title) { |
| 126 id<GREYMatcher> notPartOfOmnibox = |
| 127 grey_not(grey_ancestor(chrome_test_util::omnibox())); |
| 128 return grey_allOf(grey_accessibilityLabel(base::SysUTF8ToNSString(tab_title)), |
| 129 notPartOfOmnibox, nil); |
| 130 } |
| 131 |
| 132 // Tests that page successfully reloads. |
| 133 - (void)testReload { |
| 134 // Set up test HTTP server responses. |
| 135 std::unique_ptr<web::DataResponseProvider> provider( |
| 136 new ReloadResponseProvider()); |
| 137 web::test::SetUpHttpServer(std::move(provider)); |
| 138 |
| 139 GURL URL = ReloadResponseProvider::GetReloadTestUrl(); |
| 140 [ChromeEarlGrey loadURL:URL]; |
| 141 std::string expectedBodyBeforeReload( |
| 142 ReloadResponseProvider::GetResponseBody(0 /* request number */)); |
| 143 [[EarlGrey |
| 144 selectElementWithMatcher:webViewContainingText(expectedBodyBeforeReload)] |
| 145 assertWithMatcher:grey_notNil()]; |
| 146 |
| 147 [ChromeEarlGreyUI reload]; |
| 148 std::string expectedBodyAfterReload( |
| 149 ReloadResponseProvider::GetResponseBody(1 /* request_number */)); |
| 150 [[EarlGrey |
| 151 selectElementWithMatcher:webViewContainingText(expectedBodyAfterReload)] |
| 152 assertWithMatcher:grey_notNil()]; |
| 153 } |
| 154 |
| 155 // Tests that a tab's title is based on the URL when no other information is |
| 156 // available. |
| 157 - (void)testBrowsingTabTitleSetFromURL { |
| 158 if (!IsIPadIdiom()) { |
| 159 EARL_GREY_TEST_SKIPPED(@"Tab Title not displayed on handset."); |
| 160 } |
| 161 |
| 162 web::test::SetUpFileBasedHttpServer(); |
| 163 |
| 164 const GURL destinationURL = web::test::HttpServer::MakeUrl( |
| 165 "http://ios/testing/data/http_server_files/destination.html"); |
| 166 [ChromeEarlGrey loadURL:destinationURL]; |
| 167 |
| 168 // Add 3 for the "://" which is not considered part of the scheme |
| 169 std::string URLWithoutScheme = |
| 170 destinationURL.spec().substr(destinationURL.scheme().length() + 3); |
| 171 |
| 172 [[EarlGrey selectElementWithMatcher:tabWithTitle(URLWithoutScheme)] |
| 173 assertWithMatcher:grey_sufficientlyVisible()]; |
| 174 } |
| 175 |
| 176 // Tests that after a PDF is loaded, the title appears in the tab bar on iPad. |
| 177 - (void)testPDFLoadTitle { |
| 178 if (!IsIPadIdiom()) { |
| 179 EARL_GREY_TEST_SKIPPED(@"Tab Title not displayed on handset."); |
| 180 } |
| 181 |
| 182 web::test::SetUpFileBasedHttpServer(); |
| 183 |
| 184 const GURL destinationURL = web::test::HttpServer::MakeUrl( |
| 185 "http://ios/testing/data/http_server_files/testpage.pdf"); |
| 186 [ChromeEarlGrey loadURL:destinationURL]; |
| 187 |
| 188 // Add 3 for the "://" which is not considered part of the scheme |
| 189 std::string URLWithoutScheme = |
| 190 destinationURL.spec().substr(destinationURL.scheme().length() + 3); |
| 191 |
| 192 [[EarlGrey selectElementWithMatcher:tabWithTitle(URLWithoutScheme)] |
| 193 assertWithMatcher:grey_sufficientlyVisible()]; |
| 194 } |
| 195 |
| 196 // Tests that tab title is set to the specified title from a JavaScript. |
| 197 - (void)testBrowsingTabTitleSetFromScript { |
| 198 if (!IsIPadIdiom()) { |
| 199 EARL_GREY_TEST_SKIPPED(@"Tab Title not displayed on handset."); |
| 200 } |
| 201 |
| 202 const char* kPageTitle = "Some title"; |
| 203 const GURL URL = GURL(base::StringPrintf( |
| 204 "data:text/html;charset=utf-8,<script>document.title = " |
| 205 "\"%s\"</script>", |
| 206 kPageTitle)); |
| 207 [ChromeEarlGrey loadURL:URL]; |
| 208 |
| 209 [[EarlGrey selectElementWithMatcher:tabWithTitle(kPageTitle)] |
| 210 assertWithMatcher:grey_sufficientlyVisible()]; |
| 211 } |
| 212 |
| 213 // Tests clicking a link with target="_blank" and "event.stopPropagation()" |
| 214 // opens a new tab. |
| 215 - (void)testBrowsingStopPropagation { |
| 216 // Create map of canned responses and set up the test HTML server. |
| 217 std::map<GURL, std::string> responses; |
| 218 const GURL URL = web::test::HttpServer::MakeUrl("http://stopPropagation"); |
| 219 const GURL destinationURL = |
| 220 web::test::HttpServer::MakeUrl("http://destination"); |
| 221 // This is a page with a link to |kDestination|. |
| 222 responses[URL] = base::StringPrintf( |
| 223 "<a id='link' href='%s' target='_blank' " |
| 224 "onclick='event.stopPropagation()'>link</a>", |
| 225 destinationURL.spec().c_str()); |
| 226 // This is the destination page; it just contains some text. |
| 227 responses[destinationURL] = "You've arrived!"; |
| 228 web::test::SetUpSimpleHttpServer(responses); |
| 229 |
| 230 ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW); |
| 231 |
| 232 [ChromeEarlGrey loadURL:URL]; |
| 233 chrome_test_util::AssertMainTabCount(1); |
| 234 |
| 235 chrome_test_util::TapWebViewElementWithId("link"); |
| 236 |
| 237 chrome_test_util::AssertMainTabCount(2); |
| 238 |
| 239 // Verify the new tab was opened with the expected URL. |
| 240 [[EarlGrey selectElementWithMatcher:omniboxText(destinationURL.GetContent())] |
| 241 assertWithMatcher:grey_notNil()]; |
| 242 } |
| 243 |
| 244 // Tests clicking a relative link with target="_blank" and |
| 245 // "event.stopPropagation()" opens a new tab. |
| 246 - (void)testBrowsingStopPropagationRelativePath { |
| 247 // Create map of canned responses and set up the test HTML server. |
| 248 std::map<GURL, std::string> responses; |
| 249 const GURL URL = web::test::HttpServer::MakeUrl("http://stopPropRel"); |
| 250 const GURL destinationURL = |
| 251 web::test::HttpServer::MakeUrl("http://stopPropRel/#test"); |
| 252 // This is page with a relative link to "#test". |
| 253 responses[URL] = |
| 254 "<a id='link' href='#test' target='_blank' " |
| 255 "onclick='event.stopPropagation()'>link</a>"; |
| 256 // This is the page that should be showing at the end of the test. |
| 257 responses[destinationURL] = "You've arrived!"; |
| 258 web::test::SetUpSimpleHttpServer(responses); |
| 259 |
| 260 ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW); |
| 261 |
| 262 [ChromeEarlGrey loadURL:URL]; |
| 263 chrome_test_util::AssertMainTabCount(1); |
| 264 |
| 265 chrome_test_util::TapWebViewElementWithId("link"); |
| 266 |
| 267 chrome_test_util::AssertMainTabCount(2); |
| 268 |
| 269 // Verify the new tab was opened with the expected URL. |
| 270 [[EarlGrey selectElementWithMatcher:omniboxText(destinationURL.GetContent())] |
| 271 assertWithMatcher:grey_notNil()]; |
| 272 } |
| 273 |
| 274 // Tests that clicking a link with URL changed by onclick uses the href of the |
| 275 // anchor tag instead of the one specified in JavaScript. Also verifies a new |
| 276 // tab is opened by target '_blank'. |
| 277 - (void)testBrowsingPreventDefaultWithLinkOpenedByJavascript { |
| 278 // Create map of canned responses and set up the test HTML server. |
| 279 std::map<GURL, std::string> responses; |
| 280 const GURL URL = web::test::HttpServer::MakeUrl( |
| 281 "http://preventDefaultWithLinkOpenedByJavascript"); |
| 282 const GURL anchorURL = |
| 283 web::test::HttpServer::MakeUrl("http://anchorDestination"); |
| 284 const GURL destinationURL = |
| 285 web::test::HttpServer::MakeUrl("http://javaScriptDestination"); |
| 286 // This is a page with a link where the href and JavaScript are setting the |
| 287 // destination to two different URLs so the test can verify which one the |
| 288 // browser uses. |
| 289 responses[URL] = base::StringPrintf( |
| 290 "<a id='link' href='%s' target='_blank' " |
| 291 "onclick='window.location.href=\"%s\"; " |
| 292 "event.stopPropagation()' id='link'>link</a>", |
| 293 anchorURL.spec().c_str(), destinationURL.spec().c_str()); |
| 294 responses[anchorURL] = "anchor destination"; |
| 295 |
| 296 web::test::SetUpSimpleHttpServer(responses); |
| 297 |
| 298 ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW); |
| 299 |
| 300 [ChromeEarlGrey loadURL:URL]; |
| 301 chrome_test_util::AssertMainTabCount(1); |
| 302 |
| 303 chrome_test_util::TapWebViewElementWithId("link"); |
| 304 |
| 305 chrome_test_util::AssertMainTabCount(2); |
| 306 |
| 307 // Verify the new tab was opened with the expected URL. |
| 308 [[EarlGrey selectElementWithMatcher:omniboxText(anchorURL.GetContent())] |
| 309 assertWithMatcher:grey_notNil()]; |
| 310 } |
| 311 |
| 312 // Tests tapping a link that navigates to a page that immediately navigates |
| 313 // again via document.location.href. |
| 314 - (void)testBrowsingWindowDataLinkScriptRedirect { |
| 315 // Create map of canned responses and set up the test HTML server. |
| 316 std::map<GURL, std::string> responses; |
| 317 const GURL URL = |
| 318 web::test::HttpServer::MakeUrl("http://windowDataLinkScriptRedirect"); |
| 319 const GURL intermediateURL = |
| 320 web::test::HttpServer::MakeUrl("http://intermediate"); |
| 321 const GURL destinationURL = |
| 322 web::test::HttpServer::MakeUrl("http://destination"); |
| 323 // This is a page with a link to the intermediate page. |
| 324 responses[URL] = |
| 325 base::StringPrintf("<a id='link' href='%s' target='_blank'>link</a>", |
| 326 intermediateURL.spec().c_str()); |
| 327 // This intermediate page uses JavaScript to immediately navigate to the |
| 328 // destination page. |
| 329 responses[intermediateURL] = |
| 330 base::StringPrintf("<script>document.location.href=\"%s\"</script>", |
| 331 destinationURL.spec().c_str()); |
| 332 // This is the page that should be showing at the end of the test. |
| 333 responses[destinationURL] = "You've arrived!"; |
| 334 |
| 335 web::test::SetUpSimpleHttpServer(responses); |
| 336 |
| 337 ScopedBlockPopupsPref prefSetter(CONTENT_SETTING_ALLOW); |
| 338 |
| 339 [ChromeEarlGrey loadURL:URL]; |
| 340 chrome_test_util::AssertMainTabCount(1); |
| 341 |
| 342 chrome_test_util::TapWebViewElementWithId("link"); |
| 343 |
| 344 chrome_test_util::AssertMainTabCount(2); |
| 345 |
| 346 // Verify the new tab was opened with the expected URL. |
| 347 [[EarlGrey selectElementWithMatcher:omniboxText(destinationURL.GetContent())] |
| 348 assertWithMatcher:grey_notNil()]; |
| 349 } |
| 350 |
| 351 // Tests that pressing the button on a POST-based form changes the page and that |
| 352 // the back button works as expected afterwards. |
| 353 - (void)testBrowsingPostEntryWithButton { |
| 354 // Create map of canned responses and set up the test HTML server. |
| 355 std::map<GURL, std::string> responses; |
| 356 const GURL URL = web::test::HttpServer::MakeUrl("http://postEntryWithButton"); |
| 357 const GURL destinationURL = web::test::HttpServer::MakeUrl("http://foo"); |
| 358 // This is a page with a button that posts to the destination. |
| 359 responses[URL] = base::StringPrintf( |
| 360 "<form action='%s' method='post'>" |
| 361 "<input value='button' type='submit' id='button'></form>", |
| 362 destinationURL.spec().c_str()); |
| 363 // This is the page that should be showing at the end of the test. |
| 364 responses[destinationURL] = "bar!"; |
| 365 web::test::SetUpSimpleHttpServer(responses); |
| 366 |
| 367 [ChromeEarlGrey loadURL:URL]; |
| 368 chrome_test_util::TapWebViewElementWithId("button"); |
| 369 |
| 370 [[EarlGrey selectElementWithMatcher:omniboxText(destinationURL.GetContent())] |
| 371 assertWithMatcher:grey_notNil()]; |
| 372 |
| 373 // Go back and verify the browser navigates to the original URL. |
| 374 [self goBack]; |
| 375 [[EarlGrey selectElementWithMatcher:omniboxText(URL.GetContent())] |
| 376 assertWithMatcher:grey_notNil()]; |
| 377 } |
| 378 |
| 379 // Tests that a link with a JavaScript-based navigation changes the page and |
| 380 // that the back button works as expected afterwards. |
| 381 - (void)testBrowsingJavaScriptBasedNavigation { |
| 382 std::map<GURL, std::string> responses; |
| 383 const GURL URL = web::test::HttpServer::MakeUrl("http://origin"); |
| 384 const GURL destURL = web::test::HttpServer::MakeUrl("http://destination"); |
| 385 // Page containing a link with onclick attribute that sets window.location |
| 386 // to the destination URL. |
| 387 responses[URL] = base::StringPrintf( |
| 388 "<a href='#' onclick=\"window.location='%s';\" id='link'>Link</a>", |
| 389 destURL.spec().c_str()); |
| 390 // Page with some text. |
| 391 responses[destURL] = "You've arrived!"; |
| 392 web::test::SetUpSimpleHttpServer(responses); |
| 393 |
| 394 [ChromeEarlGrey loadURL:URL]; |
| 395 chrome_test_util::TapWebViewElementWithId("link"); |
| 396 |
| 397 [[EarlGrey selectElementWithMatcher:omniboxText(destURL.GetContent())] |
| 398 assertWithMatcher:grey_notNil()]; |
| 399 |
| 400 [self goBack]; |
| 401 [[EarlGrey selectElementWithMatcher:omniboxText(URL.GetContent())] |
| 402 assertWithMatcher:grey_notNil()]; |
| 403 } |
| 404 |
| 405 // TODO(crbug.com/638674): Evaluate if this can move to shared code |
| 406 // Navigates back to the previous webpage. |
| 407 - (void)goBack { |
| 408 base::scoped_nsobject<GenericChromeCommand> backCommand( |
| 409 [[GenericChromeCommand alloc] initWithTag:IDC_BACK]); |
| 410 chrome_test_util::RunCommandWithActiveViewController(backCommand); |
| 411 |
| 412 [ChromeEarlGrey waitForPageToFinishLoading]; |
| 413 } |
| 414 |
| 415 // Navigates forward to a previous webpage. |
| 416 // TODO(crbug.com/638674): Evaluate if this can move to shared code |
| 417 - (void)goForward { |
| 418 base::scoped_nsobject<GenericChromeCommand> forwardCommand( |
| 419 [[GenericChromeCommand alloc] initWithTag:IDC_FORWARD]); |
| 420 chrome_test_util::RunCommandWithActiveViewController(forwardCommand); |
| 421 |
| 422 [ChromeEarlGrey waitForPageToFinishLoading]; |
| 423 } |
| 424 |
| 425 // Tests that a link with WebUI URL does not trigger a load. WebUI pages may |
| 426 // have increased power and using the same web process (which may potentially |
| 427 // be controlled by an attacker) is dangerous. |
| 428 - (void)testTapLinkWithWebUIURL { |
| 429 // Create map of canned responses and set up the test HTML server. |
| 430 std::map<GURL, std::string> responses; |
| 431 const GURL URL(web::test::HttpServer::MakeUrl("http://pageWithWebUILink")); |
| 432 const char kPageHTML[] = |
| 433 "<script>" |
| 434 " function printMsg() {" |
| 435 " document.body.appendChild(document.createTextNode('Hello world!'));" |
| 436 " }" |
| 437 "</script>" |
| 438 "<a href='chrome://version' id='link' onclick='printMsg()'>Version</a>"; |
| 439 responses[URL] = kPageHTML; |
| 440 web::test::SetUpSimpleHttpServer(responses); |
| 441 |
| 442 // Assert that test is starting with one tab. |
| 443 chrome_test_util::AssertMainTabCount(1U); |
| 444 chrome_test_util::AssertIncognitoTabCount(0U); |
| 445 |
| 446 [ChromeEarlGrey loadURL:URL]; |
| 447 |
| 448 // Tap on chrome://version link. |
| 449 [ChromeEarlGrey tapWebViewElementWithID:@"link"]; |
| 450 |
| 451 // Verify that page did not change by checking its URL and message printed by |
| 452 // onclick event. |
| 453 [[EarlGrey selectElementWithMatcher:omniboxText("chrome://version")] |
| 454 assertWithMatcher:grey_nil()]; |
| 455 [[EarlGrey selectElementWithMatcher:webViewContainingText("Hello world!")] |
| 456 assertWithMatcher:grey_notNil()]; |
| 457 |
| 458 // Verify that no new tabs were open which could load chrome://version. |
| 459 chrome_test_util::AssertMainTabCount(1U); |
| 460 } |
| 461 |
| 462 // Tests that pressing the button on a POST-based form with same-page action |
| 463 // does not change the page and that the back button works as expected |
| 464 // afterwards. |
| 465 - (void)testBrowsingPostToSamePage { |
| 466 // Create map of canned responses and set up the test HTML server. |
| 467 std::map<GURL, std::string> responses; |
| 468 const GURL firstURL = web::test::HttpServer::MakeUrl("http://first"); |
| 469 const GURL formURL = web::test::HttpServer::MakeUrl("http://form"); |
| 470 // This is just a page with some text. |
| 471 responses[firstURL] = "foo"; |
| 472 // This is a page with at button that posts to the current URL. |
| 473 responses[formURL] = |
| 474 "<form method='post'>" |
| 475 "<input value='button' type='submit' id='button'></form>"; |
| 476 web::test::SetUpSimpleHttpServer(responses); |
| 477 |
| 478 // Open the first URL so it's in history. |
| 479 [ChromeEarlGrey loadURL:firstURL]; |
| 480 |
| 481 // Open the second URL, tap the button, and verify the browser navigates to |
| 482 // the expected URL. |
| 483 [ChromeEarlGrey loadURL:formURL]; |
| 484 chrome_test_util::TapWebViewElementWithId("button"); |
| 485 [[EarlGrey selectElementWithMatcher:omniboxText(formURL.GetContent())] |
| 486 assertWithMatcher:grey_notNil()]; |
| 487 |
| 488 // Go back once and verify the browser navigates to the form URL. |
| 489 [self goBack]; |
| 490 [[EarlGrey selectElementWithMatcher:omniboxText(formURL.GetContent())] |
| 491 assertWithMatcher:grey_notNil()]; |
| 492 |
| 493 // Go back a second time and verify the browser navigates to the first URL. |
| 494 [self goBack]; |
| 495 [[EarlGrey selectElementWithMatcher:omniboxText(firstURL.GetContent())] |
| 496 assertWithMatcher:grey_notNil()]; |
| 497 } |
| 498 |
| 499 // Tests that evaluating user JavaScript that causes navigation correctly |
| 500 // modifies history. |
| 501 - (void)testBrowsingUserJavaScriptNavigation { |
| 502 // TODO(crbug.com/640220): Keyboard entry inside the omnibox fails only on |
| 503 // iPad running iOS 10. |
| 504 if (IsIPadIdiom() && base::ios::IsRunningOnIOS10OrLater()) |
| 505 return; |
| 506 |
| 507 // Create map of canned responses and set up the test HTML server. |
| 508 std::map<GURL, std::string> responses; |
| 509 const GURL startURL = web::test::HttpServer::MakeUrl("http://startpage"); |
| 510 responses[startURL] = "<html><body><p>Ready to begin.</p></body></html>"; |
| 511 const GURL targetURL = web::test::HttpServer::MakeUrl("http://targetpage"); |
| 512 responses[targetURL] = "<html><body><p>You've arrived!</p></body></html>"; |
| 513 web::test::SetUpSimpleHttpServer(responses); |
| 514 |
| 515 // Load the first page and run JS (using the codepath that user-entered JS in |
| 516 // the omnibox would take, not page-triggered) that should navigate. |
| 517 [ChromeEarlGrey loadURL:startURL]; |
| 518 |
| 519 NSString* script = |
| 520 [NSString stringWithFormat:@"javascript:window.location='%s'", |
| 521 targetURL.spec().c_str()]; |
| 522 [[EarlGrey selectElementWithMatcher:chrome_test_util::omnibox()] |
| 523 performAction:grey_typeText(script)]; |
| 524 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Go")] |
| 525 performAction:grey_tap()]; |
| 526 [ChromeEarlGrey waitForPageToFinishLoading]; |
| 527 |
| 528 [[EarlGrey selectElementWithMatcher:omniboxText(targetURL.GetContent())] |
| 529 assertWithMatcher:grey_notNil()]; |
| 530 |
| 531 [self goBack]; |
| 532 [[EarlGrey selectElementWithMatcher:omniboxText(startURL.GetContent())] |
| 533 assertWithMatcher:grey_notNil()]; |
| 534 } |
| 535 |
| 536 // Tests that evaluating non-navigation user JavaScript doesn't affect history. |
| 537 - (void)testBrowsingUserJavaScriptWithoutNavigation { |
| 538 // TODO(crbug.com/640220): Keyboard entry inside the omnibox fails only on |
| 539 // iPad running iOS 10. |
| 540 if (IsIPadIdiom() && base::ios::IsRunningOnIOS10OrLater()) |
| 541 return; |
| 542 |
| 543 // Create map of canned responses and set up the test HTML server. |
| 544 std::map<GURL, std::string> responses; |
| 545 const GURL firstURL = web::test::HttpServer::MakeUrl("http://firstURL"); |
| 546 const std::string firstResponse = "Test Page 1"; |
| 547 const GURL secondURL = web::test::HttpServer::MakeUrl("http://secondURL"); |
| 548 const std::string secondResponse = "Test Page 2"; |
| 549 responses[firstURL] = firstResponse; |
| 550 responses[secondURL] = secondResponse; |
| 551 web::test::SetUpSimpleHttpServer(responses); |
| 552 |
| 553 [ChromeEarlGrey loadURL:firstURL]; |
| 554 [ChromeEarlGrey loadURL:secondURL]; |
| 555 |
| 556 // Execute some JavaScript in the omnibox. |
| 557 [[EarlGrey selectElementWithMatcher:chrome_test_util::omnibox()] |
| 558 performAction:grey_typeText(@"javascript:document.write('foo')")]; |
| 559 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Go")] |
| 560 performAction:grey_tap()]; |
| 561 |
| 562 id<GREYMatcher> webView = chrome_test_util::webViewContainingText("foo"); |
| 563 [[EarlGrey selectElementWithMatcher:webView] assertWithMatcher:grey_notNil()]; |
| 564 |
| 565 // Verify that the JavaScript did not affect history by going back and then |
| 566 // forward again. |
| 567 [self goBack]; |
| 568 [[EarlGrey selectElementWithMatcher:omniboxText(firstURL.GetContent())] |
| 569 assertWithMatcher:grey_notNil()]; |
| 570 [self goForward]; |
| 571 [[EarlGrey selectElementWithMatcher:omniboxText(secondURL.GetContent())] |
| 572 assertWithMatcher:grey_notNil()]; |
| 573 } |
| 574 |
| 575 // Tests that submitting a POST-based form by tapping the 'Go' button on the |
| 576 // keyboard navigates to the correct URL and the back button works as expected |
| 577 // afterwards. |
| 578 // TODO(crbug.com/670380): Enable this test on device. |
| 579 #if TARGET_IPHONE_SIMULATOR |
| 580 #define MAYBE_testBrowsingPostEntryWithKeyboard \ |
| 581 testBrowsingPostEntryWithKeyboard |
| 582 #else |
| 583 #define MAYBE_testBrowsingPostEntryWithKeyboard \ |
| 584 FLAKY_testBrowsingPostEntryWithKeyboard |
| 585 #endif |
| 586 - (void)MAYBE_testBrowsingPostEntryWithKeyboard { |
| 587 // Create map of canned responses and set up the test HTML server. |
| 588 std::map<GURL, std::string> responses; |
| 589 const GURL URL = |
| 590 web::test::HttpServer::MakeUrl("http://postEntryWithKeyboard"); |
| 591 const GURL destinationURL = web::test::HttpServer::MakeUrl("http://foo"); |
| 592 // This is a page this an input text field and a button that posts to the |
| 593 // destination. |
| 594 responses[URL] = base::StringPrintf( |
| 595 "hello!" |
| 596 "<form action='%s' method='post'>" |
| 597 "<input value='textfield' id='textfield' type='text'></label>" |
| 598 "<input type='submit'></form>", |
| 599 destinationURL.spec().c_str()); |
| 600 // This is the page that should be showing at the end of the test. |
| 601 responses[destinationURL] = "baz!"; |
| 602 web::test::SetUpSimpleHttpServer(responses); |
| 603 |
| 604 // Open the URL, focus the textfield,and submit via keyboard. |
| 605 [ChromeEarlGrey loadURL:URL]; |
| 606 [[EarlGrey selectElementWithMatcher:webViewContainingText("hello!")] |
| 607 assertWithMatcher:grey_notNil()]; |
| 608 |
| 609 web::WebState* currentWebState = chrome_test_util::GetCurrentWebState(); |
| 610 [[EarlGrey selectElementWithMatcher:web::webViewInWebState(currentWebState)] |
| 611 performAction:web::webViewTapElement(currentWebState, "textfield")]; |
| 612 |
| 613 // Disable EarlGrey's synchronization since it is blocked by opening the |
| 614 // keyboard from a web view. |
| 615 [[GREYConfiguration sharedInstance] |
| 616 setValue:@NO |
| 617 forConfigKey:kGREYConfigKeySynchronizationEnabled]; |
| 618 |
| 619 // Wait until the keyboard shows up before tapping. |
| 620 GREYCondition* condition = [GREYCondition |
| 621 conditionWithName:@"Wait for the keyboard to show up." |
| 622 block:^BOOL { |
| 623 NSError* error = nil; |
| 624 [[EarlGrey |
| 625 selectElementWithMatcher:grey_accessibilityID(@"Go")] |
| 626 assertWithMatcher:grey_notNil() |
| 627 error:&error]; |
| 628 return (error == nil); |
| 629 }]; |
| 630 GREYAssert([condition waitWithTimeout:10], |
| 631 @"No keyboard with 'Go' button showed up."); |
| 632 |
| 633 [[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"Go")] |
| 634 performAction:grey_tap()]; |
| 635 |
| 636 // Reenable synchronization now that the keyboard has been closed. |
| 637 [[GREYConfiguration sharedInstance] |
| 638 setValue:@YES |
| 639 forConfigKey:kGREYConfigKeySynchronizationEnabled]; |
| 640 |
| 641 // Verify that the browser navigates to the expected URL. |
| 642 [[EarlGrey selectElementWithMatcher:omniboxText(destinationURL.GetContent())] |
| 643 assertWithMatcher:grey_notNil()]; |
| 644 |
| 645 // Go back and verify that the browser navigates to the original URL. |
| 646 [self goBack]; |
| 647 [[EarlGrey selectElementWithMatcher:omniboxText(URL.GetContent())] |
| 648 assertWithMatcher:grey_notNil()]; |
| 649 } |
| 650 |
| 651 @end |
OLD | NEW |