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/ignore_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 "content/test/net/url_request_user_data_helper.h" |
| 23 #include "net/url_request/url_request.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 |
| 27 using namespace content; |
| 28 using namespace ::testing; |
| 29 |
| 30 namespace { |
| 31 |
| 32 const char* kTestUrl = "http://www.test.com/"; |
| 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(int render_process_id, int render_view_id) |
| 90 : request_(GURL(kTestUrl), NULL), |
| 91 throttle_(&request_) { |
| 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 93 ResourceRequestInfo::AllocateForTesting(&request_, |
| 94 ResourceType::MAIN_FRAME, |
| 95 &resource_context_); |
| 96 if (render_process_id != MSG_ROUTING_NONE && |
| 97 render_view_id != MSG_ROUTING_NONE) { |
| 98 URLRequestUserDataHelper::AssociateWithRenderView( |
| 99 &request_, render_process_id, render_view_id); |
| 100 } else { |
| 101 URLRequestUserDataHelper::DissasociateFromRenderView(&request_); |
| 102 } |
| 103 throttle_.set_controller_for_testing(&throttle_controller_); |
| 104 } |
| 105 |
| 106 void ThrottleWillStartRequest(bool* defer) { |
| 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 108 throttle_.WillStartRequest(defer); |
| 109 } |
| 110 |
| 111 bool request_resumed() const { |
| 112 return throttle_controller_.status() == |
| 113 MockResourceThrottleController::RESUMED; |
| 114 } |
| 115 |
| 116 bool request_cancelled() const { |
| 117 return throttle_controller_.status() == |
| 118 MockResourceThrottleController::CANCELLED; |
| 119 } |
| 120 |
| 121 private: |
| 122 content::MockResourceContext resource_context_; |
| 123 net::URLRequest request_; |
| 124 IgnoreNavigationResourceThrottle throttle_; |
| 125 MockResourceThrottleController throttle_controller_; |
| 126 }; |
| 127 |
| 128 // IgnoreNavigationResourceThrottleTest --------------------------------------- |
| 129 |
| 130 class IgnoreNavigationResourceThrottleTest |
| 131 : public ChromeRenderViewHostTestHarness { |
| 132 public: |
| 133 IgnoreNavigationResourceThrottleTest() |
| 134 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 135 io_thread_(BrowserThread::IO), |
| 136 io_thread_state_(NULL) { |
| 137 } |
| 138 |
| 139 virtual void SetUp() OVERRIDE { |
| 140 ChromeRenderViewHostTestHarness::SetUp(); |
| 141 |
| 142 io_thread_.StartIOThread(); |
| 143 } |
| 144 |
| 145 virtual void TearDown() OVERRIDE { |
| 146 web_contents()->SetDelegate(NULL); |
| 147 BrowserThread::PostTask( |
| 148 BrowserThread::IO, |
| 149 FROM_HERE, |
| 150 base::Bind(&base::DeletePointer<TestIOThreadState>, io_thread_state_)); |
| 151 |
| 152 ChromeRenderViewHostTestHarness::TearDown(); |
| 153 } |
| 154 |
| 155 void SetIOThreadState(TestIOThreadState* io_thread_state) { |
| 156 io_thread_state_ = io_thread_state; |
| 157 } |
| 158 |
| 159 void RunThrottleWillStartRequestOnIOThread( |
| 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(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 &IgnoreNavigationResourceThrottleTest:: |
| 201 RunThrottleWillStartRequestOnIOThread, |
| 202 base::Unretained(this), |
| 203 contents()->GetRenderViewHost()->GetProcess()->GetID(), |
| 204 contents()->GetRenderViewHost()->GetRoutingID(), |
| 205 base::Unretained(defer))); |
| 206 |
| 207 // Wait for the request to finish processing. |
| 208 message_loop_.Run(); |
| 209 } |
| 210 |
| 211 content::TestBrowserThread ui_thread_; |
| 212 content::TestBrowserThread io_thread_; |
| 213 TestIOThreadState* io_thread_state_; |
| 214 }; |
| 215 |
| 216 TEST_F(IgnoreNavigationResourceThrottleTest, |
| 217 RequestDeferredAndResumedIfNavigationNotIgnored) { |
| 218 bool defer = false; |
| 219 SetUpWebContentsDelegateAndRunMessageLoop(DontIgnoreNavigation, &defer); |
| 220 |
| 221 EXPECT_TRUE(defer); |
| 222 EXPECT_TRUE(io_thread_state_); |
| 223 EXPECT_TRUE(io_thread_state_->request_resumed()); |
| 224 } |
| 225 |
| 226 TEST_F(IgnoreNavigationResourceThrottleTest, |
| 227 RequestDeferredAndCancelledIfNavigationIgnored) { |
| 228 bool defer = false; |
| 229 SetUpWebContentsDelegateAndRunMessageLoop(IgnoreNavigation, &defer); |
| 230 |
| 231 EXPECT_TRUE(defer); |
| 232 EXPECT_TRUE(io_thread_state_); |
| 233 EXPECT_TRUE(io_thread_state_->request_cancelled()); |
| 234 } |
| 235 |
| 236 TEST_F(IgnoreNavigationResourceThrottleTest, |
| 237 RequestNotDeferredForRequestNotAssociatedWithARenderView) { |
| 238 bool defer = false; |
| 239 |
| 240 BrowserThread::PostTask( |
| 241 BrowserThread::IO, |
| 242 FROM_HERE, |
| 243 base::Bind( |
| 244 &IgnoreNavigationResourceThrottleTest:: |
| 245 RunThrottleWillStartRequestOnIOThread, |
| 246 base::Unretained(this), |
| 247 MSG_ROUTING_NONE, |
| 248 MSG_ROUTING_NONE, |
| 249 base::Unretained(&defer))); |
| 250 |
| 251 // Wait for the request to finish processing. |
| 252 message_loop_.Run(); |
| 253 |
| 254 EXPECT_FALSE(defer); |
| 255 } |
OLD | NEW |