OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/prefs/pref_service.h" |
| 7 #include "base/strings/stringprintf.h" |
6 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
7 #include "chrome/browser/google/google_util.h" | 9 #include "chrome/browser/google/google_util.h" |
8 #include "chrome/browser/net/url_request_mock_util.h" | 10 #include "chrome/browser/net/url_request_mock_util.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
10 #include "chrome/browser/ui/browser_commands.h" | 13 #include "chrome/browser/ui/browser_commands.h" |
11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #include "chrome/common/pref_names.h" |
12 #include "chrome/test/base/in_process_browser_test.h" | 16 #include "chrome/test/base/in_process_browser_test.h" |
13 #include "chrome/test/base/ui_test_utils.h" | 17 #include "chrome/test/base/ui_test_utils.h" |
14 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
15 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
16 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
17 #include "content/public/browser/web_contents_observer.h" | 21 #include "content/public/browser/web_contents_observer.h" |
18 #include "content/public/test/browser_test_utils.h" | 22 #include "content/public/test/browser_test_utils.h" |
19 #include "content/public/test/test_navigation_observer.h" | 23 #include "content/public/test/test_navigation_observer.h" |
20 #include "content/test/net/url_request_failed_job.h" | 24 #include "content/test/net/url_request_failed_job.h" |
21 #include "content/test/net/url_request_mock_http_job.h" | 25 #include "content/test/net/url_request_mock_http_job.h" |
22 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
| 27 #include "net/base/net_util.h" |
23 #include "net/url_request/url_request_filter.h" | 28 #include "net/url_request/url_request_filter.h" |
24 #include "net/url_request/url_request_job_factory.h" | 29 #include "net/url_request/url_request_job_factory.h" |
25 | 30 |
26 using content::BrowserThread; | 31 using content::BrowserThread; |
27 using content::NavigationController; | 32 using content::NavigationController; |
28 using content::URLRequestFailedJob; | 33 using content::URLRequestFailedJob; |
29 | 34 |
30 namespace { | 35 namespace { |
31 | 36 |
32 class ErrorPageTest : public InProcessBrowserTest { | 37 class ErrorPageTest : public InProcessBrowserTest { |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 | 347 |
343 // Checks that the Link Doctor is not loaded when we receive an actual 404 page. | 348 // Checks that the Link Doctor is not loaded when we receive an actual 404 page. |
344 IN_PROC_BROWSER_TEST_F(ErrorPageTest, Page404) { | 349 IN_PROC_BROWSER_TEST_F(ErrorPageTest, Page404) { |
345 NavigateToURLAndWaitForTitle( | 350 NavigateToURLAndWaitForTitle( |
346 content::URLRequestMockHTTPJob::GetMockUrl( | 351 content::URLRequestMockHTTPJob::GetMockUrl( |
347 base::FilePath(FILE_PATH_LITERAL("page404.html"))), | 352 base::FilePath(FILE_PATH_LITERAL("page404.html"))), |
348 "SUCCESS", | 353 "SUCCESS", |
349 1); | 354 1); |
350 } | 355 } |
351 | 356 |
| 357 // Returns Javascript code that executes plain text search for the page. |
| 358 // Pass into content::ExecuteScriptAndExtractBool as |script| parameter. |
| 359 std::string GetTextContentContainsStringScript( |
| 360 const std::string& value_to_search) { |
| 361 return base::StringPrintf( |
| 362 "var textContent = document.body.textContent;" |
| 363 "var hasError = textContent.indexOf('%s') >= 0;" |
| 364 "domAutomationController.send(hasError);", |
| 365 value_to_search.c_str()); |
| 366 } |
| 367 |
352 // Protocol handler that fails all requests with net::ERR_ADDRESS_UNREACHABLE. | 368 // Protocol handler that fails all requests with net::ERR_ADDRESS_UNREACHABLE. |
353 class AddressUnreachableProtocolHandler | 369 class AddressUnreachableProtocolHandler |
354 : public net::URLRequestJobFactory::ProtocolHandler { | 370 : public net::URLRequestJobFactory::ProtocolHandler { |
355 public: | 371 public: |
356 AddressUnreachableProtocolHandler() {} | 372 AddressUnreachableProtocolHandler() {} |
357 virtual ~AddressUnreachableProtocolHandler() {} | 373 virtual ~AddressUnreachableProtocolHandler() {} |
358 | 374 |
359 // net::URLRequestJobFactory::ProtocolHandler: | 375 // net::URLRequestJobFactory::ProtocolHandler: |
360 virtual net::URLRequestJob* MaybeCreateJob( | 376 virtual net::URLRequestJob* MaybeCreateJob( |
361 net::URLRequest* request, | 377 net::URLRequest* request, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 433 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
418 browser(), | 434 browser(), |
419 URLRequestFailedJob::GetMockHttpUrl(net::ERR_NAME_NOT_RESOLVED), | 435 URLRequestFailedJob::GetMockHttpUrl(net::ERR_NAME_NOT_RESOLVED), |
420 2); | 436 2); |
421 | 437 |
422 // Verify that the expected error page is being displayed. Do this by making | 438 // Verify that the expected error page is being displayed. Do this by making |
423 // sure the original error code (ERR_NAME_NOT_RESOLVED) is displayed. | 439 // sure the original error code (ERR_NAME_NOT_RESOLVED) is displayed. |
424 bool result = false; | 440 bool result = false; |
425 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | 441 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
426 browser()->tab_strip_model()->GetActiveWebContents(), | 442 browser()->tab_strip_model()->GetActiveWebContents(), |
427 "var textContent = document.body.textContent;" | 443 GetTextContentContainsStringScript("ERR_NAME_NOT_RESOLVED"), |
428 "var hasError = textContent.indexOf('ERR_NAME_NOT_RESOLVED') >= 0;" | 444 &result)); |
429 "domAutomationController.send(hasError);", | 445 EXPECT_TRUE(result); |
| 446 } |
| 447 |
| 448 // A test fixture that simulates failing requests for an IDN domain name. |
| 449 class ErrorPageForIDNTest : public InProcessBrowserTest { |
| 450 public: |
| 451 // Target hostname in different forms. |
| 452 static const char kHostname[]; |
| 453 static const char kHostnameJSUnicode[]; |
| 454 |
| 455 // InProcessBrowserTest: |
| 456 virtual void SetUpOnMainThread() OVERRIDE { |
| 457 // Clear AcceptLanguages to force punycode decoding. |
| 458 browser()->profile()->GetPrefs()->SetString(prefs::kAcceptLanguages, |
| 459 std::string()); |
| 460 BrowserThread::PostTask( |
| 461 BrowserThread::IO, FROM_HERE, |
| 462 base::Bind(&ErrorPageForIDNTest::AddFilters)); |
| 463 } |
| 464 |
| 465 virtual void CleanUpOnMainThread() OVERRIDE { |
| 466 BrowserThread::PostTask( |
| 467 BrowserThread::IO, FROM_HERE, |
| 468 base::Bind(&ErrorPageForIDNTest::RemoveFilters)); |
| 469 } |
| 470 |
| 471 private: |
| 472 static void AddFilters() { |
| 473 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 474 content::URLRequestFailedJob::AddUrlHandlerForHostname(kHostname); |
| 475 } |
| 476 |
| 477 static void RemoveFilters() { |
| 478 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 479 net::URLRequestFilter::GetInstance()->ClearHandlers(); |
| 480 } |
| 481 }; |
| 482 |
| 483 const char ErrorPageForIDNTest::kHostname[] = |
| 484 "xn--d1abbgf6aiiy.xn--p1ai"; |
| 485 const char ErrorPageForIDNTest::kHostnameJSUnicode[] = |
| 486 "\\u043f\\u0440\\u0435\\u0437\\u0438\\u0434\\u0435\\u043d\\u0442." |
| 487 "\\u0440\\u0444"; |
| 488 |
| 489 // Make sure error page shows correct unicode for IDN. |
| 490 IN_PROC_BROWSER_TEST_F(ErrorPageForIDNTest, IDN) { |
| 491 // ERR_UNSAFE_PORT will not trigger the link doctor. |
| 492 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 493 browser(), |
| 494 URLRequestFailedJob::GetMockHttpUrlForHostname(net::ERR_UNSAFE_PORT, |
| 495 kHostname), |
| 496 1); |
| 497 |
| 498 bool result = false; |
| 499 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 500 browser()->tab_strip_model()->GetActiveWebContents(), |
| 501 GetTextContentContainsStringScript(kHostnameJSUnicode), |
430 &result)); | 502 &result)); |
431 EXPECT_TRUE(result); | 503 EXPECT_TRUE(result); |
432 } | 504 } |
433 | 505 |
434 } // namespace | 506 } // namespace |
OLD | NEW |