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

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

Powered by Google App Engine
This is Rietveld 408576698