Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / remove_stale_cache_files_unittest.cc
blob2fed2f97c89570fa58ac9b66b10c9e154dc3019a
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
3 // found in the LICENSE file.
5 #include <string>
7 #include "base/callback_helpers.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/run_loop.h"
13 #include "chrome/browser/chromeos/drive/drive.pb.h"
14 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h"
15 #include "chrome/browser/chromeos/drive/file_system_util.h"
16 #include "chrome/browser/chromeos/drive/remove_stale_cache_files.h"
17 #include "chrome/browser/chromeos/drive/resource_metadata.h"
18 #include "chrome/browser/chromeos/drive/test_util.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "google_apis/drive/test_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace drive {
24 namespace internal {
26 class RemoveStaleCacheFilesTest : public testing::Test {
27 protected:
28 virtual void SetUp() OVERRIDE {
29 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
31 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter);
33 metadata_storage_.reset(new ResourceMetadataStorage(
34 temp_dir_.path(), base::MessageLoopProxy::current().get()));
36 cache_.reset(new FileCache(metadata_storage_.get(),
37 temp_dir_.path(),
38 base::MessageLoopProxy::current().get(),
39 fake_free_disk_space_getter_.get()));
41 resource_metadata_.reset(new ResourceMetadata(
42 metadata_storage_.get(), base::MessageLoopProxy::current()));
44 ASSERT_TRUE(metadata_storage_->Initialize());
45 ASSERT_TRUE(cache_->Initialize());
46 ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->Initialize());
49 content::TestBrowserThreadBundle thread_bundle_;
50 base::ScopedTempDir temp_dir_;
52 scoped_ptr<ResourceMetadataStorage,
53 test_util::DestroyHelperForTests> metadata_storage_;
54 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
55 scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests>
56 resource_metadata_;
57 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_;
60 TEST_F(RemoveStaleCacheFilesTest, RemoveStaleCacheFiles) {
61 base::FilePath dummy_file;
62 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &dummy_file));
63 std::string local_id("pdf:1a2b3c");
64 std::string md5("abcdef0123456789");
66 // Create a stale cache file.
67 EXPECT_EQ(FILE_ERROR_OK,
68 cache_->Store(local_id, md5, dummy_file,
69 FileCache::FILE_OPERATION_COPY));
71 // Verify that the cache entry exists.
72 FileCacheEntry cache_entry;
73 EXPECT_TRUE(cache_->GetCacheEntry(local_id, &cache_entry));
75 ResourceEntry entry;
76 EXPECT_EQ(FILE_ERROR_NOT_FOUND,
77 resource_metadata_->GetResourceEntryById(local_id, &entry));
79 // Remove stale cache files.
80 RemoveStaleCacheFiles(cache_.get(), resource_metadata_.get());
82 // Verify that the cache entry is deleted.
83 EXPECT_FALSE(cache_->GetCacheEntry(local_id, &cache_entry));
86 TEST_F(RemoveStaleCacheFilesTest, DirtyCacheFiles) {
87 base::FilePath dummy_file;
88 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &dummy_file));
90 // Dirty and deleted (= absent in resource_metada) cache entry.
91 std::string local_id_1("file:1");
92 std::string md5_1("abcdef0123456789");
93 EXPECT_EQ(FILE_ERROR_OK,
94 cache_->Store(local_id_1, md5_1, dummy_file,
95 FileCache::FILE_OPERATION_COPY));
96 scoped_ptr<base::ScopedClosureRunner> file_closer;
97 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(local_id_1, &file_closer));
99 // Dirty and mismatching-MD5 entry.
100 std::string md5_2_cache("0123456789abcdef");
101 std::string md5_2_metadata("abcdef0123456789");
103 ResourceEntry entry;
104 std::string local_id_2;
105 entry.set_resource_id("resource_id");
106 entry.mutable_file_specific_info()->set_md5(md5_2_metadata);
107 entry.set_parent_local_id(util::kDriveGrandRootLocalId);
108 entry.set_title("file.txt");
109 resource_metadata_->AddEntry(entry, &local_id_2);
111 EXPECT_EQ(FILE_ERROR_OK,
112 cache_->Store(local_id_2, md5_2_cache, dummy_file,
113 FileCache::FILE_OPERATION_COPY));
114 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(local_id_2, &file_closer));
116 // Remove stale cache files.
117 RemoveStaleCacheFiles(cache_.get(), resource_metadata_.get());
119 // Dirty cache should be removed if and only if the entry does not exist in
120 // resource_metadata.
121 FileCacheEntry cache_entry;
122 EXPECT_FALSE(cache_->GetCacheEntry(local_id_1, &cache_entry));
123 EXPECT_TRUE(cache_->GetCacheEntry(local_id_2, &cache_entry));
126 } // namespace internal
127 } // namespace drive