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_throttle_controller.h" |
| 18 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/browser/web_contents_delegate.h" |
| 20 #include "content/test/mock_resource_context.h" |
| 21 #include "content/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 |
| 33 void ContinueTestCase() { |
| 34 BrowserThread::PostTask( |
| 35 BrowserThread::UI, |
| 36 FROM_HERE, |
| 37 MessageLoop::QuitClosure()); |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 42 // MockWebContentsDelegate ---------------------------------------------------- |
| 43 |
| 44 class MockWebContentsDelegate : public content::WebContentsDelegate { |
| 45 public: |
| 46 MOCK_METHOD4(ShouldIgnoreNavigation, bool(WebContents* source, |
| 47 const GURL& url, |
| 48 const content::Referrer& referrer, |
| 49 bool is_content_initiated)); |
| 50 }; |
| 51 |
| 52 // MockResourceThrottleController |
| 53 class MockResourceThrottleController |
| 54 : public content::ResourceThrottleController { |
| 55 public: |
| 56 enum Status { |
| 57 UNKNOWN, |
| 58 RESUMED, |
| 59 CANCELLED |
| 60 }; |
| 61 |
| 62 MockResourceThrottleController() |
| 63 : status_(UNKNOWN) { |
| 64 } |
| 65 |
| 66 Status status() const { return status_; } |
| 67 |
| 68 // content::ResourceThrottleController |
| 69 virtual void Cancel() { |
| 70 DCHECK(status_ == UNKNOWN); |
| 71 status_ = CANCELLED; |
| 72 ContinueTestCase(); |
| 73 } |
| 74 virtual void Resume() { |
| 75 DCHECK(status_ == UNKNOWN); |
| 76 status_ = RESUMED; |
| 77 ContinueTestCase(); |
| 78 } |
| 79 |
| 80 private: |
| 81 Status status_; |
| 82 }; |
| 83 |
| 84 // TestIOThreadState ---------------------------------------------------------- |
| 85 |
| 86 class TestIOThreadState { |
| 87 public: |
| 88 TestIOThreadState(int render_process_id, int render_view_id) |
| 89 : request_(GURL(kTestUrl), NULL), |
| 90 throttle_(&request_) { |
| 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 92 if (render_process_id != MSG_ROUTING_NONE && |
| 93 render_view_id != MSG_ROUTING_NONE) { |
| 94 ResourceRequestInfo::AllocateForTesting(&request_, |
| 95 ResourceType::MAIN_FRAME, |
| 96 &resource_context_, |
| 97 render_process_id, |
| 98 render_view_id); |
| 99 } |
| 100 throttle_.set_controller_for_testing(&throttle_controller_); |
| 101 } |
| 102 |
| 103 void ThrottleWillStartRequest(bool* defer) { |
| 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 105 throttle_.WillStartRequest(defer); |
| 106 } |
| 107 |
| 108 bool request_resumed() const { |
| 109 return throttle_controller_.status() == |
| 110 MockResourceThrottleController::RESUMED; |
| 111 } |
| 112 |
| 113 bool request_cancelled() const { |
| 114 return throttle_controller_.status() == |
| 115 MockResourceThrottleController::CANCELLED; |
| 116 } |
| 117 |
| 118 private: |
| 119 content::MockResourceContext resource_context_; |
| 120 net::URLRequest request_; |
| 121 InterceptNavigationResourceThrottle throttle_; |
| 122 MockResourceThrottleController throttle_controller_; |
| 123 }; |
| 124 |
| 125 // InterceptNavigationResourceThrottleTest ------------------------------------ |
| 126 |
| 127 class InterceptNavigationResourceThrottleTest |
| 128 : public ChromeRenderViewHostTestHarness { |
| 129 public: |
| 130 InterceptNavigationResourceThrottleTest() |
| 131 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 132 io_thread_(BrowserThread::IO), |
| 133 io_thread_state_(NULL) { |
| 134 } |
| 135 |
| 136 virtual void SetUp() OVERRIDE { |
| 137 ChromeRenderViewHostTestHarness::SetUp(); |
| 138 |
| 139 io_thread_.StartIOThread(); |
| 140 } |
| 141 |
| 142 virtual void TearDown() OVERRIDE { |
| 143 web_contents()->SetDelegate(NULL); |
| 144 BrowserThread::PostTask( |
| 145 BrowserThread::IO, |
| 146 FROM_HERE, |
| 147 base::Bind(&base::DeletePointer<TestIOThreadState>, io_thread_state_)); |
| 148 |
| 149 ChromeRenderViewHostTestHarness::TearDown(); |
| 150 } |
| 151 |
| 152 void SetIOThreadState(TestIOThreadState* io_thread_state) { |
| 153 io_thread_state_ = io_thread_state; |
| 154 } |
| 155 |
| 156 void RunThrottleWillStartRequestOnIOThread( |
| 157 int render_process_id, |
| 158 int render_view_id, |
| 159 bool* defer) { |
| 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 161 TestIOThreadState* io_thread_state = |
| 162 new TestIOThreadState(render_process_id, render_view_id); |
| 163 |
| 164 SetIOThreadState(io_thread_state); |
| 165 io_thread_state->ThrottleWillStartRequest(defer); |
| 166 |
| 167 if (!*defer) { |
| 168 ContinueTestCase(); |
| 169 } |
| 170 } |
| 171 |
| 172 protected: |
| 173 enum ShouldIgnoreNavigationCallbackAction { |
| 174 IgnoreNavigation, |
| 175 DontIgnoreNavigation |
| 176 }; |
| 177 |
| 178 void SetUpWebContentsDelegateAndRunMessageLoop( |
| 179 ShouldIgnoreNavigationCallbackAction callback_action, |
| 180 bool* defer) { |
| 181 scoped_ptr<MockWebContentsDelegate> web_contents_delegate( |
| 182 new MockWebContentsDelegate()); |
| 183 EXPECT_FALSE(web_contents()->GetDelegate()); |
| 184 web_contents()->SetDelegate(web_contents_delegate.get()); |
| 185 |
| 186 ON_CALL(*web_contents_delegate, |
| 187 ShouldIgnoreNavigation(_, Eq(GURL(kTestUrl)), _, _)) |
| 188 .WillByDefault(Return(callback_action == IgnoreNavigation)); |
| 189 EXPECT_CALL(*web_contents_delegate, |
| 190 ShouldIgnoreNavigation(_, Eq(GURL(kTestUrl)), _, _)) |
| 191 .Times(1); |
| 192 |
| 193 BrowserThread::PostTask( |
| 194 BrowserThread::IO, |
| 195 FROM_HERE, |
| 196 base::Bind( |
| 197 &InterceptNavigationResourceThrottleTest:: |
| 198 RunThrottleWillStartRequestOnIOThread, |
| 199 base::Unretained(this), |
| 200 contents()->GetRenderViewHost()->GetProcess()->GetID(), |
| 201 contents()->GetRenderViewHost()->GetRoutingID(), |
| 202 base::Unretained(defer))); |
| 203 |
| 204 // Wait for the request to finish processing. |
| 205 message_loop_.Run(); |
| 206 } |
| 207 |
| 208 content::TestBrowserThread ui_thread_; |
| 209 content::TestBrowserThread io_thread_; |
| 210 TestIOThreadState* io_thread_state_; |
| 211 }; |
| 212 |
| 213 TEST_F(InterceptNavigationResourceThrottleTest, |
| 214 RequestDeferredAndResumedIfNavigationNotIgnored) { |
| 215 bool defer = false; |
| 216 SetUpWebContentsDelegateAndRunMessageLoop(DontIgnoreNavigation, &defer); |
| 217 |
| 218 EXPECT_TRUE(defer); |
| 219 EXPECT_TRUE(io_thread_state_); |
| 220 EXPECT_TRUE(io_thread_state_->request_resumed()); |
| 221 } |
| 222 |
| 223 TEST_F(InterceptNavigationResourceThrottleTest, |
| 224 RequestDeferredAndCancelledIfNavigationIgnored) { |
| 225 bool defer = false; |
| 226 SetUpWebContentsDelegateAndRunMessageLoop(IgnoreNavigation, &defer); |
| 227 |
| 228 EXPECT_TRUE(defer); |
| 229 EXPECT_TRUE(io_thread_state_); |
| 230 EXPECT_TRUE(io_thread_state_->request_cancelled()); |
| 231 } |
| 232 |
| 233 TEST_F(InterceptNavigationResourceThrottleTest, |
| 234 RequestNotDeferredForRequestNotAssociatedWithARenderView) { |
| 235 bool defer = false; |
| 236 |
| 237 BrowserThread::PostTask( |
| 238 BrowserThread::IO, |
| 239 FROM_HERE, |
| 240 base::Bind( |
| 241 &InterceptNavigationResourceThrottleTest:: |
| 242 RunThrottleWillStartRequestOnIOThread, |
| 243 base::Unretained(this), |
| 244 MSG_ROUTING_NONE, |
| 245 MSG_ROUTING_NONE, |
| 246 base::Unretained(&defer))); |
| 247 |
| 248 // Wait for the request to finish processing. |
| 249 message_loop_.Run(); |
| 250 |
| 251 EXPECT_FALSE(defer); |
| 252 } |
OLD | NEW |