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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 Profile* profile() { return profile_.get(); } | 73 Profile* profile() { return profile_.get(); } |
74 | 74 |
75 NetworkPortalDetector* network_portal_detector() { | 75 NetworkPortalDetector* network_portal_detector() { |
76 return network_portal_detector_.get(); | 76 return network_portal_detector_.get(); |
77 } | 77 } |
78 | 78 |
79 NetworkPortalDetector::State state() { | 79 NetworkPortalDetector::State state() { |
80 return network_portal_detector()->state(); | 80 return network_portal_detector()->state(); |
81 } | 81 } |
82 | 82 |
| 83 void enable_lazy_detection() { |
| 84 network_portal_detector()->EnableLazyDetection(); |
| 85 } |
| 86 |
| 87 void disable_lazy_detection() { |
| 88 network_portal_detector()->DisableLazyDetection(); |
| 89 } |
| 90 |
83 bool is_state_idle() { | 91 bool is_state_idle() { |
84 return (NetworkPortalDetector::STATE_IDLE == state()); | 92 return (NetworkPortalDetector::STATE_IDLE == state()); |
85 } | 93 } |
86 | 94 |
87 bool is_state_portal_detection_pending() { | 95 bool is_state_portal_detection_pending() { |
88 return (NetworkPortalDetector::STATE_PORTAL_CHECK_PENDING == state()); | 96 return (NetworkPortalDetector::STATE_PORTAL_CHECK_PENDING == state()); |
89 } | 97 } |
90 | 98 |
91 bool is_state_checking_for_portal() { | 99 bool is_state_checking_for_portal() { |
92 return (NetworkPortalDetector::STATE_CHECKING_FOR_PORTAL == state()); | 100 return (NetworkPortalDetector::STATE_CHECKING_FOR_PORTAL == state()); |
93 } | 101 } |
94 | 102 |
95 void set_request_timeout(const base::TimeDelta& timeout) { | 103 void set_request_timeout(const base::TimeDelta& timeout) { |
96 network_portal_detector()->set_request_timeout_for_testing(timeout); | 104 network_portal_detector()->set_request_timeout_for_testing(timeout); |
97 } | 105 } |
98 | 106 |
99 const base::TimeDelta& next_attempt_delay() { | 107 const base::TimeDelta& next_attempt_delay() { |
100 return network_portal_detector()->next_attempt_delay_for_testing(); | 108 return network_portal_detector()->next_attempt_delay_for_testing(); |
101 } | 109 } |
102 | 110 |
103 int attempt_count() { | 111 int attempt_count() { |
104 return network_portal_detector()->attempt_count_for_testing(); | 112 return network_portal_detector()->attempt_count_for_testing(); |
105 } | 113 } |
106 | 114 |
107 void set_min_time_between_attempts(const base::TimeDelta& delta) { | 115 void set_min_time_between_attempts(const base::TimeDelta& delta) { |
108 network_portal_detector()->set_min_time_between_attempts_for_testing(delta); | 116 network_portal_detector()->set_min_time_between_attempts_for_testing(delta); |
109 } | 117 } |
110 | 118 |
| 119 void set_lazy_check_interval(const base::TimeDelta& delta) { |
| 120 network_portal_detector()->set_lazy_check_interval_for_testing(delta); |
| 121 } |
| 122 |
111 void set_time_ticks(const base::TimeTicks& time_ticks) { | 123 void set_time_ticks(const base::TimeTicks& time_ticks) { |
112 network_portal_detector()->set_time_ticks_for_testing(time_ticks); | 124 network_portal_detector()->set_time_ticks_for_testing(time_ticks); |
113 } | 125 } |
114 | 126 |
115 void SetBehindPortal(Network* network) { | 127 void SetBehindPortal(Network* network) { |
116 Network::TestApi test_api(network); | 128 Network::TestApi test_api(network); |
117 test_api.SetBehindPortal(); | 129 test_api.SetBehindPortal(); |
118 static_cast<NetworkLibraryImplBase*>( | 130 static_cast<NetworkLibraryImplBase*>( |
119 network_library())->CallConnectToNetwork(network); | 131 network_library())->CallConnectToNetwork(network); |
120 MessageLoop::current()->RunUntilIdle(); | 132 MessageLoop::current()->RunUntilIdle(); |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 net::URLFetcher::RESPONSE_CODE_INVALID, | 463 net::URLFetcher::RESPONSE_CODE_INVALID, |
452 NULL); | 464 NULL); |
453 ASSERT_EQ(3, attempt_count()); | 465 ASSERT_EQ(3, attempt_count()); |
454 ASSERT_TRUE(is_state_idle()); | 466 ASSERT_TRUE(is_state_idle()); |
455 | 467 |
456 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, | 468 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, |
457 net::URLFetcher::RESPONSE_CODE_INVALID, | 469 net::URLFetcher::RESPONSE_CODE_INVALID, |
458 wifi1_network()); | 470 wifi1_network()); |
459 } | 471 } |
460 | 472 |
| 473 TEST_F(NetworkPortalDetectorTest, LazyDetectionForOnlineNetwork) { |
| 474 ASSERT_TRUE(is_state_idle()); |
| 475 set_min_time_between_attempts(base::TimeDelta()); |
| 476 set_lazy_check_interval(base::TimeDelta()); |
| 477 |
| 478 SetConnected(wifi1_network()); |
| 479 enable_lazy_detection(); |
| 480 CompleteURLFetch(net::OK, 204, NULL); |
| 481 |
| 482 ASSERT_EQ(3, attempt_count()); |
| 483 ASSERT_TRUE(is_state_portal_detection_pending()); |
| 484 CheckPortalState( |
| 485 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, |
| 486 wifi1_network()); |
| 487 |
| 488 // To run CaptivePortalDetector::DetectCaptivePortal(). |
| 489 MessageLoop::current()->RunUntilIdle(); |
| 490 |
| 491 CompleteURLFetch(net::OK, 204, NULL); |
| 492 |
| 493 ASSERT_EQ(3, attempt_count()); |
| 494 ASSERT_TRUE(is_state_portal_detection_pending()); |
| 495 CheckPortalState( |
| 496 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE, 204, |
| 497 wifi1_network()); |
| 498 |
| 499 // To run CaptivePortalDetector::DetectCaptivePortal(). |
| 500 MessageLoop::current()->RunUntilIdle(); |
| 501 |
| 502 disable_lazy_detection(); |
| 503 ASSERT_TRUE(is_state_idle()); |
| 504 } |
| 505 |
| 506 TEST_F(NetworkPortalDetectorTest, LazyDetectionForPortalNetwork) { |
| 507 ASSERT_TRUE(is_state_idle()); |
| 508 set_min_time_between_attempts(base::TimeDelta()); |
| 509 set_lazy_check_interval(base::TimeDelta()); |
| 510 |
| 511 SetConnected(wifi1_network()); |
| 512 enable_lazy_detection(); |
| 513 |
| 514 CompleteURLFetch(net::ERR_CONNECTION_CLOSED, |
| 515 net::URLFetcher::RESPONSE_CODE_INVALID, |
| 516 NULL); |
| 517 ASSERT_EQ(1, attempt_count()); |
| 518 ASSERT_TRUE(is_state_portal_detection_pending()); |
| 519 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, |
| 520 wifi1_network()); |
| 521 |
| 522 // To run CaptivePortalDetector::DetectCaptivePortal(). |
| 523 MessageLoop::current()->RunUntilIdle(); |
| 524 |
| 525 CompleteURLFetch(net::ERR_CONNECTION_CLOSED, |
| 526 net::URLFetcher::RESPONSE_CODE_INVALID, |
| 527 NULL); |
| 528 ASSERT_EQ(2, attempt_count()); |
| 529 ASSERT_TRUE(is_state_portal_detection_pending()); |
| 530 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN, -1, |
| 531 wifi1_network()); |
| 532 |
| 533 // To run CaptivePortalDetector::DetectCaptivePortal(). |
| 534 MessageLoop::current()->RunUntilIdle(); |
| 535 |
| 536 CompleteURLFetch(net::OK, 200, NULL); |
| 537 ASSERT_EQ(3, attempt_count()); |
| 538 ASSERT_TRUE(is_state_portal_detection_pending()); |
| 539 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, |
| 540 wifi1_network()); |
| 541 |
| 542 // To run CaptivePortalDetector::DetectCaptivePortal(). |
| 543 MessageLoop::current()->RunUntilIdle(); |
| 544 CompleteURLFetch(net::OK, 200, NULL); |
| 545 ASSERT_EQ(3, attempt_count()); |
| 546 ASSERT_TRUE(is_state_portal_detection_pending()); |
| 547 CheckPortalState(NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL, 200, |
| 548 wifi1_network()); |
| 549 |
| 550 disable_lazy_detection(); |
| 551 ASSERT_TRUE(is_state_idle()); |
| 552 } |
| 553 |
461 } // namespace chromeos | 554 } // namespace chromeos |
OLD | NEW |