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 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); |
| 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()); |
| 61 |
| 62 // Provisional load starts for the error page. |
| 63 observer().DidStartProvisionalLoadForFrame(1, true, url, true, NULL); |
| 64 |
| 65 EXPECT_CALL(mock_helper(), OnLoadCommitted(net::ERR_TIMED_OUT)).Times(1); |
| 66 observer().DidCommitProvisionalLoadForFrame( |
| 67 1, true, url, content::PAGE_TRANSITION_LINK); |
| 68 } |
| 69 |
| 70 // Simulates a abort while requesting |url|. |
| 71 void SimulateAbort(const GURL& url) { |
| 72 EXPECT_CALL(mock_helper(), OnLoadStart(url.SchemeIsSecure())).Times(1); |
| 73 observer().DidStartProvisionalLoadForFrame(1, true, url, false, NULL); |
| 74 |
| 75 EXPECT_CALL(mock_helper(), OnAbort()).Times(1); |
| 76 observer().DidFailProvisionalLoad( |
| 77 1, true, url, net::ERR_ABORTED, string16()); |
| 78 } |
| 79 |
| 80 // Simulates an abort while loading an error page. |
| 81 void SimulateAbortTimeout(const GURL& url) { |
| 82 EXPECT_CALL(mock_helper(), OnLoadStart(url.SchemeIsSecure())).Times(1); |
| 83 observer().DidStartProvisionalLoadForFrame(1, true, url, false, NULL); |
| 84 |
| 85 observer().DidFailProvisionalLoad( |
| 86 1, true, url, net::ERR_TIMED_OUT, string16()); |
| 87 |
| 88 // Start event for the error page. |
| 89 observer().DidStartProvisionalLoadForFrame(1, true, url, true, NULL); |
| 90 |
| 91 EXPECT_CALL(mock_helper(), OnAbort()).Times(1); |
| 92 observer().DidFailProvisionalLoad( |
| 93 1, true, url, net::ERR_ABORTED, string16()); |
| 94 } |
| 95 |
| 96 CaptivePortalTabObserver& observer() { |
| 97 return mock_helper_.observer(); |
| 98 } |
| 99 |
| 100 MockCaptivePortalTabHelper& mock_helper() { return mock_helper_; } |
| 101 |
| 102 private: |
| 103 testing::StrictMock<MockCaptivePortalTabHelper> mock_helper_; |
| 104 DISALLOW_COPY_AND_ASSIGN(CaptivePortalTabHelperObserverTest); |
| 105 }; |
| 106 |
| 107 TEST_F(CaptivePortalTabHelperObserverTest, HttpSuccess) { |
| 108 SimulateSuccess(GURL(kHttpUrl)); |
| 109 |
| 110 EXPECT_CALL(mock_helper(), OnStopLoading()).Times(1); |
| 111 observer().DidStopLoading(); |
| 112 } |
| 113 |
| 114 TEST_F(CaptivePortalTabHelperObserverTest, HttpTimeout) { |
| 115 SimulateTimeout(GURL(kHttpUrl)); |
| 116 |
| 117 EXPECT_CALL(mock_helper(), OnStopLoading()).Times(1); |
| 118 observer().DidStopLoading(); |
| 119 } |
| 120 |
| 121 TEST_F(CaptivePortalTabHelperObserverTest, HttpsSuccess) { |
| 122 SimulateSuccess(GURL(kHttpsUrl)); |
| 123 } |
| 124 |
| 125 TEST_F(CaptivePortalTabHelperObserverTest, HttpsTimeout) { |
| 126 SimulateTimeout(GURL(kHttpsUrl)); |
| 127 // Make sure no state was carried over from the timeout. |
| 128 SimulateSuccess(GURL(kHttpsUrl)); |
| 129 } |
| 130 |
| 131 TEST_F(CaptivePortalTabHelperObserverTest, HttpsAbort) { |
| 132 SimulateAbort(GURL(kHttpsUrl)); |
| 133 // Make sure no state was carried over from the abort. |
| 134 SimulateSuccess(GURL(kHttpsUrl)); |
| 135 } |
| 136 |
| 137 // Abort while there's a provisional timeout error page loading. |
| 138 TEST_F(CaptivePortalTabHelperObserverTest, HttpsAbortTimeout) { |
| 139 SimulateAbortTimeout(GURL(kHttpsUrl)); |
| 140 // Make sure no state was carried over from the timeout or the abort. |
| 141 SimulateSuccess(GURL(kHttpsUrl)); |
| 142 } |
| 143 |
| 144 // Simulates navigations for a number of subframes, and makes sure no |
| 145 // CaptivePortalTabHelper function is called. |
| 146 TEST_F(CaptivePortalTabHelperObserverTest, HttpsSubframe) { |
| 147 GURL url = GURL(kHttpsUrl); |
| 148 // Normal load. |
| 149 observer().DidStartProvisionalLoadForFrame(1, false, url, false, NULL); |
| 150 observer().DidCommitProvisionalLoadForFrame( |
| 151 1, false, url, content::PAGE_TRANSITION_LINK); |
| 152 |
| 153 // Timeout. |
| 154 observer().DidStartProvisionalLoadForFrame(2, false, url, false, NULL); |
| 155 observer().DidFailProvisionalLoad( |
| 156 2, false, url, net::ERR_TIMED_OUT, string16()); |
| 157 observer().DidStartProvisionalLoadForFrame(2, false, url, true, NULL); |
| 158 observer().DidFailProvisionalLoad( |
| 159 2, false, url, net::ERR_ABORTED, string16()); |
| 160 |
| 161 // Abort. |
| 162 observer().DidStartProvisionalLoadForFrame(3, false, url, false, NULL); |
| 163 observer().DidFailProvisionalLoad( |
| 164 3, false, url, net::ERR_ABORTED, string16()); |
| 165 } |
| 166 |
| 167 // Simulates a subframe erroring out at the same time as a provisional load, |
| 168 // but with a different error code. Make sure the TabHelper sees the correct |
| 169 // error. |
| 170 TEST_F(CaptivePortalTabHelperObserverTest, HttpsSubframeParallelError) { |
| 171 // URL used by both frames. |
| 172 GURL url = GURL(kHttpsUrl); |
| 173 |
| 174 int frame_id = 2; |
| 175 int subframe_id = 1; |
| 176 |
| 177 // Loads start. |
| 178 EXPECT_CALL(mock_helper(), OnLoadStart(url.SchemeIsSecure())).Times(1); |
| 179 observer().DidStartProvisionalLoadForFrame( |
| 180 frame_id, true, url, false, NULL); |
| 181 observer().DidStartProvisionalLoadForFrame( |
| 182 subframe_id, false, url, false, NULL); |
| 183 |
| 184 // Loads return errors. |
| 185 observer().DidFailProvisionalLoad( |
| 186 frame_id, true, url, net::ERR_UNEXPECTED, string16()); |
| 187 observer().DidFailProvisionalLoad( |
| 188 subframe_id, false, url, net::ERR_TIMED_OUT, string16()); |
| 189 |
| 190 // Provisional load starts for the error pages. |
| 191 observer().DidStartProvisionalLoadForFrame( |
| 192 frame_id, true, url, true, NULL); |
| 193 observer().DidStartProvisionalLoadForFrame( |
| 194 subframe_id, false, url, true, NULL); |
| 195 |
| 196 // Error page load finishes. |
| 197 EXPECT_CALL(mock_helper(), OnLoadCommitted(net::ERR_UNEXPECTED)).Times(1); |
| 198 observer().DidCommitProvisionalLoadForFrame( |
| 199 frame_id, true, url, content::PAGE_TRANSITION_LINK); |
| 200 observer().DidStartProvisionalLoadForFrame( |
| 201 subframe_id, false, url, true, NULL); |
| 202 } |
| 203 |
| 204 } // namespace captive_portal |
OLD | NEW |