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_detector.h" | 5 #include "chrome/browser/captive_portal/captive_portal_detector.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/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "chrome/browser/captive_portal/testing_utils.h" | 11 #include "chrome/browser/captive_portal/testing_utils.h" |
12 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 #include "net/http/http_response_headers.h" | |
16 #include "net/url_request/test_url_fetcher_factory.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
18 | 16 |
19 namespace captive_portal { | 17 namespace captive_portal { |
20 | 18 |
21 namespace { | 19 namespace { |
22 | 20 |
23 class CaptivePortalClient { | 21 class CaptivePortalClient { |
24 public: | 22 public: |
25 explicit CaptivePortalClient(CaptivePortalDetector* captive_portal_detector) | 23 explicit CaptivePortalClient(CaptivePortalDetector* captive_portal_detector) |
26 : captive_portal_detector_(captive_portal_detector), | 24 : captive_portal_detector_(captive_portal_detector), |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 | 59 |
62 void RunTest(const CaptivePortalDetector::Results& expected_results, | 60 void RunTest(const CaptivePortalDetector::Results& expected_results, |
63 int net_error, | 61 int net_error, |
64 int status_code, | 62 int status_code, |
65 const char* response_headers) { | 63 const char* response_headers) { |
66 ASSERT_FALSE(FetchingURL()); | 64 ASSERT_FALSE(FetchingURL()); |
67 | 65 |
68 GURL url(CaptivePortalDetector::kDefaultURL); | 66 GURL url(CaptivePortalDetector::kDefaultURL); |
69 CaptivePortalClient client(detector()); | 67 CaptivePortalClient client(detector()); |
70 | 68 |
71 net::TestURLFetcherFactory factory; | |
72 detector()->DetectCaptivePortal(url, | 69 detector()->DetectCaptivePortal(url, |
73 base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted, | 70 base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted, |
74 base::Unretained(&client))); | 71 base::Unretained(&client))); |
75 | 72 |
76 ASSERT_TRUE(FetchingURL()); | 73 ASSERT_TRUE(FetchingURL()); |
77 MessageLoop::current()->RunUntilIdle(); | 74 MessageLoop::current()->RunUntilIdle(); |
78 | 75 |
79 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 76 CompleteURLFetch(net_error, status_code, response_headers); |
80 if (net_error != net::OK) { | |
81 EXPECT_FALSE(response_headers); | |
82 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
83 net_error)); | |
84 } else { | |
85 fetcher->set_response_code(status_code); | |
86 if (response_headers) { | |
87 scoped_refptr<net::HttpResponseHeaders> headers( | |
88 CreateResponseHeaders(response_headers)); | |
89 EXPECT_EQ(status_code, headers->response_code()); | |
90 fetcher->set_response_headers(headers); | |
91 } | |
92 } | |
93 | |
94 OnURLFetchComplete(fetcher); | |
95 | 77 |
96 EXPECT_FALSE(FetchingURL()); | 78 EXPECT_FALSE(FetchingURL()); |
97 EXPECT_EQ(1, client.num_results_received()); | 79 EXPECT_EQ(1, client.num_results_received()); |
98 EXPECT_EQ(expected_results.result, client.captive_portal_results().result); | 80 EXPECT_EQ(expected_results.result, client.captive_portal_results().result); |
99 EXPECT_EQ(expected_results.retry_after_delta, | 81 EXPECT_EQ(expected_results.retry_after_delta, |
100 client.captive_portal_results().retry_after_delta); | 82 client.captive_portal_results().retry_after_delta); |
101 } | 83 } |
102 | 84 |
103 void RunCancelTest() { | 85 void RunCancelTest() { |
104 ASSERT_FALSE(FetchingURL()); | 86 ASSERT_FALSE(FetchingURL()); |
105 | 87 |
106 GURL url(CaptivePortalDetector::kDefaultURL); | 88 GURL url(CaptivePortalDetector::kDefaultURL); |
107 CaptivePortalClient client(detector()); | 89 CaptivePortalClient client(detector()); |
108 | 90 |
109 net::TestURLFetcherFactory factory; | |
110 detector()->DetectCaptivePortal(url, | 91 detector()->DetectCaptivePortal(url, |
111 base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted, | 92 base::Bind(&CaptivePortalClient::OnPortalDetectionCompleted, |
112 base::Unretained(&client))); | 93 base::Unretained(&client))); |
113 | 94 |
114 ASSERT_TRUE(FetchingURL()); | 95 ASSERT_TRUE(FetchingURL()); |
115 MessageLoop::current()->RunUntilIdle(); | 96 MessageLoop::current()->RunUntilIdle(); |
116 | 97 |
117 detector()->Cancel(); | 98 detector()->Cancel(); |
118 | 99 |
119 ASSERT_FALSE(FetchingURL()); | 100 ASSERT_FALSE(FetchingURL()); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 } | 184 } |
204 | 185 |
205 TEST_F(CaptivePortalDetectorTest, Cancel) { | 186 TEST_F(CaptivePortalDetectorTest, Cancel) { |
206 RunCancelTest(); | 187 RunCancelTest(); |
207 CaptivePortalDetector::Results results; | 188 CaptivePortalDetector::Results results; |
208 results.result = RESULT_INTERNET_CONNECTED; | 189 results.result = RESULT_INTERNET_CONNECTED; |
209 RunTest(results, net::OK, 204, NULL); | 190 RunTest(results, net::OK, 204, NULL); |
210 } | 191 } |
211 | 192 |
212 } // namespace captive_portal | 193 } // namespace captive_portal |
OLD | NEW |