| 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 <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 #include "testing/gmock/include/gmock/gmock.h" | 33 #include "testing/gmock/include/gmock/gmock.h" |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 35 | 35 |
| 36 using ::testing::AtLeast; | 36 using ::testing::AtLeast; |
| 37 using ::testing::Eq; | 37 using ::testing::Eq; |
| 38 using ::testing::NotNull; | 38 using ::testing::NotNull; |
| 39 using ::testing::Return; | 39 using ::testing::Return; |
| 40 using ::testing::StrictMock; | 40 using ::testing::StrictMock; |
| 41 using ::testing::_; | 41 using ::testing::_; |
| 42 | 42 |
| 43 namespace gdata { | 43 namespace drive { |
| 44 namespace { | 44 namespace { |
| 45 | 45 |
| 46 const int64 kLotsOfSpace = kMinFreeSpace * 10; | 46 const int64 kLotsOfSpace = kMinFreeSpace * 10; |
| 47 | 47 |
| 48 // Callback for DriveCache::StoreOnUIThread used in RemoveStaleCacheFiles test. | 48 // Callback for DriveCache::StoreOnUIThread used in RemoveStaleCacheFiles test. |
| 49 // Verifies that the result is not an error. | 49 // Verifies that the result is not an error. |
| 50 void VerifyCacheFileState(DriveFileError error, | 50 void VerifyCacheFileState(DriveFileError error, |
| 51 const std::string& resource_id, | 51 const std::string& resource_id, |
| 52 const std::string& md5) { | 52 const std::string& md5) { |
| 53 EXPECT_EQ(DRIVE_FILE_OK, error); | 53 EXPECT_EQ(DRIVE_FILE_OK, error); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 | 106 |
| 107 mock_directory_observer_.reset(new StrictMock<MockDirectoryChangeObserver>); | 107 mock_directory_observer_.reset(new StrictMock<MockDirectoryChangeObserver>); |
| 108 file_system_->AddObserver(mock_directory_observer_.get()); | 108 file_system_->AddObserver(mock_directory_observer_.get()); |
| 109 | 109 |
| 110 file_system_->Initialize(); | 110 file_system_->Initialize(); |
| 111 cache_->RequestInitializeOnUIThreadForTesting(); | 111 cache_->RequestInitializeOnUIThreadForTesting(); |
| 112 | 112 |
| 113 stale_cache_files_remover_.reset(new StaleCacheFilesRemover(file_system_, | 113 stale_cache_files_remover_.reset(new StaleCacheFilesRemover(file_system_, |
| 114 cache_)); | 114 cache_)); |
| 115 | 115 |
| 116 test_util::RunBlockingPoolTask(); | 116 gdata::test_util::RunBlockingPoolTask(); |
| 117 } | 117 } |
| 118 | 118 |
| 119 virtual void TearDown() OVERRIDE { | 119 virtual void TearDown() OVERRIDE { |
| 120 ASSERT_TRUE(file_system_); | 120 ASSERT_TRUE(file_system_); |
| 121 stale_cache_files_remover_.reset(); | 121 stale_cache_files_remover_.reset(); |
| 122 EXPECT_CALL(*mock_drive_service_, CancelAll()).Times(1); | 122 EXPECT_CALL(*mock_drive_service_, CancelAll()).Times(1); |
| 123 delete file_system_; | 123 delete file_system_; |
| 124 file_system_ = NULL; | 124 file_system_ = NULL; |
| 125 delete mock_drive_service_; | 125 delete mock_drive_service_; |
| 126 mock_drive_service_ = NULL; | 126 mock_drive_service_ = NULL; |
| 127 SetFreeDiskSpaceGetterForTesting(NULL); | 127 SetFreeDiskSpaceGetterForTesting(NULL); |
| 128 cache_->DestroyOnUIThread(); | 128 cache_->DestroyOnUIThread(); |
| 129 // The cache destruction requires to post a task to the blocking pool. | 129 // The cache destruction requires to post a task to the blocking pool. |
| 130 test_util::RunBlockingPoolTask(); | 130 gdata::test_util::RunBlockingPoolTask(); |
| 131 | 131 |
| 132 profile_.reset(NULL); | 132 profile_.reset(NULL); |
| 133 } | 133 } |
| 134 | 134 |
| 135 // Loads test json file as root ("/drive") element. | 135 // Loads test json file as root ("/drive") element. |
| 136 void LoadRootFeedDocument(const std::string& filename) { | 136 void LoadRootFeedDocument(const std::string& filename) { |
| 137 test_util::LoadChangeFeed(filename, | 137 test_util::LoadChangeFeed(filename, |
| 138 file_system_, | 138 file_system_, |
| 139 0, | 139 0, |
| 140 root_feed_changestamp_++); | 140 root_feed_changestamp_++); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 154 scoped_ptr<StrictMock<MockDriveWebAppsRegistry> > mock_webapps_registry_; | 154 scoped_ptr<StrictMock<MockDriveWebAppsRegistry> > mock_webapps_registry_; |
| 155 StrictMock<MockFreeDiskSpaceGetter>* mock_free_disk_space_checker_; | 155 StrictMock<MockFreeDiskSpaceGetter>* mock_free_disk_space_checker_; |
| 156 scoped_ptr<StrictMock<MockDriveCacheObserver> > mock_cache_observer_; | 156 scoped_ptr<StrictMock<MockDriveCacheObserver> > mock_cache_observer_; |
| 157 scoped_ptr<StrictMock<MockDirectoryChangeObserver> > mock_directory_observer_; | 157 scoped_ptr<StrictMock<MockDirectoryChangeObserver> > mock_directory_observer_; |
| 158 scoped_ptr<StaleCacheFilesRemover> stale_cache_files_remover_; | 158 scoped_ptr<StaleCacheFilesRemover> stale_cache_files_remover_; |
| 159 | 159 |
| 160 int root_feed_changestamp_; | 160 int root_feed_changestamp_; |
| 161 }; | 161 }; |
| 162 | 162 |
| 163 TEST_F(StaleCacheFilesRemoverTest, RemoveStaleCacheFiles) { | 163 TEST_F(StaleCacheFilesRemoverTest, RemoveStaleCacheFiles) { |
| 164 FilePath dummy_file = test_util::GetTestFilePath("gdata/root_feed.json"); | 164 FilePath dummy_file = |
| 165 gdata::test_util::GetTestFilePath("gdata/root_feed.json"); |
| 165 std::string resource_id("pdf:1a2b3c"); | 166 std::string resource_id("pdf:1a2b3c"); |
| 166 std::string md5("abcdef0123456789"); | 167 std::string md5("abcdef0123456789"); |
| 167 | 168 |
| 168 EXPECT_CALL(*mock_free_disk_space_checker_, AmountOfFreeDiskSpace()) | 169 EXPECT_CALL(*mock_free_disk_space_checker_, AmountOfFreeDiskSpace()) |
| 169 .Times(AtLeast(1)).WillRepeatedly(Return(kLotsOfSpace)); | 170 .Times(AtLeast(1)).WillRepeatedly(Return(kLotsOfSpace)); |
| 170 | 171 |
| 171 // Create a stale cache file. | 172 // Create a stale cache file. |
| 172 cache_->StoreOnUIThread(resource_id, md5, dummy_file, | 173 cache_->StoreOnUIThread(resource_id, md5, dummy_file, |
| 173 DriveCache::FILE_OPERATION_COPY, | 174 DriveCache::FILE_OPERATION_COPY, |
| 174 base::Bind(&gdata::VerifyCacheFileState)); | 175 base::Bind(&VerifyCacheFileState)); |
| 175 test_util::RunBlockingPoolTask(); | 176 gdata::test_util::RunBlockingPoolTask(); |
| 176 | 177 |
| 177 // Verify that the cache file exists. | 178 // Verify that the cache file exists. |
| 178 FilePath path = cache_->GetCacheFilePath(resource_id, | 179 FilePath path = cache_->GetCacheFilePath(resource_id, |
| 179 md5, | 180 md5, |
| 180 DriveCache::CACHE_TYPE_TMP, | 181 DriveCache::CACHE_TYPE_TMP, |
| 181 DriveCache::CACHED_FILE_FROM_SERVER); | 182 DriveCache::CACHED_FILE_FROM_SERVER); |
| 182 EXPECT_TRUE(file_util::PathExists(path)); | 183 EXPECT_TRUE(file_util::PathExists(path)); |
| 183 | 184 |
| 184 // Verify that the corresponding file entry doesn't exist. | 185 // Verify that the corresponding file entry doesn't exist. |
| 185 EXPECT_CALL(*mock_drive_service_, GetAccountMetadata(_)).Times(1); | 186 EXPECT_CALL(*mock_drive_service_, GetAccountMetadata(_)).Times(1); |
| 186 EXPECT_CALL(*mock_drive_service_, GetDocuments(Eq(GURL()), _, "", _, _)) | 187 EXPECT_CALL(*mock_drive_service_, GetDocuments(Eq(GURL()), _, "", _, _)) |
| 187 .Times(1); | 188 .Times(1); |
| 188 EXPECT_CALL(*mock_webapps_registry_, UpdateFromFeed(_)).Times(1); | 189 EXPECT_CALL(*mock_webapps_registry_, UpdateFromFeed(_)).Times(1); |
| 189 | 190 |
| 190 DriveFileError error(DRIVE_FILE_OK); | 191 DriveFileError error(DRIVE_FILE_OK); |
| 191 FilePath unused; | 192 FilePath unused; |
| 192 scoped_ptr<DriveEntryProto> entry_proto; | 193 scoped_ptr<DriveEntryProto> entry_proto; |
| 193 file_system_->GetEntryInfoByResourceId( | 194 file_system_->GetEntryInfoByResourceId( |
| 194 resource_id, | 195 resource_id, |
| 195 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, | 196 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, |
| 196 &error, | 197 &error, |
| 197 &unused, | 198 &unused, |
| 198 &entry_proto)); | 199 &entry_proto)); |
| 199 test_util::RunBlockingPoolTask(); | 200 gdata::test_util::RunBlockingPoolTask(); |
| 200 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); | 201 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); |
| 201 | 202 |
| 202 file_system_->GetEntryInfoByPath( | 203 file_system_->GetEntryInfoByPath( |
| 203 path, | 204 path, |
| 204 base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback, | 205 base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback, |
| 205 &error, | 206 &error, |
| 206 &entry_proto)); | 207 &entry_proto)); |
| 207 test_util::RunBlockingPoolTask(); | 208 gdata::test_util::RunBlockingPoolTask(); |
| 208 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); | 209 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); |
| 209 EXPECT_FALSE(entry_proto.get()); | 210 EXPECT_FALSE(entry_proto.get()); |
| 210 | 211 |
| 211 // Load a root feed. | 212 // Load a root feed. |
| 212 LoadRootFeedDocument("gdata/root_feed.json"); | 213 LoadRootFeedDocument("gdata/root_feed.json"); |
| 213 | 214 |
| 214 // Wait for StaleCacheFilesRemover to finish cleaning up the stale file. | 215 // Wait for StaleCacheFilesRemover to finish cleaning up the stale file. |
| 215 test_util::RunBlockingPoolTask(); | 216 gdata::test_util::RunBlockingPoolTask(); |
| 216 | 217 |
| 217 // Verify that the cache file is deleted. | 218 // Verify that the cache file is deleted. |
| 218 path = cache_->GetCacheFilePath(resource_id, | 219 path = cache_->GetCacheFilePath(resource_id, |
| 219 md5, | 220 md5, |
| 220 DriveCache::CACHE_TYPE_TMP, | 221 DriveCache::CACHE_TYPE_TMP, |
| 221 DriveCache::CACHED_FILE_FROM_SERVER); | 222 DriveCache::CACHED_FILE_FROM_SERVER); |
| 222 EXPECT_FALSE(file_util::PathExists(path)); | 223 EXPECT_FALSE(file_util::PathExists(path)); |
| 223 } | 224 } |
| 224 | 225 |
| 225 } // namespace gdata | 226 } // namespace drive |
| OLD | NEW |