| 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/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 namespace captive_portal { |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 // An observer watches the CaptivePortalDetector. It tracks the last | 31 // An observer watches the CaptivePortalDetector. It tracks the last |
| 32 // received result and the total number of received results. | 32 // received result and the total number of received results. |
| 33 class CaptivePortalObserver : public content::NotificationObserver { | 33 class CaptivePortalObserver : public content::NotificationObserver { |
| 34 public: | 34 public: |
| 35 CaptivePortalObserver(Profile* profile, | 35 CaptivePortalObserver(Profile* profile, |
| 36 CaptivePortalService* captive_portal_service) | 36 CaptivePortalService* captive_portal_service) |
| 37 : captive_portal_result_( | 37 : captive_portal_result_( |
| 38 captive_portal_service->last_detection_result()), | 38 captive_portal_service->last_detection_result()), |
| 39 num_results_received_(0), | 39 num_results_received_(0), |
| 40 profile_(profile), | 40 profile_(profile), |
| 41 captive_portal_service_(captive_portal_service) { | 41 captive_portal_service_(captive_portal_service) { |
| 42 registrar_.Add(this, | 42 registrar_.Add(this, |
| 43 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, | 43 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, |
| 44 content::Source<Profile>(profile_)); | 44 content::Source<Profile>(profile_)); |
| 45 } | 45 } |
| 46 | 46 |
| 47 Result captive_portal_result() const { return captive_portal_result_; } | 47 CaptivePortalResult captive_portal_result() const { |
| 48 return captive_portal_result_; |
| 49 } |
| 48 | 50 |
| 49 int num_results_received() const { return num_results_received_; } | 51 int num_results_received() const { return num_results_received_; } |
| 50 | 52 |
| 51 private: | 53 private: |
| 52 virtual void Observe(int type, | 54 virtual void Observe(int type, |
| 53 const content::NotificationSource& source, | 55 const content::NotificationSource& source, |
| 54 const content::NotificationDetails& details) OVERRIDE { | 56 const content::NotificationDetails& details) OVERRIDE { |
| 55 ASSERT_EQ(type, chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT); | 57 ASSERT_EQ(type, chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT); |
| 56 ASSERT_EQ(profile_, content::Source<Profile>(source).ptr()); | 58 ASSERT_EQ(profile_, content::Source<Profile>(source).ptr()); |
| 57 | 59 |
| 58 CaptivePortalService::Results *results = | 60 CaptivePortalService::Results *results = |
| 59 content::Details<CaptivePortalService::Results>(details).ptr(); | 61 content::Details<CaptivePortalService::Results>(details).ptr(); |
| 60 | 62 |
| 61 EXPECT_EQ(captive_portal_result_, results->previous_result); | 63 EXPECT_EQ(captive_portal_result_, results->previous_result); |
| 62 EXPECT_EQ(captive_portal_service_->last_detection_result(), | 64 EXPECT_EQ(captive_portal_service_->last_detection_result(), |
| 63 results->result); | 65 results->result); |
| 64 | 66 |
| 65 captive_portal_result_ = results->result; | 67 captive_portal_result_ = results->result; |
| 66 ++num_results_received_; | 68 ++num_results_received_; |
| 67 } | 69 } |
| 68 | 70 |
| 69 Result captive_portal_result_; | 71 CaptivePortalResult captive_portal_result_; |
| 70 int num_results_received_; | 72 int num_results_received_; |
| 71 | 73 |
| 72 Profile* profile_; | 74 Profile* profile_; |
| 73 CaptivePortalService* captive_portal_service_; | 75 CaptivePortalService* captive_portal_service_; |
| 74 | 76 |
| 75 content::NotificationRegistrar registrar_; | 77 content::NotificationRegistrar registrar_; |
| 76 | 78 |
| 77 DISALLOW_COPY_AND_ASSIGN(CaptivePortalObserver); | 79 DISALLOW_COPY_AND_ASSIGN(CaptivePortalObserver); |
| 78 }; | 80 }; |
| 79 | 81 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 // returning with the specified |net_error| and |status_code|. If |net_error| | 139 // returning with the specified |net_error| and |status_code|. If |net_error| |
| 138 // is not OK, |status_code| is ignored. Expects the CaptivePortalService to | 140 // is not OK, |status_code| is ignored. Expects the CaptivePortalService to |
| 139 // return |expected_result|. | 141 // return |expected_result|. |
| 140 // | 142 // |
| 141 // |expected_delay_secs| is the expected value of GetTimeUntilNextRequest(). | 143 // |expected_delay_secs| is the expected value of GetTimeUntilNextRequest(). |
| 142 // The function makes sure the value is as expected, and then simulates | 144 // The function makes sure the value is as expected, and then simulates |
| 143 // waiting for that period of time before running the test. | 145 // waiting for that period of time before running the test. |
| 144 // | 146 // |
| 145 // If |response_headers| is non-NULL, the response will use it as headers | 147 // 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. | 148 // for the simulate URL request. It must use single linefeeds as line breaks. |
| 147 void RunTest(Result expected_result, | 149 void RunTest(CaptivePortalResult expected_result, |
| 148 int net_error, | 150 int net_error, |
| 149 int status_code, | 151 int status_code, |
| 150 int expected_delay_secs, | 152 int expected_delay_secs, |
| 151 const char* response_headers) { | 153 const char* response_headers) { |
| 152 base::TimeDelta expected_delay = | 154 base::TimeDelta expected_delay = |
| 153 base::TimeDelta::FromSeconds(expected_delay_secs); | 155 base::TimeDelta::FromSeconds(expected_delay_secs); |
| 154 | 156 |
| 155 ASSERT_EQ(CaptivePortalService::STATE_IDLE, service()->state()); | 157 ASSERT_EQ(CaptivePortalService::STATE_IDLE, service()->state()); |
| 156 ASSERT_EQ(expected_delay, GetTimeUntilNextRequest()); | 158 ASSERT_EQ(expected_delay, GetTimeUntilNextRequest()); |
| 157 | 159 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 base::RunLoop().RunUntilIdle(); | 202 base::RunLoop().RunUntilIdle(); |
| 201 EXPECT_FALSE(FetchingURL()); | 203 EXPECT_FALSE(FetchingURL()); |
| 202 EXPECT_FALSE(TimerRunning()); | 204 EXPECT_FALSE(TimerRunning()); |
| 203 EXPECT_EQ(1, observer.num_results_received()); | 205 EXPECT_EQ(1, observer.num_results_received()); |
| 204 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); | 206 EXPECT_EQ(RESULT_INTERNET_CONNECTED, observer.captive_portal_result()); |
| 205 } | 207 } |
| 206 | 208 |
| 207 // Tests exponential backoff. Prior to calling, the relevant recheck settings | 209 // 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 | 210 // must be set to have a minimum time of 100 seconds, with 2 checks before |
| 209 // starting exponential backoff. | 211 // starting exponential backoff. |
| 210 void RunBackoffTest(Result expected_result, int net_error, int status_code) { | 212 void RunBackoffTest(CaptivePortalResult expected_result, |
| 213 int net_error, |
| 214 int status_code) { |
| 211 RunTest(expected_result, net_error, status_code, 0, NULL); | 215 RunTest(expected_result, net_error, status_code, 0, NULL); |
| 212 RunTest(expected_result, net_error, status_code, 0, NULL); | 216 RunTest(expected_result, net_error, status_code, 0, NULL); |
| 213 RunTest(expected_result, net_error, status_code, 100, NULL); | 217 RunTest(expected_result, net_error, status_code, 100, NULL); |
| 214 RunTest(expected_result, net_error, status_code, 200, NULL); | 218 RunTest(expected_result, net_error, status_code, 200, NULL); |
| 215 RunTest(expected_result, net_error, status_code, 400, NULL); | 219 RunTest(expected_result, net_error, status_code, 400, NULL); |
| 216 RunTest(expected_result, net_error, status_code, 800, NULL); | 220 RunTest(expected_result, net_error, status_code, 800, NULL); |
| 217 RunTest(expected_result, net_error, status_code, 1600, NULL); | 221 RunTest(expected_result, net_error, status_code, 1600, NULL); |
| 218 RunTest(expected_result, net_error, status_code, 1600, NULL); | 222 RunTest(expected_result, net_error, status_code, 1600, NULL); |
| 219 } | 223 } |
| 220 | 224 |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 | 512 |
| 509 RunTest(RESULT_NO_RESPONSE, | 513 RunTest(RESULT_NO_RESPONSE, |
| 510 net::OK, | 514 net::OK, |
| 511 503, | 515 503, |
| 512 0, | 516 0, |
| 513 "HTTP/1.1 503 OK\nRetry-After: Tue, 17 Apr 2012 18:02:51 GMT\n\n"); | 517 "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()); | 518 EXPECT_EQ(base::TimeDelta::FromSeconds(51), GetTimeUntilNextRequest()); |
| 515 } | 519 } |
| 516 | 520 |
| 517 } // namespace captive_portal | 521 } // namespace captive_portal |
| OLD | NEW |