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

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

Issue 14113053: chrome: Use base::MessageLoop. (Part 3) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase again Created 7 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/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"
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 AdvanceTime(expected_delay); 160 AdvanceTime(expected_delay);
161 ASSERT_EQ(base::TimeDelta(), GetTimeUntilNextRequest()); 161 ASSERT_EQ(base::TimeDelta(), GetTimeUntilNextRequest());
162 162
163 CaptivePortalObserver observer(profile(), service()); 163 CaptivePortalObserver observer(profile(), service());
164 service()->DetectCaptivePortal(); 164 service()->DetectCaptivePortal();
165 165
166 EXPECT_EQ(CaptivePortalService::STATE_TIMER_RUNNING, service()->state()); 166 EXPECT_EQ(CaptivePortalService::STATE_TIMER_RUNNING, service()->state());
167 EXPECT_FALSE(FetchingURL()); 167 EXPECT_FALSE(FetchingURL());
168 ASSERT_TRUE(TimerRunning()); 168 ASSERT_TRUE(TimerRunning());
169 169
170 MessageLoop::current()->RunUntilIdle(); 170 base::MessageLoop::current()->RunUntilIdle();
171 EXPECT_EQ(CaptivePortalService::STATE_CHECKING_FOR_PORTAL, 171 EXPECT_EQ(CaptivePortalService::STATE_CHECKING_FOR_PORTAL,
172 service()->state()); 172 service()->state());
173 ASSERT_TRUE(FetchingURL()); 173 ASSERT_TRUE(FetchingURL());
174 EXPECT_FALSE(TimerRunning()); 174 EXPECT_FALSE(TimerRunning());
175 175
176 CompleteURLFetch(net_error, status_code, response_headers); 176 CompleteURLFetch(net_error, status_code, response_headers);
177 177
178 EXPECT_FALSE(FetchingURL()); 178 EXPECT_FALSE(FetchingURL());
179 EXPECT_FALSE(TimerRunning()); 179 EXPECT_FALSE(TimerRunning());
180 EXPECT_EQ(1, observer.num_results_received()); 180 EXPECT_EQ(1, observer.num_results_received());
(...skipping 11 matching lines...) Expand all
192 AdvanceTime(expected_delay); 192 AdvanceTime(expected_delay);
193 ASSERT_EQ(base::TimeDelta(), GetTimeUntilNextRequest()); 193 ASSERT_EQ(base::TimeDelta(), GetTimeUntilNextRequest());
194 194
195 CaptivePortalObserver observer(profile(), service()); 195 CaptivePortalObserver observer(profile(), service());
196 service()->DetectCaptivePortal(); 196 service()->DetectCaptivePortal();
197 197
198 EXPECT_EQ(CaptivePortalService::STATE_TIMER_RUNNING, service()->state()); 198 EXPECT_EQ(CaptivePortalService::STATE_TIMER_RUNNING, service()->state());
199 EXPECT_FALSE(FetchingURL()); 199 EXPECT_FALSE(FetchingURL());
200 ASSERT_TRUE(TimerRunning()); 200 ASSERT_TRUE(TimerRunning());
201 201
202 MessageLoop::current()->RunUntilIdle(); 202 base::MessageLoop::current()->RunUntilIdle();
203 EXPECT_FALSE(FetchingURL()); 203 EXPECT_FALSE(FetchingURL());
204 EXPECT_FALSE(TimerRunning()); 204 EXPECT_FALSE(TimerRunning());
205 EXPECT_EQ(1, observer.num_results_received()); 205 EXPECT_EQ(1, observer.num_results_received());
206 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); 206 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result());
207 } 207 }
208 208
209 // Tests exponential backoff. Prior to calling, the relevant recheck settings 209 // Tests exponential backoff. Prior to calling, the relevant recheck settings
210 // must be set to have a minimum time of 100 seconds, with 2 checks before 210 // must be set to have a minimum time of 100 seconds, with 2 checks before
211 // starting exponential backoff. 211 // starting exponential backoff.
212 void RunBackoffTest(Result expected_result, int net_error, int status_code) { 212 void RunBackoffTest(Result expected_result, int net_error, int status_code) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 267
268 TestingProfile* profile() { return profile_.get(); } 268 TestingProfile* profile() { return profile_.get(); }
269 269
270 CaptivePortalService* service() { return service_.get(); } 270 CaptivePortalService* service() { return service_.get(); }
271 271
272 private: 272 private:
273 // Stores the initial CaptivePortalService::TestingState so it can be restored 273 // Stores the initial CaptivePortalService::TestingState so it can be restored
274 // after the test. 274 // after the test.
275 const CaptivePortalService::TestingState old_captive_portal_testing_state_; 275 const CaptivePortalService::TestingState old_captive_portal_testing_state_;
276 276
277 MessageLoop message_loop_; 277 base::MessageLoop message_loop_;
278 278
279 // Note that the construction order of these matters. 279 // Note that the construction order of these matters.
280 scoped_ptr<TestingProfile> profile_; 280 scoped_ptr<TestingProfile> profile_;
281 scoped_ptr<CaptivePortalService> service_; 281 scoped_ptr<CaptivePortalService> service_;
282 }; 282 };
283 283
284 // Verify that an observer doesn't get messages from the wrong profile. 284 // Verify that an observer doesn't get messages from the wrong profile.
285 TEST_F(CaptivePortalServiceTest, CaptivePortalTwoProfiles) { 285 TEST_F(CaptivePortalServiceTest, CaptivePortalTwoProfiles) {
286 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 286 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
287 TestingProfile profile2; 287 TestingProfile profile2;
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 365
366 // 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
367 // works. 367 // works.
368 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhileRunning) { 368 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhileRunning) {
369 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 369 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
370 CaptivePortalObserver observer(profile(), service()); 370 CaptivePortalObserver observer(profile(), service());
371 371
372 // 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.
373 service()->DetectCaptivePortal(); 373 service()->DetectCaptivePortal();
374 374
375 MessageLoop::current()->RunUntilIdle(); 375 base::MessageLoop::current()->RunUntilIdle();
376 EXPECT_TRUE(FetchingURL()); 376 EXPECT_TRUE(FetchingURL());
377 EXPECT_FALSE(TimerRunning()); 377 EXPECT_FALSE(TimerRunning());
378 378
379 EnableCaptivePortalDetectionPreference(false); 379 EnableCaptivePortalDetectionPreference(false);
380 EXPECT_FALSE(FetchingURL()); 380 EXPECT_FALSE(FetchingURL());
381 EXPECT_TRUE(TimerRunning()); 381 EXPECT_TRUE(TimerRunning());
382 EXPECT_EQ(0, observer.num_results_received()); 382 EXPECT_EQ(0, observer.num_results_received());
383 383
384 MessageLoop::current()->RunUntilIdle(); 384 base::MessageLoop::current()->RunUntilIdle();
385 385
386 EXPECT_FALSE(FetchingURL()); 386 EXPECT_FALSE(FetchingURL());
387 EXPECT_FALSE(TimerRunning()); 387 EXPECT_FALSE(TimerRunning());
388 EXPECT_EQ(1, observer.num_results_received()); 388 EXPECT_EQ(1, observer.num_results_received());
389 389
390 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); 390 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result());
391 } 391 }
392 392
393 // 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
394 // works. 394 // works.
395 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhilePending) { 395 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhilePending) {
396 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 396 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
397 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1)); 397 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1));
398 398
399 CaptivePortalObserver observer(profile(), service()); 399 CaptivePortalObserver observer(profile(), service());
400 service()->DetectCaptivePortal(); 400 service()->DetectCaptivePortal();
401 EXPECT_FALSE(FetchingURL()); 401 EXPECT_FALSE(FetchingURL());
402 EXPECT_TRUE(TimerRunning()); 402 EXPECT_TRUE(TimerRunning());
403 403
404 EnableCaptivePortalDetectionPreference(false); 404 EnableCaptivePortalDetectionPreference(false);
405 EXPECT_FALSE(FetchingURL()); 405 EXPECT_FALSE(FetchingURL());
406 EXPECT_TRUE(TimerRunning()); 406 EXPECT_TRUE(TimerRunning());
407 EXPECT_EQ(0, observer.num_results_received()); 407 EXPECT_EQ(0, observer.num_results_received());
408 408
409 MessageLoop::current()->RunUntilIdle(); 409 base::MessageLoop::current()->RunUntilIdle();
410 410
411 EXPECT_FALSE(FetchingURL()); 411 EXPECT_FALSE(FetchingURL());
412 EXPECT_FALSE(TimerRunning()); 412 EXPECT_FALSE(TimerRunning());
413 EXPECT_EQ(1, observer.num_results_received()); 413 EXPECT_EQ(1, observer.num_results_received());
414 414
415 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); 415 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result());
416 } 416 }
417 417
418 // Check that disabling the captive portal service while a check is pending 418 // Check that disabling the captive portal service while a check is pending
419 // works. 419 // works.
420 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefEnabledWhilePending) { 420 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefEnabledWhilePending) {
421 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 421 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
422 422
423 EnableCaptivePortalDetectionPreference(false); 423 EnableCaptivePortalDetectionPreference(false);
424 RunDisabledTest(0); 424 RunDisabledTest(0);
425 425
426 CaptivePortalObserver observer(profile(), service()); 426 CaptivePortalObserver observer(profile(), service());
427 service()->DetectCaptivePortal(); 427 service()->DetectCaptivePortal();
428 EXPECT_FALSE(FetchingURL()); 428 EXPECT_FALSE(FetchingURL());
429 EXPECT_TRUE(TimerRunning()); 429 EXPECT_TRUE(TimerRunning());
430 430
431 EnableCaptivePortalDetectionPreference(true); 431 EnableCaptivePortalDetectionPreference(true);
432 EXPECT_FALSE(FetchingURL()); 432 EXPECT_FALSE(FetchingURL());
433 EXPECT_TRUE(TimerRunning()); 433 EXPECT_TRUE(TimerRunning());
434 434
435 MessageLoop::current()->RunUntilIdle(); 435 base::MessageLoop::current()->RunUntilIdle();
436 ASSERT_TRUE(FetchingURL()); 436 ASSERT_TRUE(FetchingURL());
437 EXPECT_FALSE(TimerRunning()); 437 EXPECT_FALSE(TimerRunning());
438 438
439 CompleteURLFetch(net::OK, 200, NULL); 439 CompleteURLFetch(net::OK, 200, NULL);
440 EXPECT_FALSE(FetchingURL()); 440 EXPECT_FALSE(FetchingURL());
441 EXPECT_FALSE(TimerRunning()); 441 EXPECT_FALSE(TimerRunning());
442 442
443 EXPECT_EQ(1, observer.num_results_received()); 443 EXPECT_EQ(1, observer.num_results_received());
444 EXPECT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL, observer.captive_portal_result()); 444 EXPECT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL, observer.captive_portal_result());
445 } 445 }
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 510
511 RunTest(RESULT_NO_RESPONSE, 511 RunTest(RESULT_NO_RESPONSE,
512 net::OK, 512 net::OK,
513 503, 513 503,
514 0, 514 0,
515 "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");
516 EXPECT_EQ(base::TimeDelta::FromSeconds(51), GetTimeUntilNextRequest()); 516 EXPECT_EQ(base::TimeDelta::FromSeconds(51), GetTimeUntilNextRequest());
517 } 517 }
518 518
519 } // namespace captive_portal 519 } // namespace captive_portal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698