OLD | NEW |
(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 // Used for testing CaptivePortalTabHelper in isolation from the observer. |
| 18 // Exposes a number of private functions and mocks out others. |
| 19 class TestCaptivePortalTabHelper : public CaptivePortalTabHelper { |
| 20 public: |
| 21 // |profile| will only be used as a notification source, so don't need |
| 22 // an actual TestingProfile. |
| 23 explicit TestCaptivePortalTabHelper(Profile* profile) |
| 24 : CaptivePortalTabHelper(profile, NULL) { |
| 25 } |
| 26 |
| 27 bool TimerRunning() { |
| 28 return slow_ssl_load_timer_.IsRunning(); |
| 29 } |
| 30 |
| 31 // The following methods are aliased so they can be publicly accessed by the |
| 32 // unit tests. |
| 33 |
| 34 // Needed to get the TabHelper in STATE_CAPTIVE_PORTAL_LOGIN_PAGE. |
| 35 void SetState(State state) { |
| 36 CaptivePortalTabHelper::SetState(state); |
| 37 } |
| 38 |
| 39 State state() const { |
| 40 return CaptivePortalTabHelper::state(); |
| 41 } |
| 42 |
| 43 void set_slow_ssl_load_time(base::TimeDelta slow_ssl_load_time) { |
| 44 EXPECT_FALSE(TimerRunning()); |
| 45 CaptivePortalTabHelper::set_slow_ssl_load_time(slow_ssl_load_time); |
| 46 } |
| 47 |
| 48 // CaptivePortalTabHelper: |
| 49 virtual void OnLoadStart(bool is_ssl) OVERRIDE { |
| 50 CaptivePortalTabHelper::OnLoadStart(is_ssl); |
| 51 } |
| 52 |
| 53 virtual void OnLoadCommitted(int net_error) OVERRIDE { |
| 54 CaptivePortalTabHelper::OnLoadCommitted(net_error); |
| 55 } |
| 56 |
| 57 virtual void OnAbort() OVERRIDE { |
| 58 CaptivePortalTabHelper::OnAbort(); |
| 59 } |
| 60 |
| 61 virtual void OnStopLoading() OVERRIDE { |
| 62 CaptivePortalTabHelper::OnStopLoading(); |
| 63 } |
| 64 |
| 65 MOCK_METHOD0(ReloadTab, void()); |
| 66 MOCK_METHOD0(MaybeOpenCaptivePortalLoginTab, void()); |
| 67 MOCK_METHOD0(CheckForCaptivePortal, void()); |
| 68 }; |
| 69 |
| 70 class CaptivePortalTabHelperTest : public testing::Test { |
| 71 public: |
| 72 CaptivePortalTabHelperTest() : tab_helper_(fake_profile()) { |
| 73 // Most tests don't run the message loop, so don't use a timer for them. |
| 74 tab_helper_.set_slow_ssl_load_time(base::TimeDelta()); |
| 75 } |
| 76 |
| 77 virtual ~CaptivePortalTabHelperTest() { |
| 78 } |
| 79 |
| 80 // testing::Test |
| 81 virtual void TearDown() OVERRIDE { |
| 82 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 83 // Run any pending operations, so the test fails if there was a call to |
| 84 // a mocked out function pending. |
| 85 MessageLoop::current()->RunAllPending(); |
| 86 } |
| 87 |
| 88 void SendNotification(Result previous_result, Result result) { |
| 89 CaptivePortalService::Results results; |
| 90 results.previous_result = previous_result; |
| 91 results.result = result; |
| 92 content::NotificationService::current()->Notify( |
| 93 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, |
| 94 content::Source<Profile>(fake_profile()), |
| 95 content::Details<CaptivePortalService::Results>(&results)); |
| 96 } |
| 97 |
| 98 TestCaptivePortalTabHelper& tab_helper() { return tab_helper_; } |
| 99 |
| 100 // This Profile should only be used as a notification source, so |
| 101 // initialize it to a value that will crash if it is dereferenced |
| 102 // unexpectedly. |
| 103 Profile* fake_profile() { return reinterpret_cast<Profile*>(17); } |
| 104 |
| 105 private: |
| 106 MessageLoop message_loop_; |
| 107 |
| 108 testing::StrictMock<TestCaptivePortalTabHelper> tab_helper_; |
| 109 }; |
| 110 |
| 111 // Simulates a slow SSL load when the Internet is connected. |
| 112 TEST_F(CaptivePortalTabHelperTest, InternetConnected) { |
| 113 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 114 |
| 115 tab_helper().OnLoadStart(true); |
| 116 EXPECT_EQ(CaptivePortalTabHelper::STATE_TIMER_RUNNING, tab_helper().state()); |
| 117 EXPECT_TRUE(tab_helper().TimerRunning()); |
| 118 |
| 119 EXPECT_CALL(tab_helper(), CheckForCaptivePortal()).Times(1); |
| 120 MessageLoop::current()->RunAllPending(); |
| 121 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 122 EXPECT_EQ(CaptivePortalTabHelper::STATE_MAYBE_BROKEN_BY_PORTAL, |
| 123 tab_helper().state()); |
| 124 |
| 125 SendNotification(RESULT_INTERNET_CONNECTED, RESULT_INTERNET_CONNECTED); |
| 126 |
| 127 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 128 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 129 |
| 130 tab_helper().OnLoadCommitted(net::OK); |
| 131 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 132 } |
| 133 |
| 134 // Simulates a slow SSL load when the Internet is connected. In this case, |
| 135 // the timeout error occurs before the timer triggers. Unlikely to happen |
| 136 // in practice, but best if it still works. |
| 137 TEST_F(CaptivePortalTabHelperTest, InternetConnectedTimeout) { |
| 138 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 139 |
| 140 tab_helper().OnLoadStart(true); |
| 141 EXPECT_EQ(CaptivePortalTabHelper::STATE_TIMER_RUNNING, tab_helper().state()); |
| 142 EXPECT_TRUE(tab_helper().TimerRunning()); |
| 143 |
| 144 EXPECT_CALL(tab_helper(), CheckForCaptivePortal()).Times(1); |
| 145 tab_helper().OnLoadCommitted(net::ERR_CONNECTION_TIMED_OUT); |
| 146 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 147 EXPECT_EQ(CaptivePortalTabHelper::STATE_MAYBE_BROKEN_BY_PORTAL, |
| 148 tab_helper().state()); |
| 149 |
| 150 SendNotification(RESULT_INTERNET_CONNECTED, RESULT_INTERNET_CONNECTED); |
| 151 |
| 152 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 153 } |
| 154 |
| 155 // Simulates a slow SSL load when captive portal checks return no response. |
| 156 TEST_F(CaptivePortalTabHelperTest, NoResponse) { |
| 157 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 158 |
| 159 tab_helper().OnLoadStart(true); |
| 160 EXPECT_EQ(CaptivePortalTabHelper::STATE_TIMER_RUNNING, tab_helper().state()); |
| 161 EXPECT_TRUE(tab_helper().TimerRunning()); |
| 162 |
| 163 EXPECT_CALL(tab_helper(), CheckForCaptivePortal()).Times(1); |
| 164 MessageLoop::current()->RunAllPending(); |
| 165 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 166 EXPECT_EQ(CaptivePortalTabHelper::STATE_MAYBE_BROKEN_BY_PORTAL, |
| 167 tab_helper().state()); |
| 168 |
| 169 SendNotification(RESULT_NO_RESPONSE, RESULT_NO_RESPONSE); |
| 170 |
| 171 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 172 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 173 |
| 174 tab_helper().OnLoadCommitted(net::OK); |
| 175 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 176 } |
| 177 |
| 178 // Simulates a slow HTTP load when behind a captive portal, that eventually. |
| 179 // tiems out. Since it's HTTP, the TabHelper should do nothing. |
| 180 TEST_F(CaptivePortalTabHelperTest, DoesNothingOnHttp) { |
| 181 tab_helper().OnLoadStart(false); |
| 182 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 183 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 184 |
| 185 SendNotification(RESULT_INTERNET_CONNECTED, RESULT_BEHIND_CAPTIVE_PORTAL); |
| 186 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 187 |
| 188 // The user logs in. |
| 189 SendNotification(RESULT_BEHIND_CAPTIVE_PORTAL, RESULT_INTERNET_CONNECTED); |
| 190 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 191 |
| 192 // The page times out. |
| 193 tab_helper().OnLoadCommitted(net::ERR_CONNECTION_TIMED_OUT); |
| 194 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 195 } |
| 196 |
| 197 // Simulate the normal login process. The user logs in before the error page |
| 198 // in the original tab commits. |
| 199 TEST_F(CaptivePortalTabHelperTest, Login) { |
| 200 tab_helper().OnLoadStart(true); |
| 201 |
| 202 EXPECT_CALL(tab_helper(), CheckForCaptivePortal()).Times(1); |
| 203 MessageLoop::current()->RunAllPending(); |
| 204 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 205 EXPECT_EQ(CaptivePortalTabHelper::STATE_MAYBE_BROKEN_BY_PORTAL, |
| 206 tab_helper().state()); |
| 207 |
| 208 // The captive portal service detects a captive portal. The TabHelper should |
| 209 // try and create a new login tab in response. |
| 210 EXPECT_CALL(tab_helper(), MaybeOpenCaptivePortalLoginTab()).Times(1); |
| 211 SendNotification(RESULT_INTERNET_CONNECTED, RESULT_BEHIND_CAPTIVE_PORTAL); |
| 212 EXPECT_EQ(CaptivePortalTabHelper::STATE_BROKEN_BY_PORTAL, |
| 213 tab_helper().state()); |
| 214 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 215 |
| 216 // The user logs on from another tab, and a captive portal check is triggered. |
| 217 SendNotification(RESULT_BEHIND_CAPTIVE_PORTAL, RESULT_INTERNET_CONNECTED); |
| 218 EXPECT_EQ(CaptivePortalTabHelper::STATE_NEEDS_RELOAD, tab_helper().state()); |
| 219 |
| 220 // The error page commits, which should start an asynchronous reload. |
| 221 tab_helper().OnLoadCommitted(net::ERR_CONNECTION_TIMED_OUT); |
| 222 EXPECT_EQ(CaptivePortalTabHelper::STATE_NEEDS_RELOAD, tab_helper().state()); |
| 223 |
| 224 EXPECT_CALL(tab_helper(), ReloadTab()).Times(1); |
| 225 MessageLoop::current()->RunAllPending(); |
| 226 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 227 } |
| 228 |
| 229 // Simulate the normal login process. The user logs in after the tab finishes |
| 230 // loading the error page. |
| 231 TEST_F(CaptivePortalTabHelperTest, LoginLate) { |
| 232 tab_helper().OnLoadStart(true); |
| 233 |
| 234 EXPECT_CALL(tab_helper(), CheckForCaptivePortal()).Times(1); |
| 235 MessageLoop::current()->RunAllPending(); |
| 236 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 237 EXPECT_EQ(CaptivePortalTabHelper::STATE_MAYBE_BROKEN_BY_PORTAL, |
| 238 tab_helper().state()); |
| 239 |
| 240 // The captive portal service detects a captive portal. The TabHelper should |
| 241 // try and create a new login tab in response. |
| 242 EXPECT_CALL(tab_helper(), MaybeOpenCaptivePortalLoginTab()).Times(1); |
| 243 SendNotification(RESULT_INTERNET_CONNECTED, RESULT_BEHIND_CAPTIVE_PORTAL); |
| 244 EXPECT_EQ(CaptivePortalTabHelper::STATE_BROKEN_BY_PORTAL, |
| 245 tab_helper().state()); |
| 246 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 247 |
| 248 // The error page commits. |
| 249 tab_helper().OnLoadCommitted(net::ERR_CONNECTION_TIMED_OUT); |
| 250 EXPECT_EQ(CaptivePortalTabHelper::STATE_BROKEN_BY_PORTAL, |
| 251 tab_helper().state()); |
| 252 tab_helper().OnStopLoading(); |
| 253 |
| 254 // The user logs on from another tab, and a captive portal check is triggered. |
| 255 EXPECT_CALL(tab_helper(), ReloadTab()).Times(1); |
| 256 SendNotification(RESULT_BEHIND_CAPTIVE_PORTAL, RESULT_INTERNET_CONNECTED); |
| 257 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 258 } |
| 259 |
| 260 // Simulate a login after the tab times out unexpectedly quickly. |
| 261 TEST_F(CaptivePortalTabHelperTest, TimeoutFast) { |
| 262 tab_helper().OnLoadStart(true); |
| 263 |
| 264 // The error page commits, which should trigger a captive portal check, |
| 265 // since the timer's still running. |
| 266 EXPECT_CALL(tab_helper(), CheckForCaptivePortal()).Times(1); |
| 267 tab_helper().OnLoadCommitted(net::ERR_CONNECTION_TIMED_OUT); |
| 268 EXPECT_EQ(CaptivePortalTabHelper::STATE_MAYBE_BROKEN_BY_PORTAL, |
| 269 tab_helper().state()); |
| 270 |
| 271 // The captive portal service detects a captive portal. The TabHelper should |
| 272 // try and create a new login tab in response. |
| 273 EXPECT_CALL(tab_helper(), MaybeOpenCaptivePortalLoginTab()).Times(1); |
| 274 SendNotification(RESULT_INTERNET_CONNECTED, RESULT_BEHIND_CAPTIVE_PORTAL); |
| 275 EXPECT_EQ(CaptivePortalTabHelper::STATE_BROKEN_BY_PORTAL, |
| 276 tab_helper().state()); |
| 277 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 278 |
| 279 // The user logs on from another tab, and a captive portal check is triggered. |
| 280 EXPECT_CALL(tab_helper(), ReloadTab()).Times(1); |
| 281 SendNotification(RESULT_BEHIND_CAPTIVE_PORTAL, RESULT_INTERNET_CONNECTED); |
| 282 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 283 } |
| 284 |
| 285 // Simulate the case that a user has already logged in before the tab receives a |
| 286 // captive portal result, but a RESULT_BEHIND_CAPTIVE_PORTAL was received |
| 287 // before the tab started loading. |
| 288 TEST_F(CaptivePortalTabHelperTest, AlreadyLoggedIn) { |
| 289 tab_helper().OnLoadStart(true); |
| 290 |
| 291 EXPECT_CALL(tab_helper(), CheckForCaptivePortal()).Times(1); |
| 292 MessageLoop::current()->RunAllPending(); |
| 293 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 294 EXPECT_EQ(CaptivePortalTabHelper::STATE_MAYBE_BROKEN_BY_PORTAL, |
| 295 tab_helper().state()); |
| 296 |
| 297 // The user has already logged in. Since the last result found a captive |
| 298 // portal, the tab will be reloaded if a timeout is committed. |
| 299 SendNotification(RESULT_BEHIND_CAPTIVE_PORTAL, RESULT_INTERNET_CONNECTED); |
| 300 EXPECT_EQ(CaptivePortalTabHelper::STATE_NEEDS_RELOAD, tab_helper().state()); |
| 301 |
| 302 // The error page commits, which should start an asynchronous reload. |
| 303 tab_helper().OnLoadCommitted(net::ERR_CONNECTION_TIMED_OUT); |
| 304 EXPECT_EQ(CaptivePortalTabHelper::STATE_NEEDS_RELOAD, tab_helper().state()); |
| 305 |
| 306 EXPECT_CALL(tab_helper(), ReloadTab()).Times(1); |
| 307 MessageLoop::current()->RunAllPending(); |
| 308 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 309 } |
| 310 |
| 311 // Same as above, except the result is received even before the timer triggers, |
| 312 // due to a captive portal test request from some external source, like a login |
| 313 // tab. |
| 314 TEST_F(CaptivePortalTabHelperTest, AlreadyLoggedInBeforeTimerTriggers) { |
| 315 tab_helper().OnLoadStart(true); |
| 316 |
| 317 // The user has already logged in. Since the last result indicated there is |
| 318 // a captive portal, the tab will be reloaded if it times out. |
| 319 SendNotification(RESULT_BEHIND_CAPTIVE_PORTAL, RESULT_INTERNET_CONNECTED); |
| 320 EXPECT_EQ(CaptivePortalTabHelper::STATE_NEEDS_RELOAD, tab_helper().state()); |
| 321 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 322 |
| 323 // The error page commits, which should start an asynchronous reload. |
| 324 tab_helper().OnLoadCommitted(net::ERR_CONNECTION_TIMED_OUT); |
| 325 EXPECT_EQ(CaptivePortalTabHelper::STATE_NEEDS_RELOAD, tab_helper().state()); |
| 326 |
| 327 EXPECT_CALL(tab_helper(), ReloadTab()).Times(1); |
| 328 MessageLoop::current()->RunAllPending(); |
| 329 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 330 } |
| 331 |
| 332 // Simulate the user logging in while the timer is still running. May happen |
| 333 // if the tab is reloaded just before logging in on another tab. |
| 334 TEST_F(CaptivePortalTabHelperTest, LogInWhileTimerRunning) { |
| 335 tab_helper().OnLoadStart(true); |
| 336 EXPECT_EQ(CaptivePortalTabHelper::STATE_TIMER_RUNNING, tab_helper().state()); |
| 337 EXPECT_TRUE(tab_helper().TimerRunning()); |
| 338 |
| 339 // The user has already logged in. |
| 340 SendNotification(RESULT_BEHIND_CAPTIVE_PORTAL, RESULT_INTERNET_CONNECTED); |
| 341 EXPECT_EQ(CaptivePortalTabHelper::STATE_NEEDS_RELOAD, tab_helper().state()); |
| 342 |
| 343 // The error page commits, which should start an asynchronous reload. |
| 344 tab_helper().OnLoadCommitted(net::ERR_CONNECTION_TIMED_OUT); |
| 345 EXPECT_EQ(CaptivePortalTabHelper::STATE_NEEDS_RELOAD, tab_helper().state()); |
| 346 |
| 347 EXPECT_CALL(tab_helper(), ReloadTab()).Times(1); |
| 348 MessageLoop::current()->RunAllPending(); |
| 349 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 350 } |
| 351 |
| 352 // Simulate a captive portal being detected while the time is still running. |
| 353 // The captive portal check triggered by the timer detects the captive portal |
| 354 // again, and then the user logs in. |
| 355 TEST_F(CaptivePortalTabHelperTest, BehindPortalResultWhileTimerRunning) { |
| 356 tab_helper().OnLoadStart(true); |
| 357 EXPECT_EQ(CaptivePortalTabHelper::STATE_TIMER_RUNNING, tab_helper().state()); |
| 358 EXPECT_TRUE(tab_helper().TimerRunning()); |
| 359 |
| 360 // The user is behind a captive portal, but since the tab hasn't timed out, |
| 361 // the message is ignored. |
| 362 SendNotification(RESULT_INTERNET_CONNECTED, RESULT_BEHIND_CAPTIVE_PORTAL); |
| 363 EXPECT_EQ(CaptivePortalTabHelper::STATE_TIMER_RUNNING, tab_helper().state()); |
| 364 |
| 365 // The rest proceeds as normal. |
| 366 EXPECT_CALL(tab_helper(), CheckForCaptivePortal()).Times(1); |
| 367 MessageLoop::current()->RunAllPending(); |
| 368 EXPECT_EQ(CaptivePortalTabHelper::STATE_MAYBE_BROKEN_BY_PORTAL, |
| 369 tab_helper().state()); |
| 370 |
| 371 // The captive portal service detects a captive portal, and this time the |
| 372 // tab tries to create a login tab. |
| 373 EXPECT_CALL(tab_helper(), MaybeOpenCaptivePortalLoginTab()).Times(1); |
| 374 SendNotification(RESULT_BEHIND_CAPTIVE_PORTAL, RESULT_BEHIND_CAPTIVE_PORTAL); |
| 375 EXPECT_EQ(CaptivePortalTabHelper::STATE_BROKEN_BY_PORTAL, |
| 376 tab_helper().state()); |
| 377 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 378 |
| 379 // The user logs on from another tab, and a captive portal check is triggered. |
| 380 SendNotification(RESULT_BEHIND_CAPTIVE_PORTAL, RESULT_INTERNET_CONNECTED); |
| 381 EXPECT_EQ(CaptivePortalTabHelper::STATE_NEEDS_RELOAD, tab_helper().state()); |
| 382 |
| 383 // The error page commits, which should start an asynchronous reload. |
| 384 tab_helper().OnLoadCommitted(net::ERR_CONNECTION_TIMED_OUT); |
| 385 EXPECT_EQ(CaptivePortalTabHelper::STATE_NEEDS_RELOAD, tab_helper().state()); |
| 386 |
| 387 EXPECT_CALL(tab_helper(), ReloadTab()).Times(1); |
| 388 MessageLoop::current()->RunAllPending(); |
| 389 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 390 } |
| 391 |
| 392 // The CaptivePortalService detects the user has logged in to a captive portal |
| 393 // while the timer is still running, but the original load succeeds, so no |
| 394 // reload is done. |
| 395 TEST_F(CaptivePortalTabHelperTest, LogInWhileTimerRunningNoError) { |
| 396 tab_helper().OnLoadStart(true); |
| 397 EXPECT_EQ(CaptivePortalTabHelper::STATE_TIMER_RUNNING, tab_helper().state()); |
| 398 EXPECT_TRUE(tab_helper().TimerRunning()); |
| 399 |
| 400 // The user has already logged in. |
| 401 SendNotification(RESULT_BEHIND_CAPTIVE_PORTAL, RESULT_INTERNET_CONNECTED); |
| 402 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 403 EXPECT_EQ(CaptivePortalTabHelper::STATE_NEEDS_RELOAD, tab_helper().state()); |
| 404 |
| 405 // The page successfully commits, so no reload is triggered. |
| 406 tab_helper().OnLoadCommitted(net::OK); |
| 407 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 408 } |
| 409 |
| 410 // Simulate a normal page load in a login tab while behind a captive portal. |
| 411 // The load does not log the user in. |
| 412 TEST_F(CaptivePortalTabHelperTest, LoginTabPageLoad) { |
| 413 tab_helper().SetState( |
| 414 CaptivePortalTabHelper::STATE_CAPTIVE_PORTAL_LOGIN_PAGE); |
| 415 tab_helper().OnLoadStart(false); |
| 416 EXPECT_EQ(CaptivePortalTabHelper::STATE_CAPTIVE_PORTAL_LOGIN_PAGE, |
| 417 tab_helper().state()); |
| 418 |
| 419 tab_helper().OnLoadCommitted(net::OK); |
| 420 EXPECT_EQ(CaptivePortalTabHelper::STATE_CAPTIVE_PORTAL_LOGIN_PAGE, |
| 421 tab_helper().state()); |
| 422 |
| 423 EXPECT_CALL(tab_helper(), CheckForCaptivePortal()).Times(1); |
| 424 tab_helper().OnStopLoading(); |
| 425 EXPECT_EQ(CaptivePortalTabHelper::STATE_CAPTIVE_PORTAL_LOGIN_PAGE, |
| 426 tab_helper().state()); |
| 427 |
| 428 SendNotification(RESULT_BEHIND_CAPTIVE_PORTAL, RESULT_BEHIND_CAPTIVE_PORTAL); |
| 429 EXPECT_EQ(CaptivePortalTabHelper::STATE_CAPTIVE_PORTAL_LOGIN_PAGE, |
| 430 tab_helper().state()); |
| 431 } |
| 432 |
| 433 // Simulate logging in through a login tab via an HTTP page. |
| 434 TEST_F(CaptivePortalTabHelperTest, LoginTabLogin) { |
| 435 tab_helper().SetState( |
| 436 CaptivePortalTabHelper::STATE_CAPTIVE_PORTAL_LOGIN_PAGE); |
| 437 tab_helper().OnLoadStart(false); |
| 438 EXPECT_EQ(CaptivePortalTabHelper::STATE_CAPTIVE_PORTAL_LOGIN_PAGE, |
| 439 tab_helper().state()); |
| 440 |
| 441 tab_helper().OnLoadCommitted(net::OK); |
| 442 EXPECT_EQ(CaptivePortalTabHelper::STATE_CAPTIVE_PORTAL_LOGIN_PAGE, |
| 443 tab_helper().state()); |
| 444 |
| 445 EXPECT_CALL(tab_helper(), CheckForCaptivePortal()).Times(1); |
| 446 tab_helper().OnStopLoading(); |
| 447 EXPECT_EQ(CaptivePortalTabHelper::STATE_CAPTIVE_PORTAL_LOGIN_PAGE, |
| 448 tab_helper().state()); |
| 449 |
| 450 SendNotification(RESULT_BEHIND_CAPTIVE_PORTAL, RESULT_INTERNET_CONNECTED); |
| 451 EXPECT_EQ(CaptivePortalTabHelper::STATE_NONE, tab_helper().state()); |
| 452 } |
| 453 |
| 454 // Simulate an SSL connection timout in a login tab while behind a captive |
| 455 // portal. |
| 456 TEST_F(CaptivePortalTabHelperTest, LoginTabTimeout) { |
| 457 // Load starts. |
| 458 tab_helper().SetState( |
| 459 CaptivePortalTabHelper::STATE_CAPTIVE_PORTAL_LOGIN_PAGE); |
| 460 tab_helper().OnLoadStart(true); |
| 461 EXPECT_EQ(CaptivePortalTabHelper::STATE_CAPTIVE_PORTAL_LOGIN_PAGE, |
| 462 tab_helper().state()); |
| 463 EXPECT_FALSE(tab_helper().TimerRunning()); |
| 464 |
| 465 // Page times out. |
| 466 tab_helper().OnLoadCommitted(net::ERR_CONNECTION_TIMED_OUT); |
| 467 EXPECT_EQ(CaptivePortalTabHelper::STATE_CAPTIVE_PORTAL_LOGIN_PAGE, |
| 468 tab_helper().state()); |
| 469 |
| 470 // The error page loads. |
| 471 EXPECT_CALL(tab_helper(), CheckForCaptivePortal()).Times(1); |
| 472 tab_helper().OnStopLoading(); |
| 473 } |
| 474 |
| 475 } // namespace captive_portal |
OLD | NEW |