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.
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h"
10 #include "base/process.h"
11 #include "base/process_util.h"
12 #include "content/common/resource_dispatcher.h"
13 #include "content/common/resource_messages.h"
14 #include "content/common/resource_response.h"
15 #include "net/base/upload_data.h"
16 #include "net/http/http_response_headers.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "webkit/appcache/appcache_interfaces.h"
20 using webkit_glue::ResourceLoaderBridge
;
21 using webkit_glue::ResourceResponseInfo
;
23 static const char test_page_url
[] = "http://www.google.com/";
24 static const char test_page_headers
[] =
25 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n";
26 static const char test_page_mime_type
[] = "text/html";
27 static const char test_page_charset
[] = "";
28 static const char test_page_contents
[] =
29 "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>";
30 static const uint32 test_page_contents_len
= arraysize(test_page_contents
) - 1;
32 static const char kShmemSegmentName
[] = "DeferredResourceLoaderTest";
34 // Listens for request response data and stores it so that it can be compared
35 // to the reference data.
36 class TestRequestCallback
: public ResourceLoaderBridge::Peer
{
38 TestRequestCallback() : complete_(false) {
41 virtual void OnUploadProgress(uint64 position
, uint64 size
) {
44 virtual bool OnReceivedRedirect(
46 const ResourceResponseInfo
& info
,
47 bool* has_new_first_party_for_cookies
,
48 GURL
* new_first_party_for_cookies
) {
49 *has_new_first_party_for_cookies
= false;
53 virtual void OnReceivedResponse(const ResourceResponseInfo
& info
) {
56 virtual void OnDownloadedData(int len
) {
59 virtual void OnReceivedData(const char* data
,
61 int encoded_data_length
) {
62 EXPECT_FALSE(complete_
);
63 data_
.append(data
, data_length
);
64 total_encoded_data_length_
+= encoded_data_length
;
67 virtual void OnCompletedRequest(const net::URLRequestStatus
& status
,
68 const std::string
& security_info
,
69 const base::Time
& completion_time
) {
70 EXPECT_FALSE(complete_
);
74 bool complete() const {
77 const std::string
& data() const {
80 int total_encoded_data_length() const {
81 return total_encoded_data_length_
;
87 int total_encoded_data_length_
;
91 // Sets up the message sender override for the unit test
92 class ResourceDispatcherTest
: public testing::Test
,
93 public IPC::Message::Sender
{
95 // Emulates IPC send operations (IPC::Message::Sender) by adding
96 // pending messages to the queue.
97 virtual bool Send(IPC::Message
* msg
) {
98 message_queue_
.push_back(IPC::Message(*msg
));
103 // Emulates the browser process and processes the pending IPC messages,
104 // returning the hardcoded file contents.
105 void ProcessMessages() {
106 while (!message_queue_
.empty()) {
108 ResourceHostMsg_Request request
;
109 ASSERT_TRUE(ResourceHostMsg_RequestResource::Read(
110 &message_queue_
[0], &request_id
, &request
));
113 EXPECT_EQ(test_page_url
, request
.url
.spec());
115 // received response message
116 ResourceResponseHead response
;
117 std::string
raw_headers(test_page_headers
);
118 std::replace(raw_headers
.begin(), raw_headers
.end(), '\n', '\0');
119 response
.headers
= new net::HttpResponseHeaders(raw_headers
);
120 response
.mime_type
= test_page_mime_type
;
121 response
.charset
= test_page_charset
;
122 dispatcher_
->OnReceivedResponse(request_id
, response
);
124 // received data message with the test contents
125 base::SharedMemory shared_mem
;
126 EXPECT_TRUE(shared_mem
.CreateAndMapAnonymous(test_page_contents_len
));
127 char* put_data_here
= static_cast<char*>(shared_mem
.memory());
128 memcpy(put_data_here
, test_page_contents
, test_page_contents_len
);
129 base::SharedMemoryHandle dup_handle
;
130 EXPECT_TRUE(shared_mem
.GiveToProcess(
131 base::Process::Current().handle(), &dup_handle
));
132 dispatcher_
->OnReceivedData(
136 test_page_contents_len
,
137 test_page_contents_len
);
139 message_queue_
.erase(message_queue_
.begin());
141 // read the ack message.
142 Tuple1
<int> request_ack
;
143 ASSERT_TRUE(ResourceHostMsg_DataReceived_ACK::Read(
144 &message_queue_
[0], &request_ack
));
146 ASSERT_EQ(request_ack
.a
, request_id
);
148 message_queue_
.erase(message_queue_
.begin());
154 virtual void SetUp() {
155 dispatcher_
.reset(new ResourceDispatcher(this));
157 virtual void TearDown() {
161 ResourceLoaderBridge
* CreateBridge() {
162 webkit_glue::ResourceLoaderBridge::RequestInfo request_info
;
163 request_info
.method
= "GET";
164 request_info
.url
= GURL(test_page_url
);
165 request_info
.first_party_for_cookies
= GURL(test_page_url
);
166 request_info
.referrer
= GURL();
167 request_info
.headers
= std::string();
168 request_info
.load_flags
= 0;
169 request_info
.requestor_pid
= 0;
170 request_info
.request_type
= ResourceType::SUB_RESOURCE
;
171 request_info
.appcache_host_id
= appcache::kNoHostId
;
172 request_info
.routing_id
= 0;
174 return dispatcher_
->CreateBridge(request_info
);
177 std::vector
<IPC::Message
> message_queue_
;
178 static scoped_ptr
<ResourceDispatcher
> dispatcher_
;
182 scoped_ptr
<ResourceDispatcher
> ResourceDispatcherTest::dispatcher_
;
184 // Does a simple request and tests that the correct data is received.
185 TEST_F(ResourceDispatcherTest
, RoundTrip
) {
186 TestRequestCallback callback
;
187 ResourceLoaderBridge
* bridge
= CreateBridge();
189 bridge
->Start(&callback
);
193 // FIXME(brettw) when the request complete messages are actually handledo
194 // and dispatched, uncomment this.
195 //EXPECT_TRUE(callback.complete());
196 //EXPECT_STREQ(test_page_contents, callback.data().c_str());
197 //EXPECT_EQ(test_page_contents_len, callback.total_encoded_data_length());
202 // Tests that the request IDs are straight when there are multiple requests.
203 TEST_F(ResourceDispatcherTest
, MultipleRequests
) {
207 // Tests that the cancel method prevents other messages from being received
208 TEST_F(ResourceDispatcherTest
, Cancel
) {
212 TEST_F(ResourceDispatcherTest
, Cookies
) {
216 TEST_F(ResourceDispatcherTest
, SerializedPostData
) {
220 // This class provides functionality to validate whether the ResourceDispatcher
221 // object honors the deferred loading contract correctly, i.e. if deferred
222 // loading is enabled it should queue up any responses received. If deferred
223 // loading is enabled/disabled in the context of a dispatched message, other
224 // queued messages should not be dispatched until deferred load is turned off.
225 class DeferredResourceLoadingTest
: public ResourceDispatcherTest
,
226 public ResourceLoaderBridge::Peer
{
228 DeferredResourceLoadingTest()
229 : defer_loading_(false) {
232 virtual bool Send(IPC::Message
* msg
) {
237 void InitMessages() {
238 set_defer_loading(true);
240 ResourceResponseHead response_head
;
241 response_head
.status
.set_status(net::URLRequestStatus::SUCCESS
);
243 IPC::Message
* response_message
=
244 new ResourceMsg_ReceivedResponse(0, 0, response_head
);
246 dispatcher_
->OnMessageReceived(*response_message
);
248 delete response_message
;
250 // Duplicate the shared memory handle so both the test and the callee can
252 base::SharedMemoryHandle duplicated_handle
;
253 EXPECT_TRUE(shared_handle_
.ShareToProcess(base::GetCurrentProcessHandle(),
254 &duplicated_handle
));
257 new ResourceMsg_DataReceived(0, 0, duplicated_handle
, 100, 100);
259 dispatcher_
->OnMessageReceived(*response_message
);
261 delete response_message
;
263 set_defer_loading(false);
266 // ResourceLoaderBridge::Peer methods.
267 virtual void OnUploadProgress(uint64 position
, uint64 size
) {
270 virtual bool OnReceivedRedirect(
272 const ResourceResponseInfo
& info
,
273 bool* has_new_first_party_for_cookies
,
274 GURL
* new_first_party_for_cookies
) {
275 *has_new_first_party_for_cookies
= false;
279 virtual void OnReceivedResponse(const ResourceResponseInfo
& info
) {
280 EXPECT_EQ(defer_loading_
, false);
281 set_defer_loading(true);
284 virtual void OnDownloadedData(int len
) {
287 virtual void OnReceivedData(const char* data
,
289 int encoded_data_length
) {
290 EXPECT_EQ(defer_loading_
, false);
291 set_defer_loading(false);
294 virtual void OnCompletedRequest(const net::URLRequestStatus
& status
,
295 const std::string
& security_info
,
296 const base::Time
& completion_time
) {
300 virtual void SetUp() {
301 ResourceDispatcherTest::SetUp();
302 shared_handle_
.Delete(kShmemSegmentName
);
303 EXPECT_TRUE(shared_handle_
.CreateNamed(kShmemSegmentName
, false, 100));
306 virtual void TearDown() {
307 shared_handle_
.Close();
308 EXPECT_TRUE(shared_handle_
.Delete(kShmemSegmentName
));
309 ResourceDispatcherTest::TearDown();
313 void set_defer_loading(bool defer
) {
314 defer_loading_
= defer
;
315 dispatcher_
->SetDefersLoading(0, defer
);
318 bool defer_loading() const {
319 return defer_loading_
;
323 base::SharedMemory shared_handle_
;
326 TEST_F(DeferredResourceLoadingTest
, DeferredLoadTest
) {
327 MessageLoop
message_loop(MessageLoop::TYPE_IO
);
329 ResourceLoaderBridge
* bridge
= CreateBridge();
334 // Dispatch deferred messages.
335 message_loop
.RunAllPending();