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 size_t CountCache(const std::string& resource_id, | |
satorux1
2012/08/01 20:50:31
function comment is missing. Maybe CountCacheFiles
yoshiki
2012/08/01 21:44:59
Done.
| |
730 const std::string& md5) { | |
satorux1
2012/08/01 20:50:31
indentation is off
yoshiki
2012/08/01 21:44:59
Done.
| |
731 FilePath path = GetCacheFilePath( | |
732 resource_id, "*", | |
733 (test_util::ToCacheEntry(expected_cache_state_).is_pinned() ? | |
734 GDataCache::CACHE_TYPE_PERSISTENT : | |
735 GDataCache::CACHE_TYPE_TMP), | |
736 GDataCache::CACHED_FILE_FROM_SERVER); | |
737 file_util::FileEnumerator enumerator(path.DirName(), false, | |
738 file_util::FileEnumerator::FILES, | |
739 path.BaseName().value()); | |
740 size_t num_files_found = 0; | |
741 for (FilePath current = enumerator.Next(); !current.empty(); | |
742 current = enumerator.Next()) { | |
743 ++num_files_found; | |
744 EXPECT_EQ(util::EscapeCacheFileName(resource_id) + | |
745 FilePath::kExtensionSeparator + | |
746 util::EscapeCacheFileName(md5), | |
747 current.BaseName().value()); | |
748 } | |
749 return num_files_found; | |
750 } | |
751 | |
720 static FilePath GetTestFilePath(const FilePath::StringType& filename) { | 752 static FilePath GetTestFilePath(const FilePath::StringType& filename) { |
721 FilePath path; | 753 FilePath path; |
722 std::string error; | 754 std::string error; |
723 PathService::Get(chrome::DIR_TEST_DATA, &path); | 755 PathService::Get(chrome::DIR_TEST_DATA, &path); |
724 path = path.AppendASCII("chromeos") | 756 path = path.AppendASCII("chromeos") |
725 .AppendASCII("gdata") | 757 .AppendASCII("gdata") |
726 .AppendASCII(filename.c_str()); | 758 .AppendASCII(filename.c_str()); |
727 EXPECT_TRUE(file_util::PathExists(path)) << | 759 EXPECT_TRUE(file_util::PathExists(path)) << |
728 "Couldn't find " << path.value(); | 760 "Couldn't find " << path.value(); |
729 return path; | 761 return path; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
798 // |md5|. | 830 // |md5|. |
799 md5 = "new_md5"; | 831 md5 = "new_md5"; |
800 num_callback_invocations_ = 0; | 832 num_callback_invocations_ = 0; |
801 TestStoreToCache(resource_id, md5, GetTestFilePath("subdir_feed.json"), | 833 TestStoreToCache(resource_id, md5, GetTestFilePath("subdir_feed.json"), |
802 GDATA_FILE_OK, test_util::TEST_CACHE_STATE_PRESENT, | 834 GDATA_FILE_OK, test_util::TEST_CACHE_STATE_PRESENT, |
803 GDataCache::CACHE_TYPE_TMP); | 835 GDataCache::CACHE_TYPE_TMP); |
804 EXPECT_EQ(1, num_callback_invocations_); | 836 EXPECT_EQ(1, num_callback_invocations_); |
805 | 837 |
806 // Verify that there's only one file with name <resource_id>, i.e. previously | 838 // Verify that there's only one file with name <resource_id>, i.e. previously |
807 // cached file with the different md5 should be deleted. | 839 // cached file with the different md5 should be deleted. |
808 FilePath path = GetCacheFilePath( | 840 EXPECT_EQ(1U, CountCache(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 } | 841 } |
828 | 842 |
829 TEST_F(GDataCacheTest, GetFromCacheSimple) { | 843 TEST_F(GDataCacheTest, GetFromCacheSimple) { |
830 std::string resource_id("pdf:1a2b"); | 844 std::string resource_id("pdf:1a2b"); |
831 std::string md5("abcdef0123456789"); | 845 std::string md5("abcdef0123456789"); |
832 // First store a file to cache. | 846 // First store a file to cache. |
833 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 847 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
834 GDATA_FILE_OK, test_util::TEST_CACHE_STATE_PRESENT, | 848 GDATA_FILE_OK, test_util::TEST_CACHE_STATE_PRESENT, |
835 GDataCache::CACHE_TYPE_TMP); | 849 GDataCache::CACHE_TYPE_TMP); |
836 | 850 |
(...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1454 sort(resource_ids.begin(), resource_ids.end()); | 1468 sort(resource_ids.begin(), resource_ids.end()); |
1455 ASSERT_EQ(6U, resource_ids.size()); | 1469 ASSERT_EQ(6U, resource_ids.size()); |
1456 EXPECT_EQ("dirty:existing", resource_ids[0]); | 1470 EXPECT_EQ("dirty:existing", resource_ids[0]); |
1457 EXPECT_EQ("dirty_and_pinned:existing", resource_ids[1]); | 1471 EXPECT_EQ("dirty_and_pinned:existing", resource_ids[1]); |
1458 EXPECT_EQ("pinned:existing", resource_ids[2]); | 1472 EXPECT_EQ("pinned:existing", resource_ids[2]); |
1459 EXPECT_EQ("pinned:non-existent", resource_ids[3]); | 1473 EXPECT_EQ("pinned:non-existent", resource_ids[3]); |
1460 EXPECT_EQ("tmp:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?", resource_ids[4]); | 1474 EXPECT_EQ("tmp:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?", resource_ids[4]); |
1461 EXPECT_EQ("tmp:resource_id", resource_ids[5]); | 1475 EXPECT_EQ("tmp:resource_id", resource_ids[5]); |
1462 } | 1476 } |
1463 | 1477 |
1478 | |
1479 TEST_F(GDataCacheTest, ClearAllOnUIThread) { | |
1480 PrepareForInitCacheTest(); | |
1481 | |
1482 std::string resource_id("pdf:1a2b"); | |
1483 std::string md5("abcdef0123456789"); | |
1484 | |
1485 // Store an existing file. | |
1486 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | |
1487 GDATA_FILE_OK, test_util::TEST_CACHE_STATE_PRESENT, | |
1488 GDataCache::CACHE_TYPE_TMP); | |
1489 EXPECT_EQ(1, num_callback_invocations_); | |
1490 | |
1491 // Verify that there's only one cached file. | |
1492 EXPECT_EQ(1U, CountCache(resource_id, md5)); | |
1493 | |
1494 // Clear cache. | |
1495 GDataFileError error = GDATA_FILE_OK; | |
1496 FilePath file_path; | |
1497 cache_->ClearAllOnUIThread(base::Bind(&OnClearAll, | |
1498 &error, | |
1499 &file_path)); | |
1500 test_util::RunBlockingPoolTask(); | |
1501 EXPECT_EQ(GDATA_FILE_OK, error); | |
1502 | |
1503 // Verify that all the cache is removed. | |
1504 VerifyRemoveFromCache(error, resource_id, md5); | |
1505 EXPECT_EQ(0U, CountCache(resource_id, md5)); | |
1506 } | |
1507 | |
1464 } // namespace gdata | 1508 } // namespace gdata |
OLD | NEW |