OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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/file_system/download_operation.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h" |
| 9 #include "chrome/browser/chromeos/drive/file_cache.h" |
| 10 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" |
| 11 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 12 #include "chrome/browser/google_apis/fake_drive_service.h" |
| 13 #include "chrome/browser/google_apis/test_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace drive { |
| 17 namespace file_system { |
| 18 |
| 19 class DownloadOperationTest : public OperationTestBase { |
| 20 protected: |
| 21 virtual void SetUp() { |
| 22 OperationTestBase::SetUp(); |
| 23 |
| 24 operation_.reset(new DownloadOperation( |
| 25 blocking_task_runner(), observer(), scheduler(), metadata(), cache())); |
| 26 } |
| 27 |
| 28 scoped_ptr<DownloadOperation> operation_; |
| 29 }; |
| 30 |
| 31 TEST_F(DownloadOperationTest, |
| 32 EnsureFileDownloadedByPath_FromServer_EnoughSpace) { |
| 33 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 34 ResourceEntry src_entry; |
| 35 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
| 36 const int64 file_size = src_entry.file_info().size(); |
| 37 |
| 38 // Pretend we have enough space. |
| 39 fake_free_disk_space_getter()->Reset(); |
| 40 fake_free_disk_space_getter()->set_fake_free_disk_space( |
| 41 file_size + internal::kMinFreeSpace); |
| 42 |
| 43 FileError error = FILE_ERROR_FAILED; |
| 44 base::FilePath file_path; |
| 45 scoped_ptr<ResourceEntry> entry; |
| 46 operation_->EnsureFileDownloadedByPath( |
| 47 file_in_root, |
| 48 ClientContext(USER_INITIATED), |
| 49 GetFileContentInitializedCallback(), |
| 50 google_apis::GetContentCallback(), |
| 51 google_apis::test_util::CreateCopyResultCallback( |
| 52 &error, &file_path, &entry)); |
| 53 google_apis::test_util::RunBlockingPoolTask(); |
| 54 |
| 55 EXPECT_EQ(FILE_ERROR_OK, error); |
| 56 ASSERT_TRUE(entry); |
| 57 EXPECT_FALSE(entry->file_specific_info().is_hosted_document()); |
| 58 |
| 59 // The transfered file is cached and the change of "offline available" |
| 60 // attribute is notified. |
| 61 EXPECT_EQ(1U, observer()->get_changed_paths().size()); |
| 62 EXPECT_EQ(1U, observer()->get_changed_paths().count(file_in_root.DirName())); |
| 63 |
| 64 // Verify that readable permission is set. |
| 65 int permission = 0; |
| 66 EXPECT_TRUE(file_util::GetPosixFilePermissions(file_path, &permission)); |
| 67 EXPECT_EQ(file_util::FILE_PERMISSION_READ_BY_USER | |
| 68 file_util::FILE_PERMISSION_WRITE_BY_USER | |
| 69 file_util::FILE_PERMISSION_READ_BY_GROUP | |
| 70 file_util::FILE_PERMISSION_READ_BY_OTHERS, permission); |
| 71 } |
| 72 |
| 73 TEST_F(DownloadOperationTest, |
| 74 EnsureFileDownloadedByPath_FromServer_NoSpaceAtAll) { |
| 75 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 76 |
| 77 // Pretend we have no space at all. |
| 78 fake_free_disk_space_getter()->Reset(); |
| 79 fake_free_disk_space_getter()->set_fake_free_disk_space(0); |
| 80 |
| 81 FileError error = FILE_ERROR_OK; |
| 82 base::FilePath file_path; |
| 83 scoped_ptr<ResourceEntry> entry; |
| 84 operation_->EnsureFileDownloadedByPath( |
| 85 file_in_root, |
| 86 ClientContext(USER_INITIATED), |
| 87 GetFileContentInitializedCallback(), |
| 88 google_apis::GetContentCallback(), |
| 89 google_apis::test_util::CreateCopyResultCallback( |
| 90 &error, &file_path, &entry)); |
| 91 google_apis::test_util::RunBlockingPoolTask(); |
| 92 |
| 93 EXPECT_EQ(FILE_ERROR_NO_SPACE, error); |
| 94 } |
| 95 |
| 96 TEST_F(DownloadOperationTest, |
| 97 EnsureFileDownloadedByPath_FromServer_NoEnoughSpaceButCanFreeUp) { |
| 98 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 99 ResourceEntry src_entry; |
| 100 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
| 101 const int64 file_size = src_entry.file_info().size(); |
| 102 |
| 103 // Pretend we have no space first (checked before downloading a file), |
| 104 // but then start reporting we have space. This is to emulate that |
| 105 // the disk space was freed up by removing temporary files. |
| 106 fake_free_disk_space_getter()->Reset(); |
| 107 fake_free_disk_space_getter()->set_fake_free_disk_space( |
| 108 file_size + internal::kMinFreeSpace); |
| 109 fake_free_disk_space_getter()->set_fake_free_disk_space(0); |
| 110 fake_free_disk_space_getter()->set_fake_free_disk_space( |
| 111 file_size + internal::kMinFreeSpace); |
| 112 fake_free_disk_space_getter()->set_fake_free_disk_space( |
| 113 file_size + internal::kMinFreeSpace); |
| 114 |
| 115 // Store something of the file size in the temporary cache directory. |
| 116 const std::string content(file_size, 'x'); |
| 117 base::ScopedTempDir temp_dir; |
| 118 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 119 const base::FilePath tmp_file = |
| 120 temp_dir.path().AppendASCII("something.txt"); |
| 121 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(tmp_file, content)); |
| 122 |
| 123 FileError error = FILE_ERROR_FAILED; |
| 124 cache()->StoreOnUIThread( |
| 125 "<resource_id>", "<md5>", tmp_file, |
| 126 internal::FileCache::FILE_OPERATION_COPY, |
| 127 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 128 google_apis::test_util::RunBlockingPoolTask(); |
| 129 EXPECT_EQ(FILE_ERROR_OK, error); |
| 130 |
| 131 base::FilePath file_path; |
| 132 scoped_ptr<ResourceEntry> entry; |
| 133 operation_->EnsureFileDownloadedByPath( |
| 134 file_in_root, |
| 135 ClientContext(USER_INITIATED), |
| 136 GetFileContentInitializedCallback(), |
| 137 google_apis::GetContentCallback(), |
| 138 google_apis::test_util::CreateCopyResultCallback( |
| 139 &error, &file_path, &entry)); |
| 140 google_apis::test_util::RunBlockingPoolTask(); |
| 141 |
| 142 EXPECT_EQ(FILE_ERROR_OK, error); |
| 143 ASSERT_TRUE(entry); |
| 144 EXPECT_FALSE(entry->file_specific_info().is_hosted_document()); |
| 145 |
| 146 // The transfered file is cached and the change of "offline available" |
| 147 // attribute is notified. |
| 148 EXPECT_EQ(1U, observer()->get_changed_paths().size()); |
| 149 EXPECT_EQ(1U, observer()->get_changed_paths().count(file_in_root.DirName())); |
| 150 |
| 151 // The cache entry should be removed in order to free up space. |
| 152 FileCacheEntry cache_entry; |
| 153 bool result = true; |
| 154 cache()->GetCacheEntryOnUIThread( |
| 155 "<resource_id>", "<md5>", |
| 156 google_apis::test_util::CreateCopyResultCallback(&result, |
| 157 &cache_entry)); |
| 158 google_apis::test_util::RunBlockingPoolTask(); |
| 159 ASSERT_FALSE(result); |
| 160 } |
| 161 |
| 162 TEST_F(DownloadOperationTest, |
| 163 EnsureFileDownloadedByPath_FromServer_EnoughSpaceButBecomeFull) { |
| 164 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 165 ResourceEntry src_entry; |
| 166 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
| 167 const int64 file_size = src_entry.file_info().size(); |
| 168 |
| 169 // Pretend we have enough space first (checked before downloading a file), |
| 170 // but then start reporting we have not enough space. This is to emulate that |
| 171 // the disk space becomes full after the file is downloaded for some reason |
| 172 // (ex. the actual file was larger than the expected size). |
| 173 fake_free_disk_space_getter()->Reset(); |
| 174 fake_free_disk_space_getter()->set_fake_free_disk_space( |
| 175 file_size + internal::kMinFreeSpace); |
| 176 fake_free_disk_space_getter()->set_fake_free_disk_space( |
| 177 internal::kMinFreeSpace - 1); |
| 178 |
| 179 FileError error = FILE_ERROR_OK; |
| 180 base::FilePath file_path; |
| 181 scoped_ptr<ResourceEntry> entry; |
| 182 operation_->EnsureFileDownloadedByPath( |
| 183 file_in_root, |
| 184 ClientContext(USER_INITIATED), |
| 185 GetFileContentInitializedCallback(), |
| 186 google_apis::GetContentCallback(), |
| 187 google_apis::test_util::CreateCopyResultCallback( |
| 188 &error, &file_path, &entry)); |
| 189 google_apis::test_util::RunBlockingPoolTask(); |
| 190 |
| 191 EXPECT_EQ(FILE_ERROR_NO_SPACE, error); |
| 192 } |
| 193 |
| 194 TEST_F(DownloadOperationTest, EnsureFileDownloadedByPath_FromCache) { |
| 195 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 196 ResourceEntry src_entry; |
| 197 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
| 198 |
| 199 // Store something as cached version of this file. |
| 200 FileError error = FILE_ERROR_OK; |
| 201 cache()->StoreOnUIThread( |
| 202 src_entry.resource_id(), |
| 203 src_entry.file_specific_info().file_md5(), |
| 204 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
| 205 internal::FileCache::FILE_OPERATION_COPY, |
| 206 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 207 google_apis::test_util::RunBlockingPoolTask(); |
| 208 EXPECT_EQ(FILE_ERROR_OK, error); |
| 209 |
| 210 base::FilePath file_path; |
| 211 scoped_ptr<ResourceEntry> entry; |
| 212 operation_->EnsureFileDownloadedByPath( |
| 213 file_in_root, |
| 214 ClientContext(USER_INITIATED), |
| 215 GetFileContentInitializedCallback(), |
| 216 google_apis::GetContentCallback(), |
| 217 google_apis::test_util::CreateCopyResultCallback( |
| 218 &error, &file_path, &entry)); |
| 219 google_apis::test_util::RunBlockingPoolTask(); |
| 220 |
| 221 EXPECT_EQ(FILE_ERROR_OK, error); |
| 222 ASSERT_TRUE(entry); |
| 223 EXPECT_FALSE(entry->file_specific_info().is_hosted_document()); |
| 224 } |
| 225 |
| 226 TEST_F(DownloadOperationTest, EnsureFileDownloadedByPath_HostedDocument) { |
| 227 base::FilePath file_in_root(FILE_PATH_LITERAL( |
| 228 "drive/root/Document 1 excludeDir-test.gdoc")); |
| 229 |
| 230 FileError error = FILE_ERROR_FAILED; |
| 231 base::FilePath file_path; |
| 232 scoped_ptr<ResourceEntry> entry; |
| 233 operation_->EnsureFileDownloadedByPath( |
| 234 file_in_root, |
| 235 ClientContext(USER_INITIATED), |
| 236 GetFileContentInitializedCallback(), |
| 237 google_apis::GetContentCallback(), |
| 238 google_apis::test_util::CreateCopyResultCallback( |
| 239 &error, &file_path, &entry)); |
| 240 google_apis::test_util::RunBlockingPoolTask(); |
| 241 |
| 242 EXPECT_EQ(FILE_ERROR_OK, error); |
| 243 ASSERT_TRUE(entry); |
| 244 EXPECT_TRUE(entry->file_specific_info().is_hosted_document()); |
| 245 EXPECT_FALSE(file_path.empty()); |
| 246 |
| 247 EXPECT_EQ(GURL(entry->file_specific_info().alternate_url()), |
| 248 util::ReadUrlFromGDocFile(file_path)); |
| 249 EXPECT_EQ(entry->resource_id(), util::ReadResourceIdFromGDocFile(file_path)); |
| 250 } |
| 251 |
| 252 TEST_F(DownloadOperationTest, EnsureFileDownloadedByResourceId) { |
| 253 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 254 ResourceEntry src_entry; |
| 255 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
| 256 std::string resource_id = src_entry.resource_id(); |
| 257 |
| 258 FileError error = FILE_ERROR_OK; |
| 259 base::FilePath file_path; |
| 260 scoped_ptr<ResourceEntry> entry; |
| 261 operation_->EnsureFileDownloadedByResourceId( |
| 262 resource_id, |
| 263 ClientContext(USER_INITIATED), |
| 264 GetFileContentInitializedCallback(), |
| 265 google_apis::GetContentCallback(), |
| 266 google_apis::test_util::CreateCopyResultCallback( |
| 267 &error, &file_path, &entry)); |
| 268 google_apis::test_util::RunBlockingPoolTask(); |
| 269 |
| 270 EXPECT_EQ(FILE_ERROR_OK, error); |
| 271 ASSERT_TRUE(entry); |
| 272 EXPECT_FALSE(entry->file_specific_info().is_hosted_document()); |
| 273 |
| 274 // The transfered file is cached and the change of "offline available" |
| 275 // attribute is notified. |
| 276 EXPECT_EQ(1U, observer()->get_changed_paths().size()); |
| 277 EXPECT_EQ(1U, observer()->get_changed_paths().count(file_in_root.DirName())); |
| 278 } |
| 279 |
| 280 TEST_F(DownloadOperationTest, |
| 281 EnsureFileDownloadedByPath_WithGetContentCallback) { |
| 282 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 283 |
| 284 { |
| 285 FileError initialized_error = FILE_ERROR_FAILED; |
| 286 scoped_ptr<ResourceEntry> entry, entry_dontcare; |
| 287 base::FilePath local_path, local_path_dontcare; |
| 288 base::Closure cancel_download; |
| 289 google_apis::test_util::TestGetContentCallback get_content_callback; |
| 290 |
| 291 FileError completion_error = FILE_ERROR_FAILED; |
| 292 |
| 293 operation_->EnsureFileDownloadedByPath( |
| 294 file_in_root, |
| 295 ClientContext(USER_INITIATED), |
| 296 google_apis::test_util::CreateCopyResultCallback( |
| 297 &initialized_error, &entry, &local_path, &cancel_download), |
| 298 get_content_callback.callback(), |
| 299 google_apis::test_util::CreateCopyResultCallback( |
| 300 &completion_error, &local_path_dontcare, &entry_dontcare)); |
| 301 google_apis::test_util::RunBlockingPoolTask(); |
| 302 |
| 303 // For the first time, file is downloaded from the remote server. |
| 304 // In this case, |local_path| is empty while |cancel_download| is not. |
| 305 EXPECT_EQ(FILE_ERROR_OK, initialized_error); |
| 306 ASSERT_TRUE(entry); |
| 307 ASSERT_TRUE(local_path.empty()); |
| 308 EXPECT_TRUE(!cancel_download.is_null()); |
| 309 // Content is available through the second callback argument. |
| 310 EXPECT_EQ(static_cast<size_t>(entry->file_info().size()), |
| 311 get_content_callback.GetConcatenatedData().size()); |
| 312 EXPECT_EQ(FILE_ERROR_OK, completion_error); |
| 313 |
| 314 // The transfered file is cached and the change of "offline available" |
| 315 // attribute is notified. |
| 316 EXPECT_EQ(1U, observer()->get_changed_paths().size()); |
| 317 EXPECT_EQ(1U, |
| 318 observer()->get_changed_paths().count(file_in_root.DirName())); |
| 319 } |
| 320 |
| 321 { |
| 322 FileError initialized_error = FILE_ERROR_FAILED; |
| 323 scoped_ptr<ResourceEntry> entry, entry_dontcare; |
| 324 base::FilePath local_path, local_path_dontcare; |
| 325 base::Closure cancel_download; |
| 326 google_apis::test_util::TestGetContentCallback get_content_callback; |
| 327 |
| 328 FileError completion_error = FILE_ERROR_FAILED; |
| 329 |
| 330 operation_->EnsureFileDownloadedByPath( |
| 331 file_in_root, |
| 332 ClientContext(USER_INITIATED), |
| 333 google_apis::test_util::CreateCopyResultCallback( |
| 334 &initialized_error, &entry, &local_path, &cancel_download), |
| 335 get_content_callback.callback(), |
| 336 google_apis::test_util::CreateCopyResultCallback( |
| 337 &completion_error, &local_path_dontcare, &entry_dontcare)); |
| 338 google_apis::test_util::RunBlockingPoolTask(); |
| 339 |
| 340 // Try second download. In this case, the file should be cached, so |
| 341 // |local_path| should not be empty while |cancel_download| is empty. |
| 342 EXPECT_EQ(FILE_ERROR_OK, initialized_error); |
| 343 ASSERT_TRUE(entry); |
| 344 ASSERT_TRUE(!local_path.empty()); |
| 345 EXPECT_TRUE(cancel_download.is_null()); |
| 346 // The content is available from the cache file. |
| 347 EXPECT_TRUE(get_content_callback.data().empty()); |
| 348 int64 local_file_size = 0; |
| 349 file_util::GetFileSize(local_path, &local_file_size); |
| 350 EXPECT_EQ(entry->file_info().size(), local_file_size); |
| 351 EXPECT_EQ(FILE_ERROR_OK, completion_error); |
| 352 } |
| 353 } |
| 354 |
| 355 TEST_F(DownloadOperationTest, EnsureFileDownloadedByResourceId_FromCache) { |
| 356 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 357 ResourceEntry src_entry; |
| 358 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
| 359 |
| 360 // Store something as cached version of this file. |
| 361 FileError error = FILE_ERROR_FAILED; |
| 362 cache()->StoreOnUIThread( |
| 363 src_entry.resource_id(), |
| 364 src_entry.file_specific_info().file_md5(), |
| 365 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
| 366 internal::FileCache::FILE_OPERATION_COPY, |
| 367 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 368 google_apis::test_util::RunBlockingPoolTask(); |
| 369 EXPECT_EQ(FILE_ERROR_OK, error); |
| 370 |
| 371 // The file is obtained from the cache. |
| 372 // Hence the downloading should work even if the drive service is offline. |
| 373 fake_service()->set_offline(true); |
| 374 |
| 375 std::string resource_id = src_entry.resource_id(); |
| 376 base::FilePath file_path; |
| 377 scoped_ptr<ResourceEntry> entry; |
| 378 operation_->EnsureFileDownloadedByResourceId( |
| 379 resource_id, |
| 380 ClientContext(USER_INITIATED), |
| 381 GetFileContentInitializedCallback(), |
| 382 google_apis::GetContentCallback(), |
| 383 google_apis::test_util::CreateCopyResultCallback( |
| 384 &error, &file_path, &entry)); |
| 385 google_apis::test_util::RunBlockingPoolTask(); |
| 386 |
| 387 EXPECT_EQ(FILE_ERROR_OK, error); |
| 388 ASSERT_TRUE(entry); |
| 389 EXPECT_FALSE(entry->file_specific_info().is_hosted_document()); |
| 390 } |
| 391 |
| 392 } // namespace file_system |
| 393 } // namespace drive |
OLD | NEW |