blob: 0236262655b87b23f88aa3f3ee21385e675aa6ba [file] [log] [blame]
[email protected]f90bf0d92011-01-13 02:12:441// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#include <string>
6#include <vector>
7
[email protected]3b63f8f42011-03-28 01:54:158#include "base/memory/scoped_ptr.h"
[email protected]2602087e2009-08-24 23:12:169#include "base/message_loop.h"
[email protected]4003d7142009-01-12 12:56:2010#include "base/process.h"
[email protected]a68114f72009-11-30 23:32:4911#include "base/process_util.h"
[email protected]620161e2011-03-07 18:05:2612#include "content/common/resource_dispatcher.h"
[email protected]94dc971d2011-03-05 19:08:3213#include "content/common/resource_messages.h"
[email protected]620161e2011-03-07 18:05:2614#include "content/common/resource_response.h"
[email protected]939856a2010-08-24 20:29:0215#include "net/base/upload_data.h"
[email protected]7a4de7a62010-08-17 18:38:2416#include "net/http/http_response_headers.h"
initial.commit09911bf2008-07-26 23:55:2917#include "testing/gtest/include/gtest/gtest.h"
[email protected]f430b5712009-08-21 21:46:3118#include "webkit/appcache/appcache_interfaces.h"
initial.commit09911bf2008-07-26 23:55:2919
20using webkit_glue::ResourceLoaderBridge;
[email protected]b4b3a912010-10-08 19:05:3721using webkit_glue::ResourceResponseInfo;
initial.commit09911bf2008-07-26 23:55:2922
23static const char test_page_url[] = "http://www.google.com/";
24static const char test_page_headers[] =
25 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n";
26static const char test_page_mime_type[] = "text/html";
27static const char test_page_charset[] = "";
28static const char test_page_contents[] =
29 "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>";
[email protected]b5ab3982010-02-16 23:58:2730static const uint32 test_page_contents_len = arraysize(test_page_contents) - 1;
initial.commit09911bf2008-07-26 23:55:2931
[email protected]b8901082010-11-12 01:14:2832static const char kShmemSegmentName[] = "DeferredResourceLoaderTest";
33
initial.commit09911bf2008-07-26 23:55:2934// Listens for request response data and stores it so that it can be compared
35// to the reference data.
36class TestRequestCallback : public ResourceLoaderBridge::Peer {
37 public:
38 TestRequestCallback() : complete_(false) {
39 }
40
[email protected]bb551622010-07-22 20:52:4941 virtual void OnUploadProgress(uint64 position, uint64 size) {
42 }
43
[email protected]6568a9e32009-07-30 18:01:3944 virtual bool OnReceivedRedirect(
45 const GURL& new_url,
[email protected]b4b3a912010-10-08 19:05:3746 const ResourceResponseInfo& info,
[email protected]041b0bbb2009-11-18 02:27:3447 bool* has_new_first_party_for_cookies,
[email protected]2581e572009-11-13 21:54:5548 GURL* new_first_party_for_cookies) {
[email protected]041b0bbb2009-11-18 02:27:3449 *has_new_first_party_for_cookies = false;
[email protected]6568a9e32009-07-30 18:01:3950 return true;
initial.commit09911bf2008-07-26 23:55:2951 }
52
[email protected]b9e8ea62011-03-04 06:29:0953 virtual void OnReceivedResponse(const ResourceResponseInfo& info) {
initial.commit09911bf2008-07-26 23:55:2954 }
55
[email protected]bb551622010-07-22 20:52:4956 virtual void OnDownloadedData(int len) {
57 }
58
[email protected]8bd0de72011-04-08 18:52:1059 virtual void OnReceivedData(const char* data,
60 int data_length,
61 int raw_data_length) {
initial.commit09911bf2008-07-26 23:55:2962 EXPECT_FALSE(complete_);
[email protected]8bd0de72011-04-08 18:52:1063 data_.append(data, data_length);
64 total_raw_data_length_ += raw_data_length;
initial.commit09911bf2008-07-26 23:55:2965 }
66
[email protected]f90bf0d92011-01-13 02:12:4467 virtual void OnCompletedRequest(const net::URLRequestStatus& status,
[email protected]e0f458c2010-09-16 04:50:4168 const std::string& security_info,
69 const base::Time& completion_time) {
initial.commit09911bf2008-07-26 23:55:2970 EXPECT_FALSE(complete_);
71 complete_ = true;
72 }
73
[email protected]8bd0de72011-04-08 18:52:1074 bool complete() const {
75 return complete_;
76 }
initial.commit09911bf2008-07-26 23:55:2977 const std::string& data() const {
78 return data_;
79 }
[email protected]8bd0de72011-04-08 18:52:1080 int total_raw_data_length() const {
81 return total_raw_data_length_;
initial.commit09911bf2008-07-26 23:55:2982 }
83
84 private:
85 bool complete_;
86 std::string data_;
[email protected]8bd0de72011-04-08 18:52:1087 int total_raw_data_length_;
initial.commit09911bf2008-07-26 23:55:2988};
89
90
91// Sets up the message sender override for the unit test
92class ResourceDispatcherTest : public testing::Test,
93 public IPC::Message::Sender {
94 public:
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));
99 delete msg;
100 return true;
101 }
102
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()) {
initial.commit09911bf2008-07-26 23:55:29107 int request_id;
[email protected]94dc971d2011-03-05 19:08:32108 ResourceHostMsg_Request request;
109 ASSERT_TRUE(ResourceHostMsg_RequestResource::Read(
[email protected]eb9989092009-03-12 21:42:52110 &message_queue_[0], &request_id, &request));
initial.commit09911bf2008-07-26 23:55:29111
112 // check values
113 EXPECT_EQ(test_page_url, request.url.spec());
114
115 // received response message
[email protected]e09ba552009-02-05 03:26:29116 ResourceResponseHead response;
initial.commit09911bf2008-07-26 23:55:29117 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;
initial.commit09911bf2008-07-26 23:55:29122 dispatcher_->OnReceivedResponse(request_id, response);
123
124 // received data message with the test contents
[email protected]176aa482008-11-14 03:25:15125 base::SharedMemory shared_mem;
[email protected]54e3dfa22010-10-27 18:16:06126 EXPECT_TRUE(shared_mem.CreateAndMapAnonymous(test_page_contents_len));
initial.commit09911bf2008-07-26 23:55:29127 char* put_data_here = static_cast<char*>(shared_mem.memory());
128 memcpy(put_data_here, test_page_contents, test_page_contents_len);
[email protected]176aa482008-11-14 03:25:15129 base::SharedMemoryHandle dup_handle;
[email protected]4003d7142009-01-12 12:56:20130 EXPECT_TRUE(shared_mem.GiveToProcess(
131 base::Process::Current().handle(), &dup_handle));
[email protected]eb9989092009-03-12 21:42:52132 dispatcher_->OnReceivedData(
[email protected]8bd0de72011-04-08 18:52:10133 message_queue_[0],
134 request_id,
135 dup_handle,
136 test_page_contents_len,
137 test_page_contents_len);
initial.commit09911bf2008-07-26 23:55:29138
139 message_queue_.erase(message_queue_.begin());
140
141 // read the ack message.
[email protected]c2fe31542009-05-20 18:24:14142 Tuple1<int> request_ack;
[email protected]94dc971d2011-03-05 19:08:32143 ASSERT_TRUE(ResourceHostMsg_DataReceived_ACK::Read(
[email protected]eb9989092009-03-12 21:42:52144 &message_queue_[0], &request_ack));
initial.commit09911bf2008-07-26 23:55:29145
[email protected]c2fe31542009-05-20 18:24:14146 ASSERT_EQ(request_ack.a, request_id);
initial.commit09911bf2008-07-26 23:55:29147
148 message_queue_.erase(message_queue_.begin());
149 }
150 }
151
152 protected:
initial.commit09911bf2008-07-26 23:55:29153 // testing::Test
154 virtual void SetUp() {
[email protected]eb9989092009-03-12 21:42:52155 dispatcher_.reset(new ResourceDispatcher(this));
initial.commit09911bf2008-07-26 23:55:29156 }
157 virtual void TearDown() {
[email protected]eb9989092009-03-12 21:42:52158 dispatcher_.reset();
initial.commit09911bf2008-07-26 23:55:29159 }
160
[email protected]2602087e2009-08-24 23:12:16161 ResourceLoaderBridge* CreateBridge() {
[email protected]46b0d4a2009-12-19 00:46:33162 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();
[email protected]46b0d4a2009-12-19 00:46:33167 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;
[email protected]52bbcd932010-01-06 18:56:12171 request_info.appcache_host_id = appcache::kNoHostId;
172 request_info.routing_id = 0;
[email protected]46b0d4a2009-12-19 00:46:33173
[email protected]50285ff2011-03-11 23:10:56174 return dispatcher_->CreateBridge(request_info);
[email protected]2602087e2009-08-24 23:12:16175 }
176
initial.commit09911bf2008-07-26 23:55:29177 std::vector<IPC::Message> message_queue_;
[email protected]eb9989092009-03-12 21:42:52178 static scoped_ptr<ResourceDispatcher> dispatcher_;
initial.commit09911bf2008-07-26 23:55:29179};
180
181/*static*/
[email protected]eb9989092009-03-12 21:42:52182scoped_ptr<ResourceDispatcher> ResourceDispatcherTest::dispatcher_;
initial.commit09911bf2008-07-26 23:55:29183
184// Does a simple request and tests that the correct data is received.
185TEST_F(ResourceDispatcherTest, RoundTrip) {
186 TestRequestCallback callback;
[email protected]2602087e2009-08-24 23:12:16187 ResourceLoaderBridge* bridge = CreateBridge();
initial.commit09911bf2008-07-26 23:55:29188
189 bridge->Start(&callback);
190
191 ProcessMessages();
192
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());
[email protected]8bd0de72011-04-08 18:52:10197 //EXPECT_EQ(test_page_contents_len, callback.total_raw_data_length());
initial.commit09911bf2008-07-26 23:55:29198
199 delete bridge;
200}
201
202// Tests that the request IDs are straight when there are multiple requests.
203TEST_F(ResourceDispatcherTest, MultipleRequests) {
204 // FIXME
205}
206
207// Tests that the cancel method prevents other messages from being received
208TEST_F(ResourceDispatcherTest, Cancel) {
209 // FIXME
210}
211
212TEST_F(ResourceDispatcherTest, Cookies) {
213 // FIXME
214}
215
216TEST_F(ResourceDispatcherTest, SerializedPostData) {
217 // FIXME
218}
[email protected]2602087e2009-08-24 23:12:16219
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.
225class DeferredResourceLoadingTest : public ResourceDispatcherTest,
226 public ResourceLoaderBridge::Peer {
227 public:
228 DeferredResourceLoadingTest()
229 : defer_loading_(false) {
230 }
231
232 virtual bool Send(IPC::Message* msg) {
233 delete msg;
234 return true;
235 }
236
237 void InitMessages() {
238 set_defer_loading(true);
239
240 ResourceResponseHead response_head;
[email protected]f90bf0d92011-01-13 02:12:44241 response_head.status.set_status(net::URLRequestStatus::SUCCESS);
[email protected]2602087e2009-08-24 23:12:16242
243 IPC::Message* response_message =
[email protected]94dc971d2011-03-05 19:08:32244 new ResourceMsg_ReceivedResponse(0, 0, response_head);
[email protected]2602087e2009-08-24 23:12:16245
246 dispatcher_->OnMessageReceived(*response_message);
247
248 delete response_message;
249
[email protected]a68114f72009-11-30 23:32:49250 // Duplicate the shared memory handle so both the test and the callee can
251 // close their copy.
252 base::SharedMemoryHandle duplicated_handle;
253 EXPECT_TRUE(shared_handle_.ShareToProcess(base::GetCurrentProcessHandle(),
254 &duplicated_handle));
255
[email protected]2602087e2009-08-24 23:12:16256 response_message =
[email protected]8bd0de72011-04-08 18:52:10257 new ResourceMsg_DataReceived(0, 0, duplicated_handle, 100, 100);
[email protected]2602087e2009-08-24 23:12:16258
259 dispatcher_->OnMessageReceived(*response_message);
260
261 delete response_message;
262
263 set_defer_loading(false);
264 }
265
266 // ResourceLoaderBridge::Peer methods.
[email protected]bb551622010-07-22 20:52:49267 virtual void OnUploadProgress(uint64 position, uint64 size) {
[email protected]2602087e2009-08-24 23:12:16268 }
269
270 virtual bool OnReceivedRedirect(
271 const GURL& new_url,
[email protected]b4b3a912010-10-08 19:05:37272 const ResourceResponseInfo& info,
[email protected]041b0bbb2009-11-18 02:27:34273 bool* has_new_first_party_for_cookies,
[email protected]2581e572009-11-13 21:54:55274 GURL* new_first_party_for_cookies) {
[email protected]041b0bbb2009-11-18 02:27:34275 *has_new_first_party_for_cookies = false;
[email protected]2602087e2009-08-24 23:12:16276 return true;
277 }
278
[email protected]b9e8ea62011-03-04 06:29:09279 virtual void OnReceivedResponse(const ResourceResponseInfo& info) {
[email protected]bb551622010-07-22 20:52:49280 EXPECT_EQ(defer_loading_, false);
281 set_defer_loading(true);
282 }
283
284 virtual void OnDownloadedData(int len) {
285 }
286
[email protected]8bd0de72011-04-08 18:52:10287 virtual void OnReceivedData(const char* data,
288 int data_length,
289 int raw_data_length) {
[email protected]2602087e2009-08-24 23:12:16290 EXPECT_EQ(defer_loading_, false);
291 set_defer_loading(false);
292 }
293
[email protected]f90bf0d92011-01-13 02:12:44294 virtual void OnCompletedRequest(const net::URLRequestStatus& status,
[email protected]e0f458c2010-09-16 04:50:41295 const std::string& security_info,
296 const base::Time& completion_time) {
[email protected]2602087e2009-08-24 23:12:16297 }
298
[email protected]2602087e2009-08-24 23:12:16299 protected:
300 virtual void SetUp() {
[email protected]2602087e2009-08-24 23:12:16301 ResourceDispatcherTest::SetUp();
[email protected]b8901082010-11-12 01:14:28302 shared_handle_.Delete(kShmemSegmentName);
[email protected]515f2492011-01-14 10:36:28303 EXPECT_TRUE(shared_handle_.CreateNamed(kShmemSegmentName, false, 100));
[email protected]2602087e2009-08-24 23:12:16304 }
305
306 virtual void TearDown() {
307 shared_handle_.Close();
[email protected]b8901082010-11-12 01:14:28308 EXPECT_TRUE(shared_handle_.Delete(kShmemSegmentName));
[email protected]2602087e2009-08-24 23:12:16309 ResourceDispatcherTest::TearDown();
310 }
311
312 private:
313 void set_defer_loading(bool defer) {
314 defer_loading_ = defer;
315 dispatcher_->SetDefersLoading(0, defer);
316 }
317
318 bool defer_loading() const {
319 return defer_loading_;
320 }
321
322 bool defer_loading_;
323 base::SharedMemory shared_handle_;
324};
325
326TEST_F(DeferredResourceLoadingTest, DeferredLoadTest) {
327 MessageLoop message_loop(MessageLoop::TYPE_IO);
328
329 ResourceLoaderBridge* bridge = CreateBridge();
330
331 bridge->Start(this);
332 InitMessages();
333
334 // Dispatch deferred messages.
335 message_loop.RunAllPending();
336 delete bridge;
337}