Chromium Code Reviews| Index: chrome/browser/errorpage_browsertest.cc |
| =================================================================== |
| --- chrome/browser/errorpage_browsertest.cc (revision 248295) |
| +++ chrome/browser/errorpage_browsertest.cc (working copy) |
| @@ -3,6 +3,8 @@ |
| // found in the LICENSE file. |
| #include "base/bind.h" |
| +#include "base/compiler_specific.h" |
| +#include "base/path_service.h" |
| #include "base/prefs/pref_service.h" |
| #include "base/strings/stringprintf.h" |
| #include "base/strings/utf_string_conversions.h" |
| @@ -12,6 +14,7 @@ |
| #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/pref_names.h" |
| #include "chrome/test/base/in_process_browser_test.h" |
| #include "chrome/test/base/ui_test_utils.h" |
| @@ -34,6 +37,84 @@ |
| namespace { |
| +// Returns true if |text| is displayed on the page |browser| is currently |
| +// displaying. Uses "innerText", so will miss hidden text, and whitespace |
| +// space handling may be weird. |
| +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; |
| +} |
| + |
| +// Expands the more box on the currently displayed error page. |
|
Deprecated (see juliatuttle)
2014/02/04 17:44:21
Technically it'd be nicer if you searched for the
mmenke
2014/02/04 19:43:56
I'm not sure...Then I'd be depending on an english
Deprecated (see juliatuttle)
2014/02/04 20:20:20
Maybe search for it by id then? I don't like that
mmenke
2014/02/04 20:31:09
Getting it by its id and simulating a click sounds
|
| +void ToggleHelpBox(Browser* browser) { |
| + EXPECT_TRUE(content::ExecuteScript( |
| + browser->tab_strip_model()->GetActiveWebContents(), "toggleHelpBox()")); |
| +} |
| + |
| +// Returns true if |browser| is displaying the text representation of |
| +// |error_code| on the current page. |
| +bool WARN_UNUSED_RESULT IsDisplayingNetError(Browser* browser, |
| + net::Error error_code) { |
| + // Get the error as a string, and remove the leading "net::", which is not |
| + // included on error pages. |
| + std::string error_string = net::ErrorToString(error_code); |
| + base::RemoveChars(error_string, "net:", &error_string); |
|
Deprecated (see juliatuttle)
2014/02/04 17:44:21
It bothers me that this relies on the fact that th
mmenke
2014/02/04 19:43:56
I agree that this is weird and ugly, but it's done
Deprecated (see juliatuttle)
2014/02/04 20:20:20
Alright.
|
| + |
| + return IsDisplayingText(browser, error_string); |
| +} |
| + |
| +// Checks that the local error page is being displayed, without remotely |
| +// retrieved Link Doctor information, and with the specified error code. |
| +void ExpectDisplayingLocalErrorPage(Browser* browser, net::Error error_code) { |
| + // Expand the help box so innerText will include text below the fold. |
| + ToggleHelpBox(browser); |
| + |
| + EXPECT_TRUE(IsDisplayingNetError(browser, error_code)); |
| + |
| + // Locally generated error pages should not have Link Doctor suggestions. |
| + EXPECT_FALSE(IsDisplayingText(browser, "http://correction1/")); |
| + EXPECT_FALSE(IsDisplayingText(browser, "http://correction2/")); |
| + |
| + // Locally generated error pages should not have a populated search box. |
| + 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); |
| +} |
| + |
| +// Checks that an error page with information retrieved from the Link Doctor |
| +// is being displayed, with the specified specified error code. |
| +void ExpectDisplayingLinkDoctor(Browser* browser, net::Error error_code) { |
| + // Expand the help box so innerText will include text below the fold. |
| + ToggleHelpBox(browser); |
| + |
| + EXPECT_TRUE(IsDisplayingNetError(browser, error_code)); |
| + |
| + // Check that suggestions from the mock Link Doctor results are displayed. |
| + EXPECT_TRUE(IsDisplayingText(browser, "http://correction1/")); |
| + EXPECT_TRUE(IsDisplayingText(browser, "http://correction2/")); |
| + |
| + // Check that the search box is populated correctly. |
| + 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 ErrorPageTest : public InProcessBrowserTest { |
| public: |
| enum HistoryNavigationDirection { |
| @@ -82,11 +163,32 @@ |
| HISTORY_NAVIGATE_FORWARD); |
| } |
| + // Navigates back in the history and waits for |num_navigations| to occur. |
|
Deprecated (see juliatuttle)
2014/02/04 17:44:21
GoBack(2) to me reads as "go back twice"; I'd pref
mmenke
2014/02/04 19:43:56
I agree, done.
|
| + void GoBack(int num_navigations) { |
| + NavigateHistory(num_navigations, HISTORY_NAVIGATE_BACK); |
| + } |
| + |
| + // Navigates forward in the history and waits for |num_navigations| to occur. |
| + void GoForward(int num_navigations) { |
| + NavigateHistory(num_navigations, HISTORY_NAVIGATE_FORWARD); |
| + } |
| + |
| protected: |
| + static void EnableMocks(const GURL& search_url) { |
| + chrome_browser_net::SetUrlRequestMocksEnabled(true); |
| + |
| + // Add a mock for the search engine the error page will use. |
| + 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(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| + base::Bind(&ErrorPageTest::EnableMocks, |
| + google_util::GetGoogleSearchURL(browser()->profile()))); |
| } |
| // Returns a GURL that results in a DNS error. |
| @@ -104,6 +206,14 @@ |
| 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); |
| @@ -115,9 +225,6 @@ |
| FAIL(); |
| } |
| test_navigation_observer.Wait(); |
| - |
| - EXPECT_EQ(title_watcher.WaitAndGetTitle(), |
| - base::ASCIIToUTF16(expected_title)); |
| } |
| }; |
| @@ -158,7 +265,9 @@ |
| IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_Basic) { |
| // The first navigation should fail, and the second one should be the error |
| // page. |
| - NavigateToURLAndWaitForTitle(GetDnsErrorURL(), "Mock Link Doctor", 2); |
| + ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| + browser(), GetDnsErrorURL(), 2); |
| + ExpectDisplayingLinkDoctor(browser(), net::ERR_NAME_NOT_RESOLVED); |
| } |
| // See crbug.com/109669 |
| @@ -172,7 +281,9 @@ |
| // additional session history entry. |
| IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_GoBack1) { |
| NavigateToFileURL(FILE_PATH_LITERAL("title2.html")); |
| - NavigateToURLAndWaitForTitle(GetDnsErrorURL(), "Mock Link Doctor", 2); |
| + ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| + browser(), GetDnsErrorURL(), 2); |
| + ExpectDisplayingLinkDoctor(browser(), net::ERR_NAME_NOT_RESOLVED); |
| GoBackAndWaitForTitle("Title Of Awesomeness", 1); |
| } |
| @@ -187,10 +298,15 @@ |
| IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_GoBack2) { |
| NavigateToFileURL(FILE_PATH_LITERAL("title2.html")); |
| - NavigateToURLAndWaitForTitle(GetDnsErrorURL(), "Mock Link Doctor", 2); |
| + ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| + browser(), GetDnsErrorURL(), 2); |
| + ExpectDisplayingLinkDoctor(browser(), net::ERR_NAME_NOT_RESOLVED); |
| + |
| NavigateToFileURL(FILE_PATH_LITERAL("title3.html")); |
| - GoBackAndWaitForTitle("Mock Link Doctor", 2); |
| + GoBack(2); |
| + ExpectDisplayingLinkDoctor(browser(), net::ERR_NAME_NOT_RESOLVED); |
| + |
| GoBackAndWaitForTitle("Title Of Awesomeness", 1); |
| } |
| @@ -205,13 +321,19 @@ |
| IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_GoBack2AndForward) { |
| NavigateToFileURL(FILE_PATH_LITERAL("title2.html")); |
| - NavigateToURLAndWaitForTitle(GetDnsErrorURL(), "Mock Link Doctor", 2); |
| + ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| + browser(), GetDnsErrorURL(), 2); |
| + ExpectDisplayingLinkDoctor(browser(), net::ERR_NAME_NOT_RESOLVED); |
| + |
| NavigateToFileURL(FILE_PATH_LITERAL("title3.html")); |
| - GoBackAndWaitForTitle("Mock Link Doctor", 2); |
| + GoBack(2); |
| + ExpectDisplayingLinkDoctor(browser(), net::ERR_NAME_NOT_RESOLVED); |
| + |
| GoBackAndWaitForTitle("Title Of Awesomeness", 1); |
| - GoForwardAndWaitForTitle("Mock Link Doctor", 2); |
| + GoForward(2); |
| + ExpectDisplayingLinkDoctor(browser(), net::ERR_NAME_NOT_RESOLVED); |
| } |
| // See crbug.com/109669 |
| @@ -225,17 +347,67 @@ |
| IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_GoBack2Forward2) { |
| NavigateToFileURL(FILE_PATH_LITERAL("title3.html")); |
| - NavigateToURLAndWaitForTitle(GetDnsErrorURL(), "Mock Link Doctor", 2); |
| + ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| + browser(), GetDnsErrorURL(), 2); |
| + ExpectDisplayingLinkDoctor(browser(), net::ERR_NAME_NOT_RESOLVED); |
| + |
| NavigateToFileURL(FILE_PATH_LITERAL("title2.html")); |
| - GoBackAndWaitForTitle("Mock Link Doctor", 2); |
| + GoBack(2); |
| + ExpectDisplayingLinkDoctor(browser(), net::ERR_NAME_NOT_RESOLVED); |
| + |
| GoBackAndWaitForTitle("Title Of More Awesomeness", 1); |
| - GoForwardAndWaitForTitle("Mock Link Doctor", 2); |
| + GoForward(2); |
| + ExpectDisplayingLinkDoctor(browser(), net::ERR_NAME_NOT_RESOLVED); |
| + |
| GoForwardAndWaitForTitle("Title Of Awesomeness", 1); |
| } |
| -// Test that a DNS error occuring in an iframe. |
| +// See crbug.com/109669 |
| +#if defined(USE_AURA) |
| +#define MAYBE_DNSError_DoSearch DISABLED_DNSError_DoSearch |
| +#else |
| +#define MAYBE_DNSError_DoSearch DNSError_DoSearch |
| +#endif |
| +// Test that the search button on a DNS error page works. |
| +IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_DoSearch) { |
| + // The first navigation should fail, and the second one should be the error |
| + // page. |
| + ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| + browser(), GetDnsErrorURL(), 2); |
| + ExpectDisplayingLinkDoctor(browser(), net::ERR_NAME_NOT_RESOLVED); |
| + |
| + // Do a search and make sure the browser ends up at the right page. |
| + 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()); |
| + |
| + // Check the path and query string. |
| + 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+query", GURL(url).query()); |
| + |
| + // Go back to the error page, to make sure the history is correct. |
| + GoBack(2); |
| + ExpectDisplayingLinkDoctor(browser(), net::ERR_NAME_NOT_RESOLVED); |
| +} |
| + |
| +// Test that a DNS error occuring in an iframe does not result in showing the |
| +// Link Doctor. |
| IN_PROC_BROWSER_TEST_F(ErrorPageTest, IFrameDNSError_Basic) { |
| NavigateToURLAndWaitForTitle( |
| content::URLRequestMockHTTPJob::GetMockUrl( |
| @@ -356,17 +528,6 @@ |
| 1); |
| } |
| -// 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()); |
| -} |
| - |
| // Protocol handler that fails all requests with net::ERR_ADDRESS_UNREACHABLE. |
| class AddressUnreachableProtocolHandler |
| : public net::URLRequestJobFactory::ProtocolHandler { |
| @@ -437,14 +598,8 @@ |
| URLRequestFailedJob::GetMockHttpUrl(net::ERR_NAME_NOT_RESOLVED), |
| 2); |
| - // Verify that the expected error page is being displayed. Do this by making |
| - // sure the original error code (ERR_NAME_NOT_RESOLVED) is displayed. |
| - bool result = false; |
| - EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| - browser()->tab_strip_model()->GetActiveWebContents(), |
| - GetTextContentContainsStringScript("ERR_NAME_NOT_RESOLVED"), |
| - &result)); |
| - EXPECT_TRUE(result); |
| + // Verify that the expected error page is being displayed. |
| + ExpectDisplayingLocalErrorPage(browser(), net::ERR_NAME_NOT_RESOLVED); |
| } |
| // A test fixture that simulates failing requests for an IDN domain name. |
| @@ -497,12 +652,8 @@ |
| kHostname), |
| 1); |
| - bool result = false; |
| - EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| - browser()->tab_strip_model()->GetActiveWebContents(), |
| - GetTextContentContainsStringScript(kHostnameJSUnicode), |
| - &result)); |
| - EXPECT_TRUE(result); |
| + ToggleHelpBox(browser()); |
| + EXPECT_TRUE(IsDisplayingText(browser(), kHostnameJSUnicode)); |
| } |
| } // namespace |