| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/drive/sync/content_update_performer.h" | |
| 6 | |
| 7 #include "base/callback_helpers.h" | |
| 8 #include "base/file_util.h" | |
| 9 #include "base/md5.h" | |
| 10 #include "base/task_runner_util.h" | |
| 11 #include "chrome/browser/chromeos/drive/file_cache.h" | |
| 12 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" | |
| 13 #include "chrome/browser/chromeos/drive/file_system_interface.h" | |
| 14 #include "chrome/browser/drive/fake_drive_service.h" | |
| 15 #include "google_apis/drive/drive_api_parser.h" | |
| 16 #include "google_apis/drive/gdata_wapi_parser.h" | |
| 17 #include "google_apis/drive/test_util.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace drive { | |
| 21 namespace file_system { | |
| 22 | |
| 23 class ContentUpdatePerformerTest : public OperationTestBase { | |
| 24 protected: | |
| 25 virtual void SetUp() OVERRIDE { | |
| 26 OperationTestBase::SetUp(); | |
| 27 performer_.reset(new ContentUpdatePerformer(blocking_task_runner(), | |
| 28 scheduler(), | |
| 29 metadata(), | |
| 30 cache())); | |
| 31 } | |
| 32 | |
| 33 // Stores |content| to the cache and mark it as dirty. | |
| 34 FileError StoreAndMarkDirty(const std::string& local_id, | |
| 35 const std::string& content) { | |
| 36 base::FilePath path; | |
| 37 if (!base::CreateTemporaryFileInDir(temp_dir(), &path) || | |
| 38 !google_apis::test_util::WriteStringToFile(path, content)) | |
| 39 return FILE_ERROR_FAILED; | |
| 40 | |
| 41 // Store the file to cache. | |
| 42 FileError error = FILE_ERROR_FAILED; | |
| 43 base::PostTaskAndReplyWithResult( | |
| 44 blocking_task_runner(), | |
| 45 FROM_HERE, | |
| 46 base::Bind(&internal::FileCache::Store, | |
| 47 base::Unretained(cache()), | |
| 48 local_id, base::MD5String(content), path, | |
| 49 internal::FileCache::FILE_OPERATION_COPY), | |
| 50 google_apis::test_util::CreateCopyResultCallback(&error)); | |
| 51 test_util::RunBlockingPoolTask(); | |
| 52 if (error != FILE_ERROR_OK) | |
| 53 return error; | |
| 54 | |
| 55 // Add the dirty bit. | |
| 56 error = FILE_ERROR_FAILED; | |
| 57 scoped_ptr<base::ScopedClosureRunner> file_closer; | |
| 58 base::PostTaskAndReplyWithResult( | |
| 59 blocking_task_runner(), | |
| 60 FROM_HERE, | |
| 61 base::Bind(&internal::FileCache::OpenForWrite, | |
| 62 base::Unretained(cache()), | |
| 63 local_id, | |
| 64 &file_closer), | |
| 65 google_apis::test_util::CreateCopyResultCallback(&error)); | |
| 66 test_util::RunBlockingPoolTask(); | |
| 67 return error; | |
| 68 } | |
| 69 | |
| 70 scoped_ptr<ContentUpdatePerformer> performer_; | |
| 71 }; | |
| 72 | |
| 73 TEST_F(ContentUpdatePerformerTest, UpdateFileByLocalId) { | |
| 74 const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/File 1.txt")); | |
| 75 const std::string kResourceId("file:2_file_resource_id"); | |
| 76 | |
| 77 const std::string local_id = GetLocalId(kFilePath); | |
| 78 EXPECT_FALSE(local_id.empty()); | |
| 79 | |
| 80 const std::string kTestFileContent = "I'm being uploaded! Yay!"; | |
| 81 EXPECT_EQ(FILE_ERROR_OK, StoreAndMarkDirty(local_id, kTestFileContent)); | |
| 82 | |
| 83 int64 original_changestamp = | |
| 84 fake_service()->about_resource().largest_change_id(); | |
| 85 | |
| 86 // The callback will be called upon completion of UpdateFileByLocalId(). | |
| 87 FileError error = FILE_ERROR_FAILED; | |
| 88 performer_->UpdateFileByLocalId( | |
| 89 local_id, | |
| 90 ClientContext(USER_INITIATED), | |
| 91 google_apis::test_util::CreateCopyResultCallback(&error)); | |
| 92 test_util::RunBlockingPoolTask(); | |
| 93 EXPECT_EQ(FILE_ERROR_OK, error); | |
| 94 | |
| 95 // Check that the server has received an update. | |
| 96 EXPECT_LT(original_changestamp, | |
| 97 fake_service()->about_resource().largest_change_id()); | |
| 98 | |
| 99 // Check that the file size is updated to that of the updated content. | |
| 100 google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR; | |
| 101 scoped_ptr<google_apis::ResourceEntry> server_entry; | |
| 102 fake_service()->GetResourceEntry( | |
| 103 kResourceId, | |
| 104 google_apis::test_util::CreateCopyResultCallback(&gdata_error, | |
| 105 &server_entry)); | |
| 106 test_util::RunBlockingPoolTask(); | |
| 107 EXPECT_EQ(google_apis::HTTP_SUCCESS, gdata_error); | |
| 108 EXPECT_EQ(static_cast<int64>(kTestFileContent.size()), | |
| 109 server_entry->file_size()); | |
| 110 | |
| 111 // Make sure that the cache is no longer dirty. | |
| 112 bool success = false; | |
| 113 FileCacheEntry cache_entry; | |
| 114 base::PostTaskAndReplyWithResult( | |
| 115 blocking_task_runner(), | |
| 116 FROM_HERE, | |
| 117 base::Bind(&internal::FileCache::GetCacheEntry, | |
| 118 base::Unretained(cache()), | |
| 119 local_id, | |
| 120 &cache_entry), | |
| 121 google_apis::test_util::CreateCopyResultCallback(&success)); | |
| 122 test_util::RunBlockingPoolTask(); | |
| 123 ASSERT_TRUE(success); | |
| 124 EXPECT_FALSE(cache_entry.is_dirty()); | |
| 125 } | |
| 126 | |
| 127 TEST_F(ContentUpdatePerformerTest, UpdateFileByLocalId_NonexistentFile) { | |
| 128 FileError error = FILE_ERROR_OK; | |
| 129 performer_->UpdateFileByLocalId( | |
| 130 "nonexistent_local_id", | |
| 131 ClientContext(USER_INITIATED), | |
| 132 google_apis::test_util::CreateCopyResultCallback(&error)); | |
| 133 test_util::RunBlockingPoolTask(); | |
| 134 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); | |
| 135 } | |
| 136 | |
| 137 TEST_F(ContentUpdatePerformerTest, UpdateFileByLocalId_Md5) { | |
| 138 const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/File 1.txt")); | |
| 139 const std::string kResourceId("file:2_file_resource_id"); | |
| 140 | |
| 141 const std::string local_id = GetLocalId(kFilePath); | |
| 142 EXPECT_FALSE(local_id.empty()); | |
| 143 | |
| 144 const std::string kTestFileContent = "I'm being uploaded! Yay!"; | |
| 145 EXPECT_EQ(FILE_ERROR_OK, StoreAndMarkDirty(local_id, kTestFileContent)); | |
| 146 | |
| 147 int64 original_changestamp = | |
| 148 fake_service()->about_resource().largest_change_id(); | |
| 149 | |
| 150 // The callback will be called upon completion of UpdateFileByLocalId(). | |
| 151 FileError error = FILE_ERROR_FAILED; | |
| 152 performer_->UpdateFileByLocalId( | |
| 153 local_id, | |
| 154 ClientContext(USER_INITIATED), | |
| 155 google_apis::test_util::CreateCopyResultCallback(&error)); | |
| 156 test_util::RunBlockingPoolTask(); | |
| 157 EXPECT_EQ(FILE_ERROR_OK, error); | |
| 158 | |
| 159 // Check that the server has received an update. | |
| 160 EXPECT_LT(original_changestamp, | |
| 161 fake_service()->about_resource().largest_change_id()); | |
| 162 | |
| 163 // Check that the file size is updated to that of the updated content. | |
| 164 google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR; | |
| 165 scoped_ptr<google_apis::ResourceEntry> server_entry; | |
| 166 fake_service()->GetResourceEntry( | |
| 167 kResourceId, | |
| 168 google_apis::test_util::CreateCopyResultCallback(&gdata_error, | |
| 169 &server_entry)); | |
| 170 test_util::RunBlockingPoolTask(); | |
| 171 EXPECT_EQ(google_apis::HTTP_SUCCESS, gdata_error); | |
| 172 EXPECT_EQ(static_cast<int64>(kTestFileContent.size()), | |
| 173 server_entry->file_size()); | |
| 174 | |
| 175 // Make sure that the cache is no longer dirty. | |
| 176 bool success = false; | |
| 177 FileCacheEntry cache_entry; | |
| 178 base::PostTaskAndReplyWithResult( | |
| 179 blocking_task_runner(), | |
| 180 FROM_HERE, | |
| 181 base::Bind(&internal::FileCache::GetCacheEntry, | |
| 182 base::Unretained(cache()), | |
| 183 local_id, | |
| 184 &cache_entry), | |
| 185 google_apis::test_util::CreateCopyResultCallback(&success)); | |
| 186 test_util::RunBlockingPoolTask(); | |
| 187 ASSERT_TRUE(success); | |
| 188 EXPECT_FALSE(cache_entry.is_dirty()); | |
| 189 | |
| 190 // Again mark the cache file dirty. | |
| 191 scoped_ptr<base::ScopedClosureRunner> file_closer; | |
| 192 error = FILE_ERROR_FAILED; | |
| 193 base::PostTaskAndReplyWithResult( | |
| 194 blocking_task_runner(), | |
| 195 FROM_HERE, | |
| 196 base::Bind(&internal::FileCache::OpenForWrite, | |
| 197 base::Unretained(cache()), | |
| 198 local_id, | |
| 199 &file_closer), | |
| 200 google_apis::test_util::CreateCopyResultCallback(&error)); | |
| 201 test_util::RunBlockingPoolTask(); | |
| 202 EXPECT_EQ(FILE_ERROR_OK, error); | |
| 203 file_closer.reset(); | |
| 204 | |
| 205 // And call UpdateFileByLocalId again. | |
| 206 // In this case, although the file is marked as dirty, but the content | |
| 207 // hasn't been changed. Thus, the actual uploading should be skipped. | |
| 208 original_changestamp = fake_service()->about_resource().largest_change_id(); | |
| 209 error = FILE_ERROR_FAILED; | |
| 210 performer_->UpdateFileByLocalId( | |
| 211 local_id, | |
| 212 ClientContext(USER_INITIATED), | |
| 213 google_apis::test_util::CreateCopyResultCallback(&error)); | |
| 214 test_util::RunBlockingPoolTask(); | |
| 215 EXPECT_EQ(FILE_ERROR_OK, error); | |
| 216 | |
| 217 EXPECT_EQ(original_changestamp, | |
| 218 fake_service()->about_resource().largest_change_id()); | |
| 219 | |
| 220 // Make sure that the cache is no longer dirty. | |
| 221 success = false; | |
| 222 base::PostTaskAndReplyWithResult( | |
| 223 blocking_task_runner(), | |
| 224 FROM_HERE, | |
| 225 base::Bind(&internal::FileCache::GetCacheEntry, | |
| 226 base::Unretained(cache()), | |
| 227 local_id, | |
| 228 &cache_entry), | |
| 229 google_apis::test_util::CreateCopyResultCallback(&success)); | |
| 230 test_util::RunBlockingPoolTask(); | |
| 231 ASSERT_TRUE(success); | |
| 232 EXPECT_FALSE(cache_entry.is_dirty()); | |
| 233 } | |
| 234 | |
| 235 TEST_F(ContentUpdatePerformerTest, UpdateFileByLocalId_OpenedForWrite) { | |
| 236 const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/File 1.txt")); | |
| 237 const std::string kResourceId("file:2_file_resource_id"); | |
| 238 | |
| 239 const std::string local_id = GetLocalId(kFilePath); | |
| 240 EXPECT_FALSE(local_id.empty()); | |
| 241 | |
| 242 const std::string kTestFileContent = "I'm being uploaded! Yay!"; | |
| 243 EXPECT_EQ(FILE_ERROR_OK, StoreAndMarkDirty(local_id, kTestFileContent)); | |
| 244 | |
| 245 // Emulate a situation where someone is writing to the file. | |
| 246 scoped_ptr<base::ScopedClosureRunner> file_closer; | |
| 247 FileError error = FILE_ERROR_FAILED; | |
| 248 base::PostTaskAndReplyWithResult( | |
| 249 blocking_task_runner(), | |
| 250 FROM_HERE, | |
| 251 base::Bind(&internal::FileCache::OpenForWrite, | |
| 252 base::Unretained(cache()), | |
| 253 local_id, | |
| 254 &file_closer), | |
| 255 google_apis::test_util::CreateCopyResultCallback(&error)); | |
| 256 test_util::RunBlockingPoolTask(); | |
| 257 EXPECT_EQ(FILE_ERROR_OK, error); | |
| 258 | |
| 259 // Update. This should not clear the dirty bit. | |
| 260 error = FILE_ERROR_FAILED; | |
| 261 performer_->UpdateFileByLocalId( | |
| 262 local_id, | |
| 263 ClientContext(USER_INITIATED), | |
| 264 google_apis::test_util::CreateCopyResultCallback(&error)); | |
| 265 test_util::RunBlockingPoolTask(); | |
| 266 EXPECT_EQ(FILE_ERROR_OK, error); | |
| 267 | |
| 268 // Make sure that the cache is still dirty. | |
| 269 bool success = false; | |
| 270 FileCacheEntry cache_entry; | |
| 271 base::PostTaskAndReplyWithResult( | |
| 272 blocking_task_runner(), | |
| 273 FROM_HERE, | |
| 274 base::Bind(&internal::FileCache::GetCacheEntry, | |
| 275 base::Unretained(cache()), | |
| 276 local_id, | |
| 277 &cache_entry), | |
| 278 google_apis::test_util::CreateCopyResultCallback(&success)); | |
| 279 test_util::RunBlockingPoolTask(); | |
| 280 EXPECT_TRUE(success); | |
| 281 EXPECT_TRUE(cache_entry.is_dirty()); | |
| 282 | |
| 283 // Close the file. | |
| 284 file_closer.reset(); | |
| 285 | |
| 286 // Update. This should clear the dirty bit. | |
| 287 error = FILE_ERROR_FAILED; | |
| 288 performer_->UpdateFileByLocalId( | |
| 289 local_id, | |
| 290 ClientContext(USER_INITIATED), | |
| 291 google_apis::test_util::CreateCopyResultCallback(&error)); | |
| 292 test_util::RunBlockingPoolTask(); | |
| 293 EXPECT_EQ(FILE_ERROR_OK, error); | |
| 294 | |
| 295 // Make sure that the cache is no longer dirty. | |
| 296 base::PostTaskAndReplyWithResult( | |
| 297 blocking_task_runner(), | |
| 298 FROM_HERE, | |
| 299 base::Bind(&internal::FileCache::GetCacheEntry, | |
| 300 base::Unretained(cache()), | |
| 301 local_id, | |
| 302 &cache_entry), | |
| 303 google_apis::test_util::CreateCopyResultCallback(&success)); | |
| 304 test_util::RunBlockingPoolTask(); | |
| 305 EXPECT_TRUE(success); | |
| 306 EXPECT_FALSE(cache_entry.is_dirty()); | |
| 307 } | |
| 308 | |
| 309 } // namespace file_system | |
| 310 } // namespace drive | |
| OLD | NEW |