Chromium Code Reviews| 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/command_line.h" | |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 6 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 7 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/browsing_data/browsing_data_helper.h" | 12 #include "chrome/browser/browsing_data/browsing_data_helper.h" |
| 10 #include "chrome/browser/browsing_data/browsing_data_remover.h" | 13 #include "chrome/browser/browsing_data/browsing_data_remover.h" |
| 11 #include "chrome/browser/google/google_util.h" | 14 #include "chrome/browser/google/google_util.h" |
| 12 #include "chrome/browser/net/url_request_mock_util.h" | 15 #include "chrome/browser/net/url_request_mock_util.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/browser_commands.h" | 18 #include "chrome/browser/ui/browser_commands.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 20 #include "chrome/common/chrome_switches.h" | |
| 17 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/test/base/in_process_browser_test.h" | 22 #include "chrome/test/base/in_process_browser_test.h" |
| 19 #include "chrome/test/base/ui_test_utils.h" | 23 #include "chrome/test/base/ui_test_utils.h" |
| 20 #include "content/public/browser/notification_service.h" | 24 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/render_view_host.h" | 25 #include "content/public/browser/render_view_host.h" |
| 22 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/browser/web_contents_observer.h" | 27 #include "content/public/browser/web_contents_observer.h" |
| 24 #include "content/public/test/browser_test_utils.h" | 28 #include "content/public/test/browser_test_utils.h" |
| 25 #include "content/public/test/test_navigation_observer.h" | 29 #include "content/public/test/test_navigation_observer.h" |
| 26 #include "content/test/net/url_request_failed_job.h" | 30 #include "content/test/net/url_request_failed_job.h" |
| 27 #include "content/test/net/url_request_mock_http_job.h" | 31 #include "content/test/net/url_request_mock_http_job.h" |
| 28 #include "net/base/net_errors.h" | 32 #include "net/base/net_errors.h" |
| 29 #include "net/base/net_util.h" | 33 #include "net/base/net_util.h" |
| 30 #include "net/http/failing_http_transaction_factory.h" | 34 #include "net/http/failing_http_transaction_factory.h" |
| 31 #include "net/http/http_cache.h" | 35 #include "net/http/http_cache.h" |
| 32 #include "net/test/spawned_test_server/spawned_test_server.h" | 36 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 33 #include "net/url_request/url_request_context.h" | 37 #include "net/url_request/url_request_context.h" |
| 34 #include "net/url_request/url_request_context_getter.h" | 38 #include "net/url_request/url_request_context_getter.h" |
| 35 #include "net/url_request/url_request_filter.h" | 39 #include "net/url_request/url_request_filter.h" |
| 40 #include "net/url_request/url_request_job.h" | |
| 36 #include "net/url_request/url_request_job_factory.h" | 41 #include "net/url_request/url_request_job_factory.h" |
| 42 #include "net/url_request/url_request_test_job.h" | |
| 43 #include "net/url_request/url_request_test_util.h" | |
| 37 | 44 |
| 38 using content::BrowserThread; | 45 using content::BrowserThread; |
| 39 using content::NavigationController; | 46 using content::NavigationController; |
| 40 using content::URLRequestFailedJob; | 47 using content::URLRequestFailedJob; |
| 48 using net::TestJobInterceptor; | |
|
mmenke
2014/03/04 17:26:25
Not used.
Elly Fong-Jones
2014/03/06 21:48:37
Done.
| |
| 49 using net::URLRequestJobFactory; | |
| 50 using net::URLRequestTestJob; | |
| 41 | 51 |
| 42 namespace { | 52 namespace { |
| 43 | 53 |
| 54 // A protocol handler that fails a configurable number of requests, then | |
| 55 // succeeds all requests after that, keeping count of failures and successes. | |
| 56 class FailFirstNRequestsProtocolHandler | |
| 57 : public URLRequestJobFactory::ProtocolHandler { | |
| 58 public: | |
| 59 FailFirstNRequestsProtocolHandler(const GURL& url, int requests_to_fail) | |
| 60 : url_(url), requests_(0), failures_(0), | |
| 61 requests_to_fail_(requests_to_fail) {} | |
| 62 virtual ~FailFirstNRequestsProtocolHandler() {} | |
| 63 | |
| 64 void AddUrlHandler() { | |
| 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 66 scoped_ptr<URLRequestJobFactory::ProtocolHandler> scoped_handler(this); | |
| 67 net::URLRequestFilter::GetInstance()->AddUrlProtocolHandler(url_, | |
| 68 scoped_handler.Pass()); | |
|
mmenke
2014/03/04 17:26:25
nit: url_, should appear on next line.
Elly Fong-Jones
2014/03/06 21:48:37
Done.
| |
| 69 } | |
| 70 | |
| 71 virtual net::URLRequestJob* MaybeCreateJob( | |
| 72 net::URLRequest* request, | |
| 73 net::NetworkDelegate* network_delegate) OVERRIDE const { | |
| 74 DCHECK_EQ(request->url(), url_); | |
|
mmenke
2014/03/04 17:26:25
nit: Think url_ should go first, since that's clo
Elly Fong-Jones
2014/03/06 21:48:37
Done.
| |
| 75 requests_++; | |
| 76 if (failures_ < requests_to_fail_) { | |
| 77 failures_++; | |
| 78 return new URLRequestFailedJob(request, | |
| 79 network_delegate, | |
| 80 net::ERR_CONNECTION_RESET); | |
|
mmenke
2014/03/04 17:26:25
May want to comment somewhere that this error won'
Elly Fong-Jones
2014/03/06 21:48:37
Done.
| |
| 81 } else { | |
| 82 return new URLRequestTestJob(request, network_delegate, | |
| 83 URLRequestTestJob::test_headers(), | |
| 84 URLRequestTestJob::test_data_1(), | |
| 85 true); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 int requests() const { return requests_; } | |
| 90 int failures() const { return failures_; } | |
| 91 | |
| 92 private: | |
| 93 const GURL url_; | |
| 94 // these are mutable because MaybeCreateJob is const but we want this state | |
| 95 // for testing. | |
|
mmenke
2014/03/04 17:26:25
nit: Capitalize "these"
Elly Fong-Jones
2014/03/06 21:48:37
Done.
| |
| 96 mutable int requests_; | |
| 97 mutable int failures_; | |
| 98 int requests_to_fail_; | |
| 99 }; | |
| 100 | |
| 44 class ErrorPageTest : public InProcessBrowserTest { | 101 class ErrorPageTest : public InProcessBrowserTest { |
| 45 public: | 102 public: |
| 46 enum HistoryNavigationDirection { | 103 enum HistoryNavigationDirection { |
| 47 HISTORY_NAVIGATE_BACK, | 104 HISTORY_NAVIGATE_BACK, |
| 48 HISTORY_NAVIGATE_FORWARD, | 105 HISTORY_NAVIGATE_FORWARD, |
| 49 }; | 106 }; |
| 50 | 107 |
| 51 // Navigates the active tab to a mock url created for the file at |file_path|. | 108 // Navigates the active tab to a mock url created for the file at |file_path|. |
| 52 void NavigateToFileURL(const base::FilePath::StringType& file_path) { | 109 void NavigateToFileURL(const base::FilePath::StringType& file_path) { |
| 53 ui_test_utils::NavigateToURL( | 110 ui_test_utils::NavigateToURL( |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 // that there is no cached copy. | 492 // that there is no cached copy. |
| 436 BrowsingDataRemover* remover = | 493 BrowsingDataRemover* remover = |
| 437 BrowsingDataRemover::CreateForUnboundedRange(browser()->profile()); | 494 BrowsingDataRemover::CreateForUnboundedRange(browser()->profile()); |
| 438 remover->Remove(BrowsingDataRemover::REMOVE_CACHE, | 495 remover->Remove(BrowsingDataRemover::REMOVE_CACHE, |
| 439 BrowsingDataHelper::UNPROTECTED_WEB); | 496 BrowsingDataHelper::UNPROTECTED_WEB); |
| 440 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 497 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 441 browser(), test_url, 1); | 498 browser(), test_url, 1); |
| 442 EXPECT_TRUE(ProbeStaleCopyValue(false)); | 499 EXPECT_TRUE(ProbeStaleCopyValue(false)); |
| 443 } | 500 } |
| 444 | 501 |
| 502 class ErrorPageAutoReloadTest : public InProcessBrowserTest { | |
| 503 public: | |
| 504 virtual void SetUpCommandLine(CommandLine* command_line) { | |
|
mmenke
2014/03/04 17:26:25
OVERRIDE
Elly Fong-Jones
2014/03/06 21:48:37
Done.
| |
| 505 command_line->AppendSwitch(switches::kEnableOfflineAutoReload); | |
| 506 } | |
| 507 | |
| 508 void InstallProtocolHandler(const GURL& url, int requests_to_fail) { | |
| 509 protocol_handler_ = new FailFirstNRequestsProtocolHandler(url, | |
| 510 requests_to_fail); | |
|
mmenke
2014/03/04 17:26:25
nit: Move "url" to next line.
Elly Fong-Jones
2014/03/06 21:48:37
Done.
| |
| 511 // Tests don't need to wait for this task to complete before using the | |
| 512 // filter; any requests that might be affected by it will end up in the IO | |
| 513 // thread's message loop after this posted task anyway. | |
| 514 BrowserThread::PostTask( | |
| 515 BrowserThread::IO, FROM_HERE, | |
| 516 base::Bind(&ErrorPageAutoReloadTest::AddFilters, | |
| 517 base::Unretained(this))); | |
|
mmenke
2014/03/04 17:26:25
nit: Align base with the &
Elly Fong-Jones
2014/03/06 21:48:37
Done.
| |
| 518 } | |
| 519 | |
| 520 void NavigateToURLAndWaitForTitle(const GURL& url, | |
| 521 const std::string& expected_title, | |
| 522 int num_navigations) { | |
| 523 content::TitleWatcher title_watcher( | |
| 524 browser()->tab_strip_model()->GetActiveWebContents(), | |
| 525 base::ASCIIToUTF16(expected_title)); | |
| 526 | |
| 527 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | |
| 528 browser(), url, num_navigations); | |
| 529 | |
| 530 EXPECT_EQ(base::ASCIIToUTF16(expected_title), | |
| 531 title_watcher.WaitAndGetTitle()); | |
| 532 } | |
| 533 | |
| 534 FailFirstNRequestsProtocolHandler* protocol_handler() { | |
| 535 return protocol_handler_; | |
| 536 } | |
| 537 | |
| 538 private: | |
| 539 void AddFilters() { | |
| 540 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 541 protocol_handler_->AddUrlHandler(); | |
| 542 } | |
| 543 | |
| 544 void RemoveFilters() { | |
| 545 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 546 net::URLRequestFilter::GetInstance()->ClearHandlers(); | |
| 547 } | |
|
mmenke
2014/03/04 17:26:25
This is no longer called anywhere, so should get r
Elly Fong-Jones
2014/03/06 21:48:37
Done.
| |
| 548 | |
| 549 FailFirstNRequestsProtocolHandler* protocol_handler_; | |
| 550 }; | |
| 551 | |
| 552 IN_PROC_BROWSER_TEST_F(ErrorPageAutoReloadTest, AutoReload) { | |
| 553 GURL test_url("http://error.page.auto.reload"); | |
| 554 const int kRequestsToFail = 2; | |
| 555 InstallProtocolHandler(test_url, kRequestsToFail); | |
| 556 NavigateToURLAndWaitForTitle(test_url, "Test One", kRequestsToFail + 1); | |
| 557 // Note that the protocol handler updates these variables on the IO thread, | |
| 558 // but we read them on the main thread. The requests have to be created (on | |
| 559 // the IO thread) before NavigateToURLAndWaitForTitle returns or this becomes | |
| 560 // racey. | |
|
mmenke
2014/03/04 17:26:25
nit: Don't use "we" in comments, due to ambiguity
Elly Fong-Jones
2014/03/06 21:48:37
Done.
| |
| 561 EXPECT_EQ(kRequestsToFail, protocol_handler()->failures()); | |
| 562 EXPECT_EQ(kRequestsToFail + 1, protocol_handler()->requests()); | |
| 563 } | |
| 564 | |
| 445 // Returns Javascript code that executes plain text search for the page. | 565 // Returns Javascript code that executes plain text search for the page. |
| 446 // Pass into content::ExecuteScriptAndExtractBool as |script| parameter. | 566 // Pass into content::ExecuteScriptAndExtractBool as |script| parameter. |
| 447 std::string GetTextContentContainsStringScript( | 567 std::string GetTextContentContainsStringScript( |
| 448 const std::string& value_to_search) { | 568 const std::string& value_to_search) { |
| 449 return base::StringPrintf( | 569 return base::StringPrintf( |
| 450 "var textContent = document.body.textContent;" | 570 "var textContent = document.body.textContent;" |
| 451 "var hasError = textContent.indexOf('%s') >= 0;" | 571 "var hasError = textContent.indexOf('%s') >= 0;" |
| 452 "domAutomationController.send(hasError);", | 572 "domAutomationController.send(hasError);", |
| 453 value_to_search.c_str()); | 573 value_to_search.c_str()); |
| 454 } | 574 } |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 620 | 740 |
| 621 bool result = false; | 741 bool result = false; |
| 622 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( | 742 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 623 browser()->tab_strip_model()->GetActiveWebContents(), | 743 browser()->tab_strip_model()->GetActiveWebContents(), |
| 624 GetTextContentContainsStringScript(kHostnameJSUnicode), | 744 GetTextContentContainsStringScript(kHostnameJSUnicode), |
| 625 &result)); | 745 &result)); |
| 626 EXPECT_TRUE(result); | 746 EXPECT_TRUE(result); |
| 627 } | 747 } |
| 628 | 748 |
| 629 } // namespace | 749 } // namespace |
| OLD | NEW |