Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: chrome/browser/renderer_host/intercept_navigation_resource_throttle_unittest.cc

Issue 10946008: Componentize IgnoreNavigationResourceThrottle and add chrome and webview specific implementations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
benm (inactive) 2012/09/19 11:04:04 did we lose this test?
mkosiba (inactive) 2012/09/25 18:01:47 it should moved under chrome/browser/component/, n
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 NOTREACHED();
72 }
73 virtual void CancelAndIgnore() {
74 status_ = CANCELLED;
75 ContinueTestCase();
76 }
77 virtual void Resume() {
78 DCHECK(status_ == UNKNOWN);
79 status_ = RESUMED;
80 ContinueTestCase();
81 }
82
83 private:
84 Status status_;
85 };
86
87 // TestIOThreadState ----------------------------------------------------------
88
89 class TestIOThreadState {
90 public:
91 TestIOThreadState(const GURL& url, int render_process_id, int render_view_id,
92 MockInterceptCallbackReceiver* callback_receiver)
93 : request_(url, NULL, resource_context_.GetRequestContext()),
94 throttle_(NULL) {
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
96 if (render_process_id != MSG_ROUTING_NONE &&
97 render_view_id != MSG_ROUTING_NONE) {
98 ResourceRequestInfo::AllocateForTesting(&request_,
99 ResourceType::MAIN_FRAME,
100 &resource_context_,
101 render_process_id,
102 render_view_id);
103 }
104 throttle_.reset(new InterceptNavigationResourceThrottle(
105 &request_,
106 base::Bind(&MockInterceptCallbackReceiver::ShouldIgnoreNavigation,
107 base::Unretained(callback_receiver))));
108 throttle_->set_controller_for_testing(&throttle_controller_);
109 }
110
111 void ThrottleWillStartRequest(bool* defer) {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
113 throttle_->WillStartRequest(defer);
114 }
115
116 bool request_resumed() const {
117 return throttle_controller_.status() ==
118 MockResourceController::RESUMED;
119 }
120
121 bool request_cancelled() const {
122 return throttle_controller_.status() ==
123 MockResourceController::CANCELLED;
124 }
125
126 private:
127 content::MockResourceContext resource_context_;
128 net::URLRequest request_;
129 scoped_ptr<InterceptNavigationResourceThrottle> throttle_;
130 MockResourceController throttle_controller_;
131 };
132
133 // InterceptNavigationResourceThrottleTest ------------------------------------
134
135 class InterceptNavigationResourceThrottleTest
136 : public ChromeRenderViewHostTestHarness {
137 public:
138 InterceptNavigationResourceThrottleTest()
139 : mock_callback_receiver_(new MockInterceptCallbackReceiver()),
140 ui_thread_(BrowserThread::UI, &message_loop_),
141 io_thread_(BrowserThread::IO),
142 io_thread_state_(NULL) {
143 }
144
145 virtual void SetUp() OVERRIDE {
146 ChromeRenderViewHostTestHarness::SetUp();
147
148 io_thread_.StartIOThread();
149 }
150
151 virtual void TearDown() OVERRIDE {
152 web_contents()->SetDelegate(NULL);
153 BrowserThread::PostTask(
154 BrowserThread::IO,
155 FROM_HERE,
156 base::Bind(&base::DeletePointer<TestIOThreadState>, io_thread_state_));
157
158 RenderViewHostTestHarness::TearDown();
159 }
160
161 void SetIOThreadState(TestIOThreadState* io_thread_state) {
162 io_thread_state_ = io_thread_state;
163 }
164
165 void RunThrottleWillStartRequestOnIOThread(
166 const GURL& url,
167 int render_process_id,
168 int render_view_id,
169 bool* defer) {
170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
171 TestIOThreadState* io_thread_state =
172 new TestIOThreadState(url, render_process_id, render_view_id,
173 mock_callback_receiver_.get());
174
175 SetIOThreadState(io_thread_state);
176 io_thread_state->ThrottleWillStartRequest(defer);
177
178 if (!*defer) {
179 ContinueTestCase();
180 }
181 }
182
183 protected:
184 enum ShouldIgnoreNavigationCallbackAction {
185 IgnoreNavigation,
186 DontIgnoreNavigation
187 };
188
189 void SetUpWebContentsDelegateAndRunMessageLoop(
190 ShouldIgnoreNavigationCallbackAction callback_action,
191 bool* defer) {
192
193 ON_CALL(*mock_callback_receiver_,
194 ShouldIgnoreNavigation(_, _, _, _))
195 .WillByDefault(Return(callback_action == IgnoreNavigation));
196 EXPECT_CALL(*mock_callback_receiver_,
197 ShouldIgnoreNavigation(rvh(), Eq(GURL(kTestUrl)), _, _))
198 .Times(1);
199
200 BrowserThread::PostTask(
201 BrowserThread::IO,
202 FROM_HERE,
203 base::Bind(
204 &InterceptNavigationResourceThrottleTest::
205 RunThrottleWillStartRequestOnIOThread,
206 base::Unretained(this),
207 GURL(kTestUrl),
208 web_contents()->GetRenderViewHost()->GetProcess()->GetID(),
209 web_contents()->GetRenderViewHost()->GetRoutingID(),
210 base::Unretained(defer)));
211
212 // Wait for the request to finish processing.
213 message_loop_.Run();
214 }
215
216 scoped_ptr<MockInterceptCallbackReceiver> mock_callback_receiver_;
217 content::TestBrowserThread ui_thread_;
218 content::TestBrowserThread io_thread_;
219 TestIOThreadState* io_thread_state_;
220 };
221
222 TEST_F(InterceptNavigationResourceThrottleTest,
223 RequestDeferredAndResumedIfNavigationNotIgnored) {
224 bool defer = false;
225 SetUpWebContentsDelegateAndRunMessageLoop(DontIgnoreNavigation, &defer);
226
227 EXPECT_TRUE(defer);
228 EXPECT_TRUE(io_thread_state_);
229 EXPECT_TRUE(io_thread_state_->request_resumed());
230 }
231
232 TEST_F(InterceptNavigationResourceThrottleTest,
233 RequestDeferredAndCancelledIfNavigationIgnored) {
234 bool defer = false;
235 SetUpWebContentsDelegateAndRunMessageLoop(IgnoreNavigation, &defer);
236
237 EXPECT_TRUE(defer);
238 EXPECT_TRUE(io_thread_state_);
239 EXPECT_TRUE(io_thread_state_->request_cancelled());
240 }
241
242 TEST_F(InterceptNavigationResourceThrottleTest,
243 RequestNotDeferredForRequestNotAssociatedWithARenderView) {
244 bool defer = false;
245
246 BrowserThread::PostTask(
247 BrowserThread::IO,
248 FROM_HERE,
249 base::Bind(
250 &InterceptNavigationResourceThrottleTest::
251 RunThrottleWillStartRequestOnIOThread,
252 base::Unretained(this),
253 GURL(kTestUrl),
254 MSG_ROUTING_NONE,
255 MSG_ROUTING_NONE,
256 base::Unretained(&defer)));
257
258 // Wait for the request to finish processing.
259 message_loop_.Run();
260
261 EXPECT_FALSE(defer);
262 }
263
264 TEST_F(InterceptNavigationResourceThrottleTest,
265 CallbackCalledWithFilteredUrl) {
266 bool defer = false;
267
268 ON_CALL(*mock_callback_receiver_,
269 ShouldIgnoreNavigation(_, Ne(GURL(kUnsafeTestUrl)), _, _))
270 .WillByDefault(Return(false));
271 EXPECT_CALL(*mock_callback_receiver_,
272 ShouldIgnoreNavigation(_, Ne(GURL(kUnsafeTestUrl)), _, _))
273 .Times(1);
274
275 BrowserThread::PostTask(
276 BrowserThread::IO,
277 FROM_HERE,
278 base::Bind(
279 &InterceptNavigationResourceThrottleTest::
280 RunThrottleWillStartRequestOnIOThread,
281 base::Unretained(this),
282 GURL(kUnsafeTestUrl),
283 web_contents()->GetRenderViewHost()->GetProcess()->GetID(),
284 web_contents()->GetRenderViewHost()->GetRoutingID(),
285 base::Unretained(&defer)));
286
287 // Wait for the request to finish processing.
288 message_loop_.Run();
289 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698