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

Side by Side Diff: chrome/browser/captive_portal/captive_portal_browsertest.cc

Issue 10020051: Open a login tab on captive portal detection on SSL loads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Sync Created 8 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 | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 <map>
6 #include <set>
7
8 #include "base/basictypes.h"
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/compiler_specific.h"
12 #include "base/file_path.h"
13 #include "base/message_loop.h"
14 #include "base/path_service.h"
15 #include "base/utf_string_conversions.h"
16 #include "chrome/browser/captive_portal/captive_portal_service.h"
17 #include "chrome/browser/captive_portal/captive_portal_service_factory.h"
18 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h"
19 #include "chrome/browser/captive_portal/captive_portal_tab_reloader.h"
20 #include "chrome/browser/net/url_request_mock_util.h"
21 #include "chrome/browser/prefs/pref_service.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/browser_finder.h"
25 #include "chrome/browser/ui/browser_navigator.h"
26 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
27 #include "chrome/browser/ui/tab_contents/tab_contents.h"
28 #include "chrome/common/chrome_notification_types.h"
29 #include "chrome/common/chrome_paths.h"
30 #include "chrome/common/chrome_switches.h"
31 #include "chrome/common/pref_names.h"
32 #include "chrome/test/base/in_process_browser_test.h"
33 #include "chrome/test/base/ui_test_utils.h"
34 #include "content/public/browser/browser_thread.h"
35 #include "content/public/browser/navigation_controller.h"
36 #include "content/public/browser/notification_observer.h"
37 #include "content/public/browser/notification_registrar.h"
38 #include "content/public/browser/notification_service.h"
39 #include "content/public/browser/notification_types.h"
40 #include "content/public/browser/render_view_host.h"
41 #include "content/public/browser/web_contents.h"
42 #include "content/public/common/url_constants.h"
43 #include "content/test/net/url_request_failed_job.h"
44 #include "content/test/net/url_request_mock_http_job.h"
45 #include "net/base/net_errors.h"
46 #include "net/url_request/url_request_filter.h"
47 #include "net/url_request/url_request_job.h"
48 #include "net/url_request/url_request_status.h"
49 #include "testing/gtest/include/gtest/gtest.h"
50
51 using content::BrowserThread;
52
53 namespace captive_portal {
54
55 namespace {
56
57 // Path of the fake login page, when using the TestServer.
58 const char* const kTestServerLoginPath = "files/captive_portal/login.html";
59
60 // Path of a page with an iframe that has a mock SSL timeout, when using the
61 // TestServer.
62 const char* const kTestServerIframeTimeoutPath =
63 "files/captive_portal/iframe_timeout.html";
64
65 // The following URLs each have two different behaviors, depending on whether
66 // URLRequestMockCaptivePortalJobFactory is currently simulating the presence
67 // of a captive portal or not.
68
69 // A mock URL for the CaptivePortalService's |test_url|. When behind a captive
70 // portal, this URL return a mock login page. When connected to the Internet,
71 // it returns a 204 response.
72 const char* const kMockCaptivePortalTestUrl =
73 "http://mock.captive.portal/captive_portal_test/";
74
75 // When behind a captive portal, this URL hangs without committing until a call
76 // to URLRequestTimeoutOnDemandJob::FailRequests. When that function is called,
77 // the request will time out.
78 //
79 // When connected to the Internet, this URL returns a non-error page.
80 const char* const kMockHttpsUrl = "https://mock.captive.portal/long_timeout/";
81
82 // Same as kMockHttpsUrl, except the timeout happens instantly.
83 const char* const kMockHttpsQuickTimeoutUrl =
84 "https://mock.captive.portal/quick_timeout/";
85
86 // Expected title of a tab once an HTTPS load completes, when not behind a
87 // captive portal.
88 const char* const kInternetConnectedTitle = "Title Of Awesomeness";
89
90 // A URL request job that hangs until FailRequests() is called. Started jobs
91 // are stored in a static class variable containing a linked list so that
92 // FailRequests() can locate them.
93 class URLRequestTimeoutOnDemandJob : public net::URLRequestJob,
94 public base::NonThreadSafe {
95 public:
96 // net::URLRequestJob:
97 virtual void Start() OVERRIDE;
98
99 // All the public static methods below can be called on any thread.
100
101 // Fails all active URLRequestFailOnDemandJobs with connection timeouts.
102 // Must only be called when there are requests that have been started but not
103 // yet timed out.
104 static void FailRequests();
105
106 // Clears the |waiting_jobs_list_| without having the jobs return anything.
107 // Used to allow an assertion that jobs are not in the |waiting_jobs_list_|
108 // when destroyed. Must only be called when there are requests that have
109 // been started but not yet timed out.
110 static void AbandonRequests();
111
112 private:
113 friend class URLRequestMockCaptivePortalJobFactory;
114
115 explicit URLRequestTimeoutOnDemandJob(net::URLRequest* request);
116 virtual ~URLRequestTimeoutOnDemandJob();
117
118 // Attempts to removes |this| from |jobs_|. Returns true if it was removed
119 // from the list.
120 bool RemoveFromList();
121
122 // These do all the work of the corresponding public functions, with the only
123 // difference being that they must be called on the IO thread.
124 static void FailRequestsOnIOThread();
125 static void AbandonRequestsOnIOThread();
126
127 // Head of linked list of jobs that have been started and are now waiting to
128 // be timed out.
129 static URLRequestTimeoutOnDemandJob* job_list_;
130
131 // The next job that had been started but not yet timed out.
132 URLRequestTimeoutOnDemandJob* next_job_;
133
134 DISALLOW_COPY_AND_ASSIGN(URLRequestTimeoutOnDemandJob);
135 };
136
137 URLRequestTimeoutOnDemandJob* URLRequestTimeoutOnDemandJob::job_list_ = NULL;
138
139 void URLRequestTimeoutOnDemandJob::Start() {
140 EXPECT_TRUE(CalledOnValidThread());
141
142 // Insert at start of the list.
143 next_job_ = job_list_;
144 job_list_ = this;
145 }
146
147 // static
148 void URLRequestTimeoutOnDemandJob::FailRequests() {
149 content::BrowserThread::PostTask(
150 content::BrowserThread::IO, FROM_HERE,
151 base::Bind(&URLRequestTimeoutOnDemandJob::FailRequestsOnIOThread));
152 }
153
154 // static
155 void URLRequestTimeoutOnDemandJob::AbandonRequests() {
156 content::BrowserThread::PostTask(
157 content::BrowserThread::IO, FROM_HERE,
158 base::Bind(&URLRequestTimeoutOnDemandJob::AbandonRequestsOnIOThread));
159 }
160
161 URLRequestTimeoutOnDemandJob::URLRequestTimeoutOnDemandJob(
162 net::URLRequest* request)
163 : net::URLRequestJob(request),
164 next_job_(NULL) {
165 }
166
167 URLRequestTimeoutOnDemandJob::~URLRequestTimeoutOnDemandJob() {
168 // |this| shouldn't be in the list.
169 EXPECT_FALSE(RemoveFromList());
170 }
171
172 bool URLRequestTimeoutOnDemandJob::RemoveFromList() {
173 URLRequestTimeoutOnDemandJob** job = &job_list_;
174 while (*job) {
175 if (*job == this) {
176 *job = next_job_;
177 next_job_ = NULL;
178 return true;
179 }
180 job = &next_job_;
181 }
182
183 // If the job wasn't in this list, |next_job_| should be NULL.
184 EXPECT_FALSE(next_job_);
185 return false;
186 }
187
188 // static
189 void URLRequestTimeoutOnDemandJob::FailRequestsOnIOThread() {
190 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
191 EXPECT_TRUE(job_list_);
192 while (job_list_) {
193 URLRequestTimeoutOnDemandJob* job = job_list_;
194 // Since the error notification may result in the job's destruction, remove
195 // it from the job list before the error.
196 EXPECT_TRUE(job->RemoveFromList());
197 job->NotifyStartError(net::URLRequestStatus(
198 net::URLRequestStatus::FAILED,
199 net::ERR_CONNECTION_TIMED_OUT));
200 }
201 }
202
203 // static
204 void URLRequestTimeoutOnDemandJob::AbandonRequestsOnIOThread() {
205 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
206 EXPECT_TRUE(job_list_);
207 while (job_list_)
208 EXPECT_TRUE(job_list_->RemoveFromList());
209 }
210
211 // URLRequestCaptivePortalJobFactory emulates captive portal behavior.
212 // Initially, it emulates being behind a captive portal. When
213 // SetBehindCaptivePortal(false) is called, it emulates behavior when not behind
214 // a captive portal. The class itself is never instantiated.
215 //
216 // It handles requests for kMockCaptivePortalTestUrl, kMockHttpsUrl, and
217 // kMockHttpsQuickTimeoutUrl.
218 class URLRequestMockCaptivePortalJobFactory {
219 public:
220 // The public static methods below can be called on any thread.
221
222 // Adds the testing URLs to the net::URLRequestFilter. Should only be called
223 // once.
224 static void AddUrlHandlers();
225
226 // Sets whether or not there is a captive portal. Outstanding requests are
227 // not affected.
228 static void SetBehindCaptivePortal(bool behind_captive_portal);
229
230 private:
231 // These do all the work of the corresponding public functions, with the only
232 // difference being that they must be called on the IO thread.
233 static void AddUrlHandlersOnIOThread();
234 static void SetBehindCaptivePortalOnIOThread(bool behind_captive_portal);
235
236 // Returns a URLRequestJob that reflects the current captive portal state
237 // for the URLs: kMockCaptivePortalTestUrl, kMockHttpsUrl, and
238 // kMockHttpsQuickTimeoutUrl. See documentation of individual URLs for
239 // actual behavior.
240 static net::URLRequestJob* Factory(net::URLRequest* request,
241 const std::string& scheme);
242
243 static bool behind_captive_portal_;
244
245 DISALLOW_IMPLICIT_CONSTRUCTORS(URLRequestMockCaptivePortalJobFactory);
246 };
247
248 bool URLRequestMockCaptivePortalJobFactory::behind_captive_portal_ = true;
249
250 // static
251 void URLRequestMockCaptivePortalJobFactory::AddUrlHandlers() {
252 content::BrowserThread::PostTask(
253 content::BrowserThread::IO, FROM_HERE,
254 base::Bind(
255 &URLRequestMockCaptivePortalJobFactory::AddUrlHandlersOnIOThread));
256 }
257
258 // static
259 void URLRequestMockCaptivePortalJobFactory::SetBehindCaptivePortal(
260 bool behind_captive_portal) {
261 content::BrowserThread::PostTask(
262 content::BrowserThread::IO, FROM_HERE,
263 base::Bind(
264 &URLRequestMockCaptivePortalJobFactory::
265 SetBehindCaptivePortalOnIOThread,
266 behind_captive_portal));
267 }
268
269 // static
270 void URLRequestMockCaptivePortalJobFactory::AddUrlHandlersOnIOThread() {
271 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
272 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
273 filter->AddHostnameHandler("http", "mock.captive.portal",
274 URLRequestMockCaptivePortalJobFactory::Factory);
275 filter->AddHostnameHandler("https", "mock.captive.portal",
276 URLRequestMockCaptivePortalJobFactory::Factory);
277 }
278
279 // static
280 void URLRequestMockCaptivePortalJobFactory::SetBehindCaptivePortalOnIOThread(
281 bool behind_captive_portal) {
282 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
283 behind_captive_portal_ = behind_captive_portal;
284 }
285
286 // static
287 net::URLRequestJob* URLRequestMockCaptivePortalJobFactory::Factory(
288 net::URLRequest* request,
289 const std::string& scheme) {
290 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
291
292 // The PathService is threadsafe.
293 FilePath root_http;
294 PathService::Get(chrome::DIR_TEST_DATA, &root_http);
295
296 if (scheme == "https") {
297 if (behind_captive_portal_) {
298 // If not logged in to the captive portal, HTTPS requests will time out,
299 // either immediately on on demand.
300 if (request->url() == GURL(kMockHttpsQuickTimeoutUrl))
301 return new URLRequestFailedJob(request, net::ERR_CONNECTION_TIMED_OUT);
302 return new URLRequestTimeoutOnDemandJob(request);
303 }
304 // Once logged in to the portal, HTTPS requests return the page that was
305 // actually requested.
306 return new URLRequestMockHTTPJob(
307 request,
308 root_http.Append(FILE_PATH_LITERAL("title2.html")));
309 }
310
311 // The URL is the captive portal test URL.
312
313 if (behind_captive_portal_) {
314 // Prior to logging in to the portal, HTTP requests go to the login page.
315 return new URLRequestMockHTTPJob(
316 request,
317 root_http.Append(
318 FILE_PATH_LITERAL("captive_portal/login.html")));
319 }
320 // After logging in to the portal, the test URL returns a 204 response.
321 return new URLRequestMockHTTPJob(
322 request,
323 root_http.Append(
324 FILE_PATH_LITERAL("captive_portal/page204.html")));
325 }
326
327 // Creates a server-side redirect for use with the TestServer.
328 std::string CreateServerRedirect(const std::string& dest_url) {
329 const char* const kServerRedirectBase = "server-redirect?";
330 return kServerRedirectBase + dest_url;
331 }
332
333 // Returns the total number of loading tabs across all Browsers, for all
334 // Profiles.
335 int NumLoadingTabs() {
336 int num_loading_tabs = 0;
337 for (TabContentsIterator tab_contents_it;
338 !tab_contents_it.done();
339 ++tab_contents_it) {
340 if (tab_contents_it->web_contents()->IsLoading())
341 ++num_loading_tabs;
342 }
343 return num_loading_tabs;
344 }
345
346 bool IsLoginTab(TabContents* tab_contents) {
347 return tab_contents->captive_portal_tab_helper()->IsLoginTab();
348 }
349
350 // Tracks how many times each tab has been navigated since the Observer was
351 // created. The standard TestNavigationObserver can only watch specific
352 // pre-existing tabs or loads in serial for all tabs.
353 class MultiNavigationObserver : public content::NotificationObserver {
354 public:
355 MultiNavigationObserver();
356 virtual ~MultiNavigationObserver();
357
358 // Waits for exactly |num_navigations_to_wait_for| LOAD_STOP
359 // notifications to have occurred since the construction of |this|. More
360 // navigations than expected occuring will trigger a expect failure.
361 void WaitForNavigations(int num_navigations_to_wait_for);
362
363 // Returns the number of LOAD_STOP events that have occurred for
364 // |web_contents| since this was constructed.
365 int NumNavigationsForTab(content::WebContents* web_contents) const;
366
367 // The number of LOAD_STOP events since |this| was created.
368 int num_navigations() const { return num_navigations_; }
369
370 private:
371 typedef std::map<content::WebContents*, int> TabNavigationMap;
372
373 // content::NotificationObserver:
374 virtual void Observe(int type, const content::NotificationSource& source,
375 const content::NotificationDetails& details) OVERRIDE;
376
377 int num_navigations_;
378
379 // Map of how many times each tab has navigated since |this| was created.
380 TabNavigationMap tab_navigation_map_;
381
382 // Total number of navigations to wait for. Value only matters when
383 // |waiting_for_navigation_| is true.
384 int num_navigations_to_wait_for_;
385
386 // True if WaitForNavigations has been called, until
387 // |num_navigations_to_wait_for_| have been observed.
388 bool waiting_for_navigation_;
389
390 content::NotificationRegistrar registrar_;
391
392 DISALLOW_COPY_AND_ASSIGN(MultiNavigationObserver);
393 };
394
395 MultiNavigationObserver::MultiNavigationObserver()
396 : num_navigations_(0),
397 num_navigations_to_wait_for_(0),
398 waiting_for_navigation_(false) {
399 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP,
400 content::NotificationService::AllSources());
401 }
402
403 MultiNavigationObserver::~MultiNavigationObserver() {
404 }
405
406 void MultiNavigationObserver::WaitForNavigations(
407 int num_navigations_to_wait_for) {
408 // Shouldn't already be waiting for navigations.
409 EXPECT_FALSE(waiting_for_navigation_);
410 EXPECT_LT(0, num_navigations_to_wait_for);
411 if (num_navigations_ < num_navigations_to_wait_for) {
412 num_navigations_to_wait_for_ = num_navigations_to_wait_for;
413 waiting_for_navigation_ = true;
414 ui_test_utils::RunMessageLoop();
415 EXPECT_FALSE(waiting_for_navigation_);
416 }
417 EXPECT_EQ(num_navigations_, num_navigations_to_wait_for);
418 }
419
420 int MultiNavigationObserver::NumNavigationsForTab(
421 content::WebContents* web_contents) const {
422 TabNavigationMap::const_iterator tab_navigations =
423 tab_navigation_map_.find(web_contents);
424 if (tab_navigations == tab_navigation_map_.end())
425 return 0;
426 return tab_navigations->second;
427 }
428
429 void MultiNavigationObserver::Observe(
430 int type,
431 const content::NotificationSource& source,
432 const content::NotificationDetails& details) {
433 ASSERT_EQ(type, content::NOTIFICATION_LOAD_STOP);
434 content::NavigationController* controller =
435 content::Source<content::NavigationController>(source).ptr();
436 ++num_navigations_;
437 ++tab_navigation_map_[controller->GetWebContents()];
438 if (waiting_for_navigation_ &&
439 num_navigations_to_wait_for_ == num_navigations_) {
440 waiting_for_navigation_ = false;
441 MessageLoopForUI::current()->Quit();
442 }
443 }
444
445 // An observer for watching the CaptivePortalService. It tracks the last
446 // received result and the total number of received results.
447 class CaptivePortalObserver : public content::NotificationObserver {
448 public:
449 explicit CaptivePortalObserver(Profile* profile);
450
451 // Runs the message loop until until at exactly |update_count| capitive portal
452 // results have been received, since this creation of |this|. Expects no
453 // additional captive portal results.
454 void WaitForResults(int num_results_to_wait_for);
455
456 int num_results_received() const { return num_results_received_; }
457
458 Result captive_portal_result() const {
459 return captive_portal_result_;
460 }
461
462 private:
463 // Records results and exits the message loop, if needed.
464 void Observe(int type,
465 const content::NotificationSource& source,
466 const content::NotificationDetails& details);
467
468 // Number of times OnPortalResult has been called since construction.
469 int num_results_received_;
470
471 // If WaitForResults was called, the total number of updates for which to
472 // wait. Value doesn't matter when |waiting_for_result_| is false.
473 int num_results_to_wait_for_;
474
475 bool waiting_for_result_;
476
477 Profile* profile_;
478
479 CaptivePortalService* captive_portal_service_;
480
481 // Last result received.
482 Result captive_portal_result_;
483
484 content::NotificationRegistrar registrar_;
485
486 DISALLOW_COPY_AND_ASSIGN(CaptivePortalObserver);
487 };
488
489 CaptivePortalObserver::CaptivePortalObserver(Profile* profile)
490 : num_results_received_(0),
491 num_results_to_wait_for_(0),
492 waiting_for_result_(false),
493 profile_(profile),
494 captive_portal_service_(
495 CaptivePortalServiceFactory::GetForProfile(profile)),
496 captive_portal_result_(
497 captive_portal_service_->last_detection_result()) {
498 registrar_.Add(this,
499 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT,
500 content::Source<Profile>(profile_));
501 }
502
503 void CaptivePortalObserver::WaitForResults(int num_results_to_wait_for) {
504 EXPECT_LT(0, num_results_to_wait_for);
505 EXPECT_FALSE(waiting_for_result_);
506 if (num_results_received_ < num_results_to_wait_for) {
507 num_results_to_wait_for_ = num_results_to_wait_for;
508 waiting_for_result_ = true;
509 ui_test_utils::RunMessageLoop();
510 EXPECT_FALSE(waiting_for_result_);
511 }
512 EXPECT_EQ(num_results_received_, num_results_to_wait_for);
513 }
514
515 void CaptivePortalObserver::Observe(
516 int type,
517 const content::NotificationSource& source,
518 const content::NotificationDetails& details) {
519 ASSERT_EQ(type, chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT);
520 ASSERT_EQ(profile_, content::Source<Profile>(source).ptr());
521
522 CaptivePortalService::Results* results =
523 content::Details<CaptivePortalService::Results>(details).ptr();
524
525 EXPECT_EQ(captive_portal_result_, results->previous_result);
526 EXPECT_EQ(captive_portal_service_->last_detection_result(),
527 results->result);
528
529 captive_portal_result_ = results->result;
530 ++num_results_received_;
531
532 if (waiting_for_result_ &&
533 num_results_to_wait_for_ == num_results_received_) {
534 waiting_for_result_ = false;
535 MessageLoop::current()->Quit();
536 }
537 }
538
539 } // namespace
540
541 class CaptivePortalBrowserTest : public InProcessBrowserTest {
542 public:
543 CaptivePortalBrowserTest();
544
545 // InProcessBrowserTest:
546 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE;
547 virtual void SetUpOnMainThread() OVERRIDE;
548 virtual void CleanUpOnMainThread() OVERRIDE;
549
550 // Sets the captive portal checking preference. Does not affect the command
551 // line flag, which is set in SetUpCommandLine.
552 void EnableCaptivePortalDetection(Profile* profile, bool enabled);
553
554 // Sets up the captive portal service for the given profile so that
555 // all checks go to |test_url|. Also disables all timers.
556 void SetUpCaptivePortalService(Profile* profile, const GURL& test_url);
557
558 // Returns true if |browser|'s profile is currently running a captive portal
559 // check.
560 bool CheckPending(Browser* browser);
561
562 // Returns the CaptivePortalTabReloader::State of |tab_contents|.
563 CaptivePortalTabReloader::State GetStateOfTabReloader(
564 TabContents* tab_contents) const;
565
566 // Returns the CaptivePortalTabReloader::State of the indicated tab.
567 CaptivePortalTabReloader::State GetStateOfTabReloaderAt(Browser* browser,
568 int index) const;
569
570 // Returns the number of tabs with the given state, across all profiles.
571 int NumTabsWithState(CaptivePortalTabReloader::State state) const;
572
573 // Returns the number of tabs broken by captive portals, across all profiles.
574 int NumBrokenTabs() const;
575
576 // Returns the number of tabs that need to be reloaded due to having logged
577 // in to a captive portal, across all profiles.
578 int NumNeedReloadTabs() const;
579
580 // Navigates |browser|'s active tab to |url| and expects no captive portal
581 // test to be triggered. |expected_navigations| is the number of times the
582 // active tab will end up being navigated. It should be 1, except for the
583 // Link Doctor page, which acts like two navigations.
584 void NavigateToPageExpectNoTest(Browser* browser,
585 const GURL& url,
586 int expected_navigations);
587
588 // Navigates |browser|'s active tab to an SSL tab that takes a while to load,
589 // triggering a captive portal check, which is expected to give the result
590 // |expected_result|. The page finishes loading, with a timeout, after the
591 // captive portal check.
592 void SlowLoadNoCaptivePortal(Browser* browser, Result expected_result);
593
594 // Navigates |browser|'s active tab to an SSL timeout, expecting a captive
595 // portal check to be triggered and return a result which will indicates
596 // there's no detected captive portal.
597 void FastTimeoutNoCaptivePortal(Browser* browser, Result expected_result);
598
599 // Navigates the active tab to a slow loading SSL page, which will then
600 // trigger a captive portal test. The test is expected to find a captive
601 // portal. The slow loading page will continue to load after the function
602 // returns, until URLRequestTimeoutOnDemandJob::FailRequests() is called,
603 // at which point it will timeout.
604 //
605 // When |expect_login_tab| is false, no login tab is expected to be opened,
606 // because one already exists, and the function returns once the captive
607 // portal test is complete.
608 //
609 // If |expect_login_tab| is true, a login tab is then expected to be opened.
610 // It waits until both the login tab has finished loading, and two captive
611 // portal tests complete. The second test is triggered by the load of the
612 // captive portal tab completing.
613 //
614 // This function must not be called when the active tab is currently loading.
615 void SlowLoadBehindCaptivePortal(Browser* browser, bool expect_login_tab);
616
617 // Just like SlowLoadBehindCaptivePortal, except the navigated tab has
618 // a connection timeout rather having its time trigger, and the function
619 // waits until that timeout occurs.
620 void FastTimeoutBehindCaptivePortal(Browser* browser,
621 bool expect_open_login_tab);
622
623 // Navigates the login tab without logging in. The login tab must be the
624 // specified browser's active tab. Expects no other tab to change state.
625 // |num_loading_tabs| and |num_timed_out_tabs| are used as extra checks
626 // that nothing has gone wrong prior to the function call.
627 void NavigateLoginTab(Browser* browser,
628 int num_loading_tabs,
629 int num_timed_out_tabs);
630
631 // Simulates a login by updating the URLRequestMockCaptivePortalJob's
632 // behind captive portal state, and navigating the login tab. Waits for
633 // all broken but not loading tabs to be reloaded.
634 // |num_loading_tabs| and |num_timed_out_tabs| are used as extra checks
635 // that nothing has gone wrong prior to the function call.
636 void Login(Browser* browser, int num_loading_tabs, int num_timed_out_tabs);
637
638 // Makes the slow SSL loads of all active tabs time out at once, and waits for
639 // them to finish both that load and the automatic reload it should trigger.
640 // There should be no timed out tabs when this is called. The former login
641 // tab should be the active tab.
642 void FailLoadsAfterLogin(Browser* browser, int num_loading_tabs);
643
644 // Makes the slow SSL loads of all active tabs time out at once, and waits for
645 // them to finish displaying their error pages. The login tab should be the
646 // active tab. There should be no timed out tabs when this is called.
647 void FailLoadsWithoutLogin(Browser* browser, int num_loading_tabs);
648
649 // Sets the timeout used by a CaptivePortalTabReloader on slow SSL loads
650 // before a captive portal check.
651 void SetSlowSSLLoadTime(CaptivePortalTabReloader* tab_reloader,
652 base::TimeDelta slow_ssl_load_time);
653
654 CaptivePortalTabReloader* GetTabReloader(TabContents* tab_contents) const;
655
656 private:
657 DISALLOW_COPY_AND_ASSIGN(CaptivePortalBrowserTest);
658 };
659
660 CaptivePortalBrowserTest::CaptivePortalBrowserTest() {
661 }
662
663 void CaptivePortalBrowserTest::SetUpOnMainThread() {
664 // Enable mock requests.
665 content::BrowserThread::PostTask(
666 content::BrowserThread::IO, FROM_HERE,
667 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true));
668 URLRequestMockCaptivePortalJobFactory::AddUrlHandlers();
669
670 EnableCaptivePortalDetection(browser()->profile(), true);
671
672 // Set the captive portal service to use URLRequestMockCaptivePortalJob's
673 // mock URL, by default.
674 SetUpCaptivePortalService(browser()->profile(),
675 GURL(kMockCaptivePortalTestUrl));
676 }
677
678 void CaptivePortalBrowserTest::CleanUpOnMainThread() {
679 // No test should have a captive portal check pending on quit.
680 EXPECT_FALSE(CheckPending(browser()));
681 }
682
683 void CaptivePortalBrowserTest::SetUpCommandLine(
684 CommandLine* command_line) {
685 command_line->AppendSwitch(switches::kCaptivePortalDetection);
686 }
687
688 void CaptivePortalBrowserTest::EnableCaptivePortalDetection(
689 Profile* profile, bool enabled) {
690 profile->GetPrefs()->SetBoolean(prefs::kAlternateErrorPagesEnabled, enabled);
691 }
692
693 void CaptivePortalBrowserTest::SetUpCaptivePortalService(Profile* profile,
694 const GURL& test_url) {
695 CaptivePortalService* captive_portal_service =
696 CaptivePortalServiceFactory::GetForProfile(profile);
697 captive_portal_service->set_test_url(test_url);
698
699 // Don't use any non-zero timers. Timers are checked in unit tests.
700 CaptivePortalService::RecheckPolicy* recheck_policy =
701 &captive_portal_service->recheck_policy();
702 recheck_policy->initial_backoff_no_portal_ms = 0;
703 recheck_policy->initial_backoff_portal_ms = 0;
704 recheck_policy->backoff_policy.maximum_backoff_ms = 0;
705 }
706
707 bool CaptivePortalBrowserTest::CheckPending(Browser* browser) {
708 CaptivePortalService* captive_portal_service =
709 CaptivePortalServiceFactory::GetForProfile(browser->profile());
710
711 return captive_portal_service->FetchingURL() ||
712 captive_portal_service->TimerRunning();
713 }
714
715 CaptivePortalTabReloader::State CaptivePortalBrowserTest::GetStateOfTabReloader(
716 TabContents* tab_contents) const {
717 return GetTabReloader(tab_contents)->state();
718 }
719
720 CaptivePortalTabReloader::State
721 CaptivePortalBrowserTest::GetStateOfTabReloaderAt(Browser* browser,
722 int index) const {
723 return GetStateOfTabReloader(browser->GetTabContentsWrapperAt(index));
724 }
725
726 int CaptivePortalBrowserTest::NumTabsWithState(
727 CaptivePortalTabReloader::State state) const {
728 int num_tabs = 0;
729 for (TabContentsIterator tab_contents_it;
730 !tab_contents_it.done();
731 ++tab_contents_it) {
732 if (GetStateOfTabReloader(*tab_contents_it) == state)
733 ++num_tabs;
734 }
735 return num_tabs;
736 }
737
738 int CaptivePortalBrowserTest::NumBrokenTabs() const {
739 return NumTabsWithState(CaptivePortalTabReloader::STATE_BROKEN_BY_PORTAL);
740 }
741
742 int CaptivePortalBrowserTest::NumNeedReloadTabs() const {
743 return NumTabsWithState(CaptivePortalTabReloader::STATE_NEEDS_RELOAD);
744 }
745
746 void CaptivePortalBrowserTest::NavigateToPageExpectNoTest(
747 Browser* browser,
748 const GURL& url,
749 int expected_navigations) {
750 MultiNavigationObserver navigation_observer;
751 CaptivePortalObserver portal_observer(browser->profile());
752
753 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
754 browser, url, expected_navigations);
755
756 // No captive portal checks should have ocurred or be pending, and there
757 // should be no new tabs.
758 EXPECT_EQ(0, portal_observer.num_results_received());
759 EXPECT_FALSE(CheckPending(browser));
760 EXPECT_EQ(1, browser->tab_count());
761 EXPECT_EQ(expected_navigations, navigation_observer.num_navigations());
762 EXPECT_EQ(0, NumLoadingTabs());
763 }
764
765 void CaptivePortalBrowserTest::SlowLoadNoCaptivePortal(
766 Browser* browser, Result expected_result) {
767 CaptivePortalTabReloader* tab_reloader =
768 GetTabReloader(browser->GetSelectedTabContentsWrapper());
769 ASSERT_TRUE(tab_reloader);
770 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta());
771
772 MultiNavigationObserver navigation_observer;
773 CaptivePortalObserver portal_observer(browser->profile());
774 ui_test_utils::NavigateToURLWithDisposition(browser,
775 GURL(kMockHttpsUrl),
776 CURRENT_TAB,
777 ui_test_utils::BROWSER_TEST_NONE);
778
779 portal_observer.WaitForResults(1);
780
781 ASSERT_EQ(1, browser->tab_count());
782 EXPECT_EQ(expected_result, portal_observer.captive_portal_result());
783 EXPECT_EQ(1, portal_observer.num_results_received());
784 EXPECT_EQ(0, navigation_observer.num_navigations());
785 EXPECT_FALSE(CheckPending(browser));
786
787 // First tab should still be loading.
788 EXPECT_EQ(1, NumLoadingTabs());
789
790 // Original request times out.
791 URLRequestTimeoutOnDemandJob::FailRequests();
792 navigation_observer.WaitForNavigations(1);
793
794 ASSERT_EQ(1, browser->tab_count());
795 EXPECT_EQ(1, portal_observer.num_results_received());
796 EXPECT_FALSE(CheckPending(browser));
797 EXPECT_EQ(0, NumLoadingTabs());
798
799 // Set a slow SSL load time to prevent the timer from triggering.
800 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta::FromDays(1));
801 }
802
803 void CaptivePortalBrowserTest::FastTimeoutNoCaptivePortal(
804 Browser* browser, Result expected_result) {
805 ASSERT_NE(expected_result, RESULT_BEHIND_CAPTIVE_PORTAL);
806
807 // Set the load time to be large, so the timer won't trigger. The value is
808 // not restored at the end of the function.
809 CaptivePortalTabReloader* tab_reloader =
810 GetTabReloader(browser->GetSelectedTabContentsWrapper());
811 ASSERT_TRUE(tab_reloader);
812 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta::FromHours(1));
813
814 MultiNavigationObserver navigation_observer;
815 CaptivePortalObserver portal_observer(browser->profile());
816
817 // Neither of these should be changed by the navigation.
818 int active_index = browser->active_index();
819 int expected_tab_count = browser->tab_count();
820
821 ui_test_utils::NavigateToURL(
822 browser,
823 URLRequestFailedJob::GetMockHttpsUrl(net::ERR_CONNECTION_TIMED_OUT));
824
825 // An attempt to detect a captive portal should have started by now. If not,
826 // abort early to prevent hanging.
827 ASSERT_TRUE(portal_observer.num_results_received() > 0 ||
828 CheckPending(browser));
829
830 portal_observer.WaitForResults(1);
831 navigation_observer.WaitForNavigations(1);
832
833 // Check the result.
834 EXPECT_EQ(1, portal_observer.num_results_received());
835 EXPECT_EQ(expected_result, portal_observer.captive_portal_result());
836
837 // Check that the right tab was navigated, and there were no extra
838 // navigations.
839 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
840 browser->GetWebContentsAt(active_index)));
841 EXPECT_EQ(0, NumLoadingTabs());
842
843 // Check the tab's state, and verify no captive portal check is pending.
844 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
845 GetStateOfTabReloaderAt(browser, 0));
846 EXPECT_FALSE(CheckPending(browser));
847
848 // Make sure no login tab was opened.
849 EXPECT_EQ(expected_tab_count, browser->tab_count());
850 }
851
852 void CaptivePortalBrowserTest::SlowLoadBehindCaptivePortal(
853 Browser* browser,
854 bool expect_open_login_tab) {
855 // Calling this on a tab that's waiting for a load to manually be timed out
856 // will result in a hang.
857 ASSERT_FALSE(browser->GetSelectedWebContents()->IsLoading());
858
859 // Trigger a captive portal check quickly.
860 CaptivePortalTabReloader* tab_reloader =
861 GetTabReloader(browser->GetSelectedTabContentsWrapper());
862 ASSERT_TRUE(tab_reloader);
863 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta());
864
865 // Number of tabs expected to be open after the captive portal checks
866 // have completed.
867 int initial_tab_count = browser->tab_count();
868 int initial_active_index = browser->active_index();
869 int initial_loading_tabs = NumLoadingTabs();
870 int expected_broken_tabs = NumBrokenTabs();
871 if (CaptivePortalTabReloader::STATE_BROKEN_BY_PORTAL !=
872 GetStateOfTabReloader(browser->GetSelectedTabContentsWrapper())) {
873 ++expected_broken_tabs;
874 }
875
876 MultiNavigationObserver navigation_observer;
877 CaptivePortalObserver portal_observer(browser->profile());
878 ui_test_utils::NavigateToURLWithDisposition(browser,
879 GURL(kMockHttpsUrl),
880 CURRENT_TAB,
881 ui_test_utils::BROWSER_TEST_NONE);
882
883 if (expect_open_login_tab) {
884 portal_observer.WaitForResults(2);
885 navigation_observer.WaitForNavigations(1);
886 EXPECT_EQ(2, portal_observer.num_results_received());
887
888 ASSERT_EQ(initial_tab_count + 1, browser->tab_count());
889 EXPECT_EQ(initial_tab_count, browser->active_index());
890
891 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
892 browser->GetWebContentsAt(initial_tab_count)));
893 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
894 GetStateOfTabReloader(browser->GetTabContentsWrapperAt(1)));
895 EXPECT_TRUE(IsLoginTab(browser->GetTabContentsWrapperAt(1)));
896 } else {
897 portal_observer.WaitForResults(1);
898 EXPECT_EQ(0, navigation_observer.num_navigations());
899 EXPECT_EQ(initial_active_index, browser->active_index());
900 ASSERT_EQ(initial_tab_count, browser->tab_count());
901 EXPECT_EQ(initial_active_index, browser->active_index());
902 }
903
904 EXPECT_EQ(initial_loading_tabs + 1, NumLoadingTabs());
905 EXPECT_EQ(expected_broken_tabs, NumBrokenTabs());
906 EXPECT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL,
907 portal_observer.captive_portal_result());
908 EXPECT_FALSE(CheckPending(browser));
909
910 EXPECT_EQ(CaptivePortalTabReloader::STATE_BROKEN_BY_PORTAL,
911 GetStateOfTabReloader(
912 browser->GetTabContentsWrapperAt(initial_active_index)));
913
914 // Reset the load time to be large, so the timer won't trigger on a reload.
915 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta::FromHours(1));
916 }
917
918 void CaptivePortalBrowserTest::FastTimeoutBehindCaptivePortal(
919 Browser* browser,
920 bool expect_open_login_tab) {
921 // Calling this on a tab that's waiting for a load to manually be timed out
922 // will result in a hang.
923 ASSERT_FALSE(browser->GetSelectedWebContents()->IsLoading());
924
925 // Set the load time to be large, so the timer won't trigger. The value is
926 // not restored at the end of the function.
927 CaptivePortalTabReloader* tab_reloader =
928 GetTabReloader(browser->GetSelectedTabContentsWrapper());
929 ASSERT_TRUE(tab_reloader);
930 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta::FromHours(1));
931
932 // Number of tabs expected to be open after the captive portal checks
933 // have completed.
934 int initial_tab_count = browser->tab_count();
935 int initial_active_index = browser->active_index();
936 int initial_loading_tabs = NumLoadingTabs();
937 int expected_broken_tabs = NumBrokenTabs();
938 if (CaptivePortalTabReloader::STATE_BROKEN_BY_PORTAL !=
939 GetStateOfTabReloader(browser->GetSelectedTabContentsWrapper())) {
940 ++expected_broken_tabs;
941 }
942
943 MultiNavigationObserver navigation_observer;
944 CaptivePortalObserver portal_observer(browser->profile());
945 ui_test_utils::NavigateToURLWithDisposition(browser,
946 GURL(kMockHttpsQuickTimeoutUrl),
947 CURRENT_TAB,
948 ui_test_utils::BROWSER_TEST_NONE);
949
950 if (expect_open_login_tab) {
951 portal_observer.WaitForResults(2);
952 navigation_observer.WaitForNavigations(2);
953 EXPECT_EQ(2, portal_observer.num_results_received());
954
955 ASSERT_EQ(initial_tab_count + 1, browser->tab_count());
956 EXPECT_EQ(initial_tab_count, browser->active_index());
957 // Make sure that the originally active tab and the captive portal tab have
958 // each loaded once.
959 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
960 browser->GetWebContentsAt(initial_active_index)));
961 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
962 browser->GetWebContentsAt(initial_tab_count)));
963 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
964 GetStateOfTabReloader(browser->GetTabContentsWrapperAt(1)));
965 EXPECT_TRUE(IsLoginTab(browser->GetTabContentsWrapperAt(1)));
966 } else {
967 portal_observer.WaitForResults(1);
968 navigation_observer.WaitForNavigations(1);
969 EXPECT_EQ(1, portal_observer.num_results_received());
970
971 EXPECT_EQ(initial_active_index, browser->active_index());
972 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
973 browser->GetWebContentsAt(initial_active_index)));
974 ASSERT_EQ(initial_tab_count, browser->tab_count());
975 EXPECT_EQ(initial_active_index, browser->active_index());
976 }
977
978 EXPECT_EQ(initial_loading_tabs, NumLoadingTabs());
979 EXPECT_EQ(expected_broken_tabs, NumBrokenTabs());
980 EXPECT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL,
981 portal_observer.captive_portal_result());
982 EXPECT_FALSE(CheckPending(browser));
983
984 EXPECT_EQ(CaptivePortalTabReloader::STATE_BROKEN_BY_PORTAL,
985 GetStateOfTabReloader(
986 browser->GetTabContentsWrapperAt(initial_active_index)));
987 }
988
989 void CaptivePortalBrowserTest::NavigateLoginTab(Browser* browser,
990 int num_loading_tabs,
991 int num_timed_out_tabs) {
992 MultiNavigationObserver navigation_observer;
993 CaptivePortalObserver portal_observer(browser->profile());
994
995 int initial_tab_count = browser->tab_count();
996 EXPECT_EQ(num_loading_tabs, NumLoadingTabs());
997 EXPECT_EQ(num_timed_out_tabs, NumBrokenTabs() - NumLoadingTabs());
998
999 int login_tab_index = browser->active_index();
1000 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1001 GetStateOfTabReloader(browser->GetSelectedTabContentsWrapper()));
1002 ASSERT_TRUE(IsLoginTab(browser->GetSelectedTabContentsWrapper()));
1003
1004 // Do the navigation.
1005 content::RenderViewHost* render_view_host =
1006 browser->GetSelectedWebContents()->GetRenderViewHost();
1007 render_view_host->ExecuteJavascriptInWebFrame(
1008 string16(),
1009 ASCIIToUTF16("submitForm()"));
1010
1011 portal_observer.WaitForResults(1);
1012 navigation_observer.WaitForNavigations(1);
1013
1014 // Check the captive portal result.
1015 EXPECT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL,
1016 portal_observer.captive_portal_result());
1017 EXPECT_EQ(1, portal_observer.num_results_received());
1018 EXPECT_FALSE(CheckPending(browser));
1019
1020 // Make sure not much has changed.
1021 EXPECT_EQ(initial_tab_count, browser->tab_count());
1022 EXPECT_EQ(num_loading_tabs, NumLoadingTabs());
1023 EXPECT_EQ(num_loading_tabs + num_timed_out_tabs, NumBrokenTabs());
1024 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1025 GetStateOfTabReloader(browser->GetTabContentsWrapperAt(
1026 login_tab_index)));
1027 EXPECT_TRUE(IsLoginTab(browser->GetTabContentsWrapperAt(login_tab_index)));
1028
1029 // Make sure there were no unexpected navigations.
1030 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
1031 browser->GetWebContentsAt(login_tab_index)));
1032 }
1033
1034 void CaptivePortalBrowserTest::Login(Browser* browser,
1035 int num_loading_tabs,
1036 int num_timed_out_tabs) {
1037 // Simulate logging in.
1038 URLRequestMockCaptivePortalJobFactory::SetBehindCaptivePortal(false);
1039
1040 MultiNavigationObserver navigation_observer;
1041 CaptivePortalObserver portal_observer(browser->profile());
1042
1043 int initial_tab_count = browser->tab_count();
1044 ASSERT_EQ(num_loading_tabs, NumLoadingTabs());
1045 EXPECT_EQ(num_timed_out_tabs, NumBrokenTabs() - NumLoadingTabs());
1046
1047 // Verify that the login page is on top.
1048 int login_tab_index = browser->active_index();
1049 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1050 GetStateOfTabReloader(browser->GetTabContentsWrapperAt(
1051 login_tab_index)));
1052 ASSERT_TRUE(IsLoginTab(browser->GetTabContentsWrapperAt(login_tab_index)));
1053
1054 // Trigger a navigation.
1055 content::RenderViewHost* render_view_host =
1056 browser->GetSelectedWebContents()->GetRenderViewHost();
1057 render_view_host->ExecuteJavascriptInWebFrame(
1058 string16(),
1059 ASCIIToUTF16("submitForm()"));
1060
1061 portal_observer.WaitForResults(1);
1062
1063 // Wait for all the timed out tabs to reload.
1064 navigation_observer.WaitForNavigations(1 + num_timed_out_tabs);
1065 EXPECT_EQ(1, portal_observer.num_results_received());
1066
1067 // The tabs that were loading before should still be loading, and now be in
1068 // STATE_NEEDS_RELOAD.
1069 EXPECT_EQ(0, NumBrokenTabs());
1070 EXPECT_EQ(num_loading_tabs, NumLoadingTabs());
1071 EXPECT_EQ(num_loading_tabs, NumNeedReloadTabs());
1072
1073 // Make sure that the broken tabs have reloaded, and there's no more
1074 // captive portal tab.
1075 EXPECT_EQ(initial_tab_count, browser->tab_count());
1076 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1077 GetStateOfTabReloaderAt(browser, login_tab_index));
1078 EXPECT_FALSE(IsLoginTab(browser->GetTabContentsWrapperAt(login_tab_index)));
1079
1080 // Make sure there were no unexpected navigations of the login tab.
1081 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
1082 browser->GetWebContentsAt(login_tab_index)));
1083 }
1084
1085 void CaptivePortalBrowserTest::FailLoadsAfterLogin(Browser* browser,
1086 int num_loading_tabs) {
1087 ASSERT_EQ(num_loading_tabs, NumLoadingTabs());
1088 ASSERT_EQ(num_loading_tabs, NumNeedReloadTabs());
1089 EXPECT_EQ(0, NumBrokenTabs());
1090
1091 int initial_num_tabs = browser->tab_count();
1092 int initial_active_tab = browser->active_index();
1093
1094 CaptivePortalObserver portal_observer(browser->profile());
1095 MultiNavigationObserver navigation_observer;
1096 // Connection finally times out.
1097 URLRequestTimeoutOnDemandJob::FailRequests();
1098
1099 navigation_observer.WaitForNavigations(2 * num_loading_tabs);
1100
1101 // No captive portal checks should have ocurred or be pending, and there
1102 // should be no new tabs.
1103 EXPECT_EQ(0, portal_observer.num_results_received());
1104 EXPECT_FALSE(CheckPending(browser));
1105 EXPECT_EQ(initial_num_tabs, browser->tab_count());
1106
1107 EXPECT_EQ(initial_active_tab, browser->active_index());
1108
1109 EXPECT_EQ(0, NumNeedReloadTabs());
1110 EXPECT_EQ(0, NumLoadingTabs());
1111 EXPECT_EQ(0, navigation_observer.NumNavigationsForTab(
1112 browser->GetSelectedWebContents()));
1113 }
1114
1115 void CaptivePortalBrowserTest::FailLoadsWithoutLogin(Browser* browser,
1116 int num_loading_tabs) {
1117 ASSERT_EQ(num_loading_tabs, NumLoadingTabs());
1118 ASSERT_EQ(0, NumNeedReloadTabs());
1119 EXPECT_EQ(num_loading_tabs, NumBrokenTabs());
1120
1121 int initial_num_tabs = browser->tab_count();
1122 int login_tab = browser->active_index();
1123 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1124 GetStateOfTabReloader(browser->GetSelectedTabContentsWrapper()));
1125 ASSERT_TRUE(IsLoginTab(browser->GetSelectedTabContentsWrapper()));
1126
1127 CaptivePortalObserver portal_observer(browser->profile());
1128 MultiNavigationObserver navigation_observer;
1129 // Connection finally times out.
1130 URLRequestTimeoutOnDemandJob::FailRequests();
1131
1132 navigation_observer.WaitForNavigations(num_loading_tabs);
1133
1134 // No captive portal checks should have ocurred or be pending, and there
1135 // should be no new tabs.
1136 EXPECT_EQ(0, portal_observer.num_results_received());
1137 EXPECT_FALSE(CheckPending(browser));
1138 EXPECT_EQ(initial_num_tabs, browser->tab_count());
1139
1140 EXPECT_EQ(0, NumNeedReloadTabs());
1141 EXPECT_EQ(0, NumLoadingTabs());
1142 EXPECT_EQ(num_loading_tabs, NumBrokenTabs());
1143 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1144 GetStateOfTabReloader(browser->GetSelectedTabContentsWrapper()));
1145 EXPECT_TRUE(IsLoginTab(browser->GetSelectedTabContentsWrapper()));
1146 EXPECT_EQ(login_tab, browser->active_index());
1147
1148 EXPECT_EQ(0, navigation_observer.NumNavigationsForTab(
1149 browser->GetWebContentsAt(login_tab)));
1150 }
1151
1152 void CaptivePortalBrowserTest::SetSlowSSLLoadTime(
1153 CaptivePortalTabReloader* tab_reloader,
1154 base::TimeDelta slow_ssl_load_time) {
1155 tab_reloader->set_slow_ssl_load_time(slow_ssl_load_time);
1156 }
1157
1158 CaptivePortalTabReloader* CaptivePortalBrowserTest::GetTabReloader(
1159 TabContents* tab_contents) const {
1160 return tab_contents->captive_portal_tab_helper()->GetTabReloaderForTest();
1161 }
1162
1163 // Make sure there's no test for a captive portal on HTTP timeouts. This will
1164 // also trigger the link doctor page, which results in the load of a second
1165 // error page.
1166 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, HttpTimeout) {
1167 GURL url = URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_TIMED_OUT);
1168 NavigateToPageExpectNoTest(browser(), url, 2);
1169 }
1170
1171 // Make sure there's no check for a captive portal on HTTPS errors other than
1172 // timeouts, when they preempt the slow load timer.
1173 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, HttpsNonTimeoutError) {
1174 GURL url = URLRequestFailedJob::GetMockHttpsUrl(net::ERR_UNEXPECTED);
1175 NavigateToPageExpectNoTest(browser(), url, 1);
1176 }
1177
1178 // Make sure no captive portal test triggers on HTTPS timeouts of iframes.
1179 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, HttpsIframeTimeout) {
1180 CaptivePortalObserver portal_observer(browser()->profile());
1181
1182 // Use an HTTPS server for the top level page.
1183 net::TestServer https_server(net::TestServer::TYPE_HTTPS,
1184 net::TestServer::kLocalhost,
1185 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1186 ASSERT_TRUE(https_server.Start());
1187
1188 GURL url = https_server.GetURL(kTestServerIframeTimeoutPath);
1189 NavigateToPageExpectNoTest(browser(), url, 1);
1190 }
1191
1192 // Check the captive portal result when the test request reports a network
1193 // error. The check is triggered by a slow loading page, and the page
1194 // errors out only after getting a captive portal result.
1195 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, RequestFails) {
1196 SetUpCaptivePortalService(
1197 browser()->profile(),
1198 URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_CLOSED));
1199 SlowLoadNoCaptivePortal(browser(), RESULT_NO_RESPONSE);
1200 }
1201
1202 // Same as above, but for the rather unlikely case that the connection times out
1203 // before the timer triggers.
1204 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, RequestFailsFastTimout) {
1205 SetUpCaptivePortalService(
1206 browser()->profile(),
1207 URLRequestFailedJob::GetMockHttpUrl(net::ERR_CONNECTION_CLOSED));
1208 FastTimeoutNoCaptivePortal(browser(), RESULT_NO_RESPONSE);
1209 }
1210
1211 // Checks the case that captive portal detection is disabled.
1212 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, Disabled) {
1213 EnableCaptivePortalDetection(browser()->profile(), false);
1214 SlowLoadNoCaptivePortal(browser(), RESULT_INTERNET_CONNECTED);
1215 }
1216
1217 // Checks that we look for a captive portal on HTTPS timeouts and don't reload
1218 // the error tab when the captive portal probe gets a 204 response, indicating
1219 // there is no captive portal.
1220 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, InternetConnected) {
1221 // Can't just use SetBehindCaptivePortal(false), since then there wouldn't
1222 // be a timeout.
1223 ASSERT_TRUE(test_server()->Start());
1224 SetUpCaptivePortalService(browser()->profile(),
1225 test_server()->GetURL("nocontent"));
1226 SlowLoadNoCaptivePortal(browser(), RESULT_INTERNET_CONNECTED);
1227 }
1228
1229 // Checks that no login page is opened when the HTTP test URL redirects to an
1230 // SSL certificate error.
1231 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, RedirectSSLCertError) {
1232 // Need an HTTP TestServer to handle a dynamically created server redirect.
1233 ASSERT_TRUE(test_server()->Start());
1234
1235 net::TestServer::HTTPSOptions https_options;
1236 https_options.server_certificate =
1237 net::TestServer::HTTPSOptions::CERT_MISMATCHED_NAME;
1238 net::TestServer https_server(https_options,
1239 FilePath(FILE_PATH_LITERAL("chrome/test/data")));
1240 ASSERT_TRUE(https_server.Start());
1241
1242 GURL ssl_login_url = https_server.GetURL(kTestServerLoginPath);
1243
1244 CaptivePortalService* captive_portal_service =
1245 CaptivePortalServiceFactory::GetForProfile(browser()->profile());
1246 ASSERT_TRUE(captive_portal_service);
1247 SetUpCaptivePortalService(
1248 browser()->profile(),
1249 test_server()->GetURL(CreateServerRedirect(ssl_login_url.spec())));
1250
1251 SlowLoadNoCaptivePortal(browser(), RESULT_NO_RESPONSE);
1252 }
1253
1254 // A slow SSL load triggers a captive portal check. The user logs on before
1255 // the SSL page times out. We wait for the timeout and subsequent reload.
1256 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, Login) {
1257 // Load starts, detect captive portal and open up a login tab.
1258 SlowLoadBehindCaptivePortal(browser(), true);
1259
1260 // Log in. One loading tab, no timed out ones.
1261 Login(browser(), 1, 0);
1262
1263 string16 expected_title = ASCIIToUTF16(kInternetConnectedTitle);
1264 ui_test_utils::TitleWatcher title_watcher(
1265 browser()->GetWebContentsAt(0),
1266 expected_title);
1267
1268 // Timeout occurs, and page is automatically reloaded.
1269 FailLoadsAfterLogin(browser(), 1);
1270
1271 // Double check the tab's title.
1272 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
1273 }
1274
1275 // Same as above, except we make sure everything works with an incognito
1276 // profile. Main issues it tests for are that the incognito has its own
1277 // non-NULL captive portal service, and we open the tab in the correct
1278 // window.
1279 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, LoginIncognito) {
1280 // This will watch tabs for both profiles, but only used to make sure no
1281 // navigations occur for the non-incognito profile.
1282 MultiNavigationObserver navigation_observer;
1283 CaptivePortalObserver non_incognito_portal_observer(browser()->profile());
1284
1285 Browser* incognito_browser = CreateIncognitoBrowser();
1286 EnableCaptivePortalDetection(incognito_browser->profile(), true);
1287 SetUpCaptivePortalService(incognito_browser->profile(),
1288 GURL(kMockCaptivePortalTestUrl));
1289
1290 SlowLoadBehindCaptivePortal(incognito_browser, true);
1291
1292 EXPECT_EQ(1, browser()->tab_count());
1293 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1294 GetStateOfTabReloaderAt(browser(), 0));
1295
1296 Login(incognito_browser, 1, 0);
1297 FailLoadsAfterLogin(incognito_browser, 1);
1298
1299 EXPECT_EQ(1, browser()->tab_count());
1300 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1301 GetStateOfTabReloaderAt(browser(), 0));
1302
1303 EXPECT_EQ(0, navigation_observer.NumNavigationsForTab(
1304 browser()->GetWebContentsAt(0)));
1305 EXPECT_EQ(0, non_incognito_portal_observer.num_results_received());
1306 }
1307
1308 // The captive portal page is opened before the SSL page times out,
1309 // but the user logs in only after the page times out.
1310 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, LoginSlow) {
1311 SlowLoadBehindCaptivePortal(browser(), true);
1312 FailLoadsWithoutLogin(browser(), 1);
1313 Login(browser(), 0, 1);
1314 }
1315
1316 // Checks the unlikely case that the tab times out before the timer triggers.
1317 // This most likely won't happen, but should still work:
1318 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, LoginFastTimeout) {
1319 FastTimeoutBehindCaptivePortal(browser(), true);
1320 Login(browser(), 0, 1);
1321 }
1322
1323 // Tries navigating both the tab that encounters an SSL timeout and the
1324 // login tab twice, only logging in the second time.
1325 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, LoginExtraNavigations) {
1326 FastTimeoutBehindCaptivePortal(browser(), true);
1327
1328 // Activate the timed out tab and navigate it to a timeout again.
1329 browser()->ActivateTabAt(0, true);
1330 FastTimeoutBehindCaptivePortal(browser(), false);
1331
1332 // Activate and navigate the captive portal tab. This should not trigger a
1333 // reload of the tab with the error.
1334 browser()->ActivateTabAt(1, true);
1335 NavigateLoginTab(browser(), 0, 1);
1336
1337 // Simulate logging in.
1338 Login(browser(), 0, 1);
1339 }
1340
1341 // After the first SSL timeout, closes the login tab and makes sure it's opened
1342 // it again on a second timeout.
1343 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, CloseLoginTab) {
1344 // First load starts, opens a login tab, and then times out.
1345 SlowLoadBehindCaptivePortal(browser(), true);
1346 FailLoadsWithoutLogin(browser(), 1);
1347
1348 // Close login tab.
1349 browser()->CloseTab();
1350
1351 // Go through the standard slow load login, and make sure it still works.
1352 SlowLoadBehindCaptivePortal(browser(), true);
1353 Login(browser(), 1, 0);
1354 FailLoadsAfterLogin(browser(), 1);
1355 }
1356
1357 // Checks that two tabs with SSL timeouts in the same window work. Both
1358 // tabs only timeout after logging in.
1359 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, TwoBrokenTabs) {
1360 SlowLoadBehindCaptivePortal(browser(), true);
1361
1362 // Can't set the TabReloader HTTPS timeout on a new tab without doing some
1363 // acrobatics, so open a new tab at a normal page, and then navigate it to a
1364 // timeout.
1365 MultiNavigationObserver navigation_observer;
1366 CaptivePortalObserver portal_observer(browser()->profile());
1367 ui_test_utils::NavigateToURLWithDisposition(
1368 browser(),
1369 URLRequestMockHTTPJob::GetMockUrl(
1370 FilePath(FILE_PATH_LITERAL("title2.html"))),
1371 NEW_FOREGROUND_TAB,
1372 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1373
1374 ASSERT_EQ(3, browser()->tab_count());
1375 EXPECT_FALSE(CheckPending(browser()));
1376 EXPECT_EQ(0, portal_observer.num_results_received());
1377 EXPECT_EQ(1, NumLoadingTabs());
1378 EXPECT_EQ(1, navigation_observer.num_navigations());
1379 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
1380 browser()->GetWebContentsAt(2)));
1381 ASSERT_EQ(CaptivePortalTabReloader::STATE_BROKEN_BY_PORTAL,
1382 GetStateOfTabReloaderAt(browser(), 0));
1383 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1384 GetStateOfTabReloader(browser()->GetTabContentsWrapperAt(1)));
1385 ASSERT_TRUE(IsLoginTab(browser()->GetTabContentsWrapperAt(1)));
1386 ASSERT_EQ(CaptivePortalTabReloader::STATE_NONE,
1387 GetStateOfTabReloaderAt(browser(), 2));
1388 ASSERT_EQ(2, browser()->active_index());
1389
1390 SlowLoadBehindCaptivePortal(browser(), false);
1391
1392 browser()->ActivateTabAt(1, true);
1393 Login(browser(), 2, 0);
1394 FailLoadsAfterLogin(browser(), 2);
1395 }
1396
1397 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, AbortLoad) {
1398 // Go to the error page.
1399 SlowLoadBehindCaptivePortal(browser(), true);
1400 // The load will be destroyed without returning any result, so remove it from
1401 // the list of jobs that will timeout.
1402 URLRequestTimeoutOnDemandJob::AbandonRequests();
1403
1404 CaptivePortalObserver portal_observer(browser()->profile());
1405 MultiNavigationObserver navigation_observer;
1406
1407 // Switch back to the hung tab from the login tab, and abort the navigation.
1408 browser()->ActivateTabAt(0, true);
1409 browser()->Stop();
1410 navigation_observer.WaitForNavigations(1);
1411
1412 EXPECT_EQ(0, NumBrokenTabs());
1413 EXPECT_EQ(0, portal_observer.num_results_received());
1414 EXPECT_FALSE(CheckPending(browser()));
1415 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1416 GetStateOfTabReloaderAt(browser(), 0));
1417
1418 browser()->ActivateTabAt(1, true);
1419 Login(browser(), 0, 0);
1420 }
1421
1422 // Checks the case where the timed out tab is successfully navigated before
1423 // logging in.
1424 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, NavigateBrokenTab) {
1425 // Go to the error page.
1426 SlowLoadBehindCaptivePortal(browser(), true);
1427 FailLoadsWithoutLogin(browser(), 1);
1428
1429 // Navigate the error tab to a non-error page.
1430 browser()->ActivateTabAt(0, true);
1431 ui_test_utils::NavigateToURL(browser(),
1432 URLRequestMockHTTPJob::GetMockUrl(
1433 FilePath(FILE_PATH_LITERAL("title2.html"))));
1434 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1435 GetStateOfTabReloaderAt(browser(), 0));
1436
1437 // Simulate logging in.
1438 browser()->ActivateTabAt(1, true);
1439 Login(browser(), 0, 0);
1440 }
1441
1442 // Navigates a broken, but still loading, tab to another timeout before logging
1443 // in.
1444 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest,
1445 NavigateBrokenToTimeoutTabWhileLoading) {
1446 // Go to the error page.
1447 SlowLoadBehindCaptivePortal(browser(), true);
1448 URLRequestTimeoutOnDemandJob::AbandonRequests();
1449
1450 CaptivePortalTabReloader* tab_reloader =
1451 GetTabReloader(browser()->GetTabContentsWrapperAt(0));
1452 ASSERT_TRUE(tab_reloader);
1453 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta());
1454
1455 CaptivePortalObserver portal_observer(browser()->profile());
1456 MultiNavigationObserver navigation_observer;
1457
1458 // Navigate the error tab to a non-error page. Can't have ui_test_utils
1459 // do the navigation because it will wait for loading tabs to stop loading
1460 // before navigating.
1461 browser()->ActivateTabAt(0, true);
1462 browser()->OpenURL(content::OpenURLParams(GURL(kMockHttpsUrl),
1463 content::Referrer(),
1464 CURRENT_TAB,
1465 content::PAGE_TRANSITION_TYPED,
1466 false));
1467 navigation_observer.WaitForNavigations(1);
1468 portal_observer.WaitForResults(1);
1469 EXPECT_FALSE(CheckPending(browser()));
1470 EXPECT_EQ(1, NumLoadingTabs());
1471 EXPECT_EQ(CaptivePortalTabReloader::STATE_BROKEN_BY_PORTAL,
1472 GetStateOfTabReloaderAt(browser(), 0));
1473 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1474 GetStateOfTabReloader(browser()->GetTabContentsWrapperAt(1)));
1475 ASSERT_TRUE(IsLoginTab(browser()->GetTabContentsWrapperAt(1)));
1476
1477 // Simulate logging in.
1478 browser()->ActivateTabAt(1, true);
1479 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta::FromDays(1));
1480 Login(browser(), 1, 0);
1481
1482 // Timeout occurs, and page is automatically reloaded.
1483 FailLoadsAfterLogin(browser(), 1);
1484 }
1485
1486 // Checks that navigating a timed out tab back clears its state.
1487 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, GoBack) {
1488 // Navigate to a working page.
1489 ui_test_utils::NavigateToURL(
1490 browser(),
1491 URLRequestMockHTTPJob::GetMockUrl(
1492 FilePath(FILE_PATH_LITERAL("title2.html"))));
1493
1494 // Go to the error page.
1495 SlowLoadBehindCaptivePortal(browser(), true);
1496 FailLoadsWithoutLogin(browser(), 1);
1497
1498 CaptivePortalObserver portal_observer(browser()->profile());
1499 MultiNavigationObserver navigation_observer;
1500
1501 // Activate the error page tab again and go back.
1502 browser()->ActivateTabAt(0, true);
1503 browser()->GoBack(CURRENT_TAB);
1504 navigation_observer.WaitForNavigations(1);
1505
1506 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
1507 browser()->GetWebContentsAt(0)));
1508 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1509 GetStateOfTabReloaderAt(browser(), 0));
1510 EXPECT_EQ(0, portal_observer.num_results_received());
1511 }
1512
1513 // Checks that navigating back to a timeout triggers captive portal detection.
1514 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, GoBackToTimeout) {
1515 // Disable captive portal detection so the first navigation doesn't open a
1516 // login tab.
1517 EnableCaptivePortalDetection(browser()->profile(), false);
1518
1519 SlowLoadNoCaptivePortal(browser(), RESULT_INTERNET_CONNECTED);
1520
1521 // Navigate to a working page.
1522 ui_test_utils::NavigateToURL(browser(),
1523 URLRequestMockHTTPJob::GetMockUrl(
1524 FilePath(FILE_PATH_LITERAL("title2.html"))));
1525 ASSERT_EQ(CaptivePortalTabReloader::STATE_NONE,
1526 GetStateOfTabReloaderAt(browser(), 0));
1527
1528 EnableCaptivePortalDetection(browser()->profile(), true);
1529
1530 CaptivePortalTabReloader* tab_reloader =
1531 GetTabReloader(browser()->GetSelectedTabContentsWrapper());
1532 ASSERT_TRUE(tab_reloader);
1533 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta());
1534
1535 // Go to the error page.
1536 MultiNavigationObserver navigation_observer;
1537 CaptivePortalObserver portal_observer(browser()->profile());
1538 browser()->GoBack(CURRENT_TAB);
1539
1540 // Wait for both the check triggered by the broken tab and the first load
1541 // of the login tab, and for the login tab to stop loading.
1542 portal_observer.WaitForResults(2);
1543 navigation_observer.WaitForNavigations(1);
1544
1545 EXPECT_EQ(2, portal_observer.num_results_received());
1546 ASSERT_FALSE(CheckPending(browser()));
1547 ASSERT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL,
1548 portal_observer.captive_portal_result());
1549
1550 ASSERT_EQ(CaptivePortalTabReloader::STATE_BROKEN_BY_PORTAL,
1551 GetStateOfTabReloader(browser()->GetTabContentsWrapperAt(0)));
1552 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1553 GetStateOfTabReloader(browser()->GetTabContentsWrapperAt(1)));
1554 ASSERT_TRUE(IsLoginTab(browser()->GetTabContentsWrapperAt(1)));
1555
1556 ASSERT_EQ(2, browser()->tab_count());
1557 EXPECT_EQ(1, browser()->active_index());
1558 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
1559 browser()->GetWebContentsAt(1)));
1560 EXPECT_EQ(1, NumLoadingTabs());
1561
1562 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta::FromDays(1));
1563 Login(browser(), 1, 0);
1564 FailLoadsAfterLogin(browser(), 1);
1565 }
1566
1567 // Checks that reloading a timeout triggers captive portal detection.
1568 // Much like the last test, though the captive portal is disabled before
1569 // the inital navigation, rather than captive portal detection.
1570 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, ReloadTimeout) {
1571 URLRequestMockCaptivePortalJobFactory::SetBehindCaptivePortal(false);
1572
1573 // Do the first navigation while not behind a captive portal.
1574 CaptivePortalObserver portal_observer(browser()->profile());
1575 ui_test_utils::NavigateToURL(browser(), GURL(kMockHttpsUrl));
1576 ASSERT_EQ(0, portal_observer.num_results_received());
1577 ASSERT_EQ(1, browser()->tab_count());
1578
1579 // A captive portal spontaneously appears.
1580 URLRequestMockCaptivePortalJobFactory::SetBehindCaptivePortal(true);
1581
1582 CaptivePortalTabReloader* tab_reloader =
1583 GetTabReloader(browser()->GetSelectedTabContentsWrapper());
1584 ASSERT_TRUE(tab_reloader);
1585 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta());
1586
1587 MultiNavigationObserver navigation_observer;
1588 browser()->GetSelectedWebContents()->GetController().Reload(true);
1589
1590 // Wait for the login tab to open, and the two captive portal results from
1591 // opening an it.
1592 portal_observer.WaitForResults(2);
1593 navigation_observer.WaitForNavigations(1);
1594
1595 ASSERT_EQ(2, portal_observer.num_results_received());
1596 ASSERT_FALSE(CheckPending(browser()));
1597 ASSERT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL,
1598 portal_observer.captive_portal_result());
1599
1600 ASSERT_EQ(CaptivePortalTabReloader::STATE_BROKEN_BY_PORTAL,
1601 GetStateOfTabReloader(browser()->GetTabContentsWrapperAt(0)));
1602 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1603 GetStateOfTabReloader(browser()->GetTabContentsWrapperAt(1)));
1604 ASSERT_TRUE(IsLoginTab(browser()->GetTabContentsWrapperAt(1)));
1605
1606 ASSERT_EQ(2, browser()->tab_count());
1607 EXPECT_EQ(1, browser()->active_index());
1608 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
1609 browser()->GetWebContentsAt(1)));
1610 EXPECT_EQ(1, NumLoadingTabs());
1611
1612 SetSlowSSLLoadTime(tab_reloader, base::TimeDelta::FromDays(1));
1613 Login(browser(), 1, 0);
1614 FailLoadsAfterLogin(browser(), 1);
1615 }
1616
1617 // Checks the case where there are two windows, and there's an SSL timeout in
1618 // the background one.
1619 IN_PROC_BROWSER_TEST_F(CaptivePortalBrowserTest, TwoWindows) {
1620 Browser* browser2 = Browser::Create(browser()->profile());
1621 // Navigate the new browser window so it'll be shown and we can pick the
1622 // active window.
1623 ui_test_utils::NavigateToURL(browser2, GURL(chrome::kAboutBlankURL));
1624
1625 // Generally, |browser2| will be the active window. However, if the
1626 // original browser window lost focus before creating the new one, such as
1627 // when running multiple tests at once, the original browser window may
1628 // remain the profile's active window.
1629 Browser* active_browser =
1630 browser::FindTabbedBrowser(browser()->profile(), true);
1631 Browser* inactive_browser;
1632 if (active_browser == browser2) {
1633 // When only one test is running at a time, the new browser will probably be
1634 // on top, but when multiple tests are running at once, this is not
1635 // guaranteed.
1636 inactive_browser = browser();
1637 } else {
1638 ASSERT_EQ(active_browser, browser());
1639 inactive_browser = browser2;
1640 }
1641
1642 CaptivePortalObserver portal_observer(browser()->profile());
1643 MultiNavigationObserver navigation_observer;
1644
1645 // Navigate the tab in the inactive browser to an SSL timeout. Have to use
1646 // browser::NavigateParams and NEW_BACKGROUND_TAB to avoid activating the
1647 // window.
1648 browser::NavigateParams params(inactive_browser,
1649 GURL(kMockHttpsQuickTimeoutUrl),
1650 content::PAGE_TRANSITION_TYPED);
1651 params.disposition = NEW_BACKGROUND_TAB;
1652 params.window_action = browser::NavigateParams::NO_ACTION;
1653 ui_test_utils::NavigateToURL(&params);
1654 navigation_observer.WaitForNavigations(2);
1655
1656 // Make sure the active window hasn't changed, and its new tab is
1657 // active.
1658 ASSERT_EQ(active_browser,
1659 browser::FindTabbedBrowser(browser()->profile(), true));
1660 ASSERT_EQ(1, active_browser->active_index());
1661
1662 // Check that the only two navigated tabs were the new error tab in the
1663 // backround windows, and the login tab in the active window.
1664 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
1665 inactive_browser->GetWebContentsAt(1)));
1666 EXPECT_EQ(1, navigation_observer.NumNavigationsForTab(
1667 active_browser->GetWebContentsAt(1)));
1668 EXPECT_EQ(0, NumLoadingTabs());
1669
1670 // Check captive portal test results.
1671 portal_observer.WaitForResults(2);
1672 ASSERT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL,
1673 portal_observer.captive_portal_result());
1674 EXPECT_EQ(2, portal_observer.num_results_received());
1675
1676 // Check the inactive browser.
1677 EXPECT_EQ(2, inactive_browser->tab_count());
1678 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1679 GetStateOfTabReloaderAt(inactive_browser, 0));
1680 EXPECT_EQ(CaptivePortalTabReloader::STATE_BROKEN_BY_PORTAL,
1681 GetStateOfTabReloaderAt(inactive_browser, 1));
1682
1683 // Check the active browser.
1684 ASSERT_EQ(2, active_browser->tab_count());
1685 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1686 GetStateOfTabReloaderAt(active_browser, 0));
1687 EXPECT_EQ(CaptivePortalTabReloader::STATE_NONE,
1688 GetStateOfTabReloaderAt(active_browser, 1));
1689 EXPECT_TRUE(IsLoginTab(active_browser->GetTabContentsWrapperAt(1)));
1690
1691 // Simulate logging in.
1692 Login(active_browser, 0, 1);
1693 }
1694
1695 } // namespace captive_portal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698