Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/cache_storage/cache_storage_cache.h" | 5 #include "content/browser/cache_storage/cache_storage_cache.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 288 | 288 |
| 289 cache_->Match( | 289 cache_->Match( |
| 290 CopyFetchRequest(request), | 290 CopyFetchRequest(request), |
| 291 base::Bind(&CacheStorageCacheTest::ResponseAndErrorCallback, | 291 base::Bind(&CacheStorageCacheTest::ResponseAndErrorCallback, |
| 292 base::Unretained(this), base::Unretained(loop.get()))); | 292 base::Unretained(this), base::Unretained(loop.get()))); |
| 293 loop->Run(); | 293 loop->Run(); |
| 294 | 294 |
| 295 return callback_error_ == CACHE_STORAGE_OK; | 295 return callback_error_ == CACHE_STORAGE_OK; |
| 296 } | 296 } |
| 297 | 297 |
| 298 bool MatchAll(std::vector<ServiceWorkerResponse>* responses, | |
| 299 ScopedVector<storage::BlobDataHandle>* body_handles) { | |
| 300 base::RunLoop loop; | |
| 301 cache_->MatchAll(base::Bind( | |
| 302 &CacheStorageCacheTest::ResponsesAndErrorCallback, | |
| 303 base::Unretained(this), loop.QuitClosure(), responses, body_handles)); | |
| 304 loop.Run(); | |
| 305 return callback_error_ == CACHE_STORAGE_OK; | |
| 306 } | |
| 307 | |
| 298 bool Delete(const ServiceWorkerFetchRequest& request) { | 308 bool Delete(const ServiceWorkerFetchRequest& request) { |
| 299 CacheStorageBatchOperation operation; | 309 CacheStorageBatchOperation operation; |
| 300 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE; | 310 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE; |
| 301 operation.request = request; | 311 operation.request = request; |
| 302 | 312 |
| 303 CacheStorageError error = | 313 CacheStorageError error = |
| 304 BatchOperation(std::vector<CacheStorageBatchOperation>(1, operation)); | 314 BatchOperation(std::vector<CacheStorageBatchOperation>(1, operation)); |
| 305 return error == CACHE_STORAGE_OK; | 315 return error == CACHE_STORAGE_OK; |
| 306 } | 316 } |
| 307 | 317 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 363 callback_error_ = error; | 373 callback_error_ = error; |
| 364 callback_response_ = response.Pass(); | 374 callback_response_ = response.Pass(); |
| 365 callback_response_data_.reset(); | 375 callback_response_data_.reset(); |
| 366 if (error == CACHE_STORAGE_OK && !callback_response_->blob_uuid.empty()) | 376 if (error == CACHE_STORAGE_OK && !callback_response_->blob_uuid.empty()) |
| 367 callback_response_data_ = body_handle.Pass(); | 377 callback_response_data_ = body_handle.Pass(); |
| 368 | 378 |
| 369 if (run_loop) | 379 if (run_loop) |
| 370 run_loop->Quit(); | 380 run_loop->Quit(); |
| 371 } | 381 } |
| 372 | 382 |
| 383 void ResponsesAndErrorCallback( | |
| 384 const base::Closure& quit_closure, | |
| 385 std::vector<ServiceWorkerResponse>* responses_out, | |
| 386 ScopedVector<storage::BlobDataHandle>* body_handles_out, | |
| 387 CacheStorageError error, | |
| 388 const std::vector<ServiceWorkerResponse>& responses, | |
| 389 ScopedVector<storage::BlobDataHandle> body_handles) { | |
| 390 callback_error_ = error; | |
| 391 *responses_out = responses; | |
| 392 body_handles_out->swap(body_handles); | |
| 393 quit_closure.Run(); | |
| 394 } | |
| 395 | |
| 373 void CloseCallback(base::RunLoop* run_loop) { | 396 void CloseCallback(base::RunLoop* run_loop) { |
| 374 EXPECT_FALSE(callback_closed_); | 397 EXPECT_FALSE(callback_closed_); |
| 375 callback_closed_ = true; | 398 callback_closed_ = true; |
| 376 if (run_loop) | 399 if (run_loop) |
| 377 run_loop->Quit(); | 400 run_loop->Quit(); |
| 378 } | 401 } |
| 379 | 402 |
| 380 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) { | 403 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) { |
| 381 *output = std::string(); | 404 *output = std::string(); |
| 382 scoped_ptr<storage::BlobDataSnapshot> data = blob_handle->CreateSnapshot(); | 405 scoped_ptr<storage::BlobDataSnapshot> data = blob_handle->CreateSnapshot(); |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 613 EXPECT_STREQ("http://example.com/body.html", | 636 EXPECT_STREQ("http://example.com/body.html", |
| 614 callback_response_->url.spec().c_str()); | 637 callback_response_->url.spec().c_str()); |
| 615 EXPECT_STRNE("", callback_response_->blob_uuid.c_str()); | 638 EXPECT_STRNE("", callback_response_->blob_uuid.c_str()); |
| 616 EXPECT_EQ(expected_blob_data_.size(), callback_response_->blob_size); | 639 EXPECT_EQ(expected_blob_data_.size(), callback_response_->blob_size); |
| 617 | 640 |
| 618 std::string response_body; | 641 std::string response_body; |
| 619 CopyBody(callback_response_data_.get(), &response_body); | 642 CopyBody(callback_response_data_.get(), &response_body); |
| 620 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); | 643 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); |
| 621 } | 644 } |
| 622 | 645 |
| 646 TEST_P(CacheStorageCacheTestP, MatchAll_Empty) { | |
| 647 std::vector<ServiceWorkerResponse> responses; | |
| 648 ScopedVector<storage::BlobDataHandle> body_handles; | |
| 649 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
| 650 EXPECT_TRUE(responses.empty()); | |
| 651 EXPECT_TRUE(body_handles.empty()); | |
| 652 } | |
| 653 | |
| 654 TEST_P(CacheStorageCacheTestP, MatchAll_NoBody) { | |
| 655 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | |
| 656 | |
| 657 std::vector<ServiceWorkerResponse> responses; | |
| 658 ScopedVector<storage::BlobDataHandle> body_handles; | |
| 659 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
| 660 ASSERT_EQ(1u, responses.size()); | |
| 661 ASSERT_EQ(1u, body_handles.size()); | |
| 662 EXPECT_EQ(200, responses[0].status_code); | |
| 663 EXPECT_STREQ("OK", responses[0].status_text.c_str()); | |
|
jkarlin
2015/08/04 13:25:07
A "bool ResponsesEqual(const ServiceWorkerResponse
nhiroki
2015/08/05 08:03:04
Done.
| |
| 664 EXPECT_STREQ("http://example.com/no_body.html", | |
| 665 responses[0].url.spec().c_str()); | |
| 666 EXPECT_STREQ("", responses[0].blob_uuid.c_str()); | |
| 667 EXPECT_EQ(0u, responses[0].blob_size); | |
| 668 EXPECT_FALSE(body_handles[0]); | |
| 669 } | |
| 670 | |
| 671 TEST_P(CacheStorageCacheTestP, MatchAll_Body) { | |
| 672 EXPECT_TRUE(Put(body_request_, body_response_)); | |
| 673 | |
| 674 std::vector<ServiceWorkerResponse> responses; | |
| 675 ScopedVector<storage::BlobDataHandle> body_handles; | |
| 676 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
| 677 ASSERT_EQ(1u, responses.size()); | |
| 678 ASSERT_EQ(1u, body_handles.size()); | |
| 679 EXPECT_EQ(200, responses[0].status_code); | |
| 680 EXPECT_STREQ("OK", responses[0].status_text.c_str()); | |
| 681 EXPECT_STREQ("http://example.com/body.html", responses[0].url.spec().c_str()); | |
| 682 EXPECT_STRNE("", responses[0].blob_uuid.c_str()); | |
| 683 EXPECT_EQ(expected_blob_data_.size(), responses[0].blob_size); | |
| 684 | |
| 685 std::string response_body; | |
| 686 CopyBody(body_handles[0], &response_body); | |
| 687 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); | |
| 688 } | |
| 689 | |
| 690 TEST_P(CacheStorageCacheTestP, MatchAll_TwoResponsesThenOne) { | |
| 691 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | |
| 692 EXPECT_TRUE(Put(body_request_, body_response_)); | |
| 693 | |
| 694 std::vector<ServiceWorkerResponse> responses; | |
| 695 ScopedVector<storage::BlobDataHandle> body_handles; | |
| 696 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
| 697 ASSERT_EQ(2u, responses.size()); | |
| 698 ASSERT_EQ(2u, body_handles.size()); | |
| 699 | |
| 700 // Order of returned responses is not guaranteed. | |
| 701 std::set<std::string> matched_set; | |
| 702 for (size_t i = 0; i < responses.size(); ++i) { | |
| 703 EXPECT_EQ(200, responses[i].status_code); | |
| 704 EXPECT_STREQ("OK", responses[i].status_text.c_str()); | |
| 705 if (responses[i].url.spec() == "http://example.com/no_body.html") { | |
| 706 EXPECT_STREQ("", responses[i].blob_uuid.c_str()); | |
| 707 EXPECT_EQ(0u, responses[i].blob_size); | |
| 708 EXPECT_FALSE(body_handles[i]); | |
| 709 matched_set.insert(responses[i].url.spec()); | |
| 710 } else if (responses[i].url.spec() == "http://example.com/body.html") { | |
| 711 EXPECT_STREQ("http://example.com/body.html", | |
| 712 responses[i].url.spec().c_str()); | |
| 713 EXPECT_STRNE("", responses[i].blob_uuid.c_str()); | |
| 714 EXPECT_EQ(expected_blob_data_.size(), responses[i].blob_size); | |
| 715 matched_set.insert(responses[i].url.spec()); | |
| 716 } | |
| 717 } | |
| 718 EXPECT_EQ(2u, matched_set.size()); | |
| 719 | |
| 720 responses.clear(); | |
| 721 body_handles.clear(); | |
| 722 | |
| 723 EXPECT_TRUE(Delete(body_request_)); | |
| 724 EXPECT_TRUE(MatchAll(&responses, &body_handles)); | |
| 725 | |
| 726 ASSERT_EQ(1u, responses.size()); | |
| 727 ASSERT_EQ(1u, body_handles.size()); | |
| 728 EXPECT_EQ(200, responses[0].status_code); | |
| 729 EXPECT_STREQ("OK", responses[0].status_text.c_str()); | |
| 730 EXPECT_STREQ("http://example.com/no_body.html", | |
| 731 responses[0].url.spec().c_str()); | |
| 732 EXPECT_STREQ("", responses[0].blob_uuid.c_str()); | |
| 733 EXPECT_EQ(0u, responses[0].blob_size); | |
| 734 EXPECT_FALSE(body_handles[0]); | |
| 735 } | |
| 736 | |
| 623 TEST_P(CacheStorageCacheTestP, Vary) { | 737 TEST_P(CacheStorageCacheTestP, Vary) { |
| 624 body_request_.headers["vary_foo"] = "foo"; | 738 body_request_.headers["vary_foo"] = "foo"; |
| 625 body_response_.headers["vary"] = "vary_foo"; | 739 body_response_.headers["vary"] = "vary_foo"; |
| 626 EXPECT_TRUE(Put(body_request_, body_response_)); | 740 EXPECT_TRUE(Put(body_request_, body_response_)); |
| 627 EXPECT_TRUE(Match(body_request_)); | 741 EXPECT_TRUE(Match(body_request_)); |
| 628 | 742 |
| 629 body_request_.headers["vary_foo"] = "bar"; | 743 body_request_.headers["vary_foo"] = "bar"; |
| 630 EXPECT_FALSE(Match(body_request_)); | 744 EXPECT_FALSE(Match(body_request_)); |
| 631 | 745 |
| 632 body_request_.headers.erase("vary_foo"); | 746 body_request_.headers.erase("vary_foo"); |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 889 EXPECT_EQ(1, sequence_out); | 1003 EXPECT_EQ(1, sequence_out); |
| 890 close_loop2->Run(); | 1004 close_loop2->Run(); |
| 891 EXPECT_EQ(2, sequence_out); | 1005 EXPECT_EQ(2, sequence_out); |
| 892 } | 1006 } |
| 893 | 1007 |
| 894 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, | 1008 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, |
| 895 CacheStorageCacheTestP, | 1009 CacheStorageCacheTestP, |
| 896 ::testing::Values(false, true)); | 1010 ::testing::Values(false, true)); |
| 897 | 1011 |
| 898 } // namespace content | 1012 } // namespace content |
| OLD | NEW |