[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] | db20d21 | 2013-07-08 07:18:16 | [diff] [blame] | 11 | #include "base/prefs/testing_pref_service.h" |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 12 | #include "base/run_loop.h" |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 13 | #include "base/test/test_timeouts.h" |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 14 | #include "chrome/browser/chromeos/drive/change_list_loader.h" |
[email protected] | 5d13ab6 | 2013-08-22 07:45:14 | [diff] [blame] | 15 | #include "chrome/browser/chromeos/drive/change_list_processor.h" |
[email protected] | 15de814 | 2012-10-11 06:00:54 | [diff] [blame] | 16 | #include "chrome/browser/chromeos/drive/drive.pb.h" |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 17 | #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h" |
[email protected] | eca3fc9 | 2013-05-01 03:53:40 | [diff] [blame] | 18 | #include "chrome/browser/chromeos/drive/file_cache.h" |
[email protected] | 5170a13 | 2013-12-02 10:35:13 | [diff] [blame] | 19 | #include "chrome/browser/chromeos/drive/file_system/move_operation.h" |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 20 | #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" |
[email protected] | 562af1c1 | 2013-11-26 10:27:53 | [diff] [blame] | 21 | #include "chrome/browser/chromeos/drive/file_system/remove_operation.h" |
[email protected] | 5170a13 | 2013-12-02 10:35:13 | [diff] [blame] | 22 | #include "chrome/browser/chromeos/drive/file_system_util.h" |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 23 | #include "chrome/browser/chromeos/drive/job_scheduler.h" |
| 24 | #include "chrome/browser/chromeos/drive/resource_entry_conversion.h" |
| 25 | #include "chrome/browser/chromeos/drive/resource_metadata.h" |
[email protected] | 92931d7 | 2013-04-24 05:16:15 | [diff] [blame] | 26 | #include "chrome/browser/chromeos/drive/test_util.h" |
[email protected] | 0390b81 | 2013-06-19 00:27:50 | [diff] [blame] | 27 | #include "chrome/browser/drive/fake_drive_service.h" |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 28 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | e196bef3 | 2013-12-03 05:33:13 | [diff] [blame] | 29 | #include "google_apis/drive/test_util.h" |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 30 | #include "testing/gtest/include/gtest/gtest.h" |
| 31 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 32 | namespace drive { |
[email protected] | 4e87c486 | 2013-05-20 04:06:32 | [diff] [blame] | 33 | namespace internal { |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 34 | |
[email protected] | 8019936 | 2012-09-09 23:24:29 | [diff] [blame] | 35 | namespace { |
| 36 | |
[email protected] | 7a686016 | 2013-07-03 12:31:46 | [diff] [blame] | 37 | // The content of files initially stored in the cache. |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 38 | const char kLocalContent[] = "Hello!"; |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 39 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 40 | // The content of files stored in the service. |
| 41 | const char kRemoteContent[] = "World!"; |
| 42 | |
| 43 | // SyncClientTestDriveService will return GDATA_CANCELLED when a request is |
| 44 | // made with the specified resource ID. |
[email protected] | e50af765 | 2013-06-20 06:39:31 | [diff] [blame] | 45 | class SyncClientTestDriveService : public ::drive::FakeDriveService { |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 46 | public: |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame^] | 47 | SyncClientTestDriveService() : download_file_count_(0) {} |
| 48 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 49 | // FakeDriveService override: |
[email protected] | 7a686016 | 2013-07-03 12:31:46 | [diff] [blame] | 50 | virtual google_apis::CancelCallback DownloadFile( |
| 51 | const base::FilePath& local_cache_path, |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 52 | const std::string& resource_id, |
[email protected] | 7a686016 | 2013-07-03 12:31:46 | [diff] [blame] | 53 | const google_apis::DownloadActionCallback& download_action_callback, |
| 54 | const google_apis::GetContentCallback& get_content_callback, |
| 55 | const google_apis::ProgressCallback& progress_callback) OVERRIDE { |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame^] | 56 | ++download_file_count_; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 57 | if (resource_id == resource_id_to_be_cancelled_) { |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 58 | base::MessageLoopProxy::current()->PostTask( |
| 59 | FROM_HERE, |
[email protected] | 7a686016 | 2013-07-03 12:31:46 | [diff] [blame] | 60 | base::Bind(download_action_callback, |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 61 | google_apis::GDATA_CANCELLED, |
[email protected] | 7a686016 | 2013-07-03 12:31:46 | [diff] [blame] | 62 | base::FilePath())); |
[email protected] | cef0f06 | 2013-06-14 16:13:07 | [diff] [blame] | 63 | return google_apis::CancelCallback(); |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 64 | } |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame^] | 65 | if (resource_id == resource_id_to_be_paused_) { |
| 66 | paused_action_ = base::Bind(download_action_callback, |
| 67 | google_apis::GDATA_OTHER_ERROR, |
| 68 | base::FilePath()); |
| 69 | return google_apis::CancelCallback(); |
| 70 | } |
[email protected] | 7a686016 | 2013-07-03 12:31:46 | [diff] [blame] | 71 | return FakeDriveService::DownloadFile(local_cache_path, |
| 72 | resource_id, |
| 73 | download_action_callback, |
| 74 | get_content_callback, |
| 75 | progress_callback); |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 76 | } |
[email protected] | aaa70a4 | 2012-06-05 22:22:51 | [diff] [blame] | 77 | |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame^] | 78 | int download_file_count() const { return download_file_count_; } |
| 79 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 80 | void set_resource_id_to_be_cancelled(const std::string& resource_id) { |
| 81 | resource_id_to_be_cancelled_ = resource_id; |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 82 | } |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 83 | |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame^] | 84 | void set_resource_id_to_be_paused(const std::string& resource_id) { |
| 85 | resource_id_to_be_paused_ = resource_id; |
| 86 | } |
| 87 | |
| 88 | const base::Closure& paused_action() const { return paused_action_; } |
| 89 | |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 90 | private: |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame^] | 91 | int download_file_count_; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 92 | std::string resource_id_to_be_cancelled_; |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame^] | 93 | std::string resource_id_to_be_paused_; |
| 94 | base::Closure paused_action_; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | class DummyOperationObserver : public file_system::OperationObserver { |
[email protected] | 6bf58114 | 2013-06-11 17:13:06 | [diff] [blame] | 98 | // OperationObserver override: |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 99 | virtual void OnDirectoryChangedByOperation( |
| 100 | const base::FilePath& path) OVERRIDE {} |
[email protected] | 6bf58114 | 2013-06-11 17:13:06 | [diff] [blame] | 101 | virtual void OnCacheFileUploadNeededByOperation( |
[email protected] | 450f0e6 | 2013-08-28 10:44:08 | [diff] [blame] | 102 | const std::string& local_id) OVERRIDE {} |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 103 | }; |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 104 | |
[email protected] | 8019936 | 2012-09-09 23:24:29 | [diff] [blame] | 105 | } // namespace |
| 106 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame] | 107 | class SyncClientTest : public testing::Test { |
[email protected] | ffd60e43 | 2012-03-24 20:36:00 | [diff] [blame] | 108 | public: |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 109 | virtual void SetUp() OVERRIDE { |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 110 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 111 | |
[email protected] | db20d21 | 2013-07-08 07:18:16 | [diff] [blame] | 112 | pref_service_.reset(new TestingPrefServiceSimple); |
| 113 | test_util::RegisterDrivePrefs(pref_service_->registry()); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 114 | |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 115 | fake_network_change_notifier_.reset( |
| 116 | new test_util::FakeNetworkChangeNotifier); |
| 117 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 118 | drive_service_.reset(new SyncClientTestDriveService); |
[email protected] | b21414aa | 2013-07-02 05:12:45 | [diff] [blame] | 119 | drive_service_->LoadResourceListForWapi("gdata/empty_feed.json"); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 120 | drive_service_->LoadAccountMetadataForWapi( |
[email protected] | b21414aa | 2013-07-02 05:12:45 | [diff] [blame] | 121 | "gdata/account_metadata.json"); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 122 | |
[email protected] | db20d21 | 2013-07-08 07:18:16 | [diff] [blame] | 123 | scheduler_.reset(new JobScheduler(pref_service_.get(), |
[email protected] | 3cd8a6a | 2013-07-03 22:01:55 | [diff] [blame] | 124 | drive_service_.get(), |
| 125 | base::MessageLoopProxy::current().get())); |
[email protected] | be427d7 | 2013-06-21 07:09:27 | [diff] [blame] | 126 | |
| 127 | metadata_storage_.reset(new ResourceMetadataStorage( |
[email protected] | 3cd8a6a | 2013-07-03 22:01:55 | [diff] [blame] | 128 | temp_dir_.path(), base::MessageLoopProxy::current().get())); |
[email protected] | be427d7 | 2013-06-21 07:09:27 | [diff] [blame] | 129 | ASSERT_TRUE(metadata_storage_->Initialize()); |
| 130 | |
| 131 | metadata_.reset(new internal::ResourceMetadata( |
| 132 | metadata_storage_.get(), base::MessageLoopProxy::current())); |
[email protected] | 34a1bbf3 | 2013-06-17 07:24:02 | [diff] [blame] | 133 | ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize()); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 134 | |
[email protected] | 2df61e1 | 2013-06-21 16:00:09 | [diff] [blame] | 135 | cache_.reset(new FileCache(metadata_storage_.get(), |
[email protected] | e07f7b7b | 2013-06-19 03:43:12 | [diff] [blame] | 136 | temp_dir_.path(), |
[email protected] | 3cd8a6a | 2013-07-03 22:01:55 | [diff] [blame] | 137 | base::MessageLoopProxy::current().get(), |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 138 | NULL /* free_disk_space_getter */)); |
[email protected] | 34a1bbf3 | 2013-06-17 07:24:02 | [diff] [blame] | 139 | ASSERT_TRUE(cache_->Initialize()); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 140 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 141 | ASSERT_NO_FATAL_FAILURE(SetUpTestData()); |
| 142 | |
[email protected] | 3cd8a6a | 2013-07-03 22:01:55 | [diff] [blame] | 143 | sync_client_.reset(new SyncClient(base::MessageLoopProxy::current().get(), |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 144 | &observer_, |
| 145 | scheduler_.get(), |
| 146 | metadata_.get(), |
[email protected] | e07f7b7b | 2013-06-19 03:43:12 | [diff] [blame] | 147 | cache_.get(), |
| 148 | temp_dir_.path())); |
[email protected] | e0529fc | 2012-06-12 11:07:58 | [diff] [blame] | 149 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 150 | // Disable delaying so that DoSyncLoop() starts immediately. |
| 151 | sync_client_->set_delay_for_testing(base::TimeDelta::FromSeconds(0)); |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 154 | // Adds a file to the service root and |resource_ids_|. |
| 155 | void AddFileEntry(const std::string& title) { |
| 156 | google_apis::GDataErrorCode error = google_apis::GDATA_FILE_ERROR; |
| 157 | scoped_ptr<google_apis::ResourceEntry> entry; |
| 158 | drive_service_->AddNewFile( |
| 159 | "text/plain", |
| 160 | kRemoteContent, |
| 161 | drive_service_->GetRootResourceId(), |
| 162 | title, |
| 163 | false, // shared_with_me |
| 164 | google_apis::test_util::CreateCopyResultCallback(&error, &entry)); |
| 165 | base::RunLoop().RunUntilIdle(); |
| 166 | ASSERT_EQ(google_apis::HTTP_CREATED, error); |
| 167 | ASSERT_TRUE(entry); |
| 168 | resource_ids_[title] = entry->resource_id(); |
| 169 | } |
| 170 | |
| 171 | // Sets up data for tests. |
| 172 | void SetUpTestData() { |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 173 | // Prepare a temp file. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 174 | base::FilePath temp_file; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 175 | EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_file)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 176 | ASSERT_TRUE(google_apis::test_util::WriteStringToFile(temp_file, |
| 177 | kLocalContent)); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 178 | |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 179 | // Add file entries to the service. |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 180 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("foo")); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 181 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("bar")); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 182 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("baz")); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 183 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("fetched")); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 184 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("dirty")); |
[email protected] | 562af1c1 | 2013-11-26 10:27:53 | [diff] [blame] | 185 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("removed")); |
[email protected] | 5170a13 | 2013-12-02 10:35:13 | [diff] [blame] | 186 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("moved")); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 187 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 188 | // Load data from the service to the metadata. |
| 189 | FileError error = FILE_ERROR_FAILED; |
| 190 | internal::ChangeListLoader change_list_loader( |
[email protected] | 3cd8a6a | 2013-07-03 22:01:55 | [diff] [blame] | 191 | base::MessageLoopProxy::current().get(), |
| 192 | metadata_.get(), |
[email protected] | b99139b | 2013-09-06 11:56:45 | [diff] [blame] | 193 | scheduler_.get(), |
| 194 | drive_service_.get()); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 195 | change_list_loader.LoadIfNeeded( |
| 196 | DirectoryFetchInfo(), |
| 197 | google_apis::test_util::CreateCopyResultCallback(&error)); |
| 198 | base::RunLoop().RunUntilIdle(); |
| 199 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 200 | |
| 201 | // Prepare 3 pinned-but-not-present files. |
| 202 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("foo"))); |
| 203 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("bar"))); |
| 204 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("baz"))); |
| 205 | |
| 206 | // Prepare a pinned-and-fetched file. |
| 207 | const std::string md5_fetched = "md5"; |
| 208 | EXPECT_EQ(FILE_ERROR_OK, |
| 209 | cache_->Store(GetLocalId("fetched"), md5_fetched, |
| 210 | temp_file, FileCache::FILE_OPERATION_COPY)); |
| 211 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("fetched"))); |
| 212 | |
| 213 | // Prepare a pinned-and-fetched-and-dirty file. |
| 214 | const std::string md5_dirty = ""; // Don't care. |
| 215 | EXPECT_EQ(FILE_ERROR_OK, |
| 216 | cache_->Store(GetLocalId("dirty"), md5_dirty, |
| 217 | temp_file, FileCache::FILE_OPERATION_COPY)); |
| 218 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("dirty"))); |
| 219 | EXPECT_EQ(FILE_ERROR_OK, cache_->MarkDirty(GetLocalId("dirty"))); |
| 220 | |
[email protected] | 562af1c1 | 2013-11-26 10:27:53 | [diff] [blame] | 221 | // Prepare a removed file. |
| 222 | file_system::RemoveOperation remove_operation( |
| 223 | base::MessageLoopProxy::current().get(), &observer_, metadata_.get(), |
| 224 | cache_.get()); |
| 225 | remove_operation.Remove( |
| 226 | metadata_->GetFilePath(GetLocalId("removed")), |
| 227 | false, // is_recursive |
| 228 | google_apis::test_util::CreateCopyResultCallback(&error)); |
| 229 | base::RunLoop().RunUntilIdle(); |
| 230 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | 5170a13 | 2013-12-02 10:35:13 | [diff] [blame] | 231 | |
| 232 | // Prepare a moved file. |
| 233 | file_system::MoveOperation move_operation( |
| 234 | base::MessageLoopProxy::current().get(), &observer_, metadata_.get()); |
| 235 | move_operation.Move( |
| 236 | metadata_->GetFilePath(GetLocalId("moved")), |
| 237 | util::GetDriveMyDriveRootPath().AppendASCII("moved_new_title"), |
| 238 | false, // preserve_last_modified |
| 239 | google_apis::test_util::CreateCopyResultCallback(&error)); |
| 240 | base::RunLoop().RunUntilIdle(); |
| 241 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 242 | } |
| 243 | |
[email protected] | ffd60e43 | 2012-03-24 20:36:00 | [diff] [blame] | 244 | protected: |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 245 | std::string GetLocalId(const std::string& title) { |
| 246 | EXPECT_EQ(1U, resource_ids_.count(title)); |
| 247 | std::string local_id; |
| 248 | EXPECT_EQ(FILE_ERROR_OK, |
| 249 | metadata_->GetIdByResourceId(resource_ids_[title], &local_id)); |
| 250 | return local_id; |
| 251 | } |
| 252 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 253 | content::TestBrowserThreadBundle thread_bundle_; |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 254 | base::ScopedTempDir temp_dir_; |
[email protected] | db20d21 | 2013-07-08 07:18:16 | [diff] [blame] | 255 | scoped_ptr<TestingPrefServiceSimple> pref_service_; |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 256 | scoped_ptr<test_util::FakeNetworkChangeNotifier> |
| 257 | fake_network_change_notifier_; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 258 | scoped_ptr<SyncClientTestDriveService> drive_service_; |
| 259 | DummyOperationObserver observer_; |
| 260 | scoped_ptr<JobScheduler> scheduler_; |
[email protected] | be427d7 | 2013-06-21 07:09:27 | [diff] [blame] | 261 | scoped_ptr<ResourceMetadataStorage, |
| 262 | test_util::DestroyHelperForTests> metadata_storage_; |
| 263 | scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_; |
[email protected] | 4e87c486 | 2013-05-20 04:06:32 | [diff] [blame] | 264 | scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame] | 265 | scoped_ptr<SyncClient> sync_client_; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 266 | |
| 267 | std::map<std::string, std::string> resource_ids_; // Name-to-id map. |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 268 | }; |
| 269 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 270 | TEST_F(SyncClientTest, StartProcessingBacklog) { |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 271 | sync_client_->StartProcessingBacklog(); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 272 | base::RunLoop().RunUntilIdle(); |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 273 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 274 | FileCacheEntry cache_entry; |
| 275 | // Pinned files get downloaded. |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 276 | EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 277 | EXPECT_TRUE(cache_entry.is_present()); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 278 | |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 279 | EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("bar"), &cache_entry)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 280 | EXPECT_TRUE(cache_entry.is_present()); |
| 281 | |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 282 | EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("baz"), &cache_entry)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 283 | EXPECT_TRUE(cache_entry.is_present()); |
| 284 | |
| 285 | // Dirty file gets uploaded. |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 286 | EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("dirty"), &cache_entry)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 287 | EXPECT_FALSE(cache_entry.is_dirty()); |
[email protected] | 562af1c1 | 2013-11-26 10:27:53 | [diff] [blame] | 288 | |
| 289 | // Removed entry is not found. |
| 290 | google_apis::GDataErrorCode status = google_apis::GDATA_OTHER_ERROR; |
| 291 | scoped_ptr<google_apis::ResourceEntry> resource_entry; |
| 292 | drive_service_->GetResourceEntry( |
| 293 | resource_ids_["removed"], |
| 294 | google_apis::test_util::CreateCopyResultCallback(&status, |
| 295 | &resource_entry)); |
| 296 | base::RunLoop().RunUntilIdle(); |
| 297 | EXPECT_EQ(google_apis::HTTP_SUCCESS, status); |
| 298 | ASSERT_TRUE(resource_entry); |
| 299 | EXPECT_TRUE(resource_entry->deleted()); |
[email protected] | 5170a13 | 2013-12-02 10:35:13 | [diff] [blame] | 300 | |
| 301 | // Moved entry was moved. |
| 302 | status = google_apis::GDATA_OTHER_ERROR; |
| 303 | drive_service_->GetResourceEntry( |
| 304 | resource_ids_["moved"], |
| 305 | google_apis::test_util::CreateCopyResultCallback(&status, |
| 306 | &resource_entry)); |
| 307 | base::RunLoop().RunUntilIdle(); |
| 308 | EXPECT_EQ(google_apis::HTTP_SUCCESS, status); |
| 309 | ASSERT_TRUE(resource_entry); |
| 310 | EXPECT_EQ("moved_new_title", resource_entry->title()); |
[email protected] | a538177 | 2012-04-05 02:20:04 | [diff] [blame] | 311 | } |
| 312 | |
[email protected] | bd2254d | 2013-06-12 16:00:47 | [diff] [blame] | 313 | TEST_F(SyncClientTest, AddFetchTask) { |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 314 | sync_client_->AddFetchTask(GetLocalId("foo")); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 315 | base::RunLoop().RunUntilIdle(); |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 316 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 317 | FileCacheEntry cache_entry; |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 318 | EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 319 | EXPECT_TRUE(cache_entry.is_present()); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 320 | } |
| 321 | |
[email protected] | bd2254d | 2013-06-12 16:00:47 | [diff] [blame] | 322 | TEST_F(SyncClientTest, AddFetchTaskAndCancelled) { |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 323 | // Trigger fetching of a file which results in cancellation. |
[email protected] | bd2254d | 2013-06-12 16:00:47 | [diff] [blame] | 324 | drive_service_->set_resource_id_to_be_cancelled(resource_ids_["foo"]); |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 325 | sync_client_->AddFetchTask(GetLocalId("foo")); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 326 | base::RunLoop().RunUntilIdle(); |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 327 | |
| 328 | // 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] | 329 | FileCacheEntry cache_entry; |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 330 | EXPECT_FALSE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry)); |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 331 | } |
| 332 | |
[email protected] | bd2254d | 2013-06-12 16:00:47 | [diff] [blame] | 333 | TEST_F(SyncClientTest, RemoveFetchTask) { |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 334 | sync_client_->AddFetchTask(GetLocalId("foo")); |
| 335 | sync_client_->AddFetchTask(GetLocalId("bar")); |
| 336 | sync_client_->AddFetchTask(GetLocalId("baz")); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 337 | |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 338 | sync_client_->RemoveFetchTask(GetLocalId("foo")); |
| 339 | sync_client_->RemoveFetchTask(GetLocalId("baz")); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 340 | base::RunLoop().RunUntilIdle(); |
[email protected] | e07bbd97 | 2013-01-23 17:38:42 | [diff] [blame] | 341 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 342 | // Only "bar" should be fetched. |
| 343 | FileCacheEntry cache_entry; |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 344 | EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 345 | EXPECT_FALSE(cache_entry.is_present()); |
| 346 | |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 347 | EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("bar"), &cache_entry)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 348 | EXPECT_TRUE(cache_entry.is_present()); |
| 349 | |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 350 | EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("baz"), &cache_entry)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 351 | EXPECT_FALSE(cache_entry.is_present()); |
| 352 | |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 353 | } |
| 354 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame] | 355 | TEST_F(SyncClientTest, ExistingPinnedFiles) { |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 356 | // Start checking the existing pinned files. This will collect the resource |
| 357 | // IDs of pinned files, with stale local cache files. |
| 358 | sync_client_->StartCheckingExistingPinnedFiles(); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 359 | base::RunLoop().RunUntilIdle(); |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 360 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 361 | // "fetched" and "dirty" are the existing pinned files. |
| 362 | // The non-dirty one should be synced, but the dirty one should not. |
| 363 | base::FilePath cache_file; |
| 364 | std::string content; |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 365 | EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(GetLocalId("fetched"), &cache_file)); |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 366 | EXPECT_TRUE(base::ReadFileToString(cache_file, &content)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 367 | EXPECT_EQ(kRemoteContent, content); |
| 368 | content.clear(); |
| 369 | |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 370 | EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(GetLocalId("dirty"), &cache_file)); |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 371 | EXPECT_TRUE(base::ReadFileToString(cache_file, &content)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 372 | EXPECT_EQ(kLocalContent, content); |
[email protected] | 189541ba | 2012-10-24 11:18:15 | [diff] [blame] | 373 | } |
| 374 | |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 375 | TEST_F(SyncClientTest, RetryOnDisconnection) { |
| 376 | // Let the service go down. |
| 377 | drive_service_->set_offline(true); |
| 378 | // Change the network connection state after some delay, to test that |
| 379 | // FILE_ERROR_NO_CONNECTION is handled by SyncClient correctly. |
| 380 | // Without this delay, JobScheduler will keep the jobs unrun and SyncClient |
| 381 | // will receive no error. |
| 382 | base::MessageLoopProxy::current()->PostDelayedTask( |
| 383 | FROM_HERE, |
| 384 | base::Bind(&test_util::FakeNetworkChangeNotifier::SetConnectionType, |
| 385 | base::Unretained(fake_network_change_notifier_.get()), |
| 386 | net::NetworkChangeNotifier::CONNECTION_NONE), |
| 387 | TestTimeouts::tiny_timeout()); |
| 388 | |
| 389 | // Try fetch and upload. |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 390 | sync_client_->AddFetchTask(GetLocalId("foo")); |
[email protected] | 882e320 | 2013-11-25 08:33:50 | [diff] [blame] | 391 | sync_client_->AddUploadTask(ClientContext(USER_INITIATED), |
| 392 | GetLocalId("dirty")); |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 393 | base::RunLoop().RunUntilIdle(); |
| 394 | |
| 395 | // Not yet fetched nor uploaded. |
| 396 | FileCacheEntry cache_entry; |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 397 | EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry)); |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 398 | EXPECT_FALSE(cache_entry.is_present()); |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 399 | EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("dirty"), &cache_entry)); |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 400 | EXPECT_TRUE(cache_entry.is_dirty()); |
| 401 | |
| 402 | // Switch to online. |
| 403 | fake_network_change_notifier_->SetConnectionType( |
| 404 | net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 405 | drive_service_->set_offline(false); |
| 406 | base::RunLoop().RunUntilIdle(); |
| 407 | |
| 408 | // Fetched and uploaded. |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 409 | EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry)); |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 410 | EXPECT_TRUE(cache_entry.is_present()); |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 411 | EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("dirty"), &cache_entry)); |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 412 | EXPECT_FALSE(cache_entry.is_dirty()); |
| 413 | } |
| 414 | |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame^] | 415 | TEST_F(SyncClientTest, ScheduleRerun) { |
| 416 | // Add a fetch task for "foo", this should result in being paused. |
| 417 | drive_service_->set_resource_id_to_be_paused(resource_ids_["foo"]); |
| 418 | sync_client_->AddFetchTask(GetLocalId("foo")); |
| 419 | base::RunLoop().RunUntilIdle(); |
| 420 | |
| 421 | // While the first task is paused, add a task again. |
| 422 | // This results in scheduling rerun of the task. |
| 423 | sync_client_->AddFetchTask(GetLocalId("foo")); |
| 424 | base::RunLoop().RunUntilIdle(); |
| 425 | |
| 426 | // Resume the paused task. |
| 427 | drive_service_->set_resource_id_to_be_paused(std::string()); |
| 428 | ASSERT_FALSE(drive_service_->paused_action().is_null()); |
| 429 | drive_service_->paused_action().Run(); |
| 430 | base::RunLoop().RunUntilIdle(); |
| 431 | |
| 432 | // Task should be run twice. |
| 433 | EXPECT_EQ(2, drive_service_->download_file_count()); |
| 434 | } |
| 435 | |
[email protected] | 4e87c486 | 2013-05-20 04:06:32 | [diff] [blame] | 436 | } // namespace internal |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 437 | } // namespace drive |