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

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: incorporated feedback 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
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 NOTREACHED();
71 }
72 virtual void Resume() {
73 DCHECK(status_ == UNKNOWN);
74 status_ = RESUMED;
75 ContinueTestCase();
76 }
77 virtual void CancelWithHandledExternallyStatus() {
78 DCHECK(status_ == UNKNOWN);
79 status_ = CANCELLED;
80 ContinueTestCase();
81 }
82
83 private:
84 Status status_;
85 };
86
87 // TestIOThreadState ----------------------------------------------------------
88
89 class TestIOThreadState {
90 public:
91 TestIOThreadState(int render_process_id, int render_view_id)
92 : request_(GURL(kTestUrl), NULL),
93 throttle_(&request_) {
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
95 if (render_process_id != MSG_ROUTING_NONE &&
96 render_view_id != MSG_ROUTING_NONE) {
97 ResourceRequestInfo::AllocateForTesting(&request_,
98 ResourceType::MAIN_FRAME,
99 &resource_context_,
100 render_process_id,
101 render_view_id);
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 InterceptNavigationResourceThrottle throttle_;
125 MockResourceThrottleController throttle_controller_;
126 };
127
128 // InterceptNavigationResourceThrottleTest ------------------------------------
129
130 class InterceptNavigationResourceThrottleTest
131 : public ChromeRenderViewHostTestHarness {
132 public:
133 InterceptNavigationResourceThrottleTest()
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 &InterceptNavigationResourceThrottleTest::
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(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 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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698