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