| 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" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/threading/sequenced_worker_pool.h" | 13 #include "base/threading/sequenced_worker_pool.h" |
| 14 #include "chrome/browser/chromeos/drive/drive.pb.h" | 14 #include "chrome/browser/chromeos/drive/drive.pb.h" |
| 15 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h" | 15 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h" |
| 16 #include "chrome/browser/chromeos/drive/file_system_util.h" | 16 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 17 #include "chrome/browser/chromeos/drive/mock_file_cache_observer.h" | 17 #include "chrome/browser/chromeos/drive/mock_file_cache_observer.h" |
| 18 #include "chrome/browser/chromeos/drive/test_util.h" | 18 #include "chrome/browser/chromeos/drive/test_util.h" |
| 19 #include "chrome/browser/google_apis/test_util.h" | 19 #include "chrome/browser/google_apis/test_util.h" |
| 20 #include "content/public/test/test_browser_thread.h" | 20 #include "content/public/test/test_browser_thread.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 23 |
| 24 using ::testing::StrictMock; | 24 using ::testing::StrictMock; |
| 25 | 25 |
| 26 namespace drive { | 26 namespace drive { |
| 27 namespace internal { | 27 namespace internal { |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 struct PathToVerify { | |
| 31 PathToVerify(const base::FilePath& in_path_to_scan, | |
| 32 const base::FilePath& in_expected_existing_path) : | |
| 33 path_to_scan(in_path_to_scan), | |
| 34 expected_existing_path(in_expected_existing_path) { | |
| 35 } | |
| 36 | |
| 37 base::FilePath path_to_scan; | |
| 38 base::FilePath expected_existing_path; | |
| 39 }; | |
| 40 | |
| 41 // Copies results from Iterate(). | 30 // Copies results from Iterate(). |
| 42 void OnIterate(std::vector<std::string>* out_resource_ids, | 31 void OnIterate(std::vector<std::string>* out_resource_ids, |
| 43 std::vector<FileCacheEntry>* out_cache_entries, | 32 std::vector<FileCacheEntry>* out_cache_entries, |
| 44 const std::string& resource_id, | 33 const std::string& resource_id, |
| 45 const FileCacheEntry& cache_entry) { | 34 const FileCacheEntry& cache_entry) { |
| 46 out_resource_ids->push_back(resource_id); | 35 out_resource_ids->push_back(resource_id); |
| 47 out_cache_entries->push_back(cache_entry); | 36 out_cache_entries->push_back(cache_entry); |
| 48 } | 37 } |
| 49 | 38 |
| 50 // Called upon completion of Iterate(). | 39 // Called upon completion of Iterate(). |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 expected_error_ = expected_error; | 151 expected_error_ = expected_error; |
| 163 | 152 |
| 164 FileError error = FILE_ERROR_OK; | 153 FileError error = FILE_ERROR_OK; |
| 165 cache_->RemoveOnUIThread( | 154 cache_->RemoveOnUIThread( |
| 166 resource_id, | 155 resource_id, |
| 167 google_apis::test_util::CreateCopyResultCallback(&error)); | 156 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 168 google_apis::test_util::RunBlockingPoolTask(); | 157 google_apis::test_util::RunBlockingPoolTask(); |
| 169 VerifyRemoveFromCache(error, resource_id, ""); | 158 VerifyRemoveFromCache(error, resource_id, ""); |
| 170 } | 159 } |
| 171 | 160 |
| 161 // Returns number of files matching to |path_pattern|. |
| 162 int CountFilesWithPathPattern(const base::FilePath& path_pattern) { |
| 163 int result = 0; |
| 164 file_util::FileEnumerator enumerator( |
| 165 path_pattern.DirName(), false /* not recursive*/, |
| 166 file_util::FileEnumerator::FILES, |
| 167 path_pattern.BaseName().value()); |
| 168 for (base::FilePath current = enumerator.Next(); !current.empty(); |
| 169 current = enumerator.Next()) |
| 170 ++result; |
| 171 return result; |
| 172 } |
| 173 |
| 172 void VerifyRemoveFromCache(FileError error, | 174 void VerifyRemoveFromCache(FileError error, |
| 173 const std::string& resource_id, | 175 const std::string& resource_id, |
| 174 const std::string& md5) { | 176 const std::string& md5) { |
| 175 EXPECT_EQ(expected_error_, error); | 177 EXPECT_EQ(expected_error_, error); |
| 176 | 178 |
| 177 // Verify cache map. | 179 // Verify cache map. |
| 178 FileCacheEntry cache_entry; | 180 FileCacheEntry cache_entry; |
| 179 const bool cache_entry_found = | 181 const bool cache_entry_found = |
| 180 GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry); | 182 GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry); |
| 181 if (cache_entry_found) | 183 if (cache_entry_found) |
| 182 EXPECT_TRUE(cache_entry.is_dirty()); | 184 EXPECT_TRUE(cache_entry.is_dirty()); |
| 183 | 185 |
| 184 // If entry doesn't exist, verify that no files with "<resource_id>.*" | 186 // If entry doesn't exist, verify that no files with "<resource_id>.*" |
| 185 // exist in persistent and tmp dirs. | 187 // exist in persistent and tmp dirs. |
| 186 std::vector<PathToVerify> paths_to_verify; | 188 const base::FilePath path_pattern_tmp = |
| 187 paths_to_verify.push_back( // Index 0: CACHE_TYPE_TMP. | 189 cache_->GetCacheFilePath(resource_id, "*", |
| 188 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", | 190 FileCache::CACHE_TYPE_TMP, |
| 189 FileCache::CACHE_TYPE_TMP, | 191 FileCache::CACHED_FILE_FROM_SERVER); |
| 190 FileCache::CACHED_FILE_FROM_SERVER), base::FilePath())); | 192 const base::FilePath path_pattern_persistent = |
| 191 paths_to_verify.push_back( // Index 1: CACHE_TYPE_PERSISTENT. | 193 cache_->GetCacheFilePath(resource_id, "*", |
| 192 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", | 194 FileCache::CACHE_TYPE_PERSISTENT, |
| 193 FileCache::CACHE_TYPE_PERSISTENT, | 195 FileCache::CACHED_FILE_FROM_SERVER); |
| 194 FileCache::CACHED_FILE_FROM_SERVER), base::FilePath())); | 196 |
| 197 EXPECT_EQ(0, CountFilesWithPathPattern(path_pattern_tmp)); |
| 195 if (!cache_entry_found) { | 198 if (!cache_entry_found) { |
| 196 for (size_t i = 0; i < paths_to_verify.size(); ++i) { | 199 EXPECT_EQ(0, CountFilesWithPathPattern(path_pattern_persistent)); |
| 197 file_util::FileEnumerator enumerator( | |
| 198 paths_to_verify[i].path_to_scan.DirName(), false /* not recursive*/, | |
| 199 file_util::FileEnumerator::FILES, | |
| 200 paths_to_verify[i].path_to_scan.BaseName().value()); | |
| 201 EXPECT_TRUE(enumerator.Next().empty()); | |
| 202 } | |
| 203 } else { | 200 } else { |
| 204 // Entry is dirty, verify that: | 201 // Entry is dirty, verify that only 1 "<resource_id>.local" exists in |
| 205 // - no files with "<resource_id>.*" exist in tmp dir | 202 // persistent dir. |
| 206 // - only 1 "<resource_id>.local" exists in persistent dir | 203 EXPECT_EQ(1, CountFilesWithPathPattern(path_pattern_persistent)); |
| 207 // - if entry is pinned, only 1 <resource_id> exists in pinned dir. | 204 EXPECT_TRUE(file_util::PathExists( |
| 208 | |
| 209 // Change expected_existing_path of CACHE_TYPE_PERSISTENT (index 1). | |
| 210 paths_to_verify[1].expected_existing_path = | |
| 211 GetCacheFilePath(resource_id, | 205 GetCacheFilePath(resource_id, |
| 212 std::string(), | 206 std::string(), |
| 213 FileCache::CACHE_TYPE_PERSISTENT, | 207 FileCache::CACHE_TYPE_PERSISTENT, |
| 214 FileCache::CACHED_FILE_LOCALLY_MODIFIED); | 208 FileCache::CACHED_FILE_LOCALLY_MODIFIED))); |
| 215 | |
| 216 for (size_t i = 0; i < paths_to_verify.size(); ++i) { | |
| 217 const struct PathToVerify& verify = paths_to_verify[i]; | |
| 218 file_util::FileEnumerator enumerator( | |
| 219 verify.path_to_scan.DirName(), false /* not recursive */, | |
| 220 file_util::FileEnumerator::FILES, | |
| 221 verify.path_to_scan.BaseName().value()); | |
| 222 size_t num_files_found = 0; | |
| 223 for (base::FilePath current = enumerator.Next(); !current.empty(); | |
| 224 current = enumerator.Next()) { | |
| 225 ++num_files_found; | |
| 226 EXPECT_EQ(verify.expected_existing_path, current); | |
| 227 } | |
| 228 if (verify.expected_existing_path.empty()) | |
| 229 EXPECT_EQ(0U, num_files_found); | |
| 230 else | |
| 231 EXPECT_EQ(1U, num_files_found); | |
| 232 } | |
| 233 } | 209 } |
| 234 } | 210 } |
| 235 | 211 |
| 236 void TestPin( | 212 void TestPin( |
| 237 const std::string& resource_id, | 213 const std::string& resource_id, |
| 238 const std::string& md5, | 214 const std::string& md5, |
| 239 FileError expected_error, | 215 FileError expected_error, |
| 240 int expected_cache_state, | 216 int expected_cache_state, |
| 241 FileCache::CacheSubDirectoryType expected_sub_dir_type) { | 217 FileCache::CacheSubDirectoryType expected_sub_dir_type) { |
| 242 expected_error_ = expected_error; | 218 expected_error_ = expected_error; |
| (...skipping 1068 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1311 google_apis::test_util::GetTestFilePath("chromeos/gdata/empty_feed.json"), | 1287 google_apis::test_util::GetTestFilePath("chromeos/gdata/empty_feed.json"), |
| 1312 FILE_ERROR_OK, | 1288 FILE_ERROR_OK, |
| 1313 test_util::TEST_CACHE_STATE_PRESENT | | 1289 test_util::TEST_CACHE_STATE_PRESENT | |
| 1314 test_util::TEST_CACHE_STATE_PINNED | | 1290 test_util::TEST_CACHE_STATE_PINNED | |
| 1315 test_util::TEST_CACHE_STATE_PERSISTENT, | 1291 test_util::TEST_CACHE_STATE_PERSISTENT, |
| 1316 FileCache::CACHE_TYPE_PERSISTENT); | 1292 FileCache::CACHE_TYPE_PERSISTENT); |
| 1317 } | 1293 } |
| 1318 | 1294 |
| 1319 } // namespace internal | 1295 } // namespace internal |
| 1320 } // namespace drive | 1296 } // namespace drive |
| OLD | NEW |