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