blob: a270be3eb2aeda6f267f9efd126b884297eaaca9 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294
5#include <string>
6#include <vector>
7
[email protected]4003d7142009-01-12 12:56:208#include "base/process.h"
[email protected]1fec8402009-03-13 19:11:599#include "base/scoped_ptr.h"
initial.commit09911bf2008-07-26 23:55:2910#include "chrome/common/filter_policy.h"
[email protected]e09ba552009-02-05 03:26:2911#include "chrome/common/render_messages.h"
initial.commit09911bf2008-07-26 23:55:2912#include "chrome/common/resource_dispatcher.h"
initial.commit09911bf2008-07-26 23:55:2913#include "testing/gtest/include/gtest/gtest.h"
[email protected]f430b5712009-08-21 21:46:3114#include "webkit/appcache/appcache_interfaces.h"
initial.commit09911bf2008-07-26 23:55:2915
16using webkit_glue::ResourceLoaderBridge;
17
18static const char test_page_url[] = "http://www.google.com/";
19static const char test_page_headers[] =
20 "HTTP/1.1 200 OK\nContent-Type:text/html\n\n";
21static const char test_page_mime_type[] = "text/html";
22static const char test_page_charset[] = "";
23static const char test_page_contents[] =
24 "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>";
25static const int test_page_contents_len = arraysize(test_page_contents) - 1;
26
27// Listens for request response data and stores it so that it can be compared
28// to the reference data.
29class TestRequestCallback : public ResourceLoaderBridge::Peer {
30 public:
31 TestRequestCallback() : complete_(false) {
32 }
33
[email protected]6568a9e32009-07-30 18:01:3934 virtual bool OnReceivedRedirect(
35 const GURL& new_url,
36 const ResourceLoaderBridge::ResponseInfo& info) {
37 return true;
initial.commit09911bf2008-07-26 23:55:2938 }
39
40 virtual void OnReceivedResponse(
[email protected]8a3422c92008-09-24 17:42:4241 const ResourceLoaderBridge::ResponseInfo& info,
42 bool content_filtered) {
initial.commit09911bf2008-07-26 23:55:2943 }
44
45 virtual void OnReceivedData(const char* data, int len) {
46 EXPECT_FALSE(complete_);
47 data_.append(data, len);
48 }
49
[email protected]89c7ed002009-03-12 19:54:5750 virtual void OnUploadProgress(uint64 position, uint64 size) {
51 }
52
[email protected]c4891b32009-03-08 07:41:3153 virtual void OnCompletedRequest(const URLRequestStatus& status,
54 const std::string& security_info) {
initial.commit09911bf2008-07-26 23:55:2955 EXPECT_FALSE(complete_);
56 complete_ = true;
57 }
58
59 virtual std::string GetURLForDebugging() {
60 return std::string();
61 }
62
63 const std::string& data() const {
64 return data_;
65 }
66 const bool complete() const {
67 return complete_;
68 }
69
70 private:
71 bool complete_;
72 std::string data_;
73};
74
75
76// Sets up the message sender override for the unit test
77class ResourceDispatcherTest : public testing::Test,
78 public IPC::Message::Sender {
79 public:
80 // Emulates IPC send operations (IPC::Message::Sender) by adding
81 // pending messages to the queue.
82 virtual bool Send(IPC::Message* msg) {
83 message_queue_.push_back(IPC::Message(*msg));
84 delete msg;
85 return true;
86 }
87
88 // Emulates the browser process and processes the pending IPC messages,
89 // returning the hardcoded file contents.
90 void ProcessMessages() {
91 while (!message_queue_.empty()) {
initial.commit09911bf2008-07-26 23:55:2992 int request_id;
initial.commit09911bf2008-07-26 23:55:2993 ViewHostMsg_Resource_Request request;
[email protected]eb9989092009-03-12 21:42:5294 ASSERT_TRUE(ViewHostMsg_RequestResource::Read(
95 &message_queue_[0], &request_id, &request));
initial.commit09911bf2008-07-26 23:55:2996
97 // check values
98 EXPECT_EQ(test_page_url, request.url.spec());
99
100 // received response message
[email protected]e09ba552009-02-05 03:26:29101 ResourceResponseHead response;
initial.commit09911bf2008-07-26 23:55:29102 std::string raw_headers(test_page_headers);
103 std::replace(raw_headers.begin(), raw_headers.end(), '\n', '\0');
104 response.headers = new net::HttpResponseHeaders(raw_headers);
105 response.mime_type = test_page_mime_type;
106 response.charset = test_page_charset;
107 response.filter_policy = FilterPolicy::DONT_FILTER;
108 dispatcher_->OnReceivedResponse(request_id, response);
109
110 // received data message with the test contents
[email protected]176aa482008-11-14 03:25:15111 base::SharedMemory shared_mem;
initial.commit09911bf2008-07-26 23:55:29112 EXPECT_TRUE(shared_mem.Create(std::wstring(),
113 false, false, test_page_contents_len));
114 EXPECT_TRUE(shared_mem.Map(test_page_contents_len));
115 char* put_data_here = static_cast<char*>(shared_mem.memory());
116 memcpy(put_data_here, test_page_contents, test_page_contents_len);
[email protected]176aa482008-11-14 03:25:15117 base::SharedMemoryHandle dup_handle;
[email protected]4003d7142009-01-12 12:56:20118 EXPECT_TRUE(shared_mem.GiveToProcess(
119 base::Process::Current().handle(), &dup_handle));
[email protected]eb9989092009-03-12 21:42:52120 dispatcher_->OnReceivedData(
121 message_queue_[0], request_id, dup_handle, test_page_contents_len);
initial.commit09911bf2008-07-26 23:55:29122
123 message_queue_.erase(message_queue_.begin());
124
125 // read the ack message.
[email protected]c2fe31542009-05-20 18:24:14126 Tuple1<int> request_ack;
[email protected]eb9989092009-03-12 21:42:52127 ASSERT_TRUE(ViewHostMsg_DataReceived_ACK::Read(
128 &message_queue_[0], &request_ack));
initial.commit09911bf2008-07-26 23:55:29129
[email protected]c2fe31542009-05-20 18:24:14130 ASSERT_EQ(request_ack.a, request_id);
initial.commit09911bf2008-07-26 23:55:29131
132 message_queue_.erase(message_queue_.begin());
133 }
134 }
135
136 protected:
initial.commit09911bf2008-07-26 23:55:29137 // testing::Test
138 virtual void SetUp() {
[email protected]eb9989092009-03-12 21:42:52139 dispatcher_.reset(new ResourceDispatcher(this));
initial.commit09911bf2008-07-26 23:55:29140 }
141 virtual void TearDown() {
[email protected]eb9989092009-03-12 21:42:52142 dispatcher_.reset();
initial.commit09911bf2008-07-26 23:55:29143 }
144
145 std::vector<IPC::Message> message_queue_;
[email protected]eb9989092009-03-12 21:42:52146 static scoped_ptr<ResourceDispatcher> dispatcher_;
initial.commit09911bf2008-07-26 23:55:29147};
148
149/*static*/
[email protected]eb9989092009-03-12 21:42:52150scoped_ptr<ResourceDispatcher> ResourceDispatcherTest::dispatcher_;
initial.commit09911bf2008-07-26 23:55:29151
152// Does a simple request and tests that the correct data is received.
153TEST_F(ResourceDispatcherTest, RoundTrip) {
154 TestRequestCallback callback;
155 ResourceLoaderBridge* bridge =
156 dispatcher_->CreateBridge("GET", GURL(test_page_url), GURL(test_page_url),
[email protected]1a528c12009-04-08 06:11:34157 GURL(), "null", "null", std::string(), 0, 0,
[email protected]c46b0e662009-03-17 09:18:06158 ResourceType::SUB_RESOURCE, 0,
[email protected]f430b5712009-08-21 21:46:31159 appcache::kNoHostId,
[email protected]eb9989092009-03-12 21:42:52160 MSG_ROUTING_CONTROL);
initial.commit09911bf2008-07-26 23:55:29161
162 bridge->Start(&callback);
163
164 ProcessMessages();
165
166 // FIXME(brettw) when the request complete messages are actually handledo
167 // and dispatched, uncomment this.
168 //EXPECT_TRUE(callback.complete());
169 //EXPECT_STREQ(test_page_contents, callback.data().c_str());
170
171 delete bridge;
172}
173
174// Tests that the request IDs are straight when there are multiple requests.
175TEST_F(ResourceDispatcherTest, MultipleRequests) {
176 // FIXME
177}
178
179// Tests that the cancel method prevents other messages from being received
180TEST_F(ResourceDispatcherTest, Cancel) {
181 // FIXME
182}
183
184TEST_F(ResourceDispatcherTest, Cookies) {
185 // FIXME
186}
187
188TEST_F(ResourceDispatcherTest, SerializedPostData) {
189 // FIXME
190}