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" |
6 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" |
7 #include "base/path_service.h" | 10 #include "base/path_service.h" |
8 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
9 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
10 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
11 #include "chrome/browser/browsing_data/browsing_data_helper.h" | 14 #include "chrome/browser/browsing_data/browsing_data_helper.h" |
12 #include "chrome/browser/browsing_data/browsing_data_remover.h" | 15 #include "chrome/browser/browsing_data/browsing_data_remover.h" |
13 #include "chrome/browser/google/google_util.h" | 16 #include "chrome/browser/google/google_util.h" |
14 #include "chrome/browser/net/url_request_mock_util.h" | 17 #include "chrome/browser/net/url_request_mock_util.h" |
15 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
17 #include "chrome/browser/ui/browser_commands.h" | 20 #include "chrome/browser/ui/browser_commands.h" |
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
19 #include "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
| 23 #include "chrome/common/chrome_switches.h" |
20 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
21 #include "chrome/test/base/in_process_browser_test.h" | 25 #include "chrome/test/base/in_process_browser_test.h" |
22 #include "chrome/test/base/ui_test_utils.h" | 26 #include "chrome/test/base/ui_test_utils.h" |
| 27 #include "content/public/browser/browser_thread.h" |
23 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
24 #include "content/public/browser/render_frame_host.h" | 29 #include "content/public/browser/render_frame_host.h" |
25 #include "content/public/browser/render_view_host.h" | 30 #include "content/public/browser/render_view_host.h" |
26 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
27 #include "content/public/browser/web_contents_observer.h" | 32 #include "content/public/browser/web_contents_observer.h" |
28 #include "content/public/test/browser_test_utils.h" | 33 #include "content/public/test/browser_test_utils.h" |
29 #include "content/public/test/test_navigation_observer.h" | 34 #include "content/public/test/test_navigation_observer.h" |
30 #include "content/test/net/url_request_failed_job.h" | 35 #include "content/test/net/url_request_failed_job.h" |
31 #include "content/test/net/url_request_mock_http_job.h" | 36 #include "content/test/net/url_request_mock_http_job.h" |
32 #include "net/base/net_errors.h" | 37 #include "net/base/net_errors.h" |
33 #include "net/base/net_util.h" | 38 #include "net/base/net_util.h" |
34 #include "net/http/failing_http_transaction_factory.h" | 39 #include "net/http/failing_http_transaction_factory.h" |
35 #include "net/http/http_cache.h" | 40 #include "net/http/http_cache.h" |
36 #include "net/test/spawned_test_server/spawned_test_server.h" | 41 #include "net/test/spawned_test_server/spawned_test_server.h" |
37 #include "net/url_request/url_request_context.h" | 42 #include "net/url_request/url_request_context.h" |
38 #include "net/url_request/url_request_context_getter.h" | 43 #include "net/url_request/url_request_context_getter.h" |
39 #include "net/url_request/url_request_filter.h" | 44 #include "net/url_request/url_request_filter.h" |
| 45 #include "net/url_request/url_request_job.h" |
40 #include "net/url_request/url_request_job_factory.h" | 46 #include "net/url_request/url_request_job_factory.h" |
| 47 #include "net/url_request/url_request_test_job.h" |
| 48 #include "net/url_request/url_request_test_util.h" |
41 | 49 |
42 using content::BrowserThread; | 50 using content::BrowserThread; |
43 using content::NavigationController; | 51 using content::NavigationController; |
44 using content::URLRequestFailedJob; | 52 using content::URLRequestFailedJob; |
| 53 using net::URLRequestJobFactory; |
| 54 using net::URLRequestTestJob; |
45 | 55 |
46 namespace { | 56 namespace { |
47 | 57 |
48 // Returns true if |text| is displayed on the page |browser| is currently | 58 // Returns true if |text| is displayed on the page |browser| is currently |
49 // displaying. Uses "innerText", so will miss hidden text, and whitespace | 59 // displaying. Uses "innerText", so will miss hidden text, and whitespace |
50 // space handling may be weird. | 60 // space handling may be weird. |
51 bool WARN_UNUSED_RESULT IsDisplayingText(Browser* browser, | 61 bool WARN_UNUSED_RESULT IsDisplayingText(Browser* browser, |
52 const std::string& text) { | 62 const std::string& text) { |
53 std::string command = base::StringPrintf( | 63 std::string command = base::StringPrintf( |
54 "var textContent = document.body.innerText;" | 64 "var textContent = document.body.innerText;" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 // Check that the search box is populated correctly. | 129 // Check that the search box is populated correctly. |
120 bool search_box_populated = false; | 130 bool search_box_populated = false; |
121 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | 131 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
122 browser->tab_strip_model()->GetActiveWebContents(), | 132 browser->tab_strip_model()->GetActiveWebContents(), |
123 "var searchText = document.getElementById('search-box').value;" | 133 "var searchText = document.getElementById('search-box').value;" |
124 "domAutomationController.send(searchText == 'search query');", | 134 "domAutomationController.send(searchText == 'search query');", |
125 &search_box_populated)); | 135 &search_box_populated)); |
126 EXPECT_TRUE(search_box_populated); | 136 EXPECT_TRUE(search_box_populated); |
127 } | 137 } |
128 | 138 |
| 139 // A protocol handler that fails a configurable number of requests, then |
| 140 // succeeds all requests after that, keeping count of failures and successes. |
| 141 class FailFirstNRequestsProtocolHandler |
| 142 : public URLRequestJobFactory::ProtocolHandler { |
| 143 public: |
| 144 FailFirstNRequestsProtocolHandler(const GURL& url, int requests_to_fail) |
| 145 : url_(url), requests_(0), failures_(0), |
| 146 requests_to_fail_(requests_to_fail) {} |
| 147 virtual ~FailFirstNRequestsProtocolHandler() {} |
| 148 |
| 149 // This method deliberately violates pointer ownership rules: |
| 150 // AddUrlProtocolHandler() takes a scoped_ptr, taking ownership of the |
| 151 // supplied ProtocolHandler (i.e., |this|), but also having the caller retain |
| 152 // a pointer to |this| so the caller can use the requests() and failures() |
| 153 // accessors. |
| 154 void AddUrlHandler() { |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 156 scoped_ptr<URLRequestJobFactory::ProtocolHandler> scoped_handler(this); |
| 157 net::URLRequestFilter::GetInstance()->AddUrlProtocolHandler( |
| 158 url_, |
| 159 scoped_handler.Pass()); |
| 160 } |
| 161 |
| 162 // net::URLRequestJobFactory::ProtocolHandler implementation |
| 163 virtual net::URLRequestJob* MaybeCreateJob( |
| 164 net::URLRequest* request, |
| 165 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 166 DCHECK_EQ(url_, request->url()); |
| 167 requests_++; |
| 168 if (failures_ < requests_to_fail_) { |
| 169 failures_++; |
| 170 // Note: net::ERR_CONNECTION_RESET does not summon the Link Doctor; see |
| 171 // NetErrorHelperCore::GetErrorPageURL. |
| 172 return new URLRequestFailedJob(request, |
| 173 network_delegate, |
| 174 net::ERR_CONNECTION_RESET); |
| 175 } else { |
| 176 return new URLRequestTestJob(request, network_delegate, |
| 177 URLRequestTestJob::test_headers(), |
| 178 URLRequestTestJob::test_data_1(), |
| 179 true); |
| 180 } |
| 181 } |
| 182 |
| 183 int requests() const { return requests_; } |
| 184 int failures() const { return failures_; } |
| 185 |
| 186 private: |
| 187 const GURL url_; |
| 188 // These are mutable because MaybeCreateJob is const but we want this state |
| 189 // for testing. |
| 190 mutable int requests_; |
| 191 mutable int failures_; |
| 192 int requests_to_fail_; |
| 193 }; |
| 194 |
129 class ErrorPageTest : public InProcessBrowserTest { | 195 class ErrorPageTest : public InProcessBrowserTest { |
130 public: | 196 public: |
131 enum HistoryNavigationDirection { | 197 enum HistoryNavigationDirection { |
132 HISTORY_NAVIGATE_BACK, | 198 HISTORY_NAVIGATE_BACK, |
133 HISTORY_NAVIGATE_FORWARD, | 199 HISTORY_NAVIGATE_FORWARD, |
134 }; | 200 }; |
135 | 201 |
136 // Navigates the active tab to a mock url created for the file at |file_path|. | 202 // Navigates the active tab to a mock url created for the file at |file_path|. |
137 void NavigateToFileURL(const base::FilePath::StringType& file_path) { | 203 void NavigateToFileURL(const base::FilePath::StringType& file_path) { |
138 ui_test_utils::NavigateToURL( | 204 ui_test_utils::NavigateToURL( |
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 // that there is no cached copy. | 666 // that there is no cached copy. |
601 BrowsingDataRemover* remover = | 667 BrowsingDataRemover* remover = |
602 BrowsingDataRemover::CreateForUnboundedRange(browser()->profile()); | 668 BrowsingDataRemover::CreateForUnboundedRange(browser()->profile()); |
603 remover->Remove(BrowsingDataRemover::REMOVE_CACHE, | 669 remover->Remove(BrowsingDataRemover::REMOVE_CACHE, |
604 BrowsingDataHelper::UNPROTECTED_WEB); | 670 BrowsingDataHelper::UNPROTECTED_WEB); |
605 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 671 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
606 browser(), test_url, 1); | 672 browser(), test_url, 1); |
607 EXPECT_TRUE(ProbeStaleCopyValue(false)); | 673 EXPECT_TRUE(ProbeStaleCopyValue(false)); |
608 } | 674 } |
609 | 675 |
| 676 class ErrorPageAutoReloadTest : public InProcessBrowserTest { |
| 677 public: |
| 678 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 679 command_line->AppendSwitch(switches::kEnableOfflineAutoReload); |
| 680 } |
| 681 |
| 682 void InstallProtocolHandler(const GURL& url, int requests_to_fail) { |
| 683 protocol_handler_ = new FailFirstNRequestsProtocolHandler( |
| 684 url, |
| 685 requests_to_fail); |
| 686 // Tests don't need to wait for this task to complete before using the |
| 687 // filter; any requests that might be affected by it will end up in the IO |
| 688 // thread's message loop after this posted task anyway. |
| 689 BrowserThread::PostTask( |
| 690 BrowserThread::IO, FROM_HERE, |
| 691 base::Bind(&ErrorPageAutoReloadTest::AddFilters, |
| 692 base::Unretained(this))); |
| 693 } |
| 694 |
| 695 void NavigateToURLAndWaitForTitle(const GURL& url, |
| 696 const std::string& expected_title, |
| 697 int num_navigations) { |
| 698 content::TitleWatcher title_watcher( |
| 699 browser()->tab_strip_model()->GetActiveWebContents(), |
| 700 base::ASCIIToUTF16(expected_title)); |
| 701 |
| 702 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 703 browser(), url, num_navigations); |
| 704 |
| 705 EXPECT_EQ(base::ASCIIToUTF16(expected_title), |
| 706 title_watcher.WaitAndGetTitle()); |
| 707 } |
| 708 |
| 709 FailFirstNRequestsProtocolHandler* protocol_handler() { |
| 710 return protocol_handler_; |
| 711 } |
| 712 |
| 713 private: |
| 714 void AddFilters() { |
| 715 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 716 // Note: in theory, AddUrlHandler gives ownership of |protocol_handler_| to |
| 717 // URLRequestFilter. As soon as anything calls |
| 718 // URLRequestFilter::ClearHandlers(), |protocol_handler_| can become |
| 719 // invalid. |
| 720 protocol_handler_->AddUrlHandler(); |
| 721 } |
| 722 |
| 723 FailFirstNRequestsProtocolHandler* protocol_handler_; |
| 724 }; |
| 725 |
| 726 IN_PROC_BROWSER_TEST_F(ErrorPageAutoReloadTest, AutoReload) { |
| 727 GURL test_url("http://error.page.auto.reload"); |
| 728 const int kRequestsToFail = 2; |
| 729 InstallProtocolHandler(test_url, kRequestsToFail); |
| 730 NavigateToURLAndWaitForTitle(test_url, "Test One", kRequestsToFail + 1); |
| 731 // Note that the protocol handler updates these variables on the IO thread, |
| 732 // but this function reads them on the main thread. The requests have to be |
| 733 // created (on the IO thread) before NavigateToURLAndWaitForTitle returns or |
| 734 // this becomes racey. |
| 735 EXPECT_EQ(kRequestsToFail, protocol_handler()->failures()); |
| 736 EXPECT_EQ(kRequestsToFail + 1, protocol_handler()->requests()); |
| 737 } |
| 738 |
610 // Protocol handler that fails all requests with net::ERR_ADDRESS_UNREACHABLE. | 739 // Protocol handler that fails all requests with net::ERR_ADDRESS_UNREACHABLE. |
611 class AddressUnreachableProtocolHandler | 740 class AddressUnreachableProtocolHandler |
612 : public net::URLRequestJobFactory::ProtocolHandler { | 741 : public net::URLRequestJobFactory::ProtocolHandler { |
613 public: | 742 public: |
614 AddressUnreachableProtocolHandler() {} | 743 AddressUnreachableProtocolHandler() {} |
615 virtual ~AddressUnreachableProtocolHandler() {} | 744 virtual ~AddressUnreachableProtocolHandler() {} |
616 | 745 |
617 // net::URLRequestJobFactory::ProtocolHandler: | 746 // net::URLRequestJobFactory::ProtocolHandler: |
618 virtual net::URLRequestJob* MaybeCreateJob( | 747 virtual net::URLRequestJob* MaybeCreateJob( |
619 net::URLRequest* request, | 748 net::URLRequest* request, |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 browser(), | 892 browser(), |
764 URLRequestFailedJob::GetMockHttpUrlForHostname(net::ERR_UNSAFE_PORT, | 893 URLRequestFailedJob::GetMockHttpUrlForHostname(net::ERR_UNSAFE_PORT, |
765 kHostname), | 894 kHostname), |
766 1); | 895 1); |
767 | 896 |
768 ToggleHelpBox(browser()); | 897 ToggleHelpBox(browser()); |
769 EXPECT_TRUE(IsDisplayingText(browser(), kHostnameJSUnicode)); | 898 EXPECT_TRUE(IsDisplayingText(browser(), kHostnameJSUnicode)); |
770 } | 899 } |
771 | 900 |
772 } // namespace | 901 } // namespace |
OLD | NEW |