OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/scoped_ptr.h" | 6 #include "base/scoped_ptr.h" |
7 #include "chrome/browser/browser_thread.h" | 7 #include "chrome/browser/browser_thread.h" |
8 #include "chrome/browser/renderer_host/global_request_id.h" | 8 #include "chrome/browser/renderer_host/global_request_id.h" |
9 #include "chrome/browser/renderer_host/resource_dispatcher_host_request_info.h" | 9 #include "chrome/browser/renderer_host/resource_dispatcher_host_request_info.h" |
10 #include "chrome/browser/renderer_host/resource_handler.h" | 10 #include "chrome/browser/renderer_host/resource_handler.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 delegate_set.insert(delegate2); | 93 delegate_set.insert(delegate2); |
94 queue->Initialize(delegate_set); | 94 queue->Initialize(delegate_set); |
95 } | 95 } |
96 | 96 |
97 class NeverDelayingDelegate : public ResourceQueueDelegate { | 97 class NeverDelayingDelegate : public ResourceQueueDelegate { |
98 public: | 98 public: |
99 NeverDelayingDelegate() { | 99 NeverDelayingDelegate() { |
100 } | 100 } |
101 | 101 |
102 virtual bool ShouldDelayRequest( | 102 virtual bool ShouldDelayRequest( |
103 URLRequest* request, | 103 net::URLRequest* request, |
104 const ResourceDispatcherHostRequestInfo& request_info, | 104 const ResourceDispatcherHostRequestInfo& request_info, |
105 const GlobalRequestID& request_id) { | 105 const GlobalRequestID& request_id) { |
106 return false; | 106 return false; |
107 } | 107 } |
108 | 108 |
109 virtual void WillShutdownResourceQueue() { | 109 virtual void WillShutdownResourceQueue() { |
110 } | 110 } |
111 | 111 |
112 private: | 112 private: |
113 DISALLOW_COPY_AND_ASSIGN(NeverDelayingDelegate); | 113 DISALLOW_COPY_AND_ASSIGN(NeverDelayingDelegate); |
114 }; | 114 }; |
115 | 115 |
116 class AlwaysDelayingDelegate : public ResourceQueueDelegate { | 116 class AlwaysDelayingDelegate : public ResourceQueueDelegate { |
117 public: | 117 public: |
118 explicit AlwaysDelayingDelegate(ResourceQueue* resource_queue) | 118 explicit AlwaysDelayingDelegate(ResourceQueue* resource_queue) |
119 : resource_queue_(resource_queue) { | 119 : resource_queue_(resource_queue) { |
120 } | 120 } |
121 | 121 |
122 virtual bool ShouldDelayRequest( | 122 virtual bool ShouldDelayRequest( |
123 URLRequest* request, | 123 net::URLRequest* request, |
124 const ResourceDispatcherHostRequestInfo& request_info, | 124 const ResourceDispatcherHostRequestInfo& request_info, |
125 const GlobalRequestID& request_id) { | 125 const GlobalRequestID& request_id) { |
126 delayed_requests_.push_back(request_id); | 126 delayed_requests_.push_back(request_id); |
127 return true; | 127 return true; |
128 } | 128 } |
129 | 129 |
130 virtual void WillShutdownResourceQueue() { | 130 virtual void WillShutdownResourceQueue() { |
131 resource_queue_ = NULL; | 131 resource_queue_ = NULL; |
132 } | 132 } |
133 | 133 |
(...skipping 10 matching lines...) Expand all Loading... |
144 private: | 144 private: |
145 typedef std::vector<GlobalRequestID> RequestList; | 145 typedef std::vector<GlobalRequestID> RequestList; |
146 | 146 |
147 ResourceQueue* resource_queue_; | 147 ResourceQueue* resource_queue_; |
148 | 148 |
149 RequestList delayed_requests_; | 149 RequestList delayed_requests_; |
150 | 150 |
151 DISALLOW_COPY_AND_ASSIGN(AlwaysDelayingDelegate); | 151 DISALLOW_COPY_AND_ASSIGN(AlwaysDelayingDelegate); |
152 }; | 152 }; |
153 | 153 |
154 class ResourceQueueTest : public testing::Test, public URLRequest::Delegate { | 154 class ResourceQueueTest : public testing::Test, |
| 155 public net::URLRequest::Delegate { |
155 public: | 156 public: |
156 ResourceQueueTest() | 157 ResourceQueueTest() |
157 : response_started_count_(0), | 158 : response_started_count_(0), |
158 message_loop_(MessageLoop::TYPE_IO), | 159 message_loop_(MessageLoop::TYPE_IO), |
159 ui_thread_(BrowserThread::UI, &message_loop_), | 160 ui_thread_(BrowserThread::UI, &message_loop_), |
160 io_thread_(BrowserThread::IO, &message_loop_) { | 161 io_thread_(BrowserThread::IO, &message_loop_) { |
161 } | 162 } |
162 | 163 |
163 virtual void OnResponseStarted(URLRequest* request) { | 164 virtual void OnResponseStarted(net::URLRequest* request) { |
164 response_started_count_++; | 165 response_started_count_++; |
165 // We're not going to do anything more with the request. Cancel it now | 166 // We're not going to do anything more with the request. Cancel it now |
166 // to avoid leaking URLRequestJob. | 167 // to avoid leaking URLRequestJob. |
167 request->Cancel(); | 168 request->Cancel(); |
168 } | 169 } |
169 | 170 |
170 virtual void OnReadCompleted(URLRequest* request, int bytes_read) { | 171 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read) { |
171 } | 172 } |
172 | 173 |
173 protected: | 174 protected: |
174 int response_started_count_; | 175 int response_started_count_; |
175 | 176 |
176 private: | 177 private: |
177 MessageLoop message_loop_; | 178 MessageLoop message_loop_; |
178 BrowserThread ui_thread_; | 179 BrowserThread ui_thread_; |
179 BrowserThread io_thread_; | 180 BrowserThread io_thread_; |
180 }; | 181 }; |
181 | 182 |
182 TEST_F(ResourceQueueTest, Basic) { | 183 TEST_F(ResourceQueueTest, Basic) { |
183 // Test the simplest lifycycle of ResourceQueue. | 184 // Test the simplest lifycycle of ResourceQueue. |
184 ResourceQueue queue; | 185 ResourceQueue queue; |
185 queue.Initialize(ResourceQueue::DelegateSet()); | 186 queue.Initialize(ResourceQueue::DelegateSet()); |
186 queue.Shutdown(); | 187 queue.Shutdown(); |
187 } | 188 } |
188 | 189 |
189 TEST_F(ResourceQueueTest, NeverDelayingDelegate) { | 190 TEST_F(ResourceQueueTest, NeverDelayingDelegate) { |
190 ResourceQueue queue; | 191 ResourceQueue queue; |
191 | 192 |
192 NeverDelayingDelegate delegate; | 193 NeverDelayingDelegate delegate; |
193 InitializeQueue(&queue, &delegate); | 194 InitializeQueue(&queue, &delegate); |
194 | 195 |
195 URLRequest request(GURL(kTestUrl), this); | 196 net::URLRequest request(GURL(kTestUrl), this); |
196 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0)); | 197 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0)); |
197 EXPECT_EQ(0, response_started_count_); | 198 EXPECT_EQ(0, response_started_count_); |
198 queue.AddRequest(&request, *request_info.get()); | 199 queue.AddRequest(&request, *request_info.get()); |
199 MessageLoop::current()->RunAllPending(); | 200 MessageLoop::current()->RunAllPending(); |
200 EXPECT_EQ(1, response_started_count_); | 201 EXPECT_EQ(1, response_started_count_); |
201 | 202 |
202 queue.Shutdown(); | 203 queue.Shutdown(); |
203 } | 204 } |
204 | 205 |
205 TEST_F(ResourceQueueTest, AlwaysDelayingDelegate) { | 206 TEST_F(ResourceQueueTest, AlwaysDelayingDelegate) { |
206 ResourceQueue queue; | 207 ResourceQueue queue; |
207 | 208 |
208 AlwaysDelayingDelegate delegate(&queue); | 209 AlwaysDelayingDelegate delegate(&queue); |
209 InitializeQueue(&queue, &delegate); | 210 InitializeQueue(&queue, &delegate); |
210 | 211 |
211 URLRequest request(GURL(kTestUrl), this); | 212 net::URLRequest request(GURL(kTestUrl), this); |
212 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0)); | 213 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0)); |
213 EXPECT_EQ(0, response_started_count_); | 214 EXPECT_EQ(0, response_started_count_); |
214 queue.AddRequest(&request, *request_info.get()); | 215 queue.AddRequest(&request, *request_info.get()); |
215 MessageLoop::current()->RunAllPending(); | 216 MessageLoop::current()->RunAllPending(); |
216 EXPECT_EQ(0, response_started_count_); | 217 EXPECT_EQ(0, response_started_count_); |
217 delegate.StartDelayedRequests(); | 218 delegate.StartDelayedRequests(); |
218 MessageLoop::current()->RunAllPending(); | 219 MessageLoop::current()->RunAllPending(); |
219 EXPECT_EQ(1, response_started_count_); | 220 EXPECT_EQ(1, response_started_count_); |
220 | 221 |
221 queue.Shutdown(); | 222 queue.Shutdown(); |
222 } | 223 } |
223 | 224 |
224 TEST_F(ResourceQueueTest, AlwaysDelayingDelegateAfterShutdown) { | 225 TEST_F(ResourceQueueTest, AlwaysDelayingDelegateAfterShutdown) { |
225 ResourceQueue queue; | 226 ResourceQueue queue; |
226 | 227 |
227 AlwaysDelayingDelegate delegate(&queue); | 228 AlwaysDelayingDelegate delegate(&queue); |
228 InitializeQueue(&queue, &delegate); | 229 InitializeQueue(&queue, &delegate); |
229 | 230 |
230 URLRequest request(GURL(kTestUrl), this); | 231 net::URLRequest request(GURL(kTestUrl), this); |
231 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0)); | 232 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0)); |
232 EXPECT_EQ(0, response_started_count_); | 233 EXPECT_EQ(0, response_started_count_); |
233 queue.AddRequest(&request, *request_info.get()); | 234 queue.AddRequest(&request, *request_info.get()); |
234 MessageLoop::current()->RunAllPending(); | 235 MessageLoop::current()->RunAllPending(); |
235 EXPECT_EQ(0, response_started_count_); | 236 EXPECT_EQ(0, response_started_count_); |
236 | 237 |
237 queue.Shutdown(); | 238 queue.Shutdown(); |
238 | 239 |
239 delegate.StartDelayedRequests(); | 240 delegate.StartDelayedRequests(); |
240 MessageLoop::current()->RunAllPending(); | 241 MessageLoop::current()->RunAllPending(); |
241 EXPECT_EQ(0, response_started_count_); | 242 EXPECT_EQ(0, response_started_count_); |
242 } | 243 } |
243 | 244 |
244 TEST_F(ResourceQueueTest, TwoDelegates) { | 245 TEST_F(ResourceQueueTest, TwoDelegates) { |
245 ResourceQueue queue; | 246 ResourceQueue queue; |
246 | 247 |
247 AlwaysDelayingDelegate always_delaying_delegate(&queue); | 248 AlwaysDelayingDelegate always_delaying_delegate(&queue); |
248 NeverDelayingDelegate never_delaying_delegate; | 249 NeverDelayingDelegate never_delaying_delegate; |
249 InitializeQueue(&queue, &always_delaying_delegate, &never_delaying_delegate); | 250 InitializeQueue(&queue, &always_delaying_delegate, &never_delaying_delegate); |
250 | 251 |
251 URLRequest request(GURL(kTestUrl), this); | 252 net::URLRequest request(GURL(kTestUrl), this); |
252 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0)); | 253 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0)); |
253 EXPECT_EQ(0, response_started_count_); | 254 EXPECT_EQ(0, response_started_count_); |
254 queue.AddRequest(&request, *request_info.get()); | 255 queue.AddRequest(&request, *request_info.get()); |
255 MessageLoop::current()->RunAllPending(); | 256 MessageLoop::current()->RunAllPending(); |
256 EXPECT_EQ(0, response_started_count_); | 257 EXPECT_EQ(0, response_started_count_); |
257 always_delaying_delegate.StartDelayedRequests(); | 258 always_delaying_delegate.StartDelayedRequests(); |
258 MessageLoop::current()->RunAllPending(); | 259 MessageLoop::current()->RunAllPending(); |
259 EXPECT_EQ(1, response_started_count_); | 260 EXPECT_EQ(1, response_started_count_); |
260 | 261 |
261 queue.Shutdown(); | 262 queue.Shutdown(); |
262 } | 263 } |
263 | 264 |
264 TEST_F(ResourceQueueTest, RemoveRequest) { | 265 TEST_F(ResourceQueueTest, RemoveRequest) { |
265 ResourceQueue queue; | 266 ResourceQueue queue; |
266 | 267 |
267 AlwaysDelayingDelegate delegate(&queue); | 268 AlwaysDelayingDelegate delegate(&queue); |
268 InitializeQueue(&queue, &delegate); | 269 InitializeQueue(&queue, &delegate); |
269 | 270 |
270 URLRequest request(GURL(kTestUrl), this); | 271 net::URLRequest request(GURL(kTestUrl), this); |
271 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0)); | 272 scoped_ptr<ResourceDispatcherHostRequestInfo> request_info(GetRequestInfo(0)); |
272 GlobalRequestID request_id(request_info->child_id(), | 273 GlobalRequestID request_id(request_info->child_id(), |
273 request_info->request_id()); | 274 request_info->request_id()); |
274 EXPECT_EQ(0, response_started_count_); | 275 EXPECT_EQ(0, response_started_count_); |
275 queue.AddRequest(&request, *request_info.get()); | 276 queue.AddRequest(&request, *request_info.get()); |
276 MessageLoop::current()->RunAllPending(); | 277 MessageLoop::current()->RunAllPending(); |
277 EXPECT_EQ(0, response_started_count_); | 278 EXPECT_EQ(0, response_started_count_); |
278 queue.RemoveRequest(request_id); | 279 queue.RemoveRequest(request_id); |
279 delegate.StartDelayedRequests(); | 280 delegate.StartDelayedRequests(); |
280 MessageLoop::current()->RunAllPending(); | 281 MessageLoop::current()->RunAllPending(); |
281 EXPECT_EQ(0, response_started_count_); | 282 EXPECT_EQ(0, response_started_count_); |
282 | 283 |
283 queue.Shutdown(); | 284 queue.Shutdown(); |
284 | 285 |
285 MessageLoop::current()->RunAllPending(); | 286 MessageLoop::current()->RunAllPending(); |
286 EXPECT_EQ(0, response_started_count_); | 287 EXPECT_EQ(0, response_started_count_); |
287 } | 288 } |
288 | 289 |
289 } // namespace | 290 } // namespace |
OLD | NEW |