[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame] | 5 | #include "chrome/browser/chromeos/drive/sync_client.h" |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 6 | |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 7 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 9 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 11 | #include "base/run_loop.h" |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 12 | #include "base/test/test_timeouts.h" |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 13 | #include "chrome/browser/chromeos/drive/change_list_loader.h" |
[email protected] | 15de814 | 2012-10-11 06:00:54 | [diff] [blame] | 14 | #include "chrome/browser/chromeos/drive/drive.pb.h" |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 15 | #include "chrome/browser/chromeos/drive/file_cache.h" |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 16 | #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" |
| 17 | #include "chrome/browser/chromeos/drive/job_scheduler.h" |
| 18 | #include "chrome/browser/chromeos/drive/resource_entry_conversion.h" |
| 19 | #include "chrome/browser/chromeos/drive/resource_metadata.h" |
[email protected] | 92931d7 | 2013-04-24 05:16:15 | [diff] [blame] | 20 | #include "chrome/browser/chromeos/drive/test_util.h" |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 21 | #include "chrome/browser/google_apis/fake_drive_service.h" |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 22 | #include "chrome/browser/google_apis/test_util.h" |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 23 | #include "chrome/test/base/testing_profile.h" |
| 24 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 25 | #include "testing/gtest/include/gtest/gtest.h" |
| 26 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 27 | namespace drive { |
[email protected] | 4e87c486 | 2013-05-20 04:06:32 | [diff] [blame] | 28 | namespace internal { |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 29 | |
[email protected] | 8019936 | 2012-09-09 23:24:29 | [diff] [blame] | 30 | namespace { |
| 31 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 32 | // The content of files iniitally stored in the cache. |
| 33 | const char kLocalContent[] = "Hello!"; |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 34 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 35 | // The content of files stored in the service. |
| 36 | const char kRemoteContent[] = "World!"; |
| 37 | |
| 38 | // SyncClientTestDriveService will return GDATA_CANCELLED when a request is |
| 39 | // made with the specified resource ID. |
| 40 | class SyncClientTestDriveService : public google_apis::FakeDriveService { |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 41 | public: |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 42 | // FakeDriveService override: |
| 43 | virtual void GetResourceEntry( |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 44 | const std::string& resource_id, |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 45 | const google_apis::GetResourceEntryCallback& callback) OVERRIDE { |
| 46 | if (resource_id == resource_id_to_be_cancelled_) { |
| 47 | scoped_ptr<google_apis::ResourceEntry> null; |
| 48 | base::MessageLoopProxy::current()->PostTask( |
| 49 | FROM_HERE, |
| 50 | base::Bind(callback, |
| 51 | google_apis::GDATA_CANCELLED, |
| 52 | base::Passed(&null))); |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 53 | return; |
| 54 | } |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 55 | FakeDriveService::GetResourceEntry(resource_id, callback); |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 56 | } |
[email protected] | aaa70a4 | 2012-06-05 22:22:51 | [diff] [blame] | 57 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 58 | void set_resource_id_to_be_cancelled(const std::string& resource_id) { |
| 59 | resource_id_to_be_cancelled_ = resource_id; |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 60 | } |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 61 | |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 62 | private: |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 63 | std::string resource_id_to_be_cancelled_; |
| 64 | }; |
| 65 | |
| 66 | class DummyOperationObserver : public file_system::OperationObserver { |
| 67 | virtual void OnDirectoryChangedByOperation( |
| 68 | const base::FilePath& path) OVERRIDE {} |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 69 | }; |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 70 | |
[email protected] | 8019936 | 2012-09-09 23:24:29 | [diff] [blame] | 71 | } // namespace |
| 72 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame] | 73 | class SyncClientTest : public testing::Test { |
[email protected] | ffd60e43 | 2012-03-24 20:36:00 | [diff] [blame] | 74 | public: |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 75 | virtual void SetUp() OVERRIDE { |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 76 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 77 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 78 | profile_.reset(new TestingProfile); |
| 79 | |
| 80 | drive_service_.reset(new SyncClientTestDriveService); |
| 81 | drive_service_->LoadResourceListForWapi("chromeos/gdata/empty_feed.json"); |
| 82 | drive_service_->LoadAccountMetadataForWapi( |
| 83 | "chromeos/gdata/account_metadata.json"); |
| 84 | |
| 85 | scheduler_.reset(new JobScheduler(profile_.get(), drive_service_.get())); |
| 86 | metadata_.reset(new internal::ResourceMetadata( |
| 87 | temp_dir_.path(), base::MessageLoopProxy::current())); |
| 88 | FileError error = FILE_ERROR_FAILED; |
| 89 | metadata_->Initialize( |
| 90 | google_apis::test_util::CreateCopyResultCallback(&error)); |
| 91 | base::RunLoop().RunUntilIdle(); |
| 92 | ASSERT_EQ(FILE_ERROR_OK, error); |
| 93 | |
| 94 | cache_.reset(new FileCache(temp_dir_.path(), |
| 95 | base::MessageLoopProxy::current(), |
| 96 | NULL /* free_disk_space_getter */)); |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 97 | bool success = false; |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 98 | cache_->RequestInitialize( |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 99 | google_apis::test_util::CreateCopyResultCallback(&success)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 100 | base::RunLoop().RunUntilIdle(); |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 101 | ASSERT_TRUE(success); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 102 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 103 | ASSERT_NO_FATAL_FAILURE(SetUpTestData()); |
| 104 | |
| 105 | sync_client_.reset(new SyncClient(base::MessageLoopProxy::current(), |
| 106 | &observer_, |
| 107 | scheduler_.get(), |
| 108 | metadata_.get(), |
| 109 | cache_.get())); |
[email protected] | e0529fc | 2012-06-12 11:07:58 | [diff] [blame] | 110 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 111 | // Disable delaying so that DoSyncLoop() starts immediately. |
| 112 | sync_client_->set_delay_for_testing(base::TimeDelta::FromSeconds(0)); |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | a538177 | 2012-04-05 02:20:04 | [diff] [blame] | 115 | virtual void TearDown() OVERRIDE { |
[email protected] | 2416a2e | 2012-05-07 20:58:49 | [diff] [blame] | 116 | sync_client_.reset(); |
[email protected] | c0a3b8e8 | 2013-03-18 21:07:45 | [diff] [blame] | 117 | cache_.reset(); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 118 | metadata_.reset(); |
[email protected] | a538177 | 2012-04-05 02:20:04 | [diff] [blame] | 119 | } |
| 120 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 121 | // Adds a file to the service root and |resource_ids_|. |
| 122 | void AddFileEntry(const std::string& title) { |
| 123 | google_apis::GDataErrorCode error = google_apis::GDATA_FILE_ERROR; |
| 124 | scoped_ptr<google_apis::ResourceEntry> entry; |
| 125 | drive_service_->AddNewFile( |
| 126 | "text/plain", |
| 127 | kRemoteContent, |
| 128 | drive_service_->GetRootResourceId(), |
| 129 | title, |
| 130 | false, // shared_with_me |
| 131 | google_apis::test_util::CreateCopyResultCallback(&error, &entry)); |
| 132 | base::RunLoop().RunUntilIdle(); |
| 133 | ASSERT_EQ(google_apis::HTTP_CREATED, error); |
| 134 | ASSERT_TRUE(entry); |
| 135 | resource_ids_[title] = entry->resource_id(); |
| 136 | } |
| 137 | |
| 138 | // Sets up data for tests. |
| 139 | void SetUpTestData() { |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 140 | // Prepare a temp file. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 141 | base::FilePath temp_file; |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 142 | EXPECT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), |
| 143 | &temp_file)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 144 | ASSERT_TRUE(google_apis::test_util::WriteStringToFile(temp_file, |
| 145 | kLocalContent)); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 146 | |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 147 | // Prepare 3 pinned-but-not-present files. |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 148 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("foo")); |
| 149 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(resource_ids_["foo"], std::string())); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 150 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 151 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("bar")); |
| 152 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(resource_ids_["bar"], std::string())); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 153 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 154 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("baz")); |
| 155 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(resource_ids_["baz"], std::string())); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 156 | |
| 157 | // Prepare a pinned-and-fetched file. |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 158 | const std::string md5_fetched = "md5"; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 159 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("fetched")); |
| 160 | EXPECT_EQ(FILE_ERROR_OK, |
| 161 | cache_->Store(resource_ids_["fetched"], md5_fetched, |
| 162 | temp_file, FileCache::FILE_OPERATION_COPY)); |
| 163 | EXPECT_EQ(FILE_ERROR_OK, |
| 164 | cache_->Pin(resource_ids_["fetched"], md5_fetched)); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 165 | |
| 166 | // Prepare a pinned-and-fetched-and-dirty file. |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 167 | const std::string md5_dirty = ""; // Don't care. |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 168 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("dirty")); |
| 169 | EXPECT_EQ(FILE_ERROR_OK, |
| 170 | cache_->Store(resource_ids_["dirty"], md5_dirty, |
| 171 | temp_file, FileCache::FILE_OPERATION_COPY)); |
| 172 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(resource_ids_["dirty"], md5_dirty)); |
| 173 | EXPECT_EQ(FILE_ERROR_OK, |
| 174 | cache_->MarkDirty(resource_ids_["dirty"], md5_dirty)); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 175 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 176 | // Load data from the service to the metadata. |
| 177 | FileError error = FILE_ERROR_FAILED; |
| 178 | internal::ChangeListLoader change_list_loader( |
| 179 | base::MessageLoopProxy::current(), metadata_.get(), scheduler_.get()); |
| 180 | change_list_loader.LoadIfNeeded( |
| 181 | DirectoryFetchInfo(), |
| 182 | google_apis::test_util::CreateCopyResultCallback(&error)); |
| 183 | base::RunLoop().RunUntilIdle(); |
| 184 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 185 | } |
| 186 | |
[email protected] | ffd60e43 | 2012-03-24 20:36:00 | [diff] [blame] | 187 | protected: |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 188 | content::TestBrowserThreadBundle thread_bundle_; |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 189 | base::ScopedTempDir temp_dir_; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 190 | scoped_ptr<TestingProfile> profile_; |
| 191 | scoped_ptr<SyncClientTestDriveService> drive_service_; |
| 192 | DummyOperationObserver observer_; |
| 193 | scoped_ptr<JobScheduler> scheduler_; |
| 194 | scoped_ptr<internal::ResourceMetadata, test_util::DestroyHelperForTests> |
| 195 | metadata_; |
[email protected] | 4e87c486 | 2013-05-20 04:06:32 | [diff] [blame] | 196 | scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame] | 197 | scoped_ptr<SyncClient> sync_client_; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 198 | |
| 199 | std::map<std::string, std::string> resource_ids_; // Name-to-id map. |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 200 | }; |
| 201 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 202 | TEST_F(SyncClientTest, StartProcessingBacklog) { |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 203 | sync_client_->StartProcessingBacklog(); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 204 | base::RunLoop().RunUntilIdle(); |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 205 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 206 | FileCacheEntry cache_entry; |
| 207 | // Pinned files get downloaded. |
| 208 | EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["foo"], std::string(), |
| 209 | &cache_entry)); |
| 210 | EXPECT_TRUE(cache_entry.is_present()); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 211 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 212 | EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["bar"], std::string(), |
| 213 | &cache_entry)); |
| 214 | EXPECT_TRUE(cache_entry.is_present()); |
| 215 | |
| 216 | EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["baz"], std::string(), |
| 217 | &cache_entry)); |
| 218 | EXPECT_TRUE(cache_entry.is_present()); |
| 219 | |
| 220 | // Dirty file gets uploaded. |
| 221 | EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["dirty"], std::string(), |
| 222 | &cache_entry)); |
| 223 | EXPECT_FALSE(cache_entry.is_dirty()); |
[email protected] | a538177 | 2012-04-05 02:20:04 | [diff] [blame] | 224 | } |
| 225 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame] | 226 | TEST_F(SyncClientTest, OnCachePinned) { |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 227 | // This file will be fetched by GetFileByResourceId() as OnCachePinned() |
[email protected] | b7c5da7 | 2012-06-26 05:27:46 | [diff] [blame] | 228 | // will kick off the sync loop. |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 229 | sync_client_->OnCachePinned(resource_ids_["foo"], std::string()); |
| 230 | base::RunLoop().RunUntilIdle(); |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 231 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 232 | FileCacheEntry cache_entry; |
| 233 | EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["foo"], std::string(), |
| 234 | &cache_entry)); |
| 235 | EXPECT_TRUE(cache_entry.is_present()); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 236 | } |
| 237 | |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 238 | TEST_F(SyncClientTest, OnCachePinnedAndCancelled) { |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 239 | drive_service_->set_resource_id_to_be_cancelled(resource_ids_["foo"]); |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 240 | // Trigger fetching of a file which results in cancellation. |
| 241 | FileError error = FILE_ERROR_FAILED; |
| 242 | cache_->PinOnUIThread( |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 243 | resource_ids_["foo"], std::string(), |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 244 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 245 | base::RunLoop().RunUntilIdle(); |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 246 | EXPECT_EQ(FILE_ERROR_OK, error); |
| 247 | |
| 248 | // The file should be unpinned if the user wants the download to be cancelled. |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 249 | FileCacheEntry cache_entry; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 250 | EXPECT_FALSE(cache_->GetCacheEntry(resource_ids_["foo"], std::string(), |
| 251 | &cache_entry)); |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 252 | } |
| 253 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame] | 254 | TEST_F(SyncClientTest, OnCacheUnpinned) { |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 255 | sync_client_->AddResourceIdForTesting(SyncClient::FETCH, |
| 256 | resource_ids_["foo"]); |
| 257 | sync_client_->AddResourceIdForTesting(SyncClient::FETCH, |
| 258 | resource_ids_["bar"]); |
| 259 | sync_client_->AddResourceIdForTesting(SyncClient::FETCH, |
| 260 | resource_ids_["baz"]); |
| 261 | ASSERT_EQ(3U, |
| 262 | sync_client_->GetResourceIdsForTesting(SyncClient::FETCH).size()); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 263 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 264 | sync_client_->OnCacheUnpinned(resource_ids_["foo"], std::string()); |
| 265 | sync_client_->OnCacheUnpinned(resource_ids_["baz"], std::string()); |
| 266 | base::RunLoop().RunUntilIdle(); |
[email protected] | e07bbd97 | 2013-01-23 17:38:42 | [diff] [blame] | 267 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 268 | // Only "bar" should be fetched. |
| 269 | FileCacheEntry cache_entry; |
| 270 | EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["foo"], std::string(), |
| 271 | &cache_entry)); |
| 272 | EXPECT_FALSE(cache_entry.is_present()); |
| 273 | |
| 274 | EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["bar"], std::string(), |
| 275 | &cache_entry)); |
| 276 | EXPECT_TRUE(cache_entry.is_present()); |
| 277 | |
| 278 | EXPECT_TRUE(cache_->GetCacheEntry(resource_ids_["baz"], std::string(), |
| 279 | &cache_entry)); |
| 280 | EXPECT_FALSE(cache_entry.is_present()); |
| 281 | |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 282 | } |
| 283 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame] | 284 | TEST_F(SyncClientTest, Deduplication) { |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 285 | sync_client_->AddResourceIdForTesting(SyncClient::FETCH, |
| 286 | resource_ids_["foo"]); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 287 | |
| 288 | // Set the delay so that DoSyncLoop() is delayed. |
| 289 | sync_client_->set_delay_for_testing(TestTimeouts::action_max_timeout()); |
| 290 | // Raise OnCachePinned() event. This shouldn't result in adding the second |
| 291 | // task, as tasks are de-duplicated. |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 292 | sync_client_->OnCachePinned(resource_ids_["foo"], std::string()); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 293 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 294 | ASSERT_EQ(1U, |
| 295 | sync_client_->GetResourceIdsForTesting(SyncClient::FETCH).size()); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 296 | } |
| 297 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame] | 298 | TEST_F(SyncClientTest, ExistingPinnedFiles) { |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 299 | // Start checking the existing pinned files. This will collect the resource |
| 300 | // IDs of pinned files, with stale local cache files. |
| 301 | sync_client_->StartCheckingExistingPinnedFiles(); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 302 | base::RunLoop().RunUntilIdle(); |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 303 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame^] | 304 | // "fetched" and "dirty" are the existing pinned files. |
| 305 | // The non-dirty one should be synced, but the dirty one should not. |
| 306 | base::FilePath cache_file; |
| 307 | std::string content; |
| 308 | EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(resource_ids_["fetched"], |
| 309 | std::string(), &cache_file)); |
| 310 | EXPECT_TRUE(file_util::ReadFileToString(cache_file, &content)); |
| 311 | EXPECT_EQ(kRemoteContent, content); |
| 312 | content.clear(); |
| 313 | |
| 314 | EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(resource_ids_["dirty"], |
| 315 | std::string(), &cache_file)); |
| 316 | EXPECT_TRUE(file_util::ReadFileToString(cache_file, &content)); |
| 317 | EXPECT_EQ(kLocalContent, content); |
[email protected] | 189541ba | 2012-10-24 11:18:15 | [diff] [blame] | 318 | } |
| 319 | |
[email protected] | 4e87c486 | 2013-05-20 04:06:32 | [diff] [blame] | 320 | } // namespace internal |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 321 | } // namespace drive |