Use ExtensionRegistry::enabled_extensions instead of deprecated ExtensionService...
[chromium-blink-merge.git] / content / child / resource_dispatcher_unittest.cc
blobb12db6b290451621d8af2ba7e9b6563bbfbdeaf4
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.
5 #include <string>
6 #include <vector>
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/process/process.h"
11 #include "base/process/process_handle.h"
12 #include "content/child/request_extra_data.h"
13 #include "content/child/request_info.h"
14 #include "content/child/resource_dispatcher.h"
15 #include "content/common/resource_messages.h"
16 #include "content/common/service_worker/service_worker_types.h"
17 #include "content/public/child/request_peer.h"
18 #include "content/public/common/resource_response.h"
19 #include "net/base/net_errors.h"
20 #include "net/http/http_response_headers.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "webkit/child/resource_loader_bridge.h"
23 #include "webkit/common/appcache/appcache_interfaces.h"
25 using webkit_glue::ResourceLoaderBridge;
26 using webkit_glue::ResourceResponseInfo;
28 namespace content {
30 static const char test_page_url[] = "http://www.google.com/";
31 static const char test_page_headers[] =
32 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n";
33 static const char test_page_mime_type[] = "text/html";
34 static const char test_page_charset[] = "";
35 static const char test_page_contents[] =
36 "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>";
37 static const uint32 test_page_contents_len = arraysize(test_page_contents) - 1;
39 // Listens for request response data and stores it so that it can be compared
40 // to the reference data.
41 class TestRequestCallback : public RequestPeer {
42 public:
43 TestRequestCallback() : complete_(false) {
46 virtual void OnUploadProgress(uint64 position, uint64 size) OVERRIDE {
49 virtual bool OnReceivedRedirect(
50 const GURL& new_url,
51 const ResourceResponseInfo& info,
52 bool* has_new_first_party_for_cookies,
53 GURL* new_first_party_for_cookies) OVERRIDE {
54 *has_new_first_party_for_cookies = false;
55 return true;
58 virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE {
61 virtual void OnDownloadedData(int len, int encoded_data_length) OVERRIDE {
64 virtual void OnReceivedData(const char* data,
65 int data_length,
66 int encoded_data_length) OVERRIDE {
67 EXPECT_FALSE(complete_);
68 data_.append(data, data_length);
69 total_encoded_data_length_ += encoded_data_length;
72 virtual void OnCompletedRequest(
73 int error_code,
74 bool was_ignored_by_handler,
75 bool stale_copy_in_cache,
76 const std::string& security_info,
77 const base::TimeTicks& completion_time,
78 int64 total_transfer_size) OVERRIDE {
79 EXPECT_FALSE(complete_);
80 complete_ = true;
83 bool complete() const {
84 return complete_;
86 const std::string& data() const {
87 return data_;
89 int total_encoded_data_length() const {
90 return total_encoded_data_length_;
93 private:
94 bool complete_;
95 std::string data_;
96 int total_encoded_data_length_;
100 // Sets up the message sender override for the unit test
101 class ResourceDispatcherTest : public testing::Test, public IPC::Sender {
102 public:
103 // Emulates IPC send operations (IPC::Sender) by adding
104 // pending messages to the queue.
105 virtual bool Send(IPC::Message* msg) OVERRIDE {
106 message_queue_.push_back(IPC::Message(*msg));
107 delete msg;
108 return true;
111 // Emulates the browser process and processes the pending IPC messages,
112 // returning the hardcoded file contents.
113 void ProcessMessages() {
114 while (!message_queue_.empty()) {
115 int request_id;
116 ResourceHostMsg_Request request;
117 ASSERT_TRUE(ResourceHostMsg_RequestResource::Read(
118 &message_queue_[0], &request_id, &request));
120 // check values
121 EXPECT_EQ(test_page_url, request.url.spec());
123 // received response message
124 ResourceResponseHead response;
125 std::string raw_headers(test_page_headers);
126 std::replace(raw_headers.begin(), raw_headers.end(), '\n', '\0');
127 response.headers = new net::HttpResponseHeaders(raw_headers);
128 response.mime_type = test_page_mime_type;
129 response.charset = test_page_charset;
130 dispatcher_->OnReceivedResponse(request_id, response);
132 // received data message with the test contents
133 base::SharedMemory shared_mem;
134 EXPECT_TRUE(shared_mem.CreateAndMapAnonymous(test_page_contents_len));
135 char* put_data_here = static_cast<char*>(shared_mem.memory());
136 memcpy(put_data_here, test_page_contents, test_page_contents_len);
137 base::SharedMemoryHandle dup_handle;
138 EXPECT_TRUE(shared_mem.GiveToProcess(
139 base::Process::Current().handle(), &dup_handle));
140 dispatcher_->OnSetDataBuffer(request_id, dup_handle,
141 test_page_contents_len, 0);
142 dispatcher_->OnReceivedData(request_id, 0, test_page_contents_len,
143 test_page_contents_len);
145 message_queue_.erase(message_queue_.begin());
147 // read the ack message.
148 Tuple1<int> request_ack;
149 ASSERT_TRUE(ResourceHostMsg_DataReceived_ACK::Read(
150 &message_queue_[0], &request_ack));
152 ASSERT_EQ(request_ack.a, request_id);
154 message_queue_.erase(message_queue_.begin());
158 protected:
159 // testing::Test
160 virtual void SetUp() OVERRIDE {
161 dispatcher_.reset(new ResourceDispatcher(this));
163 virtual void TearDown() OVERRIDE {
164 dispatcher_.reset();
167 ResourceLoaderBridge* CreateBridge() {
168 RequestInfo request_info;
169 request_info.method = "GET";
170 request_info.url = GURL(test_page_url);
171 request_info.first_party_for_cookies = GURL(test_page_url);
172 request_info.referrer = GURL();
173 request_info.headers = std::string();
174 request_info.load_flags = 0;
175 request_info.requestor_pid = 0;
176 request_info.request_type = ResourceType::SUB_RESOURCE;
177 request_info.appcache_host_id = appcache::kNoHostId;
178 request_info.routing_id = 0;
179 RequestExtraData extra_data;
180 request_info.extra_data = &extra_data;
182 return dispatcher_->CreateBridge(request_info);
185 std::vector<IPC::Message> message_queue_;
186 static scoped_ptr<ResourceDispatcher> dispatcher_;
189 /*static*/
190 scoped_ptr<ResourceDispatcher> ResourceDispatcherTest::dispatcher_;
192 // Does a simple request and tests that the correct data is received.
193 TEST_F(ResourceDispatcherTest, RoundTrip) {
194 TestRequestCallback callback;
195 ResourceLoaderBridge* bridge = CreateBridge();
197 bridge->Start(&callback);
199 ProcessMessages();
201 // FIXME(brettw) when the request complete messages are actually handledo
202 // and dispatched, uncomment this.
203 //EXPECT_TRUE(callback.complete());
204 //EXPECT_STREQ(test_page_contents, callback.data().c_str());
205 //EXPECT_EQ(test_page_contents_len, callback.total_encoded_data_length());
207 delete bridge;
210 // Tests that the request IDs are straight when there are multiple requests.
211 TEST_F(ResourceDispatcherTest, MultipleRequests) {
212 // FIXME
215 // Tests that the cancel method prevents other messages from being received
216 TEST_F(ResourceDispatcherTest, Cancel) {
217 // FIXME
220 TEST_F(ResourceDispatcherTest, Cookies) {
221 // FIXME
224 TEST_F(ResourceDispatcherTest, SerializedPostData) {
225 // FIXME
228 // This class provides functionality to validate whether the ResourceDispatcher
229 // object honors the deferred loading contract correctly, i.e. if deferred
230 // loading is enabled it should queue up any responses received. If deferred
231 // loading is enabled/disabled in the context of a dispatched message, other
232 // queued messages should not be dispatched until deferred load is turned off.
233 class DeferredResourceLoadingTest : public ResourceDispatcherTest,
234 public RequestPeer {
235 public:
236 DeferredResourceLoadingTest()
237 : defer_loading_(false) {
240 virtual bool Send(IPC::Message* msg) OVERRIDE {
241 delete msg;
242 return true;
245 void InitMessages() {
246 set_defer_loading(true);
248 ResourceResponseHead response_head;
249 response_head.error_code = net::OK;
251 dispatcher_->OnMessageReceived(
252 ResourceMsg_ReceivedResponse(0, response_head));
254 // Duplicate the shared memory handle so both the test and the callee can
255 // close their copy.
256 base::SharedMemoryHandle duplicated_handle;
257 EXPECT_TRUE(shared_handle_.ShareToProcess(base::GetCurrentProcessHandle(),
258 &duplicated_handle));
260 dispatcher_->OnMessageReceived(
261 ResourceMsg_SetDataBuffer(0, duplicated_handle, 100, 0));
262 dispatcher_->OnMessageReceived(ResourceMsg_DataReceived(0, 0, 100, 100));
264 set_defer_loading(false);
267 // RequestPeer methods.
268 virtual void OnUploadProgress(uint64 position, uint64 size) OVERRIDE {
271 virtual bool OnReceivedRedirect(
272 const GURL& new_url,
273 const ResourceResponseInfo& info,
274 bool* has_new_first_party_for_cookies,
275 GURL* new_first_party_for_cookies) OVERRIDE {
276 *has_new_first_party_for_cookies = false;
277 return true;
280 virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE {
281 EXPECT_EQ(defer_loading_, false);
282 set_defer_loading(true);
285 virtual void OnDownloadedData(int len, int encoded_data_length) OVERRIDE {
288 virtual void OnReceivedData(const char* data,
289 int data_length,
290 int encoded_data_length) OVERRIDE {
291 EXPECT_EQ(defer_loading_, false);
292 set_defer_loading(false);
295 virtual void OnCompletedRequest(
296 int error_code,
297 bool was_ignored_by_handler,
298 bool stale_copy_in_cache,
299 const std::string& security_info,
300 const base::TimeTicks& completion_time,
301 int64 total_transfer_size) OVERRIDE {
304 protected:
305 virtual void SetUp() OVERRIDE {
306 ResourceDispatcherTest::SetUp();
307 EXPECT_TRUE(shared_handle_.CreateAnonymous(100));
310 virtual void TearDown() OVERRIDE {
311 shared_handle_.Close();
312 ResourceDispatcherTest::TearDown();
315 private:
316 void set_defer_loading(bool defer) {
317 defer_loading_ = defer;
318 dispatcher_->SetDefersLoading(0, defer);
321 bool defer_loading() const {
322 return defer_loading_;
325 bool defer_loading_;
326 base::SharedMemory shared_handle_;
329 TEST_F(DeferredResourceLoadingTest, DeferredLoadTest) {
330 base::MessageLoopForIO message_loop;
332 ResourceLoaderBridge* bridge = CreateBridge();
334 bridge->Start(this);
335 InitMessages();
337 // Dispatch deferred messages.
338 message_loop.RunUntilIdle();
339 delete bridge;
342 class TimeConversionTest : public ResourceDispatcherTest,
343 public RequestPeer {
344 public:
345 virtual bool Send(IPC::Message* msg) OVERRIDE {
346 delete msg;
347 return true;
350 void PerformTest(const ResourceResponseHead& response_head) {
351 scoped_ptr<ResourceLoaderBridge> bridge(CreateBridge());
352 bridge->Start(this);
354 dispatcher_->OnMessageReceived(
355 ResourceMsg_ReceivedResponse(0, response_head));
358 // RequestPeer methods.
359 virtual void OnUploadProgress(uint64 position, uint64 size) OVERRIDE {
362 virtual bool OnReceivedRedirect(
363 const GURL& new_url,
364 const ResourceResponseInfo& info,
365 bool* has_new_first_party_for_cookies,
366 GURL* new_first_party_for_cookies) OVERRIDE {
367 return true;
370 virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE {
371 response_info_ = info;
374 virtual void OnDownloadedData(int len, int encoded_data_length) OVERRIDE {
377 virtual void OnReceivedData(const char* data,
378 int data_length,
379 int encoded_data_length) OVERRIDE {
382 virtual void OnCompletedRequest(
383 int error_code,
384 bool was_ignored_by_handler,
385 bool stale_copy_in_cache,
386 const std::string& security_info,
387 const base::TimeTicks& completion_time,
388 int64 total_transfer_size) OVERRIDE {
391 const ResourceResponseInfo& response_info() const { return response_info_; }
393 private:
394 ResourceResponseInfo response_info_;
397 // TODO(simonjam): Enable this when 10829031 lands.
398 TEST_F(TimeConversionTest, DISABLED_ProperlyInitialized) {
399 ResourceResponseHead response_head;
400 response_head.error_code = net::OK;
401 response_head.request_start = base::TimeTicks::FromInternalValue(5);
402 response_head.response_start = base::TimeTicks::FromInternalValue(15);
403 response_head.load_timing.request_start_time = base::Time::Now();
404 response_head.load_timing.request_start =
405 base::TimeTicks::FromInternalValue(10);
406 response_head.load_timing.connect_timing.connect_start =
407 base::TimeTicks::FromInternalValue(13);
409 PerformTest(response_head);
411 EXPECT_LT(base::TimeTicks(), response_info().load_timing.request_start);
412 EXPECT_EQ(base::TimeTicks(),
413 response_info().load_timing.connect_timing.dns_start);
414 EXPECT_LE(response_head.load_timing.request_start,
415 response_info().load_timing.connect_timing.connect_start);
418 TEST_F(TimeConversionTest, PartiallyInitialized) {
419 ResourceResponseHead response_head;
420 response_head.error_code = net::OK;
421 response_head.request_start = base::TimeTicks::FromInternalValue(5);
422 response_head.response_start = base::TimeTicks::FromInternalValue(15);
424 PerformTest(response_head);
426 EXPECT_EQ(base::TimeTicks(), response_info().load_timing.request_start);
427 EXPECT_EQ(base::TimeTicks(),
428 response_info().load_timing.connect_timing.dns_start);
431 TEST_F(TimeConversionTest, NotInitialized) {
432 ResourceResponseHead response_head;
433 response_head.error_code = net::OK;
435 PerformTest(response_head);
437 EXPECT_EQ(base::TimeTicks(), response_info().load_timing.request_start);
438 EXPECT_EQ(base::TimeTicks(),
439 response_info().load_timing.connect_timing.dns_start);
442 } // namespace content