license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include <string> |
| 6 | #include <vector> |
| 7 | |
[email protected] | 4003d714 | 2009-01-12 12:56:20 | [diff] [blame] | 8 | #include "base/process.h" |
[email protected] | 1fec840 | 2009-03-13 19:11:59 | [diff] [blame] | 9 | #include "base/scoped_ptr.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "chrome/common/filter_policy.h" |
[email protected] | e09ba55 | 2009-02-05 03:26:29 | [diff] [blame] | 11 | #include "chrome/common/render_messages.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "chrome/common/resource_dispatcher.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | f430b571 | 2009-08-21 21:46:31 | [diff] [blame^] | 14 | #include "webkit/appcache/appcache_interfaces.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | |
| 16 | using webkit_glue::ResourceLoaderBridge; |
| 17 | |
| 18 | static const char test_page_url[] = "http://www.google.com/"; |
| 19 | static const char test_page_headers[] = |
| 20 | "HTTP/1.1 200 OK\nContent-Type:text/html\n\n"; |
| 21 | static const char test_page_mime_type[] = "text/html"; |
| 22 | static const char test_page_charset[] = ""; |
| 23 | static const char test_page_contents[] = |
| 24 | "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>"; |
| 25 | static 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. |
| 29 | class TestRequestCallback : public ResourceLoaderBridge::Peer { |
| 30 | public: |
| 31 | TestRequestCallback() : complete_(false) { |
| 32 | } |
| 33 | |
[email protected] | 6568a9e3 | 2009-07-30 18:01:39 | [diff] [blame] | 34 | virtual bool OnReceivedRedirect( |
| 35 | const GURL& new_url, |
| 36 | const ResourceLoaderBridge::ResponseInfo& info) { |
| 37 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | virtual void OnReceivedResponse( |
[email protected] | 8a3422c9 | 2008-09-24 17:42:42 | [diff] [blame] | 41 | const ResourceLoaderBridge::ResponseInfo& info, |
| 42 | bool content_filtered) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | virtual void OnReceivedData(const char* data, int len) { |
| 46 | EXPECT_FALSE(complete_); |
| 47 | data_.append(data, len); |
| 48 | } |
| 49 | |
[email protected] | 89c7ed00 | 2009-03-12 19:54:57 | [diff] [blame] | 50 | virtual void OnUploadProgress(uint64 position, uint64 size) { |
| 51 | } |
| 52 | |
[email protected] | c4891b3 | 2009-03-08 07:41:31 | [diff] [blame] | 53 | virtual void OnCompletedRequest(const URLRequestStatus& status, |
| 54 | const std::string& security_info) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 55 | 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 |
| 77 | class 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | int request_id; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | ViewHostMsg_Resource_Request request; |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 94 | ASSERT_TRUE(ViewHostMsg_RequestResource::Read( |
| 95 | &message_queue_[0], &request_id, &request)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | |
| 97 | // check values |
| 98 | EXPECT_EQ(test_page_url, request.url.spec()); |
| 99 | |
| 100 | // received response message |
[email protected] | e09ba55 | 2009-02-05 03:26:29 | [diff] [blame] | 101 | ResourceResponseHead response; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 102 | 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] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 111 | base::SharedMemory shared_mem; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | 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] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 117 | base::SharedMemoryHandle dup_handle; |
[email protected] | 4003d714 | 2009-01-12 12:56:20 | [diff] [blame] | 118 | EXPECT_TRUE(shared_mem.GiveToProcess( |
| 119 | base::Process::Current().handle(), &dup_handle)); |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 120 | dispatcher_->OnReceivedData( |
| 121 | message_queue_[0], request_id, dup_handle, test_page_contents_len); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | |
| 123 | message_queue_.erase(message_queue_.begin()); |
| 124 | |
| 125 | // read the ack message. |
[email protected] | c2fe3154 | 2009-05-20 18:24:14 | [diff] [blame] | 126 | Tuple1<int> request_ack; |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 127 | ASSERT_TRUE(ViewHostMsg_DataReceived_ACK::Read( |
| 128 | &message_queue_[0], &request_ack)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | |
[email protected] | c2fe3154 | 2009-05-20 18:24:14 | [diff] [blame] | 130 | ASSERT_EQ(request_ack.a, request_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | |
| 132 | message_queue_.erase(message_queue_.begin()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | protected: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 137 | // testing::Test |
| 138 | virtual void SetUp() { |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 139 | dispatcher_.reset(new ResourceDispatcher(this)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | } |
| 141 | virtual void TearDown() { |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 142 | dispatcher_.reset(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | std::vector<IPC::Message> message_queue_; |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 146 | static scoped_ptr<ResourceDispatcher> dispatcher_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | /*static*/ |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 150 | scoped_ptr<ResourceDispatcher> ResourceDispatcherTest::dispatcher_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 151 | |
| 152 | // Does a simple request and tests that the correct data is received. |
| 153 | TEST_F(ResourceDispatcherTest, RoundTrip) { |
| 154 | TestRequestCallback callback; |
| 155 | ResourceLoaderBridge* bridge = |
| 156 | dispatcher_->CreateBridge("GET", GURL(test_page_url), GURL(test_page_url), |
[email protected] | 1a528c1 | 2009-04-08 06:11:34 | [diff] [blame] | 157 | GURL(), "null", "null", std::string(), 0, 0, |
[email protected] | c46b0e66 | 2009-03-17 09:18:06 | [diff] [blame] | 158 | ResourceType::SUB_RESOURCE, 0, |
[email protected] | f430b571 | 2009-08-21 21:46:31 | [diff] [blame^] | 159 | appcache::kNoHostId, |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 160 | MSG_ROUTING_CONTROL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 161 | |
| 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. |
| 175 | TEST_F(ResourceDispatcherTest, MultipleRequests) { |
| 176 | // FIXME |
| 177 | } |
| 178 | |
| 179 | // Tests that the cancel method prevents other messages from being received |
| 180 | TEST_F(ResourceDispatcherTest, Cancel) { |
| 181 | // FIXME |
| 182 | } |
| 183 | |
| 184 | TEST_F(ResourceDispatcherTest, Cookies) { |
| 185 | // FIXME |
| 186 | } |
| 187 | |
| 188 | TEST_F(ResourceDispatcherTest, SerializedPostData) { |
| 189 | // FIXME |
| 190 | } |