Index: chrome/browser/chromeos/net/network_portal_detector_unittest.cc |
diff --git a/chrome/browser/chromeos/net/network_portal_detector_unittest.cc b/chrome/browser/chromeos/net/network_portal_detector_unittest.cc |
index fd77d4e7b09912539bad6152bcfc6937e1a7348f..d235f632a5c0b1371a8be3bdbec330181608eeeb 100644 |
--- a/chrome/browser/chromeos/net/network_portal_detector_unittest.cc |
+++ b/chrome/browser/chromeos/net/network_portal_detector_unittest.cc |
@@ -16,7 +16,6 @@ |
#include "chrome/browser/chromeos/net/network_portal_detector.h" |
#include "chrome/test/base/testing_profile.h" |
#include "net/base/net_errors.h" |
-#include "net/url_request/test_url_fetcher_factory.h" |
namespace chromeos { |
@@ -35,8 +34,11 @@ class NetworkPortalDetectorTest |
ethernet_network_ = network_library_->FindNetworkByPath("eth1"); |
DCHECK(ethernet_network_); |
- wifi_network_ = network_library_->FindNetworkByPath("wifi1"); |
- DCHECK(wifi_network_); |
+ wifi1_network_ = network_library_->FindNetworkByPath("wifi1"); |
+ DCHECK(wifi1_network_); |
+ |
+ wifi2_network_ = network_library_->FindNetworkByPath("wifi2"); |
+ DCHECK(wifi2_network_); |
profile_.reset(new TestingProfile()); |
network_portal_detector_.reset( |
@@ -44,6 +46,9 @@ class NetworkPortalDetectorTest |
network_portal_detector_->Init(); |
set_detector(network_portal_detector_->captive_portal_detector_.get()); |
+ |
+ // Prevents flakiness due to message loop delays. |
+ set_time_ticks(base::TimeTicks::Now()); |
} |
virtual void TearDown() { |
@@ -53,7 +58,8 @@ class NetworkPortalDetectorTest |
NetworkLibrary* network_library() { return network_library_; } |
Network* ethernet_network() { return ethernet_network_; } |
- Network* wifi_network() { return wifi_network_; } |
+ Network* wifi1_network() { return wifi1_network_; } |
+ Network* wifi2_network() { return wifi2_network_; } |
Profile* profile() { return profile_.get(); } |
@@ -61,8 +67,6 @@ class NetworkPortalDetectorTest |
return network_portal_detector_.get(); |
} |
- net::TestURLFetcher* fetcher() { return factory_.GetFetcherByID(0); } |
- |
NetworkPortalDetector::State state() { |
return network_portal_detector()->state(); |
} |
@@ -71,13 +75,32 @@ class NetworkPortalDetectorTest |
return (NetworkPortalDetector::STATE_IDLE == state()); |
} |
+ bool is_state_portal_detection_pending() { |
+ return (NetworkPortalDetector::STATE_PORTAL_CHECK_PENDING == state()); |
+ } |
+ |
bool is_state_checking_for_portal() { |
return (NetworkPortalDetector::STATE_CHECKING_FOR_PORTAL == state()); |
} |
- bool is_state_checking_for_portal_network_changed() { |
- return (NetworkPortalDetector::STATE_CHECKING_FOR_PORTAL_NETWORK_CHANGED == |
- state()); |
+ void set_request_timeout(const base::TimeDelta& timeout) { |
+ network_portal_detector()->set_request_timeout_for_testing(timeout); |
+ } |
+ |
+ const base::TimeDelta& next_attempt_delay() { |
+ return network_portal_detector()->next_attempt_delay_for_testing(); |
+ } |
+ |
+ int attempt_count() { |
+ return network_portal_detector()->attempt_count_for_testing(); |
+ } |
+ |
+ void set_min_time_between_attempts(const base::TimeDelta& delta) { |
+ network_portal_detector()->set_min_time_between_attempts_for_testing(delta); |
+ } |
+ |
+ void set_time_ticks(const base::TimeTicks& time_ticks) { |
+ network_portal_detector()->set_time_ticks_for_testing(time_ticks); |
} |
void SetConnected(Network* network) { |
@@ -91,69 +114,67 @@ class NetworkPortalDetectorTest |
private: |
NetworkLibrary* network_library_; |
- // Pointer to fake ethernet network. |
+ // Pointer to a fake ethernet network. |
Network* ethernet_network_; |
- // Pointer to fake wifi network. |
- Network* wifi_network_; |
+ // Pointer to a fake wifi1 network. |
+ Network* wifi1_network_; |
+ |
+ // Pointer to a fake wifi2 network. |
+ Network* wifi2_network_; |
MessageLoop message_loop_; |
scoped_ptr<TestingProfile> profile_; |
scoped_ptr<NetworkPortalDetector> network_portal_detector_; |
- |
- net::TestURLFetcherFactory factory_; |
}; |
TEST_F(NetworkPortalDetectorTest, NoPortal) { |
ASSERT_TRUE(is_state_idle()); |
- SetConnected(wifi_network()); |
+ SetConnected(wifi1_network()); |
ASSERT_TRUE(is_state_checking_for_portal()); |
ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATE_UNKNOWN, |
- network_portal_detector()->GetCaptivePortalState(wifi_network())); |
+ network_portal_detector()->GetCaptivePortalState(wifi1_network())); |
- fetcher()->set_response_code(204); |
- OnURLFetchComplete(fetcher()); |
+ CompleteURLFetch(net::OK, 204, NULL); |
ASSERT_TRUE(is_state_idle()); |
ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATE_ONLINE, |
network_portal_detector()->GetCaptivePortalState( |
- wifi_network())); |
+ wifi1_network())); |
} |
TEST_F(NetworkPortalDetectorTest, Portal) { |
ASSERT_TRUE(is_state_idle()); |
// Check HTTP 200 response code. |
- SetConnected(wifi_network()); |
+ SetConnected(wifi1_network()); |
ASSERT_TRUE(is_state_checking_for_portal()); |
- fetcher()->set_response_code(200); |
- OnURLFetchComplete(fetcher()); |
+ CompleteURLFetch(net::OK, 200, NULL); |
ASSERT_TRUE(is_state_idle()); |
ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATE_PORTAL, |
- network_portal_detector()->GetCaptivePortalState(wifi_network())); |
+ network_portal_detector()->GetCaptivePortalState( |
+ wifi1_network())); |
// Check HTTP 301 response code. |
- SetConnected(ethernet_network()); |
+ SetConnected(wifi2_network()); |
ASSERT_TRUE(is_state_checking_for_portal()); |
- fetcher()->set_response_code(301); |
- OnURLFetchComplete(fetcher()); |
+ CompleteURLFetch(net::OK, 301, NULL); |
ASSERT_TRUE(is_state_idle()); |
ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATE_PORTAL, |
network_portal_detector()->GetCaptivePortalState( |
- ethernet_network())); |
+ wifi2_network())); |
// Check HTTP 302 response code. |
- SetConnected(wifi_network()); |
+ SetConnected(ethernet_network()); |
ASSERT_TRUE(is_state_checking_for_portal()); |
- fetcher()->set_response_code(302); |
- OnURLFetchComplete(fetcher()); |
+ CompleteURLFetch(net::OK, 302, NULL); |
ASSERT_TRUE(is_state_idle()); |
ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATE_PORTAL, |
@@ -164,75 +185,69 @@ TEST_F(NetworkPortalDetectorTest, Portal) { |
TEST_F(NetworkPortalDetectorTest, TwoNetworks) { |
ASSERT_TRUE(is_state_idle()); |
- SetConnected(wifi_network()); |
+ SetConnected(wifi1_network()); |
ASSERT_TRUE(is_state_checking_for_portal()); |
// wifi is in portal state. |
- fetcher()->set_response_code(200); |
- OnURLFetchComplete(fetcher()); |
+ CompleteURLFetch(net::OK, 200, NULL); |
ASSERT_TRUE(is_state_idle()); |
SetConnected(ethernet_network()); |
ASSERT_TRUE(is_state_checking_for_portal()); |
// ethernet is in online state. |
- fetcher()->set_response_code(204); |
- OnURLFetchComplete(fetcher()); |
+ CompleteURLFetch(net::OK, 204, NULL); |
ASSERT_TRUE(is_state_idle()); |
ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATE_ONLINE, |
network_portal_detector()->GetCaptivePortalState( |
ethernet_network())); |
ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATE_PORTAL, |
- network_portal_detector()->GetCaptivePortalState(wifi_network())); |
+ network_portal_detector()->GetCaptivePortalState(wifi1_network())); |
} |
TEST_F(NetworkPortalDetectorTest, NetworkChanged) { |
ASSERT_TRUE(is_state_idle()); |
- SetConnected(wifi_network()); |
+ SetConnected(wifi1_network()); |
+ |
+ // WiFi is in portal state. |
+ fetcher()->set_response_code(200); |
ASSERT_TRUE(is_state_checking_for_portal()); |
// Active network is changed during portal detection for wifi. |
SetConnected(ethernet_network()); |
- ASSERT_TRUE(is_state_checking_for_portal_network_changed()); |
- // WiFi is in portal state and URL fetch is completed, but |
- // NetworkPortalDetector should ignore results and retry portal |
- // detection. |
- fetcher()->set_response_code(200); |
- OnURLFetchComplete(fetcher()); |
+ // Portal detection for wifi is cancelled, portal detection for |
+ // ethernet is initiated. |
ASSERT_TRUE(is_state_checking_for_portal()); |
// ethernet is in online state. |
- fetcher()->set_response_code(204); |
- OnURLFetchComplete(fetcher()); |
+ CompleteURLFetch(net::OK, 204, NULL); |
ASSERT_TRUE(is_state_idle()); |
ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATE_ONLINE, |
network_portal_detector()->GetCaptivePortalState( |
ethernet_network())); |
- // As active network was changed during portal detection, we can't |
- // rely on URL fetcher response. So, state for wifi network must be |
- // unknown. |
+ // As active network was changed during portal detection for wifi |
+ // network, it's state must be unknown. |
ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATE_UNKNOWN, |
- network_portal_detector()->GetCaptivePortalState(wifi_network())); |
+ network_portal_detector()->GetCaptivePortalState(wifi1_network())); |
} |
TEST_F(NetworkPortalDetectorTest, NetworkStateNotChanged) { |
ASSERT_TRUE(is_state_idle()); |
- SetConnected(wifi_network()); |
+ SetConnected(wifi1_network()); |
ASSERT_TRUE(is_state_checking_for_portal()); |
- fetcher()->set_response_code(204); |
- OnURLFetchComplete(fetcher()); |
+ CompleteURLFetch(net::OK, 204, NULL); |
ASSERT_TRUE(is_state_idle()); |
ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATE_ONLINE, |
- network_portal_detector()->GetCaptivePortalState(wifi_network())); |
- SetConnected(wifi_network()); |
+ network_portal_detector()->GetCaptivePortalState(wifi1_network())); |
+ SetConnected(wifi1_network()); |
ASSERT_TRUE(is_state_idle()); |
} |
@@ -243,4 +258,120 @@ TEST_F(NetworkPortalDetectorTest, NetworkStateChanged) { |
// corresponding test. |
} |
+TEST_F(NetworkPortalDetectorTest, PortalDetectionTimeout) { |
+ ASSERT_TRUE(is_state_idle()); |
+ |
+ // For instantaneous timeout. |
+ set_request_timeout(base::TimeDelta::FromSeconds(0)); |
+ |
+ ASSERT_TRUE(is_state_idle()); |
+ ASSERT_EQ(0, attempt_count()); |
+ |
+ SetConnected(wifi1_network()); |
+ MessageLoop::current()->RunUntilIdle(); |
+ |
+ // First portal detection timeouts, next portal detection is |
+ // scheduled. |
+ ASSERT_TRUE(is_state_portal_detection_pending()); |
+ ASSERT_EQ(1, attempt_count()); |
+ ASSERT_EQ(base::TimeDelta::FromSeconds(3), next_attempt_delay()); |
+} |
+ |
+TEST_F(NetworkPortalDetectorTest, PortalDetectionRetryAfter) { |
+ ASSERT_TRUE(is_state_idle()); |
+ |
+ const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 101\n\n"; |
+ |
+ ASSERT_TRUE(is_state_idle()); |
+ ASSERT_EQ(0, attempt_count()); |
+ |
+ SetConnected(wifi1_network()); |
+ CompleteURLFetch(net::OK, 503, retry_after); |
+ |
+ // First portal detection completed, next portal detection is |
+ // scheduled after 101 seconds. |
+ ASSERT_TRUE(is_state_portal_detection_pending()); |
+ ASSERT_EQ(1, attempt_count()); |
+ ASSERT_EQ(base::TimeDelta::FromSeconds(101), next_attempt_delay()); |
+} |
+ |
+TEST_F(NetworkPortalDetectorTest, PortalDetectorRetryAfterIsSmall) { |
+ ASSERT_TRUE(is_state_idle()); |
+ |
+ const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 1\n\n"; |
+ |
+ ASSERT_TRUE(is_state_idle()); |
+ ASSERT_EQ(0, attempt_count()); |
+ |
+ SetConnected(wifi1_network()); |
+ CompleteURLFetch(net::OK, 503, retry_after); |
+ |
+ // First portal detection completed, next portal detection is |
+ // scheduled after 3 seconds (due to minimum time between detection |
+ // attemps). |
+ ASSERT_TRUE(is_state_portal_detection_pending()); |
+ ASSERT_EQ(1, attempt_count()); |
+ ASSERT_EQ(base::TimeDelta::FromSeconds(3), next_attempt_delay()); |
+} |
+ |
+TEST_F(NetworkPortalDetectorTest, FirstAttemptFailed) { |
+ ASSERT_TRUE(is_state_idle()); |
+ |
+ set_min_time_between_attempts(base::TimeDelta()); |
+ const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 0\n\n"; |
+ |
+ ASSERT_TRUE(is_state_idle()); |
+ ASSERT_EQ(0, attempt_count()); |
+ |
+ SetConnected(wifi1_network()); |
+ |
+ CompleteURLFetch(net::OK, 503, retry_after); |
+ ASSERT_TRUE(is_state_portal_detection_pending()); |
+ ASSERT_EQ(1, attempt_count()); |
+ ASSERT_EQ(base::TimeDelta::FromSeconds(0), next_attempt_delay()); |
+ |
+ // To run CaptivePortalDetector::DetectCaptivePortal(). |
+ MessageLoop::current()->RunUntilIdle(); |
+ |
+ CompleteURLFetch(net::OK, 204, NULL); |
+ ASSERT_TRUE(is_state_idle()); |
+ ASSERT_EQ(2, attempt_count()); |
+ ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATE_ONLINE, |
+ network_portal_detector()->GetCaptivePortalState(wifi1_network())); |
+} |
+ |
+TEST_F(NetworkPortalDetectorTest, AllAttemptsFailed) { |
+ ASSERT_TRUE(is_state_idle()); |
+ |
+ set_min_time_between_attempts(base::TimeDelta()); |
+ const char* retry_after = "HTTP/1.1 503 OK\nRetry-After: 0\n\n"; |
+ |
+ ASSERT_TRUE(is_state_idle()); |
+ ASSERT_EQ(0, attempt_count()); |
+ |
+ SetConnected(wifi1_network()); |
+ |
+ CompleteURLFetch(net::OK, 503, retry_after); |
+ ASSERT_TRUE(is_state_portal_detection_pending()); |
+ ASSERT_EQ(1, attempt_count()); |
+ ASSERT_EQ(base::TimeDelta::FromSeconds(0), next_attempt_delay()); |
+ |
+ // To run CaptivePortalDetector::DetectCaptivePortal(). |
+ MessageLoop::current()->RunUntilIdle(); |
+ |
+ CompleteURLFetch(net::OK, 503, retry_after); |
+ ASSERT_TRUE(is_state_portal_detection_pending()); |
+ ASSERT_EQ(2, attempt_count()); |
+ ASSERT_EQ(base::TimeDelta::FromSeconds(0), next_attempt_delay()); |
+ |
+ // To run CaptivePortalDetector::DetectCaptivePortal(). |
+ MessageLoop::current()->RunUntilIdle(); |
+ |
+ CompleteURLFetch(net::OK, 503, retry_after); |
+ ASSERT_TRUE(is_state_idle()); |
+ ASSERT_EQ(3, attempt_count()); |
+ ASSERT_EQ(NetworkPortalDetector::CAPTIVE_PORTAL_STATE_OFFLINE, |
+ network_portal_detector()->GetCaptivePortalState(wifi1_network())); |
+} |
+ |
} // namespace chromeos |