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