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

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

Issue 242483003: Move CaptivePortalDetector to src/components/captive_portal (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 7 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/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/test_timeouts.h" 12 #include "base/test/test_timeouts.h"
13 #include "chrome/browser/captive_portal/testing_utils.h"
14 #include "chrome/browser/chrome_notification_types.h" 13 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/common/chrome_switches.h" 14 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
17 #include "chrome/test/base/testing_profile.h" 16 #include "chrome/test/base/testing_profile.h"
18 #include "chrome/test/base/ui_test_utils.h" 17 #include "chrome/test/base/ui_test_utils.h"
18 #include "components/captive_portal/captive_portal_testing_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 "content/public/test/test_browser_thread_bundle.h"
24 #include "net/base/net_errors.h" 24 #include "net/base/net_errors.h"
25 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
26 26
27 namespace captive_portal { 27 using captive_portal::CaptivePortalDetectorTestBase;
28 using captive_portal::CaptivePortalResult;
28 29
29 namespace { 30 namespace {
30 31
31 // An observer watches the CaptivePortalDetector. It tracks the last 32 // An observer watches the CaptivePortalDetector. It tracks the last
32 // received result and the total number of received results. 33 // received result and the total number of received results.
33 class CaptivePortalObserver : public content::NotificationObserver { 34 class CaptivePortalObserver : public content::NotificationObserver {
34 public: 35 public:
35 CaptivePortalObserver(Profile* profile, 36 CaptivePortalObserver(Profile* profile,
36 CaptivePortalService* captive_portal_service) 37 CaptivePortalService* captive_portal_service)
37 : captive_portal_result_( 38 : captive_portal_result_(
38 captive_portal_service->last_detection_result()), 39 captive_portal_service->last_detection_result()),
39 num_results_received_(0), 40 num_results_received_(0),
40 profile_(profile), 41 profile_(profile),
41 captive_portal_service_(captive_portal_service) { 42 captive_portal_service_(captive_portal_service) {
42 registrar_.Add(this, 43 registrar_.Add(this,
43 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, 44 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT,
44 content::Source<Profile>(profile_)); 45 content::Source<Profile>(profile_));
45 } 46 }
46 47
47 Result captive_portal_result() const { return captive_portal_result_; } 48 CaptivePortalResult captive_portal_result() const {
49 return captive_portal_result_;
50 }
48 51
49 int num_results_received() const { return num_results_received_; } 52 int num_results_received() const { return num_results_received_; }
50 53
51 private: 54 private:
52 virtual void Observe(int type, 55 virtual void Observe(int type,
53 const content::NotificationSource& source, 56 const content::NotificationSource& source,
54 const content::NotificationDetails& details) OVERRIDE { 57 const content::NotificationDetails& details) OVERRIDE {
55 ASSERT_EQ(type, chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT); 58 ASSERT_EQ(type, chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT);
56 ASSERT_EQ(profile_, content::Source<Profile>(source).ptr()); 59 ASSERT_EQ(profile_, content::Source<Profile>(source).ptr());
57 60
58 CaptivePortalService::Results *results = 61 CaptivePortalService::Results *results =
59 content::Details<CaptivePortalService::Results>(details).ptr(); 62 content::Details<CaptivePortalService::Results>(details).ptr();
60 63
61 EXPECT_EQ(captive_portal_result_, results->previous_result); 64 EXPECT_EQ(captive_portal_result_, results->previous_result);
62 EXPECT_EQ(captive_portal_service_->last_detection_result(), 65 EXPECT_EQ(captive_portal_service_->last_detection_result(),
63 results->result); 66 results->result);
64 67
65 captive_portal_result_ = results->result; 68 captive_portal_result_ = results->result;
66 ++num_results_received_; 69 ++num_results_received_;
67 } 70 }
68 71
69 Result captive_portal_result_; 72 CaptivePortalResult captive_portal_result_;
70 int num_results_received_; 73 int num_results_received_;
71 74
72 Profile* profile_; 75 Profile* profile_;
73 CaptivePortalService* captive_portal_service_; 76 CaptivePortalService* captive_portal_service_;
74 77
75 content::NotificationRegistrar registrar_; 78 content::NotificationRegistrar registrar_;
76 79
77 DISALLOW_COPY_AND_ASSIGN(CaptivePortalObserver); 80 DISALLOW_COPY_AND_ASSIGN(CaptivePortalObserver);
78 }; 81 };
79 82
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 // returning with the specified |net_error| and |status_code|. If |net_error| 140 // returning with the specified |net_error| and |status_code|. If |net_error|
138 // is not OK, |status_code| is ignored. Expects the CaptivePortalService to 141 // is not OK, |status_code| is ignored. Expects the CaptivePortalService to
139 // return |expected_result|. 142 // return |expected_result|.
140 // 143 //
141 // |expected_delay_secs| is the expected value of GetTimeUntilNextRequest(). 144 // |expected_delay_secs| is the expected value of GetTimeUntilNextRequest().
142 // The function makes sure the value is as expected, and then simulates 145 // The function makes sure the value is as expected, and then simulates
143 // waiting for that period of time before running the test. 146 // waiting for that period of time before running the test.
144 // 147 //
145 // If |response_headers| is non-NULL, the response will use it as headers 148 // If |response_headers| is non-NULL, the response will use it as headers
146 // for the simulate URL request. It must use single linefeeds as line breaks. 149 // for the simulate URL request. It must use single linefeeds as line breaks.
147 void RunTest(Result expected_result, 150 void RunTest(CaptivePortalResult expected_result,
148 int net_error, 151 int net_error,
149 int status_code, 152 int status_code,
150 int expected_delay_secs, 153 int expected_delay_secs,
151 const char* response_headers) { 154 const char* response_headers) {
152 base::TimeDelta expected_delay = 155 base::TimeDelta expected_delay =
153 base::TimeDelta::FromSeconds(expected_delay_secs); 156 base::TimeDelta::FromSeconds(expected_delay_secs);
154 157
155 ASSERT_EQ(CaptivePortalService::STATE_IDLE, service()->state()); 158 ASSERT_EQ(CaptivePortalService::STATE_IDLE, service()->state());
156 ASSERT_EQ(expected_delay, GetTimeUntilNextRequest()); 159 ASSERT_EQ(expected_delay, GetTimeUntilNextRequest());
157 160
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 service()->DetectCaptivePortal(); 197 service()->DetectCaptivePortal();
195 198
196 EXPECT_EQ(CaptivePortalService::STATE_TIMER_RUNNING, service()->state()); 199 EXPECT_EQ(CaptivePortalService::STATE_TIMER_RUNNING, service()->state());
197 EXPECT_FALSE(FetchingURL()); 200 EXPECT_FALSE(FetchingURL());
198 ASSERT_TRUE(TimerRunning()); 201 ASSERT_TRUE(TimerRunning());
199 202
200 base::RunLoop().RunUntilIdle(); 203 base::RunLoop().RunUntilIdle();
201 EXPECT_FALSE(FetchingURL()); 204 EXPECT_FALSE(FetchingURL());
202 EXPECT_FALSE(TimerRunning()); 205 EXPECT_FALSE(TimerRunning());
203 EXPECT_EQ(1, observer.num_results_received()); 206 EXPECT_EQ(1, observer.num_results_received());
204 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); 207 EXPECT_EQ(captive_portal::RESULT_INTERNET_CONNECTED,
208 observer.captive_portal_result());
205 } 209 }
206 210
207 // Tests exponential backoff. Prior to calling, the relevant recheck settings 211 // Tests exponential backoff. Prior to calling, the relevant recheck settings
208 // must be set to have a minimum time of 100 seconds, with 2 checks before 212 // must be set to have a minimum time of 100 seconds, with 2 checks before
209 // starting exponential backoff. 213 // starting exponential backoff.
210 void RunBackoffTest(Result expected_result, int net_error, int status_code) { 214 void RunBackoffTest(CaptivePortalResult expected_result,
215 int net_error,
216 int status_code) {
211 RunTest(expected_result, net_error, status_code, 0, NULL); 217 RunTest(expected_result, net_error, status_code, 0, NULL);
212 RunTest(expected_result, net_error, status_code, 0, NULL); 218 RunTest(expected_result, net_error, status_code, 0, NULL);
213 RunTest(expected_result, net_error, status_code, 100, NULL); 219 RunTest(expected_result, net_error, status_code, 100, NULL);
214 RunTest(expected_result, net_error, status_code, 200, NULL); 220 RunTest(expected_result, net_error, status_code, 200, NULL);
215 RunTest(expected_result, net_error, status_code, 400, NULL); 221 RunTest(expected_result, net_error, status_code, 400, NULL);
216 RunTest(expected_result, net_error, status_code, 800, NULL); 222 RunTest(expected_result, net_error, status_code, 800, NULL);
217 RunTest(expected_result, net_error, status_code, 1600, NULL); 223 RunTest(expected_result, net_error, status_code, 1600, NULL);
218 RunTest(expected_result, net_error, status_code, 1600, NULL); 224 RunTest(expected_result, net_error, status_code, 1600, NULL);
219 } 225 }
220 226
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 }; 286 };
281 287
282 // Verify that an observer doesn't get messages from the wrong profile. 288 // Verify that an observer doesn't get messages from the wrong profile.
283 TEST_F(CaptivePortalServiceTest, CaptivePortalTwoProfiles) { 289 TEST_F(CaptivePortalServiceTest, CaptivePortalTwoProfiles) {
284 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 290 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
285 TestingProfile profile2; 291 TestingProfile profile2;
286 scoped_ptr<CaptivePortalService> service2( 292 scoped_ptr<CaptivePortalService> service2(
287 new CaptivePortalService(&profile2)); 293 new CaptivePortalService(&profile2));
288 CaptivePortalObserver observer2(&profile2, service2.get()); 294 CaptivePortalObserver observer2(&profile2, service2.get());
289 295
290 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL); 296 RunTest(captive_portal::RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL);
291 EXPECT_EQ(0, observer2.num_results_received()); 297 EXPECT_EQ(0, observer2.num_results_received());
292 } 298 }
293 299
294 // Checks exponential backoff when the Internet is connected. 300 // Checks exponential backoff when the Internet is connected.
295 TEST_F(CaptivePortalServiceTest, CaptivePortalRecheckInternetConnected) { 301 TEST_F(CaptivePortalServiceTest, CaptivePortalRecheckInternetConnected) {
296 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 302 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
297 303
298 // This value should have no effect on this test, until the end. 304 // This value should have no effect on this test, until the end.
299 set_initial_backoff_portal(base::TimeDelta::FromSeconds(1)); 305 set_initial_backoff_portal(base::TimeDelta::FromSeconds(1));
300 306
301 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); 307 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100));
302 RunBackoffTest(RESULT_INTERNET_CONNECTED, net::OK, 204); 308 RunBackoffTest(captive_portal::RESULT_INTERNET_CONNECTED, net::OK, 204);
303 309
304 // Make sure that getting a new result resets the timer. 310 // Make sure that getting a new result resets the timer.
305 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 1600, NULL); 311 RunTest(
306 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 0, NULL); 312 captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 1600, NULL);
307 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 1, NULL); 313 RunTest(captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 0, NULL);
308 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 2, NULL); 314 RunTest(captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 1, NULL);
315 RunTest(captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 2, NULL);
309 } 316 }
310 317
311 // Checks exponential backoff when there's an HTTP error. 318 // Checks exponential backoff when there's an HTTP error.
312 TEST_F(CaptivePortalServiceTest, CaptivePortalRecheckError) { 319 TEST_F(CaptivePortalServiceTest, CaptivePortalRecheckError) {
313 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 320 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
314 321
315 // This value should have no effect on this test. 322 // This value should have no effect on this test.
316 set_initial_backoff_portal(base::TimeDelta::FromDays(1)); 323 set_initial_backoff_portal(base::TimeDelta::FromDays(1));
317 324
318 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); 325 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100));
319 RunBackoffTest(RESULT_NO_RESPONSE, net::OK, 500); 326 RunBackoffTest(captive_portal::RESULT_NO_RESPONSE, net::OK, 500);
320 327
321 // Make sure that getting a new result resets the timer. 328 // Make sure that getting a new result resets the timer.
322 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 1600, NULL); 329 RunTest(captive_portal::RESULT_INTERNET_CONNECTED, net::OK, 204, 1600, NULL);
323 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL); 330 RunTest(captive_portal::RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL);
324 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 100, NULL); 331 RunTest(captive_portal::RESULT_INTERNET_CONNECTED, net::OK, 204, 100, NULL);
325 } 332 }
326 333
327 // Checks exponential backoff when there's a captive portal. 334 // Checks exponential backoff when there's a captive portal.
328 TEST_F(CaptivePortalServiceTest, CaptivePortalRecheckBehindPortal) { 335 TEST_F(CaptivePortalServiceTest, CaptivePortalRecheckBehindPortal) {
329 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 336 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
330 337
331 // This value should have no effect on this test, until the end. 338 // This value should have no effect on this test, until the end.
332 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(250)); 339 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(250));
333 340
334 set_initial_backoff_portal(base::TimeDelta::FromSeconds(100)); 341 set_initial_backoff_portal(base::TimeDelta::FromSeconds(100));
335 RunBackoffTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200); 342 RunBackoffTest(captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200);
336 343
337 // Make sure that getting a new result resets the timer. 344 // Make sure that getting a new result resets the timer.
338 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 1600, NULL); 345 RunTest(captive_portal::RESULT_INTERNET_CONNECTED, net::OK, 204, 1600, NULL);
339 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL); 346 RunTest(captive_portal::RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL);
340 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 250, NULL); 347 RunTest(captive_portal::RESULT_INTERNET_CONNECTED, net::OK, 204, 250, NULL);
341 } 348 }
342 349
343 // Check that everything works as expected when captive portal checking is 350 // Check that everything works as expected when captive portal checking is
344 // disabled, including throttling. Then enables it again and runs another test. 351 // disabled, including throttling. Then enables it again and runs another test.
345 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabled) { 352 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabled) {
346 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 353 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
347 354
348 // This value should have no effect on this test. 355 // This value should have no effect on this test.
349 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1)); 356 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1));
350 357
351 set_initial_backoff_portal(base::TimeDelta::FromSeconds(100)); 358 set_initial_backoff_portal(base::TimeDelta::FromSeconds(100));
352 359
353 EnableCaptivePortalDetectionPreference(false); 360 EnableCaptivePortalDetectionPreference(false);
354 361
355 RunDisabledTest(0); 362 RunDisabledTest(0);
356 for (int i = 0; i < 6; ++i) 363 for (int i = 0; i < 6; ++i)
357 RunDisabledTest(100); 364 RunDisabledTest(100);
358 365
359 EnableCaptivePortalDetectionPreference(true); 366 EnableCaptivePortalDetectionPreference(true);
360 367
361 RunTest(RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 0, NULL); 368 RunTest(captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL, net::OK, 200, 0, NULL);
362 } 369 }
363 370
364 // Check that disabling the captive portal service while a check is running 371 // Check that disabling the captive portal service while a check is running
365 // works. 372 // works.
366 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhileRunning) { 373 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhileRunning) {
367 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 374 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
368 CaptivePortalObserver observer(profile(), service()); 375 CaptivePortalObserver observer(profile(), service());
369 376
370 // Needed to create the URLFetcher, even if it never returns any results. 377 // Needed to create the URLFetcher, even if it never returns any results.
371 service()->DetectCaptivePortal(); 378 service()->DetectCaptivePortal();
372 379
373 base::RunLoop().RunUntilIdle(); 380 base::RunLoop().RunUntilIdle();
374 EXPECT_TRUE(FetchingURL()); 381 EXPECT_TRUE(FetchingURL());
375 EXPECT_FALSE(TimerRunning()); 382 EXPECT_FALSE(TimerRunning());
376 383
377 EnableCaptivePortalDetectionPreference(false); 384 EnableCaptivePortalDetectionPreference(false);
378 EXPECT_FALSE(FetchingURL()); 385 EXPECT_FALSE(FetchingURL());
379 EXPECT_TRUE(TimerRunning()); 386 EXPECT_TRUE(TimerRunning());
380 EXPECT_EQ(0, observer.num_results_received()); 387 EXPECT_EQ(0, observer.num_results_received());
381 388
382 base::RunLoop().RunUntilIdle(); 389 base::RunLoop().RunUntilIdle();
383 390
384 EXPECT_FALSE(FetchingURL()); 391 EXPECT_FALSE(FetchingURL());
385 EXPECT_FALSE(TimerRunning()); 392 EXPECT_FALSE(TimerRunning());
386 EXPECT_EQ(1, observer.num_results_received()); 393 EXPECT_EQ(1, observer.num_results_received());
387 394
388 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); 395 EXPECT_EQ(captive_portal::RESULT_INTERNET_CONNECTED,
396 observer.captive_portal_result());
389 } 397 }
390 398
391 // Check that disabling the captive portal service while a check is pending 399 // Check that disabling the captive portal service while a check is pending
392 // works. 400 // works.
393 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhilePending) { 401 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefDisabledWhilePending) {
394 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 402 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
395 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1)); 403 set_initial_backoff_no_portal(base::TimeDelta::FromDays(1));
396 404
397 CaptivePortalObserver observer(profile(), service()); 405 CaptivePortalObserver observer(profile(), service());
398 service()->DetectCaptivePortal(); 406 service()->DetectCaptivePortal();
399 EXPECT_FALSE(FetchingURL()); 407 EXPECT_FALSE(FetchingURL());
400 EXPECT_TRUE(TimerRunning()); 408 EXPECT_TRUE(TimerRunning());
401 409
402 EnableCaptivePortalDetectionPreference(false); 410 EnableCaptivePortalDetectionPreference(false);
403 EXPECT_FALSE(FetchingURL()); 411 EXPECT_FALSE(FetchingURL());
404 EXPECT_TRUE(TimerRunning()); 412 EXPECT_TRUE(TimerRunning());
405 EXPECT_EQ(0, observer.num_results_received()); 413 EXPECT_EQ(0, observer.num_results_received());
406 414
407 base::RunLoop().RunUntilIdle(); 415 base::RunLoop().RunUntilIdle();
408 416
409 EXPECT_FALSE(FetchingURL()); 417 EXPECT_FALSE(FetchingURL());
410 EXPECT_FALSE(TimerRunning()); 418 EXPECT_FALSE(TimerRunning());
411 EXPECT_EQ(1, observer.num_results_received()); 419 EXPECT_EQ(1, observer.num_results_received());
412 420
413 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); 421 EXPECT_EQ(captive_portal::RESULT_INTERNET_CONNECTED,
422 observer.captive_portal_result());
414 } 423 }
415 424
416 // Check that disabling the captive portal service while a check is pending 425 // Check that disabling the captive portal service while a check is pending
417 // works. 426 // works.
418 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefEnabledWhilePending) { 427 TEST_F(CaptivePortalServiceTest, CaptivePortalPrefEnabledWhilePending) {
419 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 428 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
420 429
421 EnableCaptivePortalDetectionPreference(false); 430 EnableCaptivePortalDetectionPreference(false);
422 RunDisabledTest(0); 431 RunDisabledTest(0);
423 432
424 CaptivePortalObserver observer(profile(), service()); 433 CaptivePortalObserver observer(profile(), service());
425 service()->DetectCaptivePortal(); 434 service()->DetectCaptivePortal();
426 EXPECT_FALSE(FetchingURL()); 435 EXPECT_FALSE(FetchingURL());
427 EXPECT_TRUE(TimerRunning()); 436 EXPECT_TRUE(TimerRunning());
428 437
429 EnableCaptivePortalDetectionPreference(true); 438 EnableCaptivePortalDetectionPreference(true);
430 EXPECT_FALSE(FetchingURL()); 439 EXPECT_FALSE(FetchingURL());
431 EXPECT_TRUE(TimerRunning()); 440 EXPECT_TRUE(TimerRunning());
432 441
433 base::RunLoop().RunUntilIdle(); 442 base::RunLoop().RunUntilIdle();
434 ASSERT_TRUE(FetchingURL()); 443 ASSERT_TRUE(FetchingURL());
435 EXPECT_FALSE(TimerRunning()); 444 EXPECT_FALSE(TimerRunning());
436 445
437 CompleteURLFetch(net::OK, 200, NULL); 446 CompleteURLFetch(net::OK, 200, NULL);
438 EXPECT_FALSE(FetchingURL()); 447 EXPECT_FALSE(FetchingURL());
439 EXPECT_FALSE(TimerRunning()); 448 EXPECT_FALSE(TimerRunning());
440 449
441 EXPECT_EQ(1, observer.num_results_received()); 450 EXPECT_EQ(1, observer.num_results_received());
442 EXPECT_EQ(RESULT_BEHIND_CAPTIVE_PORTAL, observer.captive_portal_result()); 451 EXPECT_EQ(captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL,
452 observer.captive_portal_result());
443 } 453 }
444 454
445 // Checks that disabling for browser tests works as expected. 455 // Checks that disabling for browser tests works as expected.
446 TEST_F(CaptivePortalServiceTest, CaptivePortalDisableForTests) { 456 TEST_F(CaptivePortalServiceTest, CaptivePortalDisableForTests) {
447 Initialize(CaptivePortalService::DISABLED_FOR_TESTING); 457 Initialize(CaptivePortalService::DISABLED_FOR_TESTING);
448 RunDisabledTest(0); 458 RunDisabledTest(0);
449 } 459 }
450 460
451 // Checks that jitter gives us values in the correct range. 461 // Checks that jitter gives us values in the correct range.
452 TEST_F(CaptivePortalServiceTest, CaptivePortalJitter) { 462 TEST_F(CaptivePortalServiceTest, CaptivePortalJitter) {
453 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 463 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
454 set_jitter_factor(0.3); 464 set_jitter_factor(0.3);
455 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); 465 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100));
456 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL); 466 RunTest(captive_portal::RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL);
457 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL); 467 RunTest(captive_portal::RESULT_INTERNET_CONNECTED, net::OK, 204, 0, NULL);
458 468
459 for (int i = 0; i < 50; ++i) { 469 for (int i = 0; i < 50; ++i) {
460 int interval_sec = GetTimeUntilNextRequest().InSeconds(); 470 int interval_sec = GetTimeUntilNextRequest().InSeconds();
461 // Allow for roundoff, though shouldn't be necessary. 471 // Allow for roundoff, though shouldn't be necessary.
462 EXPECT_LE(69, interval_sec); 472 EXPECT_LE(69, interval_sec);
463 EXPECT_LE(interval_sec, 101); 473 EXPECT_LE(interval_sec, 101);
464 } 474 }
465 } 475 }
466 476
467 // Check a Retry-After header that contains a delay in seconds. 477 // Check a Retry-After header that contains a delay in seconds.
468 TEST_F(CaptivePortalServiceTest, CaptivePortalRetryAfterSeconds) { 478 TEST_F(CaptivePortalServiceTest, CaptivePortalRetryAfterSeconds) {
469 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 479 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
470 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); 480 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100));
471 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 101\n\n"; 481 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 101\n\n";
472 482
473 // Check that Retry-After headers work both on the first request to return a 483 // Check that Retry-After headers work both on the first request to return a
474 // result and on subsequent requests. 484 // result and on subsequent requests.
475 RunTest(RESULT_NO_RESPONSE, net::OK, 503, 0, retry_after); 485 RunTest(captive_portal::RESULT_NO_RESPONSE, net::OK, 503, 0, retry_after);
476 RunTest(RESULT_NO_RESPONSE, net::OK, 503, 101, retry_after); 486 RunTest(captive_portal::RESULT_NO_RESPONSE, net::OK, 503, 101, retry_after);
477 RunTest(RESULT_INTERNET_CONNECTED, net::OK, 204, 101, NULL); 487 RunTest(captive_portal::RESULT_INTERNET_CONNECTED, net::OK, 204, 101, NULL);
478 488
479 // Make sure that there's no effect on the next captive portal check after 489 // Make sure that there's no effect on the next captive portal check after
480 // login. 490 // login.
481 EXPECT_EQ(base::TimeDelta::FromSeconds(0), GetTimeUntilNextRequest()); 491 EXPECT_EQ(base::TimeDelta::FromSeconds(0), GetTimeUntilNextRequest());
482 } 492 }
483 493
484 // Check that the RecheckPolicy is still respected on 503 responses with 494 // Check that the RecheckPolicy is still respected on 503 responses with
485 // Retry-After headers. 495 // Retry-After headers.
486 TEST_F(CaptivePortalServiceTest, CaptivePortalRetryAfterSecondsTooShort) { 496 TEST_F(CaptivePortalServiceTest, CaptivePortalRetryAfterSecondsTooShort) {
487 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 497 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
488 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100)); 498 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(100));
489 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 99\n\n"; 499 const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 99\n\n";
490 500
491 RunTest(RESULT_NO_RESPONSE, net::OK, 503, 0, retry_after); 501 RunTest(captive_portal::RESULT_NO_RESPONSE, net::OK, 503, 0, retry_after);
492 // Normally would be no delay on the first check with a new result. 502 // Normally would be no delay on the first check with a new result.
493 RunTest(RESULT_NO_RESPONSE, net::OK, 503, 99, retry_after); 503 RunTest(captive_portal::RESULT_NO_RESPONSE, net::OK, 503, 99, retry_after);
494 EXPECT_EQ(base::TimeDelta::FromSeconds(100), GetTimeUntilNextRequest()); 504 EXPECT_EQ(base::TimeDelta::FromSeconds(100), GetTimeUntilNextRequest());
495 } 505 }
496 506
497 // Check a Retry-After header that contains a date. 507 // Check a Retry-After header that contains a date.
498 TEST_F(CaptivePortalServiceTest, CaptivePortalRetryAfterDate) { 508 TEST_F(CaptivePortalServiceTest, CaptivePortalRetryAfterDate) {
499 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING); 509 Initialize(CaptivePortalService::SKIP_OS_CHECK_FOR_TESTING);
500 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(50)); 510 set_initial_backoff_no_portal(base::TimeDelta::FromSeconds(50));
501 511
502 // base has a function to get a time in the right format from a string, but 512 // base has a function to get a time in the right format from a string, but
503 // not the other way around. 513 // not the other way around.
504 base::Time start_time; 514 base::Time start_time;
505 ASSERT_TRUE( 515 ASSERT_TRUE(
506 base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT", &start_time)); 516 base::Time::FromString("Tue, 17 Apr 2012 18:02:00 GMT", &start_time));
507 SetTime(start_time); 517 SetTime(start_time);
508 518
509 RunTest(RESULT_NO_RESPONSE, 519 RunTest(captive_portal::RESULT_NO_RESPONSE,
510 net::OK, 520 net::OK,
511 503, 521 503,
512 0, 522 0,
513 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n"); 523 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n");
514 EXPECT_EQ(base::TimeDelta::FromSeconds(51), GetTimeUntilNextRequest()); 524 EXPECT_EQ(base::TimeDelta::FromSeconds(51), GetTimeUntilNextRequest());
515 } 525 }
516
517 } // namespace captive_portal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698