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 "base/bind.h" |
| 6 #include "base/bind_helpers.h" |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/memory/scoped_vector.h" |
| 9 #include "chrome/browser/renderer_host/intercept_navigation_resource_throttle.h" |
| 10 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "content/public/browser/resource_context.h" |
| 13 #include "content/public/browser/resource_dispatcher_host.h" |
| 14 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 15 #include "content/public/browser/resource_request_info.h" |
| 16 #include "content/public/browser/resource_throttle.h" |
| 17 #include "content/public/browser/resource_controller.h" |
| 18 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/browser/web_contents_delegate.h" |
| 20 #include "content/public/test/mock_resource_context.h" |
| 21 #include "content/public/test/test_browser_thread.h" |
| 22 #include "net/url_request/url_request.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 |
| 26 using namespace content; |
| 27 using namespace ::testing; |
| 28 |
| 29 namespace { |
| 30 |
| 31 const char* kTestUrl = "http://www.test.com/"; |
| 32 const char* kUnsafeTestUrl = "about:crash"; |
| 33 |
| 34 void ContinueTestCase() { |
| 35 BrowserThread::PostTask( |
| 36 BrowserThread::UI, |
| 37 FROM_HERE, |
| 38 MessageLoop::QuitClosure()); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 // MockInterceptCallbackReceiver ---------------------------------------------- |
| 44 |
| 45 class MockInterceptCallbackReceiver { |
| 46 public: |
| 47 MOCK_METHOD4(ShouldIgnoreNavigation, bool(RenderViewHost* source, |
| 48 const GURL& url, |
| 49 const content::Referrer& referrer, |
| 50 bool is_content_initiated)); |
| 51 }; |
| 52 |
| 53 // MockResourceController ----------------------------------------------------- |
| 54 class MockResourceController |
| 55 : public content::ResourceController { |
| 56 public: |
| 57 enum Status { |
| 58 UNKNOWN, |
| 59 RESUMED, |
| 60 CANCELLED |
| 61 }; |
| 62 |
| 63 MockResourceController() |
| 64 : status_(UNKNOWN) { |
| 65 } |
| 66 |
| 67 Status status() const { return status_; } |
| 68 |
| 69 // content::ResourceController |
| 70 virtual void Cancel() { |
| 71 DCHECK(status_ == UNKNOWN); |
| 72 status_ = CANCELLED; |
| 73 ContinueTestCase(); |
| 74 } |
| 75 virtual void Resume() { |
| 76 DCHECK(status_ == UNKNOWN); |
| 77 status_ = RESUMED; |
| 78 ContinueTestCase(); |
| 79 } |
| 80 |
| 81 private: |
| 82 Status status_; |
| 83 }; |
| 84 |
| 85 // TestIOThreadState ---------------------------------------------------------- |
| 86 |
| 87 class TestIOThreadState { |
| 88 public: |
| 89 TestIOThreadState(const GURL& url, int render_process_id, int render_view_id, |
| 90 MockInterceptCallbackReceiver* callback_receiver) |
| 91 : request_(url, NULL), |
| 92 throttle_(NULL) { |
| 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 94 if (render_process_id != MSG_ROUTING_NONE && |
| 95 render_view_id != MSG_ROUTING_NONE) { |
| 96 ResourceRequestInfo::AllocateForTesting(&request_, |
| 97 ResourceType::MAIN_FRAME, |
| 98 &resource_context_, |
| 99 render_process_id, |
| 100 render_view_id); |
| 101 } |
| 102 throttle_.reset(new InterceptNavigationResourceThrottle( |
| 103 &request_, |
| 104 base::Bind(&MockInterceptCallbackReceiver::ShouldIgnoreNavigation, |
| 105 base::Unretained(callback_receiver)))); |
| 106 throttle_->set_controller_for_testing(&throttle_controller_); |
| 107 } |
| 108 |
| 109 void ThrottleWillStartRequest(bool* defer) { |
| 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 111 throttle_->WillStartRequest(defer); |
| 112 } |
| 113 |
| 114 bool request_resumed() const { |
| 115 return throttle_controller_.status() == |
| 116 MockResourceController::RESUMED; |
| 117 } |
| 118 |
| 119 bool request_cancelled() const { |
| 120 return throttle_controller_.status() == |
| 121 MockResourceController::CANCELLED; |
| 122 } |
| 123 |
| 124 private: |
| 125 content::MockResourceContext resource_context_; |
| 126 net::URLRequest request_; |
| 127 scoped_ptr<InterceptNavigationResourceThrottle> throttle_; |
| 128 MockResourceController throttle_controller_; |
| 129 }; |
| 130 |
| 131 // InterceptNavigationResourceThrottleTest ------------------------------------ |
| 132 |
| 133 class InterceptNavigationResourceThrottleTest |
| 134 : public ChromeRenderViewHostTestHarness { |
| 135 public: |
| 136 InterceptNavigationResourceThrottleTest() |
| 137 : mock_callback_receiver_(new MockInterceptCallbackReceiver()), |
| 138 ui_thread_(BrowserThread::UI, &message_loop_), |
| 139 io_thread_(BrowserThread::IO), |
| 140 io_thread_state_(NULL) { |
| 141 } |
| 142 |
| 143 virtual void SetUp() OVERRIDE { |
| 144 ChromeRenderViewHostTestHarness::SetUp(); |
| 145 |
| 146 io_thread_.StartIOThread(); |
| 147 } |
| 148 |
| 149 virtual void TearDown() OVERRIDE { |
| 150 web_contents()->SetDelegate(NULL); |
| 151 BrowserThread::PostTask( |
| 152 BrowserThread::IO, |
| 153 FROM_HERE, |
| 154 base::Bind(&base::DeletePointer<TestIOThreadState>, io_thread_state_)); |
| 155 |
| 156 RenderViewHostTestHarness::TearDown(); |
| 157 } |
| 158 |
| 159 void SetIOThreadState(TestIOThreadState* io_thread_state) { |
| 160 io_thread_state_ = io_thread_state; |
| 161 } |
| 162 |
| 163 void RunThrottleWillStartRequestOnIOThread( |
| 164 const GURL& url, |
| 165 int render_process_id, |
| 166 int render_view_id, |
| 167 bool* defer) { |
| 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 169 TestIOThreadState* io_thread_state = |
| 170 new TestIOThreadState(url, render_process_id, render_view_id, |
| 171 mock_callback_receiver_.get()); |
| 172 |
| 173 SetIOThreadState(io_thread_state); |
| 174 io_thread_state->ThrottleWillStartRequest(defer); |
| 175 |
| 176 if (!*defer) { |
| 177 ContinueTestCase(); |
| 178 } |
| 179 } |
| 180 |
| 181 protected: |
| 182 enum ShouldIgnoreNavigationCallbackAction { |
| 183 IgnoreNavigation, |
| 184 DontIgnoreNavigation |
| 185 }; |
| 186 |
| 187 void SetUpWebContentsDelegateAndRunMessageLoop( |
| 188 ShouldIgnoreNavigationCallbackAction callback_action, |
| 189 bool* defer) { |
| 190 |
| 191 ON_CALL(*mock_callback_receiver_, |
| 192 ShouldIgnoreNavigation(_, _, _, _)) |
| 193 .WillByDefault(Return(callback_action == IgnoreNavigation)); |
| 194 EXPECT_CALL(*mock_callback_receiver_, |
| 195 ShouldIgnoreNavigation(rvh(), Eq(GURL(kTestUrl)), _, _)) |
| 196 .Times(1); |
| 197 |
| 198 BrowserThread::PostTask( |
| 199 BrowserThread::IO, |
| 200 FROM_HERE, |
| 201 base::Bind( |
| 202 &InterceptNavigationResourceThrottleTest:: |
| 203 RunThrottleWillStartRequestOnIOThread, |
| 204 base::Unretained(this), |
| 205 GURL(kTestUrl), |
| 206 web_contents()->GetRenderViewHost()->GetProcess()->GetID(), |
| 207 web_contents()->GetRenderViewHost()->GetRoutingID(), |
| 208 base::Unretained(defer))); |
| 209 |
| 210 // Wait for the request to finish processing. |
| 211 message_loop_.Run(); |
| 212 } |
| 213 |
| 214 scoped_ptr<MockInterceptCallbackReceiver> mock_callback_receiver_; |
| 215 content::TestBrowserThread ui_thread_; |
| 216 content::TestBrowserThread io_thread_; |
| 217 TestIOThreadState* io_thread_state_; |
| 218 }; |
| 219 |
| 220 TEST_F(InterceptNavigationResourceThrottleTest, |
| 221 RequestDeferredAndResumedIfNavigationNotIgnored) { |
| 222 bool defer = false; |
| 223 SetUpWebContentsDelegateAndRunMessageLoop(DontIgnoreNavigation, &defer); |
| 224 |
| 225 EXPECT_TRUE(defer); |
| 226 EXPECT_TRUE(io_thread_state_); |
| 227 EXPECT_TRUE(io_thread_state_->request_resumed()); |
| 228 } |
| 229 |
| 230 TEST_F(InterceptNavigationResourceThrottleTest, |
| 231 RequestDeferredAndCancelledIfNavigationIgnored) { |
| 232 bool defer = false; |
| 233 SetUpWebContentsDelegateAndRunMessageLoop(IgnoreNavigation, &defer); |
| 234 |
| 235 EXPECT_TRUE(defer); |
| 236 EXPECT_TRUE(io_thread_state_); |
| 237 EXPECT_TRUE(io_thread_state_->request_cancelled()); |
| 238 } |
| 239 |
| 240 TEST_F(InterceptNavigationResourceThrottleTest, |
| 241 RequestNotDeferredForRequestNotAssociatedWithARenderView) { |
| 242 bool defer = false; |
| 243 |
| 244 BrowserThread::PostTask( |
| 245 BrowserThread::IO, |
| 246 FROM_HERE, |
| 247 base::Bind( |
| 248 &InterceptNavigationResourceThrottleTest:: |
| 249 RunThrottleWillStartRequestOnIOThread, |
| 250 base::Unretained(this), |
| 251 GURL(kTestUrl), |
| 252 MSG_ROUTING_NONE, |
| 253 MSG_ROUTING_NONE, |
| 254 base::Unretained(&defer))); |
| 255 |
| 256 // Wait for the request to finish processing. |
| 257 message_loop_.Run(); |
| 258 |
| 259 EXPECT_FALSE(defer); |
| 260 } |
| 261 |
| 262 TEST_F(InterceptNavigationResourceThrottleTest, |
| 263 CallbackCalledWithFilteredUrl) { |
| 264 bool defer = false; |
| 265 |
| 266 ON_CALL(*mock_callback_receiver_, |
| 267 ShouldIgnoreNavigation(_, Ne(GURL(kUnsafeTestUrl)), _, _)) |
| 268 .WillByDefault(Return(false)); |
| 269 EXPECT_CALL(*mock_callback_receiver_, |
| 270 ShouldIgnoreNavigation(_, Ne(GURL(kUnsafeTestUrl)), _, _)) |
| 271 .Times(1); |
| 272 |
| 273 BrowserThread::PostTask( |
| 274 BrowserThread::IO, |
| 275 FROM_HERE, |
| 276 base::Bind( |
| 277 &InterceptNavigationResourceThrottleTest:: |
| 278 RunThrottleWillStartRequestOnIOThread, |
| 279 base::Unretained(this), |
| 280 GURL(kUnsafeTestUrl), |
| 281 web_contents()->GetRenderViewHost()->GetProcess()->GetID(), |
| 282 web_contents()->GetRenderViewHost()->GetRoutingID(), |
| 283 base::Unretained(&defer))); |
| 284 |
| 285 // Wait for the request to finish processing. |
| 286 message_loop_.Run(); |
| 287 } |
OLD | NEW |