[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 | |
yawano | 3513e14 | 2016-04-20 00:42:42 | [diff] [blame] | 5 | #include "components/drive/chromeos/sync_client.h" |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 6 | |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 11 | #include "base/files/scoped_temp_dir.h" |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 12 | #include "base/run_loop.h" |
pranay.kumar | 229290b5 | 2015-05-14 05:21:04 | [diff] [blame] | 13 | #include "base/single_thread_task_runner.h" |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 14 | #include "base/test/test_timeouts.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 15 | #include "base/threading/thread_task_runner_handle.h" |
Stuart Langley | ad8728c | 2018-04-19 04:05:56 | [diff] [blame] | 16 | #include "components/drive/chromeos/about_resource_loader.h" |
yawano | 3513e14 | 2016-04-20 00:42:42 | [diff] [blame] | 17 | #include "components/drive/chromeos/change_list_loader.h" |
| 18 | #include "components/drive/chromeos/drive_test_util.h" |
| 19 | #include "components/drive/chromeos/fake_free_disk_space_getter.h" |
| 20 | #include "components/drive/chromeos/file_cache.h" |
| 21 | #include "components/drive/chromeos/file_system/move_operation.h" |
| 22 | #include "components/drive/chromeos/file_system/operation_delegate.h" |
| 23 | #include "components/drive/chromeos/file_system/remove_operation.h" |
| 24 | #include "components/drive/chromeos/resource_metadata.h" |
lukasza | 01b9d55a | 2015-07-21 15:19:25 | [diff] [blame] | 25 | #include "components/drive/drive.pb.h" |
lukasza | 8acc4eb | 2015-07-20 20:57:20 | [diff] [blame] | 26 | #include "components/drive/event_logger.h" |
lukasza | 76b4a98 | 2015-08-08 00:36:39 | [diff] [blame] | 27 | #include "components/drive/file_change.h" |
lukasza | 6364a02 | 2015-08-21 01:13:24 | [diff] [blame] | 28 | #include "components/drive/file_system_core_util.h" |
lukasza | b371ff4f | 2015-08-13 18:23:47 | [diff] [blame] | 29 | #include "components/drive/job_scheduler.h" |
lukasza | 6364a02 | 2015-08-21 01:13:24 | [diff] [blame] | 30 | #include "components/drive/resource_entry_conversion.h" |
lukasza | 8acc4eb | 2015-07-20 20:57:20 | [diff] [blame] | 31 | #include "components/drive/service/fake_drive_service.h" |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 32 | #include "components/prefs/testing_pref_service.h" |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 33 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | 3125844 | 2014-06-05 03:37:41 | [diff] [blame] | 34 | #include "google_apis/drive/drive_api_parser.h" |
[email protected] | e196bef3 | 2013-12-03 05:33:13 | [diff] [blame] | 35 | #include "google_apis/drive/test_util.h" |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 36 | #include "testing/gtest/include/gtest/gtest.h" |
| 37 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 38 | namespace drive { |
[email protected] | 4e87c486 | 2013-05-20 04:06:32 | [diff] [blame] | 39 | namespace internal { |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 40 | |
[email protected] | 8019936 | 2012-09-09 23:24:29 | [diff] [blame] | 41 | namespace { |
| 42 | |
[email protected] | 7a686016 | 2013-07-03 12:31:46 | [diff] [blame] | 43 | // The content of files initially stored in the cache. |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 44 | const char kLocalContent[] = "Hello!"; |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 45 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 46 | // The content of files stored in the service. |
| 47 | const char kRemoteContent[] = "World!"; |
| 48 | |
satorux | 1e04b42 | 2015-01-29 07:50:53 | [diff] [blame] | 49 | // SyncClientTestDriveService will return DRIVE_CANCELLED when a request is |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 50 | // made with the specified resource ID. |
[email protected] | e50af765 | 2013-06-20 06:39:31 | [diff] [blame] | 51 | class SyncClientTestDriveService : public ::drive::FakeDriveService { |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 52 | public: |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame] | 53 | SyncClientTestDriveService() : download_file_count_(0) {} |
| 54 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 55 | // FakeDriveService override: |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 56 | google_apis::CancelCallback DownloadFile( |
[email protected] | 7a686016 | 2013-07-03 12:31:46 | [diff] [blame] | 57 | const base::FilePath& local_cache_path, |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 58 | const std::string& resource_id, |
[email protected] | 7a686016 | 2013-07-03 12:31:46 | [diff] [blame] | 59 | const google_apis::DownloadActionCallback& download_action_callback, |
| 60 | const google_apis::GetContentCallback& get_content_callback, |
mostynb | 9108efb9 | 2014-10-03 23:46:15 | [diff] [blame] | 61 | const google_apis::ProgressCallback& progress_callback) override { |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame] | 62 | ++download_file_count_; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 63 | if (resource_id == resource_id_to_be_cancelled_) { |
pranay.kumar | 229290b5 | 2015-05-14 05:21:04 | [diff] [blame] | 64 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 65 | FROM_HERE, |
[email protected] | 7a686016 | 2013-07-03 12:31:46 | [diff] [blame] | 66 | base::Bind(download_action_callback, |
satorux | 1e04b42 | 2015-01-29 07:50:53 | [diff] [blame] | 67 | google_apis::DRIVE_CANCELLED, |
[email protected] | 7a686016 | 2013-07-03 12:31:46 | [diff] [blame] | 68 | base::FilePath())); |
[email protected] | cef0f06 | 2013-06-14 16:13:07 | [diff] [blame] | 69 | return google_apis::CancelCallback(); |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 70 | } |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame] | 71 | if (resource_id == resource_id_to_be_paused_) { |
| 72 | paused_action_ = base::Bind(download_action_callback, |
satorux | 1e04b42 | 2015-01-29 07:50:53 | [diff] [blame] | 73 | google_apis::DRIVE_OTHER_ERROR, |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame] | 74 | base::FilePath()); |
| 75 | return google_apis::CancelCallback(); |
| 76 | } |
[email protected] | 7a686016 | 2013-07-03 12:31:46 | [diff] [blame] | 77 | return FakeDriveService::DownloadFile(local_cache_path, |
| 78 | resource_id, |
| 79 | download_action_callback, |
| 80 | get_content_callback, |
| 81 | progress_callback); |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 82 | } |
[email protected] | aaa70a4 | 2012-06-05 22:22:51 | [diff] [blame] | 83 | |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame] | 84 | int download_file_count() const { return download_file_count_; } |
| 85 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 86 | void set_resource_id_to_be_cancelled(const std::string& resource_id) { |
| 87 | resource_id_to_be_cancelled_ = resource_id; |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 88 | } |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 89 | |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame] | 90 | void set_resource_id_to_be_paused(const std::string& resource_id) { |
| 91 | resource_id_to_be_paused_ = resource_id; |
| 92 | } |
| 93 | |
| 94 | const base::Closure& paused_action() const { return paused_action_; } |
| 95 | |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 96 | private: |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame] | 97 | int download_file_count_; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 98 | std::string resource_id_to_be_cancelled_; |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame] | 99 | std::string resource_id_to_be_paused_; |
| 100 | base::Closure paused_action_; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 101 | }; |
| 102 | |
[email protected] | 8019936 | 2012-09-09 23:24:29 | [diff] [blame] | 103 | } // namespace |
| 104 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame] | 105 | class SyncClientTest : public testing::Test { |
[email protected] | ffd60e43 | 2012-03-24 20:36:00 | [diff] [blame] | 106 | public: |
dcheng | fe77330 | 2015-01-22 19:45:52 | [diff] [blame] | 107 | void SetUp() override { |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 108 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 109 | |
[email protected] | db20d21 | 2013-07-08 07:18:16 | [diff] [blame] | 110 | pref_service_.reset(new TestingPrefServiceSimple); |
| 111 | test_util::RegisterDrivePrefs(pref_service_->registry()); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 112 | |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 113 | fake_network_change_notifier_.reset( |
| 114 | new test_util::FakeNetworkChangeNotifier); |
| 115 | |
[email protected] | 9475cc7 | 2014-02-05 01:50:57 | [diff] [blame] | 116 | logger_.reset(new EventLogger); |
| 117 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 118 | drive_service_.reset(new SyncClientTestDriveService); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 119 | |
pranay.kumar | 229290b5 | 2015-05-14 05:21:04 | [diff] [blame] | 120 | scheduler_.reset(new JobScheduler( |
Ke He | 24a0464 | 2017-06-26 08:38:36 | [diff] [blame] | 121 | pref_service_.get(), logger_.get(), drive_service_.get(), |
| 122 | base::ThreadTaskRunnerHandle::Get().get(), nullptr)); |
[email protected] | be427d7 | 2013-06-21 07:09:27 | [diff] [blame] | 123 | |
| 124 | metadata_storage_.reset(new ResourceMetadataStorage( |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 125 | temp_dir_.GetPath(), base::ThreadTaskRunnerHandle::Get().get())); |
[email protected] | be427d7 | 2013-06-21 07:09:27 | [diff] [blame] | 126 | ASSERT_TRUE(metadata_storage_->Initialize()); |
| 127 | |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 128 | cache_.reset(new FileCache(metadata_storage_.get(), temp_dir_.GetPath(), |
pranay.kumar | 229290b5 | 2015-05-14 05:21:04 | [diff] [blame] | 129 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 130 | NULL /* free_disk_space_getter */)); |
[email protected] | 34a1bbf3 | 2013-06-17 07:24:02 | [diff] [blame] | 131 | ASSERT_TRUE(cache_->Initialize()); |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 132 | |
[email protected] | 1c98a9ee | 2014-05-14 04:01:55 | [diff] [blame] | 133 | metadata_.reset(new internal::ResourceMetadata( |
| 134 | metadata_storage_.get(), cache_.get(), |
pranay.kumar | 229290b5 | 2015-05-14 05:21:04 | [diff] [blame] | 135 | base::ThreadTaskRunnerHandle::Get())); |
[email protected] | 1c98a9ee | 2014-05-14 04:01:55 | [diff] [blame] | 136 | ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize()); |
| 137 | |
[email protected] | 5a06ba8 | 2014-02-10 06:00:06 | [diff] [blame] | 138 | about_resource_loader_.reset(new AboutResourceLoader(scheduler_.get())); |
[email protected] | 20f33e6 | 2014-01-30 10:50:39 | [diff] [blame] | 139 | loader_controller_.reset(new LoaderController); |
[email protected] | 069011f | 2014-01-29 07:56:59 | [diff] [blame] | 140 | change_list_loader_.reset(new ChangeListLoader( |
[email protected] | 9475cc7 | 2014-02-05 01:50:57 | [diff] [blame] | 141 | logger_.get(), |
pranay.kumar | 229290b5 | 2015-05-14 05:21:04 | [diff] [blame] | 142 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 069011f | 2014-01-29 07:56:59 | [diff] [blame] | 143 | metadata_.get(), |
| 144 | scheduler_.get(), |
[email protected] | 5a06ba8 | 2014-02-10 06:00:06 | [diff] [blame] | 145 | about_resource_loader_.get(), |
[email protected] | 20f33e6 | 2014-01-30 10:50:39 | [diff] [blame] | 146 | loader_controller_.get())); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 147 | ASSERT_NO_FATAL_FAILURE(SetUpTestData()); |
| 148 | |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 149 | sync_client_.reset( |
| 150 | new SyncClient(base::ThreadTaskRunnerHandle::Get().get(), &delegate_, |
| 151 | scheduler_.get(), metadata_.get(), cache_.get(), |
| 152 | loader_controller_.get(), temp_dir_.GetPath())); |
[email protected] | e0529fc | 2012-06-12 11:07:58 | [diff] [blame] | 153 | |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 154 | // Disable delaying so that DoSyncLoop() starts immediately. |
| 155 | sync_client_->set_delay_for_testing(base::TimeDelta::FromSeconds(0)); |
[email protected] | 92b84f33 | 2012-03-21 20:45:21 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 158 | // Adds a file to the service root and |resource_ids_|. |
| 159 | void AddFileEntry(const std::string& title) { |
satorux | 1e04b42 | 2015-01-29 07:50:53 | [diff] [blame] | 160 | google_apis::DriveApiErrorCode error = google_apis::DRIVE_FILE_ERROR; |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 161 | std::unique_ptr<google_apis::FileResource> entry; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 162 | drive_service_->AddNewFile( |
| 163 | "text/plain", |
| 164 | kRemoteContent, |
| 165 | drive_service_->GetRootResourceId(), |
| 166 | title, |
| 167 | false, // shared_with_me |
| 168 | google_apis::test_util::CreateCopyResultCallback(&error, &entry)); |
| 169 | base::RunLoop().RunUntilIdle(); |
| 170 | ASSERT_EQ(google_apis::HTTP_CREATED, error); |
| 171 | ASSERT_TRUE(entry); |
[email protected] | 3125844 | 2014-06-05 03:37:41 | [diff] [blame] | 172 | resource_ids_[title] = entry->file_id(); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // Sets up data for tests. |
| 176 | void SetUpTestData() { |
[email protected] | e63bdc97 | 2012-11-22 12:10:48 | [diff] [blame] | 177 | // Prepare a temp file. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 178 | base::FilePath temp_file; |
vabr | 96fd0c0f | 2016-09-13 14:21:31 | [diff] [blame] | 179 | EXPECT_TRUE( |
| 180 | base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &temp_file)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 181 | ASSERT_TRUE(google_apis::test_util::WriteStringToFile(temp_file, |
| 182 | kLocalContent)); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 183 | |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 184 | // Add file entries to the service. |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 185 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("foo")); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 186 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("bar")); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 187 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("baz")); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 188 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("fetched")); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 189 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("dirty")); |
[email protected] | 562af1c1 | 2013-11-26 10:27:53 | [diff] [blame] | 190 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("removed")); |
[email protected] | 5170a13 | 2013-12-02 10:35:13 | [diff] [blame] | 191 | ASSERT_NO_FATAL_FAILURE(AddFileEntry("moved")); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 192 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 193 | // Load data from the service to the metadata. |
| 194 | FileError error = FILE_ERROR_FAILED; |
[email protected] | 53f5710 | 2014-02-26 03:39:03 | [diff] [blame] | 195 | change_list_loader_->LoadIfNeeded( |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 196 | google_apis::test_util::CreateCopyResultCallback(&error)); |
| 197 | base::RunLoop().RunUntilIdle(); |
| 198 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 199 | |
| 200 | // Prepare 3 pinned-but-not-present files. |
| 201 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("foo"))); |
| 202 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("bar"))); |
| 203 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("baz"))); |
| 204 | |
| 205 | // Prepare a pinned-and-fetched file. |
| 206 | const std::string md5_fetched = "md5"; |
| 207 | EXPECT_EQ(FILE_ERROR_OK, |
| 208 | cache_->Store(GetLocalId("fetched"), md5_fetched, |
| 209 | temp_file, FileCache::FILE_OPERATION_COPY)); |
| 210 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("fetched"))); |
| 211 | |
| 212 | // Prepare a pinned-and-fetched-and-dirty file. |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 213 | EXPECT_EQ(FILE_ERROR_OK, |
[email protected] | bae99ae5 | 2014-01-29 01:13:14 | [diff] [blame] | 214 | cache_->Store(GetLocalId("dirty"), std::string(), |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 215 | temp_file, FileCache::FILE_OPERATION_COPY)); |
| 216 | EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("dirty"))); |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 217 | |
[email protected] | 562af1c1 | 2013-11-26 10:27:53 | [diff] [blame] | 218 | // Prepare a removed file. |
| 219 | file_system::RemoveOperation remove_operation( |
pranay.kumar | 229290b5 | 2015-05-14 05:21:04 | [diff] [blame] | 220 | base::ThreadTaskRunnerHandle::Get().get(), &delegate_, metadata_.get(), |
[email protected] | 562af1c1 | 2013-11-26 10:27:53 | [diff] [blame] | 221 | cache_.get()); |
| 222 | remove_operation.Remove( |
[email protected] | 86097a0a | 2014-05-12 09:25:19 | [diff] [blame] | 223 | util::GetDriveMyDriveRootPath().AppendASCII("removed"), |
[email protected] | 562af1c1 | 2013-11-26 10:27:53 | [diff] [blame] | 224 | false, // is_recursive |
| 225 | google_apis::test_util::CreateCopyResultCallback(&error)); |
| 226 | base::RunLoop().RunUntilIdle(); |
| 227 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | 5170a13 | 2013-12-02 10:35:13 | [diff] [blame] | 228 | |
| 229 | // Prepare a moved file. |
| 230 | file_system::MoveOperation move_operation( |
pranay.kumar | 229290b5 | 2015-05-14 05:21:04 | [diff] [blame] | 231 | base::ThreadTaskRunnerHandle::Get().get(), &delegate_, metadata_.get()); |
[email protected] | 5170a13 | 2013-12-02 10:35:13 | [diff] [blame] | 232 | move_operation.Move( |
[email protected] | 86097a0a | 2014-05-12 09:25:19 | [diff] [blame] | 233 | util::GetDriveMyDriveRootPath().AppendASCII("moved"), |
[email protected] | 5170a13 | 2013-12-02 10:35:13 | [diff] [blame] | 234 | util::GetDriveMyDriveRootPath().AppendASCII("moved_new_title"), |
[email protected] | 5170a13 | 2013-12-02 10:35:13 | [diff] [blame] | 235 | google_apis::test_util::CreateCopyResultCallback(&error)); |
| 236 | base::RunLoop().RunUntilIdle(); |
| 237 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 238 | } |
| 239 | |
[email protected] | ffd60e43 | 2012-03-24 20:36:00 | [diff] [blame] | 240 | protected: |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 241 | std::string GetLocalId(const std::string& title) { |
| 242 | EXPECT_EQ(1U, resource_ids_.count(title)); |
| 243 | std::string local_id; |
| 244 | EXPECT_EQ(FILE_ERROR_OK, |
| 245 | metadata_->GetIdByResourceId(resource_ids_[title], &local_id)); |
| 246 | return local_id; |
| 247 | } |
| 248 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 249 | content::TestBrowserThreadBundle thread_bundle_; |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 250 | base::ScopedTempDir temp_dir_; |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 251 | std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 252 | std::unique_ptr<test_util::FakeNetworkChangeNotifier> |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 253 | fake_network_change_notifier_; |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 254 | std::unique_ptr<EventLogger> logger_; |
| 255 | std::unique_ptr<SyncClientTestDriveService> drive_service_; |
[email protected] | 4ffef21 | 2014-07-24 02:33:18 | [diff] [blame] | 256 | file_system::OperationDelegate delegate_; |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 257 | std::unique_ptr<JobScheduler> scheduler_; |
| 258 | std::unique_ptr<ResourceMetadataStorage, test_util::DestroyHelperForTests> |
| 259 | metadata_storage_; |
| 260 | std::unique_ptr<FileCache, test_util::DestroyHelperForTests> cache_; |
| 261 | std::unique_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_; |
| 262 | std::unique_ptr<AboutResourceLoader> about_resource_loader_; |
| 263 | std::unique_ptr<LoaderController> loader_controller_; |
| 264 | std::unique_ptr<ChangeListLoader> change_list_loader_; |
| 265 | std::unique_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] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 274 | ResourceEntry entry; |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 275 | // Pinned files get downloaded. |
[email protected] | bbd72ec | 2014-05-12 09:27:04 | [diff] [blame] | 276 | EXPECT_EQ(FILE_ERROR_OK, |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 277 | metadata_->GetResourceEntryById(GetLocalId("foo"), &entry)); |
| 278 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); |
[email protected] | b83e520 | 2012-06-27 07:50:24 | [diff] [blame] | 279 | |
[email protected] | bbd72ec | 2014-05-12 09:27:04 | [diff] [blame] | 280 | EXPECT_EQ(FILE_ERROR_OK, |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 281 | metadata_->GetResourceEntryById(GetLocalId("bar"), &entry)); |
| 282 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 283 | |
[email protected] | bbd72ec | 2014-05-12 09:27:04 | [diff] [blame] | 284 | EXPECT_EQ(FILE_ERROR_OK, |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 285 | metadata_->GetResourceEntryById(GetLocalId("baz"), &entry)); |
| 286 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 287 | |
| 288 | // Dirty file gets uploaded. |
[email protected] | bbd72ec | 2014-05-12 09:27:04 | [diff] [blame] | 289 | EXPECT_EQ(FILE_ERROR_OK, |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 290 | metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry)); |
| 291 | EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty()); |
[email protected] | 562af1c1 | 2013-11-26 10:27:53 | [diff] [blame] | 292 | |
| 293 | // Removed entry is not found. |
satorux | 1e04b42 | 2015-01-29 07:50:53 | [diff] [blame] | 294 | google_apis::DriveApiErrorCode status = google_apis::DRIVE_OTHER_ERROR; |
dcheng | f4275023 | 2016-04-12 04:12:27 | [diff] [blame] | 295 | std::unique_ptr<google_apis::FileResource> server_entry; |
[email protected] | 3125844 | 2014-06-05 03:37:41 | [diff] [blame] | 296 | drive_service_->GetFileResource( |
[email protected] | 562af1c1 | 2013-11-26 10:27:53 | [diff] [blame] | 297 | resource_ids_["removed"], |
[email protected] | 3125844 | 2014-06-05 03:37:41 | [diff] [blame] | 298 | google_apis::test_util::CreateCopyResultCallback(&status, &server_entry)); |
[email protected] | 562af1c1 | 2013-11-26 10:27:53 | [diff] [blame] | 299 | base::RunLoop().RunUntilIdle(); |
| 300 | EXPECT_EQ(google_apis::HTTP_SUCCESS, status); |
[email protected] | 3125844 | 2014-06-05 03:37:41 | [diff] [blame] | 301 | ASSERT_TRUE(server_entry); |
| 302 | EXPECT_TRUE(server_entry->labels().is_trashed()); |
[email protected] | 5170a13 | 2013-12-02 10:35:13 | [diff] [blame] | 303 | |
| 304 | // Moved entry was moved. |
satorux | 1e04b42 | 2015-01-29 07:50:53 | [diff] [blame] | 305 | status = google_apis::DRIVE_OTHER_ERROR; |
[email protected] | 3125844 | 2014-06-05 03:37:41 | [diff] [blame] | 306 | drive_service_->GetFileResource( |
[email protected] | 5170a13 | 2013-12-02 10:35:13 | [diff] [blame] | 307 | resource_ids_["moved"], |
[email protected] | 3125844 | 2014-06-05 03:37:41 | [diff] [blame] | 308 | google_apis::test_util::CreateCopyResultCallback(&status, &server_entry)); |
[email protected] | 5170a13 | 2013-12-02 10:35:13 | [diff] [blame] | 309 | base::RunLoop().RunUntilIdle(); |
| 310 | EXPECT_EQ(google_apis::HTTP_SUCCESS, status); |
[email protected] | 3125844 | 2014-06-05 03:37:41 | [diff] [blame] | 311 | ASSERT_TRUE(server_entry); |
| 312 | EXPECT_EQ("moved_new_title", server_entry->title()); |
[email protected] | a538177 | 2012-04-05 02:20:04 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | bd2254d | 2013-06-12 16:00:47 | [diff] [blame] | 315 | TEST_F(SyncClientTest, AddFetchTask) { |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 316 | sync_client_->AddFetchTask(GetLocalId("foo")); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 317 | base::RunLoop().RunUntilIdle(); |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 318 | |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 319 | ResourceEntry entry; |
[email protected] | bbd72ec | 2014-05-12 09:27:04 | [diff] [blame] | 320 | EXPECT_EQ(FILE_ERROR_OK, |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 321 | metadata_->GetResourceEntryById(GetLocalId("foo"), &entry)); |
| 322 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 323 | } |
| 324 | |
[email protected] | bd2254d | 2013-06-12 16:00:47 | [diff] [blame] | 325 | TEST_F(SyncClientTest, AddFetchTaskAndCancelled) { |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 326 | // Trigger fetching of a file which results in cancellation. |
[email protected] | bd2254d | 2013-06-12 16:00:47 | [diff] [blame] | 327 | drive_service_->set_resource_id_to_be_cancelled(resource_ids_["foo"]); |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 328 | sync_client_->AddFetchTask(GetLocalId("foo")); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 329 | base::RunLoop().RunUntilIdle(); |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 330 | |
| 331 | // The file should be unpinned if the user wants the download to be cancelled. |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 332 | ResourceEntry entry; |
| 333 | EXPECT_EQ(FILE_ERROR_OK, |
| 334 | metadata_->GetResourceEntryById(GetLocalId("foo"), &entry)); |
| 335 | EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned()); |
[email protected] | 61a41c1 | 2013-06-06 06:23:36 | [diff] [blame] | 336 | } |
| 337 | |
[email protected] | bd2254d | 2013-06-12 16:00:47 | [diff] [blame] | 338 | TEST_F(SyncClientTest, RemoveFetchTask) { |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 339 | sync_client_->AddFetchTask(GetLocalId("foo")); |
| 340 | sync_client_->AddFetchTask(GetLocalId("bar")); |
| 341 | sync_client_->AddFetchTask(GetLocalId("baz")); |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 342 | |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 343 | sync_client_->RemoveFetchTask(GetLocalId("foo")); |
| 344 | sync_client_->RemoveFetchTask(GetLocalId("baz")); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 345 | base::RunLoop().RunUntilIdle(); |
[email protected] | e07bbd97 | 2013-01-23 17:38:42 | [diff] [blame] | 346 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 347 | // Only "bar" should be fetched. |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 348 | ResourceEntry entry; |
[email protected] | bbd72ec | 2014-05-12 09:27:04 | [diff] [blame] | 349 | EXPECT_EQ(FILE_ERROR_OK, |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 350 | metadata_->GetResourceEntryById(GetLocalId("foo"), &entry)); |
| 351 | EXPECT_FALSE(entry.file_specific_info().cache_state().is_present()); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 352 | |
[email protected] | bbd72ec | 2014-05-12 09:27:04 | [diff] [blame] | 353 | EXPECT_EQ(FILE_ERROR_OK, |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 354 | metadata_->GetResourceEntryById(GetLocalId("bar"), &entry)); |
| 355 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 356 | |
[email protected] | bbd72ec | 2014-05-12 09:27:04 | [diff] [blame] | 357 | EXPECT_EQ(FILE_ERROR_OK, |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 358 | metadata_->GetResourceEntryById(GetLocalId("baz"), &entry)); |
| 359 | EXPECT_FALSE(entry.file_specific_info().cache_state().is_present()); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 360 | |
[email protected] | adf8440 | 2012-03-25 21:56:38 | [diff] [blame] | 361 | } |
| 362 | |
[email protected] | d876d70b | 2013-04-23 20:06:15 | [diff] [blame] | 363 | TEST_F(SyncClientTest, ExistingPinnedFiles) { |
[email protected] | 85b6219 | 2012-06-29 19:56:38 | [diff] [blame] | 364 | // Start checking the existing pinned files. This will collect the resource |
| 365 | // IDs of pinned files, with stale local cache files. |
| 366 | sync_client_->StartCheckingExistingPinnedFiles(); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 367 | base::RunLoop().RunUntilIdle(); |
[email protected] | 67e264a | 2013-06-03 05:39:01 | [diff] [blame] | 368 | |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 369 | // "fetched" and "dirty" are the existing pinned files. |
| 370 | // The non-dirty one should be synced, but the dirty one should not. |
| 371 | base::FilePath cache_file; |
| 372 | std::string content; |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 373 | EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(GetLocalId("fetched"), &cache_file)); |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 374 | EXPECT_TRUE(base::ReadFileToString(cache_file, &content)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 375 | EXPECT_EQ(kRemoteContent, content); |
| 376 | content.clear(); |
| 377 | |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 378 | EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(GetLocalId("dirty"), &cache_file)); |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 379 | EXPECT_TRUE(base::ReadFileToString(cache_file, &content)); |
[email protected] | b568b88 | 2013-06-10 04:38:07 | [diff] [blame] | 380 | EXPECT_EQ(kLocalContent, content); |
[email protected] | 189541ba | 2012-10-24 11:18:15 | [diff] [blame] | 381 | } |
| 382 | |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 383 | TEST_F(SyncClientTest, RetryOnDisconnection) { |
| 384 | // Let the service go down. |
| 385 | drive_service_->set_offline(true); |
| 386 | // Change the network connection state after some delay, to test that |
| 387 | // FILE_ERROR_NO_CONNECTION is handled by SyncClient correctly. |
| 388 | // Without this delay, JobScheduler will keep the jobs unrun and SyncClient |
| 389 | // will receive no error. |
pranay.kumar | 229290b5 | 2015-05-14 05:21:04 | [diff] [blame] | 390 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 391 | FROM_HERE, |
| 392 | base::Bind(&test_util::FakeNetworkChangeNotifier::SetConnectionType, |
| 393 | base::Unretained(fake_network_change_notifier_.get()), |
| 394 | net::NetworkChangeNotifier::CONNECTION_NONE), |
| 395 | TestTimeouts::tiny_timeout()); |
| 396 | |
| 397 | // Try fetch and upload. |
[email protected] | efd37ff | 2013-10-11 03:27:57 | [diff] [blame] | 398 | sync_client_->AddFetchTask(GetLocalId("foo")); |
[email protected] | bfcaaf33 | 2014-01-27 06:34:59 | [diff] [blame] | 399 | sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), |
[email protected] | 882e320 | 2013-11-25 08:33:50 | [diff] [blame] | 400 | GetLocalId("dirty")); |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 401 | base::RunLoop().RunUntilIdle(); |
| 402 | |
| 403 | // Not yet fetched nor uploaded. |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 404 | ResourceEntry entry; |
[email protected] | bbd72ec | 2014-05-12 09:27:04 | [diff] [blame] | 405 | EXPECT_EQ(FILE_ERROR_OK, |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 406 | metadata_->GetResourceEntryById(GetLocalId("foo"), &entry)); |
| 407 | EXPECT_FALSE(entry.file_specific_info().cache_state().is_present()); |
[email protected] | bbd72ec | 2014-05-12 09:27:04 | [diff] [blame] | 408 | EXPECT_EQ(FILE_ERROR_OK, |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 409 | metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry)); |
| 410 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty()); |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 411 | |
| 412 | // Switch to online. |
| 413 | fake_network_change_notifier_->SetConnectionType( |
| 414 | net::NetworkChangeNotifier::CONNECTION_WIFI); |
| 415 | drive_service_->set_offline(false); |
| 416 | base::RunLoop().RunUntilIdle(); |
| 417 | |
| 418 | // Fetched and uploaded. |
[email protected] | bbd72ec | 2014-05-12 09:27:04 | [diff] [blame] | 419 | EXPECT_EQ(FILE_ERROR_OK, |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 420 | metadata_->GetResourceEntryById(GetLocalId("foo"), &entry)); |
| 421 | EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); |
[email protected] | bbd72ec | 2014-05-12 09:27:04 | [diff] [blame] | 422 | EXPECT_EQ(FILE_ERROR_OK, |
[email protected] | 9971db1 | 2014-05-16 06:57:12 | [diff] [blame] | 423 | metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry)); |
| 424 | EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty()); |
[email protected] | d2c08b7 | 2013-07-02 08:23:48 | [diff] [blame] | 425 | } |
| 426 | |
[email protected] | 799d4ec | 2013-12-06 07:39:23 | [diff] [blame] | 427 | TEST_F(SyncClientTest, ScheduleRerun) { |
| 428 | // Add a fetch task for "foo", this should result in being paused. |
| 429 | drive_service_->set_resource_id_to_be_paused(resource_ids_["foo"]); |
| 430 | sync_client_->AddFetchTask(GetLocalId("foo")); |
| 431 | base::RunLoop().RunUntilIdle(); |
| 432 | |
| 433 | // While the first task is paused, add a task again. |
| 434 | // This results in scheduling rerun of the task. |
| 435 | sync_client_->AddFetchTask(GetLocalId("foo")); |
| 436 | base::RunLoop().RunUntilIdle(); |
| 437 | |
| 438 | // Resume the paused task. |
| 439 | drive_service_->set_resource_id_to_be_paused(std::string()); |
| 440 | ASSERT_FALSE(drive_service_->paused_action().is_null()); |
| 441 | drive_service_->paused_action().Run(); |
| 442 | base::RunLoop().RunUntilIdle(); |
| 443 | |
| 444 | // Task should be run twice. |
| 445 | EXPECT_EQ(2, drive_service_->download_file_count()); |
| 446 | } |
| 447 | |
[email protected] | 99bd543 | 2014-02-03 06:53:38 | [diff] [blame] | 448 | TEST_F(SyncClientTest, Dependencies) { |
| 449 | // Create directories locally. |
| 450 | const base::FilePath kPath1(FILE_PATH_LITERAL("drive/root/dir1")); |
| 451 | const base::FilePath kPath2 = kPath1.AppendASCII("dir2"); |
| 452 | |
| 453 | ResourceEntry parent; |
| 454 | EXPECT_EQ(FILE_ERROR_OK, |
| 455 | metadata_->GetResourceEntryByPath(kPath1.DirName(), &parent)); |
| 456 | |
| 457 | ResourceEntry entry1; |
| 458 | entry1.set_parent_local_id(parent.local_id()); |
| 459 | entry1.set_title(kPath1.BaseName().AsUTF8Unsafe()); |
| 460 | entry1.mutable_file_info()->set_is_directory(true); |
| 461 | entry1.set_metadata_edit_state(ResourceEntry::DIRTY); |
| 462 | std::string local_id1; |
| 463 | EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry1, &local_id1)); |
| 464 | |
| 465 | ResourceEntry entry2; |
| 466 | entry2.set_parent_local_id(local_id1); |
| 467 | entry2.set_title(kPath2.BaseName().AsUTF8Unsafe()); |
| 468 | entry2.mutable_file_info()->set_is_directory(true); |
| 469 | entry2.set_metadata_edit_state(ResourceEntry::DIRTY); |
| 470 | std::string local_id2; |
| 471 | EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry2, &local_id2)); |
| 472 | |
| 473 | // Start syncing the child first. |
| 474 | sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id2); |
[email protected] | 99bd543 | 2014-02-03 06:53:38 | [diff] [blame] | 475 | // Start syncing the parent later. |
| 476 | sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id1); |
| 477 | base::RunLoop().RunUntilIdle(); |
| 478 | |
| 479 | // Both entries are synced. |
| 480 | EXPECT_EQ(FILE_ERROR_OK, metadata_->GetResourceEntryById(local_id1, &entry1)); |
| 481 | EXPECT_EQ(ResourceEntry::CLEAN, entry1.metadata_edit_state()); |
| 482 | EXPECT_EQ(FILE_ERROR_OK, metadata_->GetResourceEntryById(local_id2, &entry2)); |
| 483 | EXPECT_EQ(ResourceEntry::CLEAN, entry2.metadata_edit_state()); |
| 484 | } |
| 485 | |
[email protected] | e849db5 | 2014-07-23 07:02:09 | [diff] [blame] | 486 | TEST_F(SyncClientTest, WaitForUpdateTaskToComplete) { |
| 487 | // Create a directory locally. |
| 488 | const base::FilePath kPath(FILE_PATH_LITERAL("drive/root/dir1")); |
| 489 | |
| 490 | ResourceEntry parent; |
| 491 | EXPECT_EQ(FILE_ERROR_OK, |
| 492 | metadata_->GetResourceEntryByPath(kPath.DirName(), &parent)); |
| 493 | |
| 494 | ResourceEntry entry; |
| 495 | entry.set_parent_local_id(parent.local_id()); |
| 496 | entry.set_title(kPath.BaseName().AsUTF8Unsafe()); |
| 497 | entry.mutable_file_info()->set_is_directory(true); |
| 498 | entry.set_metadata_edit_state(ResourceEntry::DIRTY); |
| 499 | std::string local_id; |
| 500 | EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry, &local_id)); |
| 501 | |
| 502 | // Sync task is not yet avialable. |
| 503 | FileError error = FILE_ERROR_FAILED; |
| 504 | EXPECT_FALSE(sync_client_->WaitForUpdateTaskToComplete( |
| 505 | local_id, google_apis::test_util::CreateCopyResultCallback(&error))); |
| 506 | |
| 507 | // Start syncing the directory and wait for it to complete. |
| 508 | sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id); |
| 509 | |
| 510 | EXPECT_TRUE(sync_client_->WaitForUpdateTaskToComplete( |
| 511 | local_id, google_apis::test_util::CreateCopyResultCallback(&error))); |
| 512 | |
| 513 | base::RunLoop().RunUntilIdle(); |
| 514 | |
| 515 | // The callback is called. |
| 516 | EXPECT_EQ(FILE_ERROR_OK, error); |
| 517 | } |
| 518 | |
[email protected] | 4e87c486 | 2013-05-20 04:06:32 | [diff] [blame] | 519 | } // namespace internal |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 520 | } // namespace drive |