| 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/base/request_priority.h" |
| 17 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
| 18 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
| 19 #include "net/url_request/url_request_context.h" | 20 #include "net/url_request/url_request_context.h" |
| 20 #include "net/url_request/url_request_error_job.h" | 21 #include "net/url_request/url_request_error_job.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "webkit/browser/appcache/appcache_response.h" | 23 #include "webkit/browser/appcache/appcache_response.h" |
| 23 #include "webkit/browser/appcache/appcache_url_request_job.h" | 24 #include "webkit/browser/appcache/appcache_url_request_job.h" |
| 24 #include "webkit/browser/appcache/mock_appcache_service.h" | 25 #include "webkit/browser/appcache/mock_appcache_service.h" |
| 25 | 26 |
| 26 using net::IOBuffer; | 27 using net::IOBuffer; |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 return true; | 370 return true; |
| 370 } | 371 } |
| 371 | 372 |
| 372 // Individual Tests --------------------------------------------------------- | 373 // Individual Tests --------------------------------------------------------- |
| 373 // Some of the individual tests involve multiple async steps. Each test | 374 // Some of the individual tests involve multiple async steps. Each test |
| 374 // is delineated with a section header. | 375 // is delineated with a section header. |
| 375 | 376 |
| 376 // Basic ------------------------------------------------------------------- | 377 // Basic ------------------------------------------------------------------- |
| 377 void Basic() { | 378 void Basic() { |
| 378 AppCacheStorage* storage = service_->storage(); | 379 AppCacheStorage* storage = service_->storage(); |
| 379 net::URLRequest request(GURL("http://blah/"), NULL, &empty_context_); | 380 net::URLRequest request(GURL("http://blah/"), |
| 381 net::DEFAULT_PRIORITY, |
| 382 NULL, |
| 383 &empty_context_, |
| 384 NULL); |
| 380 scoped_refptr<AppCacheURLRequestJob> job; | 385 scoped_refptr<AppCacheURLRequestJob> job; |
| 381 | 386 |
| 382 // Create an instance and see that it looks as expected. | 387 // Create an instance and see that it looks as expected. |
| 383 | 388 |
| 384 job = new AppCacheURLRequestJob( | 389 job = new AppCacheURLRequestJob( |
| 385 &request, NULL, storage, NULL); | 390 &request, NULL, storage, NULL); |
| 386 EXPECT_TRUE(job->is_waiting()); | 391 EXPECT_TRUE(job->is_waiting()); |
| 387 EXPECT_FALSE(job->is_delivering_appcache_response()); | 392 EXPECT_FALSE(job->is_delivering_appcache_response()); |
| 388 EXPECT_FALSE(job->is_delivering_network_response()); | 393 EXPECT_FALSE(job->is_delivering_network_response()); |
| 389 EXPECT_FALSE(job->is_delivering_error_response()); | 394 EXPECT_FALSE(job->is_delivering_error_response()); |
| 390 EXPECT_FALSE(job->has_been_started()); | 395 EXPECT_FALSE(job->has_been_started()); |
| 391 EXPECT_FALSE(job->has_been_killed()); | 396 EXPECT_FALSE(job->has_been_killed()); |
| 392 EXPECT_EQ(GURL(), job->manifest_url()); | 397 EXPECT_EQ(GURL(), job->manifest_url()); |
| 393 EXPECT_EQ(kNoCacheId, job->cache_id()); | 398 EXPECT_EQ(kNoCacheId, job->cache_id()); |
| 394 EXPECT_FALSE(job->entry().has_response_id()); | 399 EXPECT_FALSE(job->entry().has_response_id()); |
| 395 | 400 |
| 396 TestFinished(); | 401 TestFinished(); |
| 397 } | 402 } |
| 398 | 403 |
| 399 // DeliveryOrders ----------------------------------------------------- | 404 // DeliveryOrders ----------------------------------------------------- |
| 400 void DeliveryOrders() { | 405 void DeliveryOrders() { |
| 401 AppCacheStorage* storage = service_->storage(); | 406 AppCacheStorage* storage = service_->storage(); |
| 402 net::URLRequest request(GURL("http://blah/"), NULL, &empty_context_); | 407 net::URLRequest request(GURL("http://blah/"), |
| 408 net::DEFAULT_PRIORITY, |
| 409 NULL, |
| 410 &empty_context_, |
| 411 NULL); |
| 403 scoped_refptr<AppCacheURLRequestJob> job; | 412 scoped_refptr<AppCacheURLRequestJob> job; |
| 404 | 413 |
| 405 // Create an instance, give it a delivery order and see that | 414 // Create an instance, give it a delivery order and see that |
| 406 // it looks as expected. | 415 // it looks as expected. |
| 407 | 416 |
| 408 job = new AppCacheURLRequestJob(&request, NULL, storage, NULL); | 417 job = new AppCacheURLRequestJob(&request, NULL, storage, NULL); |
| 409 job->DeliverErrorResponse(); | 418 job->DeliverErrorResponse(); |
| 410 EXPECT_TRUE(job->is_delivering_error_response()); | 419 EXPECT_TRUE(job->is_delivering_error_response()); |
| 411 EXPECT_FALSE(job->has_been_started()); | 420 EXPECT_FALSE(job->has_been_started()); |
| 412 | 421 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 436 | 445 |
| 437 // DeliverNetworkResponse -------------------------------------------------- | 446 // DeliverNetworkResponse -------------------------------------------------- |
| 438 | 447 |
| 439 void DeliverNetworkResponse() { | 448 void DeliverNetworkResponse() { |
| 440 // This test has async steps. | 449 // This test has async steps. |
| 441 PushNextTask( | 450 PushNextTask( |
| 442 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverNetworkResponse, | 451 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverNetworkResponse, |
| 443 base::Unretained(this))); | 452 base::Unretained(this))); |
| 444 | 453 |
| 445 AppCacheStorage* storage = service_->storage(); | 454 AppCacheStorage* storage = service_->storage(); |
| 446 request_.reset(empty_context_.CreateRequest( | 455 request_.reset(empty_context_.CreateRequest(GURL("http://blah/"), |
| 447 GURL("http://blah/"), url_request_delegate_.get())); | 456 net::DEFAULT_PRIORITY, |
| 457 url_request_delegate_.get())); |
| 448 | 458 |
| 449 // Setup to create an AppCacheURLRequestJob with orders to deliver | 459 // Setup to create an AppCacheURLRequestJob with orders to deliver |
| 450 // a network response. | 460 // a network response. |
| 451 mock_factory_job_ = new AppCacheURLRequestJob( | 461 mock_factory_job_ = new AppCacheURLRequestJob( |
| 452 request_.get(), NULL, storage, NULL); | 462 request_.get(), NULL, storage, NULL); |
| 453 mock_factory_job_->DeliverNetworkResponse(); | 463 mock_factory_job_->DeliverNetworkResponse(); |
| 454 EXPECT_TRUE(mock_factory_job_->is_delivering_network_response()); | 464 EXPECT_TRUE(mock_factory_job_->is_delivering_network_response()); |
| 455 EXPECT_FALSE(mock_factory_job_->has_been_started()); | 465 EXPECT_FALSE(mock_factory_job_->has_been_started()); |
| 456 | 466 |
| 457 // Start the request. | 467 // Start the request. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 470 | 480 |
| 471 // DeliverErrorResponse -------------------------------------------------- | 481 // DeliverErrorResponse -------------------------------------------------- |
| 472 | 482 |
| 473 void DeliverErrorResponse() { | 483 void DeliverErrorResponse() { |
| 474 // This test has async steps. | 484 // This test has async steps. |
| 475 PushNextTask( | 485 PushNextTask( |
| 476 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverErrorResponse, | 486 base::Bind(&AppCacheURLRequestJobTest::VerifyDeliverErrorResponse, |
| 477 base::Unretained(this))); | 487 base::Unretained(this))); |
| 478 | 488 |
| 479 AppCacheStorage* storage = service_->storage(); | 489 AppCacheStorage* storage = service_->storage(); |
| 480 request_.reset(empty_context_.CreateRequest(GURL( | 490 request_.reset(empty_context_.CreateRequest(GURL("http://blah/"), |
| 481 "http://blah/"), url_request_delegate_.get())); | 491 net::DEFAULT_PRIORITY, |
| 492 url_request_delegate_.get())); |
| 482 | 493 |
| 483 // Setup to create an AppCacheURLRequestJob with orders to deliver | 494 // Setup to create an AppCacheURLRequestJob with orders to deliver |
| 484 // a network response. | 495 // a network response. |
| 485 mock_factory_job_ = new AppCacheURLRequestJob( | 496 mock_factory_job_ = new AppCacheURLRequestJob( |
| 486 request_.get(), NULL, storage, NULL); | 497 request_.get(), NULL, storage, NULL); |
| 487 mock_factory_job_->DeliverErrorResponse(); | 498 mock_factory_job_->DeliverErrorResponse(); |
| 488 EXPECT_TRUE(mock_factory_job_->is_delivering_error_response()); | 499 EXPECT_TRUE(mock_factory_job_->is_delivering_error_response()); |
| 489 EXPECT_FALSE(mock_factory_job_->has_been_started()); | 500 EXPECT_FALSE(mock_factory_job_->has_been_started()); |
| 490 | 501 |
| 491 // Start the request. | 502 // Start the request. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 519 base::Unretained(this), false)); | 530 base::Unretained(this), false)); |
| 520 | 531 |
| 521 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); | 532 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); |
| 522 written_response_id_ = writer_->response_id(); | 533 written_response_id_ = writer_->response_id(); |
| 523 WriteBasicResponse(); | 534 WriteBasicResponse(); |
| 524 // Continues async | 535 // Continues async |
| 525 } | 536 } |
| 526 | 537 |
| 527 void RequestAppCachedResource(bool start_after_delivery_orders) { | 538 void RequestAppCachedResource(bool start_after_delivery_orders) { |
| 528 AppCacheStorage* storage = service_->storage(); | 539 AppCacheStorage* storage = service_->storage(); |
| 529 request_.reset(empty_context_.CreateRequest( | 540 request_.reset(empty_context_.CreateRequest(GURL("http://blah/"), |
| 530 GURL("http://blah/"), url_request_delegate_.get())); | 541 net::DEFAULT_PRIORITY, |
| 542 url_request_delegate_.get())); |
| 531 | 543 |
| 532 // Setup to create an AppCacheURLRequestJob with orders to deliver | 544 // Setup to create an AppCacheURLRequestJob with orders to deliver |
| 533 // a network response. | 545 // a network response. |
| 534 scoped_refptr<AppCacheURLRequestJob> job(new AppCacheURLRequestJob( | 546 scoped_refptr<AppCacheURLRequestJob> job(new AppCacheURLRequestJob( |
| 535 request_.get(), NULL, storage, NULL)); | 547 request_.get(), NULL, storage, NULL)); |
| 536 | 548 |
| 537 if (start_after_delivery_orders) { | 549 if (start_after_delivery_orders) { |
| 538 job->DeliverAppCachedResponse( | 550 job->DeliverAppCachedResponse( |
| 539 GURL(), 0, 111, | 551 GURL(), 0, 111, |
| 540 AppCacheEntry(AppCacheEntry::EXPLICIT, written_response_id_), | 552 AppCacheEntry(AppCacheEntry::EXPLICIT, written_response_id_), |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 632 PushNextTask(base::Bind( | 644 PushNextTask(base::Bind( |
| 633 &AppCacheURLRequestJobTest::MakeRangeRequest, base::Unretained(this))); | 645 &AppCacheURLRequestJobTest::MakeRangeRequest, base::Unretained(this))); |
| 634 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); | 646 writer_.reset(service_->storage()->CreateResponseWriter(GURL(), 0)); |
| 635 written_response_id_ = writer_->response_id(); | 647 written_response_id_ = writer_->response_id(); |
| 636 WriteBasicResponse(); | 648 WriteBasicResponse(); |
| 637 // Continues async | 649 // Continues async |
| 638 } | 650 } |
| 639 | 651 |
| 640 void MakeRangeRequest() { | 652 void MakeRangeRequest() { |
| 641 AppCacheStorage* storage = service_->storage(); | 653 AppCacheStorage* storage = service_->storage(); |
| 642 request_.reset(empty_context_.CreateRequest( | 654 request_.reset(empty_context_.CreateRequest(GURL("http://blah/"), |
| 643 GURL("http://blah/"), url_request_delegate_.get())); | 655 net::DEFAULT_PRIORITY, |
| 656 url_request_delegate_.get())); |
| 644 | 657 |
| 645 // Request a range, the 3 middle chars out of 'Hello' | 658 // Request a range, the 3 middle chars out of 'Hello' |
| 646 net::HttpRequestHeaders extra_headers; | 659 net::HttpRequestHeaders extra_headers; |
| 647 extra_headers.SetHeader("Range", "bytes= 1-3"); | 660 extra_headers.SetHeader("Range", "bytes= 1-3"); |
| 648 request_->SetExtraRequestHeaders(extra_headers); | 661 request_->SetExtraRequestHeaders(extra_headers); |
| 649 | 662 |
| 650 // Create job with orders to deliver an appcached entry. | 663 // Create job with orders to deliver an appcached entry. |
| 651 scoped_refptr<AppCacheURLRequestJob> job(new AppCacheURLRequestJob( | 664 scoped_refptr<AppCacheURLRequestJob> job(new AppCacheURLRequestJob( |
| 652 request_.get(), NULL, storage, NULL)); | 665 request_.get(), NULL, storage, NULL)); |
| 653 job->DeliverAppCachedResponse( | 666 job->DeliverAppCachedResponse( |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 800 | 813 |
| 801 TEST_F(AppCacheURLRequestJobTest, CancelRequest) { | 814 TEST_F(AppCacheURLRequestJobTest, CancelRequest) { |
| 802 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequest); | 815 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequest); |
| 803 } | 816 } |
| 804 | 817 |
| 805 TEST_F(AppCacheURLRequestJobTest, CancelRequestWithIOPending) { | 818 TEST_F(AppCacheURLRequestJobTest, CancelRequestWithIOPending) { |
| 806 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending); | 819 RunTestOnIOThread(&AppCacheURLRequestJobTest::CancelRequestWithIOPending); |
| 807 } | 820 } |
| 808 | 821 |
| 809 } // namespace appcache | 822 } // namespace appcache |
| OLD | NEW |