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