blob: 471f9eace01cee1aa68ae844ff16b8ccc6dc7fd1 [file] [log] [blame]
[email protected]7ad40222011-09-12 16:22:441// 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]67770432011-09-21 18:12:325#include "base/bind.h"
[email protected]7ad40222011-09-12 16:22:446#include "base/utf_string_conversions.h"
[email protected]67770432011-09-21 18:12:327#include "chrome/browser/net/url_request_mock_util.h"
[email protected]7ad40222011-09-12 16:22:448#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]7ad40222011-09-12 16:22:4412#include "content/browser/net/url_request_failed_dns_job.h"
13#include "content/browser/net/url_request_mock_http_job.h"
[email protected]83ff91c2012-01-05 20:54:1314#include "content/public/browser/web_contents.h"
[email protected]405af132011-12-14 01:17:5715#include "content/test/test_navigation_observer.h"
[email protected]7ad40222011-09-12 16:22:4416
[email protected]631bb742011-11-02 11:29:3917using content::BrowserThread;
[email protected]c5eed492012-01-04 17:07:5018using content::NavigationController;
[email protected]631bb742011-11-02 11:29:3919
[email protected]7ad40222011-09-12 16:22:4420class 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]bb81f382012-01-03 22:45:4440 browser()->GetSelectedWebContents(),
[email protected]7ad40222011-09-12 16:22:4441 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]67770432011-09-21 18:12:3267 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]7ad40222011-09-12 16:22:4474 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]bb81f382012-01-03 22:45:4481 browser()->GetSelectedWebContents(),
[email protected]7ad40222011-09-12 16:22:4482 ASCIIToUTF16(expected_title));
83
84 TestNavigationObserver test_navigation_observer(
[email protected]c5eed492012-01-04 17:07:5085 content::Source<NavigationController>(
[email protected]83ff91c2012-01-05 20:54:1386 &browser()->GetSelectedTabContentsWrapper()->web_contents()->
[email protected]f5fa20e2011-12-21 22:35:5687 GetController()),
[email protected]7ad40222011-09-12 16:22:4488 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]405af132011-12-14 01:17:5797 test_navigation_observer.WaitForObservation(
98 base::Bind(&ui_test_utils::RunMessageLoop),
99 base::Bind(&MessageLoop::Quit,
100 base::Unretained(MessageLoopForUI::current())));
[email protected]7ad40222011-09-12 16:22:44101
102 EXPECT_EQ(title_watcher.WaitAndGetTitle(), ASCIIToUTF16(expected_title));
103 }
104};
105
[email protected]c61d8f32012-01-11 01:33:28106// 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]7ad40222011-09-12 16:22:44112// Test that a DNS error occuring in the main frame redirects to an error page.
[email protected]c61d8f32012-01-11 01:33:28113IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_Basic) {
[email protected]7ad40222011-09-12 16:22:44114 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.
122IN_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]058fce352012-01-09 22:13:39129// 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]7ad40222011-09-12 16:22:44135// Test that a DNS error occuring in the main frame does not result in an
136// additional session history entry.
[email protected]058fce352012-01-09 22:13:39137IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_GoBack2) {
[email protected]7ad40222011-09-12 16:22:44138 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]c61d8f32012-01-11 01:33:28148// 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]7ad40222011-09-12 16:22:44154// Test that a DNS error occuring in the main frame does not result in an
155// additional session history entry.
[email protected]c61d8f32012-01-11 01:33:28156IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_GoBack2AndForward) {
[email protected]7ad40222011-09-12 16:22:44157 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]c61d8f32012-01-11 01:33:28169// See crbug.com/109669
170#if defined(USE_AURA)
[email protected]adbc48a2012-01-11 04:19:05171#define MAYBE_DNSError_GoBack2Forward2 FLAKY_DNSError_GoBack2Forward2
[email protected]c61d8f32012-01-11 01:33:28172#else
[email protected]adbc48a2012-01-11 04:19:05173#define MAYBE_DNSError_GoBack2Forward2 DNSError_GoBack2Forward2
[email protected]c61d8f32012-01-11 01:33:28174#endif
[email protected]7ad40222011-09-12 16:22:44175// Test that a DNS error occuring in the main frame does not result in an
176// additional session history entry.
[email protected]c61d8f32012-01-11 01:33:28177IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_GoBack2Forward2) {
[email protected]7ad40222011-09-12 16:22:44178 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.
192IN_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.
202IN_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.
210IN_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.
218IN_PROC_BROWSER_TEST_F(ErrorPageTest, Page404) {
219 NavigateToURLAndWaitForTitle(
220 URLRequestMockHTTPJob::GetMockUrl(
221 FilePath(FILE_PATH_LITERAL("page404.html"))),
222 "SUCCESS",
223 1);
224}