| 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 "chrome/browser/chromeos/drive/file_cache.h" | 5 #include "chrome/browser/chromeos/drive/file_cache.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 virtual void SetUp() OVERRIDE { | 52 virtual void SetUp() OVERRIDE { |
| 53 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 53 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 54 const base::FilePath metadata_dir = temp_dir_.path().AppendASCII("meta"); | 54 const base::FilePath metadata_dir = temp_dir_.path().AppendASCII("meta"); |
| 55 const base::FilePath cache_dir = | 55 const base::FilePath cache_dir = |
| 56 temp_dir_.path().AppendASCII(kCacheFileDirectory); | 56 temp_dir_.path().AppendASCII(kCacheFileDirectory); |
| 57 | 57 |
| 58 ASSERT_TRUE(file_util::CreateDirectory(metadata_dir)); | 58 ASSERT_TRUE(file_util::CreateDirectory(metadata_dir)); |
| 59 ASSERT_TRUE(file_util::CreateDirectory(cache_dir)); | 59 ASSERT_TRUE(file_util::CreateDirectory(cache_dir)); |
| 60 | 60 |
| 61 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), | 61 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), |
| 62 &dummy_file_path_)); | 62 &dummy_file_path_)); |
| 63 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); | 63 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); |
| 64 | 64 |
| 65 scoped_refptr<base::SequencedWorkerPool> pool = | 65 scoped_refptr<base::SequencedWorkerPool> pool = |
| 66 content::BrowserThread::GetBlockingPool(); | 66 content::BrowserThread::GetBlockingPool(); |
| 67 blocking_task_runner_ = | 67 blocking_task_runner_ = |
| 68 pool->GetSequencedTaskRunner(pool->GetSequenceToken()); | 68 pool->GetSequencedTaskRunner(pool->GetSequenceToken()); |
| 69 | 69 |
| 70 metadata_storage_.reset(new ResourceMetadataStorage( | 70 metadata_storage_.reset(new ResourceMetadataStorage( |
| 71 metadata_dir, | 71 metadata_dir, |
| 72 blocking_task_runner_.get())); | 72 blocking_task_runner_.get())); |
| (...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 796 EXPECT_TRUE(base::ContentsEqual( | 796 EXPECT_TRUE(base::ContentsEqual( |
| 797 src_path, | 797 src_path, |
| 798 dest_directory.AppendASCII("baz00000002.png"))); | 798 dest_directory.AppendASCII("baz00000002.png"))); |
| 799 } | 799 } |
| 800 EXPECT_FALSE(base::PathExists( | 800 EXPECT_FALSE(base::PathExists( |
| 801 dest_directory.AppendASCII("image00000003.png"))); | 801 dest_directory.AppendASCII("image00000003.png"))); |
| 802 } | 802 } |
| 803 | 803 |
| 804 TEST_F(FileCacheTest, Iterator) { | 804 TEST_F(FileCacheTest, Iterator) { |
| 805 base::FilePath src_file; | 805 base::FilePath src_file; |
| 806 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); | 806 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); |
| 807 | 807 |
| 808 // Prepare entries. | 808 // Prepare entries. |
| 809 std::map<std::string, std::string> md5s; | 809 std::map<std::string, std::string> md5s; |
| 810 md5s["id1"] = "md5-1"; | 810 md5s["id1"] = "md5-1"; |
| 811 md5s["id2"] = "md5-2"; | 811 md5s["id2"] = "md5-2"; |
| 812 md5s["id3"] = "md5-3"; | 812 md5s["id3"] = "md5-3"; |
| 813 md5s["id4"] = "md5-4"; | 813 md5s["id4"] = "md5-4"; |
| 814 for (std::map<std::string, std::string>::iterator it = md5s.begin(); | 814 for (std::map<std::string, std::string>::iterator it = md5s.begin(); |
| 815 it != md5s.end(); ++it) { | 815 it != md5s.end(); ++it) { |
| 816 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( | 816 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( |
| 817 it->first, it->second, src_file, FileCache::FILE_OPERATION_COPY)); | 817 it->first, it->second, src_file, FileCache::FILE_OPERATION_COPY)); |
| 818 } | 818 } |
| 819 | 819 |
| 820 // Iterate. | 820 // Iterate. |
| 821 std::map<std::string, std::string> result; | 821 std::map<std::string, std::string> result; |
| 822 scoped_ptr<FileCache::Iterator> it = cache_->GetIterator(); | 822 scoped_ptr<FileCache::Iterator> it = cache_->GetIterator(); |
| 823 for (; !it->IsAtEnd(); it->Advance()) | 823 for (; !it->IsAtEnd(); it->Advance()) |
| 824 result[it->GetID()] = it->GetValue().md5(); | 824 result[it->GetID()] = it->GetValue().md5(); |
| 825 EXPECT_EQ(md5s, result); | 825 EXPECT_EQ(md5s, result); |
| 826 EXPECT_FALSE(it->HasError()); | 826 EXPECT_FALSE(it->HasError()); |
| 827 } | 827 } |
| 828 | 828 |
| 829 TEST_F(FileCacheTest, FreeDiskSpaceIfNeededFor) { | 829 TEST_F(FileCacheTest, FreeDiskSpaceIfNeededFor) { |
| 830 base::FilePath src_file; | 830 base::FilePath src_file; |
| 831 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); | 831 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); |
| 832 | 832 |
| 833 // Store a file as a 'temporary' file and remember the path. | 833 // Store a file as a 'temporary' file and remember the path. |
| 834 const std::string id_tmp = "id_tmp", md5_tmp = "md5_tmp"; | 834 const std::string id_tmp = "id_tmp", md5_tmp = "md5_tmp"; |
| 835 ASSERT_EQ(FILE_ERROR_OK, | 835 ASSERT_EQ(FILE_ERROR_OK, |
| 836 cache_->Store(id_tmp, md5_tmp, src_file, | 836 cache_->Store(id_tmp, md5_tmp, src_file, |
| 837 FileCache::FILE_OPERATION_COPY)); | 837 FileCache::FILE_OPERATION_COPY)); |
| 838 base::FilePath tmp_path; | 838 base::FilePath tmp_path; |
| 839 ASSERT_EQ(FILE_ERROR_OK, cache_->GetFile(id_tmp, &tmp_path)); | 839 ASSERT_EQ(FILE_ERROR_OK, cache_->GetFile(id_tmp, &tmp_path)); |
| 840 | 840 |
| 841 // Store a file as a pinned file and remember the path. | 841 // Store a file as a pinned file and remember the path. |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 952 &contents)); | 952 &contents)); |
| 953 EXPECT_EQ("kyu", contents); | 953 EXPECT_EQ("kyu", contents); |
| 954 } | 954 } |
| 955 | 955 |
| 956 TEST_F(FileCacheTest, ClearAll) { | 956 TEST_F(FileCacheTest, ClearAll) { |
| 957 const std::string id("pdf:1a2b"); | 957 const std::string id("pdf:1a2b"); |
| 958 const std::string md5("abcdef0123456789"); | 958 const std::string md5("abcdef0123456789"); |
| 959 | 959 |
| 960 // Store an existing file. | 960 // Store an existing file. |
| 961 base::FilePath src_file; | 961 base::FilePath src_file; |
| 962 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); | 962 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); |
| 963 ASSERT_EQ(FILE_ERROR_OK, | 963 ASSERT_EQ(FILE_ERROR_OK, |
| 964 cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY)); | 964 cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY)); |
| 965 | 965 |
| 966 // Verify that the cache entry is created. | 966 // Verify that the cache entry is created. |
| 967 FileCacheEntry cache_entry; | 967 FileCacheEntry cache_entry; |
| 968 ASSERT_TRUE(cache_->GetCacheEntry(id, &cache_entry)); | 968 ASSERT_TRUE(cache_->GetCacheEntry(id, &cache_entry)); |
| 969 | 969 |
| 970 // Clear cache. | 970 // Clear cache. |
| 971 EXPECT_TRUE(cache_->ClearAll()); | 971 EXPECT_TRUE(cache_->ClearAll()); |
| 972 | 972 |
| 973 // Verify that the cache is removed. | 973 // Verify that the cache is removed. |
| 974 EXPECT_FALSE(cache_->GetCacheEntry(id, &cache_entry)); | 974 EXPECT_FALSE(cache_->GetCacheEntry(id, &cache_entry)); |
| 975 EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_)); | 975 EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_)); |
| 976 } | 976 } |
| 977 | 977 |
| 978 } // namespace internal | 978 } // namespace internal |
| 979 } // namespace drive | 979 } // namespace drive |
| OLD | NEW |