blob: 8e2b9b456910cb81a3690391cda36e44ce051373 [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]4a63e242011-12-12 15:23:0814#include "content/browser/tab_contents/tab_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;
18
[email protected]7ad40222011-09-12 16:22:4419class ErrorPageTest : public InProcessBrowserTest {
20 public:
21 enum HistoryNavigationDirection {
22 HISTORY_NAVIGATE_BACK,
23 HISTORY_NAVIGATE_FORWARD,
24 };
25
26 // Navigates the active tab to a mock url created for the file at |file_path|.
27 void NavigateToFileURL(const FilePath::StringType& file_path) {
28 ui_test_utils::NavigateToURL(
29 browser(),
30 URLRequestMockHTTPJob::GetMockUrl(FilePath(file_path)));
31 }
32
33 // Navigates to the given URL and waits for |num_navigations| to occur, and
34 // the title to change to |expected_title|.
35 void NavigateToURLAndWaitForTitle(const GURL& url,
36 const std::string& expected_title,
37 int num_navigations) {
38 ui_test_utils::TitleWatcher title_watcher(
39 browser()->GetSelectedTabContents(),
40 ASCIIToUTF16(expected_title));
41
42 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
43 browser(), url, num_navigations);
44
45 EXPECT_EQ(title_watcher.WaitAndGetTitle(), ASCIIToUTF16(expected_title));
46 }
47
48 // Navigates back in the history and waits for |num_navigations| to occur, and
49 // the title to change to |expected_title|.
50 void GoBackAndWaitForTitle(const std::string& expected_title,
51 int num_navigations) {
52 NavigateHistoryAndWaitForTitle(expected_title,
53 num_navigations,
54 HISTORY_NAVIGATE_BACK);
55 }
56
57 // Navigates forward in the history and waits for |num_navigations| to occur,
58 // and the title to change to |expected_title|.
59 void GoForwardAndWaitForTitle(const std::string& expected_title,
60 int num_navigations) {
61 NavigateHistoryAndWaitForTitle(expected_title,
62 num_navigations,
63 HISTORY_NAVIGATE_FORWARD);
64 }
65
[email protected]67770432011-09-21 18:12:3266 protected:
67 void SetUpOnMainThread() OVERRIDE {
68 BrowserThread::PostTask(
69 BrowserThread::IO, FROM_HERE,
70 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
71 }
72
[email protected]7ad40222011-09-12 16:22:4473 private:
74 // Navigates the browser the indicated direction in the history and waits for
75 // |num_navigations| to occur and the title to change to |expected_title|.
76 void NavigateHistoryAndWaitForTitle(const std::string& expected_title,
77 int num_navigations,
78 HistoryNavigationDirection direction) {
79 ui_test_utils::TitleWatcher title_watcher(
80 browser()->GetSelectedTabContents(),
81 ASCIIToUTF16(expected_title));
82
83 TestNavigationObserver test_navigation_observer(
[email protected]6c2381d2011-10-19 02:52:5384 content::Source<NavigationController>(
[email protected]4a63e242011-12-12 15:23:0885 &browser()->GetSelectedTabContentsWrapper()->tab_contents()->
86 controller()),
[email protected]7ad40222011-09-12 16:22:4487 NULL,
88 num_navigations);
89 if (direction == HISTORY_NAVIGATE_BACK) {
90 browser()->GoBack(CURRENT_TAB);
91 } else if (direction == HISTORY_NAVIGATE_FORWARD) {
92 browser()->GoForward(CURRENT_TAB);
93 } else {
94 FAIL();
95 }
[email protected]405af132011-12-14 01:17:5796 test_navigation_observer.WaitForObservation(
97 base::Bind(&ui_test_utils::RunMessageLoop),
98 base::Bind(&MessageLoop::Quit,
99 base::Unretained(MessageLoopForUI::current())));
[email protected]7ad40222011-09-12 16:22:44100
101 EXPECT_EQ(title_watcher.WaitAndGetTitle(), ASCIIToUTF16(expected_title));
102 }
103};
104
105// Test that a DNS error occuring in the main frame redirects to an error page.
106IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_Basic) {
107 GURL test_url(URLRequestFailedDnsJob::kTestUrl);
108 // The first navigation should fail, and the second one should be the error
109 // page.
110 NavigateToURLAndWaitForTitle(test_url, "Mock Link Doctor", 2);
111}
112
113// Test that a DNS error occuring in the main frame does not result in an
114// additional session history entry.
115IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_GoBack1) {
116 GURL test_url(URLRequestFailedDnsJob::kTestUrl);
117 NavigateToFileURL(FILE_PATH_LITERAL("title2.html"));
118 NavigateToURLAndWaitForTitle(test_url, "Mock Link Doctor", 2);
119 GoBackAndWaitForTitle("Title Of Awesomeness", 1);
120}
121
122// Test that a DNS error occuring in the main frame does not result in an
123// additional session history entry.
124IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_GoBack2) {
125 GURL test_url(URLRequestFailedDnsJob::kTestUrl);
126 NavigateToFileURL(FILE_PATH_LITERAL("title2.html"));
127
128 NavigateToURLAndWaitForTitle(test_url, "Mock Link Doctor", 2);
129 NavigateToFileURL(FILE_PATH_LITERAL("title3.html"));
130
131 GoBackAndWaitForTitle("Mock Link Doctor", 2);
132 GoBackAndWaitForTitle("Title Of Awesomeness", 1);
133}
134
135// Test that a DNS error occuring in the main frame does not result in an
136// additional session history entry.
137IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_GoBack2AndForward) {
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 GoForwardAndWaitForTitle("Mock Link Doctor", 2);
148}
149
150// Test that a DNS error occuring in the main frame does not result in an
151// additional session history entry.
152IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_GoBack2Forward2) {
153 GURL test_url(URLRequestFailedDnsJob::kTestUrl);
154 NavigateToFileURL(FILE_PATH_LITERAL("title3.html"));
155
156 NavigateToURLAndWaitForTitle(test_url, "Mock Link Doctor", 2);
157 NavigateToFileURL(FILE_PATH_LITERAL("title2.html"));
158
159 GoBackAndWaitForTitle("Mock Link Doctor", 2);
160 GoBackAndWaitForTitle("Title Of More Awesomeness", 1);
161
162 GoForwardAndWaitForTitle("Mock Link Doctor", 2);
163 GoForwardAndWaitForTitle("Title Of Awesomeness", 1);
164}
165
166// Test that a DNS error occuring in an iframe.
167IN_PROC_BROWSER_TEST_F(ErrorPageTest, IFrameDNSError_Basic) {
168 NavigateToURLAndWaitForTitle(
169 URLRequestMockHTTPJob::GetMockUrl(
170 FilePath(FILE_PATH_LITERAL("iframe_dns_error.html"))),
171 "Blah",
172 1);
173}
174
175// Test that a DNS error occuring in an iframe does not result in an
176// additional session history entry.
177IN_PROC_BROWSER_TEST_F(ErrorPageTest, IFrameDNSError_GoBack) {
178 NavigateToFileURL(FILE_PATH_LITERAL("title2.html"));
179 NavigateToFileURL(FILE_PATH_LITERAL("iframe_dns_error.html"));
180 GoBackAndWaitForTitle("Title Of Awesomeness", 1);
181}
182
183// Test that a DNS error occuring in an iframe does not result in an
184// additional session history entry.
185IN_PROC_BROWSER_TEST_F(ErrorPageTest, IFrameDNSError_GoBackAndForward) {
186 NavigateToFileURL(FILE_PATH_LITERAL("title2.html"));
187 NavigateToFileURL(FILE_PATH_LITERAL("iframe_dns_error.html"));
188 GoBackAndWaitForTitle("Title Of Awesomeness", 1);
189 GoForwardAndWaitForTitle("Blah", 1);
190}
191
192// Checks that the Link Doctor is not loaded when we receive an actual 404 page.
193IN_PROC_BROWSER_TEST_F(ErrorPageTest, Page404) {
194 NavigateToURLAndWaitForTitle(
195 URLRequestMockHTTPJob::GetMockUrl(
196 FilePath(FILE_PATH_LITERAL("page404.html"))),
197 "SUCCESS",
198 1);
199}