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

Side by Side Diff: chrome/browser/captive_portal/captive_portal_tab_observer_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: cleanup comment Created 8 years, 7 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/message_loop.h"
8 #include "chrome/browser/captive_portal/captive_portal_service.h"
9 #include "chrome/common/chrome_notification_types.h"
10 #include "content/public/browser/notification_service.h"
11 #include "net/base/net_errors.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace captive_portal {
16
17 namespace {
18
19 const char* const kHttpUrl = "http://whatever.com/";
20 const char* const kHttpsUrl = "https://whatever.com/";
21 // Error pages use a "data:" URL. Shouldn't actually matter what this is.
22 const char* const kErrorPageUrl = "data:blah";
23
24 } // namespace
25
26 class MockCaptivePortalTabHelper : public CaptivePortalTabHelper {
27 public:
28 MockCaptivePortalTabHelper() : CaptivePortalTabHelper(NULL, NULL) {
29 }
30
31 CaptivePortalTabObserver& observer() { return observer_; }
32
33 MOCK_METHOD1(OnLoadStart, void(bool));
34 MOCK_METHOD1(OnLoadCommitted, void(int));
35 MOCK_METHOD0(OnAbort, void());
36 MOCK_METHOD0(OnStopLoading, void());
37 };
38
39 class CaptivePortalTabHelperObserverTest : public testing::Test {
40 public:
41 CaptivePortalTabHelperObserverTest() {}
42 virtual ~CaptivePortalTabHelperObserverTest() {}
43
44 // Simulates a successful load of |url|.
45 void SimulateSuccess(const GURL& url) {
46 EXPECT_CALL(mock_helper(), OnLoadStart(url.SchemeIsSecure())).Times(1);
47 observer().DidStartProvisionalLoadForFrame(1, true, url, false, NULL);
48
49 EXPECT_CALL(mock_helper(), OnLoadCommitted(net::OK)).Times(1);
50 observer().DidCommitProvisionalLoadForFrame(
51 1, true, url, content::PAGE_TRANSITION_LINK, NULL);
52 }
53
54 // Simulates a connection timeout while requesting |url|.
55 void SimulateTimeout(const GURL& url) {
56 EXPECT_CALL(mock_helper(), OnLoadStart(url.SchemeIsSecure())).Times(1);
57 observer().DidStartProvisionalLoadForFrame(1, true, url, false, NULL);
58
59 observer().DidFailProvisionalLoad(
60 1, true, url, net::ERR_TIMED_OUT, string16(), NULL);
61
62 // Provisional load starts for the error page.
63 observer().DidStartProvisionalLoadForFrame(
64 1, true, GURL(kErrorPageUrl), true, NULL);
65
66 EXPECT_CALL(mock_helper(), OnLoadCommitted(net::ERR_TIMED_OUT)).Times(1);
67 observer().DidCommitProvisionalLoadForFrame(
68 1, true, GURL(kErrorPageUrl), content::PAGE_TRANSITION_LINK, NULL);
69 }
70
71 // Simulates an abort while requesting |url|.
72 void SimulateAbort(const GURL& url) {
73 EXPECT_CALL(mock_helper(), OnLoadStart(url.SchemeIsSecure())).Times(1);
74 observer().DidStartProvisionalLoadForFrame(1, true, url, false, NULL);
75
76 EXPECT_CALL(mock_helper(), OnAbort()).Times(1);
77 observer().DidFailProvisionalLoad(
78 1, true, url, net::ERR_ABORTED, string16(), NULL);
79 }
80
81 // Simulates an abort while loading an error page.
82 void SimulateAbortTimeout(const GURL& url) {
83 EXPECT_CALL(mock_helper(), OnLoadStart(url.SchemeIsSecure())).Times(1);
84 observer().DidStartProvisionalLoadForFrame(1, true, url, false, NULL);
85
86 observer().DidFailProvisionalLoad(
87 1, true, url, net::ERR_TIMED_OUT, string16(), NULL);
88
89 // Start event for the error page.
90 observer().DidStartProvisionalLoadForFrame(1, true, url, true, NULL);
91
92 EXPECT_CALL(mock_helper(), OnAbort()).Times(1);
93 observer().DidFailProvisionalLoad(
94 1, true, url, net::ERR_ABORTED, string16(), NULL);
95 }
96
97 CaptivePortalTabObserver& observer() {
98 return mock_helper_.observer();
99 }
100
101 MockCaptivePortalTabHelper& mock_helper() { return mock_helper_; }
102
103 private:
104 testing::StrictMock<MockCaptivePortalTabHelper> mock_helper_;
105 DISALLOW_COPY_AND_ASSIGN(CaptivePortalTabHelperObserverTest);
106 };
107
108 TEST_F(CaptivePortalTabHelperObserverTest, HttpSuccess) {
109 SimulateSuccess(GURL(kHttpUrl));
110
111 EXPECT_CALL(mock_helper(), OnStopLoading()).Times(1);
112 observer().DidStopLoading();
113 }
114
115 TEST_F(CaptivePortalTabHelperObserverTest, HttpTimeout) {
116 SimulateTimeout(GURL(kHttpUrl));
117
118 EXPECT_CALL(mock_helper(), OnStopLoading()).Times(1);
119 observer().DidStopLoading();
120 }
121
122 // Same as above, but simulates what happens when the Link Doctor is enabled,
123 // which adds another provisional load/commit for the error page, after the
124 // first two.
125 TEST_F(CaptivePortalTabHelperObserverTest, HttpTimeoutLinkDoctor) {
126 SimulateTimeout(GURL(kHttpUrl));
127
128 EXPECT_CALL(mock_helper(), OnLoadStart(false)).Times(1);
129 // Provisional load starts for the error page.
130 observer().DidStartProvisionalLoadForFrame(
131 1, true, GURL(kErrorPageUrl), true, NULL);
132
133 EXPECT_CALL(mock_helper(), OnLoadCommitted(net::OK)).Times(1);
134 observer().DidCommitProvisionalLoadForFrame(
135 1, true, GURL(kErrorPageUrl), content::PAGE_TRANSITION_LINK, NULL);
136
137 EXPECT_CALL(mock_helper(), OnStopLoading()).Times(1);
138 observer().DidStopLoading();
139 }
140
141 TEST_F(CaptivePortalTabHelperObserverTest, HttpsSuccess) {
142 SimulateSuccess(GURL(kHttpsUrl));
143
144 EXPECT_CALL(mock_helper(), OnStopLoading()).Times(1);
145 observer().DidStopLoading();
146 }
147
148 TEST_F(CaptivePortalTabHelperObserverTest, HttpsTimeout) {
149 SimulateTimeout(GURL(kHttpsUrl));
150 // Make sure no state was carried over from the timeout.
151 SimulateSuccess(GURL(kHttpsUrl));
152 }
153
154 TEST_F(CaptivePortalTabHelperObserverTest, HttpsAbort) {
155 SimulateAbort(GURL(kHttpsUrl));
156 // Make sure no state was carried over from the abort.
157 SimulateSuccess(GURL(kHttpsUrl));
158 }
159
160 // Abort while there's a provisional timeout error page loading.
161 TEST_F(CaptivePortalTabHelperObserverTest, HttpsAbortTimeout) {
162 SimulateAbortTimeout(GURL(kHttpsUrl));
163 // Make sure no state was carried over from the timeout or the abort.
164 SimulateSuccess(GURL(kHttpsUrl));
165 }
166
167 // Simulates navigations for a number of subframes, and makes sure no
168 // CaptivePortalTabHelper function is called.
169 TEST_F(CaptivePortalTabHelperObserverTest, HttpsSubframe) {
170 GURL url = GURL(kHttpsUrl);
171 // Normal load.
172 observer().DidStartProvisionalLoadForFrame(1, false, url, false, NULL);
173 observer().DidCommitProvisionalLoadForFrame(
174 1, false, url, content::PAGE_TRANSITION_LINK, NULL);
175
176 // Timeout.
177 observer().DidStartProvisionalLoadForFrame(2, false, url, false, NULL);
178 observer().DidFailProvisionalLoad(
179 2, false, url, net::ERR_TIMED_OUT, string16(), NULL);
180 observer().DidStartProvisionalLoadForFrame(2, false, url, true, NULL);
181 observer().DidFailProvisionalLoad(
182 2, false, url, net::ERR_ABORTED, string16(), NULL);
183
184 // Abort.
185 observer().DidStartProvisionalLoadForFrame(3, false, url, false, NULL);
186 observer().DidFailProvisionalLoad(
187 3, false, url, net::ERR_ABORTED, string16(), NULL);
188 }
189
190 // Simulates a subframe erroring out at the same time as a provisional load,
191 // but with a different error code. Make sure the TabHelper sees the correct
192 // error.
193 TEST_F(CaptivePortalTabHelperObserverTest, HttpsSubframeParallelError) {
194 // URL used by both frames.
195 GURL url = GURL(kHttpsUrl);
196
197 int frame_id = 2;
198 int subframe_id = 1;
199
200 // Loads start.
201 EXPECT_CALL(mock_helper(), OnLoadStart(url.SchemeIsSecure())).Times(1);
202 observer().DidStartProvisionalLoadForFrame(
203 frame_id, true, url, false, NULL);
204 observer().DidStartProvisionalLoadForFrame(
205 subframe_id, false, url, false, NULL);
206
207 // Loads return errors.
208 observer().DidFailProvisionalLoad(
209 frame_id, true, url, net::ERR_UNEXPECTED, string16(), NULL);
210 observer().DidFailProvisionalLoad(
211 subframe_id, false, url, net::ERR_TIMED_OUT, string16(), NULL);
212
213 // Provisional load starts for the error pages.
214 observer().DidStartProvisionalLoadForFrame(
215 frame_id, true, url, true, NULL);
216 observer().DidStartProvisionalLoadForFrame(
217 subframe_id, false, url, true, NULL);
218
219 // Error page load finishes.
220 observer().DidCommitProvisionalLoadForFrame(
221 subframe_id, false, url, content::PAGE_TRANSITION_AUTO_SUBFRAME, NULL);
222 EXPECT_CALL(mock_helper(), OnLoadCommitted(net::ERR_UNEXPECTED)).Times(1);
223 observer().DidCommitProvisionalLoadForFrame(
224 frame_id, true, url, content::PAGE_TRANSITION_LINK, NULL);
225 }
226
227 } // namespace captive_portal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698