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/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/test/simple_test_tick_clock.h" |
12 #include "base/test/test_timeouts.h" | 13 #include "base/test/test_timeouts.h" |
13 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
14 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
15 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
16 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
17 #include "components/captive_portal/captive_portal_testing_utils.h" | 18 #include "components/captive_portal/captive_portal_testing_utils.h" |
18 #include "content/public/browser/notification_details.h" | 19 #include "content/public/browser/notification_details.h" |
19 #include "content/public/browser/notification_observer.h" | 20 #include "content/public/browser/notification_observer.h" |
20 #include "content/public/browser/notification_registrar.h" | 21 #include "content/public/browser/notification_registrar.h" |
21 #include "content/public/browser/notification_source.h" | 22 #include "content/public/browser/notification_source.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 old_captive_portal_testing_state_); | 95 old_captive_portal_testing_state_); |
95 } | 96 } |
96 | 97 |
97 // |enable_service| is whether or not the captive portal service itself | 98 // |enable_service| is whether or not the captive portal service itself |
98 // should be disabled. This is different from enabling the captive portal | 99 // should be disabled. This is different from enabling the captive portal |
99 // detection preference. | 100 // detection preference. |
100 void Initialize(CaptivePortalService::TestingState testing_state) { | 101 void Initialize(CaptivePortalService::TestingState testing_state) { |
101 CaptivePortalService::set_state_for_testing(testing_state); | 102 CaptivePortalService::set_state_for_testing(testing_state); |
102 | 103 |
103 profile_.reset(new TestingProfile()); | 104 profile_.reset(new TestingProfile()); |
104 service_.reset(new CaptivePortalService(profile_.get())); | 105 tick_clock_.reset(new base::SimpleTestTickClock()); |
105 service_->set_time_ticks_for_testing(base::TimeTicks::Now()); | 106 tick_clock_->Advance(base::TimeTicks::Now() - tick_clock_->NowTicks()); |
| 107 service_.reset(new CaptivePortalService(profile_.get(), tick_clock_.get())); |
106 | 108 |
107 // Use no delays for most tests. | 109 // Use no delays for most tests. |
108 set_initial_backoff_no_portal(base::TimeDelta()); | 110 set_initial_backoff_no_portal(base::TimeDelta()); |
109 set_initial_backoff_portal(base::TimeDelta()); | 111 set_initial_backoff_portal(base::TimeDelta()); |
110 | 112 |
111 set_detector(&service_->captive_portal_detector_); | 113 set_detector(&service_->captive_portal_detector_); |
112 SetTime(base::Time::Now()); | 114 SetTime(base::Time::Now()); |
113 | 115 |
114 // Disable jitter, so can check exact values. | 116 // Disable jitter, so can check exact values. |
115 set_jitter_factor(0.0); | 117 set_jitter_factor(0.0); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 RunTest(expected_result, net_error, status_code, 200, NULL); | 221 RunTest(expected_result, net_error, status_code, 200, NULL); |
220 RunTest(expected_result, net_error, status_code, 400, NULL); | 222 RunTest(expected_result, net_error, status_code, 400, NULL); |
221 RunTest(expected_result, net_error, status_code, 800, NULL); | 223 RunTest(expected_result, net_error, status_code, 800, NULL); |
222 RunTest(expected_result, net_error, status_code, 1600, NULL); | 224 RunTest(expected_result, net_error, status_code, 1600, NULL); |
223 RunTest(expected_result, net_error, status_code, 1600, NULL); | 225 RunTest(expected_result, net_error, status_code, 1600, NULL); |
224 } | 226 } |
225 | 227 |
226 // Changes test time for the service and service's captive portal | 228 // Changes test time for the service and service's captive portal |
227 // detector. | 229 // detector. |
228 void AdvanceTime(const base::TimeDelta& delta) { | 230 void AdvanceTime(const base::TimeDelta& delta) { |
229 service()->advance_time_ticks_for_testing(delta); | 231 tick_clock_->Advance(delta); |
230 CaptivePortalDetectorTestBase::AdvanceTime(delta); | 232 CaptivePortalDetectorTestBase::AdvanceTime(delta); |
231 } | 233 } |
232 | 234 |
233 bool TimerRunning() { | 235 bool TimerRunning() { |
234 return service()->TimerRunning(); | 236 return service()->TimerRunning(); |
235 } | 237 } |
236 | 238 |
237 base::TimeDelta GetTimeUntilNextRequest() { | 239 base::TimeDelta GetTimeUntilNextRequest() { |
238 return service()->backoff_entry_->GetTimeUntilRelease(); | 240 return service()->backoff_entry_->GetTimeUntilRelease(); |
239 } | 241 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 | 276 |
275 private: | 277 private: |
276 // Stores the initial CaptivePortalService::TestingState so it can be restored | 278 // Stores the initial CaptivePortalService::TestingState so it can be restored |
277 // after the test. | 279 // after the test. |
278 const CaptivePortalService::TestingState old_captive_portal_testing_state_; | 280 const CaptivePortalService::TestingState old_captive_portal_testing_state_; |
279 | 281 |
280 content::TestBrowserThreadBundle thread_bundle_; | 282 content::TestBrowserThreadBundle thread_bundle_; |
281 | 283 |
282 // Note that the construction order of these matters. | 284 // Note that the construction order of these matters. |
283 scoped_ptr<TestingProfile> profile_; | 285 scoped_ptr<TestingProfile> profile_; |
| 286 scoped_ptr<base::SimpleTestTickClock> tick_clock_; |
284 scoped_ptr<CaptivePortalService> service_; | 287 scoped_ptr<CaptivePortalService> service_; |
285 }; | 288 }; |
286 | 289 |
287 // Verify that an observer doesn't get messages from the wrong profile. | 290 // Verify that an observer doesn't get messages from the wrong profile. |
288 TEST_F(CaptivePortalServiceTest, CaptivePortalTwoProfiles) { | 291 TEST_F(CaptivePortalServiceTest, CaptivePortalTwoProfiles) { |
289 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); | 292 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); |
290 TestingProfile profile2; | 293 TestingProfile profile2; |
291 scoped_ptr<CaptivePortalService> service2( | 294 scoped_ptr<CaptivePortalService> service2( |
292 new CaptivePortalService(&profile2)); | 295 new CaptivePortalService(&profile2)); |
293 CaptivePortalObserver observer2(&profile2, service2.get()); | 296 CaptivePortalObserver observer2(&profile2, service2.get()); |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT", &start_time)); | 518 base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT", &start_time)); |
516 SetTime(start_time); | 519 SetTime(start_time); |
517 | 520 |
518 RunTest(captive_portal::RESULT_NO_RESPONSE, | 521 RunTest(captive_portal::RESULT_NO_RESPONSE, |
519 net::OK, | 522 net::OK, |
520 503, | 523 503, |
521 0, | 524 0, |
522 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n"); | 525 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n"); |
523 EXPECT_EQ(base::TimeDelta::FromSeconds(51), GetTimeUntilNextRequest()); | 526 EXPECT_EQ(base::TimeDelta::FromSeconds(51), GetTimeUntilNextRequest()); |
524 } | 527 } |
OLD | NEW |