[email protected] | 8074478 | 2012-05-04 01:47:00 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 8 | #include "base/memory/scoped_ptr.h" |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 9 | #include "base/message_loop.h" |
[email protected] | 4003d714 | 2009-01-12 12:56:20 | [diff] [blame] | 10 | #include "base/process.h" |
[email protected] | a68114f7 | 2009-11-30 23:32:49 | [diff] [blame] | 11 | #include "base/process_util.h" |
[email protected] | 940895b | 2011-08-20 00:50:05 | [diff] [blame] | 12 | #include "content/common/request_extra_data.h" |
[email protected] | 620161e | 2011-03-07 18:05:26 | [diff] [blame] | 13 | #include "content/common/resource_dispatcher.h" |
[email protected] | 94dc971d | 2011-03-05 19:08:32 | [diff] [blame] | 14 | #include "content/common/resource_messages.h" |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 15 | #include "content/public/common/resource_response.h" |
[email protected] | 939856a | 2010-08-24 20:29:02 | [diff] [blame] | 16 | #include "net/base/upload_data.h" |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 17 | #include "net/http/http_response_headers.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | f430b571 | 2009-08-21 21:46:31 | [diff] [blame] | 19 | #include "webkit/appcache/appcache_interfaces.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | |
| 21 | using webkit_glue::ResourceLoaderBridge; |
[email protected] | b4b3a91 | 2010-10-08 19:05:37 | [diff] [blame] | 22 | using webkit_glue::ResourceResponseInfo; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | |
[email protected] | be7b41e8 | 2012-07-04 09:46:51 | [diff] [blame] | 24 | namespace content { |
| 25 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | static const char test_page_url[] = "http://www.google.com/"; |
| 27 | static const char test_page_headers[] = |
| 28 | "HTTP/1.1 200 OK\nContent-Type:text/html\n\n"; |
| 29 | static const char test_page_mime_type[] = "text/html"; |
| 30 | static const char test_page_charset[] = ""; |
| 31 | static const char test_page_contents[] = |
| 32 | "<html><head><title>Google</title></head><body><h1>Google</h1></body></html>"; |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 33 | static const uint32 test_page_contents_len = arraysize(test_page_contents) - 1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | |
[email protected] | b890108 | 2010-11-12 01:14:28 | [diff] [blame] | 35 | static const char kShmemSegmentName[] = "DeferredResourceLoaderTest"; |
| 36 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | // Listens for request response data and stores it so that it can be compared |
| 38 | // to the reference data. |
| 39 | class TestRequestCallback : public ResourceLoaderBridge::Peer { |
| 40 | public: |
| 41 | TestRequestCallback() : complete_(false) { |
| 42 | } |
| 43 | |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 44 | virtual void OnUploadProgress(uint64 position, uint64 size) OVERRIDE { |
[email protected] | bb55162 | 2010-07-22 20:52:49 | [diff] [blame] | 45 | } |
| 46 | |
[email protected] | 6568a9e3 | 2009-07-30 18:01:39 | [diff] [blame] | 47 | virtual bool OnReceivedRedirect( |
| 48 | const GURL& new_url, |
[email protected] | b4b3a91 | 2010-10-08 19:05:37 | [diff] [blame] | 49 | const ResourceResponseInfo& info, |
[email protected] | 041b0bbb | 2009-11-18 02:27:34 | [diff] [blame] | 50 | bool* has_new_first_party_for_cookies, |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 51 | GURL* new_first_party_for_cookies) OVERRIDE { |
[email protected] | 041b0bbb | 2009-11-18 02:27:34 | [diff] [blame] | 52 | *has_new_first_party_for_cookies = false; |
[email protected] | 6568a9e3 | 2009-07-30 18:01:39 | [diff] [blame] | 53 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 56 | virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 59 | virtual void OnDownloadedData(int len) OVERRIDE { |
[email protected] | bb55162 | 2010-07-22 20:52:49 | [diff] [blame] | 60 | } |
| 61 | |
[email protected] | 8bd0de7 | 2011-04-08 18:52:10 | [diff] [blame] | 62 | virtual void OnReceivedData(const char* data, |
| 63 | int data_length, |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 64 | int encoded_data_length) OVERRIDE { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | EXPECT_FALSE(complete_); |
[email protected] | 8bd0de7 | 2011-04-08 18:52:10 | [diff] [blame] | 66 | data_.append(data, data_length); |
[email protected] | dfd68217 | 2011-04-13 19:57:25 | [diff] [blame] | 67 | total_encoded_data_length_ += encoded_data_length; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | } |
| 69 | |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 70 | virtual void OnCompletedRequest( |
| 71 | const net::URLRequestStatus& status, |
| 72 | const std::string& security_info, |
| 73 | const base::TimeTicks& completion_time) OVERRIDE { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | EXPECT_FALSE(complete_); |
| 75 | complete_ = true; |
| 76 | } |
| 77 | |
[email protected] | 8bd0de7 | 2011-04-08 18:52:10 | [diff] [blame] | 78 | bool complete() const { |
| 79 | return complete_; |
| 80 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | const std::string& data() const { |
| 82 | return data_; |
| 83 | } |
[email protected] | dfd68217 | 2011-04-13 19:57:25 | [diff] [blame] | 84 | int total_encoded_data_length() const { |
| 85 | return total_encoded_data_length_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | private: |
| 89 | bool complete_; |
| 90 | std::string data_; |
[email protected] | dfd68217 | 2011-04-13 19:57:25 | [diff] [blame] | 91 | int total_encoded_data_length_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | |
| 95 | // Sets up the message sender override for the unit test |
[email protected] | d84effeb | 2012-06-25 17:03:10 | [diff] [blame] | 96 | class ResourceDispatcherTest : public testing::Test, public IPC::Sender { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | public: |
[email protected] | d84effeb | 2012-06-25 17:03:10 | [diff] [blame] | 98 | // Emulates IPC send operations (IPC::Sender) by adding |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 99 | // pending messages to the queue. |
| 100 | virtual bool Send(IPC::Message* msg) { |
| 101 | message_queue_.push_back(IPC::Message(*msg)); |
| 102 | delete msg; |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | // Emulates the browser process and processes the pending IPC messages, |
| 107 | // returning the hardcoded file contents. |
| 108 | void ProcessMessages() { |
| 109 | while (!message_queue_.empty()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | int request_id; |
[email protected] | 94dc971d | 2011-03-05 19:08:32 | [diff] [blame] | 111 | ResourceHostMsg_Request request; |
| 112 | ASSERT_TRUE(ResourceHostMsg_RequestResource::Read( |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 113 | &message_queue_[0], &request_id, &request)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 114 | |
| 115 | // check values |
| 116 | EXPECT_EQ(test_page_url, request.url.spec()); |
| 117 | |
| 118 | // received response message |
[email protected] | be7b41e8 | 2012-07-04 09:46:51 | [diff] [blame] | 119 | ResourceResponseHead response; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | std::string raw_headers(test_page_headers); |
| 121 | std::replace(raw_headers.begin(), raw_headers.end(), '\n', '\0'); |
| 122 | response.headers = new net::HttpResponseHeaders(raw_headers); |
| 123 | response.mime_type = test_page_mime_type; |
| 124 | response.charset = test_page_charset; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | dispatcher_->OnReceivedResponse(request_id, response); |
| 126 | |
| 127 | // received data message with the test contents |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 128 | base::SharedMemory shared_mem; |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 129 | EXPECT_TRUE(shared_mem.CreateAndMapAnonymous(test_page_contents_len)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 130 | char* put_data_here = static_cast<char*>(shared_mem.memory()); |
| 131 | memcpy(put_data_here, test_page_contents, test_page_contents_len); |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 132 | base::SharedMemoryHandle dup_handle; |
[email protected] | 4003d714 | 2009-01-12 12:56:20 | [diff] [blame] | 133 | EXPECT_TRUE(shared_mem.GiveToProcess( |
| 134 | base::Process::Current().handle(), &dup_handle)); |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 135 | dispatcher_->OnReceivedData( |
[email protected] | 8bd0de7 | 2011-04-08 18:52:10 | [diff] [blame] | 136 | message_queue_[0], |
| 137 | request_id, |
| 138 | dup_handle, |
| 139 | test_page_contents_len, |
| 140 | test_page_contents_len); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | |
| 142 | message_queue_.erase(message_queue_.begin()); |
| 143 | |
| 144 | // read the ack message. |
[email protected] | c2fe3154 | 2009-05-20 18:24:14 | [diff] [blame] | 145 | Tuple1<int> request_ack; |
[email protected] | 94dc971d | 2011-03-05 19:08:32 | [diff] [blame] | 146 | ASSERT_TRUE(ResourceHostMsg_DataReceived_ACK::Read( |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 147 | &message_queue_[0], &request_ack)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | |
[email protected] | c2fe3154 | 2009-05-20 18:24:14 | [diff] [blame] | 149 | ASSERT_EQ(request_ack.a, request_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | |
| 151 | message_queue_.erase(message_queue_.begin()); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | protected: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | // testing::Test |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 157 | virtual void SetUp() OVERRIDE { |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 158 | dispatcher_.reset(new ResourceDispatcher(this)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | } |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 160 | virtual void TearDown() OVERRIDE { |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 161 | dispatcher_.reset(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 164 | ResourceLoaderBridge* CreateBridge() { |
[email protected] | 46b0d4a | 2009-12-19 00:46:33 | [diff] [blame] | 165 | webkit_glue::ResourceLoaderBridge::RequestInfo request_info; |
| 166 | request_info.method = "GET"; |
| 167 | request_info.url = GURL(test_page_url); |
| 168 | request_info.first_party_for_cookies = GURL(test_page_url); |
| 169 | request_info.referrer = GURL(); |
[email protected] | 46b0d4a | 2009-12-19 00:46:33 | [diff] [blame] | 170 | request_info.headers = std::string(); |
| 171 | request_info.load_flags = 0; |
| 172 | request_info.requestor_pid = 0; |
| 173 | request_info.request_type = ResourceType::SUB_RESOURCE; |
[email protected] | 52bbcd93 | 2010-01-06 18:56:12 | [diff] [blame] | 174 | request_info.appcache_host_id = appcache::kNoHostId; |
| 175 | request_info.routing_id = 0; |
[email protected] | 537fbe0 | 2011-11-24 00:58:06 | [diff] [blame] | 176 | RequestExtraData extra_data(WebKit::WebReferrerPolicyDefault, |
[email protected] | e372f3d | 2012-07-17 19:16:44 | [diff] [blame] | 177 | WebKit::WebString(), |
[email protected] | 8074478 | 2012-05-04 01:47:00 | [diff] [blame] | 178 | true, 0, false, -1, true, |
[email protected] | be7b41e8 | 2012-07-04 09:46:51 | [diff] [blame] | 179 | PAGE_TRANSITION_LINK, -1, -1); |
[email protected] | 940895b | 2011-08-20 00:50:05 | [diff] [blame] | 180 | request_info.extra_data = &extra_data; |
[email protected] | 46b0d4a | 2009-12-19 00:46:33 | [diff] [blame] | 181 | |
[email protected] | 50285ff | 2011-03-11 23:10:56 | [diff] [blame] | 182 | return dispatcher_->CreateBridge(request_info); |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 183 | } |
| 184 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | std::vector<IPC::Message> message_queue_; |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 186 | static scoped_ptr<ResourceDispatcher> dispatcher_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | /*static*/ |
[email protected] | eb998909 | 2009-03-12 21:42:52 | [diff] [blame] | 190 | scoped_ptr<ResourceDispatcher> ResourceDispatcherTest::dispatcher_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 191 | |
| 192 | // Does a simple request and tests that the correct data is received. |
| 193 | TEST_F(ResourceDispatcherTest, RoundTrip) { |
| 194 | TestRequestCallback callback; |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 195 | ResourceLoaderBridge* bridge = CreateBridge(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 196 | |
| 197 | bridge->Start(&callback); |
| 198 | |
| 199 | ProcessMessages(); |
| 200 | |
| 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()); |
[email protected] | dfd68217 | 2011-04-13 19:57:25 | [diff] [blame] | 205 | //EXPECT_EQ(test_page_contents_len, callback.total_encoded_data_length()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | |
| 207 | delete bridge; |
| 208 | } |
| 209 | |
| 210 | // Tests that the request IDs are straight when there are multiple requests. |
| 211 | TEST_F(ResourceDispatcherTest, MultipleRequests) { |
| 212 | // FIXME |
| 213 | } |
| 214 | |
| 215 | // Tests that the cancel method prevents other messages from being received |
| 216 | TEST_F(ResourceDispatcherTest, Cancel) { |
| 217 | // FIXME |
| 218 | } |
| 219 | |
| 220 | TEST_F(ResourceDispatcherTest, Cookies) { |
| 221 | // FIXME |
| 222 | } |
| 223 | |
| 224 | TEST_F(ResourceDispatcherTest, SerializedPostData) { |
| 225 | // FIXME |
| 226 | } |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 227 | |
| 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 ResourceLoaderBridge::Peer { |
| 235 | public: |
| 236 | DeferredResourceLoadingTest() |
| 237 | : defer_loading_(false) { |
| 238 | } |
| 239 | |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 240 | virtual bool Send(IPC::Message* msg) OVERRIDE { |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 241 | delete msg; |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | void InitMessages() { |
| 246 | set_defer_loading(true); |
| 247 | |
[email protected] | be7b41e8 | 2012-07-04 09:46:51 | [diff] [blame] | 248 | ResourceResponseHead response_head; |
[email protected] | f90bf0d9 | 2011-01-13 02:12:44 | [diff] [blame] | 249 | response_head.status.set_status(net::URLRequestStatus::SUCCESS); |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 250 | |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 251 | dispatcher_->OnMessageReceived( |
| 252 | ResourceMsg_ReceivedResponse(0, 0, response_head)); |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 253 | |
[email protected] | a68114f7 | 2009-11-30 23:32:49 | [diff] [blame] | 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)); |
| 259 | |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 260 | dispatcher_->OnMessageReceived( |
| 261 | ResourceMsg_DataReceived(0, 0, duplicated_handle, 100, 100)); |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 262 | |
| 263 | set_defer_loading(false); |
| 264 | } |
| 265 | |
| 266 | // ResourceLoaderBridge::Peer methods. |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 267 | virtual void OnUploadProgress(uint64 position, uint64 size) OVERRIDE { |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | virtual bool OnReceivedRedirect( |
| 271 | const GURL& new_url, |
[email protected] | b4b3a91 | 2010-10-08 19:05:37 | [diff] [blame] | 272 | const ResourceResponseInfo& info, |
[email protected] | 041b0bbb | 2009-11-18 02:27:34 | [diff] [blame] | 273 | bool* has_new_first_party_for_cookies, |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 274 | GURL* new_first_party_for_cookies) OVERRIDE { |
[email protected] | 041b0bbb | 2009-11-18 02:27:34 | [diff] [blame] | 275 | *has_new_first_party_for_cookies = false; |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 276 | return true; |
| 277 | } |
| 278 | |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 279 | virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE { |
[email protected] | bb55162 | 2010-07-22 20:52:49 | [diff] [blame] | 280 | EXPECT_EQ(defer_loading_, false); |
| 281 | set_defer_loading(true); |
| 282 | } |
| 283 | |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 284 | virtual void OnDownloadedData(int len) OVERRIDE { |
[email protected] | bb55162 | 2010-07-22 20:52:49 | [diff] [blame] | 285 | } |
| 286 | |
[email protected] | 8bd0de7 | 2011-04-08 18:52:10 | [diff] [blame] | 287 | virtual void OnReceivedData(const char* data, |
| 288 | int data_length, |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 289 | int encoded_data_length) OVERRIDE { |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 290 | EXPECT_EQ(defer_loading_, false); |
| 291 | set_defer_loading(false); |
| 292 | } |
| 293 | |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 294 | virtual void OnCompletedRequest( |
| 295 | const net::URLRequestStatus& status, |
| 296 | const std::string& security_info, |
| 297 | const base::TimeTicks& completion_time) OVERRIDE { |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 298 | } |
| 299 | |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 300 | protected: |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 301 | virtual void SetUp() OVERRIDE { |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 302 | ResourceDispatcherTest::SetUp(); |
[email protected] | b890108 | 2010-11-12 01:14:28 | [diff] [blame] | 303 | shared_handle_.Delete(kShmemSegmentName); |
[email protected] | 515f249 | 2011-01-14 10:36:28 | [diff] [blame] | 304 | EXPECT_TRUE(shared_handle_.CreateNamed(kShmemSegmentName, false, 100)); |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 305 | } |
| 306 | |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 307 | virtual void TearDown() OVERRIDE { |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 308 | shared_handle_.Close(); |
[email protected] | b890108 | 2010-11-12 01:14:28 | [diff] [blame] | 309 | EXPECT_TRUE(shared_handle_.Delete(kShmemSegmentName)); |
[email protected] | 2602087e | 2009-08-24 23:12:16 | [diff] [blame] | 310 | ResourceDispatcherTest::TearDown(); |
| 311 | } |
| 312 | |
| 313 | private: |
| 314 | void set_defer_loading(bool defer) { |
| 315 | defer_loading_ = defer; |
| 316 | dispatcher_->SetDefersLoading(0, defer); |
| 317 | } |
| 318 | |
| 319 | bool defer_loading() const { |
| 320 | return defer_loading_; |
| 321 | } |
| 322 | |
| 323 | bool defer_loading_; |
| 324 | base::SharedMemory shared_handle_; |
| 325 | }; |
| 326 | |
| 327 | TEST_F(DeferredResourceLoadingTest, DeferredLoadTest) { |
| 328 | MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 329 | |
| 330 | ResourceLoaderBridge* bridge = CreateBridge(); |
| 331 | |
| 332 | bridge->Start(this); |
| 333 | InitMessages(); |
| 334 | |
| 335 | // Dispatch deferred messages. |
| 336 | message_loop.RunAllPending(); |
| 337 | delete bridge; |
| 338 | } |
[email protected] | be7b41e8 | 2012-07-04 09:46:51 | [diff] [blame] | 339 | |
[email protected] | 04f6f98 | 2012-08-03 01:02:15 | [diff] [blame^] | 340 | class TimeConversionTest : public ResourceDispatcherTest, |
| 341 | public ResourceLoaderBridge::Peer { |
| 342 | public: |
| 343 | virtual bool Send(IPC::Message* msg) OVERRIDE { |
| 344 | delete msg; |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | void PerformTest(const ResourceResponseHead& response_head) { |
| 349 | scoped_ptr<ResourceLoaderBridge> bridge(CreateBridge()); |
| 350 | bridge->Start(this); |
| 351 | |
| 352 | dispatcher_->OnMessageReceived( |
| 353 | ResourceMsg_ReceivedResponse(0, 0, response_head)); |
| 354 | } |
| 355 | |
| 356 | // ResourceLoaderBridge::Peer methods. |
| 357 | virtual void OnUploadProgress(uint64 position, uint64 size) OVERRIDE { |
| 358 | } |
| 359 | |
| 360 | virtual bool OnReceivedRedirect( |
| 361 | const GURL& new_url, |
| 362 | const ResourceResponseInfo& info, |
| 363 | bool* has_new_first_party_for_cookies, |
| 364 | GURL* new_first_party_for_cookies) { |
| 365 | return true; |
| 366 | } |
| 367 | |
| 368 | virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE { |
| 369 | response_info_ = info; |
| 370 | } |
| 371 | |
| 372 | virtual void OnDownloadedData(int len) OVERRIDE { |
| 373 | } |
| 374 | |
| 375 | virtual void OnReceivedData(const char* data, |
| 376 | int data_length, |
| 377 | int encoded_data_length) OVERRIDE { |
| 378 | } |
| 379 | |
| 380 | virtual void OnCompletedRequest( |
| 381 | const net::URLRequestStatus& status, |
| 382 | const std::string& security_info, |
| 383 | const base::TimeTicks& completion_time) OVERRIDE { |
| 384 | } |
| 385 | |
| 386 | const ResourceResponseInfo& response_info() const { return response_info_; } |
| 387 | |
| 388 | private: |
| 389 | ResourceResponseInfo response_info_; |
| 390 | }; |
| 391 | |
| 392 | // TODO(simonjam): Enable this when 10829031 lands. |
| 393 | TEST_F(TimeConversionTest, DISABLED_ProperlyInitialized) { |
| 394 | ResourceResponseHead response_head; |
| 395 | response_head.status.set_status(net::URLRequestStatus::SUCCESS); |
| 396 | response_head.request_start = base::TimeTicks::FromInternalValue(5); |
| 397 | response_head.response_start = base::TimeTicks::FromInternalValue(15); |
| 398 | response_head.load_timing.base_time = base::Time::Now(); |
| 399 | response_head.load_timing.base_ticks = base::TimeTicks::FromInternalValue(10); |
| 400 | response_head.load_timing.dns_start = -1; |
| 401 | response_head.load_timing.connect_start = 3; |
| 402 | |
| 403 | PerformTest(response_head); |
| 404 | |
| 405 | EXPECT_LT(0, response_info().load_timing.base_ticks.ToInternalValue()); |
| 406 | EXPECT_EQ(-1, response_info().load_timing.dns_start); |
| 407 | EXPECT_LE(0, response_info().load_timing.connect_start); |
| 408 | } |
| 409 | |
| 410 | TEST_F(TimeConversionTest, PartiallyInitialized) { |
| 411 | ResourceResponseHead response_head; |
| 412 | response_head.status.set_status(net::URLRequestStatus::SUCCESS); |
| 413 | response_head.request_start = base::TimeTicks::FromInternalValue(5); |
| 414 | response_head.response_start = base::TimeTicks::FromInternalValue(15); |
| 415 | |
| 416 | PerformTest(response_head); |
| 417 | |
| 418 | EXPECT_EQ(0, response_info().load_timing.base_ticks.ToInternalValue()); |
| 419 | EXPECT_EQ(-1, response_info().load_timing.dns_start); |
| 420 | } |
| 421 | |
| 422 | TEST_F(TimeConversionTest, NotInitialized) { |
| 423 | ResourceResponseHead response_head; |
| 424 | response_head.status.set_status(net::URLRequestStatus::SUCCESS); |
| 425 | |
| 426 | PerformTest(response_head); |
| 427 | |
| 428 | EXPECT_EQ(0, response_info().load_timing.base_ticks.ToInternalValue()); |
| 429 | EXPECT_EQ(-1, response_info().load_timing.dns_start); |
| 430 | } |
| 431 | |
[email protected] | be7b41e8 | 2012-07-04 09:46:51 | [diff] [blame] | 432 | } // namespace content |