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

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

Powered by Google App Engine
This is Rietveld 408576698