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

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

Issue 10020051: Open a login tab on captive portal detection on SSL loads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Fix typo in comment, forward declare CaptivePortalTabReloader in the TabHelper Created 8 years, 6 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h"
6
7 #include "base/callback.h"
8 #include "chrome/browser/captive_portal/captive_portal_service.h"
9 #include "chrome/browser/captive_portal/captive_portal_tab_reloader.h"
10 #include "chrome/common/chrome_notification_types.h"
11 #include "content/public/browser/notification_details.h"
12 #include "content/public/browser/notification_service.h"
13 #include "content/public/browser/notification_source.h"
14 #include "net/base/net_errors.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace captive_portal {
19
20 namespace {
21
22 const char* const kHttpUrl = "http://whatever.com/";
23 const char* const kHttpsUrl = "https://whatever.com/";
24 // Error pages use a "data:" URL. Shouldn't actually matter what this is.
25 const char* const kErrorPageUrl = "data:blah";
26
27 } // namespace
28
29 class MockCaptivePortalTabReloader : public CaptivePortalTabReloader {
30 public:
31 MockCaptivePortalTabReloader()
32 : CaptivePortalTabReloader(NULL, NULL, base::Callback<void()>()) {
33 }
34
35 MOCK_METHOD1(OnLoadStart, void(bool));
36 MOCK_METHOD1(OnLoadCommitted, void(int));
37 MOCK_METHOD0(OnAbort, void());
38 MOCK_METHOD2(OnCaptivePortalResults, void(Result, Result));
39 };
40
41 class CaptivePortalTabHelperTest : public testing::Test {
42 public:
43 CaptivePortalTabHelperTest()
44 : tab_helper_(NULL, NULL),
45 mock_reloader_(new testing::StrictMock<MockCaptivePortalTabReloader>) {
46 tab_helper_.SetTabReloaderForTest(mock_reloader_);
47 }
48 virtual ~CaptivePortalTabHelperTest() {}
49
50 // Simulates a successful load of |url|.
51 void SimulateSuccess(const GURL& url) {
52 EXPECT_CALL(mock_reloader(), OnLoadStart(url.SchemeIsSecure())).Times(1);
53 tab_helper().DidStartProvisionalLoadForFrame(1, true, url, false, NULL);
54
55 EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::OK)).Times(1);
56 tab_helper().DidCommitProvisionalLoadForFrame(
57 1, true, url, content::PAGE_TRANSITION_LINK, NULL);
58 }
59
60 // Simulates a connection timeout while requesting |url|.
61 void SimulateTimeout(const GURL& url) {
62 EXPECT_CALL(mock_reloader(), OnLoadStart(url.SchemeIsSecure())).Times(1);
63 tab_helper().DidStartProvisionalLoadForFrame(1, true, url, false, NULL);
64
65 tab_helper().DidFailProvisionalLoad(
66 1, true, url, net::ERR_TIMED_OUT, string16(), NULL);
67
68 // Provisional load starts for the error page.
69 tab_helper().DidStartProvisionalLoadForFrame(
70 1, true, GURL(kErrorPageUrl), true, NULL);
71
72 EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::ERR_TIMED_OUT)).Times(1);
73 tab_helper().DidCommitProvisionalLoadForFrame(
74 1, true, GURL(kErrorPageUrl), content::PAGE_TRANSITION_LINK, NULL);
75 }
76
77 // Simulates an abort while requesting |url|.
78 void SimulateAbort(const GURL& url) {
79 EXPECT_CALL(mock_reloader(), OnLoadStart(url.SchemeIsSecure())).Times(1);
80 tab_helper().DidStartProvisionalLoadForFrame(1, true, url, false, NULL);
81
82 EXPECT_CALL(mock_reloader(), OnAbort()).Times(1);
83 tab_helper().DidFailProvisionalLoad(
84 1, true, url, net::ERR_ABORTED, string16(), NULL);
85 }
86
87 // Simulates an abort while loading an error page.
88 void SimulateAbortTimeout(const GURL& url) {
89 EXPECT_CALL(mock_reloader(), OnLoadStart(url.SchemeIsSecure())).Times(1);
90 tab_helper().DidStartProvisionalLoadForFrame(1, true, url, false, NULL);
91
92 tab_helper().DidFailProvisionalLoad(
93 1, true, url, net::ERR_TIMED_OUT, string16(), NULL);
94
95 // Start event for the error page.
96 tab_helper().DidStartProvisionalLoadForFrame(1, true, url, true, NULL);
97
98 EXPECT_CALL(mock_reloader(), OnAbort()).Times(1);
99 tab_helper().DidFailProvisionalLoad(
100 1, true, url, net::ERR_ABORTED, string16(), NULL);
101 }
102
103 CaptivePortalTabHelper& tab_helper() {
104 return tab_helper_;
105 }
106
107 void ObservePortalResult(Result previous_result, Result result) {
108 content::Source<Profile> source_profile(NULL);
109
110 CaptivePortalService::Results results;
111 results.previous_result = previous_result;
112 results.result = result;
113 content::Details<CaptivePortalService::Results> details_results(&results);
114
115 EXPECT_CALL(mock_reloader(), OnCaptivePortalResults(previous_result,
116 result)).Times(1);
117 tab_helper().Observe(chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT,
118 source_profile,
119 details_results);
120 }
121
122 MockCaptivePortalTabReloader& mock_reloader() { return *mock_reloader_; }
123
124 void SetIsLoginTab() {
125 tab_helper().SetIsLoginTab();
126 }
127
128 private:
129 CaptivePortalTabHelper tab_helper_;
130
131 // Owned by |tab_helper_|.
132 testing::StrictMock<MockCaptivePortalTabReloader>* mock_reloader_;
133
134 DISALLOW_COPY_AND_ASSIGN(CaptivePortalTabHelperTest);
135 };
136
137 TEST_F(CaptivePortalTabHelperTest, HttpSuccess) {
138 SimulateSuccess(GURL(kHttpUrl));
139 tab_helper().DidStopLoading();
140 }
141
142 TEST_F(CaptivePortalTabHelperTest, HttpTimeout) {
143 SimulateTimeout(GURL(kHttpUrl));
144 tab_helper().DidStopLoading();
145 }
146
147 // Same as above, but simulates what happens when the Link Doctor is enabled,
148 // which adds another provisional load/commit for the error page, after the
149 // first two.
150 TEST_F(CaptivePortalTabHelperTest, HttpTimeoutLinkDoctor) {
151 SimulateTimeout(GURL(kHttpUrl));
152
153 EXPECT_CALL(mock_reloader(), OnLoadStart(false)).Times(1);
154 // Provisional load starts for the error page.
155 tab_helper().DidStartProvisionalLoadForFrame(
156 1, true, GURL(kErrorPageUrl), true, NULL);
157
158 EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::OK)).Times(1);
159 tab_helper().DidCommitProvisionalLoadForFrame(
160 1, true, GURL(kErrorPageUrl), content::PAGE_TRANSITION_LINK, NULL);
161 tab_helper().DidStopLoading();
162 }
163
164 TEST_F(CaptivePortalTabHelperTest, HttpsSuccess) {
165 SimulateSuccess(GURL(kHttpsUrl));
166 tab_helper().DidStopLoading();
167 EXPECT_FALSE(tab_helper().IsLoginTab());
168 }
169
170 TEST_F(CaptivePortalTabHelperTest, HttpsTimeout) {
171 SimulateTimeout(GURL(kHttpsUrl));
172 // Make sure no state was carried over from the timeout.
173 SimulateSuccess(GURL(kHttpsUrl));
174 EXPECT_FALSE(tab_helper().IsLoginTab());
175 }
176
177 TEST_F(CaptivePortalTabHelperTest, HttpsAbort) {
178 SimulateAbort(GURL(kHttpsUrl));
179 // Make sure no state was carried over from the abort.
180 SimulateSuccess(GURL(kHttpsUrl));
181 EXPECT_FALSE(tab_helper().IsLoginTab());
182 }
183
184 // Abort while there's a provisional timeout error page loading.
185 TEST_F(CaptivePortalTabHelperTest, HttpsAbortTimeout) {
186 SimulateAbortTimeout(GURL(kHttpsUrl));
187 // Make sure no state was carried over from the timeout or the abort.
188 SimulateSuccess(GURL(kHttpsUrl));
189 EXPECT_FALSE(tab_helper().IsLoginTab());
190 }
191
192 // Simulates navigations for a number of subframes, and makes sure no
193 // CaptivePortalTabHelper function is called.
194 TEST_F(CaptivePortalTabHelperTest, HttpsSubframe) {
195 GURL url = GURL(kHttpsUrl);
196 // Normal load.
197 tab_helper().DidStartProvisionalLoadForFrame(1, false, url, false, NULL);
198 tab_helper().DidCommitProvisionalLoadForFrame(
199 1, false, url, content::PAGE_TRANSITION_LINK, NULL);
200
201 // Timeout.
202 tab_helper().DidStartProvisionalLoadForFrame(2, false, url, false, NULL);
203 tab_helper().DidFailProvisionalLoad(
204 2, false, url, net::ERR_TIMED_OUT, string16(), NULL);
205 tab_helper().DidStartProvisionalLoadForFrame(2, false, url, true, NULL);
206 tab_helper().DidFailProvisionalLoad(
207 2, false, url, net::ERR_ABORTED, string16(), NULL);
208
209 // Abort.
210 tab_helper().DidStartProvisionalLoadForFrame(3, false, url, false, NULL);
211 tab_helper().DidFailProvisionalLoad(
212 3, false, url, net::ERR_ABORTED, string16(), NULL);
213 }
214
215 // Simulates a subframe erroring out at the same time as a provisional load,
216 // but with a different error code. Make sure the TabHelper sees the correct
217 // error.
218 TEST_F(CaptivePortalTabHelperTest, HttpsSubframeParallelError) {
219 // URL used by both frames.
220 GURL url = GURL(kHttpsUrl);
221
222 int frame_id = 2;
223 int subframe_id = 1;
224
225 // Loads start.
226 EXPECT_CALL(mock_reloader(), OnLoadStart(url.SchemeIsSecure())).Times(1);
227 tab_helper().DidStartProvisionalLoadForFrame(
228 frame_id, true, url, false, NULL);
229 tab_helper().DidStartProvisionalLoadForFrame(
230 subframe_id, false, url, false, NULL);
231
232 // Loads return errors.
233 tab_helper().DidFailProvisionalLoad(
234 frame_id, true, url, net::ERR_UNEXPECTED, string16(), NULL);
235 tab_helper().DidFailProvisionalLoad(
236 subframe_id, false, url, net::ERR_TIMED_OUT, string16(), NULL);
237
238 // Provisional load starts for the error pages.
239 tab_helper().DidStartProvisionalLoadForFrame(
240 frame_id, true, url, true, NULL);
241 tab_helper().DidStartProvisionalLoadForFrame(
242 subframe_id, false, url, true, NULL);
243
244 // Error page load finishes.
245 tab_helper().DidCommitProvisionalLoadForFrame(
246 subframe_id, false, url, content::PAGE_TRANSITION_AUTO_SUBFRAME, NULL);
247 EXPECT_CALL(mock_reloader(), OnLoadCommitted(net::ERR_UNEXPECTED)).Times(1);
248 tab_helper().DidCommitProvisionalLoadForFrame(
249 frame_id, true, url, content::PAGE_TRANSITION_LINK, NULL);
250 }
251
252 TEST_F(CaptivePortalTabHelperTest, LoginTabLogin) {
253 EXPECT_FALSE(tab_helper().IsLoginTab());
254 SetIsLoginTab();
255 EXPECT_TRUE(tab_helper().IsLoginTab());
256
257 ObservePortalResult(RESULT_INTERNET_CONNECTED, RESULT_INTERNET_CONNECTED);
258 EXPECT_FALSE(tab_helper().IsLoginTab());
259 }
260
261 TEST_F(CaptivePortalTabHelperTest, LoginTabError) {
262 EXPECT_FALSE(tab_helper().IsLoginTab());
263
264 SetIsLoginTab();
265 EXPECT_TRUE(tab_helper().IsLoginTab());
266
267 ObservePortalResult(RESULT_INTERNET_CONNECTED, RESULT_NO_RESPONSE);
268 EXPECT_FALSE(tab_helper().IsLoginTab());
269 }
270
271 TEST_F(CaptivePortalTabHelperTest, LoginTabMultipleResultsBeforeLogin) {
272 EXPECT_FALSE(tab_helper().IsLoginTab());
273
274 SetIsLoginTab();
275 EXPECT_TRUE(tab_helper().IsLoginTab());
276
277 ObservePortalResult(RESULT_INTERNET_CONNECTED, RESULT_BEHIND_CAPTIVE_PORTAL);
278 EXPECT_TRUE(tab_helper().IsLoginTab());
279
280 ObservePortalResult(RESULT_BEHIND_CAPTIVE_PORTAL,
281 RESULT_BEHIND_CAPTIVE_PORTAL);
282 EXPECT_TRUE(tab_helper().IsLoginTab());
283
284 ObservePortalResult(RESULT_NO_RESPONSE, RESULT_INTERNET_CONNECTED);
285 EXPECT_FALSE(tab_helper().IsLoginTab());
286 }
287
288 TEST_F(CaptivePortalTabHelperTest, NoLoginTab) {
289 EXPECT_FALSE(tab_helper().IsLoginTab());
290
291 ObservePortalResult(RESULT_INTERNET_CONNECTED, RESULT_BEHIND_CAPTIVE_PORTAL);
292 EXPECT_FALSE(tab_helper().IsLoginTab());
293
294 ObservePortalResult(RESULT_BEHIND_CAPTIVE_PORTAL, RESULT_NO_RESPONSE);
295 EXPECT_FALSE(tab_helper().IsLoginTab());
296
297 ObservePortalResult(RESULT_NO_RESPONSE, RESULT_INTERNET_CONNECTED);
298 EXPECT_FALSE(tab_helper().IsLoginTab());
299 }
300
301 } // namespace captive_portal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698