This source file includes following definitions.
- IsDisplayingText
- ToggleHelpBox
- IsDisplayingNetError
- ExpectDisplayingLocalErrorPage
- ExpectDisplayingNavigationCorrections
- requests_to_fail_
- AddUrlHandler
- MaybeCreateJob
- requests
- failures
- NavigateToFileURL
- NavigateToURLAndWaitForTitle
- GoBackAndWaitForTitle
- GoForwardAndWaitForTitle
- GoBackAndWaitForNavigations
- GoForwardAndWaitForNavigations
- ProbeStaleCopyValue
- ReloadStaleCopyFromCache
- EnableMocks
- SetUpOnMainThread
- GetDnsErrorURL
- NavigateHistoryAndWaitForTitle
- NavigateHistory
- DidFailProvisionalLoad
- fail_url
- InterceptNetworkTransactions
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- SetUpCommandLine
- InstallProtocolHandler
- NavigateToURLAndWaitForTitle
- protocol_handler
- AddFilters
- IN_PROC_BROWSER_TEST_F
- MaybeCreateJob
- SetUpOnMainThread
- CleanUpOnMainThread
- AddFilters
- RemoveFilters
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- SetUpOnMainThread
- CleanUpOnMainThread
- AddFilters
- RemoveFilters
- IN_PROC_BROWSER_TEST_F
#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"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browsing_data/browsing_data_helper.h"
#include "chrome/browser/browsing_data/browsing_data_remover.h"
#include "chrome/browser/google/google_util.h"
#include "chrome/browser/net/url_request_mock_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#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"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/test/net/url_request_failed_job.h"
#include "content/test/net/url_request_mock_http_job.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/http/failing_http_transaction_factory.h"
#include "net/http/http_cache.h"
#include "net/test/spawned_test_server/spawned_test_server.h"
#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 {
bool WARN_UNUSED_RESULT IsDisplayingText(Browser* browser,
const std::string& text) {
std::string command = base::StringPrintf(
"var textContent = document.body.innerText;"
"var hasText = textContent.indexOf('%s') >= 0;"
"domAutomationController.send(hasText);",
text.c_str());
bool result = false;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
browser->tab_strip_model()->GetActiveWebContents(), command, &result));
return result;
}
void ToggleHelpBox(Browser* browser) {
EXPECT_TRUE(content::ExecuteScript(
browser->tab_strip_model()->GetActiveWebContents(),
"document.getElementById('more-less-button').click();"));
}
bool WARN_UNUSED_RESULT IsDisplayingNetError(Browser* browser,
net::Error error_code) {
std::string error_string = net::ErrorToString(error_code);
base::RemoveChars(error_string, "net:", &error_string);
return IsDisplayingText(browser, error_string);
}
void ExpectDisplayingLocalErrorPage(Browser* browser, net::Error error_code) {
ToggleHelpBox(browser);
EXPECT_TRUE(IsDisplayingNetError(browser, error_code));
EXPECT_FALSE(IsDisplayingText(browser, "http://correction1/"));
EXPECT_FALSE(IsDisplayingText(browser, "http://correction2/"));
bool search_box_populated = false;
ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
browser->tab_strip_model()->GetActiveWebContents(),
"var searchText = document.getElementById('search-box').value;"
"domAutomationController.send(searchText == 'search query');",
&search_box_populated));
EXPECT_FALSE(search_box_populated);
}
void ExpectDisplayingNavigationCorrections(Browser* browser,
net::Error error_code) {
ToggleHelpBox(browser);
EXPECT_TRUE(IsDisplayingNetError(browser, error_code));
EXPECT_TRUE(IsDisplayingText(browser, "http://correction1/"));
EXPECT_TRUE(IsDisplayingText(browser, "http://correction2/"));
bool search_box_populated = false;
ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
browser->tab_strip_model()->GetActiveWebContents(),
"var searchText = document.getElementById('search-box').value;"
"domAutomationController.send(searchText == 'search query');",
&search_box_populated));
EXPECT_TRUE(search_box_populated);
}
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() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
scoped_ptr<URLRequestJobFactory::ProtocolHandler> scoped_handler(this);
net::URLRequestFilter::GetInstance()->AddUrlProtocolHandler(
url_,
scoped_handler.Pass());
}
virtual net::URLRequestJob* MaybeCreateJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const OVERRIDE {
DCHECK_EQ(url_, request->url());
requests_++;
if (failures_ < requests_to_fail_) {
failures_++;
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_;
mutable int requests_;
mutable int failures_;
int requests_to_fail_;
};
class ErrorPageTest : public InProcessBrowserTest {
public:
enum HistoryNavigationDirection {
HISTORY_NAVIGATE_BACK,
HISTORY_NAVIGATE_FORWARD,
};
void NavigateToFileURL(const base::FilePath::StringType& file_path) {
ui_test_utils::NavigateToURL(
browser(),
content::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(file_path)));
}
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());
}
void GoBackAndWaitForTitle(const std::string& expected_title,
int num_navigations) {
NavigateHistoryAndWaitForTitle(expected_title,
num_navigations,
HISTORY_NAVIGATE_BACK);
}
void GoForwardAndWaitForTitle(const std::string& expected_title,
int num_navigations) {
NavigateHistoryAndWaitForTitle(expected_title,
num_navigations,
HISTORY_NAVIGATE_FORWARD);
}
void GoBackAndWaitForNavigations(int num_navigations) {
NavigateHistory(num_navigations, HISTORY_NAVIGATE_BACK);
}
void GoForwardAndWaitForNavigations(int num_navigations) {
NavigateHistory(num_navigations, HISTORY_NAVIGATE_FORWARD);
}
bool ProbeStaleCopyValue(bool expected) {
const char* js_cache_probe =
"try {\n"
" domAutomationController.send(\n"
" templateData.staleCopyInCache ? 'yes' : 'no');\n"
"} catch (e) {\n"
" domAutomationController.send(e.message);\n"
"}\n";
std::string result;
bool ret =
content::ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetActiveWebContents(),
js_cache_probe,
&result);
EXPECT_TRUE(ret);
if (!ret)
return false;
EXPECT_EQ(expected ? "yes" : "no", result);
return ((expected ? "yes" : "no") == result);
}
testing::AssertionResult ReloadStaleCopyFromCache() {
const char* js_reload_script =
"try {\n"
" errorCacheLoad.reloadStaleInstance();\n"
" domAutomationController.send('success');\n"
"} catch (e) {\n"
" domAutomationController.send(e.message);\n"
"}\n";
std::string result;
bool ret = content::ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetActiveWebContents(),
js_reload_script,
&result);
EXPECT_TRUE(ret);
if (!ret)
return testing::AssertionFailure();
return ("success" == result ? testing::AssertionSuccess() :
(testing::AssertionFailure() << "Exception message is " << result));
}
protected:
static void EnableMocks(const GURL& search_url) {
chrome_browser_net::SetUrlRequestMocksEnabled(true);
base::FilePath root_http;
PathService::Get(chrome::DIR_TEST_DATA, &root_http);
content::URLRequestMockHTTPJob::AddHostnameToFileHandler(
search_url.host(), root_http.AppendASCII("title3.html"));
}
virtual void SetUpOnMainThread() OVERRIDE {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&ErrorPageTest::EnableMocks,
google_util::GetGoogleSearchURL(browser()->profile())));
}
GURL GetDnsErrorURL() const {
return URLRequestFailedJob::GetMockHttpUrl(net::ERR_NAME_NOT_RESOLVED);
}
private:
void NavigateHistoryAndWaitForTitle(const std::string& expected_title,
int num_navigations,
HistoryNavigationDirection direction) {
content::TitleWatcher title_watcher(
browser()->tab_strip_model()->GetActiveWebContents(),
base::ASCIIToUTF16(expected_title));
NavigateHistory(num_navigations, direction);
EXPECT_EQ(title_watcher.WaitAndGetTitle(),
base::ASCIIToUTF16(expected_title));
}
void NavigateHistory(int num_navigations,
HistoryNavigationDirection direction) {
content::TestNavigationObserver test_navigation_observer(
browser()->tab_strip_model()->GetActiveWebContents(),
num_navigations);
if (direction == HISTORY_NAVIGATE_BACK) {
chrome::GoBack(browser(), CURRENT_TAB);
} else if (direction == HISTORY_NAVIGATE_FORWARD) {
chrome::GoForward(browser(), CURRENT_TAB);
} else {
FAIL();
}
test_navigation_observer.Wait();
}
};
class TestFailProvisionalLoadObserver : public content::WebContentsObserver {
public:
explicit TestFailProvisionalLoadObserver(content::WebContents* contents)
: content::WebContentsObserver(contents) {}
virtual ~TestFailProvisionalLoadObserver() {}
virtual void DidFailProvisionalLoad(
int64 frame_id,
const base::string16& frame_unique_name,
bool is_main_frame,
const GURL& validated_url,
int error_code,
const base::string16& error_description,
content::RenderViewHost* render_view_host) OVERRIDE {
fail_url_ = validated_url;
}
const GURL& fail_url() const { return fail_url_; }
private:
GURL fail_url_;
DISALLOW_COPY_AND_ASSIGN(TestFailProvisionalLoadObserver);
};
void InterceptNetworkTransactions(net::URLRequestContextGetter* getter,
net::Error error) {
DCHECK(content::BrowserThread::CurrentlyOn(BrowserThread::IO));
net::HttpCache* cache(
getter->GetURLRequestContext()->http_transaction_factory()->GetCache());
DCHECK(cache);
scoped_ptr<net::HttpTransactionFactory> factory(
new net::FailingHttpTransactionFactory(cache->GetSession(), error));
cache->SetHttpNetworkTransactionFactoryForTesting(factory.Pass());
}
#if defined(USE_AURA) || defined(OS_WIN)
#define MAYBE_DNSError_Basic DISABLED_DNSError_Basic
#else
#define MAYBE_DNSError_Basic DNSError_Basic
#endif
IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_Basic) {
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), GetDnsErrorURL(), 2);
ExpectDisplayingNavigationCorrections(browser(), net::ERR_NAME_NOT_RESOLVED);
}
#if defined(USE_AURA)
#define MAYBE_DNSError_GoBack1 DISABLED_DNSError_GoBack1
#else
#define MAYBE_DNSError_GoBack1 DNSError_GoBack1
#endif
IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_GoBack1) {
NavigateToFileURL(FILE_PATH_LITERAL("title2.html"));
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), GetDnsErrorURL(), 2);
ExpectDisplayingNavigationCorrections(browser(), net::ERR_NAME_NOT_RESOLVED);
GoBackAndWaitForTitle("Title Of Awesomeness", 1);
}
#if defined(USE_AURA)
#define MAYBE_DNSError_GoBack2 DISABLED_DNSError_GoBack2
#else
#define MAYBE_DNSError_GoBack2 DNSError_GoBack2
#endif
IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_GoBack2) {
NavigateToFileURL(FILE_PATH_LITERAL("title2.html"));
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), GetDnsErrorURL(), 2);
ExpectDisplayingNavigationCorrections(browser(), net::ERR_NAME_NOT_RESOLVED);
NavigateToFileURL(FILE_PATH_LITERAL("title3.html"));
GoBackAndWaitForNavigations(2);
ExpectDisplayingNavigationCorrections(browser(), net::ERR_NAME_NOT_RESOLVED);
GoBackAndWaitForTitle("Title Of Awesomeness", 1);
}
#if defined(USE_AURA)
#define MAYBE_DNSError_GoBack2AndForward DISABLED_DNSError_GoBack2AndForward
#else
#define MAYBE_DNSError_GoBack2AndForward DNSError_GoBack2AndForward
#endif
IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_GoBack2AndForward) {
NavigateToFileURL(FILE_PATH_LITERAL("title2.html"));
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), GetDnsErrorURL(), 2);
ExpectDisplayingNavigationCorrections(browser(), net::ERR_NAME_NOT_RESOLVED);
NavigateToFileURL(FILE_PATH_LITERAL("title3.html"));
GoBackAndWaitForNavigations(2);
ExpectDisplayingNavigationCorrections(browser(), net::ERR_NAME_NOT_RESOLVED);
GoBackAndWaitForTitle("Title Of Awesomeness", 1);
GoForwardAndWaitForNavigations(2);
ExpectDisplayingNavigationCorrections(browser(), net::ERR_NAME_NOT_RESOLVED);
}
#if defined(USE_AURA)
#define MAYBE_DNSError_GoBack2Forward2 DISABLED_DNSError_GoBack2Forward2
#else
#define MAYBE_DNSError_GoBack2Forward2 DNSError_GoBack2Forward2
#endif
IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_GoBack2Forward2) {
NavigateToFileURL(FILE_PATH_LITERAL("title3.html"));
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), GetDnsErrorURL(), 2);
ExpectDisplayingNavigationCorrections(browser(), net::ERR_NAME_NOT_RESOLVED);
NavigateToFileURL(FILE_PATH_LITERAL("title2.html"));
GoBackAndWaitForNavigations(2);
ExpectDisplayingNavigationCorrections(browser(), net::ERR_NAME_NOT_RESOLVED);
GoBackAndWaitForTitle("Title Of More Awesomeness", 1);
GoForwardAndWaitForNavigations(2);
ExpectDisplayingNavigationCorrections(browser(), net::ERR_NAME_NOT_RESOLVED);
GoForwardAndWaitForTitle("Title Of Awesomeness", 1);
}
#if defined(USE_AURA)
#define MAYBE_DNSError_DoSearch DNSError_DoSearch
#else
#define MAYBE_DNSError_DoSearch DNSError_DoSearch
#endif
IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_DoSearch) {
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), GetDnsErrorURL(), 2);
ExpectDisplayingNavigationCorrections(browser(), net::ERR_NAME_NOT_RESOLVED);
content::TestNavigationObserver nav_observer(
browser()->tab_strip_model()->GetActiveWebContents(),
1);
content::TitleWatcher title_watcher(
browser()->tab_strip_model()->GetActiveWebContents(),
base::ASCIIToUTF16("Title Of More Awesomeness"));
ASSERT_TRUE(content::ExecuteScript(
browser()->tab_strip_model()->GetActiveWebContents(),
"document.getElementById('search-button').click();"));
nav_observer.Wait();
EXPECT_EQ(base::ASCIIToUTF16("Title Of More Awesomeness"),
title_watcher.WaitAndGetTitle());
std::string url;
ASSERT_TRUE(content::ExecuteScriptAndExtractString(
browser()->tab_strip_model()->GetActiveWebContents(),
"domAutomationController.send(window.location.href);",
&url));
EXPECT_EQ("/search", GURL(url).path());
EXPECT_EQ("q=search%20query", GURL(url).query());
GoBackAndWaitForNavigations(2);
ExpectDisplayingNavigationCorrections(browser(), net::ERR_NAME_NOT_RESOLVED);
}
IN_PROC_BROWSER_TEST_F(ErrorPageTest, IFrameDNSError_Basic) {
NavigateToURLAndWaitForTitle(
content::URLRequestMockHTTPJob::GetMockUrl(
base::FilePath(FILE_PATH_LITERAL("iframe_dns_error.html"))),
"Blah",
1);
EXPECT_EQ(2,
browser()->tab_strip_model()->GetActiveWebContents()->
GetController().GetEntryCount());
}
#if defined(OS_WIN)
#define MAYBE_IFrameDNSError_GoBack DISABLED_IFrameDNSError_GoBack
#else
#define MAYBE_IFrameDNSError_GoBack IFrameDNSError_GoBack
#endif
IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_IFrameDNSError_GoBack) {
NavigateToFileURL(FILE_PATH_LITERAL("title2.html"));
NavigateToFileURL(FILE_PATH_LITERAL("iframe_dns_error.html"));
GoBackAndWaitForTitle("Title Of Awesomeness", 1);
}
#if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA))
#define MAYBE_IFrameDNSError_GoBackAndForward DISABLED_IFrameDNSError_GoBackAndForward
#else
#define MAYBE_IFrameDNSError_GoBackAndForward IFrameDNSError_GoBackAndForward
#endif
IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_IFrameDNSError_GoBackAndForward) {
NavigateToFileURL(FILE_PATH_LITERAL("title2.html"));
NavigateToFileURL(FILE_PATH_LITERAL("iframe_dns_error.html"));
GoBackAndWaitForTitle("Title Of Awesomeness", 1);
GoForwardAndWaitForTitle("Blah", 1);
}
IN_PROC_BROWSER_TEST_F(ErrorPageTest, IFrameDNSError_JavaScript) {
content::WebContents* wc =
browser()->tab_strip_model()->GetActiveWebContents();
GURL fail_url =
URLRequestFailedJob::GetMockHttpUrl(net::ERR_NAME_NOT_RESOLVED);
NavigateToFileURL(FILE_PATH_LITERAL("title2.html"));
EXPECT_EQ(2, wc->GetController().GetEntryCount());
std::string script = "var frame = document.createElement('iframe');"
"frame.src = '" + fail_url.spec() + "';"
"document.body.appendChild(frame);";
{
TestFailProvisionalLoadObserver fail_observer(wc);
content::WindowedNotificationObserver load_observer(
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(&wc->GetController()));
wc->GetMainFrame()->ExecuteJavaScript(base::ASCIIToUTF16(script));
load_observer.Wait();
EXPECT_EQ(fail_url, fail_observer.fail_url());
EXPECT_EQ(2, wc->GetController().GetEntryCount());
}
script = "var frame = document.createElement('iframe');"
"frame.id = 'target_frame';"
"document.body.appendChild(frame);";
{
content::WindowedNotificationObserver load_observer(
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(&wc->GetController()));
wc->GetMainFrame()->ExecuteJavaScript(base::ASCIIToUTF16(script));
load_observer.Wait();
}
script = "var f = document.getElementById('target_frame');"
"f.src = '" + fail_url.spec() + "';";
{
TestFailProvisionalLoadObserver fail_observer(wc);
content::WindowedNotificationObserver load_observer(
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(&wc->GetController()));
wc->GetMainFrame()->ExecuteJavaScript(base::ASCIIToUTF16(script));
load_observer.Wait();
EXPECT_EQ(fail_url, fail_observer.fail_url());
EXPECT_EQ(2, wc->GetController().GetEntryCount());
}
}
IN_PROC_BROWSER_TEST_F(ErrorPageTest, Page404) {
NavigateToURLAndWaitForTitle(
content::URLRequestMockHTTPJob::GetMockUrl(
base::FilePath(FILE_PATH_LITERAL("page404.html"))),
"SUCCESS",
1);
}
IN_PROC_BROWSER_TEST_F(ErrorPageTest, StaleCacheStatus) {
ASSERT_TRUE(test_server()->Start());
GURL test_url(test_server()->GetURL("files/nocache.html"));
NavigateToURLAndWaitForTitle(test_url, "Nocache Test Page", 1);
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter =
browser()->profile()->GetRequestContext();
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&InterceptNetworkTransactions, url_request_context_getter,
net::ERR_FAILED));
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), test_url, 1);
EXPECT_TRUE(ProbeStaleCopyValue(true));
EXPECT_NE(base::ASCIIToUTF16("Nocache Test Page"),
browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
content::TestNavigationObserver same_tab_observer(
browser()->tab_strip_model()->GetActiveWebContents(), 1);
ASSERT_TRUE(ReloadStaleCopyFromCache());
same_tab_observer.Wait();
EXPECT_EQ(base::ASCIIToUTF16("Nocache Test Page"),
browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
BrowsingDataRemover* remover =
BrowsingDataRemover::CreateForUnboundedRange(browser()->profile());
remover->Remove(BrowsingDataRemover::REMOVE_CACHE,
BrowsingDataHelper::UNPROTECTED_WEB);
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), test_url, 1);
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);
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);
EXPECT_EQ(kRequestsToFail, protocol_handler()->failures());
EXPECT_EQ(kRequestsToFail + 1, protocol_handler()->requests());
}
class AddressUnreachableProtocolHandler
: public net::URLRequestJobFactory::ProtocolHandler {
public:
AddressUnreachableProtocolHandler() {}
virtual ~AddressUnreachableProtocolHandler() {}
virtual net::URLRequestJob* MaybeCreateJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const OVERRIDE {
return new URLRequestFailedJob(request,
network_delegate,
net::ERR_ADDRESS_UNREACHABLE);
}
private:
DISALLOW_COPY_AND_ASSIGN(AddressUnreachableProtocolHandler);
};
class ErrorPageNavigationCorrectionsFailTest : public ErrorPageTest {
public:
virtual void SetUpOnMainThread() OVERRIDE {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&ErrorPageNavigationCorrectionsFailTest::AddFilters));
}
virtual void CleanUpOnMainThread() OVERRIDE {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&ErrorPageNavigationCorrectionsFailTest::RemoveFilters));
}
private:
static void AddFilters() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
content::URLRequestFailedJob::AddUrlHandler();
net::URLRequestFilter::GetInstance()->AddUrlProtocolHandler(
google_util::LinkDoctorBaseURL(),
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(
new AddressUnreachableProtocolHandler()));
}
static void RemoveFilters() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
net::URLRequestFilter::GetInstance()->ClearHandlers();
}
};
IN_PROC_BROWSER_TEST_F(ErrorPageNavigationCorrectionsFailTest,
FetchCorrectionsFails) {
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(),
URLRequestFailedJob::GetMockHttpUrl(net::ERR_NAME_NOT_RESOLVED),
2);
ExpectDisplayingLocalErrorPage(browser(), net::ERR_NAME_NOT_RESOLVED);
}
IN_PROC_BROWSER_TEST_F(ErrorPageNavigationCorrectionsFailTest,
StaleCacheStatusFailedCorrections) {
ASSERT_TRUE(test_server()->Start());
GURL test_url(test_server()->GetURL("files/nocache.html"));
NavigateToURLAndWaitForTitle(test_url, "Nocache Test Page", 1);
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter =
browser()->profile()->GetRequestContext();
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&InterceptNetworkTransactions, url_request_context_getter,
net::ERR_CONNECTION_FAILED));
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), test_url, 2);
ProbeStaleCopyValue(true);
content::TestNavigationObserver same_tab_observer(
browser()->tab_strip_model()->GetActiveWebContents(), 1);
ASSERT_TRUE(ReloadStaleCopyFromCache());
same_tab_observer.Wait();
EXPECT_EQ(base::ASCIIToUTF16("Nocache Test Page"),
browser()->tab_strip_model()->GetActiveWebContents()->GetTitle());
BrowsingDataRemover* remover =
BrowsingDataRemover::CreateForUnboundedRange(browser()->profile());
remover->Remove(BrowsingDataRemover::REMOVE_CACHE,
BrowsingDataHelper::UNPROTECTED_WEB);
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), test_url, 2);
ProbeStaleCopyValue(false);
}
class ErrorPageForIDNTest : public InProcessBrowserTest {
public:
static const char kHostname[];
static const char kHostnameJSUnicode[];
virtual void SetUpOnMainThread() OVERRIDE {
browser()->profile()->GetPrefs()->SetString(prefs::kAcceptLanguages,
std::string());
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&ErrorPageForIDNTest::AddFilters));
}
virtual void CleanUpOnMainThread() OVERRIDE {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&ErrorPageForIDNTest::RemoveFilters));
}
private:
static void AddFilters() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
content::URLRequestFailedJob::AddUrlHandlerForHostname(kHostname);
}
static void RemoveFilters() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
net::URLRequestFilter::GetInstance()->ClearHandlers();
}
};
const char ErrorPageForIDNTest::kHostname[] =
"xn--d1abbgf6aiiy.xn--p1ai";
const char ErrorPageForIDNTest::kHostnameJSUnicode[] =
"\\u043f\\u0440\\u0435\\u0437\\u0438\\u0434\\u0435\\u043d\\u0442."
"\\u0440\\u0444";
IN_PROC_BROWSER_TEST_F(ErrorPageForIDNTest, IDN) {
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(),
URLRequestFailedJob::GetMockHttpUrlForHostname(net::ERR_UNSAFE_PORT,
kHostname),
1);
ToggleHelpBox(browser());
EXPECT_TRUE(IsDisplayingText(browser(), kHostnameJSUnicode));
}
}