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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 *out_to_fetch = to_fetch; | 106 *out_to_fetch = to_fetch; |
107 *out_to_upload = to_upload; | 107 *out_to_upload = to_upload; |
108 } | 108 } |
109 | 109 |
110 // Copies results from GetResourceIdsCallback. | 110 // Copies results from GetResourceIdsCallback. |
111 void OnGetResourceIds(std::vector<std::string>* out_resource_ids, | 111 void OnGetResourceIds(std::vector<std::string>* out_resource_ids, |
112 const std::vector<std::string>& resource_ids) { | 112 const std::vector<std::string>& resource_ids) { |
113 *out_resource_ids = resource_ids; | 113 *out_resource_ids = resource_ids; |
114 } | 114 } |
115 | 115 |
| 116 // Copies results from ClearAllOnUIThread. |
| 117 void OnClearAll(GDataFileError* out_error, |
| 118 FilePath* out_file_path, |
| 119 GDataFileError error, |
| 120 const FilePath& file_path) { |
| 121 *out_file_path = file_path; |
| 122 *out_error = error; |
| 123 } |
| 124 |
116 } // namespace | 125 } // namespace |
117 | 126 |
118 class GDataCacheTest : public testing::Test { | 127 class GDataCacheTest : public testing::Test { |
119 protected: | 128 protected: |
120 GDataCacheTest() | 129 GDataCacheTest() |
121 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 130 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
122 io_thread_(content::BrowserThread::IO), | 131 io_thread_(content::BrowserThread::IO), |
123 cache_(NULL), | 132 cache_(NULL), |
124 num_callback_invocations_(0), | 133 num_callback_invocations_(0), |
125 expected_error_(GDATA_FILE_OK), | 134 expected_error_(GDATA_FILE_OK), |
(...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 | 719 |
711 // FilePath::Extension returns ".", so strip it. | 720 // FilePath::Extension returns ".", so strip it. |
712 std::string unescaped_md5 = util::UnescapeCacheFileName( | 721 std::string unescaped_md5 = util::UnescapeCacheFileName( |
713 base_name.Extension().substr(1)); | 722 base_name.Extension().substr(1)); |
714 EXPECT_EQ(md5, unescaped_md5); | 723 EXPECT_EQ(md5, unescaped_md5); |
715 std::string unescaped_resource_id = util::UnescapeCacheFileName( | 724 std::string unescaped_resource_id = util::UnescapeCacheFileName( |
716 base_name.RemoveExtension().value()); | 725 base_name.RemoveExtension().value()); |
717 EXPECT_EQ(resource_id, unescaped_resource_id); | 726 EXPECT_EQ(resource_id, unescaped_resource_id); |
718 } | 727 } |
719 | 728 |
| 729 // Returns the number of the cache files with name <resource_id>, and Confirm |
| 730 // that they have the <md5>. This should return 1 or 0. |
| 731 size_t CountCacheFiles(const std::string& resource_id, |
| 732 const std::string& md5) { |
| 733 FilePath path = GetCacheFilePath( |
| 734 resource_id, "*", |
| 735 (test_util::ToCacheEntry(expected_cache_state_).is_pinned() ? |
| 736 GDataCache::CACHE_TYPE_PERSISTENT : |
| 737 GDataCache::CACHE_TYPE_TMP), |
| 738 GDataCache::CACHED_FILE_FROM_SERVER); |
| 739 file_util::FileEnumerator enumerator(path.DirName(), false, |
| 740 file_util::FileEnumerator::FILES, |
| 741 path.BaseName().value()); |
| 742 size_t num_files_found = 0; |
| 743 for (FilePath current = enumerator.Next(); !current.empty(); |
| 744 current = enumerator.Next()) { |
| 745 ++num_files_found; |
| 746 EXPECT_EQ(util::EscapeCacheFileName(resource_id) + |
| 747 FilePath::kExtensionSeparator + |
| 748 util::EscapeCacheFileName(md5), |
| 749 current.BaseName().value()); |
| 750 } |
| 751 return num_files_found; |
| 752 } |
| 753 |
720 static FilePath GetTestFilePath(const FilePath::StringType& filename) { | 754 static FilePath GetTestFilePath(const FilePath::StringType& filename) { |
721 FilePath path; | 755 FilePath path; |
722 std::string error; | 756 std::string error; |
723 PathService::Get(chrome::DIR_TEST_DATA, &path); | 757 PathService::Get(chrome::DIR_TEST_DATA, &path); |
724 path = path.AppendASCII("chromeos") | 758 path = path.AppendASCII("chromeos") |
725 .AppendASCII("gdata") | 759 .AppendASCII("gdata") |
726 .AppendASCII(filename.c_str()); | 760 .AppendASCII(filename.c_str()); |
727 EXPECT_TRUE(file_util::PathExists(path)) << | 761 EXPECT_TRUE(file_util::PathExists(path)) << |
728 "Couldn't find " << path.value(); | 762 "Couldn't find " << path.value(); |
729 return path; | 763 return path; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
798 // |md5|. | 832 // |md5|. |
799 md5 = "new_md5"; | 833 md5 = "new_md5"; |
800 num_callback_invocations_ = 0; | 834 num_callback_invocations_ = 0; |
801 TestStoreToCache(resource_id, md5, GetTestFilePath("subdir_feed.json"), | 835 TestStoreToCache(resource_id, md5, GetTestFilePath("subdir_feed.json"), |
802 GDATA_FILE_OK, test_util::TEST_CACHE_STATE_PRESENT, | 836 GDATA_FILE_OK, test_util::TEST_CACHE_STATE_PRESENT, |
803 GDataCache::CACHE_TYPE_TMP); | 837 GDataCache::CACHE_TYPE_TMP); |
804 EXPECT_EQ(1, num_callback_invocations_); | 838 EXPECT_EQ(1, num_callback_invocations_); |
805 | 839 |
806 // Verify that there's only one file with name <resource_id>, i.e. previously | 840 // Verify that there's only one file with name <resource_id>, i.e. previously |
807 // cached file with the different md5 should be deleted. | 841 // cached file with the different md5 should be deleted. |
808 FilePath path = GetCacheFilePath( | 842 EXPECT_EQ(1U, CountCacheFiles(resource_id, md5)); |
809 resource_id, "*", | |
810 (test_util::ToCacheEntry(expected_cache_state_).is_pinned() ? | |
811 GDataCache::CACHE_TYPE_PERSISTENT : | |
812 GDataCache::CACHE_TYPE_TMP), | |
813 GDataCache::CACHED_FILE_FROM_SERVER); | |
814 file_util::FileEnumerator enumerator(path.DirName(), false, | |
815 file_util::FileEnumerator::FILES, | |
816 path.BaseName().value()); | |
817 size_t num_files_found = 0; | |
818 for (FilePath current = enumerator.Next(); !current.empty(); | |
819 current = enumerator.Next()) { | |
820 ++num_files_found; | |
821 EXPECT_EQ(util::EscapeCacheFileName(resource_id) + | |
822 FilePath::kExtensionSeparator + | |
823 util::EscapeCacheFileName(md5), | |
824 current.BaseName().value()); | |
825 } | |
826 EXPECT_EQ(1U, num_files_found); | |
827 } | 843 } |
828 | 844 |
829 TEST_F(GDataCacheTest, GetFromCacheSimple) { | 845 TEST_F(GDataCacheTest, GetFromCacheSimple) { |
830 std::string resource_id("pdf:1a2b"); | 846 std::string resource_id("pdf:1a2b"); |
831 std::string md5("abcdef0123456789"); | 847 std::string md5("abcdef0123456789"); |
832 // First store a file to cache. | 848 // First store a file to cache. |
833 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 849 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
834 GDATA_FILE_OK, test_util::TEST_CACHE_STATE_PRESENT, | 850 GDATA_FILE_OK, test_util::TEST_CACHE_STATE_PRESENT, |
835 GDataCache::CACHE_TYPE_TMP); | 851 GDataCache::CACHE_TYPE_TMP); |
836 | 852 |
(...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1454 sort(resource_ids.begin(), resource_ids.end()); | 1470 sort(resource_ids.begin(), resource_ids.end()); |
1455 ASSERT_EQ(6U, resource_ids.size()); | 1471 ASSERT_EQ(6U, resource_ids.size()); |
1456 EXPECT_EQ("dirty:existing", resource_ids[0]); | 1472 EXPECT_EQ("dirty:existing", resource_ids[0]); |
1457 EXPECT_EQ("dirty_and_pinned:existing", resource_ids[1]); | 1473 EXPECT_EQ("dirty_and_pinned:existing", resource_ids[1]); |
1458 EXPECT_EQ("pinned:existing", resource_ids[2]); | 1474 EXPECT_EQ("pinned:existing", resource_ids[2]); |
1459 EXPECT_EQ("pinned:non-existent", resource_ids[3]); | 1475 EXPECT_EQ("pinned:non-existent", resource_ids[3]); |
1460 EXPECT_EQ("tmp:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?", resource_ids[4]); | 1476 EXPECT_EQ("tmp:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?", resource_ids[4]); |
1461 EXPECT_EQ("tmp:resource_id", resource_ids[5]); | 1477 EXPECT_EQ("tmp:resource_id", resource_ids[5]); |
1462 } | 1478 } |
1463 | 1479 |
| 1480 |
| 1481 TEST_F(GDataCacheTest, ClearAllOnUIThread) { |
| 1482 PrepareForInitCacheTest(); |
| 1483 |
| 1484 std::string resource_id("pdf:1a2b"); |
| 1485 std::string md5("abcdef0123456789"); |
| 1486 |
| 1487 // Store an existing file. |
| 1488 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
| 1489 GDATA_FILE_OK, test_util::TEST_CACHE_STATE_PRESENT, |
| 1490 GDataCache::CACHE_TYPE_TMP); |
| 1491 EXPECT_EQ(1, num_callback_invocations_); |
| 1492 |
| 1493 // Verify that there's only one cached file. |
| 1494 EXPECT_EQ(1U, CountCacheFiles(resource_id, md5)); |
| 1495 |
| 1496 // Clear cache. |
| 1497 GDataFileError error = GDATA_FILE_OK; |
| 1498 FilePath file_path; |
| 1499 cache_->ClearAllOnUIThread(base::Bind(&OnClearAll, |
| 1500 &error, |
| 1501 &file_path)); |
| 1502 test_util::RunBlockingPoolTask(); |
| 1503 EXPECT_EQ(GDATA_FILE_OK, error); |
| 1504 |
| 1505 // Verify that all the cache is removed. |
| 1506 VerifyRemoveFromCache(error, resource_id, md5); |
| 1507 EXPECT_EQ(0U, CountCacheFiles(resource_id, md5)); |
| 1508 } |
| 1509 |
1464 } // namespace gdata | 1510 } // namespace gdata |
OLD | NEW |