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

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

Issue 460108: Implement ResourceQueue, an object that makes it easy to delay starting (Closed)
Patch Set: nitfixing Created 11 years 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) 2009 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/message_loop.h"
6 #include "chrome/browser/chrome_thread.h"
7 #include "chrome/browser/renderer_host/global_request_id.h"
8 #include "chrome/browser/renderer_host/resource_dispatcher_host_request_info.h"
9 #include "chrome/browser/renderer_host/resource_handler.h"
10 #include "chrome/browser/renderer_host/resource_queue.h"
11 #include "googleurl/src/gurl.h"
12 #include "net/url_request/url_request.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 const char kTestUrl[] = "data:text/plain,Hello World!";
18
19 class DummyResourceHandler : public ResourceHandler {
20 public:
21 DummyResourceHandler() {
22 }
23
24 virtual bool OnRequestRedirected(int request_id, const GURL& url,
25 ResourceResponse* response,
26 bool* defer) {
27 NOTREACHED();
28 return true;
29 }
30
31 virtual bool OnResponseStarted(int request_id,
32 ResourceResponse* response) {
33 NOTREACHED();
34 return true;
35 }
36
37 virtual bool OnWillRead(int request_id,
38 net::IOBuffer** buf,
39 int* buf_size,
40 int min_size) {
41 NOTREACHED();
42 return true;
43 }
44
45 virtual bool OnReadCompleted(int request_id, int* bytes_read) {
46 NOTREACHED();
47 return true;
48 }
49
50 virtual bool OnResponseCompleted(int request_id,
51 const URLRequestStatus& status,
52 const std::string& security_info) {
53 NOTREACHED();
54 return true;
55 }
56
57 private:
58 DISALLOW_COPY_AND_ASSIGN(DummyResourceHandler);
59 };
60
61 ResourceDispatcherHostRequestInfo* GetRequestInfo(int request_id) {
62 return new ResourceDispatcherHostRequestInfo(
63 new DummyResourceHandler(), ChildProcessInfo::RENDER_PROCESS, 0, 0,
64 request_id, "null", "null", ResourceType::MAIN_FRAME,
65 0, false, false, -1, -1);
66 }
67
68 void InitializeQueue(ResourceQueue* queue, ResourceQueueDelegate* delegate) {
69 ResourceQueue::DelegateSet delegate_set;
70 delegate_set.insert(delegate);
71 queue->Initialize(delegate_set);
72 }
73
74 void InitializeQueue(ResourceQueue* queue,
75 ResourceQueueDelegate* delegate1,
76 ResourceQueueDelegate* delegate2) {
77 ResourceQueue::DelegateSet delegate_set;
78 delegate_set.insert(delegate1);
79 delegate_set.insert(delegate2);
80 queue->Initialize(delegate_set);
81 }
82
83 class NeverDelayingDelegate : public ResourceQueueDelegate {
84 public:
85 NeverDelayingDelegate() {
86 }
87
88 virtual bool ShouldDelayRequest(
89 URLRequest* request,
90 const ResourceDispatcherHostRequestInfo& request_info,
91 const GlobalRequestID& request_id) {
92 return false;
93 }
94
95 virtual void WillShutdownResourceQueue() {
96 }
97
98 private:
99 DISALLOW_COPY_AND_ASSIGN(NeverDelayingDelegate);
100 };
101
102 class AlwaysDelayingDelegate : public ResourceQueueDelegate {
103 public:
104 AlwaysDelayingDelegate(ResourceQueue* resource_queue)
105 : resource_queue_(resource_queue) {
106 }
107
108 virtual bool ShouldDelayRequest(
109 URLRequest* request,
110 const ResourceDispatcherHostRequestInfo& request_info,
111 const GlobalRequestID& request_id) {
112 delayed_requests_.push_back(request_id);
113 return true;
114 }
115
116 virtual void WillShutdownResourceQueue() {
117 resource_queue_ = NULL;
118 }
119
120 void StartDelayedRequests() {
121 if (!resource_queue_)
122 return;
123
124 for (RequestList::iterator i = delayed_requests_.begin();
125 i != delayed_requests_.end(); ++i) {
126 resource_queue_->StartDelayedRequest(this, *i);
127 }
128 }
129
130 private:
131 typedef std::vector<GlobalRequestID> RequestList;
132
133 ResourceQueue* resource_queue_;
134
135 RequestList delayed_requests_;
136
137 DISALLOW_COPY_AND_ASSIGN(AlwaysDelayingDelegate);
138 };
139
140 class ResourceQueueTest : public testing::Test, public URLRequest::Delegate {
141 public:
142 ResourceQueueTest()
143 : response_started_count_(0),
144 message_loop_(MessageLoop::TYPE_IO),
145 ui_thread_(ChromeThread::UI, &message_loop_),
146 io_thread_(ChromeThread::IO, &message_loop_) {
147 }
148
149 virtual void OnResponseStarted(URLRequest* request) {
150 response_started_count_++;
151 // We're not going to do anything more with the request. Cancel it now
152 // to avoid leaking URLRequestJob.
153 request->Cancel();
154 }
155
156 virtual void OnReadCompleted(URLRequest* request, int bytes_read) {
157 }
158
159 protected:
160 int response_started_count_;
161
162 private:
163 MessageLoop message_loop_;
164 ChromeThread ui_thread_;
165 ChromeThread io_thread_;
166 };
167
168 TEST_F(ResourceQueueTest, Basic) {
169 // Test the simplest lifycycle of ResourceQueue.
170 ResourceQueue queue;
171 queue.Initialize(ResourceQueue::DelegateSet());
172 queue.Shutdown();
173 }
174
175 TEST_F(ResourceQueueTest, NeverDelayingDelegate) {
176 ResourceQueue queue;
177
178 NeverDelayingDelegate delegate;
179 InitializeQueue(&queue, &delegate);
180
181 URLRequest request(GURL(kTestUrl), this);
182 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0));
183 EXPECT_EQ(0, response_started_count_);
184 queue.AddRequest(&request, *request_info.get());
185 MessageLoop::current()->RunAllPending();
186 EXPECT_EQ(1, response_started_count_);
187
188 queue.Shutdown();
189 }
190
191 TEST_F(ResourceQueueTest, AlwaysDelayingDelegate) {
192 ResourceQueue queue;
193
194 AlwaysDelayingDelegate delegate(&queue);
195 InitializeQueue(&queue, &delegate);
196
197 URLRequest request(GURL(kTestUrl), this);
198 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0));
199 EXPECT_EQ(0, response_started_count_);
200 queue.AddRequest(&request, *request_info.get());
201 MessageLoop::current()->RunAllPending();
202 EXPECT_EQ(0, response_started_count_);
203 delegate.StartDelayedRequests();
204 MessageLoop::current()->RunAllPending();
205 EXPECT_EQ(1, response_started_count_);
206
207 queue.Shutdown();
208 }
209
210 TEST_F(ResourceQueueTest, AlwaysDelayingDelegateAfterShutdown) {
211 ResourceQueue queue;
212
213 AlwaysDelayingDelegate delegate(&queue);
214 InitializeQueue(&queue, &delegate);
215
216 URLRequest request(GURL(kTestUrl), this);
217 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0));
218 EXPECT_EQ(0, response_started_count_);
219 queue.AddRequest(&request, *request_info.get());
220 MessageLoop::current()->RunAllPending();
221 EXPECT_EQ(0, response_started_count_);
222
223 queue.Shutdown();
224
225 delegate.StartDelayedRequests();
226 MessageLoop::current()->RunAllPending();
227 EXPECT_EQ(0, response_started_count_);
228 }
229
230 TEST_F(ResourceQueueTest, TwoDelegates) {
231 ResourceQueue queue;
232
233 AlwaysDelayingDelegate always_delaying_delegate(&queue);
234 NeverDelayingDelegate never_delaying_delegate;
235 InitializeQueue(&queue, &always_delaying_delegate, &never_delaying_delegate);
236
237 URLRequest request(GURL(kTestUrl), this);
238 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0));
239 EXPECT_EQ(0, response_started_count_);
240 queue.AddRequest(&request, *request_info.get());
241 MessageLoop::current()->RunAllPending();
242 EXPECT_EQ(0, response_started_count_);
243 always_delaying_delegate.StartDelayedRequests();
244 MessageLoop::current()->RunAllPending();
245 EXPECT_EQ(1, response_started_count_);
246
247 queue.Shutdown();
248 }
249
250 TEST_F(ResourceQueueTest, RemoveRequest) {
251 ResourceQueue queue;
252
253 AlwaysDelayingDelegate delegate(&queue);
254 InitializeQueue(&queue, &delegate);
255
256 URLRequest request(GURL(kTestUrl), this);
257 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0));
258 GlobalRequestID request_id(request_info->child_id(),
259 request_info->request_id());
260 EXPECT_EQ(0, response_started_count_);
261 queue.AddRequest(&request, *request_info.get());
262 MessageLoop::current()->RunAllPending();
263 EXPECT_EQ(0, response_started_count_);
264 queue.RemoveRequest(request_id);
265 delegate.StartDelayedRequests();
266 MessageLoop::current()->RunAllPending();
267 EXPECT_EQ(0, response_started_count_);
268
269 queue.Shutdown();
270
271 MessageLoop::current()->RunAllPending();
272 EXPECT_EQ(0, response_started_count_);
273 }
274
275 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/renderer_host/resource_queue.cc ('k') | chrome/browser/ssl/ssl_cert_error_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698