OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/process.h" | 10 #include "base/process.h" |
(...skipping 23 matching lines...) Expand all Loading... | |
34 | 34 |
35 static const char kShmemSegmentName[] = "DeferredResourceLoaderTest"; | 35 static const char kShmemSegmentName[] = "DeferredResourceLoaderTest"; |
36 | 36 |
37 // Listens for request response data and stores it so that it can be compared | 37 // Listens for request response data and stores it so that it can be compared |
38 // to the reference data. | 38 // to the reference data. |
39 class TestRequestCallback : public ResourceLoaderBridge::Peer { | 39 class TestRequestCallback : public ResourceLoaderBridge::Peer { |
40 public: | 40 public: |
41 TestRequestCallback() : complete_(false) { | 41 TestRequestCallback() : complete_(false) { |
42 } | 42 } |
43 | 43 |
44 virtual void OnUploadProgress(uint64 position, uint64 size) { | 44 virtual void OnUploadProgress(uint64 position, uint64 size) OVERRIDE { |
45 } | 45 } |
46 | 46 |
47 virtual bool OnReceivedRedirect( | 47 virtual bool OnReceivedRedirect( |
48 const GURL& new_url, | 48 const GURL& new_url, |
49 const ResourceResponseInfo& info, | 49 const ResourceResponseInfo& info, |
50 bool* has_new_first_party_for_cookies, | 50 bool* has_new_first_party_for_cookies, |
51 GURL* new_first_party_for_cookies) { | 51 GURL* new_first_party_for_cookies) OVERRIDE { |
52 *has_new_first_party_for_cookies = false; | 52 *has_new_first_party_for_cookies = false; |
53 return true; | 53 return true; |
54 } | 54 } |
55 | 55 |
56 virtual void OnReceivedResponse(const ResourceResponseInfo& info) { | 56 virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE { |
57 } | 57 } |
58 | 58 |
59 virtual void OnDownloadedData(int len) { | 59 virtual void OnDownloadedData(int len) OVERRIDE { |
60 } | 60 } |
61 | 61 |
62 virtual void OnReceivedData(const char* data, | 62 virtual void OnReceivedData(const char* data, |
63 int data_length, | 63 int data_length, |
64 int encoded_data_length) { | 64 int encoded_data_length) OVERRIDE { |
65 EXPECT_FALSE(complete_); | 65 EXPECT_FALSE(complete_); |
66 data_.append(data, data_length); | 66 data_.append(data, data_length); |
67 total_encoded_data_length_ += encoded_data_length; | 67 total_encoded_data_length_ += encoded_data_length; |
68 } | 68 } |
69 | 69 |
70 virtual void OnCompletedRequest(const net::URLRequestStatus& status, | 70 virtual void OnCompletedRequest( |
71 const std::string& security_info, | 71 const net::URLRequestStatus& status, |
72 const base::TimeTicks& completion_time) { | 72 const std::string& security_info, |
73 const base::TimeTicks& completion_time) OVERRIDE { | |
73 EXPECT_FALSE(complete_); | 74 EXPECT_FALSE(complete_); |
74 complete_ = true; | 75 complete_ = true; |
75 } | 76 } |
76 | 77 |
77 bool complete() const { | 78 bool complete() const { |
78 return complete_; | 79 return complete_; |
79 } | 80 } |
80 const std::string& data() const { | 81 const std::string& data() const { |
81 return data_; | 82 return data_; |
82 } | 83 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 &message_queue_[0], &request_ack)); | 147 &message_queue_[0], &request_ack)); |
147 | 148 |
148 ASSERT_EQ(request_ack.a, request_id); | 149 ASSERT_EQ(request_ack.a, request_id); |
149 | 150 |
150 message_queue_.erase(message_queue_.begin()); | 151 message_queue_.erase(message_queue_.begin()); |
151 } | 152 } |
152 } | 153 } |
153 | 154 |
154 protected: | 155 protected: |
155 // testing::Test | 156 // testing::Test |
156 virtual void SetUp() { | 157 virtual void SetUp() OVERRIDE { |
157 dispatcher_.reset(new ResourceDispatcher(this)); | 158 dispatcher_.reset(new ResourceDispatcher(this)); |
158 } | 159 } |
159 virtual void TearDown() { | 160 virtual void TearDown() OVERRIDE { |
160 dispatcher_.reset(); | 161 dispatcher_.reset(); |
161 } | 162 } |
162 | 163 |
163 ResourceLoaderBridge* CreateBridge() { | 164 ResourceLoaderBridge* CreateBridge() { |
164 webkit_glue::ResourceLoaderBridge::RequestInfo request_info; | 165 webkit_glue::ResourceLoaderBridge::RequestInfo request_info; |
165 request_info.method = "GET"; | 166 request_info.method = "GET"; |
166 request_info.url = GURL(test_page_url); | 167 request_info.url = GURL(test_page_url); |
167 request_info.first_party_for_cookies = GURL(test_page_url); | 168 request_info.first_party_for_cookies = GURL(test_page_url); |
168 request_info.referrer = GURL(); | 169 request_info.referrer = GURL(); |
169 request_info.headers = std::string(); | 170 request_info.headers = std::string(); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
229 // loading is enabled it should queue up any responses received. If deferred | 230 // loading is enabled it should queue up any responses received. If deferred |
230 // loading is enabled/disabled in the context of a dispatched message, other | 231 // loading is enabled/disabled in the context of a dispatched message, other |
231 // queued messages should not be dispatched until deferred load is turned off. | 232 // queued messages should not be dispatched until deferred load is turned off. |
232 class DeferredResourceLoadingTest : public ResourceDispatcherTest, | 233 class DeferredResourceLoadingTest : public ResourceDispatcherTest, |
233 public ResourceLoaderBridge::Peer { | 234 public ResourceLoaderBridge::Peer { |
234 public: | 235 public: |
235 DeferredResourceLoadingTest() | 236 DeferredResourceLoadingTest() |
236 : defer_loading_(false) { | 237 : defer_loading_(false) { |
237 } | 238 } |
238 | 239 |
239 virtual bool Send(IPC::Message* msg) { | 240 virtual bool Send(IPC::Message* msg) OVERRIDE { |
240 delete msg; | 241 delete msg; |
241 return true; | 242 return true; |
242 } | 243 } |
243 | 244 |
244 void InitMessages() { | 245 void InitMessages() { |
245 set_defer_loading(true); | 246 set_defer_loading(true); |
246 | 247 |
247 ResourceResponseHead response_head; | 248 ResourceResponseHead response_head; |
248 response_head.status.set_status(net::URLRequestStatus::SUCCESS); | 249 response_head.status.set_status(net::URLRequestStatus::SUCCESS); |
249 | 250 |
250 IPC::Message* response_message = | 251 ResourceMsg_ReceivedResponse response_message(0, 0, response_head); |
251 new ResourceMsg_ReceivedResponse(0, 0, response_head); | 252 dispatcher_->OnMessageReceived(response_message); |
252 | |
253 dispatcher_->OnMessageReceived(*response_message); | |
254 | |
255 delete response_message; | |
256 | 253 |
257 // Duplicate the shared memory handle so both the test and the callee can | 254 // Duplicate the shared memory handle so both the test and the callee can |
258 // close their copy. | 255 // close their copy. |
259 base::SharedMemoryHandle duplicated_handle; | 256 base::SharedMemoryHandle duplicated_handle; |
260 EXPECT_TRUE(shared_handle_.ShareToProcess(base::GetCurrentProcessHandle(), | 257 EXPECT_TRUE(shared_handle_.ShareToProcess(base::GetCurrentProcessHandle(), |
261 &duplicated_handle)); | 258 &duplicated_handle)); |
262 | 259 |
263 response_message = | 260 ResourceMsg_DataReceived data_message(0, 0, duplicated_handle, 100, 100); |
264 new ResourceMsg_DataReceived(0, 0, duplicated_handle, 100, 100); | 261 dispatcher_->OnMessageReceived(response_message); |
darin (slow to review)
2012/08/02 20:30:04
typo? looks like you need to pass data_message in
James Simonsen
2012/08/02 21:29:32
Done.
| |
265 | |
266 dispatcher_->OnMessageReceived(*response_message); | |
267 | |
268 delete response_message; | |
269 | 262 |
270 set_defer_loading(false); | 263 set_defer_loading(false); |
271 } | 264 } |
272 | 265 |
273 // ResourceLoaderBridge::Peer methods. | 266 // ResourceLoaderBridge::Peer methods. |
274 virtual void OnUploadProgress(uint64 position, uint64 size) { | 267 virtual void OnUploadProgress(uint64 position, uint64 size) OVERRIDE { |
275 } | 268 } |
276 | 269 |
277 virtual bool OnReceivedRedirect( | 270 virtual bool OnReceivedRedirect( |
278 const GURL& new_url, | 271 const GURL& new_url, |
279 const ResourceResponseInfo& info, | 272 const ResourceResponseInfo& info, |
280 bool* has_new_first_party_for_cookies, | 273 bool* has_new_first_party_for_cookies, |
281 GURL* new_first_party_for_cookies) { | 274 GURL* new_first_party_for_cookies) OVERRIDE { |
282 *has_new_first_party_for_cookies = false; | 275 *has_new_first_party_for_cookies = false; |
283 return true; | 276 return true; |
284 } | 277 } |
285 | 278 |
286 virtual void OnReceivedResponse(const ResourceResponseInfo& info) { | 279 virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE { |
287 EXPECT_EQ(defer_loading_, false); | 280 EXPECT_EQ(defer_loading_, false); |
288 set_defer_loading(true); | 281 set_defer_loading(true); |
289 } | 282 } |
290 | 283 |
291 virtual void OnDownloadedData(int len) { | 284 virtual void OnDownloadedData(int len) OVERRIDE { |
292 } | 285 } |
293 | 286 |
294 virtual void OnReceivedData(const char* data, | 287 virtual void OnReceivedData(const char* data, |
295 int data_length, | 288 int data_length, |
296 int encoded_data_length) { | 289 int encoded_data_length) OVERRIDE { |
297 EXPECT_EQ(defer_loading_, false); | 290 EXPECT_EQ(defer_loading_, false); |
298 set_defer_loading(false); | 291 set_defer_loading(false); |
299 } | 292 } |
300 | 293 |
301 virtual void OnCompletedRequest(const net::URLRequestStatus& status, | 294 virtual void OnCompletedRequest( |
302 const std::string& security_info, | 295 const net::URLRequestStatus& status, |
303 const base::TimeTicks& completion_time) { | 296 const std::string& security_info, |
297 const base::TimeTicks& completion_time) OVERRIDE { | |
304 } | 298 } |
305 | 299 |
306 protected: | 300 protected: |
307 virtual void SetUp() { | 301 virtual void SetUp() OVERRIDE { |
308 ResourceDispatcherTest::SetUp(); | 302 ResourceDispatcherTest::SetUp(); |
309 shared_handle_.Delete(kShmemSegmentName); | 303 shared_handle_.Delete(kShmemSegmentName); |
310 EXPECT_TRUE(shared_handle_.CreateNamed(kShmemSegmentName, false, 100)); | 304 EXPECT_TRUE(shared_handle_.CreateNamed(kShmemSegmentName, false, 100)); |
311 } | 305 } |
312 | 306 |
313 virtual void TearDown() { | 307 virtual void TearDown() OVERRIDE { |
314 shared_handle_.Close(); | 308 shared_handle_.Close(); |
315 EXPECT_TRUE(shared_handle_.Delete(kShmemSegmentName)); | 309 EXPECT_TRUE(shared_handle_.Delete(kShmemSegmentName)); |
316 ResourceDispatcherTest::TearDown(); | 310 ResourceDispatcherTest::TearDown(); |
317 } | 311 } |
318 | 312 |
319 private: | 313 private: |
320 void set_defer_loading(bool defer) { | 314 void set_defer_loading(bool defer) { |
321 defer_loading_ = defer; | 315 defer_loading_ = defer; |
322 dispatcher_->SetDefersLoading(0, defer); | 316 dispatcher_->SetDefersLoading(0, defer); |
323 } | 317 } |
(...skipping 12 matching lines...) Expand all Loading... | |
336 ResourceLoaderBridge* bridge = CreateBridge(); | 330 ResourceLoaderBridge* bridge = CreateBridge(); |
337 | 331 |
338 bridge->Start(this); | 332 bridge->Start(this); |
339 InitMessages(); | 333 InitMessages(); |
340 | 334 |
341 // Dispatch deferred messages. | 335 // Dispatch deferred messages. |
342 message_loop.RunAllPending(); | 336 message_loop.RunAllPending(); |
343 delete bridge; | 337 delete bridge; |
344 } | 338 } |
345 | 339 |
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 ResourceMsg_ReceivedResponse response_message(0, 0, response_head); | |
353 dispatcher_->OnMessageReceived(response_message); | |
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 | |
346 } // namespace content | 432 } // namespace content |
OLD | NEW |