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

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

Issue 11419070: Added detection timeouts and usage of Retry-After HTTP header. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix. Created 8 years, 1 month 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/captive_portal/captive_portal_service.h" 5 #include "chrome/browser/captive_portal/captive_portal_service.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/test/test_timeouts.h" 11 #include "base/test/test_timeouts.h"
12 #include "chrome/browser/captive_portal/testing_utils.h" 12 #include "chrome/browser/captive_portal/testing_utils.h"
13 #include "chrome/browser/prefs/pref_service.h" 13 #include "chrome/browser/prefs/pref_service.h"
14 #include "chrome/common/chrome_notification_types.h" 14 #include "chrome/common/chrome_notification_types.h"
15 #include "chrome/common/chrome_switches.h" 15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/testing_profile.h" 17 #include "chrome/test/base/testing_profile.h"
18 #include "chrome/test/base/ui_test_utils.h" 18 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/browser/notification_details.h" 19 #include "content/public/browser/notification_details.h"
20 #include "content/public/browser/notification_observer.h" 20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h" 21 #include "content/public/browser/notification_registrar.h"
22 #include "content/public/browser/notification_source.h" 22 #include "content/public/browser/notification_source.h"
23 #include "net/base/net_errors.h" 23 #include "net/base/net_errors.h"
24 #include "net/http/http_response_headers.h"
25 #include "net/url_request/test_url_fetcher_factory.h"
26 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
27 25
28 namespace captive_portal { 26 namespace captive_portal {
29 27
30 namespace { 28 namespace {
31 29
32 // A short amount of time that some tests wait for. 30 // A short amount of time that some tests wait for.
33 const int kShortTimeMs = 10; 31 const int kShortTimeMs = 10;
34 32
35 // An observer watches the CaptivePortalDetector. It tracks the last 33 // An observer watches the CaptivePortalDetector. It tracks the last
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 base::TimeDelta expected_delay = 154 base::TimeDelta expected_delay =
157 base::TimeDelta::FromSeconds(expected_delay_secs); 155 base::TimeDelta::FromSeconds(expected_delay_secs);
158 156
159 ASSERT_EQ(CaptivePortalService::STATE_IDLE, service()->state()); 157 ASSERT_EQ(CaptivePortalService::STATE_IDLE, service()->state());
160 ASSERT_EQ(expected_delay, GetTimeUntilNextRequest()); 158 ASSERT_EQ(expected_delay, GetTimeUntilNextRequest());
161 159
162 AdvanceTime(expected_delay); 160 AdvanceTime(expected_delay);
163 ASSERT_EQ(base::TimeDelta(), GetTimeUntilNextRequest()); 161 ASSERT_EQ(base::TimeDelta(), GetTimeUntilNextRequest());
164 162
165 CaptivePortalObserver observer(profile(), service()); 163 CaptivePortalObserver observer(profile(), service());
166 net::TestURLFetcherFactory factory;
167 service()->DetectCaptivePortal(); 164 service()->DetectCaptivePortal();
168 165
169 EXPECT_EQ(CaptivePortalService::STATE_TIMER_RUNNING, service()->state()); 166 EXPECT_EQ(CaptivePortalService::STATE_TIMER_RUNNING, service()->state());
170 EXPECT_FALSE(FetchingURL()); 167 EXPECT_FALSE(FetchingURL());
171 ASSERT_TRUE(TimerRunning()); 168 ASSERT_TRUE(TimerRunning());
172 169
173 MessageLoop::current()->RunUntilIdle(); 170 MessageLoop::current()->RunUntilIdle();
174 EXPECT_EQ(CaptivePortalService::STATE_CHECKING_FOR_PORTAL, 171 EXPECT_EQ(CaptivePortalService::STATE_CHECKING_FOR_PORTAL,
175 service()->state()); 172 service()->state());
176 ASSERT_TRUE(FetchingURL()); 173 ASSERT_TRUE(FetchingURL());
177 EXPECT_FALSE(TimerRunning()); 174 EXPECT_FALSE(TimerRunning());
178 175
179 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 176 CompleteURLFetch(net_error, status_code, response_headers);
180 if (net_error != net::OK) {
181 EXPECT_FALSE(response_headers);
182 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
183 net_error));
184 } else {
185 fetcher->set_response_code(status_code);
186 if (response_headers) {
187 scoped_refptr<net::HttpResponseHeaders> headers(
188 CreateResponseHeaders(response_headers));
189 // Sanity check.
190 EXPECT_EQ(status_code, headers->response_code());
191 fetcher->set_response_headers(headers);
192 }
193 }
194
195 OnURLFetchComplete(fetcher);
196 177
197 EXPECT_FALSE(FetchingURL()); 178 EXPECT_FALSE(FetchingURL());
198 EXPECT_FALSE(TimerRunning()); 179 EXPECT_FALSE(TimerRunning());
199 EXPECT_EQ(1, observer.num_results_received()); 180 EXPECT_EQ(1, observer.num_results_received());
200 EXPECT_EQ(expected_result, observer.captive_portal_result()); 181 EXPECT_EQ(expected_result, observer.captive_portal_result());
201 } 182 }
202 183
203 // Runs a test when the captive portal service is disabled. 184 // Runs a test when the captive portal service is disabled.
204 void RunDisabledTest(int expected_delay_secs) { 185 void RunDisabledTest(int expected_delay_secs) {
205 base::TimeDelta expected_delay = 186 base::TimeDelta expected_delay =
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 0, NULL); 363 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 0, NULL);
383 } 364 }
384 365
385 // Check that disabling the captive portal service while a check is running 366 // Check that disabling the captive portal service while a check is running
386 // works. 367 // works.
387 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhileRunning) { 368 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhileRunning) {
388 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 369 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
389 CaptivePortalObserver observer(profile(), service()); 370 CaptivePortalObserver observer(profile(), service());
390 371
391 // Needed to create the URLFetcher, even if it never returns any results. 372 // Needed to create the URLFetcher, even if it never returns any results.
392 net::TestURLFetcherFactory factory;
393 service()->DetectCaptivePortal(); 373 service()->DetectCaptivePortal();
394 374
395 MessageLoop::current()->RunUntilIdle(); 375 MessageLoop::current()->RunUntilIdle();
396 EXPECT_TRUE(FetchingURL()); 376 EXPECT_TRUE(FetchingURL());
397 EXPECT_FALSE(TimerRunning()); 377 EXPECT_FALSE(TimerRunning());
398 378
399 EnableCaptivePortalDetectionPreference(false); 379 EnableCaptivePortalDetectionPreference(false);
400 EXPECT_FALSE(FetchingURL()); 380 EXPECT_FALSE(FetchingURL());
401 EXPECT_TRUE(TimerRunning()); 381 EXPECT_TRUE(TimerRunning());
402 EXPECT_EQ(0, observer.num_results_received()); 382 EXPECT_EQ(0, observer.num_results_received());
403 383
404 MessageLoop::current()->RunUntilIdle(); 384 MessageLoop::current()->RunUntilIdle();
405 385
406 EXPECT_FALSE(FetchingURL()); 386 EXPECT_FALSE(FetchingURL());
407 EXPECT_FALSE(TimerRunning()); 387 EXPECT_FALSE(TimerRunning());
408 EXPECT_EQ(1, observer.num_results_received()); 388 EXPECT_EQ(1, observer.num_results_received());
409 389
410 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); 390 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result());
411 } 391 }
412 392
413 // Check that disabling the captive portal service while a check is pending 393 // Check that disabling the captive portal service while a check is pending
414 // works. 394 // works.
415 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhilePending) { 395 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhilePending) {
416 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 396 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
417 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1)); 397 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1));
418 398
419 // Needed to create the URLFetcher, even if it never returns any results.
420 net::TestURLFetcherFactory factory;
421
422 CaptivePortalObserver observer(profile(), service()); 399 CaptivePortalObserver observer(profile(), service());
423 service()->DetectCaptivePortal(); 400 service()->DetectCaptivePortal();
424 EXPECT_FALSE(FetchingURL()); 401 EXPECT_FALSE(FetchingURL());
425 EXPECT_TRUE(TimerRunning()); 402 EXPECT_TRUE(TimerRunning());
426 403
427 EnableCaptivePortalDetectionPreference(false); 404 EnableCaptivePortalDetectionPreference(false);
428 EXPECT_FALSE(FetchingURL()); 405 EXPECT_FALSE(FetchingURL());
429 EXPECT_TRUE(TimerRunning()); 406 EXPECT_TRUE(TimerRunning());
430 EXPECT_EQ(0, observer.num_results_received()); 407 EXPECT_EQ(0, observer.num_results_received());
431 408
(...skipping 12 matching lines...) Expand all
444 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 421 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
445 422
446 EnableCaptivePortalDetectionPreference(false); 423 EnableCaptivePortalDetectionPreference(false);
447 RunDisabledTest(0); 424 RunDisabledTest(0);
448 425
449 CaptivePortalObserver observer(profile(), service()); 426 CaptivePortalObserver observer(profile(), service());
450 service()->DetectCaptivePortal(); 427 service()->DetectCaptivePortal();
451 EXPECT_FALSE(FetchingURL()); 428 EXPECT_FALSE(FetchingURL());
452 EXPECT_TRUE(TimerRunning()); 429 EXPECT_TRUE(TimerRunning());
453 430
454 net::TestURLFetcherFactory factory;
455
456 EnableCaptivePortalDetectionPreference(true); 431 EnableCaptivePortalDetectionPreference(true);
457 EXPECT_FALSE(FetchingURL()); 432 EXPECT_FALSE(FetchingURL());
458 EXPECT_TRUE(TimerRunning()); 433 EXPECT_TRUE(TimerRunning());
459 434
460 MessageLoop::current()->RunUntilIdle(); 435 MessageLoop::current()->RunUntilIdle();
461 ASSERT_TRUE(FetchingURL()); 436 ASSERT_TRUE(FetchingURL());
462 EXPECT_FALSE(TimerRunning()); 437 EXPECT_FALSE(TimerRunning());
463 438
464 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 439 CompleteURLFetch(net::OK, 200, NULL);
465 fetcher->set_response_code(200);
466 OnURLFetchComplete(fetcher);
467 EXPECT_FALSE(FetchingURL()); 440 EXPECT_FALSE(FetchingURL());
468 EXPECT_FALSE(TimerRunning()); 441 EXPECT_FALSE(TimerRunning());
469 442
470 EXPECT_EQ(1, observer.num_results_received()); 443 EXPECT_EQ(1, observer.num_results_received());
471 EXPECT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL, observer.captive_portal_result()); 444 EXPECT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL, observer.captive_portal_result());
472 } 445 }
473 446
474 // Checks that disabling for browser tests works as expected. 447 // Checks that disabling for browser tests works as expected.
475 TEST_F(CaptivePortalServiceTest, CaptivePortalDisableForTests) { 448 TEST_F(CaptivePortalServiceTest, CaptivePortalDisableForTests) {
476 Initialize(CaptivePortalService::DISABLED_FOR_TESTING); 449 Initialize(CaptivePortalService::DISABLED_FOR_TESTING);
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
537 510
538 RunTest(RESULT_NO_RESPONSE, 511 RunTest(RESULT_NO_RESPONSE,
539 net::OK, 512 net::OK,
540 503, 513 503,
541 0, 514 0,
542 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n"); 515 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n");
543 EXPECT_EQ(base::TimeDelta::FromSeconds(51), GetTimeUntilNextRequest()); 516 EXPECT_EQ(base::TimeDelta::FromSeconds(51), GetTimeUntilNextRequest());
544 } 517 }
545 518
546 } // namespace captive_portal 519 } // namespace captive_portal
OLDNEW
« no previous file with comments | « chrome/browser/captive_portal/captive_portal_service.cc ('k') | chrome/browser/captive_portal/testing_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698