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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/json/json_file_value_serializer.h" | 6 #include "base/json/json_file_value_serializer.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "chrome/browser/chromeos/cros/cros_library.h" | 9 #include "chrome/browser/chromeos/cros/cros_library.h" |
10 #include "chrome/browser/chromeos/gdata/drive_webapps_registry.h" | 10 #include "chrome/browser/chromeos/gdata/drive_webapps_registry.h" |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 GDataCache::IsCachePresent(resource.cache_state) ? | 324 GDataCache::IsCachePresent(resource.cache_state) ? |
325 base::PLATFORM_FILE_OK : | 325 base::PLATFORM_FILE_OK : |
326 base::PLATFORM_FILE_ERROR_NOT_FOUND, | 326 base::PLATFORM_FILE_ERROR_NOT_FOUND, |
327 resource.expected_file_extension); | 327 resource.expected_file_extension); |
328 EXPECT_EQ(1, num_callback_invocations_); | 328 EXPECT_EQ(1, num_callback_invocations_); |
329 | 329 |
330 // Verify cache state. | 330 // Verify cache state. |
331 std::string md5; | 331 std::string md5; |
332 if (GDataCache::IsCachePresent(resource.cache_state)) | 332 if (GDataCache::IsCachePresent(resource.cache_state)) |
333 md5 = resource.md5; | 333 md5 = resource.md5; |
334 scoped_ptr<GDataCache::CacheEntry> cache_entry = | 334 GDataCache::CacheEntry cache_entry; |
335 GetCacheEntryFromOriginThread(resource.resource_id, md5); | 335 ASSERT_TRUE(GetCacheEntryFromOriginThread( |
336 ASSERT_TRUE(cache_entry.get()); | 336 resource.resource_id, md5, &cache_entry)); |
337 EXPECT_EQ(resource.cache_state, cache_entry->cache_state); | 337 EXPECT_EQ(resource.cache_state, cache_entry.cache_state); |
338 EXPECT_EQ(resource.expected_sub_dir_type, | 338 EXPECT_EQ(resource.expected_sub_dir_type, |
339 cache_entry->GetSubDirectoryType()); | 339 cache_entry.GetSubDirectoryType()); |
340 } | 340 } |
341 } | 341 } |
342 | 342 |
343 void TestGetFileFromCacheByResourceIdAndMd5( | 343 void TestGetFileFromCacheByResourceIdAndMd5( |
344 const std::string& resource_id, | 344 const std::string& resource_id, |
345 const std::string& md5, | 345 const std::string& md5, |
346 base::PlatformFileError expected_error, | 346 base::PlatformFileError expected_error, |
347 const std::string& expected_file_extension) { | 347 const std::string& expected_file_extension) { |
348 expected_error_ = expected_error; | 348 expected_error_ = expected_error; |
349 expected_file_extension_ = expected_file_extension; | 349 expected_file_extension_ = expected_file_extension; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 } | 411 } |
412 | 412 |
413 void VerifyRemoveFromCache(base::PlatformFileError error, | 413 void VerifyRemoveFromCache(base::PlatformFileError error, |
414 const std::string& resource_id, | 414 const std::string& resource_id, |
415 const std::string& md5) { | 415 const std::string& md5) { |
416 ++num_callback_invocations_; | 416 ++num_callback_invocations_; |
417 | 417 |
418 EXPECT_EQ(expected_error_, error); | 418 EXPECT_EQ(expected_error_, error); |
419 | 419 |
420 // Verify cache map. | 420 // Verify cache map. |
421 scoped_ptr<GDataCache::CacheEntry> cache_entry = | 421 GDataCache::CacheEntry cache_entry; |
422 GetCacheEntryFromOriginThread(resource_id, md5); | 422 const bool cache_entry_found = |
423 if (cache_entry.get()) | 423 GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry); |
424 EXPECT_TRUE(cache_entry->IsDirty()); | 424 if (cache_entry_found) |
| 425 EXPECT_TRUE(cache_entry.IsDirty()); |
425 | 426 |
426 // If entry doesn't exist, verify that: | 427 // If entry doesn't exist, verify that: |
427 // - no files with "<resource_id>.* exists in persistent and tmp dirs | 428 // - no files with "<resource_id>.* exists in persistent and tmp dirs |
428 // - no "<resource_id>" symlink exists in pinned and outgoing dirs. | 429 // - no "<resource_id>" symlink exists in pinned and outgoing dirs. |
429 std::vector<PathToVerify> paths_to_verify; | 430 std::vector<PathToVerify> paths_to_verify; |
430 paths_to_verify.push_back( // Index 0: CACHE_TYPE_TMP. | 431 paths_to_verify.push_back( // Index 0: CACHE_TYPE_TMP. |
431 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", | 432 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", |
432 GDataCache::CACHE_TYPE_TMP, | 433 GDataCache::CACHE_TYPE_TMP, |
433 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); | 434 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); |
434 paths_to_verify.push_back( // Index 1: CACHE_TYPE_PERSISTENT. | 435 paths_to_verify.push_back( // Index 1: CACHE_TYPE_PERSISTENT. |
435 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", | 436 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", |
436 GDataCache::CACHE_TYPE_PERSISTENT, | 437 GDataCache::CACHE_TYPE_PERSISTENT, |
437 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); | 438 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); |
438 paths_to_verify.push_back( // Index 2: CACHE_TYPE_TMP, but STATE_PINNED. | 439 paths_to_verify.push_back( // Index 2: CACHE_TYPE_TMP, but STATE_PINNED. |
439 PathToVerify(cache_->GetCacheFilePath(resource_id, "", | 440 PathToVerify(cache_->GetCacheFilePath(resource_id, "", |
440 GDataCache::CACHE_TYPE_PINNED, | 441 GDataCache::CACHE_TYPE_PINNED, |
441 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); | 442 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); |
442 paths_to_verify.push_back( // Index 3: CACHE_TYPE_OUTGOING. | 443 paths_to_verify.push_back( // Index 3: CACHE_TYPE_OUTGOING. |
443 PathToVerify(cache_->GetCacheFilePath(resource_id, "", | 444 PathToVerify(cache_->GetCacheFilePath(resource_id, "", |
444 GDataCache::CACHE_TYPE_OUTGOING, | 445 GDataCache::CACHE_TYPE_OUTGOING, |
445 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); | 446 GDataCache::CACHED_FILE_FROM_SERVER), FilePath())); |
446 if (!cache_entry.get()) { | 447 if (!cache_entry_found) { |
447 for (size_t i = 0; i < paths_to_verify.size(); ++i) { | 448 for (size_t i = 0; i < paths_to_verify.size(); ++i) { |
448 file_util::FileEnumerator enumerator( | 449 file_util::FileEnumerator enumerator( |
449 paths_to_verify[i].path_to_scan.DirName(), false /* not recursive*/, | 450 paths_to_verify[i].path_to_scan.DirName(), false /* not recursive*/, |
450 static_cast<file_util::FileEnumerator::FileType>( | 451 static_cast<file_util::FileEnumerator::FileType>( |
451 file_util::FileEnumerator::FILES | | 452 file_util::FileEnumerator::FILES | |
452 file_util::FileEnumerator::SHOW_SYM_LINKS), | 453 file_util::FileEnumerator::SHOW_SYM_LINKS), |
453 paths_to_verify[i].path_to_scan.BaseName().value()); | 454 paths_to_verify[i].path_to_scan.BaseName().value()); |
454 EXPECT_TRUE(enumerator.Next().empty()); | 455 EXPECT_TRUE(enumerator.Next().empty()); |
455 } | 456 } |
456 } else { | 457 } else { |
(...skipping 10 matching lines...) Expand all Loading... |
467 GDataCache::CACHE_TYPE_PERSISTENT, | 468 GDataCache::CACHE_TYPE_PERSISTENT, |
468 GDataCache::CACHED_FILE_LOCALLY_MODIFIED); | 469 GDataCache::CACHED_FILE_LOCALLY_MODIFIED); |
469 | 470 |
470 // Change expected_existing_path of CACHE_TYPE_OUTGOING (index 3). | 471 // Change expected_existing_path of CACHE_TYPE_OUTGOING (index 3). |
471 paths_to_verify[3].expected_existing_path = | 472 paths_to_verify[3].expected_existing_path = |
472 GetCacheFilePath(resource_id, | 473 GetCacheFilePath(resource_id, |
473 std::string(), | 474 std::string(), |
474 GDataCache::CACHE_TYPE_OUTGOING, | 475 GDataCache::CACHE_TYPE_OUTGOING, |
475 GDataCache::CACHED_FILE_FROM_SERVER); | 476 GDataCache::CACHED_FILE_FROM_SERVER); |
476 | 477 |
477 if (cache_entry->IsPinned()) { | 478 if (cache_entry.IsPinned()) { |
478 // Change expected_existing_path of CACHE_TYPE_TMP but STATE_PINNED | 479 // Change expected_existing_path of CACHE_TYPE_TMP but STATE_PINNED |
479 // (index 2). | 480 // (index 2). |
480 paths_to_verify[2].expected_existing_path = | 481 paths_to_verify[2].expected_existing_path = |
481 GetCacheFilePath(resource_id, | 482 GetCacheFilePath(resource_id, |
482 std::string(), | 483 std::string(), |
483 GDataCache::CACHE_TYPE_PINNED, | 484 GDataCache::CACHE_TYPE_PINNED, |
484 GDataCache::CACHED_FILE_FROM_SERVER); | 485 GDataCache::CACHED_FILE_FROM_SERVER); |
485 } | 486 } |
486 | 487 |
487 for (size_t i = 0; i < paths_to_verify.size(); ++i) { | 488 for (size_t i = 0; i < paths_to_verify.size(); ++i) { |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 } | 680 } |
680 | 681 |
681 void VerifyCacheFileState(base::PlatformFileError error, | 682 void VerifyCacheFileState(base::PlatformFileError error, |
682 const std::string& resource_id, | 683 const std::string& resource_id, |
683 const std::string& md5) { | 684 const std::string& md5) { |
684 ++num_callback_invocations_; | 685 ++num_callback_invocations_; |
685 | 686 |
686 EXPECT_EQ(expected_error_, error); | 687 EXPECT_EQ(expected_error_, error); |
687 | 688 |
688 // Verify cache map. | 689 // Verify cache map. |
689 scoped_ptr<GDataCache::CacheEntry> cache_entry = | 690 GDataCache::CacheEntry cache_entry; |
690 GetCacheEntryFromOriginThread(resource_id, md5); | 691 const bool cache_entry_found = |
| 692 GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry); |
691 if (GDataCache::IsCachePresent(expected_cache_state_) || | 693 if (GDataCache::IsCachePresent(expected_cache_state_) || |
692 GDataCache::IsCachePinned(expected_cache_state_)) { | 694 GDataCache::IsCachePinned(expected_cache_state_)) { |
693 ASSERT_TRUE(cache_entry.get()); | 695 ASSERT_TRUE(cache_entry_found); |
694 EXPECT_EQ(expected_cache_state_, cache_entry->cache_state); | 696 EXPECT_EQ(expected_cache_state_, cache_entry.cache_state); |
695 EXPECT_EQ(expected_sub_dir_type_, cache_entry->GetSubDirectoryType()); | 697 EXPECT_EQ(expected_sub_dir_type_, cache_entry.GetSubDirectoryType()); |
696 } else { | 698 } else { |
697 EXPECT_FALSE(cache_entry.get()); | 699 EXPECT_FALSE(cache_entry_found); |
698 } | 700 } |
699 | 701 |
700 // Verify actual cache file. | 702 // Verify actual cache file. |
701 FilePath dest_path = cache_->GetCacheFilePath( | 703 FilePath dest_path = cache_->GetCacheFilePath( |
702 resource_id, | 704 resource_id, |
703 md5, | 705 md5, |
704 GDataCache::IsCachePinned(expected_cache_state_) || | 706 GDataCache::IsCachePinned(expected_cache_state_) || |
705 GDataCache::IsCacheDirty(expected_cache_state_) ? | 707 GDataCache::IsCacheDirty(expected_cache_state_) ? |
706 GDataCache::CACHE_TYPE_PERSISTENT : | 708 GDataCache::CACHE_TYPE_PERSISTENT : |
707 GDataCache::CACHE_TYPE_TMP, | 709 GDataCache::CACHE_TYPE_TMP, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
757 | 759 |
758 FilePath GetCacheFilePath(const std::string& resource_id, | 760 FilePath GetCacheFilePath(const std::string& resource_id, |
759 const std::string& md5, | 761 const std::string& md5, |
760 GDataCache::CacheSubDirectoryType sub_dir_type, | 762 GDataCache::CacheSubDirectoryType sub_dir_type, |
761 GDataCache::CachedFileOrigin file_origin) { | 763 GDataCache::CachedFileOrigin file_origin) { |
762 return cache_->GetCacheFilePath(resource_id, md5, sub_dir_type, | 764 return cache_->GetCacheFilePath(resource_id, md5, sub_dir_type, |
763 file_origin); | 765 file_origin); |
764 } | 766 } |
765 | 767 |
766 // Helper function to call GetCacheEntry from origin thread. | 768 // Helper function to call GetCacheEntry from origin thread. |
767 scoped_ptr<GDataCache::CacheEntry> GetCacheEntryFromOriginThread( | 769 bool GetCacheEntryFromOriginThread(const std::string& resource_id, |
768 const std::string& resource_id, | 770 const std::string& md5, |
769 const std::string& md5) { | 771 GDataCache::CacheEntry* cache_entry) { |
770 scoped_ptr<GDataCache::CacheEntry> cache_entry; | 772 bool result = false; |
771 content::BrowserThread::GetBlockingPool() | 773 content::BrowserThread::GetBlockingPool() |
772 ->GetSequencedTaskRunner(sequence_token_)->PostTask( | 774 ->GetSequencedTaskRunner(sequence_token_)->PostTask( |
773 FROM_HERE, | 775 FROM_HERE, |
774 base::Bind( | 776 base::Bind( |
775 &GDataCacheTest::GetCacheEntryFromOriginThreadInternal, | 777 &GDataCacheTest::GetCacheEntryFromOriginThreadInternal, |
776 base::Unretained(this), | 778 base::Unretained(this), |
777 resource_id, | 779 resource_id, |
778 md5, | 780 md5, |
779 &cache_entry)); | 781 cache_entry, |
| 782 &result)); |
780 test_util::RunBlockingPoolTask(); | 783 test_util::RunBlockingPoolTask(); |
781 return cache_entry.Pass(); | 784 return result; |
782 } | 785 } |
783 | 786 |
784 // Used to implement GetCacheEntry. | 787 // Used to implement GetCacheEntry. |
785 void GetCacheEntryFromOriginThreadInternal( | 788 void GetCacheEntryFromOriginThreadInternal( |
786 const std::string& resource_id, | 789 const std::string& resource_id, |
787 const std::string& md5, | 790 const std::string& md5, |
788 scoped_ptr<GDataCache::CacheEntry>* cache_entry) { | 791 GDataCache::CacheEntry* cache_entry, |
789 cache_entry->reset(cache_->GetCacheEntry(resource_id, md5).release()); | 792 bool* result) { |
| 793 *result = cache_->GetCacheEntry(resource_id, md5, cache_entry); |
790 } | 794 } |
791 | 795 |
792 // Returns true if the cache entry exists for the given resource ID and MD5. | 796 // Returns true if the cache entry exists for the given resource ID and MD5. |
793 bool CacheEntryExists(const std::string& resource_id, | 797 bool CacheEntryExists(const std::string& resource_id, |
794 const std::string& md5) { | 798 const std::string& md5) { |
795 return GetCacheEntryFromOriginThread(resource_id, md5).get(); | 799 GDataCache::CacheEntry cache_entry; |
| 800 return GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry); |
796 } | 801 } |
797 | 802 |
798 void TestGetCacheFilePath(const std::string& resource_id, | 803 void TestGetCacheFilePath(const std::string& resource_id, |
799 const std::string& md5, | 804 const std::string& md5, |
800 const std::string& expected_filename) { | 805 const std::string& expected_filename) { |
801 FilePath actual_path = cache_->GetCacheFilePath( | 806 FilePath actual_path = cache_->GetCacheFilePath( |
802 resource_id, | 807 resource_id, |
803 md5, | 808 md5, |
804 GDataCache::CACHE_TYPE_TMP, | 809 GDataCache::CACHE_TYPE_TMP, |
805 GDataCache::CACHED_FILE_FROM_SERVER); | 810 GDataCache::CACHED_FILE_FROM_SERVER); |
(...skipping 833 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1639 EXPECT_EQ(1, num_callback_invocations_); | 1644 EXPECT_EQ(1, num_callback_invocations_); |
1640 EXPECT_TRUE(CacheEntryExists(resource_id, md5)); | 1645 EXPECT_TRUE(CacheEntryExists(resource_id, md5)); |
1641 | 1646 |
1642 // Try to remove the file. | 1647 // Try to remove the file. |
1643 num_callback_invocations_ = 0; | 1648 num_callback_invocations_ = 0; |
1644 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); | 1649 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); |
1645 EXPECT_EQ(1, num_callback_invocations_); | 1650 EXPECT_EQ(1, num_callback_invocations_); |
1646 } | 1651 } |
1647 | 1652 |
1648 } // namespace gdata | 1653 } // namespace gdata |
OLD | NEW |