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

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

Issue 10581012: Move test_url_fetcher_factory.* from content/ to net/url_request (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove export annotations 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
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/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/test/test_timeouts.h" 10 #include "base/test/test_timeouts.h"
11 #include "chrome/browser/prefs/pref_service.h" 11 #include "chrome/browser/prefs/pref_service.h"
12 #include "chrome/common/chrome_notification_types.h" 12 #include "chrome/common/chrome_notification_types.h"
13 #include "chrome/common/chrome_switches.h" 13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/pref_names.h" 14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/testing_profile.h" 15 #include "chrome/test/base/testing_profile.h"
16 #include "chrome/test/base/ui_test_utils.h" 16 #include "chrome/test/base/ui_test_utils.h"
17 #include "content/public/browser/notification_details.h" 17 #include "content/public/browser/notification_details.h"
18 #include "content/public/browser/notification_observer.h" 18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h" 19 #include "content/public/browser/notification_registrar.h"
20 #include "content/public/browser/notification_source.h" 20 #include "content/public/browser/notification_source.h"
21 #include "content/public/test/test_url_fetcher_factory.h"
22 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
23 #include "net/http/http_response_headers.h" 22 #include "net/http/http_response_headers.h"
24 #include "net/http/http_util.h" 23 #include "net/http/http_util.h"
24 #include "net/url_request/test_url_fetcher_factory.h"
25 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
26 26
27 namespace captive_portal { 27 namespace captive_portal {
28 28
29 namespace { 29 namespace {
30 30
31 // A short amount of time that some tests wait for. 31 // A short amount of time that some tests wait for.
32 const int kShortTimeMs = 10; 32 const int kShortTimeMs = 10;
33 33
34 scoped_refptr<net::HttpResponseHeaders> CreateResponseHeaders( 34 scoped_refptr<net::HttpResponseHeaders> CreateResponseHeaders(
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 base::TimeDelta expected_delay = 194 base::TimeDelta expected_delay =
195 base::TimeDelta::FromSeconds(expected_delay_secs); 195 base::TimeDelta::FromSeconds(expected_delay_secs);
196 196
197 ASSERT_EQ(CaptivePortalService::STATE_IDLE, service()->state()); 197 ASSERT_EQ(CaptivePortalService::STATE_IDLE, service()->state());
198 ASSERT_EQ(expected_delay, GetTimeUntilNextRequest()); 198 ASSERT_EQ(expected_delay, GetTimeUntilNextRequest());
199 199
200 service()->AdvanceTime(expected_delay); 200 service()->AdvanceTime(expected_delay);
201 ASSERT_EQ(base::TimeDelta(), GetTimeUntilNextRequest()); 201 ASSERT_EQ(base::TimeDelta(), GetTimeUntilNextRequest());
202 202
203 CaptivePortalObserver observer(profile(), service()); 203 CaptivePortalObserver observer(profile(), service());
204 TestURLFetcherFactory factory; 204 net::TestURLFetcherFactory factory;
205 service()->DetectCaptivePortal(); 205 service()->DetectCaptivePortal();
206 206
207 EXPECT_EQ(CaptivePortalService::STATE_TIMER_RUNNING, service()->state()); 207 EXPECT_EQ(CaptivePortalService::STATE_TIMER_RUNNING, service()->state());
208 EXPECT_FALSE(FetchingURL()); 208 EXPECT_FALSE(FetchingURL());
209 ASSERT_TRUE(TimerRunning()); 209 ASSERT_TRUE(TimerRunning());
210 210
211 MessageLoop::current()->RunAllPending(); 211 MessageLoop::current()->RunAllPending();
212 EXPECT_EQ(CaptivePortalService::STATE_CHECKING_FOR_PORTAL, 212 EXPECT_EQ(CaptivePortalService::STATE_CHECKING_FOR_PORTAL,
213 service()->state()); 213 service()->state());
214 ASSERT_TRUE(FetchingURL()); 214 ASSERT_TRUE(FetchingURL());
215 EXPECT_FALSE(TimerRunning()); 215 EXPECT_FALSE(TimerRunning());
216 216
217 TestURLFetcher* fetcher = factory.GetFetcherByID(0); 217 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
218 if (net_error != net::OK) { 218 if (net_error != net::OK) {
219 EXPECT_FALSE(response_headers); 219 EXPECT_FALSE(response_headers);
220 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, 220 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
221 net_error)); 221 net_error));
222 } else { 222 } else {
223 fetcher->set_response_code(status_code); 223 fetcher->set_response_code(status_code);
224 if (response_headers) { 224 if (response_headers) {
225 scoped_refptr<net::HttpResponseHeaders> headers( 225 scoped_refptr<net::HttpResponseHeaders> headers(
226 CreateResponseHeaders(response_headers)); 226 CreateResponseHeaders(response_headers));
227 // Sanity check. 227 // Sanity check.
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 0, NULL); 446 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 0, NULL);
447 } 447 }
448 448
449 // Check that disabling the captive portal service while a check is running 449 // Check that disabling the captive portal service while a check is running
450 // works. 450 // works.
451 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhileRunning) { 451 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhileRunning) {
452 Initialize(true); 452 Initialize(true);
453 CaptivePortalObserver observer(profile(), service()); 453 CaptivePortalObserver observer(profile(), service());
454 454
455 // Needed to create the URLFetcher, even if it never returns any results. 455 // Needed to create the URLFetcher, even if it never returns any results.
456 TestURLFetcherFactory factory; 456 net::TestURLFetcherFactory factory;
457 service()->DetectCaptivePortal(); 457 service()->DetectCaptivePortal();
458 458
459 MessageLoop::current()->RunAllPending(); 459 MessageLoop::current()->RunAllPending();
460 EXPECT_TRUE(FetchingURL()); 460 EXPECT_TRUE(FetchingURL());
461 EXPECT_FALSE(TimerRunning()); 461 EXPECT_FALSE(TimerRunning());
462 462
463 EnableCaptivePortalDetection(false); 463 EnableCaptivePortalDetection(false);
464 EXPECT_FALSE(FetchingURL()); 464 EXPECT_FALSE(FetchingURL());
465 EXPECT_TRUE(TimerRunning()); 465 EXPECT_TRUE(TimerRunning());
466 EXPECT_EQ(0, observer.num_results_received()); 466 EXPECT_EQ(0, observer.num_results_received());
467 467
468 MessageLoop::current()->RunAllPending(); 468 MessageLoop::current()->RunAllPending();
469 469
470 EXPECT_FALSE(FetchingURL()); 470 EXPECT_FALSE(FetchingURL());
471 EXPECT_FALSE(TimerRunning()); 471 EXPECT_FALSE(TimerRunning());
472 EXPECT_EQ(1, observer.num_results_received()); 472 EXPECT_EQ(1, observer.num_results_received());
473 473
474 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); 474 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result());
475 } 475 }
476 476
477 // Check that disabling the captive portal service while a check is pending 477 // Check that disabling the captive portal service while a check is pending
478 // works. 478 // works.
479 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhilePending) { 479 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhilePending) {
480 Initialize(true); 480 Initialize(true);
481 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1)); 481 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1));
482 482
483 // Needed to create the URLFetcher, even if it never returns any results. 483 // Needed to create the URLFetcher, even if it never returns any results.
484 TestURLFetcherFactory factory; 484 net::TestURLFetcherFactory factory;
485 485
486 CaptivePortalObserver observer(profile(), service()); 486 CaptivePortalObserver observer(profile(), service());
487 service()->DetectCaptivePortal(); 487 service()->DetectCaptivePortal();
488 EXPECT_FALSE(FetchingURL()); 488 EXPECT_FALSE(FetchingURL());
489 EXPECT_TRUE(TimerRunning()); 489 EXPECT_TRUE(TimerRunning());
490 490
491 EnableCaptivePortalDetection(false); 491 EnableCaptivePortalDetection(false);
492 EXPECT_FALSE(FetchingURL()); 492 EXPECT_FALSE(FetchingURL());
493 EXPECT_TRUE(TimerRunning()); 493 EXPECT_TRUE(TimerRunning());
494 EXPECT_EQ(0, observer.num_results_received()); 494 EXPECT_EQ(0, observer.num_results_received());
(...skipping 13 matching lines...) Expand all
508 Initialize(true); 508 Initialize(true);
509 509
510 EnableCaptivePortalDetection(false); 510 EnableCaptivePortalDetection(false);
511 RunDisabledTest(0); 511 RunDisabledTest(0);
512 512
513 CaptivePortalObserver observer(profile(), service()); 513 CaptivePortalObserver observer(profile(), service());
514 service()->DetectCaptivePortal(); 514 service()->DetectCaptivePortal();
515 EXPECT_FALSE(FetchingURL()); 515 EXPECT_FALSE(FetchingURL());
516 EXPECT_TRUE(TimerRunning()); 516 EXPECT_TRUE(TimerRunning());
517 517
518 TestURLFetcherFactory factory; 518 net::TestURLFetcherFactory factory;
519 519
520 EnableCaptivePortalDetection(true); 520 EnableCaptivePortalDetection(true);
521 EXPECT_FALSE(FetchingURL()); 521 EXPECT_FALSE(FetchingURL());
522 EXPECT_TRUE(TimerRunning()); 522 EXPECT_TRUE(TimerRunning());
523 523
524 MessageLoop::current()->RunAllPending(); 524 MessageLoop::current()->RunAllPending();
525 ASSERT_TRUE(FetchingURL()); 525 ASSERT_TRUE(FetchingURL());
526 EXPECT_FALSE(TimerRunning()); 526 EXPECT_FALSE(TimerRunning());
527 527
528 TestURLFetcher* fetcher = factory.GetFetcherByID(0); 528 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
529 fetcher->set_response_code(200); 529 fetcher->set_response_code(200);
530 OnURLFetchComplete(fetcher); 530 OnURLFetchComplete(fetcher);
531 EXPECT_FALSE(FetchingURL()); 531 EXPECT_FALSE(FetchingURL());
532 EXPECT_FALSE(TimerRunning()); 532 EXPECT_FALSE(TimerRunning());
533 533
534 EXPECT_EQ(1, observer.num_results_received()); 534 EXPECT_EQ(1, observer.num_results_received());
535 EXPECT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL, observer.captive_portal_result()); 535 EXPECT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL, observer.captive_portal_result());
536 } 536 }
537 537
538 // Checks that disabling with a command line flag works as expected. 538 // Checks that disabling with a command line flag works as expected.
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
614 614
615 RunTest(RESULT_NO_RESPONSE, 615 RunTest(RESULT_NO_RESPONSE,
616 net::OK, 616 net::OK,
617 503, 617 503,
618 0, 618 0,
619 "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n"); 619 "HTTP/1.1 503 OK\nRetry-After: Christmas\n\n");
620 EXPECT_EQ(base::TimeDelta::FromSeconds(100), GetTimeUntilNextRequest()); 620 EXPECT_EQ(base::TimeDelta::FromSeconds(100), GetTimeUntilNextRequest());
621 } 621 }
622 622
623 } // namespace captive_portal 623 } // namespace captive_portal
OLDNEW
« no previous file with comments | « chrome/browser/autofill/autofill_download_unittest.cc ('k') | chrome/browser/chromeos/login/login_utils_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698