Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: chrome/browser/errorpage_browsertest.cc

Issue 136203009: Support auto-reload on errors. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: More testing. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
29 #include "content/public/browser/thread.h"
25 #include "content/public/browser/web_contents.h" 30 #include "content/public/browser/web_contents.h"
26 #include "content/public/browser/web_contents_observer.h" 31 #include "content/public/browser/web_contents_observer.h"
27 #include "content/public/test/browser_test_utils.h" 32 #include "content/public/test/browser_test_utils.h"
28 #include "content/public/test/test_navigation_observer.h" 33 #include "content/public/test/test_navigation_observer.h"
29 #include "content/test/net/url_request_failed_job.h" 34 #include "content/test/net/url_request_failed_job.h"
30 #include "content/test/net/url_request_mock_http_job.h" 35 #include "content/test/net/url_request_mock_http_job.h"
31 #include "net/base/net_errors.h" 36 #include "net/base/net_errors.h"
32 #include "net/base/net_util.h" 37 #include "net/base/net_util.h"
33 #include "net/http/failing_http_transaction_factory.h" 38 #include "net/http/failing_http_transaction_factory.h"
34 #include "net/http/http_cache.h" 39 #include "net/http/http_cache.h"
35 #include "net/test/spawned_test_server/spawned_test_server.h" 40 #include "net/test/spawned_test_server/spawned_test_server.h"
36 #include "net/url_request/url_request_context.h" 41 #include "net/url_request/url_request_context.h"
37 #include "net/url_request/url_request_context_getter.h" 42 #include "net/url_request/url_request_context_getter.h"
38 #include "net/url_request/url_request_filter.h" 43 #include "net/url_request/url_request_filter.h"
44 #include "net/url_request/url_request_job.h"
39 #include "net/url_request/url_request_job_factory.h" 45 #include "net/url_request/url_request_job_factory.h"
46 #include "net/url_request/url_request_test_job.h"
47 #include "net/url_request/url_request_test_util.h"
40 48
41 using content::BrowserThread; 49 using content::BrowserThread;
42 using content::NavigationController; 50 using content::NavigationController;
43 using content::URLRequestFailedJob; 51 using content::URLRequestFailedJob;
52 using net::URLRequestJobFactory;
53 using net::URLRequestTestJob;
44 54
45 namespace { 55 namespace {
46 56
47 // Returns true if |text| is displayed on the page |browser| is currently 57 // Returns true if |text| is displayed on the page |browser| is currently
48 // displaying. Uses "innerText", so will miss hidden text, and whitespace 58 // displaying. Uses "innerText", so will miss hidden text, and whitespace
49 // space handling may be weird. 59 // space handling may be weird.
50 bool WARN_UNUSED_RESULT IsDisplayingText(Browser* browser, 60 bool WARN_UNUSED_RESULT IsDisplayingText(Browser* browser,
51 const std::string& text) { 61 const std::string& text) {
52 std::string command = base::StringPrintf( 62 std::string command = base::StringPrintf(
53 "var textContent = document.body.innerText;" 63 "var textContent = document.body.innerText;"
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 // Check that the search box is populated correctly. 128 // Check that the search box is populated correctly.
119 bool search_box_populated = false; 129 bool search_box_populated = false;
120 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( 130 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
121 browser->tab_strip_model()->GetActiveWebContents(), 131 browser->tab_strip_model()->GetActiveWebContents(),
122 "var searchText = document.getElementById('search-box').value;" 132 "var searchText = document.getElementById('search-box').value;"
123 "domAutomationController.send(searchText == 'search query');", 133 "domAutomationController.send(searchText == 'search query');",
124 &search_box_populated)); 134 &search_box_populated));
125 EXPECT_TRUE(search_box_populated); 135 EXPECT_TRUE(search_box_populated);
126 } 136 }
127 137
138 // A protocol handler that fails a configurable number of requests, then
139 // succeeds all requests after that, keeping count of failures and successes.
140 class FailFirstNRequestsProtocolHandler
141 : public URLRequestJobFactory::ProtocolHandler {
142 public:
143 FailFirstNRequestsProtocolHandler(const GURL& url, int requests_to_fail)
144 : url_(url), requests_(0), failures_(0),
145 requests_to_fail_(requests_to_fail) {}
146 virtual ~FailFirstNRequestsProtocolHandler() {}
147
148 // This method deliberately violates pointer ownership rules:
149 // AddUrlProtocolHandler() takes a scoped_ptr, taking ownership of the
150 // supplied ProtocolHandler (i.e., |this|), but also having the caller retain
151 // a pointer to |this| so the caller can use the requests() and failures()
152 // accessors.
153 void AddUrlHandler() {
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
155 scoped_ptr<URLRequestJobFactory::ProtocolHandler> scoped_handler(this);
156 net::URLRequestFilter::GetInstance()->AddUrlProtocolHandler(
157 url_,
158 scoped_handler.Pass());
159 }
160
161 // net::URLRequestJobFactory::ProtocolHandler implementation
162 virtual net::URLRequestJob* MaybeCreateJob(
163 net::URLRequest* request,
164 net::NetworkDelegate* network_delegate) const OVERRIDE {
165 DCHECK_EQ(url_, request->url());
166 requests_++;
167 if (failures_ < requests_to_fail_) {
168 failures_++;
169 // Note: net::ERR_CONNECTION_RESET does not summon the Link Doctor; see
170 // NetErrorHelperCore::GetErrorPageURL.
171 return new URLRequestFailedJob(request,
172 network_delegate,
173 net::ERR_CONNECTION_RESET);
174 } else {
175 return new URLRequestTestJob(request, network_delegate,
176 URLRequestTestJob::test_headers(),
177 URLRequestTestJob::test_data_1(),
178 true);
179 }
180 }
181
182 int requests() const { return requests_; }
183 int failures() const { return failures_; }
184
185 private:
186 const GURL url_;
187 // These are mutable because MaybeCreateJob is const but we want this state
188 // for testing.
189 mutable int requests_;
190 mutable int failures_;
191 int requests_to_fail_;
192 };
193
128 class ErrorPageTest : public InProcessBrowserTest { 194 class ErrorPageTest : public InProcessBrowserTest {
129 public: 195 public:
130 enum HistoryNavigationDirection { 196 enum HistoryNavigationDirection {
131 HISTORY_NAVIGATE_BACK, 197 HISTORY_NAVIGATE_BACK,
132 HISTORY_NAVIGATE_FORWARD, 198 HISTORY_NAVIGATE_FORWARD,
133 }; 199 };
134 200
135 // Navigates the active tab to a mock url created for the file at |file_path|. 201 // Navigates the active tab to a mock url created for the file at |file_path|.
136 void NavigateToFileURL(const base::FilePath::StringType& file_path) { 202 void NavigateToFileURL(const base::FilePath::StringType& file_path) {
137 ui_test_utils::NavigateToURL( 203 ui_test_utils::NavigateToURL(
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 // that there is no cached copy. 668 // that there is no cached copy.
603 BrowsingDataRemover* remover = 669 BrowsingDataRemover* remover =
604 BrowsingDataRemover::CreateForUnboundedRange(browser()->profile()); 670 BrowsingDataRemover::CreateForUnboundedRange(browser()->profile());
605 remover->Remove(BrowsingDataRemover::REMOVE_CACHE, 671 remover->Remove(BrowsingDataRemover::REMOVE_CACHE,
606 BrowsingDataHelper::UNPROTECTED_WEB); 672 BrowsingDataHelper::UNPROTECTED_WEB);
607 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( 673 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
608 browser(), test_url, 1); 674 browser(), test_url, 1);
609 EXPECT_TRUE(ProbeStaleCopyValue(false)); 675 EXPECT_TRUE(ProbeStaleCopyValue(false));
610 } 676 }
611 677
678 class ErrorPageAutoReloadTest : public InProcessBrowserTest {
679 public:
680 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
681 command_line->AppendSwitch(switches::kEnableOfflineAutoReload);
682 }
683
684 void InstallProtocolHandler(const GURL& url, int requests_to_fail) {
685 protocol_handler_ = new FailFirstNRequestsProtocolHandler(
686 url,
687 requests_to_fail);
688 // Tests don't need to wait for this task to complete before using the
689 // filter; any requests that might be affected by it will end up in the IO
690 // thread's message loop after this posted task anyway.
691 BrowserThread::PostTask(
692 BrowserThread::IO, FROM_HERE,
693 base::Bind(&ErrorPageAutoReloadTest::AddFilters,
694 base::Unretained(this)));
695 }
696
697 void NavigateToURLAndWaitForTitle(const GURL& url,
698 const std::string& expected_title,
699 int num_navigations) {
700 content::TitleWatcher title_watcher(
701 browser()->tab_strip_model()->GetActiveWebContents(),
702 base::ASCIIToUTF16(expected_title));
703
704 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
705 browser(), url, num_navigations);
706
707 EXPECT_EQ(base::ASCIIToUTF16(expected_title),
708 title_watcher.WaitAndGetTitle());
709 }
710
711 FailFirstNRequestsProtocolHandler* protocol_handler() {
712 return protocol_handler_;
713 }
714
715 private:
716 void AddFilters() {
717 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
718 // Note: in theory, AddUrlHandler gives ownership of |protocol_handler_| to
719 // URLRequestFilter. As soon as anything calls
720 // URLRequestFilter::ClearHandlers(), |protocol_handler_| can become
721 // invalid.
722 protocol_handler_->AddUrlHandler();
723 }
724
725 FailFirstNRequestsProtocolHandler* protocol_handler_;
726 };
727
728 IN_PROC_BROWSER_TEST_F(ErrorPageAutoReloadTest, AutoReload) {
729 GURL test_url("http://error.page.auto.reload");
730 const int kRequestsToFail = 2;
731 InstallProtocolHandler(test_url, kRequestsToFail);
732 NavigateToURLAndWaitForTitle(test_url, "Test One", kRequestsToFail + 1);
733 // Note that the protocol handler updates these variables on the IO thread,
734 // but this function reads them on the main thread. The requests have to be
735 // created (on the IO thread) before NavigateToURLAndWaitForTitle returns or
736 // this becomes racey.
737 EXPECT_EQ(kRequestsToFail, protocol_handler()->failures());
738 EXPECT_EQ(kRequestsToFail + 1, protocol_handler()->requests());
739 }
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698