OLD | NEW |
---|---|
(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/dns_probe_test_util.h" | |
13 #include "chrome/browser/net/net_error_tab_helper.h" | |
14 #include "chrome/browser/net/url_request_mock_util.h" | |
15 #include "chrome/browser/ui/browser.h" | |
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
17 #include "chrome/common/chrome_paths.h" | |
18 #include "chrome/common/net/net_error_info.h" | |
19 #include "chrome/test/base/in_process_browser_test.h" | |
20 #include "chrome/test/base/ui_test_utils.h" | |
21 #include "content/public/browser/browser_thread.h" | |
22 #include "content/public/browser/web_contents.h" | |
23 #include "content/public/test/browser_test_utils.h" | |
24 #include "content/test/net/url_request_failed_job.h" | |
25 #include "content/test/net/url_request_mock_http_job.h" | |
26 #include "net/base/net_errors.h" | |
27 #include "net/dns/dns_test_util.h" | |
28 #include "net/url_request/url_request_filter.h" | |
29 #include "net/url_request/url_request_job.h" | |
30 #include "net/url_request/url_request_job_factory.h" | |
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_common_net::DnsProbeStatus; | |
39 using content::BrowserThread; | |
40 using content::URLRequestFailedJob; | |
41 using content::URLRequestMockHTTPJob; | |
42 using content::WebContents; | |
43 using google_util::LinkDoctorBaseURL; | |
44 using net::MockDnsClientRule; | |
45 using net::NetworkDelegate; | |
46 using net::URLRequest; | |
47 using net::URLRequestFilter; | |
48 using net::URLRequestJob; | |
49 using net::URLRequestJobFactory; | |
50 using ui_test_utils::NavigateToURL; | |
51 using ui_test_utils::NavigateToURLBlockUntilNavigationsComplete; | |
52 | |
53 namespace chrome_browser_net { | |
54 | |
55 namespace { | |
56 | |
57 class BrokenLinkDoctorProtocolHandlerDelegate { | |
mmenke
2013/06/20 15:47:36
Do we really need a delegate class? Seems like we
Deprecated (see juliatuttle)
2013/06/21 19:55:52
It's what keeps BrokenLinkDoctorProtocolHandler an
| |
58 public: | |
59 virtual int link_doctor_net_error() const = 0; | |
60 }; | |
61 | |
62 class BrokenLinkDoctorProtocolHandler | |
63 : public URLRequestJobFactory::ProtocolHandler { | |
64 public: | |
65 explicit BrokenLinkDoctorProtocolHandler( | |
66 BrokenLinkDoctorProtocolHandlerDelegate* delegate) | |
67 : delegate_(delegate) {} | |
68 | |
69 virtual URLRequestJob* MaybeCreateJob( | |
70 URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE { | |
71 int net_error = delegate_->link_doctor_net_error(); | |
72 if (net_error != net::OK) { | |
73 return new URLRequestFailedJob(request, network_delegate, net_error); | |
74 } else { | |
75 FilePath file_path = GetMockLinkDoctorFilePath(); | |
76 return new URLRequestMockHTTPJob(request, network_delegate, file_path); | |
77 } | |
78 } | |
79 | |
80 private: | |
81 FilePath GetMockLinkDoctorFilePath() const { | |
mmenke
2013/06/20 15:47:36
Suggest making this static, just to indicate it do
Deprecated (see juliatuttle)
2013/06/21 19:55:52
Done.
| |
82 FilePath root_http; | |
83 PathService::Get(chrome::DIR_TEST_DATA, &root_http); | |
84 return root_http.AppendASCII("mock-link-doctor.html"); | |
85 } | |
86 | |
87 const BrokenLinkDoctorProtocolHandlerDelegate* delegate_; | |
88 }; | |
89 | |
90 class DnsProbeBrowserTestIOThreadHelper | |
91 : public BrokenLinkDoctorProtocolHandlerDelegate { | |
92 public: | |
93 DnsProbeBrowserTestIOThreadHelper() | |
94 : io_thread_(NULL), | |
mmenke
2013/06/20 15:47:36
I'm assuming io_thread_ hasn't been created yet by
Deprecated (see juliatuttle)
2013/06/21 19:55:52
Done.
| |
95 link_doctor_net_error_(net::OK) { | |
96 } | |
97 | |
98 virtual ~DnsProbeBrowserTestIOThreadHelper() {} | |
mmenke
2013/06/20 15:47:36
Suggest CHECKs on all these functions (Including s
Deprecated (see juliatuttle)
2013/06/21 19:55:52
Done.
| |
99 | |
100 void SetUpOnIOThread(IOThread* io_thread); | |
101 void CleanUpOnIOThreadAndDeleteHelper(); | |
102 | |
103 void SetRules(MockDnsClientRule::Result system_good_result, | |
mmenke
2013/06/20 15:47:36
Think this function name could use improvement.
Deprecated (see juliatuttle)
2013/06/21 19:55:52
Done.
| |
104 MockDnsClientRule::Result public_good_result); | |
105 | |
106 virtual int link_doctor_net_error() const OVERRIDE { | |
107 return link_doctor_net_error_; | |
108 } | |
109 void set_link_doctor_net_error(int link_doctor_net_error) { | |
110 link_doctor_net_error_ = link_doctor_net_error; | |
111 } | |
112 | |
113 private: | |
114 IOThread* io_thread_; | |
115 int link_doctor_net_error_; | |
116 }; | |
117 | |
118 void DnsProbeBrowserTestIOThreadHelper::SetUpOnIOThread( | |
119 IOThread* io_thread) { | |
120 DCHECK(io_thread); | |
121 DCHECK(!io_thread_); | |
122 | |
123 io_thread_ = io_thread; | |
124 | |
125 // SetUrlRequestMocksEnabled clears the filter list and then adds filters | |
126 // for several things, including the mock link doctor. So, we call it | |
127 // first, then remove the handler it's added for the mock link doctor and | |
128 // add our own. | |
129 SetUrlRequestMocksEnabled(true); | |
mmenke
2013/06/20 15:47:36
Think it may just be simpler to install only the U
Deprecated (see juliatuttle)
2013/06/21 19:55:52
Done.
| |
130 | |
131 URLRequestFilter* filter = URLRequestFilter::GetInstance(); | |
132 const GURL link_doctor_base_url = LinkDoctorBaseURL(); | |
133 const std::string host = link_doctor_base_url.host(); | |
134 scoped_ptr<URLRequestJobFactory::ProtocolHandler> handler( | |
135 new BrokenLinkDoctorProtocolHandler(this)); | |
136 filter->RemoveHostnameHandler("http", host); | |
137 filter->AddHostnameProtocolHandler("http", host, handler.Pass()); | |
138 } | |
139 | |
140 void DnsProbeBrowserTestIOThreadHelper::CleanUpOnIOThreadAndDeleteHelper() { | |
141 DCHECK(io_thread_); | |
142 | |
143 SetUrlRequestMocksEnabled(false); | |
144 io_thread_ = NULL; | |
mmenke
2013/06/20 15:47:36
Should also call filter->ClearHandlers();
As a si
Deprecated (see juliatuttle)
2013/06/21 19:55:52
Done.
| |
145 | |
146 delete this; | |
147 } | |
148 | |
149 void DnsProbeBrowserTestIOThreadHelper::SetRules( | |
150 MockDnsClientRule::Result system_good_result, | |
151 MockDnsClientRule::Result public_good_result) { | |
152 DnsProbeService* service = io_thread_->globals()->dns_probe_service.get(); | |
153 service->SetSystemClientForTesting( | |
154 CreateMockDnsClientForProbes(system_good_result)); | |
155 service->SetPublicClientForTesting( | |
156 CreateMockDnsClientForProbes(public_good_result)); | |
157 } | |
158 | |
159 class DnsProbeBrowserTest : public InProcessBrowserTest { | |
160 public: | |
161 DnsProbeBrowserTest(); | |
162 | |
163 virtual void SetUpOnMainThread() OVERRIDE; | |
164 virtual void CleanUpOnMainThread() OVERRIDE; | |
165 | |
166 protected: | |
167 void SetLinkDoctorBroken(bool broken); | |
168 void SetRules(MockDnsClientRule::Result system_good_result, | |
169 MockDnsClientRule::Result public_good_result); | |
170 void NavigateToDnsError(); | |
171 void NavigateToOtherError(); | |
172 | |
173 bool TitleContains(const std::string& expected); | |
174 bool PageContains(const std::string& expected); | |
175 | |
176 private: | |
177 void SetLinkDoctorNetError(int net_error); | |
178 void OnProbeStarted(); | |
179 | |
180 DnsProbeBrowserTestIOThreadHelper* helper_; | |
181 }; | |
182 | |
183 DnsProbeBrowserTest::DnsProbeBrowserTest() | |
184 : helper_(new DnsProbeBrowserTestIOThreadHelper()) {} | |
185 | |
186 void DnsProbeBrowserTest::SetUpOnMainThread() { | |
187 NetErrorTabHelper::set_state_for_testing( | |
188 NetErrorTabHelper::TESTING_FORCE_ENABLED); | |
189 | |
190 BrowserThread::PostTask( | |
191 BrowserThread::IO, FROM_HERE, | |
192 Bind(&DnsProbeBrowserTestIOThreadHelper::SetUpOnIOThread, | |
193 Unretained(helper_), | |
194 g_browser_process->io_thread())); | |
195 } | |
196 | |
197 void DnsProbeBrowserTest::CleanUpOnMainThread() { | |
198 NetErrorTabHelper::set_state_for_testing( | |
199 NetErrorTabHelper::TESTING_DEFAULT); | |
200 | |
201 BrowserThread::PostTask( | |
202 BrowserThread::IO, FROM_HERE, | |
203 Bind(&DnsProbeBrowserTestIOThreadHelper::CleanUpOnIOThreadAndDeleteHelper, | |
204 Unretained(helper_))); | |
mmenke
2013/06/20 15:47:36
nit: Suggest reversing the order of setup. Doesn
Deprecated (see juliatuttle)
2013/06/21 19:55:52
Done.
| |
205 } | |
206 | |
207 void DnsProbeBrowserTest::SetLinkDoctorBroken(bool broken) { | |
mmenke
2013/06/20 15:47:36
Do we really need both this and SetLinkDoctorNetEr
Deprecated (see juliatuttle)
2013/06/21 19:55:52
No.
| |
208 SetLinkDoctorNetError(broken ? net::ERR_NAME_NOT_RESOLVED : net::OK); | |
209 } | |
210 | |
211 // These two functions wait for two navigations because Link Doctor loads two | |
212 // pages: a blank page, so the user stops seeing the previous page, and then | |
213 // either the Link Doctor page or a regular error page. We want to wait for | |
214 // the error page, so we wait for both loads to finish. | |
215 | |
216 void DnsProbeBrowserTest::NavigateToDnsError() { | |
217 NavigateToURLBlockUntilNavigationsComplete( | |
218 browser(), | |
219 URLRequestFailedJob::GetMockHttpUrl(net::ERR_NAME_NOT_RESOLVED), | |
220 2); | |
221 } | |
222 | |
223 void DnsProbeBrowserTest::NavigateToOtherError() { | |
224 NavigateToURLBlockUntilNavigationsComplete( | |
225 browser(), | |
226 URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_REFUSED), | |
227 2); | |
228 } | |
229 | |
230 void DnsProbeBrowserTest::SetRules( | |
231 MockDnsClientRule::Result system_good_result, | |
232 MockDnsClientRule::Result public_good_result) { | |
233 BrowserThread::PostTask( | |
234 BrowserThread::IO, FROM_HERE, | |
235 Bind(&DnsProbeBrowserTestIOThreadHelper::SetRules, | |
236 Unretained(helper_), | |
237 system_good_result, | |
238 public_good_result)); | |
239 } | |
240 | |
241 bool DnsProbeBrowserTest::TitleContains(const std::string& expected) { | |
mmenke
2013/06/20 15:47:36
Think it's worth mentioning why this is used inste
Deprecated (see juliatuttle)
2013/06/21 19:55:52
Done.
| |
242 std::string title; | |
243 | |
244 WebContents* contents = | |
245 browser()->tab_strip_model()->GetActiveWebContents(); | |
246 | |
247 bool rv = content::ExecuteScriptAndExtractString( | |
248 contents, | |
249 "domAutomationController.send(document.title);", | |
250 &title); | |
251 if (!rv) | |
252 return false; | |
253 | |
254 return title.find(expected) != std::string::npos; | |
mmenke
2013/06/20 15:47:36
Is there a reason for not testing for an exact mat
Deprecated (see juliatuttle)
2013/06/21 19:55:52
No! Done.
| |
255 } | |
256 | |
257 bool DnsProbeBrowserTest::PageContains(const std::string& expected) { | |
258 std::string text_content; | |
259 | |
260 bool rv = content::ExecuteScriptAndExtractString( | |
261 browser()->tab_strip_model()->GetActiveWebContents(), | |
262 "domAutomationController.send(document.body.textContent);", | |
263 &text_content); | |
264 if (!rv) | |
265 return false; | |
266 | |
267 return text_content.find(expected) != std::string::npos; | |
268 } | |
269 | |
270 void DnsProbeBrowserTest::SetLinkDoctorNetError(int net_error) { | |
271 BrowserThread::PostTask( | |
272 BrowserThread::IO, FROM_HERE, | |
273 Bind(&DnsProbeBrowserTestIOThreadHelper::set_link_doctor_net_error, | |
274 Unretained(helper_), | |
275 net_error)); | |
276 } | |
277 | |
278 IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, OtherErrorWithLinkDoctor) { | |
279 SetLinkDoctorBroken(false); | |
280 | |
281 NavigateToOtherError(); | |
282 EXPECT_TRUE(TitleContains("Mock Link Doctor")); | |
mmenke
2013/06/20 15:47:36
Suggest adding a way to check if probes are queued
Deprecated (see juliatuttle)
2013/06/21 19:55:52
Done.
| |
283 } | |
284 | |
285 IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, OtherErrorWithoutLinkDoctor) { | |
286 SetLinkDoctorBroken(true); | |
287 | |
288 NavigateToOtherError(); | |
289 EXPECT_TRUE(PageContains("CONNECTION_REFUSED")); | |
290 } | |
291 | |
292 IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, NxdomainWithLinkDoctor) { | |
293 SetLinkDoctorBroken(false); | |
294 SetRules(MockDnsClientRule::OK, MockDnsClientRule::OK); | |
295 | |
296 NavigateToDnsError(); | |
297 EXPECT_TRUE(TitleContains("Mock Link Doctor")); | |
298 } | |
299 | |
300 IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, NoInternetWithoutLinkDoctor) { | |
301 SetLinkDoctorBroken(true); | |
302 SetRules(MockDnsClientRule::TIMEOUT, MockDnsClientRule::TIMEOUT); | |
303 | |
304 NavigateToDnsError(); | |
305 // EXPECT_TRUE(PageContains("DNS_PROBE_STARTED")); | |
306 EXPECT_TRUE(PageContains("DNS_PROBE_FINISHED_NO_INTERNET")); | |
mmenke
2013/06/20 15:47:36
Why are we guaranteed the probes have completed by
Deprecated (see juliatuttle)
2013/06/21 19:55:52
We're not. Fixed.
| |
307 } | |
308 | |
309 /* static const FilePath::CharType kIframeDnsErrorHtmlName[] = | |
310 FILE_PATH_LITERAL("iframe_dns_error.html"); | |
311 | |
312 IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, NoProbeInSubframe) { | |
313 SetLinkDoctorBroken(false); | |
314 | |
315 NavigateToURL( | |
316 browser(), | |
317 URLRequestMockHTTPJob::GetMockUrl(FilePath(kIframeDnsErrorHtmlName))); | |
318 EXPECT_EQ(0, probe_count()); | |
319 } */ | |
mmenke
2013/06/20 15:47:36
You plan to enable this?
Deprecated (see juliatuttle)
2013/06/21 19:55:52
Done.
| |
320 | |
mmenke
2013/06/20 15:47:36
Other test suggestion: Check the case that DNS pr
Deprecated (see juliatuttle)
2013/06/21 19:55:52
Done.
| |
321 } // namespace | |
322 | |
323 } // namespace chrome_browser_net | |
OLD | NEW |