blob: 8729f8f8732e6b2a8a199f06bb1ebb69670c3f24 [file] [log] [blame]
[email protected]2ea1efe2013-07-17 05:23:131// Copyright 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
dcheng4e7c0422016-04-14 00:59:055#include <memory>
[email protected]3a621992013-11-27 19:51:476#include <set>
7
[email protected]2ea1efe2013-07-17 05:23:138#include "base/bind.h"
[email protected]b292d762013-07-17 21:22:099#include "base/message_loop/message_loop.h"
[email protected]2ea1efe2013-07-17 05:23:1310#include "base/path_service.h"
[email protected]3a621992013-11-27 19:51:4711#include "base/run_loop.h"
[email protected]2ea1efe2013-07-17 05:23:1312#include "base/threading/thread_restrictions.h"
13#include "chrome/browser/browser_process.h"
[email protected]2ea1efe2013-07-17 05:23:1314#include "chrome/browser/io_thread.h"
15#include "chrome/browser/net/dns_probe_test_util.h"
16#include "chrome/browser/net/net_error_tab_helper.h"
17#include "chrome/browser/net/url_request_mock_util.h"
[email protected]d8574df2013-11-22 00:11:5218#include "chrome/browser/profiles/profile.h"
[email protected]2ea1efe2013-07-17 05:23:1319#include "chrome/browser/ui/browser.h"
[email protected]3a621992013-11-27 19:51:4720#include "chrome/browser/ui/browser_commands.h"
[email protected]2ea1efe2013-07-17 05:23:1321#include "chrome/browser/ui/tabs/tab_strip_model.h"
22#include "chrome/common/chrome_paths.h"
[email protected]d8574df2013-11-22 00:11:5223#include "chrome/common/pref_names.h"
[email protected]2ea1efe2013-07-17 05:23:1324#include "chrome/test/base/in_process_browser_test.h"
25#include "chrome/test/base/ui_test_utils.h"
hashimoto9b160e22014-10-15 03:56:5326#include "components/error_page/common/net_error_info.h"
[email protected]8e44a5b02014-06-19 19:03:2427#include "components/google/core/browser/google_util.h"
brettwb1fc1b82016-02-02 00:19:0828#include "components/prefs/pref_service.h"
[email protected]2ea1efe2013-07-17 05:23:1329#include "content/public/browser/browser_thread.h"
30#include "content/public/browser/web_contents.h"
31#include "content/public/test/browser_test_utils.h"
[email protected]3a621992013-11-27 19:51:4732#include "content/public/test/test_navigation_observer.h"
[email protected]2ea1efe2013-07-17 05:23:1333#include "net/base/net_errors.h"
34#include "net/dns/dns_test_util.h"
xunjieli2906f152014-09-12 00:08:2335#include "net/test/url_request/url_request_failed_job.h"
xunjieli0332c192014-09-10 23:23:3136#include "net/test/url_request/url_request_mock_http_job.h"
[email protected]2ea1efe2013-07-17 05:23:1337#include "net/url_request/url_request_filter.h"
[email protected]c74b6462014-06-06 17:45:5538#include "net/url_request/url_request_interceptor.h"
[email protected]2ea1efe2013-07-17 05:23:1339#include "net/url_request/url_request_job.h"
[email protected]2ea1efe2013-07-17 05:23:1340
41using base::Bind;
42using base::Callback;
43using base::Closure;
44using base::ConstRef;
45using base::FilePath;
46using base::MessageLoop;
47using base::Unretained;
[email protected]2ea1efe2013-07-17 05:23:1348using content::BrowserThread;
[email protected]2ea1efe2013-07-17 05:23:1349using content::WebContents;
thestig4b0b01652015-08-18 20:36:3950using error_page::DnsProbeStatus;
[email protected]2ea1efe2013-07-17 05:23:1351using google_util::LinkDoctorBaseURL;
52using net::MockDnsClientRule;
53using net::NetworkDelegate;
54using net::URLRequest;
thestig4b0b01652015-08-18 20:36:3955using net::URLRequestFailedJob;
[email protected]2ea1efe2013-07-17 05:23:1356using net::URLRequestFilter;
[email protected]c74b6462014-06-06 17:45:5557using net::URLRequestInterceptor;
[email protected]2ea1efe2013-07-17 05:23:1358using net::URLRequestJob;
thestig4b0b01652015-08-18 20:36:3959using net::URLRequestMockHTTPJob;
[email protected]2ea1efe2013-07-17 05:23:1360using ui_test_utils::NavigateToURL;
61using ui_test_utils::NavigateToURLBlockUntilNavigationsComplete;
62
63namespace chrome_browser_net {
64
65namespace {
66
[email protected]3a621992013-11-27 19:51:4767// Postable function to run a Closure on the UI thread. Since
68// BrowserThread::PostTask returns a bool, it can't directly be posted to
69// another thread.
70void RunClosureOnUIThread(const base::Closure& closure) {
71 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure);
72}
73
[email protected]2ea1efe2013-07-17 05:23:1374// Wraps DnsProbeService and delays callbacks until someone calls
75// CallDelayedCallbacks. This allows the DnsProbeBrowserTest to enforce a
76// stricter ordering of events.
77class DelayingDnsProbeService : public DnsProbeService {
78 public:
79 DelayingDnsProbeService() {}
80
dchengfce29ad2014-10-23 03:47:4781 ~DelayingDnsProbeService() override { EXPECT_TRUE(delayed_probes_.empty()); }
[email protected]2ea1efe2013-07-17 05:23:1382
dchengfce29ad2014-10-23 03:47:4783 void ProbeDns(const ProbeCallback& callback) override {
[email protected]2ea1efe2013-07-17 05:23:1384 delayed_probes_.push_back(callback);
85 }
86
87 void StartDelayedProbes() {
88 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
89
90 std::vector<ProbeCallback> probes;
91 probes.swap(delayed_probes_);
92
93 for (std::vector<ProbeCallback>::const_iterator i = probes.begin();
94 i != probes.end(); ++i) {
95 DnsProbeService::ProbeDns(*i);
96 }
97 }
98
99 int delayed_probe_count() const {
100 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
101 return delayed_probes_.size();
102 }
103
104 private:
105 std::vector<ProbeCallback> delayed_probes_;
106};
107
108FilePath GetMockLinkDoctorFilePath() {
109 FilePath root_http;
110 PathService::Get(chrome::DIR_TEST_DATA, &root_http);
[email protected]26bd3893c2014-04-15 18:59:58111 return root_http.AppendASCII("mock-link-doctor.json");
[email protected]2ea1efe2013-07-17 05:23:13112}
113
[email protected]3a621992013-11-27 19:51:47114// A request that can be delayed until Resume() is called. Can also run a
115// callback if destroyed without being resumed. Resume can be called either
116// before or after a the request is started.
117class DelayableRequest {
118 public:
119 // Called by a DelayableRequest if it was set to be delayed, and has been
120 // destroyed without Undelay being called.
121 typedef base::Callback<void(DelayableRequest* request)> DestructionCallback;
122
123 virtual void Resume() = 0;
124
125 protected:
126 virtual ~DelayableRequest() {}
127};
128
129class DelayableURLRequestFailedJob : public URLRequestFailedJob,
130 public DelayableRequest {
131 public:
132 // |destruction_callback| is only called if a delayed request is destroyed
133 // without being resumed.
134 DelayableURLRequestFailedJob(net::URLRequest* request,
135 net::NetworkDelegate* network_delegate,
136 int net_error,
137 bool should_delay,
138 const DestructionCallback& destruction_callback)
139 : URLRequestFailedJob(request, network_delegate, net_error),
140 should_delay_(should_delay),
141 start_delayed_(false),
142 destruction_callback_(destruction_callback) {}
143
dchengfce29ad2014-10-23 03:47:47144 void Start() override {
[email protected]3a621992013-11-27 19:51:47145 if (should_delay_) {
146 DCHECK(!start_delayed_);
147 start_delayed_ = true;
148 return;
149 }
150 URLRequestFailedJob::Start();
151 }
152
dchengfce29ad2014-10-23 03:47:47153 void Resume() override {
[email protected]3a621992013-11-27 19:51:47154 DCHECK(should_delay_);
155 should_delay_ = false;
156 if (start_delayed_) {
157 start_delayed_ = false;
158 Start();
159 }
160 }
161
162 private:
dchengfce29ad2014-10-23 03:47:47163 ~DelayableURLRequestFailedJob() override {
[email protected]3a621992013-11-27 19:51:47164 if (should_delay_)
165 destruction_callback_.Run(this);
166 }
167
168 bool should_delay_;
169 bool start_delayed_;
170 const DestructionCallback destruction_callback_;
171};
172
173class DelayableURLRequestMockHTTPJob : public URLRequestMockHTTPJob,
174 public DelayableRequest {
175 public:
176 DelayableURLRequestMockHTTPJob(
177 net::URLRequest* request,
178 net::NetworkDelegate* network_delegate,
179 const base::FilePath& file_path,
180 bool should_delay,
181 const DestructionCallback& destruction_callback)
xunjieli0332c192014-09-10 23:23:31182 : URLRequestMockHTTPJob(
183 request,
184 network_delegate,
185 file_path,
186 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
187 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)),
[email protected]3a621992013-11-27 19:51:47188 should_delay_(should_delay),
189 start_delayed_(false),
190 destruction_callback_(destruction_callback) {}
191
dchengfce29ad2014-10-23 03:47:47192 void Start() override {
[email protected]3a621992013-11-27 19:51:47193 if (should_delay_) {
194 DCHECK(!start_delayed_);
195 start_delayed_ = true;
196 return;
197 }
198 URLRequestMockHTTPJob::Start();
199 }
200
dchengfce29ad2014-10-23 03:47:47201 void Resume() override {
[email protected]3a621992013-11-27 19:51:47202 DCHECK(should_delay_);
203 should_delay_ = false;
204 if (start_delayed_) {
205 start_delayed_ = false;
206 Start();
207 }
208 }
209
210 private:
dchengfce29ad2014-10-23 03:47:47211 ~DelayableURLRequestMockHTTPJob() override {
[email protected]3a621992013-11-27 19:51:47212 if (should_delay_)
213 destruction_callback_.Run(this);
214 }
215
216 bool should_delay_;
217 bool start_delayed_;
218 const DestructionCallback destruction_callback_;
219};
220
[email protected]c74b6462014-06-06 17:45:55221// Interceptor for navigation correction requests. Can cause requests to
[email protected]26bd3893c2014-04-15 18:59:58222// fail with an error, and/or delay a request until a test allows to continue.
223// Also can run a callback when a delayed request is cancelled.
[email protected]c74b6462014-06-06 17:45:55224class BreakableCorrectionInterceptor : public URLRequestInterceptor {
[email protected]2ea1efe2013-07-17 05:23:13225 public:
[email protected]c74b6462014-06-06 17:45:55226 explicit BreakableCorrectionInterceptor(
[email protected]26bd3893c2014-04-15 18:59:58227 const FilePath& mock_corrections_file_path)
228 : mock_corrections_file_path_(mock_corrections_file_path),
[email protected]3a621992013-11-27 19:51:47229 net_error_(net::OK),
230 delay_requests_(false),
231 on_request_destroyed_callback_(
[email protected]c74b6462014-06-06 17:45:55232 base::Bind(&BreakableCorrectionInterceptor::OnRequestDestroyed,
[email protected]3a621992013-11-27 19:51:47233 base::Unretained(this))) {
234 }
[email protected]2ea1efe2013-07-17 05:23:13235
dchengfce29ad2014-10-23 03:47:47236 ~BreakableCorrectionInterceptor() override {
[email protected]3a621992013-11-27 19:51:47237 // All delayed requests should have been resumed or cancelled by this point.
238 EXPECT_TRUE(delayed_requests_.empty());
239 }
[email protected]2ea1efe2013-07-17 05:23:13240
dchengfce29ad2014-10-23 03:47:47241 URLRequestJob* MaybeInterceptRequest(
[email protected]3a621992013-11-27 19:51:47242 URLRequest* request,
mostynb2b52d1db2014-10-07 02:47:17243 NetworkDelegate* network_delegate) const override {
[email protected]2ea1efe2013-07-17 05:23:13244 if (net_error_ != net::OK) {
[email protected]3a621992013-11-27 19:51:47245 DelayableURLRequestFailedJob* job =
246 new DelayableURLRequestFailedJob(
247 request, network_delegate, net_error_, delay_requests_,
248 on_request_destroyed_callback_);
249 if (delay_requests_)
250 delayed_requests_.insert(job);
251 return job;
[email protected]2ea1efe2013-07-17 05:23:13252 } else {
[email protected]3a621992013-11-27 19:51:47253 DelayableURLRequestMockHTTPJob* job =
254 new DelayableURLRequestMockHTTPJob(
[email protected]26bd3893c2014-04-15 18:59:58255 request, network_delegate, mock_corrections_file_path_,
[email protected]3a621992013-11-27 19:51:47256 delay_requests_, on_request_destroyed_callback_);
257 if (delay_requests_)
258 delayed_requests_.insert(job);
259 return job;
[email protected]2ea1efe2013-07-17 05:23:13260 }
261 }
262
263 void set_net_error(int net_error) { net_error_ = net_error; }
264
[email protected]3a621992013-11-27 19:51:47265 void SetDelayRequests(bool delay_requests) {
266 delay_requests_ = delay_requests;
267
268 // Resume all delayed requests if no longer delaying requests.
269 if (!delay_requests) {
270 while (!delayed_requests_.empty()) {
271 DelayableRequest* request = *delayed_requests_.begin();
272 delayed_requests_.erase(request);
273 request->Resume();
274 }
275 }
276 }
277
278 // Runs |callback| once all delayed requests have been destroyed. Does not
279 // wait for delayed requests that have been resumed.
280 void SetRequestDestructionCallback(const base::Closure& callback) {
281 ASSERT_TRUE(delayed_request_destruction_callback_.is_null());
282 if (delayed_requests_.empty()) {
283 callback.Run();
284 return;
285 }
286 delayed_request_destruction_callback_ = callback;
287 }
288
289 void OnRequestDestroyed(DelayableRequest* request) {
290 ASSERT_EQ(1u, delayed_requests_.count(request));
291 delayed_requests_.erase(request);
292 if (delayed_requests_.empty() &&
293 !delayed_request_destruction_callback_.is_null()) {
294 delayed_request_destruction_callback_.Run();
295 delayed_request_destruction_callback_.Reset();
296 }
297 }
298
[email protected]2ea1efe2013-07-17 05:23:13299 private:
[email protected]26bd3893c2014-04-15 18:59:58300 const FilePath mock_corrections_file_path_;
[email protected]2ea1efe2013-07-17 05:23:13301 int net_error_;
[email protected]3a621992013-11-27 19:51:47302 bool delay_requests_;
303
304 // Called when a request is destroyed. Memeber variable because
305 // MaybeCreateJob is "const", so calling base::Bind in that function does
306 // not work well.
307 const DelayableRequest::DestructionCallback on_request_destroyed_callback_;
308
309 // Mutable is needed because MaybeCreateJob is const.
310 mutable std::set<DelayableRequest*> delayed_requests_;
311
312 base::Closure delayed_request_destruction_callback_;
[email protected]2ea1efe2013-07-17 05:23:13313};
314
315class DnsProbeBrowserTestIOThreadHelper {
316 public:
317 DnsProbeBrowserTestIOThreadHelper();
318
319 void SetUpOnIOThread(IOThread* io_thread);
320 void CleanUpOnIOThreadAndDeleteHelper();
321
eroman1efc237c2016-12-14 00:00:45322 void SetMockDnsClientRules(MockDnsClientRule::ResultType system_good_result,
323 MockDnsClientRule::ResultType public_good_result);
[email protected]26bd3893c2014-04-15 18:59:58324 void SetCorrectionServiceNetError(int net_error);
325 void SetCorrectionServiceDelayRequests(bool delay_requests);
[email protected]3a621992013-11-27 19:51:47326 void SetRequestDestructionCallback(const base::Closure& callback);
[email protected]2ea1efe2013-07-17 05:23:13327 void StartDelayedProbes(int expected_delayed_probe_count);
328
329 private:
330 IOThread* io_thread_;
331 DnsProbeService* original_dns_probe_service_;
332 DelayingDnsProbeService* delaying_dns_probe_service_;
[email protected]c74b6462014-06-06 17:45:55333 BreakableCorrectionInterceptor* interceptor_;
[email protected]26bd3893c2014-04-15 18:59:58334 FilePath mock_corrections_file_path_;
[email protected]2ea1efe2013-07-17 05:23:13335};
336
337DnsProbeBrowserTestIOThreadHelper::DnsProbeBrowserTestIOThreadHelper()
338 : io_thread_(NULL),
339 original_dns_probe_service_(NULL),
340 delaying_dns_probe_service_(NULL),
[email protected]c74b6462014-06-06 17:45:55341 interceptor_(NULL),
[email protected]26bd3893c2014-04-15 18:59:58342 mock_corrections_file_path_(GetMockLinkDoctorFilePath()) {}
[email protected]2ea1efe2013-07-17 05:23:13343
344void DnsProbeBrowserTestIOThreadHelper::SetUpOnIOThread(IOThread* io_thread) {
345 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
346 CHECK(io_thread);
347 CHECK(!io_thread_);
348 CHECK(!original_dns_probe_service_);
349 CHECK(!delaying_dns_probe_service_);
[email protected]c74b6462014-06-06 17:45:55350 CHECK(!interceptor_);
[email protected]2ea1efe2013-07-17 05:23:13351
352 io_thread_ = io_thread;
353
354 delaying_dns_probe_service_ = new DelayingDnsProbeService();
355
356 IOThread::Globals* globals = io_thread_->globals();
357 original_dns_probe_service_ = globals->dns_probe_service.release();
358 globals->dns_probe_service.reset(delaying_dns_probe_service_);
359
360 URLRequestFailedJob::AddUrlHandler();
361
[email protected]c74b6462014-06-06 17:45:55362 interceptor_ =
363 new BreakableCorrectionInterceptor(mock_corrections_file_path_);
364 URLRequestFilter::GetInstance()->AddUrlInterceptor(
dcheng4e7c0422016-04-14 00:59:05365 LinkDoctorBaseURL(),
366 std::unique_ptr<URLRequestInterceptor>(interceptor_));
[email protected]2ea1efe2013-07-17 05:23:13367}
368
369void DnsProbeBrowserTestIOThreadHelper::CleanUpOnIOThreadAndDeleteHelper() {
370 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
371
372 URLRequestFilter::GetInstance()->ClearHandlers();
373
374 IOThread::Globals* globals = io_thread_->globals();
dcheng4e7c0422016-04-14 00:59:05375 std::unique_ptr<DnsProbeService> delaying_dns_probe_service(
[email protected]2ea1efe2013-07-17 05:23:13376 globals->dns_probe_service.release());
377 globals->dns_probe_service.reset(original_dns_probe_service_);
378
379 CHECK_EQ(delaying_dns_probe_service_, delaying_dns_probe_service.get());
380
381 delete this;
382}
383
384void DnsProbeBrowserTestIOThreadHelper::SetMockDnsClientRules(
eroman1efc237c2016-12-14 00:00:45385 MockDnsClientRule::ResultType system_result,
386 MockDnsClientRule::ResultType public_result) {
[email protected]2ea1efe2013-07-17 05:23:13387 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
388
389 DnsProbeService* service = io_thread_->globals()->dns_probe_service.get();
390 service->SetSystemClientForTesting(
391 CreateMockDnsClientForProbes(system_result));
392 service->SetPublicClientForTesting(
393 CreateMockDnsClientForProbes(public_result));
394}
395
[email protected]26bd3893c2014-04-15 18:59:58396void DnsProbeBrowserTestIOThreadHelper::SetCorrectionServiceNetError(
397 int net_error) {
[email protected]2ea1efe2013-07-17 05:23:13398 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
399
[email protected]c74b6462014-06-06 17:45:55400 interceptor_->set_net_error(net_error);
[email protected]2ea1efe2013-07-17 05:23:13401}
402
[email protected]26bd3893c2014-04-15 18:59:58403void DnsProbeBrowserTestIOThreadHelper::SetCorrectionServiceDelayRequests(
[email protected]3a621992013-11-27 19:51:47404 bool delay_requests) {
405 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
406
[email protected]c74b6462014-06-06 17:45:55407 interceptor_->SetDelayRequests(delay_requests);
[email protected]3a621992013-11-27 19:51:47408}
409
410void DnsProbeBrowserTestIOThreadHelper::SetRequestDestructionCallback(
411 const base::Closure& callback) {
412 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
413
[email protected]c74b6462014-06-06 17:45:55414 interceptor_->SetRequestDestructionCallback(callback);
[email protected]3a621992013-11-27 19:51:47415}
416
[email protected]2ea1efe2013-07-17 05:23:13417void DnsProbeBrowserTestIOThreadHelper::StartDelayedProbes(
418 int expected_delayed_probe_count) {
419 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
420
421 CHECK(delaying_dns_probe_service_);
422
423 int actual_delayed_probe_count =
424 delaying_dns_probe_service_->delayed_probe_count();
425 EXPECT_EQ(expected_delayed_probe_count, actual_delayed_probe_count);
426
427 delaying_dns_probe_service_->StartDelayedProbes();
428}
429
430class DnsProbeBrowserTest : public InProcessBrowserTest {
431 public:
432 DnsProbeBrowserTest();
dchenge1bc7982014-10-30 00:32:40433 ~DnsProbeBrowserTest() override;
[email protected]2ea1efe2013-07-17 05:23:13434
dchengfce29ad2014-10-23 03:47:47435 void SetUpOnMainThread() override;
436 void TearDownOnMainThread() override;
[email protected]2ea1efe2013-07-17 05:23:13437
438 protected:
[email protected]d8574df2013-11-22 00:11:52439 // Sets the browser object that other methods apply to, and that has the
440 // DnsProbeStatus messages of its currently active tab monitored.
441 void SetActiveBrowser(Browser* browser);
442
[email protected]26bd3893c2014-04-15 18:59:58443 void SetCorrectionServiceBroken(bool broken);
444 void SetCorrectionServiceDelayRequests(bool delay_requests);
[email protected]3a621992013-11-27 19:51:47445 void WaitForDelayedRequestDestruction();
eroman1efc237c2016-12-14 00:00:45446 void SetMockDnsClientRules(MockDnsClientRule::ResultType system_result,
447 MockDnsClientRule::ResultType public_result);
[email protected]d8574df2013-11-22 00:11:52448
[email protected]26bd3893c2014-04-15 18:59:58449 // These functions are often used to wait for two navigations because two
450 // pages are loaded when navigation corrections are enabled: a blank page, so
451 // the user stops seeing the previous page, and then the error page, either
452 // with navigation corrections or without them (If the request failed).
[email protected]d8574df2013-11-22 00:11:52453 void NavigateToDnsError(int num_navigations);
454 void NavigateToOtherError(int num_navigations);
[email protected]2ea1efe2013-07-17 05:23:13455
456 void StartDelayedProbes(int expected_delayed_probe_count);
457 DnsProbeStatus WaitForSentStatus();
458 int pending_status_count() const { return dns_probe_status_queue_.size(); }
459
460 std::string Title();
461 bool PageContains(const std::string& expected);
462
[email protected]26bd3893c2014-04-15 18:59:58463 // Checks that the local error page is being displayed, without navigation
464 // corrections, and with the specified status text. The status text should be
465 // either a network error or DNS probe status.
466 void ExpectDisplayingLocalErrorPage(const std::string& status_text);
467
468 // Checks that an error page with mock navigation corrections is being
469 // displayed, with the specified status text. The status text should be either
470 // a network error or DNS probe status.
471 void ExpectDisplayingCorrections(const std::string& status_text);
472
[email protected]2ea1efe2013-07-17 05:23:13473 private:
474 void OnDnsProbeStatusSent(DnsProbeStatus dns_probe_status);
475
476 DnsProbeBrowserTestIOThreadHelper* helper_;
477
[email protected]d8574df2013-11-22 00:11:52478 // Browser that methods apply to.
479 Browser* active_browser_;
480 // Helper that current has its DnsProbeStatus messages monitored.
481 NetErrorTabHelper* monitored_tab_helper_;
482
[email protected]2ea1efe2013-07-17 05:23:13483 bool awaiting_dns_probe_status_;
484 // Queue of statuses received but not yet consumed by WaitForSentStatus().
485 std::list<DnsProbeStatus> dns_probe_status_queue_;
486};
487
488DnsProbeBrowserTest::DnsProbeBrowserTest()
489 : helper_(new DnsProbeBrowserTestIOThreadHelper()),
[email protected]d8574df2013-11-22 00:11:52490 active_browser_(NULL),
491 monitored_tab_helper_(NULL),
[email protected]2ea1efe2013-07-17 05:23:13492 awaiting_dns_probe_status_(false) {
493}
494
[email protected]3a621992013-11-27 19:51:47495DnsProbeBrowserTest::~DnsProbeBrowserTest() {
496 // No tests should have any unconsumed probe statuses.
497 EXPECT_EQ(0, pending_status_count());
498}
499
[email protected]2ea1efe2013-07-17 05:23:13500void DnsProbeBrowserTest::SetUpOnMainThread() {
501 NetErrorTabHelper::set_state_for_testing(
[email protected]d8574df2013-11-22 00:11:52502 NetErrorTabHelper::TESTING_DEFAULT);
503
504 browser()->profile()->GetPrefs()->SetBoolean(
505 prefs::kAlternateErrorPagesEnabled, true);
[email protected]2ea1efe2013-07-17 05:23:13506
507 BrowserThread::PostTask(
508 BrowserThread::IO, FROM_HERE,
509 Bind(&DnsProbeBrowserTestIOThreadHelper::SetUpOnIOThread,
510 Unretained(helper_),
511 g_browser_process->io_thread()));
512
[email protected]d8574df2013-11-22 00:11:52513 SetActiveBrowser(browser());
[email protected]2ea1efe2013-07-17 05:23:13514}
515
[email protected]ad354972014-07-25 18:58:20516void DnsProbeBrowserTest::TearDownOnMainThread() {
[email protected]2ea1efe2013-07-17 05:23:13517 BrowserThread::PostTask(
518 BrowserThread::IO, FROM_HERE,
519 Bind(&DnsProbeBrowserTestIOThreadHelper::CleanUpOnIOThreadAndDeleteHelper,
520 Unretained(helper_)));
521
522 NetErrorTabHelper::set_state_for_testing(
523 NetErrorTabHelper::TESTING_DEFAULT);
524}
525
[email protected]d8574df2013-11-22 00:11:52526void DnsProbeBrowserTest::SetActiveBrowser(Browser* browser) {
527 // If currently watching a NetErrorTabHelper, stop doing so before start
528 // watching another.
529 if (monitored_tab_helper_) {
530 monitored_tab_helper_->set_dns_probe_status_snoop_callback_for_testing(
531 NetErrorTabHelper::DnsProbeStatusSnoopCallback());
532 }
533 active_browser_ = browser;
534 monitored_tab_helper_ = NetErrorTabHelper::FromWebContents(
535 active_browser_->tab_strip_model()->GetActiveWebContents());
536 monitored_tab_helper_->set_dns_probe_status_snoop_callback_for_testing(
537 Bind(&DnsProbeBrowserTest::OnDnsProbeStatusSent, Unretained(this)));
538}
539
[email protected]26bd3893c2014-04-15 18:59:58540void DnsProbeBrowserTest::SetCorrectionServiceBroken(bool broken) {
[email protected]2ea1efe2013-07-17 05:23:13541 int net_error = broken ? net::ERR_NAME_NOT_RESOLVED : net::OK;
542
543 BrowserThread::PostTask(
544 BrowserThread::IO, FROM_HERE,
[email protected]26bd3893c2014-04-15 18:59:58545 Bind(&DnsProbeBrowserTestIOThreadHelper::SetCorrectionServiceNetError,
[email protected]2ea1efe2013-07-17 05:23:13546 Unretained(helper_),
547 net_error));
548}
549
[email protected]26bd3893c2014-04-15 18:59:58550void DnsProbeBrowserTest::SetCorrectionServiceDelayRequests(
551 bool delay_requests) {
[email protected]3a621992013-11-27 19:51:47552 BrowserThread::PostTask(
553 BrowserThread::IO, FROM_HERE,
[email protected]26bd3893c2014-04-15 18:59:58554 Bind(&DnsProbeBrowserTestIOThreadHelper::
555 SetCorrectionServiceDelayRequests,
[email protected]3a621992013-11-27 19:51:47556 Unretained(helper_),
557 delay_requests));
558}
559
560void DnsProbeBrowserTest::WaitForDelayedRequestDestruction() {
561 base::RunLoop run_loop;
562 BrowserThread::PostTask(
563 BrowserThread::IO, FROM_HERE,
564 Bind(&DnsProbeBrowserTestIOThreadHelper::SetRequestDestructionCallback,
565 Unretained(helper_),
566 base::Bind(&RunClosureOnUIThread,
567 run_loop.QuitClosure())));
568 run_loop.Run();
569}
570
[email protected]d8574df2013-11-22 00:11:52571void DnsProbeBrowserTest::NavigateToDnsError(int num_navigations) {
[email protected]2ea1efe2013-07-17 05:23:13572 NavigateToURLBlockUntilNavigationsComplete(
[email protected]d8574df2013-11-22 00:11:52573 active_browser_,
[email protected]2ea1efe2013-07-17 05:23:13574 URLRequestFailedJob::GetMockHttpUrl(net::ERR_NAME_NOT_RESOLVED),
[email protected]d8574df2013-11-22 00:11:52575 num_navigations);
[email protected]2ea1efe2013-07-17 05:23:13576}
577
[email protected]d8574df2013-11-22 00:11:52578void DnsProbeBrowserTest::NavigateToOtherError(int num_navigations) {
[email protected]2ea1efe2013-07-17 05:23:13579 NavigateToURLBlockUntilNavigationsComplete(
[email protected]d8574df2013-11-22 00:11:52580 active_browser_,
[email protected]2ea1efe2013-07-17 05:23:13581 URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_REFUSED),
[email protected]d8574df2013-11-22 00:11:52582 num_navigations);
[email protected]2ea1efe2013-07-17 05:23:13583}
584
585void DnsProbeBrowserTest::SetMockDnsClientRules(
eroman1efc237c2016-12-14 00:00:45586 MockDnsClientRule::ResultType system_result,
587 MockDnsClientRule::ResultType public_result) {
[email protected]2ea1efe2013-07-17 05:23:13588 BrowserThread::PostTask(
589 BrowserThread::IO, FROM_HERE,
590 Bind(&DnsProbeBrowserTestIOThreadHelper::SetMockDnsClientRules,
591 Unretained(helper_),
592 system_result,
593 public_result));
594}
595
596void DnsProbeBrowserTest::StartDelayedProbes(
597 int expected_delayed_probe_count) {
598 BrowserThread::PostTask(
599 BrowserThread::IO, FROM_HERE,
600 Bind(&DnsProbeBrowserTestIOThreadHelper::StartDelayedProbes,
601 Unretained(helper_),
602 expected_delayed_probe_count));
603}
604
605DnsProbeStatus DnsProbeBrowserTest::WaitForSentStatus() {
606 CHECK(!awaiting_dns_probe_status_);
607 while (dns_probe_status_queue_.empty()) {
608 awaiting_dns_probe_status_ = true;
fdoraycb32419d2016-06-23 15:52:55609 base::RunLoop().Run();
[email protected]2ea1efe2013-07-17 05:23:13610 awaiting_dns_probe_status_ = false;
611 }
612
613 CHECK(!dns_probe_status_queue_.empty());
614 DnsProbeStatus status = dns_probe_status_queue_.front();
615 dns_probe_status_queue_.pop_front();
616 return status;
617}
618
619// Check title by roundtripping to renderer, to make sure any probe results
620// sent before this have been applied.
621std::string DnsProbeBrowserTest::Title() {
622 std::string title;
623
624 WebContents* contents =
[email protected]d8574df2013-11-22 00:11:52625 active_browser_->tab_strip_model()->GetActiveWebContents();
[email protected]2ea1efe2013-07-17 05:23:13626
627 bool rv = content::ExecuteScriptAndExtractString(
628 contents,
629 "domAutomationController.send(document.title);",
630 &title);
631 if (!rv)
632 return "";
633
634 return title;
635}
636
637// Check text by roundtripping to renderer, to make sure any probe results
638// sent before this have been applied.
639bool DnsProbeBrowserTest::PageContains(const std::string& expected) {
640 std::string text_content;
641
642 bool rv = content::ExecuteScriptAndExtractString(
[email protected]d8574df2013-11-22 00:11:52643 active_browser_->tab_strip_model()->GetActiveWebContents(),
[email protected]2ea1efe2013-07-17 05:23:13644 "domAutomationController.send(document.body.textContent);",
645 &text_content);
646 if (!rv)
647 return false;
648
649 return text_content.find(expected) != std::string::npos;
650}
651
[email protected]26bd3893c2014-04-15 18:59:58652void DnsProbeBrowserTest::ExpectDisplayingLocalErrorPage(
653 const std::string& status_text) {
edwardjungf3d772c2016-02-26 02:38:36654 EXPECT_FALSE(PageContains("http://mock.http/title2.html"));
[email protected]26bd3893c2014-04-15 18:59:58655 EXPECT_TRUE(PageContains(status_text));
656}
657
658void DnsProbeBrowserTest::ExpectDisplayingCorrections(
659 const std::string& status_text) {
edwardjungf3d772c2016-02-26 02:38:36660 EXPECT_TRUE(PageContains("http://mock.http/title2.html"));
[email protected]26bd3893c2014-04-15 18:59:58661 EXPECT_TRUE(PageContains(status_text));
662}
663
[email protected]2ea1efe2013-07-17 05:23:13664void DnsProbeBrowserTest::OnDnsProbeStatusSent(
665 DnsProbeStatus dns_probe_status) {
666 dns_probe_status_queue_.push_back(dns_probe_status);
667 if (awaiting_dns_probe_status_)
ki.stfuc4f8e242015-10-09 20:40:20668 MessageLoop::current()->QuitWhenIdle();
[email protected]2ea1efe2013-07-17 05:23:13669}
670
[email protected]26bd3893c2014-04-15 18:59:58671// Make sure probes don't break non-DNS error pages when corrections load.
672IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, OtherErrorWithCorrectionsSuccess) {
673 SetCorrectionServiceBroken(false);
[email protected]2ea1efe2013-07-17 05:23:13674
[email protected]d8574df2013-11-22 00:11:52675 NavigateToOtherError(2);
[email protected]26bd3893c2014-04-15 18:59:58676 ExpectDisplayingCorrections("ERR_CONNECTION_REFUSED");
[email protected]2ea1efe2013-07-17 05:23:13677}
678
[email protected]26bd3893c2014-04-15 18:59:58679// Make sure probes don't break non-DNS error pages when corrections failed to
[email protected]2ea1efe2013-07-17 05:23:13680// load.
[email protected]26bd3893c2014-04-15 18:59:58681IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, OtherErrorWithCorrectionsFailure) {
682 SetCorrectionServiceBroken(true);
[email protected]2ea1efe2013-07-17 05:23:13683
[email protected]d8574df2013-11-22 00:11:52684 NavigateToOtherError(2);
[email protected]26bd3893c2014-04-15 18:59:58685 ExpectDisplayingLocalErrorPage("ERR_CONNECTION_REFUSED");
[email protected]2ea1efe2013-07-17 05:23:13686}
687
[email protected]26bd3893c2014-04-15 18:59:58688// Make sure probes don't break DNS error pages when corrections load.
[email protected]2ea1efe2013-07-17 05:23:13689IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest,
[email protected]26bd3893c2014-04-15 18:59:58690 NxdomainProbeResultWithWorkingCorrections) {
691 SetCorrectionServiceBroken(false);
[email protected]2ea1efe2013-07-17 05:23:13692 SetMockDnsClientRules(MockDnsClientRule::OK, MockDnsClientRule::OK);
693
[email protected]d8574df2013-11-22 00:11:52694 NavigateToDnsError(2);
[email protected]26bd3893c2014-04-15 18:59:58695 ExpectDisplayingCorrections("ERR_NAME_NOT_RESOLVED");
[email protected]2ea1efe2013-07-17 05:23:13696
[email protected]26bd3893c2014-04-15 18:59:58697 // One status for committing a blank page before the corrections, and one for
698 // when the error page with corrections is committed.
thestig4b0b01652015-08-18 20:36:39699 EXPECT_EQ(error_page::DNS_PROBE_STARTED, WaitForSentStatus());
700 EXPECT_EQ(error_page::DNS_PROBE_STARTED, WaitForSentStatus());
[email protected]2ea1efe2013-07-17 05:23:13701 EXPECT_EQ(0, pending_status_count());
[email protected]26bd3893c2014-04-15 18:59:58702 ExpectDisplayingCorrections("ERR_NAME_NOT_RESOLVED");
[email protected]2ea1efe2013-07-17 05:23:13703
704 StartDelayedProbes(1);
705
thestig4b0b01652015-08-18 20:36:39706 EXPECT_EQ(error_page::DNS_PROBE_FINISHED_NXDOMAIN,
[email protected]2ea1efe2013-07-17 05:23:13707 WaitForSentStatus());
708 EXPECT_EQ(0, pending_status_count());
[email protected]26bd3893c2014-04-15 18:59:58709 ExpectDisplayingCorrections("ERR_NAME_NOT_RESOLVED");
[email protected]2ea1efe2013-07-17 05:23:13710}
711
[email protected]26bd3893c2014-04-15 18:59:58712// Make sure probes don't break corrections when probes complete before the
713// corrections load.
[email protected]3a621992013-11-27 19:51:47714IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest,
[email protected]26bd3893c2014-04-15 18:59:58715 NxdomainProbeResultWithWorkingSlowCorrections) {
716 SetCorrectionServiceBroken(false);
717 SetCorrectionServiceDelayRequests(true);
[email protected]3a621992013-11-27 19:51:47718 SetMockDnsClientRules(MockDnsClientRule::OK, MockDnsClientRule::OK);
719
720 NavigateToDnsError(1);
[email protected]26bd3893c2014-04-15 18:59:58721 // A blank page should be displayed while the corrections are loaded.
[email protected]3a621992013-11-27 19:51:47722 EXPECT_EQ("", Title());
723
724 // A single probe should be triggered by the error page load, and it should
725 // be ignored.
thestig4b0b01652015-08-18 20:36:39726 EXPECT_EQ(error_page::DNS_PROBE_STARTED, WaitForSentStatus());
[email protected]3a621992013-11-27 19:51:47727 EXPECT_EQ(0, pending_status_count());
728 EXPECT_EQ("", Title());
729
730 StartDelayedProbes(1);
thestig4b0b01652015-08-18 20:36:39731 EXPECT_EQ(error_page::DNS_PROBE_FINISHED_NXDOMAIN,
[email protected]3a621992013-11-27 19:51:47732 WaitForSentStatus());
733 EXPECT_EQ(0, pending_status_count());
734 EXPECT_EQ("", Title());
735
736 content::TestNavigationObserver observer(
737 browser()->tab_strip_model()->GetActiveWebContents(), 1);
[email protected]26bd3893c2014-04-15 18:59:58738 // The corrections finish loading.
739 SetCorrectionServiceDelayRequests(false);
[email protected]3a621992013-11-27 19:51:47740 // Wait for it to commit.
741 observer.Wait();
[email protected]26bd3893c2014-04-15 18:59:58742 ExpectDisplayingCorrections("ERR_NAME_NOT_RESOLVED");
[email protected]3a621992013-11-27 19:51:47743
[email protected]26bd3893c2014-04-15 18:59:58744 // Committing the corections page should trigger sending the probe result
[email protected]3a621992013-11-27 19:51:47745 // again.
thestig4b0b01652015-08-18 20:36:39746 EXPECT_EQ(error_page::DNS_PROBE_FINISHED_NXDOMAIN,
[email protected]3a621992013-11-27 19:51:47747 WaitForSentStatus());
[email protected]26bd3893c2014-04-15 18:59:58748 ExpectDisplayingCorrections("ERR_NAME_NOT_RESOLVED");
[email protected]3a621992013-11-27 19:51:47749}
750
[email protected]2ea1efe2013-07-17 05:23:13751// Make sure probes update DNS error page properly when they're supposed to.
752IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest,
[email protected]26bd3893c2014-04-15 18:59:58753 NoInternetProbeResultWithBrokenCorrections) {
754 SetCorrectionServiceBroken(true);
[email protected]2ea1efe2013-07-17 05:23:13755 SetMockDnsClientRules(MockDnsClientRule::TIMEOUT,
756 MockDnsClientRule::TIMEOUT);
757
[email protected]d8574df2013-11-22 00:11:52758 NavigateToDnsError(2);
[email protected]2ea1efe2013-07-17 05:23:13759
thestig4b0b01652015-08-18 20:36:39760 EXPECT_EQ(error_page::DNS_PROBE_STARTED, WaitForSentStatus());
761 EXPECT_EQ(error_page::DNS_PROBE_STARTED, WaitForSentStatus());
[email protected]2ea1efe2013-07-17 05:23:13762
[email protected]26bd3893c2014-04-15 18:59:58763 // Checking the page runs the RunLoop, so make sure nothing hairy happens.
[email protected]2ea1efe2013-07-17 05:23:13764 EXPECT_EQ(0, pending_status_count());
[email protected]26bd3893c2014-04-15 18:59:58765 ExpectDisplayingLocalErrorPage("DNS_PROBE_STARTED");
[email protected]2ea1efe2013-07-17 05:23:13766 EXPECT_EQ(0, pending_status_count());
767
768 StartDelayedProbes(1);
769
thestig4b0b01652015-08-18 20:36:39770 EXPECT_EQ(error_page::DNS_PROBE_FINISHED_NO_INTERNET,
[email protected]2ea1efe2013-07-17 05:23:13771 WaitForSentStatus());
772
[email protected]26bd3893c2014-04-15 18:59:58773 // Checking the page runs the RunLoop, so make sure nothing hairy happens.
[email protected]2ea1efe2013-07-17 05:23:13774 EXPECT_EQ(0, pending_status_count());
[email protected]26bd3893c2014-04-15 18:59:58775 ExpectDisplayingLocalErrorPage("DNS_PROBE_FINISHED_NO_INTERNET");
[email protected]3a621992013-11-27 19:51:47776}
777
[email protected]26bd3893c2014-04-15 18:59:58778// Make sure probes don't break corrections when probes complete before the
779// corrections request returns an error.
[email protected]3a621992013-11-27 19:51:47780IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest,
[email protected]26bd3893c2014-04-15 18:59:58781 NoInternetProbeResultWithSlowBrokenCorrections) {
782 SetCorrectionServiceBroken(true);
783 SetCorrectionServiceDelayRequests(true);
[email protected]3a621992013-11-27 19:51:47784 SetMockDnsClientRules(MockDnsClientRule::TIMEOUT,
785 MockDnsClientRule::TIMEOUT);
786
787 NavigateToDnsError(1);
[email protected]26bd3893c2014-04-15 18:59:58788 // A blank page should be displayed while the corrections load.
[email protected]3a621992013-11-27 19:51:47789 EXPECT_EQ("", Title());
790
791 // A single probe should be triggered by the error page load, and it should
792 // be ignored.
thestig4b0b01652015-08-18 20:36:39793 EXPECT_EQ(error_page::DNS_PROBE_STARTED, WaitForSentStatus());
[email protected]2ea1efe2013-07-17 05:23:13794 EXPECT_EQ(0, pending_status_count());
[email protected]3a621992013-11-27 19:51:47795 EXPECT_EQ("", Title());
796
797 StartDelayedProbes(1);
thestig4b0b01652015-08-18 20:36:39798 EXPECT_EQ(error_page::DNS_PROBE_FINISHED_NO_INTERNET,
[email protected]3a621992013-11-27 19:51:47799 WaitForSentStatus());
800 EXPECT_EQ("", Title());
801 EXPECT_EQ(0, pending_status_count());
802
803 content::TestNavigationObserver observer(
804 browser()->tab_strip_model()->GetActiveWebContents(), 1);
[email protected]26bd3893c2014-04-15 18:59:58805 // The corrections request fails.
806 SetCorrectionServiceDelayRequests(false);
[email protected]3a621992013-11-27 19:51:47807 // Wait for the DNS error page to load instead.
808 observer.Wait();
809 // The page committing should result in sending the probe results again.
thestig4b0b01652015-08-18 20:36:39810 EXPECT_EQ(error_page::DNS_PROBE_FINISHED_NO_INTERNET,
[email protected]3a621992013-11-27 19:51:47811 WaitForSentStatus());
812
813 EXPECT_EQ(0, pending_status_count());
[email protected]26bd3893c2014-04-15 18:59:58814 ExpectDisplayingLocalErrorPage("DNS_PROBE_FINISHED_NO_INTERNET");
[email protected]2ea1efe2013-07-17 05:23:13815}
816
817// Double-check to make sure sync failures don't explode.
[email protected]26bd3893c2014-04-15 18:59:58818IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, SyncFailureWithBrokenCorrections) {
819 SetCorrectionServiceBroken(true);
[email protected]a210eef92013-07-19 19:06:12820 SetMockDnsClientRules(MockDnsClientRule::FAIL, MockDnsClientRule::FAIL);
[email protected]2ea1efe2013-07-17 05:23:13821
[email protected]d8574df2013-11-22 00:11:52822 NavigateToDnsError(2);
[email protected]2ea1efe2013-07-17 05:23:13823
thestig4b0b01652015-08-18 20:36:39824 EXPECT_EQ(error_page::DNS_PROBE_STARTED, WaitForSentStatus());
825 EXPECT_EQ(error_page::DNS_PROBE_STARTED, WaitForSentStatus());
[email protected]2ea1efe2013-07-17 05:23:13826
[email protected]26bd3893c2014-04-15 18:59:58827 // Checking the page runs the RunLoop, so make sure nothing hairy happens.
[email protected]2ea1efe2013-07-17 05:23:13828 EXPECT_EQ(0, pending_status_count());
[email protected]26bd3893c2014-04-15 18:59:58829 ExpectDisplayingLocalErrorPage("DNS_PROBE_STARTED");
[email protected]2ea1efe2013-07-17 05:23:13830 EXPECT_EQ(0, pending_status_count());
831
832 StartDelayedProbes(1);
833
thestig4b0b01652015-08-18 20:36:39834 EXPECT_EQ(error_page::DNS_PROBE_FINISHED_INCONCLUSIVE,
[email protected]2ea1efe2013-07-17 05:23:13835 WaitForSentStatus());
836
[email protected]26bd3893c2014-04-15 18:59:58837 // Checking the page runs the RunLoop, so make sure nothing hairy happens.
[email protected]2ea1efe2013-07-17 05:23:13838 EXPECT_EQ(0, pending_status_count());
[email protected]26bd3893c2014-04-15 18:59:58839 ExpectDisplayingLocalErrorPage("ERR_NAME_NOT_RESOLVED");
[email protected]2ea1efe2013-07-17 05:23:13840 EXPECT_EQ(0, pending_status_count());
841}
842
[email protected]26bd3893c2014-04-15 18:59:58843// Test that pressing the stop button cancels loading corrections.
[email protected]3a621992013-11-27 19:51:47844// TODO(mmenke): Add a test for the cross process navigation case.
845// TODO(mmenke): This test could flakily pass due to the timeout on downloading
[email protected]26bd3893c2014-04-15 18:59:58846// the corrections. Disable that timeout for browser tests.
847IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, CorrectionsLoadStopped) {
848 SetCorrectionServiceDelayRequests(true);
849 SetCorrectionServiceBroken(true);
[email protected]3a621992013-11-27 19:51:47850 SetMockDnsClientRules(MockDnsClientRule::TIMEOUT, MockDnsClientRule::TIMEOUT);
851
852 NavigateToDnsError(1);
853
thestig4b0b01652015-08-18 20:36:39854 EXPECT_EQ(error_page::DNS_PROBE_STARTED, WaitForSentStatus());
[email protected]3a621992013-11-27 19:51:47855 StartDelayedProbes(1);
thestig4b0b01652015-08-18 20:36:39856 EXPECT_EQ(error_page::DNS_PROBE_FINISHED_NO_INTERNET,
[email protected]3a621992013-11-27 19:51:47857 WaitForSentStatus());
858
859 EXPECT_EQ("", Title());
860 EXPECT_EQ(0, pending_status_count());
861
862 chrome::Stop(browser());
863 WaitForDelayedRequestDestruction();
864
865 // End up displaying a blank page.
866 EXPECT_EQ("", Title());
867}
868
[email protected]26bd3893c2014-04-15 18:59:58869// Test that pressing the stop button cancels the load of corrections, and
870// receiving a probe result afterwards does not swap in a DNS error page.
871IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, CorrectionsLoadStoppedSlowProbe) {
872 SetCorrectionServiceDelayRequests(true);
873 SetCorrectionServiceBroken(true);
[email protected]3a621992013-11-27 19:51:47874 SetMockDnsClientRules(MockDnsClientRule::TIMEOUT, MockDnsClientRule::TIMEOUT);
875
876 NavigateToDnsError(1);
877
thestig4b0b01652015-08-18 20:36:39878 EXPECT_EQ(error_page::DNS_PROBE_STARTED, WaitForSentStatus());
[email protected]3a621992013-11-27 19:51:47879
880 EXPECT_EQ("", Title());
881 EXPECT_EQ(0, pending_status_count());
882
883 chrome::Stop(browser());
884 WaitForDelayedRequestDestruction();
885
886 EXPECT_EQ("", Title());
887 EXPECT_EQ(0, pending_status_count());
888
889 StartDelayedProbes(1);
thestig4b0b01652015-08-18 20:36:39890 EXPECT_EQ(error_page::DNS_PROBE_FINISHED_NO_INTERNET,
[email protected]3a621992013-11-27 19:51:47891 WaitForSentStatus());
892
893 EXPECT_EQ("", Title());
894}
895
[email protected]2ea1efe2013-07-17 05:23:13896// Make sure probes don't run for subframe DNS errors.
897IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, NoProbeInSubframe) {
[email protected]26bd3893c2014-04-15 18:59:58898 SetCorrectionServiceBroken(false);
[email protected]2ea1efe2013-07-17 05:23:13899
tfarina5e7b57232015-10-17 23:37:40900 NavigateToURL(browser(),
901 URLRequestMockHTTPJob::GetMockUrl("iframe_dns_error.html"));
[email protected]2ea1efe2013-07-17 05:23:13902
903 // By the time NavigateToURL returns, the browser will have seen the failed
904 // provisional load. If a probe was started (or considered but not run),
905 // then the NetErrorTabHelper would have sent a NetErrorInfo message. Thus,
906 // if one hasn't been sent by now, the NetErrorTabHelper has not (and won't)
907 // start a probe for this DNS error.
908 EXPECT_EQ(0, pending_status_count());
909}
910
911// Make sure browser sends NOT_RUN properly when probes are disabled.
912IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, ProbesDisabled) {
[email protected]26bd3893c2014-04-15 18:59:58913 // Disable probes (And corrections).
[email protected]d8574df2013-11-22 00:11:52914 browser()->profile()->GetPrefs()->SetBoolean(
915 prefs::kAlternateErrorPagesEnabled, false);
[email protected]2ea1efe2013-07-17 05:23:13916
[email protected]26bd3893c2014-04-15 18:59:58917 SetCorrectionServiceBroken(true);
[email protected]d8574df2013-11-22 00:11:52918 SetMockDnsClientRules(MockDnsClientRule::TIMEOUT, MockDnsClientRule::TIMEOUT);
[email protected]2ea1efe2013-07-17 05:23:13919
[email protected]d8574df2013-11-22 00:11:52920 NavigateToDnsError(1);
[email protected]2ea1efe2013-07-17 05:23:13921
thestig4b0b01652015-08-18 20:36:39922 EXPECT_EQ(error_page::DNS_PROBE_NOT_RUN, WaitForSentStatus());
[email protected]2ea1efe2013-07-17 05:23:13923
[email protected]26bd3893c2014-04-15 18:59:58924 // Checking the page runs the RunLoop, so make sure nothing hairy happens.
[email protected]2ea1efe2013-07-17 05:23:13925 EXPECT_EQ(0, pending_status_count());
[email protected]26bd3893c2014-04-15 18:59:58926 ExpectDisplayingLocalErrorPage("ERR_NAME_NOT_RESOLVED");
[email protected]2ea1efe2013-07-17 05:23:13927}
928
[email protected]26bd3893c2014-04-15 18:59:58929// Test the case that corrections are disabled, but DNS probes are enabled.
930// This is the case with Chromium builds.
931IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, CorrectionsDisabled) {
932 // Disable corrections.
[email protected]d8574df2013-11-22 00:11:52933 browser()->profile()->GetPrefs()->SetBoolean(
934 prefs::kAlternateErrorPagesEnabled, false);
[email protected]26bd3893c2014-04-15 18:59:58935 // Requests to the correction service should work if any are made, so the test
936 // fails if that happens unexpectedly.
937 SetCorrectionServiceBroken(false);
938 // Normally disabling corrections disables DNS probes, so force DNS probes
[email protected]d8574df2013-11-22 00:11:52939 // to be enabled.
940 NetErrorTabHelper::set_state_for_testing(
941 NetErrorTabHelper::TESTING_FORCE_ENABLED);
942
943 SetMockDnsClientRules(MockDnsClientRule::FAIL, MockDnsClientRule::FAIL);
944
[email protected]26bd3893c2014-04-15 18:59:58945 // Just one commit and one sent status, since corrections are disabled.
[email protected]d8574df2013-11-22 00:11:52946 NavigateToDnsError(1);
thestig4b0b01652015-08-18 20:36:39947 EXPECT_EQ(error_page::DNS_PROBE_STARTED, WaitForSentStatus());
[email protected]d8574df2013-11-22 00:11:52948
[email protected]26bd3893c2014-04-15 18:59:58949 // Checking the page runs the RunLoop, so make sure nothing hairy happens.
[email protected]d8574df2013-11-22 00:11:52950 EXPECT_EQ(0, pending_status_count());
[email protected]26bd3893c2014-04-15 18:59:58951 ExpectDisplayingLocalErrorPage("DNS_PROBE_STARTED");
[email protected]d8574df2013-11-22 00:11:52952 EXPECT_EQ(0, pending_status_count());
953
954 StartDelayedProbes(1);
955
thestig4b0b01652015-08-18 20:36:39956 EXPECT_EQ(error_page::DNS_PROBE_FINISHED_INCONCLUSIVE,
[email protected]d8574df2013-11-22 00:11:52957 WaitForSentStatus());
958 EXPECT_EQ(0, pending_status_count());
[email protected]26bd3893c2014-04-15 18:59:58959 ExpectDisplayingLocalErrorPage("ERR_NAME_NOT_RESOLVED");
[email protected]d8574df2013-11-22 00:11:52960}
961
[email protected]26bd3893c2014-04-15 18:59:58962// Test incognito mode. Corrections should be disabled, but DNS probes are
[email protected]d8574df2013-11-22 00:11:52963// still enabled.
964IN_PROC_BROWSER_TEST_F(DnsProbeBrowserTest, Incognito) {
[email protected]26bd3893c2014-04-15 18:59:58965 // Requests to the correction service should work if any are made, so the test
966 // will fail if one is requested unexpectedly.
967 SetCorrectionServiceBroken(false);
[email protected]d8574df2013-11-22 00:11:52968
969 Browser* incognito = CreateIncognitoBrowser();
970 SetActiveBrowser(incognito);
971
972 SetMockDnsClientRules(MockDnsClientRule::FAIL, MockDnsClientRule::FAIL);
973
[email protected]26bd3893c2014-04-15 18:59:58974 // Just one commit and one sent status, since the corrections are disabled.
[email protected]d8574df2013-11-22 00:11:52975 NavigateToDnsError(1);
thestig4b0b01652015-08-18 20:36:39976 EXPECT_EQ(error_page::DNS_PROBE_STARTED, WaitForSentStatus());
[email protected]d8574df2013-11-22 00:11:52977
[email protected]26bd3893c2014-04-15 18:59:58978 // Checking the page runs the RunLoop, so make sure nothing hairy happens.
[email protected]d8574df2013-11-22 00:11:52979 EXPECT_EQ(0, pending_status_count());
[email protected]26bd3893c2014-04-15 18:59:58980 ExpectDisplayingLocalErrorPage("DNS_PROBE_STARTED");
[email protected]d8574df2013-11-22 00:11:52981 EXPECT_EQ(0, pending_status_count());
982
983 StartDelayedProbes(1);
984
thestig4b0b01652015-08-18 20:36:39985 EXPECT_EQ(error_page::DNS_PROBE_FINISHED_INCONCLUSIVE,
[email protected]d8574df2013-11-22 00:11:52986 WaitForSentStatus());
987 EXPECT_EQ(0, pending_status_count());
[email protected]26bd3893c2014-04-15 18:59:58988 ExpectDisplayingLocalErrorPage("ERR_NAME_NOT_RESOLVED");
[email protected]d8574df2013-11-22 00:11:52989}
990
[email protected]2ea1efe2013-07-17 05:23:13991} // namespace
992
993} // namespace chrome_browser_net