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 21 matching lines...) Expand all Loading... |
32 const char* source_file; // Source file to be used for cache. | 32 const char* source_file; // Source file to be used for cache. |
33 const char* resource_id; // Resource id of cache file. | 33 const char* resource_id; // Resource id of cache file. |
34 const char* md5; // MD5 of cache file. | 34 const char* md5; // MD5 of cache file. |
35 int cache_state; // Cache state of cache file. | 35 int cache_state; // Cache state of cache file. |
36 const char* expected_file_extension; // Expected extension of cached file. | 36 const char* expected_file_extension; // Expected extension of cached file. |
37 // Expected CacheSubDirectoryType of cached file. | 37 // Expected CacheSubDirectoryType of cached file. |
38 GDataCache::CacheSubDirectoryType expected_sub_dir_type; | 38 GDataCache::CacheSubDirectoryType expected_sub_dir_type; |
39 } const initial_cache_resources[] = { | 39 } const initial_cache_resources[] = { |
40 // Cache resource in tmp dir, i.e. not pinned or dirty. | 40 // Cache resource in tmp dir, i.e. not pinned or dirty. |
41 { "root_feed.json", "tmp:resource_id", "md5_tmp_alphanumeric", | 41 { "root_feed.json", "tmp:resource_id", "md5_tmp_alphanumeric", |
42 GDataCache::CACHE_STATE_PRESENT, | 42 CACHE_STATE_PRESENT, |
43 "md5_tmp_alphanumeric", GDataCache::CACHE_TYPE_TMP }, | 43 "md5_tmp_alphanumeric", GDataCache::CACHE_TYPE_TMP }, |
44 // Cache resource in tmp dir, i.e. not pinned or dirty, with resource_id | 44 // Cache resource in tmp dir, i.e. not pinned or dirty, with resource_id |
45 // containing non-alphanumeric characters, to test resource_id is escaped and | 45 // containing non-alphanumeric characters, to test resource_id is escaped and |
46 // unescaped correctly. | 46 // unescaped correctly. |
47 { "subdir_feed.json", "tmp:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?", | 47 { "subdir_feed.json", "tmp:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?", |
48 "md5_tmp_non_alphanumeric", | 48 "md5_tmp_non_alphanumeric", |
49 GDataCache::CACHE_STATE_PRESENT, | 49 CACHE_STATE_PRESENT, |
50 "md5_tmp_non_alphanumeric", GDataCache::CACHE_TYPE_TMP }, | 50 "md5_tmp_non_alphanumeric", GDataCache::CACHE_TYPE_TMP }, |
51 // Cache resource that is pinned, to test a pinned file is in persistent dir | 51 // Cache resource that is pinned, to test a pinned file is in persistent dir |
52 // with a symlink in pinned dir referencing it. | 52 // with a symlink in pinned dir referencing it. |
53 { "directory_entry_atom.json", "pinned:existing", "md5_pinned_existing", | 53 { "directory_entry_atom.json", "pinned:existing", "md5_pinned_existing", |
54 GDataCache::CACHE_STATE_PRESENT | | 54 CACHE_STATE_PRESENT | |
55 GDataCache::CACHE_STATE_PINNED | | 55 CACHE_STATE_PINNED | |
56 GDataCache::CACHE_STATE_PERSISTENT, | 56 CACHE_STATE_PERSISTENT, |
57 "md5_pinned_existing", GDataCache::CACHE_TYPE_PERSISTENT }, | 57 "md5_pinned_existing", GDataCache::CACHE_TYPE_PERSISTENT }, |
58 // Cache resource with a non-existent source file that is pinned, to test that | 58 // Cache resource with a non-existent source file that is pinned, to test that |
59 // a pinned file can reference a non-existent file. | 59 // a pinned file can reference a non-existent file. |
60 { "", "pinned:non-existent", "md5_pinned_non_existent", | 60 { "", "pinned:non-existent", "md5_pinned_non_existent", |
61 GDataCache::CACHE_STATE_PINNED, | 61 CACHE_STATE_PINNED, |
62 "md5_pinned_non_existent", GDataCache::CACHE_TYPE_TMP }, | 62 "md5_pinned_non_existent", GDataCache::CACHE_TYPE_TMP }, |
63 // Cache resource that is dirty, to test a dirty file is in persistent dir | 63 // Cache resource that is dirty, to test a dirty file is in persistent dir |
64 // with a symlink in outgoing dir referencing it. | 64 // with a symlink in outgoing dir referencing it. |
65 { "account_metadata.json", "dirty:existing", "md5_dirty_existing", | 65 { "account_metadata.json", "dirty:existing", "md5_dirty_existing", |
66 GDataCache::CACHE_STATE_PRESENT | | 66 CACHE_STATE_PRESENT | |
67 GDataCache::CACHE_STATE_DIRTY | | 67 CACHE_STATE_DIRTY | |
68 GDataCache::CACHE_STATE_PERSISTENT, | 68 CACHE_STATE_PERSISTENT, |
69 "local", GDataCache::CACHE_TYPE_PERSISTENT }, | 69 "local", GDataCache::CACHE_TYPE_PERSISTENT }, |
70 // Cache resource that is pinned and dirty, to test a dirty pinned file is in | 70 // Cache resource that is pinned and dirty, to test a dirty pinned file is in |
71 // persistent dir with symlink in pinned and outgoing dirs referencing it. | 71 // persistent dir with symlink in pinned and outgoing dirs referencing it. |
72 { "basic_feed.json", "dirty_and_pinned:existing", | 72 { "basic_feed.json", "dirty_and_pinned:existing", |
73 "md5_dirty_and_pinned_existing", | 73 "md5_dirty_and_pinned_existing", |
74 GDataCache::CACHE_STATE_PRESENT | | 74 CACHE_STATE_PRESENT | |
75 GDataCache::CACHE_STATE_PINNED | | 75 CACHE_STATE_PINNED | |
76 GDataCache::CACHE_STATE_DIRTY | | 76 CACHE_STATE_DIRTY | |
77 GDataCache::CACHE_STATE_PERSISTENT, | 77 CACHE_STATE_PERSISTENT, |
78 "local", GDataCache::CACHE_TYPE_PERSISTENT }, | 78 "local", GDataCache::CACHE_TYPE_PERSISTENT }, |
79 }; | 79 }; |
80 | 80 |
81 const int64 kLotsOfSpace = kMinFreeSpace * 10; | 81 const int64 kLotsOfSpace = kMinFreeSpace * 10; |
82 | 82 |
83 struct PathToVerify { | 83 struct PathToVerify { |
84 PathToVerify(const FilePath& in_path_to_scan, | 84 PathToVerify(const FilePath& in_path_to_scan, |
85 const FilePath& in_expected_existing_path) : | 85 const FilePath& in_expected_existing_path) : |
86 path_to_scan(in_path_to_scan), | 86 path_to_scan(in_path_to_scan), |
87 expected_existing_path(in_expected_existing_path) { | 87 expected_existing_path(in_expected_existing_path) { |
88 } | 88 } |
89 | 89 |
90 FilePath path_to_scan; | 90 FilePath path_to_scan; |
91 FilePath expected_existing_path; | 91 FilePath expected_existing_path; |
92 }; | 92 }; |
93 | 93 |
94 // Converts |cache_state| to a GDataCache::CacheEntry. | 94 // Converts |cache_state| to a GDataCacheEntry. |
95 GDataCache::CacheEntry ToCacheEntry(int cache_state) { | 95 GDataCacheEntry ToCacheEntry(int cache_state) { |
96 return GDataCache::CacheEntry("dummy_md5", cache_state); | 96 return GDataCacheEntry("dummy_md5", cache_state); |
97 } | 97 } |
98 | 98 |
99 } // namespace | 99 } // namespace |
100 | 100 |
101 class MockFreeDiskSpaceGetter : public FreeDiskSpaceGetterInterface { | 101 class MockFreeDiskSpaceGetter : public FreeDiskSpaceGetterInterface { |
102 public: | 102 public: |
103 virtual ~MockFreeDiskSpaceGetter() {} | 103 virtual ~MockFreeDiskSpaceGetter() {} |
104 MOCK_CONST_METHOD0(AmountOfFreeDiskSpace, int64()); | 104 MOCK_CONST_METHOD0(AmountOfFreeDiskSpace, int64()); |
105 }; | 105 }; |
106 | 106 |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 ToCacheEntry(resource.cache_state).IsPresent() ? | 330 ToCacheEntry(resource.cache_state).IsPresent() ? |
331 base::PLATFORM_FILE_OK : | 331 base::PLATFORM_FILE_OK : |
332 base::PLATFORM_FILE_ERROR_NOT_FOUND, | 332 base::PLATFORM_FILE_ERROR_NOT_FOUND, |
333 resource.expected_file_extension); | 333 resource.expected_file_extension); |
334 EXPECT_EQ(1, num_callback_invocations_); | 334 EXPECT_EQ(1, num_callback_invocations_); |
335 | 335 |
336 // Verify cache state. | 336 // Verify cache state. |
337 std::string md5; | 337 std::string md5; |
338 if (ToCacheEntry(resource.cache_state).IsPresent()) | 338 if (ToCacheEntry(resource.cache_state).IsPresent()) |
339 md5 = resource.md5; | 339 md5 = resource.md5; |
340 scoped_ptr<GDataCache::CacheEntry> cache_entry = | 340 scoped_ptr<GDataCacheEntry> cache_entry = |
341 GetCacheEntryFromOriginThread(resource.resource_id, md5); | 341 GetCacheEntryFromOriginThread(resource.resource_id, md5); |
342 ASSERT_TRUE(cache_entry.get()); | 342 ASSERT_TRUE(cache_entry.get()); |
343 EXPECT_EQ(resource.cache_state, cache_entry->cache_state()); | 343 EXPECT_EQ(resource.cache_state, cache_entry->cache_state()); |
344 EXPECT_EQ(resource.expected_sub_dir_type, | 344 EXPECT_EQ(resource.expected_sub_dir_type, |
345 cache_entry->GetSubDirectoryType()); | 345 GDataCache::GetSubDirectoryType(*cache_entry)); |
346 } | 346 } |
347 } | 347 } |
348 | 348 |
349 void TestGetFileFromCacheByResourceIdAndMd5( | 349 void TestGetFileFromCacheByResourceIdAndMd5( |
350 const std::string& resource_id, | 350 const std::string& resource_id, |
351 const std::string& md5, | 351 const std::string& md5, |
352 base::PlatformFileError expected_error, | 352 base::PlatformFileError expected_error, |
353 const std::string& expected_file_extension) { | 353 const std::string& expected_file_extension) { |
354 expected_error_ = expected_error; | 354 expected_error_ = expected_error; |
355 expected_file_extension_ = expected_file_extension; | 355 expected_file_extension_ = expected_file_extension; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 } | 417 } |
418 | 418 |
419 void VerifyRemoveFromCache(base::PlatformFileError error, | 419 void VerifyRemoveFromCache(base::PlatformFileError error, |
420 const std::string& resource_id, | 420 const std::string& resource_id, |
421 const std::string& md5) { | 421 const std::string& md5) { |
422 ++num_callback_invocations_; | 422 ++num_callback_invocations_; |
423 | 423 |
424 EXPECT_EQ(expected_error_, error); | 424 EXPECT_EQ(expected_error_, error); |
425 | 425 |
426 // Verify cache map. | 426 // Verify cache map. |
427 scoped_ptr<GDataCache::CacheEntry> cache_entry = | 427 scoped_ptr<GDataCacheEntry> cache_entry = |
428 GetCacheEntryFromOriginThread(resource_id, md5); | 428 GetCacheEntryFromOriginThread(resource_id, md5); |
429 if (cache_entry.get()) | 429 if (cache_entry.get()) |
430 EXPECT_TRUE(cache_entry->IsDirty()); | 430 EXPECT_TRUE(cache_entry->IsDirty()); |
431 | 431 |
432 // If entry doesn't exist, verify that: | 432 // If entry doesn't exist, verify that: |
433 // - no files with "<resource_id>.* exists in persistent and tmp dirs | 433 // - no files with "<resource_id>.* exists in persistent and tmp dirs |
434 // - no "<resource_id>" symlink exists in pinned and outgoing dirs. | 434 // - no "<resource_id>" symlink exists in pinned and outgoing dirs. |
435 std::vector<PathToVerify> paths_to_verify; | 435 std::vector<PathToVerify> paths_to_verify; |
436 paths_to_verify.push_back( // Index 0: CACHE_TYPE_TMP. | 436 paths_to_verify.push_back( // Index 0: CACHE_TYPE_TMP. |
437 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", | 437 PathToVerify(cache_->GetCacheFilePath(resource_id, "*", |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 expected_cache_state_ = expected_cache_state; | 557 expected_cache_state_ = expected_cache_state; |
558 | 558 |
559 cache_->GetCacheEntryOnUIThread(resource_id, md5, | 559 cache_->GetCacheEntryOnUIThread(resource_id, md5, |
560 base::Bind(&GDataCacheTest::VerifyGetCacheState, | 560 base::Bind(&GDataCacheTest::VerifyGetCacheState, |
561 base::Unretained(this))); | 561 base::Unretained(this))); |
562 | 562 |
563 test_util::RunBlockingPoolTask(); | 563 test_util::RunBlockingPoolTask(); |
564 } | 564 } |
565 | 565 |
566 void VerifyGetCacheState(bool success, | 566 void VerifyGetCacheState(bool success, |
567 const GDataCache::CacheEntry& cache_entry) { | 567 const GDataCacheEntry& cache_entry) { |
568 ++num_callback_invocations_; | 568 ++num_callback_invocations_; |
569 | 569 |
570 EXPECT_EQ(expected_success_, success); | 570 EXPECT_EQ(expected_success_, success); |
571 | 571 |
572 if (success) { | 572 if (success) { |
573 EXPECT_EQ(expected_cache_state_, cache_entry.cache_state()); | 573 EXPECT_EQ(expected_cache_state_, cache_entry.cache_state()); |
574 } | 574 } |
575 } | 575 } |
576 | 576 |
577 void TestMarkDirty( | 577 void TestMarkDirty( |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
685 } | 685 } |
686 | 686 |
687 void VerifyCacheFileState(base::PlatformFileError error, | 687 void VerifyCacheFileState(base::PlatformFileError error, |
688 const std::string& resource_id, | 688 const std::string& resource_id, |
689 const std::string& md5) { | 689 const std::string& md5) { |
690 ++num_callback_invocations_; | 690 ++num_callback_invocations_; |
691 | 691 |
692 EXPECT_EQ(expected_error_, error); | 692 EXPECT_EQ(expected_error_, error); |
693 | 693 |
694 // Verify cache map. | 694 // Verify cache map. |
695 scoped_ptr<GDataCache::CacheEntry> cache_entry = | 695 scoped_ptr<GDataCacheEntry> cache_entry = |
696 GetCacheEntryFromOriginThread(resource_id, md5); | 696 GetCacheEntryFromOriginThread(resource_id, md5); |
697 if (ToCacheEntry(expected_cache_state_).IsPresent() || | 697 if (ToCacheEntry(expected_cache_state_).IsPresent() || |
698 ToCacheEntry(expected_cache_state_).IsPinned()) { | 698 ToCacheEntry(expected_cache_state_).IsPinned()) { |
699 ASSERT_TRUE(cache_entry.get()); | 699 ASSERT_TRUE(cache_entry.get()); |
700 EXPECT_EQ(expected_cache_state_, cache_entry->cache_state()); | 700 EXPECT_EQ(expected_cache_state_, cache_entry->cache_state()); |
701 EXPECT_EQ(expected_sub_dir_type_, cache_entry->GetSubDirectoryType()); | 701 EXPECT_EQ(expected_sub_dir_type_, |
| 702 GDataCache::GetSubDirectoryType(*cache_entry)); |
702 } else { | 703 } else { |
703 EXPECT_FALSE(cache_entry.get()); | 704 EXPECT_FALSE(cache_entry.get()); |
704 } | 705 } |
705 | 706 |
706 // Verify actual cache file. | 707 // Verify actual cache file. |
707 FilePath dest_path = cache_->GetCacheFilePath( | 708 FilePath dest_path = cache_->GetCacheFilePath( |
708 resource_id, | 709 resource_id, |
709 md5, | 710 md5, |
710 ToCacheEntry(expected_cache_state_).IsPinned() || | 711 ToCacheEntry(expected_cache_state_).IsPinned() || |
711 ToCacheEntry(expected_cache_state_).IsDirty() ? | 712 ToCacheEntry(expected_cache_state_).IsDirty() ? |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 | 764 |
764 FilePath GetCacheFilePath(const std::string& resource_id, | 765 FilePath GetCacheFilePath(const std::string& resource_id, |
765 const std::string& md5, | 766 const std::string& md5, |
766 GDataCache::CacheSubDirectoryType sub_dir_type, | 767 GDataCache::CacheSubDirectoryType sub_dir_type, |
767 GDataCache::CachedFileOrigin file_origin) { | 768 GDataCache::CachedFileOrigin file_origin) { |
768 return cache_->GetCacheFilePath(resource_id, md5, sub_dir_type, | 769 return cache_->GetCacheFilePath(resource_id, md5, sub_dir_type, |
769 file_origin); | 770 file_origin); |
770 } | 771 } |
771 | 772 |
772 // Helper function to call GetCacheEntry from origin thread. | 773 // Helper function to call GetCacheEntry from origin thread. |
773 scoped_ptr<GDataCache::CacheEntry> GetCacheEntryFromOriginThread( | 774 scoped_ptr<GDataCacheEntry> GetCacheEntryFromOriginThread( |
774 const std::string& resource_id, | 775 const std::string& resource_id, |
775 const std::string& md5) { | 776 const std::string& md5) { |
776 scoped_ptr<GDataCache::CacheEntry> cache_entry; | 777 scoped_ptr<GDataCacheEntry> cache_entry; |
777 content::BrowserThread::GetBlockingPool() | 778 content::BrowserThread::GetBlockingPool() |
778 ->GetSequencedTaskRunner(sequence_token_)->PostTask( | 779 ->GetSequencedTaskRunner(sequence_token_)->PostTask( |
779 FROM_HERE, | 780 FROM_HERE, |
780 base::Bind( | 781 base::Bind( |
781 &GDataCacheTest::GetCacheEntryFromOriginThreadInternal, | 782 &GDataCacheTest::GetCacheEntryFromOriginThreadInternal, |
782 base::Unretained(this), | 783 base::Unretained(this), |
783 resource_id, | 784 resource_id, |
784 md5, | 785 md5, |
785 &cache_entry)); | 786 &cache_entry)); |
786 test_util::RunBlockingPoolTask(); | 787 test_util::RunBlockingPoolTask(); |
787 return cache_entry.Pass(); | 788 return cache_entry.Pass(); |
788 } | 789 } |
789 | 790 |
790 // Used to implement GetCacheEntry. | 791 // Used to implement GetCacheEntry. |
791 void GetCacheEntryFromOriginThreadInternal( | 792 void GetCacheEntryFromOriginThreadInternal( |
792 const std::string& resource_id, | 793 const std::string& resource_id, |
793 const std::string& md5, | 794 const std::string& md5, |
794 scoped_ptr<GDataCache::CacheEntry>* cache_entry) { | 795 scoped_ptr<GDataCacheEntry>* cache_entry) { |
795 cache_entry->reset(cache_->GetCacheEntry(resource_id, md5).release()); | 796 cache_entry->reset(cache_->GetCacheEntry(resource_id, md5).release()); |
796 } | 797 } |
797 | 798 |
798 // Returns true if the cache entry exists for the given resource ID and MD5. | 799 // Returns true if the cache entry exists for the given resource ID and MD5. |
799 bool CacheEntryExists(const std::string& resource_id, | 800 bool CacheEntryExists(const std::string& resource_id, |
800 const std::string& md5) { | 801 const std::string& md5) { |
801 return GetCacheEntryFromOriginThread(resource_id, md5).get(); | 802 return GetCacheEntryFromOriginThread(resource_id, md5).get(); |
802 } | 803 } |
803 | 804 |
804 void TestGetCacheFilePath(const std::string& resource_id, | 805 void TestGetCacheFilePath(const std::string& resource_id, |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
871 bool expect_outgoing_symlink_; | 872 bool expect_outgoing_symlink_; |
872 std::string expected_file_extension_; | 873 std::string expected_file_extension_; |
873 int root_feed_changestamp_; | 874 int root_feed_changestamp_; |
874 }; | 875 }; |
875 | 876 |
876 TEST_F(GDataCacheTest, InitializeCache) { | 877 TEST_F(GDataCacheTest, InitializeCache) { |
877 PrepareForInitCacheTest(); | 878 PrepareForInitCacheTest(); |
878 TestInitializeCache(); | 879 TestInitializeCache(); |
879 } | 880 } |
880 | 881 |
881 TEST_F(GDataCacheTest, CacheEntry_CacheStateChanges) { | |
882 GDataCache::CacheEntry cache_entry("dummy_md5", GDataCache::CACHE_STATE_NONE); | |
883 EXPECT_FALSE(cache_entry.IsPresent()); | |
884 EXPECT_FALSE(cache_entry.IsPinned()); | |
885 EXPECT_FALSE(cache_entry.IsDirty()); | |
886 EXPECT_FALSE(cache_entry.IsMounted()); | |
887 EXPECT_FALSE(cache_entry.IsPersistent()); | |
888 | |
889 cache_entry.SetPresent(true); | |
890 EXPECT_TRUE(cache_entry.IsPresent()); | |
891 EXPECT_FALSE(cache_entry.IsPinned()); | |
892 EXPECT_FALSE(cache_entry.IsDirty()); | |
893 EXPECT_FALSE(cache_entry.IsMounted()); | |
894 EXPECT_FALSE(cache_entry.IsPersistent()); | |
895 | |
896 cache_entry.SetPinned(true); | |
897 EXPECT_TRUE(cache_entry.IsPresent()); | |
898 EXPECT_TRUE(cache_entry.IsPinned()); | |
899 EXPECT_FALSE(cache_entry.IsDirty()); | |
900 EXPECT_FALSE(cache_entry.IsMounted()); | |
901 EXPECT_FALSE(cache_entry.IsPersistent()); | |
902 | |
903 cache_entry.SetDirty(true); | |
904 EXPECT_TRUE(cache_entry.IsPresent()); | |
905 EXPECT_TRUE(cache_entry.IsPinned()); | |
906 EXPECT_TRUE(cache_entry.IsDirty()); | |
907 EXPECT_FALSE(cache_entry.IsMounted()); | |
908 EXPECT_FALSE(cache_entry.IsPersistent()); | |
909 | |
910 cache_entry.SetMounted(true); | |
911 EXPECT_TRUE(cache_entry.IsPresent()); | |
912 EXPECT_TRUE(cache_entry.IsPinned()); | |
913 EXPECT_TRUE(cache_entry.IsDirty()); | |
914 EXPECT_TRUE(cache_entry.IsMounted()); | |
915 EXPECT_FALSE(cache_entry.IsPersistent()); | |
916 | |
917 cache_entry.SetPersistent(true); | |
918 EXPECT_TRUE(cache_entry.IsPresent()); | |
919 EXPECT_TRUE(cache_entry.IsPinned()); | |
920 EXPECT_TRUE(cache_entry.IsDirty()); | |
921 EXPECT_TRUE(cache_entry.IsMounted()); | |
922 EXPECT_TRUE(cache_entry.IsPersistent()); | |
923 | |
924 cache_entry.SetPresent(false); | |
925 EXPECT_FALSE(cache_entry.IsPresent()); | |
926 EXPECT_TRUE(cache_entry.IsPinned()); | |
927 EXPECT_TRUE(cache_entry.IsDirty()); | |
928 EXPECT_TRUE(cache_entry.IsMounted()); | |
929 EXPECT_TRUE(cache_entry.IsPersistent()); | |
930 | |
931 cache_entry.SetPresent(false); | |
932 EXPECT_FALSE(cache_entry.IsPresent()); | |
933 EXPECT_TRUE(cache_entry.IsPinned()); | |
934 EXPECT_TRUE(cache_entry.IsDirty()); | |
935 EXPECT_TRUE(cache_entry.IsMounted()); | |
936 EXPECT_TRUE(cache_entry.IsPersistent()); | |
937 | |
938 cache_entry.SetPinned(false); | |
939 EXPECT_FALSE(cache_entry.IsPresent()); | |
940 EXPECT_FALSE(cache_entry.IsPinned()); | |
941 EXPECT_TRUE(cache_entry.IsDirty()); | |
942 EXPECT_TRUE(cache_entry.IsMounted()); | |
943 EXPECT_TRUE(cache_entry.IsPersistent()); | |
944 | |
945 cache_entry.SetDirty(false); | |
946 EXPECT_FALSE(cache_entry.IsPresent()); | |
947 EXPECT_FALSE(cache_entry.IsPinned()); | |
948 EXPECT_FALSE(cache_entry.IsDirty()); | |
949 EXPECT_TRUE(cache_entry.IsMounted()); | |
950 EXPECT_TRUE(cache_entry.IsPersistent()); | |
951 | |
952 cache_entry.SetMounted(false); | |
953 EXPECT_FALSE(cache_entry.IsPresent()); | |
954 EXPECT_FALSE(cache_entry.IsPinned()); | |
955 EXPECT_FALSE(cache_entry.IsDirty()); | |
956 EXPECT_FALSE(cache_entry.IsMounted()); | |
957 EXPECT_TRUE(cache_entry.IsPersistent()); | |
958 | |
959 cache_entry.SetPersistent(false); | |
960 EXPECT_FALSE(cache_entry.IsPresent()); | |
961 EXPECT_FALSE(cache_entry.IsPinned()); | |
962 EXPECT_FALSE(cache_entry.IsDirty()); | |
963 EXPECT_FALSE(cache_entry.IsMounted()); | |
964 EXPECT_FALSE(cache_entry.IsPersistent()); | |
965 } | |
966 | |
967 TEST_F(GDataCacheTest, GetCacheFilePath) { | 882 TEST_F(GDataCacheTest, GetCacheFilePath) { |
968 // Use alphanumeric characters for resource id. | 883 // Use alphanumeric characters for resource id. |
969 std::string resource_id("pdf:1a2b"); | 884 std::string resource_id("pdf:1a2b"); |
970 std::string md5("abcdef0123456789"); | 885 std::string md5("abcdef0123456789"); |
971 TestGetCacheFilePath(resource_id, md5, | 886 TestGetCacheFilePath(resource_id, md5, |
972 resource_id + FilePath::kExtensionSeparator + md5); | 887 resource_id + FilePath::kExtensionSeparator + md5); |
973 EXPECT_EQ(0, num_callback_invocations_); | 888 EXPECT_EQ(0, num_callback_invocations_); |
974 | 889 |
975 // Use non-alphanumeric characters for resource id, including '.' which is an | 890 // Use non-alphanumeric characters for resource id, including '.' which is an |
976 // extension separator, to test that the characters are escaped and unescaped | 891 // extension separator, to test that the characters are escaped and unescaped |
977 // correctly, and '.' doesn't mess up the filename format and operations. | 892 // correctly, and '.' doesn't mess up the filename format and operations. |
978 resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?"; | 893 resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?"; |
979 std::string escaped_resource_id = util::EscapeCacheFileName(resource_id); | 894 std::string escaped_resource_id = util::EscapeCacheFileName(resource_id); |
980 std::string escaped_md5 = util::EscapeCacheFileName(md5); | 895 std::string escaped_md5 = util::EscapeCacheFileName(md5); |
981 num_callback_invocations_ = 0; | 896 num_callback_invocations_ = 0; |
982 TestGetCacheFilePath(resource_id, md5, | 897 TestGetCacheFilePath(resource_id, md5, |
983 escaped_resource_id + FilePath::kExtensionSeparator + | 898 escaped_resource_id + FilePath::kExtensionSeparator + |
984 escaped_md5); | 899 escaped_md5); |
985 EXPECT_EQ(0, num_callback_invocations_); | 900 EXPECT_EQ(0, num_callback_invocations_); |
986 } | 901 } |
987 | 902 |
988 TEST_F(GDataCacheTest, StoreToCacheSimple) { | 903 TEST_F(GDataCacheTest, StoreToCacheSimple) { |
989 std::string resource_id("pdf:1a2b"); | 904 std::string resource_id("pdf:1a2b"); |
990 std::string md5("abcdef0123456789"); | 905 std::string md5("abcdef0123456789"); |
991 | 906 |
992 // Store an existing file. | 907 // Store an existing file. |
993 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 908 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
994 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 909 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
995 GDataCache::CACHE_TYPE_TMP); | 910 GDataCache::CACHE_TYPE_TMP); |
996 EXPECT_EQ(1, num_callback_invocations_); | 911 EXPECT_EQ(1, num_callback_invocations_); |
997 | 912 |
998 // Store a non-existent file to the same |resource_id| and |md5|. | 913 // Store a non-existent file to the same |resource_id| and |md5|. |
999 num_callback_invocations_ = 0; | 914 num_callback_invocations_ = 0; |
1000 TestStoreToCache(resource_id, md5, FilePath("./non_existent.json"), | 915 TestStoreToCache(resource_id, md5, FilePath("./non_existent.json"), |
1001 base::PLATFORM_FILE_ERROR_NOT_FOUND, | 916 base::PLATFORM_FILE_ERROR_NOT_FOUND, |
1002 GDataCache::CACHE_STATE_PRESENT, | 917 CACHE_STATE_PRESENT, |
1003 GDataCache::CACHE_TYPE_TMP); | 918 GDataCache::CACHE_TYPE_TMP); |
1004 EXPECT_EQ(1, num_callback_invocations_); | 919 EXPECT_EQ(1, num_callback_invocations_); |
1005 | 920 |
1006 // Store a different existing file to the same |resource_id| but different | 921 // Store a different existing file to the same |resource_id| but different |
1007 // |md5|. | 922 // |md5|. |
1008 md5 = "new_md5"; | 923 md5 = "new_md5"; |
1009 num_callback_invocations_ = 0; | 924 num_callback_invocations_ = 0; |
1010 TestStoreToCache(resource_id, md5, GetTestFilePath("subdir_feed.json"), | 925 TestStoreToCache(resource_id, md5, GetTestFilePath("subdir_feed.json"), |
1011 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 926 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1012 GDataCache::CACHE_TYPE_TMP); | 927 GDataCache::CACHE_TYPE_TMP); |
1013 EXPECT_EQ(1, num_callback_invocations_); | 928 EXPECT_EQ(1, num_callback_invocations_); |
1014 | 929 |
1015 // Verify that there's only one file with name <resource_id>, i.e. previously | 930 // Verify that there's only one file with name <resource_id>, i.e. previously |
1016 // cached file with the different md5 should be deleted. | 931 // cached file with the different md5 should be deleted. |
1017 FilePath path = GetCacheFilePath( | 932 FilePath path = GetCacheFilePath( |
1018 resource_id, "*", | 933 resource_id, "*", |
1019 (ToCacheEntry(expected_cache_state_).IsPinned() ? | 934 (ToCacheEntry(expected_cache_state_).IsPinned() ? |
1020 GDataCache::CACHE_TYPE_PERSISTENT : | 935 GDataCache::CACHE_TYPE_PERSISTENT : |
1021 GDataCache::CACHE_TYPE_TMP), | 936 GDataCache::CACHE_TYPE_TMP), |
(...skipping 11 matching lines...) Expand all Loading... |
1033 current.BaseName().value()); | 948 current.BaseName().value()); |
1034 } | 949 } |
1035 EXPECT_EQ(1U, num_files_found); | 950 EXPECT_EQ(1U, num_files_found); |
1036 } | 951 } |
1037 | 952 |
1038 TEST_F(GDataCacheTest, GetFromCacheSimple) { | 953 TEST_F(GDataCacheTest, GetFromCacheSimple) { |
1039 std::string resource_id("pdf:1a2b"); | 954 std::string resource_id("pdf:1a2b"); |
1040 std::string md5("abcdef0123456789"); | 955 std::string md5("abcdef0123456789"); |
1041 // First store a file to cache. | 956 // First store a file to cache. |
1042 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 957 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1043 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 958 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1044 GDataCache::CACHE_TYPE_TMP); | 959 GDataCache::CACHE_TYPE_TMP); |
1045 | 960 |
1046 // Then try to get the existing file from cache. | 961 // Then try to get the existing file from cache. |
1047 num_callback_invocations_ = 0; | 962 num_callback_invocations_ = 0; |
1048 TestGetFileFromCacheByResourceIdAndMd5( | 963 TestGetFileFromCacheByResourceIdAndMd5( |
1049 resource_id, md5, base::PLATFORM_FILE_OK, md5); | 964 resource_id, md5, base::PLATFORM_FILE_OK, md5); |
1050 EXPECT_EQ(1, num_callback_invocations_); | 965 EXPECT_EQ(1, num_callback_invocations_); |
1051 | 966 |
1052 // Get file from cache with same resource id as existing file but different | 967 // Get file from cache with same resource id as existing file but different |
1053 // md5. | 968 // md5. |
(...skipping 10 matching lines...) Expand all Loading... |
1064 resource_id, md5, base::PLATFORM_FILE_ERROR_NOT_FOUND, md5); | 979 resource_id, md5, base::PLATFORM_FILE_ERROR_NOT_FOUND, md5); |
1065 EXPECT_EQ(1, num_callback_invocations_); | 980 EXPECT_EQ(1, num_callback_invocations_); |
1066 } | 981 } |
1067 | 982 |
1068 TEST_F(GDataCacheTest, RemoveFromCacheSimple) { | 983 TEST_F(GDataCacheTest, RemoveFromCacheSimple) { |
1069 // Use alphanumeric characters for resource id. | 984 // Use alphanumeric characters for resource id. |
1070 std::string resource_id("pdf:1a2b"); | 985 std::string resource_id("pdf:1a2b"); |
1071 std::string md5("abcdef0123456789"); | 986 std::string md5("abcdef0123456789"); |
1072 // First store a file to cache. | 987 // First store a file to cache. |
1073 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 988 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1074 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 989 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1075 GDataCache::CACHE_TYPE_TMP); | 990 GDataCache::CACHE_TYPE_TMP); |
1076 | 991 |
1077 // Then try to remove existing file from cache. | 992 // Then try to remove existing file from cache. |
1078 num_callback_invocations_ = 0; | 993 num_callback_invocations_ = 0; |
1079 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); | 994 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); |
1080 EXPECT_EQ(1, num_callback_invocations_); | 995 EXPECT_EQ(1, num_callback_invocations_); |
1081 | 996 |
1082 // Repeat using non-alphanumeric characters for resource id, including '.' | 997 // Repeat using non-alphanumeric characters for resource id, including '.' |
1083 // which is an extension separator. | 998 // which is an extension separator. |
1084 resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?"; | 999 resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?"; |
1085 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1000 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1086 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 1001 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1087 GDataCache::CACHE_TYPE_TMP); | 1002 GDataCache::CACHE_TYPE_TMP); |
1088 | 1003 |
1089 num_callback_invocations_ = 0; | 1004 num_callback_invocations_ = 0; |
1090 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); | 1005 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); |
1091 EXPECT_EQ(1, num_callback_invocations_); | 1006 EXPECT_EQ(1, num_callback_invocations_); |
1092 } | 1007 } |
1093 | 1008 |
1094 TEST_F(GDataCacheTest, PinAndUnpin) { | 1009 TEST_F(GDataCacheTest, PinAndUnpin) { |
1095 EXPECT_CALL(*mock_free_disk_space_checker_, AmountOfFreeDiskSpace()) | 1010 EXPECT_CALL(*mock_free_disk_space_checker_, AmountOfFreeDiskSpace()) |
1096 .Times(AtLeast(1)).WillRepeatedly(Return(kLotsOfSpace)); | 1011 .Times(AtLeast(1)).WillRepeatedly(Return(kLotsOfSpace)); |
1097 | 1012 |
1098 std::string resource_id("pdf:1a2b"); | 1013 std::string resource_id("pdf:1a2b"); |
1099 std::string md5("abcdef0123456789"); | 1014 std::string md5("abcdef0123456789"); |
1100 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(2); | 1015 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(2); |
1101 EXPECT_CALL(*mock_sync_client_, OnCacheUnpinned(resource_id, md5)).Times(1); | 1016 EXPECT_CALL(*mock_sync_client_, OnCacheUnpinned(resource_id, md5)).Times(1); |
1102 | 1017 |
1103 // First store a file to cache. | 1018 // First store a file to cache. |
1104 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1019 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1105 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 1020 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1106 GDataCache::CACHE_TYPE_TMP); | 1021 GDataCache::CACHE_TYPE_TMP); |
1107 | 1022 |
1108 // Pin the existing file in cache. | 1023 // Pin the existing file in cache. |
1109 num_callback_invocations_ = 0; | 1024 num_callback_invocations_ = 0; |
1110 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, | 1025 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, |
1111 GDataCache::CACHE_STATE_PRESENT | GDataCache::CACHE_STATE_PINNED | | 1026 CACHE_STATE_PRESENT | CACHE_STATE_PINNED | |
1112 GDataCache::CACHE_STATE_PERSISTENT, | 1027 CACHE_STATE_PERSISTENT, |
1113 GDataCache::CACHE_TYPE_PERSISTENT); | 1028 GDataCache::CACHE_TYPE_PERSISTENT); |
1114 EXPECT_EQ(1, num_callback_invocations_); | 1029 EXPECT_EQ(1, num_callback_invocations_); |
1115 | 1030 |
1116 // Unpin the existing file in cache. | 1031 // Unpin the existing file in cache. |
1117 num_callback_invocations_ = 0; | 1032 num_callback_invocations_ = 0; |
1118 TestUnpin(resource_id, md5, base::PLATFORM_FILE_OK, | 1033 TestUnpin(resource_id, md5, base::PLATFORM_FILE_OK, |
1119 GDataCache::CACHE_STATE_PRESENT, | 1034 CACHE_STATE_PRESENT, |
1120 GDataCache::CACHE_TYPE_TMP); | 1035 GDataCache::CACHE_TYPE_TMP); |
1121 EXPECT_EQ(1, num_callback_invocations_); | 1036 EXPECT_EQ(1, num_callback_invocations_); |
1122 | 1037 |
1123 // Pin back the same existing file in cache. | 1038 // Pin back the same existing file in cache. |
1124 num_callback_invocations_ = 0; | 1039 num_callback_invocations_ = 0; |
1125 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, | 1040 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, |
1126 GDataCache::CACHE_STATE_PRESENT | GDataCache::CACHE_STATE_PINNED | | 1041 CACHE_STATE_PRESENT | CACHE_STATE_PINNED | |
1127 GDataCache::CACHE_STATE_PERSISTENT, | 1042 CACHE_STATE_PERSISTENT, |
1128 GDataCache::CACHE_TYPE_PERSISTENT); | 1043 GDataCache::CACHE_TYPE_PERSISTENT); |
1129 EXPECT_EQ(1, num_callback_invocations_); | 1044 EXPECT_EQ(1, num_callback_invocations_); |
1130 | 1045 |
1131 // Pin a non-existent file in cache. | 1046 // Pin a non-existent file in cache. |
1132 resource_id = "document:1a2b"; | 1047 resource_id = "document:1a2b"; |
1133 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); | 1048 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); |
1134 EXPECT_CALL(*mock_sync_client_, OnCacheUnpinned(resource_id, md5)).Times(1); | 1049 EXPECT_CALL(*mock_sync_client_, OnCacheUnpinned(resource_id, md5)).Times(1); |
1135 | 1050 |
1136 num_callback_invocations_ = 0; | 1051 num_callback_invocations_ = 0; |
1137 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, | 1052 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, |
1138 GDataCache::CACHE_STATE_PINNED, | 1053 CACHE_STATE_PINNED, |
1139 GDataCache::CACHE_TYPE_TMP); | 1054 GDataCache::CACHE_TYPE_TMP); |
1140 EXPECT_EQ(1, num_callback_invocations_); | 1055 EXPECT_EQ(1, num_callback_invocations_); |
1141 | 1056 |
1142 // Unpin the previously pinned non-existent file in cache. | 1057 // Unpin the previously pinned non-existent file in cache. |
1143 num_callback_invocations_ = 0; | 1058 num_callback_invocations_ = 0; |
1144 TestUnpin(resource_id, md5, base::PLATFORM_FILE_OK, | 1059 TestUnpin(resource_id, md5, base::PLATFORM_FILE_OK, |
1145 GDataCache::CACHE_STATE_NONE, | 1060 CACHE_STATE_NONE, |
1146 GDataCache::CACHE_TYPE_TMP); | 1061 GDataCache::CACHE_TYPE_TMP); |
1147 EXPECT_EQ(1, num_callback_invocations_); | 1062 EXPECT_EQ(1, num_callback_invocations_); |
1148 | 1063 |
1149 // Unpin a file that doesn't exist in cache and is not pinned, i.e. cache | 1064 // Unpin a file that doesn't exist in cache and is not pinned, i.e. cache |
1150 // has zero knowledge of the file. | 1065 // has zero knowledge of the file. |
1151 resource_id = "not-in-cache:1a2b"; | 1066 resource_id = "not-in-cache:1a2b"; |
1152 // Because unpinning will fail, OnCacheUnpinned() won't be run. | 1067 // Because unpinning will fail, OnCacheUnpinned() won't be run. |
1153 EXPECT_CALL(*mock_sync_client_, OnCacheUnpinned(resource_id, md5)).Times(0); | 1068 EXPECT_CALL(*mock_sync_client_, OnCacheUnpinned(resource_id, md5)).Times(0); |
1154 | 1069 |
1155 num_callback_invocations_ = 0; | 1070 num_callback_invocations_ = 0; |
1156 TestUnpin(resource_id, md5, base::PLATFORM_FILE_ERROR_NOT_FOUND, | 1071 TestUnpin(resource_id, md5, base::PLATFORM_FILE_ERROR_NOT_FOUND, |
1157 GDataCache::CACHE_STATE_NONE, | 1072 CACHE_STATE_NONE, |
1158 GDataCache::CACHE_TYPE_TMP /* non-applicable */); | 1073 GDataCache::CACHE_TYPE_TMP /* non-applicable */); |
1159 EXPECT_EQ(1, num_callback_invocations_); | 1074 EXPECT_EQ(1, num_callback_invocations_); |
1160 } | 1075 } |
1161 | 1076 |
1162 TEST_F(GDataCacheTest, StoreToCachePinned) { | 1077 TEST_F(GDataCacheTest, StoreToCachePinned) { |
1163 std::string resource_id("pdf:1a2b"); | 1078 std::string resource_id("pdf:1a2b"); |
1164 std::string md5("abcdef0123456789"); | 1079 std::string md5("abcdef0123456789"); |
1165 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); | 1080 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); |
1166 | 1081 |
1167 // Pin a non-existent file. | 1082 // Pin a non-existent file. |
1168 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, | 1083 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, |
1169 GDataCache::CACHE_STATE_PINNED, | 1084 CACHE_STATE_PINNED, |
1170 GDataCache::CACHE_TYPE_TMP); | 1085 GDataCache::CACHE_TYPE_TMP); |
1171 | 1086 |
1172 // Store an existing file to a previously pinned file. | 1087 // Store an existing file to a previously pinned file. |
1173 num_callback_invocations_ = 0; | 1088 num_callback_invocations_ = 0; |
1174 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1089 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1175 base::PLATFORM_FILE_OK, | 1090 base::PLATFORM_FILE_OK, |
1176 GDataCache::CACHE_STATE_PRESENT | | 1091 CACHE_STATE_PRESENT | |
1177 GDataCache::CACHE_STATE_PINNED | | 1092 CACHE_STATE_PINNED | |
1178 GDataCache::CACHE_STATE_PERSISTENT, | 1093 CACHE_STATE_PERSISTENT, |
1179 GDataCache::CACHE_TYPE_PERSISTENT); | 1094 GDataCache::CACHE_TYPE_PERSISTENT); |
1180 EXPECT_EQ(1, num_callback_invocations_); | 1095 EXPECT_EQ(1, num_callback_invocations_); |
1181 | 1096 |
1182 // Store a non-existent file to a previously pinned and stored file. | 1097 // Store a non-existent file to a previously pinned and stored file. |
1183 num_callback_invocations_ = 0; | 1098 num_callback_invocations_ = 0; |
1184 TestStoreToCache(resource_id, md5, FilePath("./non_existent.json"), | 1099 TestStoreToCache(resource_id, md5, FilePath("./non_existent.json"), |
1185 base::PLATFORM_FILE_ERROR_NOT_FOUND, | 1100 base::PLATFORM_FILE_ERROR_NOT_FOUND, |
1186 GDataCache::CACHE_STATE_PRESENT | | 1101 CACHE_STATE_PRESENT | |
1187 GDataCache::CACHE_STATE_PINNED | | 1102 CACHE_STATE_PINNED | |
1188 GDataCache::CACHE_STATE_PERSISTENT, | 1103 CACHE_STATE_PERSISTENT, |
1189 GDataCache::CACHE_TYPE_PERSISTENT); | 1104 GDataCache::CACHE_TYPE_PERSISTENT); |
1190 EXPECT_EQ(1, num_callback_invocations_); | 1105 EXPECT_EQ(1, num_callback_invocations_); |
1191 } | 1106 } |
1192 | 1107 |
1193 TEST_F(GDataCacheTest, GetFromCachePinned) { | 1108 TEST_F(GDataCacheTest, GetFromCachePinned) { |
1194 std::string resource_id("pdf:1a2b"); | 1109 std::string resource_id("pdf:1a2b"); |
1195 std::string md5("abcdef0123456789"); | 1110 std::string md5("abcdef0123456789"); |
1196 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); | 1111 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); |
1197 | 1112 |
1198 // Pin a non-existent file. | 1113 // Pin a non-existent file. |
1199 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, | 1114 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, |
1200 GDataCache::CACHE_STATE_PINNED, | 1115 CACHE_STATE_PINNED, |
1201 GDataCache::CACHE_TYPE_TMP); | 1116 GDataCache::CACHE_TYPE_TMP); |
1202 | 1117 |
1203 // Get the non-existent pinned file from cache. | 1118 // Get the non-existent pinned file from cache. |
1204 num_callback_invocations_ = 0; | 1119 num_callback_invocations_ = 0; |
1205 TestGetFileFromCacheByResourceIdAndMd5( | 1120 TestGetFileFromCacheByResourceIdAndMd5( |
1206 resource_id, md5, base::PLATFORM_FILE_ERROR_NOT_FOUND, md5); | 1121 resource_id, md5, base::PLATFORM_FILE_ERROR_NOT_FOUND, md5); |
1207 EXPECT_EQ(1, num_callback_invocations_); | 1122 EXPECT_EQ(1, num_callback_invocations_); |
1208 | 1123 |
1209 // Store an existing file to the previously pinned non-existent file. | 1124 // Store an existing file to the previously pinned non-existent file. |
1210 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1125 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1211 base::PLATFORM_FILE_OK, | 1126 base::PLATFORM_FILE_OK, |
1212 GDataCache::CACHE_STATE_PRESENT | | 1127 CACHE_STATE_PRESENT | |
1213 GDataCache::CACHE_STATE_PINNED | | 1128 CACHE_STATE_PINNED | |
1214 GDataCache::CACHE_STATE_PERSISTENT, | 1129 CACHE_STATE_PERSISTENT, |
1215 GDataCache::CACHE_TYPE_PERSISTENT); | 1130 GDataCache::CACHE_TYPE_PERSISTENT); |
1216 | 1131 |
1217 // Get the previously pinned and stored file from cache. | 1132 // Get the previously pinned and stored file from cache. |
1218 num_callback_invocations_ = 0; | 1133 num_callback_invocations_ = 0; |
1219 TestGetFileFromCacheByResourceIdAndMd5( | 1134 TestGetFileFromCacheByResourceIdAndMd5( |
1220 resource_id, md5, base::PLATFORM_FILE_OK, md5); | 1135 resource_id, md5, base::PLATFORM_FILE_OK, md5); |
1221 EXPECT_EQ(1, num_callback_invocations_); | 1136 EXPECT_EQ(1, num_callback_invocations_); |
1222 } | 1137 } |
1223 | 1138 |
1224 TEST_F(GDataCacheTest, RemoveFromCachePinned) { | 1139 TEST_F(GDataCacheTest, RemoveFromCachePinned) { |
1225 // Use alphanumeric characters for resource_id. | 1140 // Use alphanumeric characters for resource_id. |
1226 std::string resource_id("pdf:1a2b"); | 1141 std::string resource_id("pdf:1a2b"); |
1227 std::string md5("abcdef0123456789"); | 1142 std::string md5("abcdef0123456789"); |
1228 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); | 1143 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); |
1229 | 1144 |
1230 // Store a file to cache, and pin it. | 1145 // Store a file to cache, and pin it. |
1231 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1146 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1232 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 1147 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1233 GDataCache::CACHE_TYPE_TMP); | 1148 GDataCache::CACHE_TYPE_TMP); |
1234 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, | 1149 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, |
1235 GDataCache::CACHE_STATE_PRESENT | GDataCache::CACHE_STATE_PINNED | | 1150 CACHE_STATE_PRESENT | CACHE_STATE_PINNED | |
1236 GDataCache::CACHE_STATE_PERSISTENT, | 1151 CACHE_STATE_PERSISTENT, |
1237 GDataCache::CACHE_TYPE_PERSISTENT); | 1152 GDataCache::CACHE_TYPE_PERSISTENT); |
1238 | 1153 |
1239 // Remove |resource_id| from cache. | 1154 // Remove |resource_id| from cache. |
1240 num_callback_invocations_ = 0; | 1155 num_callback_invocations_ = 0; |
1241 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); | 1156 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); |
1242 EXPECT_EQ(1, num_callback_invocations_); | 1157 EXPECT_EQ(1, num_callback_invocations_); |
1243 | 1158 |
1244 // Repeat using non-alphanumeric characters for resource id, including '.' | 1159 // Repeat using non-alphanumeric characters for resource id, including '.' |
1245 // which is an extension separator. | 1160 // which is an extension separator. |
1246 resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?"; | 1161 resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?"; |
1247 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); | 1162 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); |
1248 | 1163 |
1249 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1164 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1250 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 1165 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1251 GDataCache::CACHE_TYPE_TMP); | 1166 GDataCache::CACHE_TYPE_TMP); |
1252 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, | 1167 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, |
1253 GDataCache::CACHE_STATE_PRESENT | GDataCache::CACHE_STATE_PINNED | | 1168 CACHE_STATE_PRESENT | CACHE_STATE_PINNED | |
1254 GDataCache::CACHE_STATE_PERSISTENT, | 1169 CACHE_STATE_PERSISTENT, |
1255 GDataCache::CACHE_TYPE_PERSISTENT); | 1170 GDataCache::CACHE_TYPE_PERSISTENT); |
1256 | 1171 |
1257 num_callback_invocations_ = 0; | 1172 num_callback_invocations_ = 0; |
1258 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); | 1173 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); |
1259 EXPECT_EQ(1, num_callback_invocations_); | 1174 EXPECT_EQ(1, num_callback_invocations_); |
1260 } | 1175 } |
1261 | 1176 |
1262 TEST_F(GDataCacheTest, DirtyCacheSimple) { | 1177 TEST_F(GDataCacheTest, DirtyCacheSimple) { |
1263 std::string resource_id("pdf:1a2b"); | 1178 std::string resource_id("pdf:1a2b"); |
1264 std::string md5("abcdef0123456789"); | 1179 std::string md5("abcdef0123456789"); |
1265 EXPECT_CALL(*mock_sync_client_, OnCacheCommitted(resource_id)).Times(1); | 1180 EXPECT_CALL(*mock_sync_client_, OnCacheCommitted(resource_id)).Times(1); |
1266 | 1181 |
1267 // First store a file to cache. | 1182 // First store a file to cache. |
1268 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1183 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1269 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 1184 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1270 GDataCache::CACHE_TYPE_TMP); | 1185 GDataCache::CACHE_TYPE_TMP); |
1271 | 1186 |
1272 // Mark the file dirty. | 1187 // Mark the file dirty. |
1273 num_callback_invocations_ = 0; | 1188 num_callback_invocations_ = 0; |
1274 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1189 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1275 GDataCache::CACHE_STATE_PRESENT | | 1190 CACHE_STATE_PRESENT | |
1276 GDataCache::CACHE_STATE_DIRTY | | 1191 CACHE_STATE_DIRTY | |
1277 GDataCache::CACHE_STATE_PERSISTENT, | 1192 CACHE_STATE_PERSISTENT, |
1278 GDataCache::CACHE_TYPE_PERSISTENT); | 1193 GDataCache::CACHE_TYPE_PERSISTENT); |
1279 EXPECT_EQ(1, num_callback_invocations_); | 1194 EXPECT_EQ(1, num_callback_invocations_); |
1280 | 1195 |
1281 // Commit the file dirty. | 1196 // Commit the file dirty. |
1282 num_callback_invocations_ = 0; | 1197 num_callback_invocations_ = 0; |
1283 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1198 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1284 GDataCache::CACHE_STATE_PRESENT | | 1199 CACHE_STATE_PRESENT | |
1285 GDataCache::CACHE_STATE_DIRTY | | 1200 CACHE_STATE_DIRTY | |
1286 GDataCache::CACHE_STATE_PERSISTENT, | 1201 CACHE_STATE_PERSISTENT, |
1287 GDataCache::CACHE_TYPE_PERSISTENT); | 1202 GDataCache::CACHE_TYPE_PERSISTENT); |
1288 EXPECT_EQ(1, num_callback_invocations_); | 1203 EXPECT_EQ(1, num_callback_invocations_); |
1289 | 1204 |
1290 // Clear dirty state of the file. | 1205 // Clear dirty state of the file. |
1291 num_callback_invocations_ = 0; | 1206 num_callback_invocations_ = 0; |
1292 TestClearDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1207 TestClearDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1293 GDataCache::CACHE_STATE_PRESENT, | 1208 CACHE_STATE_PRESENT, |
1294 GDataCache::CACHE_TYPE_TMP); | 1209 GDataCache::CACHE_TYPE_TMP); |
1295 EXPECT_EQ(1, num_callback_invocations_); | 1210 EXPECT_EQ(1, num_callback_invocations_); |
1296 } | 1211 } |
1297 | 1212 |
1298 TEST_F(GDataCacheTest, DirtyCachePinned) { | 1213 TEST_F(GDataCacheTest, DirtyCachePinned) { |
1299 std::string resource_id("pdf:1a2b"); | 1214 std::string resource_id("pdf:1a2b"); |
1300 std::string md5("abcdef0123456789"); | 1215 std::string md5("abcdef0123456789"); |
1301 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); | 1216 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); |
1302 EXPECT_CALL(*mock_sync_client_, OnCacheCommitted(resource_id)).Times(1); | 1217 EXPECT_CALL(*mock_sync_client_, OnCacheCommitted(resource_id)).Times(1); |
1303 | 1218 |
1304 // First store a file to cache and pin it. | 1219 // First store a file to cache and pin it. |
1305 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1220 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1306 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 1221 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1307 GDataCache::CACHE_TYPE_TMP); | 1222 GDataCache::CACHE_TYPE_TMP); |
1308 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, | 1223 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, |
1309 GDataCache::CACHE_STATE_PRESENT | GDataCache::CACHE_STATE_PINNED | | 1224 CACHE_STATE_PRESENT | CACHE_STATE_PINNED | |
1310 GDataCache::CACHE_STATE_PERSISTENT, | 1225 CACHE_STATE_PERSISTENT, |
1311 GDataCache::CACHE_TYPE_PERSISTENT); | 1226 GDataCache::CACHE_TYPE_PERSISTENT); |
1312 | 1227 |
1313 // Mark the file dirty. | 1228 // Mark the file dirty. |
1314 num_callback_invocations_ = 0; | 1229 num_callback_invocations_ = 0; |
1315 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1230 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1316 GDataCache::CACHE_STATE_PRESENT | | 1231 CACHE_STATE_PRESENT | |
1317 GDataCache::CACHE_STATE_DIRTY | | 1232 CACHE_STATE_DIRTY | |
1318 GDataCache::CACHE_STATE_PINNED | | 1233 CACHE_STATE_PINNED | |
1319 GDataCache::CACHE_STATE_PERSISTENT, | 1234 CACHE_STATE_PERSISTENT, |
1320 GDataCache::CACHE_TYPE_PERSISTENT); | 1235 GDataCache::CACHE_TYPE_PERSISTENT); |
1321 EXPECT_EQ(1, num_callback_invocations_); | 1236 EXPECT_EQ(1, num_callback_invocations_); |
1322 | 1237 |
1323 // Commit the file dirty. | 1238 // Commit the file dirty. |
1324 num_callback_invocations_ = 0; | 1239 num_callback_invocations_ = 0; |
1325 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1240 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1326 GDataCache::CACHE_STATE_PRESENT | | 1241 CACHE_STATE_PRESENT | |
1327 GDataCache::CACHE_STATE_DIRTY | | 1242 CACHE_STATE_DIRTY | |
1328 GDataCache::CACHE_STATE_PINNED | | 1243 CACHE_STATE_PINNED | |
1329 GDataCache::CACHE_STATE_PERSISTENT, | 1244 CACHE_STATE_PERSISTENT, |
1330 GDataCache::CACHE_TYPE_PERSISTENT); | 1245 GDataCache::CACHE_TYPE_PERSISTENT); |
1331 EXPECT_EQ(1, num_callback_invocations_); | 1246 EXPECT_EQ(1, num_callback_invocations_); |
1332 | 1247 |
1333 // Clear dirty state of the file. | 1248 // Clear dirty state of the file. |
1334 num_callback_invocations_ = 0; | 1249 num_callback_invocations_ = 0; |
1335 TestClearDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1250 TestClearDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1336 GDataCache::CACHE_STATE_PRESENT | | 1251 CACHE_STATE_PRESENT | |
1337 GDataCache::CACHE_STATE_PINNED | | 1252 CACHE_STATE_PINNED | |
1338 GDataCache::CACHE_STATE_PERSISTENT, | 1253 CACHE_STATE_PERSISTENT, |
1339 GDataCache::CACHE_TYPE_PERSISTENT); | 1254 GDataCache::CACHE_TYPE_PERSISTENT); |
1340 EXPECT_EQ(1, num_callback_invocations_); | 1255 EXPECT_EQ(1, num_callback_invocations_); |
1341 } | 1256 } |
1342 | 1257 |
1343 // Test is disabled because it is flaky (http://crbug.com/134146) | 1258 // Test is disabled because it is flaky (http://crbug.com/134146) |
1344 TEST_F(GDataCacheTest, PinAndUnpinDirtyCache) { | 1259 TEST_F(GDataCacheTest, PinAndUnpinDirtyCache) { |
1345 EXPECT_CALL(*mock_free_disk_space_checker_, AmountOfFreeDiskSpace()) | 1260 EXPECT_CALL(*mock_free_disk_space_checker_, AmountOfFreeDiskSpace()) |
1346 .Times(AtLeast(1)).WillRepeatedly(Return(kLotsOfSpace)); | 1261 .Times(AtLeast(1)).WillRepeatedly(Return(kLotsOfSpace)); |
1347 | 1262 |
1348 std::string resource_id("pdf:1a2b"); | 1263 std::string resource_id("pdf:1a2b"); |
1349 std::string md5("abcdef0123456789"); | 1264 std::string md5("abcdef0123456789"); |
1350 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); | 1265 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); |
1351 EXPECT_CALL(*mock_sync_client_, OnCacheUnpinned(resource_id, md5)).Times(1); | 1266 EXPECT_CALL(*mock_sync_client_, OnCacheUnpinned(resource_id, md5)).Times(1); |
1352 | 1267 |
1353 // First store a file to cache and mark it as dirty. | 1268 // First store a file to cache and mark it as dirty. |
1354 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1269 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1355 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 1270 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1356 GDataCache::CACHE_TYPE_TMP); | 1271 GDataCache::CACHE_TYPE_TMP); |
1357 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1272 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1358 GDataCache::CACHE_STATE_PRESENT | | 1273 CACHE_STATE_PRESENT | |
1359 GDataCache::CACHE_STATE_DIRTY | | 1274 CACHE_STATE_DIRTY | |
1360 GDataCache::CACHE_STATE_PERSISTENT, | 1275 CACHE_STATE_PERSISTENT, |
1361 GDataCache::CACHE_TYPE_PERSISTENT); | 1276 GDataCache::CACHE_TYPE_PERSISTENT); |
1362 | 1277 |
1363 // Verifies dirty file exists. | 1278 // Verifies dirty file exists. |
1364 FilePath dirty_path = GetCacheFilePath( | 1279 FilePath dirty_path = GetCacheFilePath( |
1365 resource_id, | 1280 resource_id, |
1366 md5, | 1281 md5, |
1367 GDataCache::CACHE_TYPE_PERSISTENT, | 1282 GDataCache::CACHE_TYPE_PERSISTENT, |
1368 GDataCache::CACHED_FILE_LOCALLY_MODIFIED); | 1283 GDataCache::CACHED_FILE_LOCALLY_MODIFIED); |
1369 EXPECT_TRUE(file_util::PathExists(dirty_path)); | 1284 EXPECT_TRUE(file_util::PathExists(dirty_path)); |
1370 | 1285 |
1371 // Pin the dirty file. | 1286 // Pin the dirty file. |
1372 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, | 1287 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, |
1373 GDataCache::CACHE_STATE_PRESENT | GDataCache::CACHE_STATE_DIRTY | | 1288 CACHE_STATE_PRESENT | CACHE_STATE_DIRTY | |
1374 GDataCache::CACHE_STATE_PINNED | GDataCache::CACHE_STATE_PERSISTENT, | 1289 CACHE_STATE_PINNED | CACHE_STATE_PERSISTENT, |
1375 GDataCache::CACHE_TYPE_PERSISTENT); | 1290 GDataCache::CACHE_TYPE_PERSISTENT); |
1376 | 1291 |
1377 // Verify dirty file still exist at the same pathname. | 1292 // Verify dirty file still exist at the same pathname. |
1378 EXPECT_TRUE(file_util::PathExists(dirty_path)); | 1293 EXPECT_TRUE(file_util::PathExists(dirty_path)); |
1379 | 1294 |
1380 // Unpin the dirty file. | 1295 // Unpin the dirty file. |
1381 TestUnpin(resource_id, md5, base::PLATFORM_FILE_OK, | 1296 TestUnpin(resource_id, md5, base::PLATFORM_FILE_OK, |
1382 GDataCache::CACHE_STATE_PRESENT | GDataCache::CACHE_STATE_DIRTY | | 1297 CACHE_STATE_PRESENT | CACHE_STATE_DIRTY | |
1383 GDataCache::CACHE_STATE_PERSISTENT, | 1298 CACHE_STATE_PERSISTENT, |
1384 GDataCache::CACHE_TYPE_PERSISTENT); | 1299 GDataCache::CACHE_TYPE_PERSISTENT); |
1385 | 1300 |
1386 // Verify dirty file still exist at the same pathname. | 1301 // Verify dirty file still exist at the same pathname. |
1387 EXPECT_TRUE(file_util::PathExists(dirty_path)); | 1302 EXPECT_TRUE(file_util::PathExists(dirty_path)); |
1388 } | 1303 } |
1389 | 1304 |
1390 TEST_F(GDataCacheTest, DirtyCacheRepetitive) { | 1305 TEST_F(GDataCacheTest, DirtyCacheRepetitive) { |
1391 std::string resource_id("pdf:1a2b"); | 1306 std::string resource_id("pdf:1a2b"); |
1392 std::string md5("abcdef0123456789"); | 1307 std::string md5("abcdef0123456789"); |
1393 EXPECT_CALL(*mock_sync_client_, OnCacheCommitted(resource_id)).Times(3); | 1308 EXPECT_CALL(*mock_sync_client_, OnCacheCommitted(resource_id)).Times(3); |
1394 | 1309 |
1395 // First store a file to cache. | 1310 // First store a file to cache. |
1396 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1311 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1397 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 1312 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1398 GDataCache::CACHE_TYPE_TMP); | 1313 GDataCache::CACHE_TYPE_TMP); |
1399 | 1314 |
1400 // Mark the file dirty. | 1315 // Mark the file dirty. |
1401 num_callback_invocations_ = 0; | 1316 num_callback_invocations_ = 0; |
1402 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1317 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1403 GDataCache::CACHE_STATE_PRESENT | | 1318 CACHE_STATE_PRESENT | |
1404 GDataCache::CACHE_STATE_DIRTY | | 1319 CACHE_STATE_DIRTY | |
1405 GDataCache::CACHE_STATE_PERSISTENT, | 1320 CACHE_STATE_PERSISTENT, |
1406 GDataCache::CACHE_TYPE_PERSISTENT); | 1321 GDataCache::CACHE_TYPE_PERSISTENT); |
1407 EXPECT_EQ(1, num_callback_invocations_); | 1322 EXPECT_EQ(1, num_callback_invocations_); |
1408 | 1323 |
1409 // Again, mark the file dirty. Nothing should change. | 1324 // Again, mark the file dirty. Nothing should change. |
1410 num_callback_invocations_ = 0; | 1325 num_callback_invocations_ = 0; |
1411 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1326 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1412 GDataCache::CACHE_STATE_PRESENT | | 1327 CACHE_STATE_PRESENT | |
1413 GDataCache::CACHE_STATE_DIRTY | | 1328 CACHE_STATE_DIRTY | |
1414 GDataCache::CACHE_STATE_PERSISTENT, | 1329 CACHE_STATE_PERSISTENT, |
1415 GDataCache::CACHE_TYPE_PERSISTENT); | 1330 GDataCache::CACHE_TYPE_PERSISTENT); |
1416 EXPECT_EQ(1, num_callback_invocations_); | 1331 EXPECT_EQ(1, num_callback_invocations_); |
1417 | 1332 |
1418 // Commit the file dirty. Outgoing symlink should be created. | 1333 // Commit the file dirty. Outgoing symlink should be created. |
1419 num_callback_invocations_ = 0; | 1334 num_callback_invocations_ = 0; |
1420 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1335 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1421 GDataCache::CACHE_STATE_PRESENT | | 1336 CACHE_STATE_PRESENT | |
1422 GDataCache::CACHE_STATE_DIRTY | | 1337 CACHE_STATE_DIRTY | |
1423 GDataCache::CACHE_STATE_PERSISTENT, | 1338 CACHE_STATE_PERSISTENT, |
1424 GDataCache::CACHE_TYPE_PERSISTENT); | 1339 GDataCache::CACHE_TYPE_PERSISTENT); |
1425 EXPECT_EQ(1, num_callback_invocations_); | 1340 EXPECT_EQ(1, num_callback_invocations_); |
1426 | 1341 |
1427 // Again, commit the file dirty. Nothing should change. | 1342 // Again, commit the file dirty. Nothing should change. |
1428 num_callback_invocations_ = 0; | 1343 num_callback_invocations_ = 0; |
1429 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1344 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1430 GDataCache::CACHE_STATE_PRESENT | | 1345 CACHE_STATE_PRESENT | |
1431 GDataCache::CACHE_STATE_DIRTY | | 1346 CACHE_STATE_DIRTY | |
1432 GDataCache::CACHE_STATE_PERSISTENT, | 1347 CACHE_STATE_PERSISTENT, |
1433 GDataCache::CACHE_TYPE_PERSISTENT); | 1348 GDataCache::CACHE_TYPE_PERSISTENT); |
1434 EXPECT_EQ(1, num_callback_invocations_); | 1349 EXPECT_EQ(1, num_callback_invocations_); |
1435 | 1350 |
1436 // Mark the file dirty agian after it's being committed. Outgoing symlink | 1351 // Mark the file dirty agian after it's being committed. Outgoing symlink |
1437 // should be deleted. | 1352 // should be deleted. |
1438 num_callback_invocations_ = 0; | 1353 num_callback_invocations_ = 0; |
1439 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1354 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1440 GDataCache::CACHE_STATE_PRESENT | | 1355 CACHE_STATE_PRESENT | |
1441 GDataCache::CACHE_STATE_DIRTY | | 1356 CACHE_STATE_DIRTY | |
1442 GDataCache::CACHE_STATE_PERSISTENT, | 1357 CACHE_STATE_PERSISTENT, |
1443 GDataCache::CACHE_TYPE_PERSISTENT); | 1358 GDataCache::CACHE_TYPE_PERSISTENT); |
1444 EXPECT_EQ(1, num_callback_invocations_); | 1359 EXPECT_EQ(1, num_callback_invocations_); |
1445 | 1360 |
1446 // Commit the file dirty. Outgoing symlink should be created again. | 1361 // Commit the file dirty. Outgoing symlink should be created again. |
1447 num_callback_invocations_ = 0; | 1362 num_callback_invocations_ = 0; |
1448 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1363 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1449 GDataCache::CACHE_STATE_PRESENT | | 1364 CACHE_STATE_PRESENT | |
1450 GDataCache::CACHE_STATE_DIRTY | | 1365 CACHE_STATE_DIRTY | |
1451 GDataCache::CACHE_STATE_PERSISTENT, | 1366 CACHE_STATE_PERSISTENT, |
1452 GDataCache::CACHE_TYPE_PERSISTENT); | 1367 GDataCache::CACHE_TYPE_PERSISTENT); |
1453 EXPECT_EQ(1, num_callback_invocations_); | 1368 EXPECT_EQ(1, num_callback_invocations_); |
1454 | 1369 |
1455 // Clear dirty state of the file. | 1370 // Clear dirty state of the file. |
1456 num_callback_invocations_ = 0; | 1371 num_callback_invocations_ = 0; |
1457 TestClearDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1372 TestClearDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1458 GDataCache::CACHE_STATE_PRESENT, | 1373 CACHE_STATE_PRESENT, |
1459 GDataCache::CACHE_TYPE_TMP); | 1374 GDataCache::CACHE_TYPE_TMP); |
1460 EXPECT_EQ(1, num_callback_invocations_); | 1375 EXPECT_EQ(1, num_callback_invocations_); |
1461 | 1376 |
1462 // Again, clear dirty state of the file, which is no longer dirty. | 1377 // Again, clear dirty state of the file, which is no longer dirty. |
1463 num_callback_invocations_ = 0; | 1378 num_callback_invocations_ = 0; |
1464 TestClearDirty(resource_id, md5, base::PLATFORM_FILE_ERROR_INVALID_OPERATION, | 1379 TestClearDirty(resource_id, md5, base::PLATFORM_FILE_ERROR_INVALID_OPERATION, |
1465 GDataCache::CACHE_STATE_PRESENT, | 1380 CACHE_STATE_PRESENT, |
1466 GDataCache::CACHE_TYPE_TMP); | 1381 GDataCache::CACHE_TYPE_TMP); |
1467 EXPECT_EQ(1, num_callback_invocations_); | 1382 EXPECT_EQ(1, num_callback_invocations_); |
1468 } | 1383 } |
1469 | 1384 |
1470 TEST_F(GDataCacheTest, DirtyCacheInvalid) { | 1385 TEST_F(GDataCacheTest, DirtyCacheInvalid) { |
1471 std::string resource_id("pdf:1a2b"); | 1386 std::string resource_id("pdf:1a2b"); |
1472 std::string md5("abcdef0123456789"); | 1387 std::string md5("abcdef0123456789"); |
1473 | 1388 |
1474 // Mark a non-existent file dirty. | 1389 // Mark a non-existent file dirty. |
1475 num_callback_invocations_ = 0; | 1390 num_callback_invocations_ = 0; |
1476 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_ERROR_NOT_FOUND, | 1391 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_ERROR_NOT_FOUND, |
1477 GDataCache::CACHE_STATE_NONE, | 1392 CACHE_STATE_NONE, |
1478 GDataCache::CACHE_TYPE_TMP); | 1393 GDataCache::CACHE_TYPE_TMP); |
1479 EXPECT_EQ(1, num_callback_invocations_); | 1394 EXPECT_EQ(1, num_callback_invocations_); |
1480 | 1395 |
1481 // Commit a non-existent file dirty. | 1396 // Commit a non-existent file dirty. |
1482 num_callback_invocations_ = 0; | 1397 num_callback_invocations_ = 0; |
1483 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_ERROR_NOT_FOUND, | 1398 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_ERROR_NOT_FOUND, |
1484 GDataCache::CACHE_STATE_NONE, | 1399 CACHE_STATE_NONE, |
1485 GDataCache::CACHE_TYPE_TMP); | 1400 GDataCache::CACHE_TYPE_TMP); |
1486 EXPECT_EQ(1, num_callback_invocations_); | 1401 EXPECT_EQ(1, num_callback_invocations_); |
1487 | 1402 |
1488 // Clear dirty state of a non-existent file. | 1403 // Clear dirty state of a non-existent file. |
1489 num_callback_invocations_ = 0; | 1404 num_callback_invocations_ = 0; |
1490 TestClearDirty(resource_id, md5, base::PLATFORM_FILE_ERROR_NOT_FOUND, | 1405 TestClearDirty(resource_id, md5, base::PLATFORM_FILE_ERROR_NOT_FOUND, |
1491 GDataCache::CACHE_STATE_NONE, | 1406 CACHE_STATE_NONE, |
1492 GDataCache::CACHE_TYPE_TMP); | 1407 GDataCache::CACHE_TYPE_TMP); |
1493 EXPECT_EQ(1, num_callback_invocations_); | 1408 EXPECT_EQ(1, num_callback_invocations_); |
1494 | 1409 |
1495 // Store a file to cache. | 1410 // Store a file to cache. |
1496 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1411 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1497 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 1412 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1498 GDataCache::CACHE_TYPE_TMP); | 1413 GDataCache::CACHE_TYPE_TMP); |
1499 | 1414 |
1500 // Commit a non-dirty existing file dirty. | 1415 // Commit a non-dirty existing file dirty. |
1501 num_callback_invocations_ = 0; | 1416 num_callback_invocations_ = 0; |
1502 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_ERROR_INVALID_OPERATION, | 1417 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_ERROR_INVALID_OPERATION, |
1503 GDataCache::CACHE_STATE_PRESENT, | 1418 CACHE_STATE_PRESENT, |
1504 GDataCache::CACHE_TYPE_TMP); | 1419 GDataCache::CACHE_TYPE_TMP); |
1505 EXPECT_EQ(1, num_callback_invocations_); | 1420 EXPECT_EQ(1, num_callback_invocations_); |
1506 | 1421 |
1507 // Clear dirty state of a non-dirty existing file. | 1422 // Clear dirty state of a non-dirty existing file. |
1508 num_callback_invocations_ = 0; | 1423 num_callback_invocations_ = 0; |
1509 TestClearDirty(resource_id, md5, base::PLATFORM_FILE_ERROR_INVALID_OPERATION, | 1424 TestClearDirty(resource_id, md5, base::PLATFORM_FILE_ERROR_INVALID_OPERATION, |
1510 GDataCache::CACHE_STATE_PRESENT, | 1425 CACHE_STATE_PRESENT, |
1511 GDataCache::CACHE_TYPE_TMP); | 1426 GDataCache::CACHE_TYPE_TMP); |
1512 EXPECT_EQ(1, num_callback_invocations_); | 1427 EXPECT_EQ(1, num_callback_invocations_); |
1513 | 1428 |
1514 // Mark an existing file dirty, then store a new file to the same resource id | 1429 // Mark an existing file dirty, then store a new file to the same resource id |
1515 // but different md5, which should fail. | 1430 // but different md5, which should fail. |
1516 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1431 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1517 GDataCache::CACHE_STATE_PRESENT | | 1432 CACHE_STATE_PRESENT | |
1518 GDataCache::CACHE_STATE_DIRTY | | 1433 CACHE_STATE_DIRTY | |
1519 GDataCache::CACHE_STATE_PERSISTENT, | 1434 CACHE_STATE_PERSISTENT, |
1520 GDataCache::CACHE_TYPE_PERSISTENT); | 1435 GDataCache::CACHE_TYPE_PERSISTENT); |
1521 num_callback_invocations_ = 0; | 1436 num_callback_invocations_ = 0; |
1522 md5 = "new_md5"; | 1437 md5 = "new_md5"; |
1523 TestStoreToCache(resource_id, md5, GetTestFilePath("subdir_feed.json"), | 1438 TestStoreToCache(resource_id, md5, GetTestFilePath("subdir_feed.json"), |
1524 base::PLATFORM_FILE_ERROR_IN_USE, | 1439 base::PLATFORM_FILE_ERROR_IN_USE, |
1525 GDataCache::CACHE_STATE_PRESENT | | 1440 CACHE_STATE_PRESENT | |
1526 GDataCache::CACHE_STATE_DIRTY | | 1441 CACHE_STATE_DIRTY | |
1527 GDataCache::CACHE_STATE_PERSISTENT, | 1442 CACHE_STATE_PERSISTENT, |
1528 GDataCache::CACHE_TYPE_PERSISTENT); | 1443 GDataCache::CACHE_TYPE_PERSISTENT); |
1529 EXPECT_EQ(1, num_callback_invocations_); | 1444 EXPECT_EQ(1, num_callback_invocations_); |
1530 } | 1445 } |
1531 | 1446 |
1532 TEST_F(GDataCacheTest, RemoveFromDirtyCache) { | 1447 TEST_F(GDataCacheTest, RemoveFromDirtyCache) { |
1533 std::string resource_id("pdf:1a2b"); | 1448 std::string resource_id("pdf:1a2b"); |
1534 std::string md5("abcdef0123456789"); | 1449 std::string md5("abcdef0123456789"); |
1535 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); | 1450 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); |
1536 EXPECT_CALL(*mock_sync_client_, OnCacheCommitted(resource_id)).Times(1); | 1451 EXPECT_CALL(*mock_sync_client_, OnCacheCommitted(resource_id)).Times(1); |
1537 | 1452 |
1538 // Store a file to cache, pin it, mark it dirty and commit it. | 1453 // Store a file to cache, pin it, mark it dirty and commit it. |
1539 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1454 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1540 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 1455 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1541 GDataCache::CACHE_TYPE_TMP); | 1456 GDataCache::CACHE_TYPE_TMP); |
1542 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, | 1457 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, |
1543 GDataCache::CACHE_STATE_PRESENT | GDataCache::CACHE_STATE_PINNED | | 1458 CACHE_STATE_PRESENT | CACHE_STATE_PINNED | |
1544 GDataCache::CACHE_STATE_PERSISTENT, | 1459 CACHE_STATE_PERSISTENT, |
1545 GDataCache::CACHE_TYPE_PERSISTENT); | 1460 GDataCache::CACHE_TYPE_PERSISTENT); |
1546 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1461 TestMarkDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1547 GDataCache::CACHE_STATE_PRESENT | | 1462 CACHE_STATE_PRESENT | |
1548 GDataCache::CACHE_STATE_PINNED | | 1463 CACHE_STATE_PINNED | |
1549 GDataCache::CACHE_STATE_DIRTY | | 1464 CACHE_STATE_DIRTY | |
1550 GDataCache::CACHE_STATE_PERSISTENT, | 1465 CACHE_STATE_PERSISTENT, |
1551 GDataCache::CACHE_TYPE_PERSISTENT); | 1466 GDataCache::CACHE_TYPE_PERSISTENT); |
1552 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_OK, | 1467 TestCommitDirty(resource_id, md5, base::PLATFORM_FILE_OK, |
1553 GDataCache::CACHE_STATE_PRESENT | | 1468 CACHE_STATE_PRESENT | |
1554 GDataCache::CACHE_STATE_PINNED | | 1469 CACHE_STATE_PINNED | |
1555 GDataCache::CACHE_STATE_DIRTY | | 1470 CACHE_STATE_DIRTY | |
1556 GDataCache::CACHE_STATE_PERSISTENT, | 1471 CACHE_STATE_PERSISTENT, |
1557 GDataCache::CACHE_TYPE_PERSISTENT); | 1472 GDataCache::CACHE_TYPE_PERSISTENT); |
1558 | 1473 |
1559 // Try to remove the file. Since file is dirty, it and the corresponding | 1474 // Try to remove the file. Since file is dirty, it and the corresponding |
1560 // pinned and outgoing symlinks should not be removed. | 1475 // pinned and outgoing symlinks should not be removed. |
1561 num_callback_invocations_ = 0; | 1476 num_callback_invocations_ = 0; |
1562 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); | 1477 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); |
1563 EXPECT_EQ(1, num_callback_invocations_); | 1478 EXPECT_EQ(1, num_callback_invocations_); |
1564 } | 1479 } |
1565 | 1480 |
1566 TEST_F(GDataCacheTest, GetCacheState) { | 1481 TEST_F(GDataCacheTest, GetCacheState) { |
1567 // Populate gdata file system. | 1482 // Populate gdata file system. |
1568 LoadRootFeedDocument("root_feed.json"); | 1483 LoadRootFeedDocument("root_feed.json"); |
1569 | 1484 |
1570 { // Test cache state of an existing normal file. | 1485 { // Test cache state of an existing normal file. |
1571 // Retrieve resource id and md5 of a file from file system. | 1486 // Retrieve resource id and md5 of a file from file system. |
1572 FilePath file_path(FILE_PATH_LITERAL("drive/File 1.txt")); | 1487 FilePath file_path(FILE_PATH_LITERAL("drive/File 1.txt")); |
1573 GDataEntry* entry = FindEntry(file_path); | 1488 GDataEntry* entry = FindEntry(file_path); |
1574 ASSERT_TRUE(entry != NULL); | 1489 ASSERT_TRUE(entry != NULL); |
1575 GDataFile* file = entry->AsGDataFile(); | 1490 GDataFile* file = entry->AsGDataFile(); |
1576 ASSERT_TRUE(file != NULL); | 1491 ASSERT_TRUE(file != NULL); |
1577 std::string resource_id = file->resource_id(); | 1492 std::string resource_id = file->resource_id(); |
1578 std::string md5 = file->file_md5(); | 1493 std::string md5 = file->file_md5(); |
1579 | 1494 |
1580 // Store a file corresponding to |resource_id| and |md5| to cache. | 1495 // Store a file corresponding to |resource_id| and |md5| to cache. |
1581 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1496 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1582 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 1497 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1583 GDataCache::CACHE_TYPE_TMP); | 1498 GDataCache::CACHE_TYPE_TMP); |
1584 | 1499 |
1585 // Get its cache state. | 1500 // Get its cache state. |
1586 num_callback_invocations_ = 0; | 1501 num_callback_invocations_ = 0; |
1587 TestGetCacheState(resource_id, md5, true, | 1502 TestGetCacheState(resource_id, md5, true, |
1588 GDataCache::CACHE_STATE_PRESENT, file); | 1503 CACHE_STATE_PRESENT, file); |
1589 EXPECT_EQ(1, num_callback_invocations_); | 1504 EXPECT_EQ(1, num_callback_invocations_); |
1590 } | 1505 } |
1591 | 1506 |
1592 { // Test cache state of an existing pinned file. | 1507 { // Test cache state of an existing pinned file. |
1593 // Retrieve resource id and md5 of a file from file system. | 1508 // Retrieve resource id and md5 of a file from file system. |
1594 FilePath file_path( | 1509 FilePath file_path( |
1595 FILE_PATH_LITERAL("drive/Directory 1/SubDirectory File 1.txt")); | 1510 FILE_PATH_LITERAL("drive/Directory 1/SubDirectory File 1.txt")); |
1596 GDataEntry* entry = FindEntry(file_path); | 1511 GDataEntry* entry = FindEntry(file_path); |
1597 ASSERT_TRUE(entry != NULL); | 1512 ASSERT_TRUE(entry != NULL); |
1598 GDataFile* file = entry->AsGDataFile(); | 1513 GDataFile* file = entry->AsGDataFile(); |
1599 ASSERT_TRUE(file != NULL); | 1514 ASSERT_TRUE(file != NULL); |
1600 std::string resource_id = file->resource_id(); | 1515 std::string resource_id = file->resource_id(); |
1601 std::string md5 = file->file_md5(); | 1516 std::string md5 = file->file_md5(); |
1602 | 1517 |
1603 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); | 1518 EXPECT_CALL(*mock_sync_client_, OnCachePinned(resource_id, md5)).Times(1); |
1604 | 1519 |
1605 // Store a file corresponding to |resource_id| and |md5| to cache, and pin | 1520 // Store a file corresponding to |resource_id| and |md5| to cache, and pin |
1606 // it. | 1521 // it. |
1607 int expected_cache_state = (GDataCache::CACHE_STATE_PRESENT | | 1522 int expected_cache_state = (CACHE_STATE_PRESENT | |
1608 GDataCache::CACHE_STATE_PINNED | | 1523 CACHE_STATE_PINNED | |
1609 GDataCache::CACHE_STATE_PERSISTENT); | 1524 CACHE_STATE_PERSISTENT); |
1610 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1525 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1611 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 1526 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1612 GDataCache::CACHE_TYPE_TMP); | 1527 GDataCache::CACHE_TYPE_TMP); |
1613 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, expected_cache_state, | 1528 TestPin(resource_id, md5, base::PLATFORM_FILE_OK, expected_cache_state, |
1614 GDataCache::CACHE_TYPE_PERSISTENT); | 1529 GDataCache::CACHE_TYPE_PERSISTENT); |
1615 | 1530 |
1616 // Get its cache state. | 1531 // Get its cache state. |
1617 num_callback_invocations_ = 0; | 1532 num_callback_invocations_ = 0; |
1618 TestGetCacheState(resource_id, md5, true, | 1533 TestGetCacheState(resource_id, md5, true, |
1619 expected_cache_state, file); | 1534 expected_cache_state, file); |
1620 EXPECT_EQ(1, num_callback_invocations_); | 1535 EXPECT_EQ(1, num_callback_invocations_); |
1621 } | 1536 } |
1622 | 1537 |
1623 { // Test cache state of a non-existent file. | 1538 { // Test cache state of a non-existent file. |
1624 num_callback_invocations_ = 0; | 1539 num_callback_invocations_ = 0; |
1625 TestGetCacheState("pdf:12345", "abcd", false, | 1540 TestGetCacheState("pdf:12345", "abcd", false, |
1626 0, NULL); | 1541 0, NULL); |
1627 EXPECT_EQ(1, num_callback_invocations_); | 1542 EXPECT_EQ(1, num_callback_invocations_); |
1628 } | 1543 } |
1629 } | 1544 } |
1630 | 1545 |
1631 TEST_F(GDataCacheTest, MountUnmount) { | 1546 TEST_F(GDataCacheTest, MountUnmount) { |
1632 FilePath file_path; | 1547 FilePath file_path; |
1633 std::string resource_id("pdf:1a2b"); | 1548 std::string resource_id("pdf:1a2b"); |
1634 std::string md5("abcdef0123456789"); | 1549 std::string md5("abcdef0123456789"); |
1635 | 1550 |
1636 // First store a file to cache in the tmp subdir. | 1551 // First store a file to cache in the tmp subdir. |
1637 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), | 1552 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"), |
1638 base::PLATFORM_FILE_OK, GDataCache::CACHE_STATE_PRESENT, | 1553 base::PLATFORM_FILE_OK, CACHE_STATE_PRESENT, |
1639 GDataCache::CACHE_TYPE_TMP); | 1554 GDataCache::CACHE_TYPE_TMP); |
1640 | 1555 |
1641 // Mark the file mounted. | 1556 // Mark the file mounted. |
1642 num_callback_invocations_ = 0; | 1557 num_callback_invocations_ = 0; |
1643 file_path = cache_->GetCacheFilePath(resource_id, md5, | 1558 file_path = cache_->GetCacheFilePath(resource_id, md5, |
1644 GDataCache::CACHE_TYPE_TMP, | 1559 GDataCache::CACHE_TYPE_TMP, |
1645 GDataCache::CACHED_FILE_FROM_SERVER); | 1560 GDataCache::CACHED_FILE_FROM_SERVER); |
1646 TestSetMountedState(resource_id, md5, file_path, true, | 1561 TestSetMountedState(resource_id, md5, file_path, true, |
1647 base::PLATFORM_FILE_OK, | 1562 base::PLATFORM_FILE_OK, |
1648 GDataCache::CACHE_STATE_PRESENT | | 1563 CACHE_STATE_PRESENT | |
1649 GDataCache::CACHE_STATE_MOUNTED | | 1564 CACHE_STATE_MOUNTED | |
1650 GDataCache::CACHE_STATE_PERSISTENT, | 1565 CACHE_STATE_PERSISTENT, |
1651 GDataCache::CACHE_TYPE_PERSISTENT); | 1566 GDataCache::CACHE_TYPE_PERSISTENT); |
1652 EXPECT_EQ(1, num_callback_invocations_); | 1567 EXPECT_EQ(1, num_callback_invocations_); |
1653 EXPECT_TRUE(CacheEntryExists(resource_id, md5)); | 1568 EXPECT_TRUE(CacheEntryExists(resource_id, md5)); |
1654 | 1569 |
1655 // Clear mounted state of the file. | 1570 // Clear mounted state of the file. |
1656 num_callback_invocations_ = 0; | 1571 num_callback_invocations_ = 0; |
1657 file_path = cache_->GetCacheFilePath(resource_id, | 1572 file_path = cache_->GetCacheFilePath(resource_id, |
1658 md5, | 1573 md5, |
1659 GDataCache::CACHE_TYPE_PERSISTENT, | 1574 GDataCache::CACHE_TYPE_PERSISTENT, |
1660 GDataCache::CACHED_FILE_MOUNTED); | 1575 GDataCache::CACHED_FILE_MOUNTED); |
1661 TestSetMountedState(resource_id, md5, file_path, false, | 1576 TestSetMountedState(resource_id, md5, file_path, false, |
1662 base::PLATFORM_FILE_OK, | 1577 base::PLATFORM_FILE_OK, |
1663 GDataCache::CACHE_STATE_PRESENT, | 1578 CACHE_STATE_PRESENT, |
1664 GDataCache::CACHE_TYPE_TMP); | 1579 GDataCache::CACHE_TYPE_TMP); |
1665 EXPECT_EQ(1, num_callback_invocations_); | 1580 EXPECT_EQ(1, num_callback_invocations_); |
1666 EXPECT_TRUE(CacheEntryExists(resource_id, md5)); | 1581 EXPECT_TRUE(CacheEntryExists(resource_id, md5)); |
1667 | 1582 |
1668 // Try to remove the file. | 1583 // Try to remove the file. |
1669 num_callback_invocations_ = 0; | 1584 num_callback_invocations_ = 0; |
1670 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); | 1585 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK); |
1671 EXPECT_EQ(1, num_callback_invocations_); | 1586 EXPECT_EQ(1, num_callback_invocations_); |
1672 } | 1587 } |
1673 | 1588 |
1674 } // namespace gdata | 1589 } // namespace gdata |
OLD | NEW |