Chromium Code Reviews| Index: chrome/browser/errorpage_browsertest.cc |
| diff --git a/chrome/browser/errorpage_browsertest.cc b/chrome/browser/errorpage_browsertest.cc |
| index 47c00f84ee751b6d745b6d7600508d20001553ac..ab51ad9fcb8706c19579a2541ce807cc06ec2d2f 100644 |
| --- a/chrome/browser/errorpage_browsertest.cc |
| +++ b/chrome/browser/errorpage_browsertest.cc |
| @@ -3,7 +3,10 @@ |
| // found in the LICENSE file. |
| #include "base/bind.h" |
| +#include "base/command_line.h" |
| #include "base/compiler_specific.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| #include "base/path_service.h" |
| #include "base/prefs/pref_service.h" |
| #include "base/strings/stringprintf.h" |
| @@ -17,6 +20,7 @@ |
| #include "chrome/browser/ui/browser_commands.h" |
| #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| #include "chrome/common/chrome_paths.h" |
| +#include "chrome/common/chrome_switches.h" |
| #include "chrome/common/pref_names.h" |
| #include "chrome/test/base/in_process_browser_test.h" |
| #include "chrome/test/base/ui_test_utils.h" |
| @@ -36,11 +40,16 @@ |
| #include "net/url_request/url_request_context.h" |
| #include "net/url_request/url_request_context_getter.h" |
| #include "net/url_request/url_request_filter.h" |
| +#include "net/url_request/url_request_job.h" |
| #include "net/url_request/url_request_job_factory.h" |
| +#include "net/url_request/url_request_test_job.h" |
| +#include "net/url_request/url_request_test_util.h" |
| using content::BrowserThread; |
| using content::NavigationController; |
| using content::URLRequestFailedJob; |
| +using net::URLRequestJobFactory; |
| +using net::URLRequestTestJob; |
| namespace { |
| @@ -125,6 +134,56 @@ void ExpectDisplayingNavigationCorrections(Browser* browser, |
| EXPECT_TRUE(search_box_populated); |
| } |
| +// A protocol handler that fails a configurable number of requests, then |
| +// succeeds all requests after that, keeping count of failures and successes. |
| +class FailFirstNRequestsProtocolHandler |
| + : public URLRequestJobFactory::ProtocolHandler { |
| + public: |
| + FailFirstNRequestsProtocolHandler(const GURL& url, int requests_to_fail) |
| + : url_(url), requests_(0), failures_(0), |
| + requests_to_fail_(requests_to_fail) {} |
| + virtual ~FailFirstNRequestsProtocolHandler() {} |
| + |
| + 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.
|
| + 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.
|
| + scoped_ptr<URLRequestJobFactory::ProtocolHandler> scoped_handler(this); |
| + net::URLRequestFilter::GetInstance()->AddUrlProtocolHandler( |
| + url_, |
| + scoped_handler.Pass()); |
| + } |
| + |
| + 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.
|
| + net::URLRequest* request, |
| + net::NetworkDelegate* network_delegate) const OVERRIDE { |
| + DCHECK_EQ(url_, request->url()); |
| + requests_++; |
| + if (failures_ < requests_to_fail_) { |
| + failures_++; |
| + // Note: net::ERR_CONNECTION_RESET does not summon the Link Doctor; see |
| + // NetErrorHelperCore::GetErrorPageURL. |
| + return new URLRequestFailedJob(request, |
| + network_delegate, |
| + net::ERR_CONNECTION_RESET); |
| + } else { |
| + return new URLRequestTestJob(request, network_delegate, |
| + URLRequestTestJob::test_headers(), |
| + URLRequestTestJob::test_data_1(), |
| + true); |
| + } |
| + } |
| + |
| + int requests() const { return requests_; } |
| + int failures() const { return failures_; } |
| + |
| + private: |
| + const GURL url_; |
| + // These are mutable because MaybeCreateJob is const but we want this state |
| + // for testing. |
| + mutable int requests_; |
| + mutable int failures_; |
| + int requests_to_fail_; |
| +}; |
| + |
| class ErrorPageTest : public InProcessBrowserTest { |
| public: |
| enum HistoryNavigationDirection { |
| @@ -609,6 +668,76 @@ IN_PROC_BROWSER_TEST_F(ErrorPageTest, StaleCacheStatus) { |
| EXPECT_TRUE(ProbeStaleCopyValue(false)); |
| } |
| +class ErrorPageAutoReloadTest : public InProcessBrowserTest { |
| + public: |
| + virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| + command_line->AppendSwitch(switches::kEnableOfflineAutoReload); |
| + } |
| + |
| + void InstallProtocolHandler(const GURL& url, int requests_to_fail) { |
| + protocol_handler_ = new FailFirstNRequestsProtocolHandler( |
| + url, |
| + requests_to_fail); |
| + // Tests don't need to wait for this task to complete before using the |
| + // filter; any requests that might be affected by it will end up in the IO |
| + // thread's message loop after this posted task anyway. |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&ErrorPageAutoReloadTest::AddFilters, |
| + base::Unretained(this))); |
| + } |
| + |
| + void NavigateToURLAndWaitForTitle(const GURL& url, |
| + const std::string& expected_title, |
| + int num_navigations) { |
| + content::TitleWatcher title_watcher( |
| + browser()->tab_strip_model()->GetActiveWebContents(), |
| + base::ASCIIToUTF16(expected_title)); |
| + |
| + ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| + browser(), url, num_navigations); |
| + |
| + EXPECT_EQ(base::ASCIIToUTF16(expected_title), |
| + title_watcher.WaitAndGetTitle()); |
| + } |
| + |
| + FailFirstNRequestsProtocolHandler* protocol_handler() { |
| + return protocol_handler_; |
| + } |
| + |
| + private: |
| + void AddFilters() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + protocol_handler_->AddUrlHandler(); |
| + } |
| + |
| + FailFirstNRequestsProtocolHandler* protocol_handler_; |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(ErrorPageAutoReloadTest, AutoReload) { |
| + GURL test_url("http://error.page.auto.reload"); |
| + const int kRequestsToFail = 2; |
| + InstallProtocolHandler(test_url, kRequestsToFail); |
| + NavigateToURLAndWaitForTitle(test_url, "Test One", kRequestsToFail + 1); |
| + // Note that the protocol handler updates these variables on the IO thread, |
| + // but this function reads them on the main thread. The requests have to be |
| + // created (on the IO thread) before NavigateToURLAndWaitForTitle returns or |
| + // this becomes racey. |
| + EXPECT_EQ(kRequestsToFail, protocol_handler()->failures()); |
| + EXPECT_EQ(kRequestsToFail + 1, protocol_handler()->requests()); |
| +} |
| + |
| +// Returns Javascript code that executes plain text search for the page. |
| +// Pass into content::ExecuteScriptAndExtractBool as |script| parameter. |
| +std::string GetTextContentContainsStringScript( |
| + const std::string& value_to_search) { |
| + return base::StringPrintf( |
| + "var textContent = document.body.textContent;" |
| + "var hasError = textContent.indexOf('%s') >= 0;" |
| + "domAutomationController.send(hasError);", |
| + value_to_search.c_str()); |
| +} |
|
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.
|
| + |
| // Protocol handler that fails all requests with net::ERR_ADDRESS_UNREACHABLE. |
| class AddressUnreachableProtocolHandler |
| : public net::URLRequestJobFactory::ProtocolHandler { |