Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: chrome/browser/net/dns_probe_browsertest.cc

Issue 13270005: Display DNS probe results. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: ...actually add the unittests Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013 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
5 #include "base/bind.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop.h"
8 #include "base/path_service.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/google/google_util.h"
11 #include "chrome/browser/io_thread.h"
12 #include "chrome/browser/net/net_error_tab_helper.h"
13 #include "chrome/browser/net/url_request_mock_util.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/net/net_error_info.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/test/browser_test_utils.h"
23 #include "content/test/net/url_request_failed_job.h"
24 #include "content/test/net/url_request_mock_http_job.h"
25 #include "net/base/net_errors.h"
26 #include "net/url_request/url_request_filter.h"
27 #include "net/url_request/url_request_job.h"
28 #include "net/url_request/url_request_job_factory.h"
29
30 namespace {
31
32 using base::Bind;
33 using base::Callback;
34 using base::Closure;
35 using base::ConstRef;
36 using base::FilePath;
37 using base::Unretained;
38 using chrome_browser_net::NetErrorTabHelper;
39 using chrome_browser_net::SetUrlRequestMocksEnabled;
40 using chrome_common_net::DnsProbeStatus;
41 using content::BrowserThread;
42 using content::URLRequestFailedJob;
43 using content::URLRequestMockHTTPJob;
44 using content::WebContents;
45 using google_util::LinkDoctorBaseURL;
46 using net::NetworkDelegate;
47 using net::URLRequest;
48 using net::URLRequestFilter;
49 using net::URLRequestJob;
50 using net::URLRequestJobFactory;
51 using ui_test_utils::NavigateToURL;
52 using ui_test_utils::NavigateToURLBlockUntilNavigationsComplete;
53
54 class BrokenLinkDoctorProtocolHandlerDelegate {
55 public:
56 virtual int link_doctor_net_error() const = 0;
57 };
58
59 class BrokenLinkDoctorProtocolHandler
60 : public URLRequestJobFactory::ProtocolHandler {
61 public:
62 explicit BrokenLinkDoctorProtocolHandler(
63 BrokenLinkDoctorProtocolHandlerDelegate* delegate)
64 : delegate_(delegate) {}
65
66 virtual URLRequestJob* MaybeCreateJob(
67 URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE {
68 int net_error = delegate_->link_doctor_net_error();
69 if (net_error != net::OK) {
70 return new URLRequestFailedJob(request, network_delegate, net_error);
71 } else {
72 FilePath file_path = GetMockLinkDoctorFilePath();
73 return new URLRequestMockHTTPJob(request, network_delegate, file_path);
74 }
75 }
76
77 private:
78 FilePath GetMockLinkDoctorFilePath() const {
79 FilePath root_http;
80 PathService::Get(chrome::DIR_TEST_DATA, &root_http);
81 return root_http.AppendASCII("mock-link-doctor.html");
82 }
83
84 const BrokenLinkDoctorProtocolHandlerDelegate* delegate_;
85 };
86
87 class DnsProbeBrowserTestIOThreadHelper
88 : public BrokenLinkDoctorProtocolHandlerDelegate {
89 public:
90 DnsProbeBrowserTestIOThreadHelper()
91 : io_thread_(NULL),
92 link_doctor_net_error_(net::OK) {
93 }
94
95 virtual ~DnsProbeBrowserTestIOThreadHelper() {}
96
97 void SetUpOnIOThread(IOThread* io_thread);
98 void CleanUpOnIOThreadAndDeleteHelper();
99
100 void SetProbeResults(DnsProbeRunner::Result system_good_result,
101 DnsProbeRunner::Result public_good_result);
102
103 virtual int link_doctor_net_error() const OVERRIDE {
104 return link_doctor_net_error_;
105 }
106 void set_link_doctor_net_error(int link_doctor_net_error) {
107 link_doctor_net_error_ = link_doctor_net_error;
108 }
109
110 private:
111 IOThread* io_thread_;
112 int link_doctor_net_error_;
113 };
114
115 void DnsProbeBrowserTestIOThreadHelper::SetUpOnIOThread(
116 IOThread* io_thread) {
117 DCHECK(io_thread);
118 DCHECK(!io_thread_);
119
120 io_thread_ = io_thread;
121
122 // SetUrlRequestMocksEnabled clears the filter list and then adds filters
123 // for several things, including the mock link doctor. So, we call it
124 // first, then remove the handler it's added for the mock link doctor and
125 // add our own.
126 SetUrlRequestMocksEnabled(true);
127
128 URLRequestFilter* filter = URLRequestFilter::GetInstance();
129 const GURL link_doctor_base_url = LinkDoctorBaseURL();
130 const std::string host = link_doctor_base_url.host();
131 scoped_ptr<URLRequestJobFactory::ProtocolHandler> handler(
132 new BrokenLinkDoctorProtocolHandler(this));
133 filter->RemoveHostnameHandler("http", host);
134 filter->AddHostnameProtocolHandler("http", host, handler.Pass());
135 }
136
137 void DnsProbeBrowserTestIOThreadHelper::CleanUpOnIOThreadAndDeleteHelper() {
138 DCHECK(io_thread_);
139
140 SetUrlRequestMocksEnabled(false);
141 io_thread_ = NULL;
142
143 delete this;
144 }
145
146 void DnsProbeBrowserTestIOThreadHelper::SetProbeResults(
147 DnsProbeRunner::Result system_good_result,
148 DnsProbeRunner::Result public_good_result) {
149 io_thread_->globals()->dns_probe_service.get()->GetProbeRunnerForTesting()->
150 SetResultsForTesting(system_good_result, public_good_result);
151 }
152
153 class DnsProbeBrowserTest : public InProcessBrowserTest {
154 public:
155 DnsProbeBrowserTest();
156
157 virtual void SetUpOnMainThread() OVERRIDE;
158 virtual void CleanUpOnMainThread() OVERRIDE;
159
160 protected:
161 void SetLinkDoctorBroken(bool broken);
162 void SetProbeResults(DnsProbeRunner::Result system_good_result,
163 DnsProbeRunner::Result public_good_result);
164 void NavigateToDnsError();
165 void NavigateToOtherError();
166
167 bool TitleContains(const std::string& expected);
168 bool PageContains(const std::string& expected);
169
170 private:
171 void SetLinkDoctorNetError(int net_error);
172 void OnProbeStarted();
173
174 DnsProbeBrowserTestIOThreadHelper* helper_;
175 };
176
177 DnsProbeBrowserTest::DnsProbeBrowserTest()
178 : helper_(new DnsProbeBrowserTestIOThreadHelper()) {}
179
180 void DnsProbeBrowserTest::SetUpOnMainThread() {
181 NetErrorTabHelper::set_state_for_testing(
182 NetErrorTabHelper::TESTING_FORCE_ENABLED);
183
184 BrowserThread::PostTask(
185 BrowserThread::IO, FROM_HERE,
186 Bind(&DnsProbeBrowserTestIOThreadHelper::SetUpOnIOThread,
187 Unretained(helper_),
188 g_browser_process->io_thread()));
189 }
190
191 void DnsProbeBrowserTest::CleanUpOnMainThread() {
192 NetErrorTabHelper::set_state_for_testing(
193 NetErrorTabHelper::TESTING_DEFAULT);
194
195 BrowserThread::PostTask(
196 BrowserThread::IO, FROM_HERE,
197 Bind(&DnsProbeBrowserTestIOThreadHelper::CleanUpOnIOThreadAndDeleteHelper,
198 Unretained(helper_)));
199 }
200
201 void DnsProbeBrowserTest::SetLinkDoctorBroken(bool broken) {
202 SetLinkDoctorNetError(broken ? net::ERR_NAME_NOT_RESOLVED : net::OK);
203 }
204
205 // These two functions wait for two navigations because Link Doctor loads two
206 // pages: a blank page, so the user stops seeing the previous page, and then
207 // either the Link Doctor page or a regular error page. We want to wait for
208 // the error page, so we wait for both loads to finish.
209
210 void DnsProbeBrowserTest::NavigateToDnsError() {
211 NavigateToURLBlockUntilNavigationsComplete(
212 browser(),
213 URLRequestFailedJob::GetMockHttpUrl(net::ERR_NAME_NOT_RESOLVED),
214 2);
215 }
216
217 void DnsProbeBrowserTest::NavigateToOtherError() {
218 NavigateToURLBlockUntilNavigationsComplete(
219 browser(),
220 URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_REFUSED),
221 2);
222 }
223
224 void DnsProbeBrowserTest::SetProbeResults(
225 DnsProbeRunner::Result system_good_result,
226 DnsProbeRunner::Result public_good_result) {
227 BrowserThread::PostTask(
228 BrowserThread::IO, FROM_HERE,
229 Bind(&DnsProbeBrowserTestIOThreadHelper::SetProbeResults,
230 Unretained(helper_),
231 system_good_result,
232 public_good_result));
233 }
234
235 bool DnsProbeBrowserTest::TitleContains(const std::string& expected) {
236 std::string title;
237
238 WebContents* contents =
239 browser()->tab_strip_model()->GetActiveWebContents();
240
241 bool rv = content::ExecuteScriptAndExtractString(
242 contents,
243 "domAutomationController.send(document.title);",
244 &title);
245 if (!rv)
246 return false;
247
248 return title.find(expected) != std::string::npos;
249 }
250
251 bool DnsProbeBrowserTest::PageContains(const std::string& expected) {
252 std::string text_content;
253
254 bool rv = content::ExecuteScriptAndExtractString(
255 browser()->tab_strip_model()->GetActiveWebContents(),
256 "domAutomationController.send(document.body.textContent);",
257 &text_content);
258 if (!rv)
259 return false;
260
261 return text_content.find(expected) != std::string::npos;
262 }
263
264 void DnsProbeBrowserTest::SetLinkDoctorNetError(int net_error) {
265 BrowserThread::PostTask(
266 BrowserThread::IO, FROM_HERE,
267 Bind(&DnsProbeBrowserTestIOThreadHelper::set_link_doctor_net_error,
268 Unretained(helper_),
269 net_error));
270 }
271
272 IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, OtherErrorWithLinkDoctor) {
273 SetLinkDoctorBroken(false);
274
275 NavigateToOtherError();
276 EXPECT_TRUE(TitleContains("Mock Link Doctor"));
277 }
278
279 IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, OtherErrorWithoutLinkDoctor) {
280 SetLinkDoctorBroken(true);
281
282 NavigateToOtherError();
283 EXPECT_TRUE(PageContains("CONNECTION_REFUSED"));
284 }
285
286 IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, NxdomainWithLinkDoctor) {
287 SetLinkDoctorBroken(false);
288 SetProbeResults(DnsProbeRunner::CORRECT, DnsProbeRunner::CORRECT);
289
290 NavigateToDnsError();
291 EXPECT_TRUE(TitleContains("Mock Link Doctor"));
292 }
293
294 IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, NoInternetWithoutLinkDoctor) {
295 SetLinkDoctorBroken(true);
296 SetProbeResults(DnsProbeRunner::UNREACHABLE, DnsProbeRunner::UNREACHABLE);
297
298 NavigateToDnsError();
299 // EXPECT_TRUE(PageContains("DNS_PROBE_STARTED"));
300 EXPECT_TRUE(PageContains("DNS_PROBE_FINISHED_NO_INTERNET"));
301 }
302
303 /* static const FilePath::CharType kIframeDnsErrorHtmlName[] =
304 FILE_PATH_LITERAL("iframe_dns_error.html");
305
306 IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, NoProbeInSubframe) {
307 SetLinkDoctorBroken(false);
308
309 NavigateToURL(
310 browser(),
311 URLRequestMockHTTPJob::GetMockUrl(FilePath(kIframeDnsErrorHtmlName)));
312 EXPECT_EQ(0, probe_count());
313 } */
314
315 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698