OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <stack> | 5 #include <stack> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/pickle.h" | 12 #include "base/pickle.h" |
13 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
17 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
18 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
19 #include "net/url_request/url_request_context.h" | |
19 #include "net/url_request/url_request_error_job.h" | 20 #include "net/url_request/url_request_error_job.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "webkit/appcache/appcache_response.h" | 22 #include "webkit/appcache/appcache_response.h" |
22 #include "webkit/appcache/appcache_url_request_job.h" | 23 #include "webkit/appcache/appcache_url_request_job.h" |
23 #include "webkit/appcache/mock_appcache_service.h" | 24 #include "webkit/appcache/mock_appcache_service.h" |
24 | 25 |
25 using net::IOBuffer; | 26 using net::IOBuffer; |
26 using net::WrappedIOBuffer; | 27 using net::WrappedIOBuffer; |
27 | 28 |
28 namespace appcache { | 29 namespace appcache { |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
362 return true; | 363 return true; |
363 } | 364 } |
364 | 365 |
365 // Individual Tests --------------------------------------------------------- | 366 // Individual Tests --------------------------------------------------------- |
366 // Some of the individual tests involve multiple async steps. Each test | 367 // Some of the individual tests involve multiple async steps. Each test |
367 // is delineated with a section header. | 368 // is delineated with a section header. |
368 | 369 |
369 // Basic ------------------------------------------------------------------- | 370 // Basic ------------------------------------------------------------------- |
370 void Basic() { | 371 void Basic() { |
371 AppCacheStorage* storage = service_->storage(); | 372 AppCacheStorage* storage = service_->storage(); |
372 net::URLRequest request(GURL("http://blah/"), NULL); | 373 net::URLRequest request(GURL("http://blah/"), NULL, &empty_context_); |
373 scoped_refptr<AppCacheURLRequestJob> job; | 374 scoped_refptr<AppCacheURLRequestJob> job; |
374 | 375 |
375 // Create an instance and see that it looks as expected. | 376 // Create an instance and see that it looks as expected. |
376 | 377 |
377 job = new AppCacheURLRequestJob(&request, storage); | 378 job = new AppCacheURLRequestJob(&request, storage); |
378 EXPECT_TRUE(job->is_waiting()); | 379 EXPECT_TRUE(job->is_waiting()); |
379 EXPECT_FALSE(job->is_delivering_appcache_response()); | 380 EXPECT_FALSE(job->is_delivering_appcache_response()); |
380 EXPECT_FALSE(job->is_delivering_network_response()); | 381 EXPECT_FALSE(job->is_delivering_network_response()); |
381 EXPECT_FALSE(job->is_delivering_error_response()); | 382 EXPECT_FALSE(job->is_delivering_error_response()); |
382 EXPECT_FALSE(job->has_been_started()); | 383 EXPECT_FALSE(job->has_been_started()); |
383 EXPECT_FALSE(job->has_been_killed()); | 384 EXPECT_FALSE(job->has_been_killed()); |
384 EXPECT_EQ(GURL(), job->manifest_url()); | 385 EXPECT_EQ(GURL(), job->manifest_url()); |
385 EXPECT_EQ(kNoCacheId, job->cache_id()); | 386 EXPECT_EQ(kNoCacheId, job->cache_id()); |
386 EXPECT_FALSE(job->entry().has_response_id()); | 387 EXPECT_FALSE(job->entry().has_response_id()); |
387 | 388 |
388 TestFinished(); | 389 TestFinished(); |
389 } | 390 } |
390 | 391 |
391 // DeliveryOrders ----------------------------------------------------- | 392 // DeliveryOrders ----------------------------------------------------- |
392 void DeliveryOrders() { | 393 void DeliveryOrders() { |
393 AppCacheStorage* storage = service_->storage(); | 394 AppCacheStorage* storage = service_->storage(); |
394 net::URLRequest request(GURL("http://blah/"), NULL); | 395 net::URLRequest request(GURL("http://blah/"), NULL, &empty_context_); |
395 scoped_refptr<AppCacheURLRequestJob> job; | 396 scoped_refptr<AppCacheURLRequestJob> job; |
396 | 397 |
397 // Create an instance, give it a delivery order and see that | 398 // Create an instance, give it a delivery order and see that |
398 // it looks as expected. | 399 // it looks as expected. |
399 | 400 |
400 job = new AppCacheURLRequestJob(&request, storage); | 401 job = new AppCacheURLRequestJob(&request, storage); |
401 job->DeliverErrorResponse(); | 402 job->DeliverErrorResponse(); |
402 EXPECT_TRUE(job->is_delivering_error_response()); | 403 EXPECT_TRUE(job->is_delivering_error_response()); |
403 EXPECT_FALSE(job->has_been_started()); | 404 EXPECT_FALSE(job->has_been_started()); |
404 | 405 |
(...skipping 24 matching lines...) Expand all Loading... | |
429 // DeliverNetworkResponse -------------------------------------------------- | 430 // DeliverNetworkResponse -------------------------------------------------- |
430 | 431 |
431 void DeliverNetworkResponse() { | 432 void DeliverNetworkResponse() { |
432 // This test has async steps. | 433 // This test has async steps. |
433 PushNextTask( | 434 PushNextTask( |
434 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverNetworkResponse, | 435 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverNetworkResponse, |
435 base::Unretained(this))); | 436 base::Unretained(this))); |
436 | 437 |
437 AppCacheStorage* storage = service_->storage(); | 438 AppCacheStorage* storage = service_->storage(); |
438 request_.reset( | 439 request_.reset( |
439 new net::URLRequest(GURL("http://blah/"), url_request_delegate_.get())); | 440 new net::URLRequest(GURL("http://blah/"), |
441 url_request_delegate_.get(), | |
442 &empty_context_)); | |
440 | 443 |
441 // Setup to create an AppCacheURLRequestJob with orders to deliver | 444 // Setup to create an AppCacheURLRequestJob with orders to deliver |
442 // a network response. | 445 // a network response. |
443 mock_factory_job_ = new AppCacheURLRequestJob(request_.get(), storage); | 446 mock_factory_job_ = new AppCacheURLRequestJob(request_.get(), storage); |
444 mock_factory_job_->DeliverNetworkResponse(); | 447 mock_factory_job_->DeliverNetworkResponse(); |
445 EXPECT_TRUE(mock_factory_job_->is_delivering_network_response()); | 448 EXPECT_TRUE(mock_factory_job_->is_delivering_network_response()); |
446 EXPECT_FALSE(mock_factory_job_->has_been_started()); | 449 EXPECT_FALSE(mock_factory_job_->has_been_started()); |
447 | 450 |
448 // Start the request. | 451 // Start the request. |
449 request_->Start(); | 452 request_->Start(); |
(...skipping 12 matching lines...) Expand all Loading... | |
462 // DeliverErrorResponse -------------------------------------------------- | 465 // DeliverErrorResponse -------------------------------------------------- |
463 | 466 |
464 void DeliverErrorResponse() { | 467 void DeliverErrorResponse() { |
465 // This test has async steps. | 468 // This test has async steps. |
466 PushNextTask( | 469 PushNextTask( |
467 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverErrorResponse, | 470 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverErrorResponse, |
468 base::Unretained(this))); | 471 base::Unretained(this))); |
469 | 472 |
470 AppCacheStorage* storage = service_->storage(); | 473 AppCacheStorage* storage = service_->storage(); |
471 request_.reset( | 474 request_.reset( |
472 new net::URLRequest(GURL("http://blah/"), url_request_delegate_.get())); | 475 new net::URLRequest(GURL("http://blah/"), |
476 url_request_delegate_.get(), | |
477 &empty_context_)); | |
473 | 478 |
474 // Setup to create an AppCacheURLRequestJob with orders to deliver | 479 // Setup to create an AppCacheURLRequestJob with orders to deliver |
475 // a network response. | 480 // a network response. |
476 mock_factory_job_ = new AppCacheURLRequestJob(request_.get(), storage); | 481 mock_factory_job_ = new AppCacheURLRequestJob(request_.get(), storage); |
477 mock_factory_job_->DeliverErrorResponse(); | 482 mock_factory_job_->DeliverErrorResponse(); |
478 EXPECT_TRUE(mock_factory_job_->is_delivering_error_response()); | 483 EXPECT_TRUE(mock_factory_job_->is_delivering_error_response()); |
479 EXPECT_FALSE(mock_factory_job_->has_been_started()); | 484 EXPECT_FALSE(mock_factory_job_->has_been_started()); |
480 | 485 |
481 // Start the request. | 486 // Start the request. |
482 request_->Start(); | 487 request_->Start(); |
(...skipping 27 matching lines...) Expand all Loading... | |
510 | 515 |
511 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); | 516 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); |
512 written_response_id_ = writer_->response_id(); | 517 written_response_id_ = writer_->response_id(); |
513 WriteBasicResponse(); | 518 WriteBasicResponse(); |
514 // Continues async | 519 // Continues async |
515 } | 520 } |
516 | 521 |
517 void RequestAppCachedResource(bool start_after_delivery_orders) { | 522 void RequestAppCachedResource(bool start_after_delivery_orders) { |
518 AppCacheStorage* storage = service_->storage(); | 523 AppCacheStorage* storage = service_->storage(); |
519 request_.reset( | 524 request_.reset( |
520 new net::URLRequest(GURL("http://blah/"), url_request_delegate_.get())); | 525 new net::URLRequest(GURL("http://blah/"), |
526 url_request_delegate_.get(), | |
527 &empty_context_)); | |
521 | 528 |
522 // Setup to create an AppCacheURLRequestJob with orders to deliver | 529 // Setup to create an AppCacheURLRequestJob with orders to deliver |
523 // a network response. | 530 // a network response. |
524 scoped_refptr<AppCacheURLRequestJob> job( | 531 scoped_refptr<AppCacheURLRequestJob> job( |
525 new AppCacheURLRequestJob(request_.get(), storage)); | 532 new AppCacheURLRequestJob(request_.get(), storage)); |
526 | 533 |
527 if (start_after_delivery_orders) { | 534 if (start_after_delivery_orders) { |
528 job->DeliverAppCachedResponse( | 535 job->DeliverAppCachedResponse( |
529 GURL(), 0, 111, | 536 GURL(), 0, 111, |
530 AppCacheEntry(AppCacheEntry::EXPLICIT, written_response_id_), | 537 AppCacheEntry(AppCacheEntry::EXPLICIT, written_response_id_), |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
622 &AppCacheURLRequestJobTest::MakeRangeRequest, base::Unretained(this))); | 629 &AppCacheURLRequestJobTest::MakeRangeRequest, base::Unretained(this))); |
623 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); | 630 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); |
624 written_response_id_ = writer_->response_id(); | 631 written_response_id_ = writer_->response_id(); |
625 WriteBasicResponse(); | 632 WriteBasicResponse(); |
626 // Continues async | 633 // Continues async |
627 } | 634 } |
628 | 635 |
629 void MakeRangeRequest() { | 636 void MakeRangeRequest() { |
630 AppCacheStorage* storage = service_->storage(); | 637 AppCacheStorage* storage = service_->storage(); |
631 request_.reset( | 638 request_.reset( |
632 new net::URLRequest(GURL("http://blah/"), url_request_delegate_.get())); | 639 new net::URLRequest(GURL("http://blah/"), |
640 url_request_delegate_.get(), | |
641 &empty_context_)); | |
633 | 642 |
634 // Request a range, the 3 middle chars out of 'Hello' | 643 // Request a range, the 3 middle chars out of 'Hello' |
635 net::HttpRequestHeaders extra_headers; | 644 net::HttpRequestHeaders extra_headers; |
636 extra_headers.SetHeader("Range", "bytes= 1-3"); | 645 extra_headers.SetHeader("Range", "bytes= 1-3"); |
637 request_->SetExtraRequestHeaders(extra_headers); | 646 request_->SetExtraRequestHeaders(extra_headers); |
638 | 647 |
639 // Create job with orders to deliver an appcached entry. | 648 // Create job with orders to deliver an appcached entry. |
640 scoped_refptr<AppCacheURLRequestJob> job( | 649 scoped_refptr<AppCacheURLRequestJob> job( |
641 new AppCacheURLRequestJob(request_.get(), storage)); | 650 new AppCacheURLRequestJob(request_.get(), storage)); |
642 job->DeliverAppCachedResponse( | 651 job->DeliverAppCachedResponse( |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
740 int reader_deletion_count_down_; | 749 int reader_deletion_count_down_; |
741 | 750 |
742 int64 written_response_id_; | 751 int64 written_response_id_; |
743 scoped_ptr<AppCacheResponseWriter> writer_; | 752 scoped_ptr<AppCacheResponseWriter> writer_; |
744 scoped_refptr<HttpResponseInfoIOBuffer> write_info_buffer_; | 753 scoped_refptr<HttpResponseInfoIOBuffer> write_info_buffer_; |
745 scoped_refptr<IOBuffer> write_buffer_; | 754 scoped_refptr<IOBuffer> write_buffer_; |
746 int expected_write_result_; | 755 int expected_write_result_; |
747 int writer_deletion_count_down_; | 756 int writer_deletion_count_down_; |
748 | 757 |
749 net::URLRequest::ProtocolFactory* orig_http_factory_; | 758 net::URLRequest::ProtocolFactory* orig_http_factory_; |
759 net::URLRequestContext empty_context_; | |
mmenke
2012/06/20 18:33:50
Suggest a warning that order matters here, like wi
| |
750 scoped_ptr<net::URLRequest> request_; | 760 scoped_ptr<net::URLRequest> request_; |
751 scoped_ptr<MockURLRequestDelegate> url_request_delegate_; | 761 scoped_ptr<MockURLRequestDelegate> url_request_delegate_; |
752 | 762 |
753 static scoped_ptr<base::Thread> io_thread_; | 763 static scoped_ptr<base::Thread> io_thread_; |
754 static AppCacheURLRequestJob* mock_factory_job_; | 764 static AppCacheURLRequestJob* mock_factory_job_; |
755 }; | 765 }; |
756 | 766 |
757 // static | 767 // static |
758 scoped_ptr<base::Thread> AppCacheURLRequestJobTest::io_thread_; | 768 scoped_ptr<base::Thread> AppCacheURLRequestJobTest::io_thread_; |
759 AppCacheURLRequestJob* AppCacheURLRequestJobTest::mock_factory_job_ = NULL; | 769 AppCacheURLRequestJob* AppCacheURLRequestJobTest::mock_factory_job_ = NULL; |
(...skipping 28 matching lines...) Expand all Loading... | |
788 | 798 |
789 TEST_F(AppCacheURLRequestJobTest, CancelRequest) { | 799 TEST_F(AppCacheURLRequestJobTest, CancelRequest) { |
790 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequest); | 800 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequest); |
791 } | 801 } |
792 | 802 |
793 TEST_F(AppCacheURLRequestJobTest, CancelRequestWithIOPending) { | 803 TEST_F(AppCacheURLRequestJobTest, CancelRequestWithIOPending) { |
794 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending); | 804 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending); |
795 } | 805 } |
796 | 806 |
797 } // namespace appcache | 807 } // namespace appcache |
OLD | NEW |