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