[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 6777043 | 2011-09-21 18:12:32 | [diff] [blame] | 5 | #include "base/bind.h" |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 6 | #include "base/utf_string_conversions.h" |
[email protected] | 6777043 | 2011-09-21 18:12:32 | [diff] [blame] | 7 | #include "chrome/browser/net/url_request_mock_util.h" |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 8 | #include "chrome/browser/ui/browser.h" |
| 9 | #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 10 | #include "chrome/test/base/in_process_browser_test.h" |
| 11 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 12 | #include "content/browser/net/url_request_failed_dns_job.h" |
| 13 | #include "content/browser/net/url_request_mock_http_job.h" |
[email protected] | 83ff91c | 2012-01-05 20:54:13 | [diff] [blame] | 14 | #include "content/public/browser/web_contents.h" |
[email protected] | 405af13 | 2011-12-14 01:17:57 | [diff] [blame] | 15 | #include "content/test/test_navigation_observer.h" |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 16 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 17 | using content::BrowserThread; |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 18 | using content::NavigationController; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 19 | |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 20 | class ErrorPageTest : public InProcessBrowserTest { |
| 21 | public: |
| 22 | enum HistoryNavigationDirection { |
| 23 | HISTORY_NAVIGATE_BACK, |
| 24 | HISTORY_NAVIGATE_FORWARD, |
| 25 | }; |
| 26 | |
| 27 | // Navigates the active tab to a mock url created for the file at |file_path|. |
| 28 | void NavigateToFileURL(const FilePath::StringType& file_path) { |
| 29 | ui_test_utils::NavigateToURL( |
| 30 | browser(), |
| 31 | URLRequestMockHTTPJob::GetMockUrl(FilePath(file_path))); |
| 32 | } |
| 33 | |
| 34 | // Navigates to the given URL and waits for |num_navigations| to occur, and |
| 35 | // the title to change to |expected_title|. |
| 36 | void NavigateToURLAndWaitForTitle(const GURL& url, |
| 37 | const std::string& expected_title, |
| 38 | int num_navigations) { |
| 39 | ui_test_utils::TitleWatcher title_watcher( |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 40 | browser()->GetSelectedWebContents(), |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 41 | ASCIIToUTF16(expected_title)); |
| 42 | |
| 43 | ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 44 | browser(), url, num_navigations); |
| 45 | |
| 46 | EXPECT_EQ(title_watcher.WaitAndGetTitle(), ASCIIToUTF16(expected_title)); |
| 47 | } |
| 48 | |
| 49 | // Navigates back in the history and waits for |num_navigations| to occur, and |
| 50 | // the title to change to |expected_title|. |
| 51 | void GoBackAndWaitForTitle(const std::string& expected_title, |
| 52 | int num_navigations) { |
| 53 | NavigateHistoryAndWaitForTitle(expected_title, |
| 54 | num_navigations, |
| 55 | HISTORY_NAVIGATE_BACK); |
| 56 | } |
| 57 | |
| 58 | // Navigates forward in the history and waits for |num_navigations| to occur, |
| 59 | // and the title to change to |expected_title|. |
| 60 | void GoForwardAndWaitForTitle(const std::string& expected_title, |
| 61 | int num_navigations) { |
| 62 | NavigateHistoryAndWaitForTitle(expected_title, |
| 63 | num_navigations, |
| 64 | HISTORY_NAVIGATE_FORWARD); |
| 65 | } |
| 66 | |
[email protected] | 6777043 | 2011-09-21 18:12:32 | [diff] [blame] | 67 | protected: |
| 68 | void SetUpOnMainThread() OVERRIDE { |
| 69 | BrowserThread::PostTask( |
| 70 | BrowserThread::IO, FROM_HERE, |
| 71 | base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); |
| 72 | } |
| 73 | |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 74 | private: |
| 75 | // Navigates the browser the indicated direction in the history and waits for |
| 76 | // |num_navigations| to occur and the title to change to |expected_title|. |
| 77 | void NavigateHistoryAndWaitForTitle(const std::string& expected_title, |
| 78 | int num_navigations, |
| 79 | HistoryNavigationDirection direction) { |
| 80 | ui_test_utils::TitleWatcher title_watcher( |
[email protected] | bb81f38 | 2012-01-03 22:45:44 | [diff] [blame] | 81 | browser()->GetSelectedWebContents(), |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 82 | ASCIIToUTF16(expected_title)); |
| 83 | |
| 84 | TestNavigationObserver test_navigation_observer( |
[email protected] | c5eed49 | 2012-01-04 17:07:50 | [diff] [blame] | 85 | content::Source<NavigationController>( |
[email protected] | 83ff91c | 2012-01-05 20:54:13 | [diff] [blame] | 86 | &browser()->GetSelectedTabContentsWrapper()->web_contents()-> |
[email protected] | f5fa20e | 2011-12-21 22:35:56 | [diff] [blame] | 87 | GetController()), |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 88 | NULL, |
| 89 | num_navigations); |
| 90 | if (direction == HISTORY_NAVIGATE_BACK) { |
| 91 | browser()->GoBack(CURRENT_TAB); |
| 92 | } else if (direction == HISTORY_NAVIGATE_FORWARD) { |
| 93 | browser()->GoForward(CURRENT_TAB); |
| 94 | } else { |
| 95 | FAIL(); |
| 96 | } |
[email protected] | 405af13 | 2011-12-14 01:17:57 | [diff] [blame] | 97 | test_navigation_observer.WaitForObservation( |
| 98 | base::Bind(&ui_test_utils::RunMessageLoop), |
| 99 | base::Bind(&MessageLoop::Quit, |
| 100 | base::Unretained(MessageLoopForUI::current()))); |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 101 | |
| 102 | EXPECT_EQ(title_watcher.WaitAndGetTitle(), ASCIIToUTF16(expected_title)); |
| 103 | } |
| 104 | }; |
| 105 | |
[email protected] | c61d8f3 | 2012-01-11 01:33:28 | [diff] [blame] | 106 | // See crbug.com/109669 |
| 107 | #if defined(USE_AURA) |
| 108 | #define MAYBE_DNSError_Basic FLAKY_DNSError_Basic |
| 109 | #else |
| 110 | #define MAYBE_DNSError_Basic DNSError_Basic |
| 111 | #endif |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 112 | // Test that a DNS error occuring in the main frame redirects to an error page. |
[email protected] | c61d8f3 | 2012-01-11 01:33:28 | [diff] [blame] | 113 | IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_Basic) { |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 114 | GURL test_url(URLRequestFailedDnsJob::kTestUrl); |
| 115 | // The first navigation should fail, and the second one should be the error |
| 116 | // page. |
| 117 | NavigateToURLAndWaitForTitle(test_url, "Mock Link Doctor", 2); |
| 118 | } |
| 119 | |
| 120 | // Test that a DNS error occuring in the main frame does not result in an |
| 121 | // additional session history entry. |
| 122 | IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_GoBack1) { |
| 123 | GURL test_url(URLRequestFailedDnsJob::kTestUrl); |
| 124 | NavigateToFileURL(FILE_PATH_LITERAL("title2.html")); |
| 125 | NavigateToURLAndWaitForTitle(test_url, "Mock Link Doctor", 2); |
| 126 | GoBackAndWaitForTitle("Title Of Awesomeness", 1); |
| 127 | } |
| 128 | |
[email protected] | 058fce35 | 2012-01-09 22:13:39 | [diff] [blame] | 129 | // See crbug.com/109669 |
| 130 | #if defined(USE_AURA) |
| 131 | #define MAYBE_DNSError_GoBack2 FLAKY_DNSError_GoBack2 |
| 132 | #else |
| 133 | #define MAYBE_DNSError_GoBack2 DNSError_GoBack2 |
| 134 | #endif |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 135 | // Test that a DNS error occuring in the main frame does not result in an |
| 136 | // additional session history entry. |
[email protected] | 058fce35 | 2012-01-09 22:13:39 | [diff] [blame] | 137 | IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_GoBack2) { |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 138 | GURL test_url(URLRequestFailedDnsJob::kTestUrl); |
| 139 | NavigateToFileURL(FILE_PATH_LITERAL("title2.html")); |
| 140 | |
| 141 | NavigateToURLAndWaitForTitle(test_url, "Mock Link Doctor", 2); |
| 142 | NavigateToFileURL(FILE_PATH_LITERAL("title3.html")); |
| 143 | |
| 144 | GoBackAndWaitForTitle("Mock Link Doctor", 2); |
| 145 | GoBackAndWaitForTitle("Title Of Awesomeness", 1); |
| 146 | } |
| 147 | |
[email protected] | c61d8f3 | 2012-01-11 01:33:28 | [diff] [blame] | 148 | // See crbug.com/109669 |
| 149 | #if defined(USE_AURA) |
| 150 | #define MAYBE_DNSError_GoBack2AndForward FLAKY_DNSError_GoBack2AndForward |
| 151 | #else |
| 152 | #define MAYBE_DNSError_GoBack2AndForward DNSError_GoBack2AndForward |
| 153 | #endif |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 154 | // Test that a DNS error occuring in the main frame does not result in an |
| 155 | // additional session history entry. |
[email protected] | c61d8f3 | 2012-01-11 01:33:28 | [diff] [blame] | 156 | IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_GoBack2AndForward) { |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 157 | GURL test_url(URLRequestFailedDnsJob::kTestUrl); |
| 158 | NavigateToFileURL(FILE_PATH_LITERAL("title2.html")); |
| 159 | |
| 160 | NavigateToURLAndWaitForTitle(test_url, "Mock Link Doctor", 2); |
| 161 | NavigateToFileURL(FILE_PATH_LITERAL("title3.html")); |
| 162 | |
| 163 | GoBackAndWaitForTitle("Mock Link Doctor", 2); |
| 164 | GoBackAndWaitForTitle("Title Of Awesomeness", 1); |
| 165 | |
| 166 | GoForwardAndWaitForTitle("Mock Link Doctor", 2); |
| 167 | } |
| 168 | |
[email protected] | c61d8f3 | 2012-01-11 01:33:28 | [diff] [blame] | 169 | // See crbug.com/109669 |
| 170 | #if defined(USE_AURA) |
[email protected] | adbc48a | 2012-01-11 04:19:05 | [diff] [blame] | 171 | #define MAYBE_DNSError_GoBack2Forward2 FLAKY_DNSError_GoBack2Forward2 |
[email protected] | c61d8f3 | 2012-01-11 01:33:28 | [diff] [blame] | 172 | #else |
[email protected] | adbc48a | 2012-01-11 04:19:05 | [diff] [blame] | 173 | #define MAYBE_DNSError_GoBack2Forward2 DNSError_GoBack2Forward2 |
[email protected] | c61d8f3 | 2012-01-11 01:33:28 | [diff] [blame] | 174 | #endif |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 175 | // Test that a DNS error occuring in the main frame does not result in an |
| 176 | // additional session history entry. |
[email protected] | c61d8f3 | 2012-01-11 01:33:28 | [diff] [blame] | 177 | IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_GoBack2Forward2) { |
[email protected] | 7ad4022 | 2011-09-12 16:22:44 | [diff] [blame] | 178 | GURL test_url(URLRequestFailedDnsJob::kTestUrl); |
| 179 | NavigateToFileURL(FILE_PATH_LITERAL("title3.html")); |
| 180 | |
| 181 | NavigateToURLAndWaitForTitle(test_url, "Mock Link Doctor", 2); |
| 182 | NavigateToFileURL(FILE_PATH_LITERAL("title2.html")); |
| 183 | |
| 184 | GoBackAndWaitForTitle("Mock Link Doctor", 2); |
| 185 | GoBackAndWaitForTitle("Title Of More Awesomeness", 1); |
| 186 | |
| 187 | GoForwardAndWaitForTitle("Mock Link Doctor", 2); |
| 188 | GoForwardAndWaitForTitle("Title Of Awesomeness", 1); |
| 189 | } |
| 190 | |
| 191 | // Test that a DNS error occuring in an iframe. |
| 192 | IN_PROC_BROWSER_TEST_F(ErrorPageTest, IFrameDNSError_Basic) { |
| 193 | NavigateToURLAndWaitForTitle( |
| 194 | URLRequestMockHTTPJob::GetMockUrl( |
| 195 | FilePath(FILE_PATH_LITERAL("iframe_dns_error.html"))), |
| 196 | "Blah", |
| 197 | 1); |
| 198 | } |
| 199 | |
| 200 | // Test that a DNS error occuring in an iframe does not result in an |
| 201 | // additional session history entry. |
| 202 | IN_PROC_BROWSER_TEST_F(ErrorPageTest, IFrameDNSError_GoBack) { |
| 203 | NavigateToFileURL(FILE_PATH_LITERAL("title2.html")); |
| 204 | NavigateToFileURL(FILE_PATH_LITERAL("iframe_dns_error.html")); |
| 205 | GoBackAndWaitForTitle("Title Of Awesomeness", 1); |
| 206 | } |
| 207 | |
| 208 | // Test that a DNS error occuring in an iframe does not result in an |
| 209 | // additional session history entry. |
| 210 | IN_PROC_BROWSER_TEST_F(ErrorPageTest, IFrameDNSError_GoBackAndForward) { |
| 211 | NavigateToFileURL(FILE_PATH_LITERAL("title2.html")); |
| 212 | NavigateToFileURL(FILE_PATH_LITERAL("iframe_dns_error.html")); |
| 213 | GoBackAndWaitForTitle("Title Of Awesomeness", 1); |
| 214 | GoForwardAndWaitForTitle("Blah", 1); |
| 215 | } |
| 216 | |
| 217 | // Checks that the Link Doctor is not loaded when we receive an actual 404 page. |
| 218 | IN_PROC_BROWSER_TEST_F(ErrorPageTest, Page404) { |
| 219 | NavigateToURLAndWaitForTitle( |
| 220 | URLRequestMockHTTPJob::GetMockUrl( |
| 221 | FilePath(FILE_PATH_LITERAL("page404.html"))), |
| 222 | "SUCCESS", |
| 223 | 1); |
| 224 | } |