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

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

Issue 10310124: Implement a ResourceThrottle for URL overriding in Chrome on Android. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: removed code from web_contents_impl Created 8 years, 6 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
OLDNEW
(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 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_(&request_) {
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_.set_controller_for_testing(&throttle_controller_);
102 }
103
104 void ThrottleWillStartRequest(bool* defer) {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
106 throttle_.WillStartRequest(defer);
107 }
108
109 bool request_resumed() const {
110 return throttle_controller_.status() ==
111 MockResourceThrottleController::RESUMED;
112 }
113
114 bool request_cancelled() const {
115 return throttle_controller_.status() ==
116 MockResourceThrottleController::CANCELLED;
117 }
118
119 private:
120 content::MockResourceContext resource_context_;
121 net::URLRequest request_;
122 InterceptNavigationResourceThrottle throttle_;
123 MockResourceThrottleController throttle_controller_;
124 };
125
126 // InterceptNavigationResourceThrottleTest ------------------------------------
127
128 class InterceptNavigationResourceThrottleTest
129 : public ChromeRenderViewHostTestHarness {
130 public:
131 InterceptNavigationResourceThrottleTest()
132 : ui_thread_(BrowserThread::UI, &message_loop_),
133 io_thread_(BrowserThread::IO),
134 io_thread_state_(NULL) {
135 }
136
137 virtual void SetUp() OVERRIDE {
138 ChromeRenderViewHostTestHarness::SetUp();
139
140 io_thread_.StartIOThread();
141 }
142
143 virtual void TearDown() OVERRIDE {
144 web_contents()->SetDelegate(NULL);
145 BrowserThread::PostTask(
146 BrowserThread::IO,
147 FROM_HERE,
148 base::Bind(&base::DeletePointer<TestIOThreadState>, io_thread_state_));
149
150 ChromeRenderViewHostTestHarness::TearDown();
151 }
152
153 void SetIOThreadState(TestIOThreadState* io_thread_state) {
154 io_thread_state_ = io_thread_state;
155 }
156
157 void RunThrottleWillStartRequestOnIOThread(
158 const GURL& url,
159 int render_process_id,
160 int render_view_id,
161 bool* defer) {
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
163 TestIOThreadState* io_thread_state =
164 new TestIOThreadState(url, render_process_id, render_view_id);
165
166 SetIOThreadState(io_thread_state);
167 io_thread_state->ThrottleWillStartRequest(defer);
168
169 if (!*defer) {
170 ContinueTestCase();
171 }
172 }
173
174 protected:
175 enum ShouldIgnoreNavigationCallbackAction {
176 IgnoreNavigation,
177 DontIgnoreNavigation
178 };
179
180 void SetUpWebContentsDelegateAndRunMessageLoop(
181 ShouldIgnoreNavigationCallbackAction callback_action,
182 bool* defer) {
183 scoped_ptr<MockWebContentsDelegate> web_contents_delegate(
184 new MockWebContentsDelegate());
185 EXPECT_FALSE(web_contents()->GetDelegate());
186 web_contents()->SetDelegate(web_contents_delegate.get());
187
188 ON_CALL(*web_contents_delegate,
189 ShouldIgnoreNavigation(_, Eq(GURL(kTestUrl)), _, _))
190 .WillByDefault(Return(callback_action == IgnoreNavigation));
191 EXPECT_CALL(*web_contents_delegate,
192 ShouldIgnoreNavigation(_, Eq(GURL(kTestUrl)), _, _))
193 .Times(1);
194
195 BrowserThread::PostTask(
196 BrowserThread::IO,
197 FROM_HERE,
198 base::Bind(
199 &InterceptNavigationResourceThrottleTest::
200 RunThrottleWillStartRequestOnIOThread,
201 base::Unretained(this),
202 GURL(kTestUrl),
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(InterceptNavigationResourceThrottleTest,
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(InterceptNavigationResourceThrottleTest,
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(InterceptNavigationResourceThrottleTest,
237 RequestNotDeferredForRequestNotAssociatedWithARenderView) {
238 bool defer = false;
239
240 BrowserThread::PostTask(
241 BrowserThread::IO,
242 FROM_HERE,
243 base::Bind(
244 &InterceptNavigationResourceThrottleTest::
245 RunThrottleWillStartRequestOnIOThread,
246 base::Unretained(this),
247 GURL(kTestUrl),
248 MSG_ROUTING_NONE,
249 MSG_ROUTING_NONE,
250 base::Unretained(&defer)));
251
252 // Wait for the request to finish processing.
253 message_loop_.Run();
254
255 EXPECT_FALSE(defer);
256 }
257
258 TEST_F(InterceptNavigationResourceThrottleTest,
259 DelegateMethodCalledWithFilteredUrl) {
260 bool defer = false;
261 scoped_ptr<MockWebContentsDelegate> web_contents_delegate(
262 new MockWebContentsDelegate());
263 EXPECT_FALSE(web_contents()->GetDelegate());
264 web_contents()->SetDelegate(web_contents_delegate.get());
265
266 ON_CALL(*web_contents_delegate,
267 ShouldIgnoreNavigation(_, Ne(GURL(kUnsafeTestUrl)), _, _))
268 .WillByDefault(Return(false));
269 EXPECT_CALL(*web_contents_delegate,
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 contents()->GetRenderViewHost()->GetProcess()->GetID(),
282 contents()->GetRenderViewHost()->GetRoutingID(),
283 base::Unretained(&defer)));
284
285 // Wait for the request to finish processing.
286 message_loop_.Run();
287 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698