OLD | NEW |
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/message_loop.h" | |
11 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/run_loop.h" |
12 #include "base/test/test_timeouts.h" | 12 #include "base/test/test_timeouts.h" |
13 #include "chrome/browser/captive_portal/testing_utils.h" | 13 #include "chrome/browser/captive_portal/testing_utils.h" |
14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/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 "content/public/test/test_browser_thread_bundle.h" |
23 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 | 26 |
26 namespace captive_portal { | 27 namespace captive_portal { |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 // A short amount of time that some tests wait for. | 31 // A short amount of time that some tests wait for. |
31 const int kShortTimeMs = 10; | 32 const int kShortTimeMs = 10; |
32 | 33 |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 AdvanceTime(expected_delay); | 161 AdvanceTime(expected_delay); |
161 ASSERT_EQ(base::TimeDelta(), GetTimeUntilNextRequest()); | 162 ASSERT_EQ(base::TimeDelta(), GetTimeUntilNextRequest()); |
162 | 163 |
163 CaptivePortalObserver observer(profile(), service()); | 164 CaptivePortalObserver observer(profile(), service()); |
164 service()->DetectCaptivePortal(); | 165 service()->DetectCaptivePortal(); |
165 | 166 |
166 EXPECT_EQ(CaptivePortalService::STATE_TIMER_RUNNING, service()->state()); | 167 EXPECT_EQ(CaptivePortalService::STATE_TIMER_RUNNING, service()->state()); |
167 EXPECT_FALSE(FetchingURL()); | 168 EXPECT_FALSE(FetchingURL()); |
168 ASSERT_TRUE(TimerRunning()); | 169 ASSERT_TRUE(TimerRunning()); |
169 | 170 |
170 base::MessageLoop::current()->RunUntilIdle(); | 171 base::RunLoop().RunUntilIdle(); |
171 EXPECT_EQ(CaptivePortalService::STATE_CHECKING_FOR_PORTAL, | 172 EXPECT_EQ(CaptivePortalService::STATE_CHECKING_FOR_PORTAL, |
172 service()->state()); | 173 service()->state()); |
173 ASSERT_TRUE(FetchingURL()); | 174 ASSERT_TRUE(FetchingURL()); |
174 EXPECT_FALSE(TimerRunning()); | 175 EXPECT_FALSE(TimerRunning()); |
175 | 176 |
176 CompleteURLFetch(net_error, status_code, response_headers); | 177 CompleteURLFetch(net_error, status_code, response_headers); |
177 | 178 |
178 EXPECT_FALSE(FetchingURL()); | 179 EXPECT_FALSE(FetchingURL()); |
179 EXPECT_FALSE(TimerRunning()); | 180 EXPECT_FALSE(TimerRunning()); |
180 EXPECT_EQ(1, observer.num_results_received()); | 181 EXPECT_EQ(1, observer.num_results_received()); |
(...skipping 11 matching lines...) Expand all Loading... |
192 AdvanceTime(expected_delay); | 193 AdvanceTime(expected_delay); |
193 ASSERT_EQ(base::TimeDelta(), GetTimeUntilNextRequest()); | 194 ASSERT_EQ(base::TimeDelta(), GetTimeUntilNextRequest()); |
194 | 195 |
195 CaptivePortalObserver observer(profile(), service()); | 196 CaptivePortalObserver observer(profile(), service()); |
196 service()->DetectCaptivePortal(); | 197 service()->DetectCaptivePortal(); |
197 | 198 |
198 EXPECT_EQ(CaptivePortalService::STATE_TIMER_RUNNING, service()->state()); | 199 EXPECT_EQ(CaptivePortalService::STATE_TIMER_RUNNING, service()->state()); |
199 EXPECT_FALSE(FetchingURL()); | 200 EXPECT_FALSE(FetchingURL()); |
200 ASSERT_TRUE(TimerRunning()); | 201 ASSERT_TRUE(TimerRunning()); |
201 | 202 |
202 base::MessageLoop::current()->RunUntilIdle(); | 203 base::RunLoop().RunUntilIdle(); |
203 EXPECT_FALSE(FetchingURL()); | 204 EXPECT_FALSE(FetchingURL()); |
204 EXPECT_FALSE(TimerRunning()); | 205 EXPECT_FALSE(TimerRunning()); |
205 EXPECT_EQ(1, observer.num_results_received()); | 206 EXPECT_EQ(1, observer.num_results_received()); |
206 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); | 207 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); |
207 } | 208 } |
208 | 209 |
209 // Tests exponential backoff. Prior to calling, the relevant recheck settings | 210 // 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 | 211 // must be set to have a minimum time of 100 seconds, with 2 checks before |
211 // starting exponential backoff. | 212 // starting exponential backoff. |
212 void RunBackoffTest(Result expected_result, int net_error, int status_code) { | 213 void RunBackoffTest(Result expected_result, int net_error, int status_code) { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 | 268 |
268 TestingProfile* profile() { return profile_.get(); } | 269 TestingProfile* profile() { return profile_.get(); } |
269 | 270 |
270 CaptivePortalService* service() { return service_.get(); } | 271 CaptivePortalService* service() { return service_.get(); } |
271 | 272 |
272 private: | 273 private: |
273 // Stores the initial CaptivePortalService::TestingState so it can be restored | 274 // Stores the initial CaptivePortalService::TestingState so it can be restored |
274 // after the test. | 275 // after the test. |
275 const CaptivePortalService::TestingState old_captive_portal_testing_state_; | 276 const CaptivePortalService::TestingState old_captive_portal_testing_state_; |
276 | 277 |
277 base::MessageLoop message_loop_; | 278 content::TestBrowserThreadBundle thread_bundle_; |
278 | 279 |
279 // Note that the construction order of these matters. | 280 // Note that the construction order of these matters. |
280 scoped_ptr<TestingProfile> profile_; | 281 scoped_ptr<TestingProfile> profile_; |
281 scoped_ptr<CaptivePortalService> service_; | 282 scoped_ptr<CaptivePortalService> service_; |
282 }; | 283 }; |
283 | 284 |
284 // Verify that an observer doesn't get messages from the wrong profile. | 285 // Verify that an observer doesn't get messages from the wrong profile. |
285 TEST_F(CaptivePortalServiceTest, CaptivePortalTwoProfiles) { | 286 TEST_F(CaptivePortalServiceTest, CaptivePortalTwoProfiles) { |
286 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); | 287 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); |
287 TestingProfile profile2; | 288 TestingProfile profile2; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 | 366 |
366 // Check that disabling the captive portal service while a check is running | 367 // Check that disabling the captive portal service while a check is running |
367 // works. | 368 // works. |
368 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhileRunning) { | 369 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhileRunning) { |
369 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); | 370 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); |
370 CaptivePortalObserver observer(profile(), service()); | 371 CaptivePortalObserver observer(profile(), service()); |
371 | 372 |
372 // Needed to create the URLFetcher, even if it never returns any results. | 373 // Needed to create the URLFetcher, even if it never returns any results. |
373 service()->DetectCaptivePortal(); | 374 service()->DetectCaptivePortal(); |
374 | 375 |
375 base::MessageLoop::current()->RunUntilIdle(); | 376 base::RunLoop().RunUntilIdle(); |
376 EXPECT_TRUE(FetchingURL()); | 377 EXPECT_TRUE(FetchingURL()); |
377 EXPECT_FALSE(TimerRunning()); | 378 EXPECT_FALSE(TimerRunning()); |
378 | 379 |
379 EnableCaptivePortalDetectionPreference(false); | 380 EnableCaptivePortalDetectionPreference(false); |
380 EXPECT_FALSE(FetchingURL()); | 381 EXPECT_FALSE(FetchingURL()); |
381 EXPECT_TRUE(TimerRunning()); | 382 EXPECT_TRUE(TimerRunning()); |
382 EXPECT_EQ(0, observer.num_results_received()); | 383 EXPECT_EQ(0, observer.num_results_received()); |
383 | 384 |
384 base::MessageLoop::current()->RunUntilIdle(); | 385 base::RunLoop().RunUntilIdle(); |
385 | 386 |
386 EXPECT_FALSE(FetchingURL()); | 387 EXPECT_FALSE(FetchingURL()); |
387 EXPECT_FALSE(TimerRunning()); | 388 EXPECT_FALSE(TimerRunning()); |
388 EXPECT_EQ(1, observer.num_results_received()); | 389 EXPECT_EQ(1, observer.num_results_received()); |
389 | 390 |
390 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); | 391 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); |
391 } | 392 } |
392 | 393 |
393 // Check that disabling the captive portal service while a check is pending | 394 // Check that disabling the captive portal service while a check is pending |
394 // works. | 395 // works. |
395 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhilePending) { | 396 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhilePending) { |
396 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); | 397 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); |
397 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1)); | 398 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1)); |
398 | 399 |
399 CaptivePortalObserver observer(profile(), service()); | 400 CaptivePortalObserver observer(profile(), service()); |
400 service()->DetectCaptivePortal(); | 401 service()->DetectCaptivePortal(); |
401 EXPECT_FALSE(FetchingURL()); | 402 EXPECT_FALSE(FetchingURL()); |
402 EXPECT_TRUE(TimerRunning()); | 403 EXPECT_TRUE(TimerRunning()); |
403 | 404 |
404 EnableCaptivePortalDetectionPreference(false); | 405 EnableCaptivePortalDetectionPreference(false); |
405 EXPECT_FALSE(FetchingURL()); | 406 EXPECT_FALSE(FetchingURL()); |
406 EXPECT_TRUE(TimerRunning()); | 407 EXPECT_TRUE(TimerRunning()); |
407 EXPECT_EQ(0, observer.num_results_received()); | 408 EXPECT_EQ(0, observer.num_results_received()); |
408 | 409 |
409 base::MessageLoop::current()->RunUntilIdle(); | 410 base::RunLoop().RunUntilIdle(); |
410 | 411 |
411 EXPECT_FALSE(FetchingURL()); | 412 EXPECT_FALSE(FetchingURL()); |
412 EXPECT_FALSE(TimerRunning()); | 413 EXPECT_FALSE(TimerRunning()); |
413 EXPECT_EQ(1, observer.num_results_received()); | 414 EXPECT_EQ(1, observer.num_results_received()); |
414 | 415 |
415 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); | 416 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); |
416 } | 417 } |
417 | 418 |
418 // Check that disabling the captive portal service while a check is pending | 419 // Check that disabling the captive portal service while a check is pending |
419 // works. | 420 // works. |
420 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefEnabledWhilePending) { | 421 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefEnabledWhilePending) { |
421 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); | 422 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); |
422 | 423 |
423 EnableCaptivePortalDetectionPreference(false); | 424 EnableCaptivePortalDetectionPreference(false); |
424 RunDisabledTest(0); | 425 RunDisabledTest(0); |
425 | 426 |
426 CaptivePortalObserver observer(profile(), service()); | 427 CaptivePortalObserver observer(profile(), service()); |
427 service()->DetectCaptivePortal(); | 428 service()->DetectCaptivePortal(); |
428 EXPECT_FALSE(FetchingURL()); | 429 EXPECT_FALSE(FetchingURL()); |
429 EXPECT_TRUE(TimerRunning()); | 430 EXPECT_TRUE(TimerRunning()); |
430 | 431 |
431 EnableCaptivePortalDetectionPreference(true); | 432 EnableCaptivePortalDetectionPreference(true); |
432 EXPECT_FALSE(FetchingURL()); | 433 EXPECT_FALSE(FetchingURL()); |
433 EXPECT_TRUE(TimerRunning()); | 434 EXPECT_TRUE(TimerRunning()); |
434 | 435 |
435 base::MessageLoop::current()->RunUntilIdle(); | 436 base::RunLoop().RunUntilIdle(); |
436 ASSERT_TRUE(FetchingURL()); | 437 ASSERT_TRUE(FetchingURL()); |
437 EXPECT_FALSE(TimerRunning()); | 438 EXPECT_FALSE(TimerRunning()); |
438 | 439 |
439 CompleteURLFetch(net::OK, 200, NULL); | 440 CompleteURLFetch(net::OK, 200, NULL); |
440 EXPECT_FALSE(FetchingURL()); | 441 EXPECT_FALSE(FetchingURL()); |
441 EXPECT_FALSE(TimerRunning()); | 442 EXPECT_FALSE(TimerRunning()); |
442 | 443 |
443 EXPECT_EQ(1, observer.num_results_received()); | 444 EXPECT_EQ(1, observer.num_results_received()); |
444 EXPECT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL, observer.captive_portal_result()); | 445 EXPECT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL, observer.captive_portal_result()); |
445 } | 446 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 | 511 |
511 RunTest(RESULT_NO_RESPONSE, | 512 RunTest(RESULT_NO_RESPONSE, |
512 net::OK, | 513 net::OK, |
513 503, | 514 503, |
514 0, | 515 0, |
515 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n"); | 516 "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()); | 517 EXPECT_EQ(base::TimeDelta::FromSeconds(51), GetTimeUntilNextRequest()); |
517 } | 518 } |
518 | 519 |
519 } // namespace captive_portal | 520 } // namespace captive_portal |
OLD | NEW |