Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: chrome/browser/chromeos/drive/file_cache_unittest.cc

Issue 23462021: Move out Drive cache file path constants outside of file_system_util.h. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 // pool and tests the interaction among threads. 55 // pool and tests the interaction among threads.
56 // TODO(hashimoto): remove this class. crbug.com/231221. 56 // TODO(hashimoto): remove this class. crbug.com/231221.
57 class FileCacheTestOnUIThread : public testing::Test { 57 class FileCacheTestOnUIThread : public testing::Test {
58 protected: 58 protected:
59 FileCacheTestOnUIThread() : expected_error_(FILE_ERROR_OK), 59 FileCacheTestOnUIThread() : expected_error_(FILE_ERROR_OK),
60 expected_cache_state_(0) { 60 expected_cache_state_(0) {
61 } 61 }
62 62
63 virtual void SetUp() OVERRIDE { 63 virtual void SetUp() OVERRIDE {
64 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 64 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
65 ASSERT_TRUE(file_util::CreateDirectory( 65 const base::FilePath metadata_dir = temp_dir_.path().AppendASCII("meta");
66 temp_dir_.path().Append(util::kMetadataDirectory))); 66 const base::FilePath cache_dir = temp_dir_.path().AppendASCII("files");
67 ASSERT_TRUE(file_util::CreateDirectory( 67
68 temp_dir_.path().Append(util::kCacheFileDirectory))); 68 ASSERT_TRUE(file_util::CreateDirectory(metadata_dir));
69 ASSERT_TRUE(file_util::CreateDirectory(cache_dir));
69 70
70 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), 71 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(),
71 &dummy_file_path_)); 72 &dummy_file_path_));
72 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); 73 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter);
73 74
74 scoped_refptr<base::SequencedWorkerPool> pool = 75 scoped_refptr<base::SequencedWorkerPool> pool =
75 content::BrowserThread::GetBlockingPool(); 76 content::BrowserThread::GetBlockingPool();
76 blocking_task_runner_ = 77 blocking_task_runner_ =
77 pool->GetSequencedTaskRunner(pool->GetSequenceToken()); 78 pool->GetSequencedTaskRunner(pool->GetSequenceToken());
78 79
79 metadata_storage_.reset(new ResourceMetadataStorage( 80 metadata_storage_.reset(new ResourceMetadataStorage(
80 temp_dir_.path(), blocking_task_runner_.get())); 81 metadata_dir,
82 blocking_task_runner_.get()));
81 83
82 bool success = false; 84 bool success = false;
83 base::PostTaskAndReplyWithResult( 85 base::PostTaskAndReplyWithResult(
84 blocking_task_runner_.get(), 86 blocking_task_runner_.get(),
85 FROM_HERE, 87 FROM_HERE,
86 base::Bind(&ResourceMetadataStorage::Initialize, 88 base::Bind(&ResourceMetadataStorage::Initialize,
87 base::Unretained(metadata_storage_.get())), 89 base::Unretained(metadata_storage_.get())),
88 google_apis::test_util::CreateCopyResultCallback(&success)); 90 google_apis::test_util::CreateCopyResultCallback(&success));
89 test_util::RunBlockingPoolTask(); 91 test_util::RunBlockingPoolTask();
90 ASSERT_TRUE(success); 92 ASSERT_TRUE(success);
91 93
92 cache_.reset(new FileCache( 94 cache_.reset(new FileCache(
93 metadata_storage_.get(), 95 metadata_storage_.get(),
94 temp_dir_.path().Append(util::kCacheFileDirectory), 96 cache_dir,
95 blocking_task_runner_.get(), 97 blocking_task_runner_.get(),
96 fake_free_disk_space_getter_.get())); 98 fake_free_disk_space_getter_.get()));
97 99
98 success = false; 100 success = false;
99 base::PostTaskAndReplyWithResult( 101 base::PostTaskAndReplyWithResult(
100 blocking_task_runner_.get(), 102 blocking_task_runner_.get(),
101 FROM_HERE, 103 FROM_HERE,
102 base::Bind(&FileCache::Initialize, base::Unretained(cache_.get())), 104 base::Bind(&FileCache::Initialize, base::Unretained(cache_.get())),
103 google_apis::test_util::CreateCopyResultCallback(&success)); 105 google_apis::test_util::CreateCopyResultCallback(&success));
104 test_util::RunBlockingPoolTask(); 106 test_util::RunBlockingPoolTask();
(...skipping 686 matching lines...) Expand 10 before | Expand all | Expand 10 after
791 // Store the file with a modified content and md5. It should stay pinned. 793 // Store the file with a modified content and md5. It should stay pinned.
792 TestStoreToCache(id, md5_modified, dummy_file_path_, FILE_ERROR_OK, 794 TestStoreToCache(id, md5_modified, dummy_file_path_, FILE_ERROR_OK,
793 TEST_CACHE_STATE_PRESENT | TEST_CACHE_STATE_PINNED); 795 TEST_CACHE_STATE_PRESENT | TEST_CACHE_STATE_PINNED);
794 } 796 }
795 797
796 // Tests FileCache methods working with the blocking task runner. 798 // Tests FileCache methods working with the blocking task runner.
797 class FileCacheTest : public testing::Test { 799 class FileCacheTest : public testing::Test {
798 protected: 800 protected:
799 virtual void SetUp() OVERRIDE { 801 virtual void SetUp() OVERRIDE {
800 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 802 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
801 ASSERT_TRUE(file_util::CreateDirectory( 803 const base::FilePath metadata_dir = temp_dir_.path().AppendASCII("meta");
802 temp_dir_.path().Append(util::kMetadataDirectory))); 804 const base::FilePath cache_dir = temp_dir_.path().AppendASCII("files");
803 ASSERT_TRUE(file_util::CreateDirectory( 805
804 temp_dir_.path().Append(util::kCacheFileDirectory))); 806 ASSERT_TRUE(file_util::CreateDirectory(metadata_dir));
807 ASSERT_TRUE(file_util::CreateDirectory(cache_dir));
805 808
806 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); 809 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter);
807 810
808 metadata_storage_.reset(new ResourceMetadataStorage( 811 metadata_storage_.reset(new ResourceMetadataStorage(
809 temp_dir_.path().Append(util::kMetadataDirectory), 812 metadata_dir,
810 base::MessageLoopProxy::current().get())); 813 base::MessageLoopProxy::current().get()));
811 ASSERT_TRUE(metadata_storage_->Initialize()); 814 ASSERT_TRUE(metadata_storage_->Initialize());
812 815
813 cache_.reset(new FileCache( 816 cache_.reset(new FileCache(
814 metadata_storage_.get(), 817 metadata_storage_.get(),
815 temp_dir_.path().Append(util::kCacheFileDirectory), 818 cache_dir,
816 base::MessageLoopProxy::current().get(), 819 base::MessageLoopProxy::current().get(),
817 fake_free_disk_space_getter_.get())); 820 fake_free_disk_space_getter_.get()));
818 ASSERT_TRUE(cache_->Initialize()); 821 ASSERT_TRUE(cache_->Initialize());
819 } 822 }
820 823
821 static void RenameCacheFilesToNewFormat(FileCache* cache) { 824 static void RenameCacheFilesToNewFormat(FileCache* cache) {
822 cache->RenameCacheFilesToNewFormat(); 825 cache->RenameCacheFilesToNewFormat();
823 } 826 }
824 827
825 content::TestBrowserThreadBundle thread_bundle_; 828 content::TestBrowserThreadBundle thread_bundle_;
826 base::ScopedTempDir temp_dir_; 829 base::ScopedTempDir temp_dir_;
827 830
828 scoped_ptr<ResourceMetadataStorage, test_util::DestroyHelperForTests> 831 scoped_ptr<ResourceMetadataStorage, test_util::DestroyHelperForTests>
829 metadata_storage_; 832 metadata_storage_;
830 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; 833 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
831 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; 834 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_;
832 }; 835 };
833 836
834 TEST_F(FileCacheTest, ScanCacheFile) { 837 TEST_F(FileCacheTest, ScanCacheFile) {
835 // Set up files in the cache directory. 838 // Set up files in the cache directory.
836 const base::FilePath file_directory = 839 const base::FilePath file_directory = temp_dir_.path().AppendASCII("files");
837 temp_dir_.path().Append(util::kCacheFileDirectory);
838 ASSERT_TRUE(google_apis::test_util::WriteStringToFile( 840 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(
839 file_directory.AppendASCII("id_foo"), "foo")); 841 file_directory.AppendASCII("id_foo"), "foo"));
840 842
841 // Remove the existing DB. 843 // Remove the existing DB.
842 const base::FilePath metadata_directory = 844 const base::FilePath metadata_directory =
843 temp_dir_.path().Append(util::kMetadataDirectory); 845 temp_dir_.path().AppendASCII("meta");
844 ASSERT_TRUE(base::DeleteFile(metadata_directory, true /* recursive */)); 846 ASSERT_TRUE(base::DeleteFile(metadata_directory, true /* recursive */));
845 847
846 // Create a new cache and initialize it. 848 // Create a new cache and initialize it.
847 metadata_storage_.reset(new ResourceMetadataStorage( 849 metadata_storage_.reset(new ResourceMetadataStorage(
848 metadata_directory, base::MessageLoopProxy::current().get())); 850 metadata_directory, base::MessageLoopProxy::current().get()));
849 ASSERT_TRUE(metadata_storage_->Initialize()); 851 ASSERT_TRUE(metadata_storage_->Initialize());
850 852
851 cache_.reset(new FileCache(metadata_storage_.get(), 853 cache_.reset(new FileCache(metadata_storage_.get(),
852 temp_dir_.path().Append(util::kCacheFileDirectory), 854 file_directory,
853 base::MessageLoopProxy::current().get(), 855 base::MessageLoopProxy::current().get(),
854 fake_free_disk_space_getter_.get())); 856 fake_free_disk_space_getter_.get()));
855 ASSERT_TRUE(cache_->Initialize()); 857 ASSERT_TRUE(cache_->Initialize());
856 858
857 // Check contents of the cache. 859 // Check contents of the cache.
858 FileCacheEntry cache_entry; 860 FileCacheEntry cache_entry;
859 EXPECT_TRUE(cache_->GetCacheEntry("id_foo", &cache_entry)); 861 EXPECT_TRUE(cache_->GetCacheEntry("id_foo", &cache_entry));
860 EXPECT_TRUE(cache_entry.is_present()); 862 EXPECT_TRUE(cache_entry.is_present());
861 EXPECT_EQ(base::MD5String("foo"), cache_entry.md5()); 863 EXPECT_EQ(base::MD5String("foo"), cache_entry.md5());
862 } 864 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
895 897
896 EXPECT_TRUE(cache_->GetCacheEntry(id_pinned, &entry)); 898 EXPECT_TRUE(cache_->GetCacheEntry(id_pinned, &entry));
897 EXPECT_TRUE(base::PathExists(pinned_path)); 899 EXPECT_TRUE(base::PathExists(pinned_path));
898 900
899 // Returns false when disk space cannot be freed. 901 // Returns false when disk space cannot be freed.
900 fake_free_disk_space_getter_->set_default_value(0); 902 fake_free_disk_space_getter_->set_default_value(0);
901 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); 903 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes));
902 } 904 }
903 905
904 TEST_F(FileCacheTest, RenameCacheFilesToNewFormat) { 906 TEST_F(FileCacheTest, RenameCacheFilesToNewFormat) {
905 const base::FilePath file_directory = 907 const base::FilePath file_directory = temp_dir_.path().AppendASCII("files");
906 temp_dir_.path().Append(util::kCacheFileDirectory);
907 908
908 // File with an old style "<ID>.<MD5>" name. 909 // File with an old style "<ID>.<MD5>" name.
909 ASSERT_TRUE(google_apis::test_util::WriteStringToFile( 910 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(
910 file_directory.AppendASCII("id_koo.md5"), "koo")); 911 file_directory.AppendASCII("id_koo.md5"), "koo"));
911 912
912 // File with multiple extensions should be removed. 913 // File with multiple extensions should be removed.
913 ASSERT_TRUE(google_apis::test_util::WriteStringToFile( 914 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(
914 file_directory.AppendASCII("id_kyu.md5.mounted"), "kyu (mounted)")); 915 file_directory.AppendASCII("id_kyu.md5.mounted"), "kyu (mounted)"));
915 ASSERT_TRUE(google_apis::test_util::WriteStringToFile( 916 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(
916 file_directory.AppendASCII("id_kyu.md5"), "kyu")); 917 file_directory.AppendASCII("id_kyu.md5"), "kyu"));
(...skipping 18 matching lines...) Expand all
935 &contents)); 936 &contents));
936 EXPECT_EQ("koo", contents); 937 EXPECT_EQ("koo", contents);
937 contents.clear(); 938 contents.clear();
938 EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_kyu"), 939 EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_kyu"),
939 &contents)); 940 &contents));
940 EXPECT_EQ("kyu", contents); 941 EXPECT_EQ("kyu", contents);
941 } 942 }
942 943
943 } // namespace internal 944 } // namespace internal
944 } // namespace drive 945 } // namespace drive
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/drive/drive_integration_service.cc ('k') | chrome/browser/chromeos/drive/file_system_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698