blob: 782885dc8f7836733de609c0009e91310e390273 [file] [log] [blame]
[email protected]92b84f332012-03-21 20:45:211// 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
yawano3513e142016-04-20 00:42:425#include "components/drive/chromeos/sync_client.h"
[email protected]92b84f332012-03-21 20:45:216
dchengf42750232016-04-12 04:12:277#include <memory>
8
[email protected]57999812013-02-24 05:40:529#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0410#include "base/files/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:2311#include "base/files/scoped_temp_dir.h"
[email protected]b568b882013-06-10 04:38:0712#include "base/run_loop.h"
pranay.kumar229290b52015-05-14 05:21:0413#include "base/single_thread_task_runner.h"
[email protected]b83e5202012-06-27 07:50:2414#include "base/test/test_timeouts.h"
gab7966d312016-05-11 20:35:0115#include "base/threading/thread_task_runner_handle.h"
Stuart Langleyad8728c2018-04-19 04:05:5616#include "components/drive/chromeos/about_resource_loader.h"
Stuart Langley24e4ff52018-05-25 00:29:1917#include "components/drive/chromeos/about_resource_root_folder_id_loader.h"
yawano3513e142016-04-20 00:42:4218#include "components/drive/chromeos/change_list_loader.h"
19#include "components/drive/chromeos/drive_test_util.h"
20#include "components/drive/chromeos/fake_free_disk_space_getter.h"
21#include "components/drive/chromeos/file_cache.h"
22#include "components/drive/chromeos/file_system/move_operation.h"
23#include "components/drive/chromeos/file_system/operation_delegate.h"
24#include "components/drive/chromeos/file_system/remove_operation.h"
Stuart Langleyf51f3412018-05-08 23:43:0425#include "components/drive/chromeos/loader_controller.h"
yawano3513e142016-04-20 00:42:4226#include "components/drive/chromeos/resource_metadata.h"
Stuart Langleyc92ed252018-05-16 08:38:3427#include "components/drive/chromeos/start_page_token_loader.h"
lukasza01b9d55a2015-07-21 15:19:2528#include "components/drive/drive.pb.h"
lukasza8acc4eb2015-07-20 20:57:2029#include "components/drive/event_logger.h"
lukasza76b4a982015-08-08 00:36:3930#include "components/drive/file_change.h"
lukasza6364a022015-08-21 01:13:2431#include "components/drive/file_system_core_util.h"
lukaszab371ff4f2015-08-13 18:23:4732#include "components/drive/job_scheduler.h"
lukasza6364a022015-08-21 01:13:2433#include "components/drive/resource_entry_conversion.h"
lukasza8acc4eb2015-07-20 20:57:2034#include "components/drive/service/fake_drive_service.h"
brettw066508682016-02-03 08:22:0235#include "components/prefs/testing_pref_service.h"
[email protected]b568b882013-06-10 04:38:0736#include "content/public/test/test_browser_thread_bundle.h"
[email protected]31258442014-06-05 03:37:4137#include "google_apis/drive/drive_api_parser.h"
[email protected]e196bef32013-12-03 05:33:1338#include "google_apis/drive/test_util.h"
[email protected]92b84f332012-03-21 20:45:2139#include "testing/gtest/include/gtest/gtest.h"
40
[email protected]d9d04df2012-10-12 07:06:3541namespace drive {
[email protected]4e87c4862013-05-20 04:06:3242namespace internal {
[email protected]92b84f332012-03-21 20:45:2143
[email protected]80199362012-09-09 23:24:2944namespace {
45
[email protected]7a6860162013-07-03 12:31:4646// The content of files initially stored in the cache.
[email protected]b568b882013-06-10 04:38:0747const char kLocalContent[] = "Hello!";
[email protected]61a41c12013-06-06 06:23:3648
[email protected]b568b882013-06-10 04:38:0749// The content of files stored in the service.
50const char kRemoteContent[] = "World!";
51
satorux1e04b422015-01-29 07:50:5352// SyncClientTestDriveService will return DRIVE_CANCELLED when a request is
[email protected]b568b882013-06-10 04:38:0753// made with the specified resource ID.
[email protected]e50af7652013-06-20 06:39:3154class SyncClientTestDriveService : public ::drive::FakeDriveService {
[email protected]67e264a2013-06-03 05:39:0155 public:
[email protected]799d4ec2013-12-06 07:39:2356 SyncClientTestDriveService() : download_file_count_(0) {}
57
[email protected]b568b882013-06-10 04:38:0758 // FakeDriveService override:
dchengfe773302015-01-22 19:45:5259 google_apis::CancelCallback DownloadFile(
[email protected]7a6860162013-07-03 12:31:4660 const base::FilePath& local_cache_path,
[email protected]67e264a2013-06-03 05:39:0161 const std::string& resource_id,
[email protected]7a6860162013-07-03 12:31:4662 const google_apis::DownloadActionCallback& download_action_callback,
63 const google_apis::GetContentCallback& get_content_callback,
mostynb9108efb92014-10-03 23:46:1564 const google_apis::ProgressCallback& progress_callback) override {
[email protected]799d4ec2013-12-06 07:39:2365 ++download_file_count_;
[email protected]b568b882013-06-10 04:38:0766 if (resource_id == resource_id_to_be_cancelled_) {
pranay.kumar229290b52015-05-14 05:21:0467 base::ThreadTaskRunnerHandle::Get()->PostTask(
[email protected]b568b882013-06-10 04:38:0768 FROM_HERE,
tzik2bcf8e42018-07-31 11:22:1569 base::BindOnce(download_action_callback, google_apis::DRIVE_CANCELLED,
70 base::FilePath()));
[email protected]cef0f062013-06-14 16:13:0771 return google_apis::CancelCallback();
[email protected]61a41c12013-06-06 06:23:3672 }
[email protected]799d4ec2013-12-06 07:39:2373 if (resource_id == resource_id_to_be_paused_) {
74 paused_action_ = base::Bind(download_action_callback,
satorux1e04b422015-01-29 07:50:5375 google_apis::DRIVE_OTHER_ERROR,
[email protected]799d4ec2013-12-06 07:39:2376 base::FilePath());
77 return google_apis::CancelCallback();
78 }
[email protected]7a6860162013-07-03 12:31:4679 return FakeDriveService::DownloadFile(local_cache_path,
80 resource_id,
81 download_action_callback,
82 get_content_callback,
83 progress_callback);
[email protected]67e264a2013-06-03 05:39:0184 }
[email protected]aaa70a42012-06-05 22:22:5185
[email protected]799d4ec2013-12-06 07:39:2386 int download_file_count() const { return download_file_count_; }
87
[email protected]b568b882013-06-10 04:38:0788 void set_resource_id_to_be_cancelled(const std::string& resource_id) {
89 resource_id_to_be_cancelled_ = resource_id;
[email protected]67e264a2013-06-03 05:39:0190 }
[email protected]b83e5202012-06-27 07:50:2491
[email protected]799d4ec2013-12-06 07:39:2392 void set_resource_id_to_be_paused(const std::string& resource_id) {
93 resource_id_to_be_paused_ = resource_id;
94 }
95
96 const base::Closure& paused_action() const { return paused_action_; }
97
[email protected]67e264a2013-06-03 05:39:0198 private:
[email protected]799d4ec2013-12-06 07:39:2399 int download_file_count_;
[email protected]b568b882013-06-10 04:38:07100 std::string resource_id_to_be_cancelled_;
[email protected]799d4ec2013-12-06 07:39:23101 std::string resource_id_to_be_paused_;
102 base::Closure paused_action_;
[email protected]b568b882013-06-10 04:38:07103};
104
[email protected]80199362012-09-09 23:24:29105} // namespace
106
[email protected]d876d70b2013-04-23 20:06:15107class SyncClientTest : public testing::Test {
[email protected]ffd60e432012-03-24 20:36:00108 public:
dchengfe773302015-01-22 19:45:52109 void SetUp() override {
[email protected]92b84f332012-03-21 20:45:21110 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
111
Stuart Langley6b785f82018-06-13 05:32:20112 pref_service_ = std::make_unique<TestingPrefServiceSimple>();
[email protected]db20d212013-07-08 07:18:16113 test_util::RegisterDrivePrefs(pref_service_->registry());
[email protected]b568b882013-06-10 04:38:07114
Stuart Langley6b785f82018-06-13 05:32:20115 fake_network_change_notifier_ =
116 std::make_unique<test_util::FakeNetworkChangeNotifier>();
[email protected]d2c08b72013-07-02 08:23:48117
Stuart Langley6b785f82018-06-13 05:32:20118 logger_ = std::make_unique<EventLogger>();
[email protected]9475cc72014-02-05 01:50:57119
Stuart Langley6b785f82018-06-13 05:32:20120 drive_service_ = std::make_unique<SyncClientTestDriveService>();
[email protected]b568b882013-06-10 04:38:07121
Stuart Langley6b785f82018-06-13 05:32:20122 scheduler_ = std::make_unique<JobScheduler>(
Ke He24a04642017-06-26 08:38:36123 pref_service_.get(), logger_.get(), drive_service_.get(),
Stuart Langley6b785f82018-06-13 05:32:20124 base::ThreadTaskRunnerHandle::Get().get(), nullptr);
[email protected]be427d72013-06-21 07:09:27125
126 metadata_storage_.reset(new ResourceMetadataStorage(
vabr96fd0c0f2016-09-13 14:21:31127 temp_dir_.GetPath(), base::ThreadTaskRunnerHandle::Get().get()));
[email protected]be427d72013-06-21 07:09:27128 ASSERT_TRUE(metadata_storage_->Initialize());
129
vabr96fd0c0f2016-09-13 14:21:31130 cache_.reset(new FileCache(metadata_storage_.get(), temp_dir_.GetPath(),
pranay.kumar229290b52015-05-14 05:21:04131 base::ThreadTaskRunnerHandle::Get().get(),
Stuart Langleyfda065f2018-05-30 03:01:05132 nullptr /* free_disk_space_getter */));
[email protected]34a1bbf32013-06-17 07:24:02133 ASSERT_TRUE(cache_->Initialize());
[email protected]e63bdc972012-11-22 12:10:48134
[email protected]1c98a9ee2014-05-14 04:01:55135 metadata_.reset(new internal::ResourceMetadata(
136 metadata_storage_.get(), cache_.get(),
pranay.kumar229290b52015-05-14 05:21:04137 base::ThreadTaskRunnerHandle::Get()));
[email protected]1c98a9ee2014-05-14 04:01:55138 ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize());
139
Stuart Langley6b785f82018-06-13 05:32:20140 about_resource_loader_ =
141 std::make_unique<AboutResourceLoader>(scheduler_.get());
Stuart Langley24e4ff52018-05-25 00:29:19142 root_folder_id_loader_ = std::make_unique<AboutResourceRootFolderIdLoader>(
143 about_resource_loader_.get());
144
Stuart Langley6b785f82018-06-13 05:32:20145 start_page_token_loader_ = std::make_unique<StartPageTokenLoader>(
146 drive::util::kTeamDriveIdDefaultCorpus, scheduler_.get());
147 loader_controller_ = std::make_unique<LoaderController>();
148 change_list_loader_ = std::make_unique<ChangeListLoader>(
Stuart Langleyc92ed252018-05-16 08:38:34149 logger_.get(), base::ThreadTaskRunnerHandle::Get().get(),
Stuart Langley24e4ff52018-05-25 00:29:19150 metadata_.get(), scheduler_.get(), root_folder_id_loader_.get(),
Stuart Langley95c21502018-05-25 04:21:29151 start_page_token_loader_.get(), loader_controller_.get(),
Stuart Langley6b785f82018-06-13 05:32:20152 util::kTeamDriveIdDefaultCorpus, util::GetDriveMyDriveRootPath());
[email protected]b568b882013-06-10 04:38:07153 ASSERT_NO_FATAL_FAILURE(SetUpTestData());
154
Stuart Langley6b785f82018-06-13 05:32:20155 sync_client_ = std::make_unique<SyncClient>(
156 base::ThreadTaskRunnerHandle::Get().get(), &delegate_, scheduler_.get(),
157 metadata_.get(), cache_.get(), loader_controller_.get(),
158 temp_dir_.GetPath());
[email protected]e0529fc2012-06-12 11:07:58159
[email protected]b83e5202012-06-27 07:50:24160 // Disable delaying so that DoSyncLoop() starts immediately.
161 sync_client_->set_delay_for_testing(base::TimeDelta::FromSeconds(0));
[email protected]92b84f332012-03-21 20:45:21162 }
163
[email protected]b568b882013-06-10 04:38:07164 // Adds a file to the service root and |resource_ids_|.
165 void AddFileEntry(const std::string& title) {
satorux1e04b422015-01-29 07:50:53166 google_apis::DriveApiErrorCode error = google_apis::DRIVE_FILE_ERROR;
dchengf42750232016-04-12 04:12:27167 std::unique_ptr<google_apis::FileResource> entry;
[email protected]b568b882013-06-10 04:38:07168 drive_service_->AddNewFile(
169 "text/plain",
170 kRemoteContent,
171 drive_service_->GetRootResourceId(),
172 title,
173 false, // shared_with_me
174 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
175 base::RunLoop().RunUntilIdle();
176 ASSERT_EQ(google_apis::HTTP_CREATED, error);
177 ASSERT_TRUE(entry);
[email protected]31258442014-06-05 03:37:41178 resource_ids_[title] = entry->file_id();
[email protected]b568b882013-06-10 04:38:07179 }
180
181 // Sets up data for tests.
182 void SetUpTestData() {
[email protected]e63bdc972012-11-22 12:10:48183 // Prepare a temp file.
[email protected]650b2d52013-02-10 03:41:45184 base::FilePath temp_file;
vabr96fd0c0f2016-09-13 14:21:31185 EXPECT_TRUE(
186 base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &temp_file));
[email protected]b568b882013-06-10 04:38:07187 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(temp_file,
188 kLocalContent));
[email protected]b83e5202012-06-27 07:50:24189
[email protected]efd37ff2013-10-11 03:27:57190 // Add file entries to the service.
[email protected]b568b882013-06-10 04:38:07191 ASSERT_NO_FATAL_FAILURE(AddFileEntry("foo"));
[email protected]b568b882013-06-10 04:38:07192 ASSERT_NO_FATAL_FAILURE(AddFileEntry("bar"));
[email protected]b568b882013-06-10 04:38:07193 ASSERT_NO_FATAL_FAILURE(AddFileEntry("baz"));
[email protected]b568b882013-06-10 04:38:07194 ASSERT_NO_FATAL_FAILURE(AddFileEntry("fetched"));
[email protected]b568b882013-06-10 04:38:07195 ASSERT_NO_FATAL_FAILURE(AddFileEntry("dirty"));
[email protected]562af1c12013-11-26 10:27:53196 ASSERT_NO_FATAL_FAILURE(AddFileEntry("removed"));
[email protected]5170a132013-12-02 10:35:13197 ASSERT_NO_FATAL_FAILURE(AddFileEntry("moved"));
[email protected]adf84402012-03-25 21:56:38198
[email protected]b568b882013-06-10 04:38:07199 // Load data from the service to the metadata.
200 FileError error = FILE_ERROR_FAILED;
[email protected]53f57102014-02-26 03:39:03201 change_list_loader_->LoadIfNeeded(
[email protected]b568b882013-06-10 04:38:07202 google_apis::test_util::CreateCopyResultCallback(&error));
203 base::RunLoop().RunUntilIdle();
204 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]efd37ff2013-10-11 03:27:57205
206 // Prepare 3 pinned-but-not-present files.
207 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("foo")));
208 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("bar")));
209 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("baz")));
210
211 // Prepare a pinned-and-fetched file.
212 const std::string md5_fetched = "md5";
213 EXPECT_EQ(FILE_ERROR_OK,
214 cache_->Store(GetLocalId("fetched"), md5_fetched,
215 temp_file, FileCache::FILE_OPERATION_COPY));
216 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("fetched")));
217
218 // Prepare a pinned-and-fetched-and-dirty file.
[email protected]efd37ff2013-10-11 03:27:57219 EXPECT_EQ(FILE_ERROR_OK,
[email protected]bae99ae52014-01-29 01:13:14220 cache_->Store(GetLocalId("dirty"), std::string(),
[email protected]efd37ff2013-10-11 03:27:57221 temp_file, FileCache::FILE_OPERATION_COPY));
222 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(GetLocalId("dirty")));
[email protected]efd37ff2013-10-11 03:27:57223
[email protected]562af1c12013-11-26 10:27:53224 // Prepare a removed file.
225 file_system::RemoveOperation remove_operation(
pranay.kumar229290b52015-05-14 05:21:04226 base::ThreadTaskRunnerHandle::Get().get(), &delegate_, metadata_.get(),
[email protected]562af1c12013-11-26 10:27:53227 cache_.get());
228 remove_operation.Remove(
[email protected]86097a0a2014-05-12 09:25:19229 util::GetDriveMyDriveRootPath().AppendASCII("removed"),
[email protected]562af1c12013-11-26 10:27:53230 false, // is_recursive
231 google_apis::test_util::CreateCopyResultCallback(&error));
232 base::RunLoop().RunUntilIdle();
233 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]5170a132013-12-02 10:35:13234
235 // Prepare a moved file.
236 file_system::MoveOperation move_operation(
pranay.kumar229290b52015-05-14 05:21:04237 base::ThreadTaskRunnerHandle::Get().get(), &delegate_, metadata_.get());
[email protected]5170a132013-12-02 10:35:13238 move_operation.Move(
[email protected]86097a0a2014-05-12 09:25:19239 util::GetDriveMyDriveRootPath().AppendASCII("moved"),
[email protected]5170a132013-12-02 10:35:13240 util::GetDriveMyDriveRootPath().AppendASCII("moved_new_title"),
[email protected]5170a132013-12-02 10:35:13241 google_apis::test_util::CreateCopyResultCallback(&error));
242 base::RunLoop().RunUntilIdle();
243 EXPECT_EQ(FILE_ERROR_OK, error);
[email protected]b83e5202012-06-27 07:50:24244 }
245
[email protected]ffd60e432012-03-24 20:36:00246 protected:
[email protected]efd37ff2013-10-11 03:27:57247 std::string GetLocalId(const std::string& title) {
248 EXPECT_EQ(1U, resource_ids_.count(title));
249 std::string local_id;
250 EXPECT_EQ(FILE_ERROR_OK,
251 metadata_->GetIdByResourceId(resource_ids_[title], &local_id));
252 return local_id;
253 }
254
[email protected]b568b882013-06-10 04:38:07255 content::TestBrowserThreadBundle thread_bundle_;
[email protected]ea1a3f62012-11-16 20:34:23256 base::ScopedTempDir temp_dir_;
dchengf42750232016-04-12 04:12:27257 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
258 std::unique_ptr<test_util::FakeNetworkChangeNotifier>
[email protected]d2c08b72013-07-02 08:23:48259 fake_network_change_notifier_;
dchengf42750232016-04-12 04:12:27260 std::unique_ptr<EventLogger> logger_;
261 std::unique_ptr<SyncClientTestDriveService> drive_service_;
[email protected]4ffef212014-07-24 02:33:18262 file_system::OperationDelegate delegate_;
dchengf42750232016-04-12 04:12:27263 std::unique_ptr<JobScheduler> scheduler_;
264 std::unique_ptr<ResourceMetadataStorage, test_util::DestroyHelperForTests>
265 metadata_storage_;
266 std::unique_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
267 std::unique_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_;
268 std::unique_ptr<AboutResourceLoader> about_resource_loader_;
Stuart Langleyc92ed252018-05-16 08:38:34269 std::unique_ptr<StartPageTokenLoader> start_page_token_loader_;
dchengf42750232016-04-12 04:12:27270 std::unique_ptr<LoaderController> loader_controller_;
271 std::unique_ptr<ChangeListLoader> change_list_loader_;
272 std::unique_ptr<SyncClient> sync_client_;
Stuart Langley24e4ff52018-05-25 00:29:19273 std::unique_ptr<AboutResourceRootFolderIdLoader> root_folder_id_loader_;
[email protected]b568b882013-06-10 04:38:07274
275 std::map<std::string, std::string> resource_ids_; // Name-to-id map.
[email protected]92b84f332012-03-21 20:45:21276};
277
[email protected]b568b882013-06-10 04:38:07278TEST_F(SyncClientTest, StartProcessingBacklog) {
[email protected]b83e5202012-06-27 07:50:24279 sync_client_->StartProcessingBacklog();
[email protected]b568b882013-06-10 04:38:07280 base::RunLoop().RunUntilIdle();
[email protected]92b84f332012-03-21 20:45:21281
[email protected]9971db12014-05-16 06:57:12282 ResourceEntry entry;
[email protected]b568b882013-06-10 04:38:07283 // Pinned files get downloaded.
[email protected]bbd72ec2014-05-12 09:27:04284 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12285 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
286 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]b83e5202012-06-27 07:50:24287
[email protected]bbd72ec2014-05-12 09:27:04288 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12289 metadata_->GetResourceEntryById(GetLocalId("bar"), &entry));
290 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]b568b882013-06-10 04:38:07291
[email protected]bbd72ec2014-05-12 09:27:04292 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12293 metadata_->GetResourceEntryById(GetLocalId("baz"), &entry));
294 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]b568b882013-06-10 04:38:07295
296 // Dirty file gets uploaded.
[email protected]bbd72ec2014-05-12 09:27:04297 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12298 metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry));
299 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
[email protected]562af1c12013-11-26 10:27:53300
301 // Removed entry is not found.
satorux1e04b422015-01-29 07:50:53302 google_apis::DriveApiErrorCode status = google_apis::DRIVE_OTHER_ERROR;
dchengf42750232016-04-12 04:12:27303 std::unique_ptr<google_apis::FileResource> server_entry;
[email protected]31258442014-06-05 03:37:41304 drive_service_->GetFileResource(
[email protected]562af1c12013-11-26 10:27:53305 resource_ids_["removed"],
[email protected]31258442014-06-05 03:37:41306 google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
[email protected]562af1c12013-11-26 10:27:53307 base::RunLoop().RunUntilIdle();
308 EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
[email protected]31258442014-06-05 03:37:41309 ASSERT_TRUE(server_entry);
310 EXPECT_TRUE(server_entry->labels().is_trashed());
[email protected]5170a132013-12-02 10:35:13311
312 // Moved entry was moved.
satorux1e04b422015-01-29 07:50:53313 status = google_apis::DRIVE_OTHER_ERROR;
[email protected]31258442014-06-05 03:37:41314 drive_service_->GetFileResource(
[email protected]5170a132013-12-02 10:35:13315 resource_ids_["moved"],
[email protected]31258442014-06-05 03:37:41316 google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
[email protected]5170a132013-12-02 10:35:13317 base::RunLoop().RunUntilIdle();
318 EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
[email protected]31258442014-06-05 03:37:41319 ASSERT_TRUE(server_entry);
320 EXPECT_EQ("moved_new_title", server_entry->title());
[email protected]a5381772012-04-05 02:20:04321}
322
[email protected]bd2254d2013-06-12 16:00:47323TEST_F(SyncClientTest, AddFetchTask) {
[email protected]efd37ff2013-10-11 03:27:57324 sync_client_->AddFetchTask(GetLocalId("foo"));
[email protected]b568b882013-06-10 04:38:07325 base::RunLoop().RunUntilIdle();
[email protected]67e264a2013-06-03 05:39:01326
[email protected]9971db12014-05-16 06:57:12327 ResourceEntry entry;
[email protected]bbd72ec2014-05-12 09:27:04328 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12329 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
330 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]adf84402012-03-25 21:56:38331}
332
[email protected]bd2254d2013-06-12 16:00:47333TEST_F(SyncClientTest, AddFetchTaskAndCancelled) {
[email protected]61a41c12013-06-06 06:23:36334 // Trigger fetching of a file which results in cancellation.
[email protected]bd2254d2013-06-12 16:00:47335 drive_service_->set_resource_id_to_be_cancelled(resource_ids_["foo"]);
[email protected]efd37ff2013-10-11 03:27:57336 sync_client_->AddFetchTask(GetLocalId("foo"));
[email protected]b568b882013-06-10 04:38:07337 base::RunLoop().RunUntilIdle();
[email protected]61a41c12013-06-06 06:23:36338
339 // The file should be unpinned if the user wants the download to be cancelled.
[email protected]9971db12014-05-16 06:57:12340 ResourceEntry entry;
341 EXPECT_EQ(FILE_ERROR_OK,
342 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
343 EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned());
[email protected]61a41c12013-06-06 06:23:36344}
345
[email protected]bd2254d2013-06-12 16:00:47346TEST_F(SyncClientTest, RemoveFetchTask) {
[email protected]efd37ff2013-10-11 03:27:57347 sync_client_->AddFetchTask(GetLocalId("foo"));
348 sync_client_->AddFetchTask(GetLocalId("bar"));
349 sync_client_->AddFetchTask(GetLocalId("baz"));
[email protected]adf84402012-03-25 21:56:38350
[email protected]efd37ff2013-10-11 03:27:57351 sync_client_->RemoveFetchTask(GetLocalId("foo"));
352 sync_client_->RemoveFetchTask(GetLocalId("baz"));
[email protected]b568b882013-06-10 04:38:07353 base::RunLoop().RunUntilIdle();
[email protected]e07bbd972013-01-23 17:38:42354
[email protected]b568b882013-06-10 04:38:07355 // Only "bar" should be fetched.
[email protected]9971db12014-05-16 06:57:12356 ResourceEntry entry;
[email protected]bbd72ec2014-05-12 09:27:04357 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12358 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
359 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present());
[email protected]b568b882013-06-10 04:38:07360
[email protected]bbd72ec2014-05-12 09:27:04361 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12362 metadata_->GetResourceEntryById(GetLocalId("bar"), &entry));
363 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]b568b882013-06-10 04:38:07364
[email protected]bbd72ec2014-05-12 09:27:04365 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12366 metadata_->GetResourceEntryById(GetLocalId("baz"), &entry));
367 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present());
[email protected]b568b882013-06-10 04:38:07368
[email protected]adf84402012-03-25 21:56:38369}
370
[email protected]d876d70b2013-04-23 20:06:15371TEST_F(SyncClientTest, ExistingPinnedFiles) {
[email protected]85b62192012-06-29 19:56:38372 // Start checking the existing pinned files. This will collect the resource
373 // IDs of pinned files, with stale local cache files.
374 sync_client_->StartCheckingExistingPinnedFiles();
[email protected]b568b882013-06-10 04:38:07375 base::RunLoop().RunUntilIdle();
[email protected]67e264a2013-06-03 05:39:01376
[email protected]b568b882013-06-10 04:38:07377 // "fetched" and "dirty" are the existing pinned files.
378 // The non-dirty one should be synced, but the dirty one should not.
379 base::FilePath cache_file;
380 std::string content;
[email protected]efd37ff2013-10-11 03:27:57381 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(GetLocalId("fetched"), &cache_file));
[email protected]82f84b92013-08-30 18:23:50382 EXPECT_TRUE(base::ReadFileToString(cache_file, &content));
[email protected]b568b882013-06-10 04:38:07383 EXPECT_EQ(kRemoteContent, content);
384 content.clear();
385
[email protected]efd37ff2013-10-11 03:27:57386 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(GetLocalId("dirty"), &cache_file));
[email protected]82f84b92013-08-30 18:23:50387 EXPECT_TRUE(base::ReadFileToString(cache_file, &content));
[email protected]b568b882013-06-10 04:38:07388 EXPECT_EQ(kLocalContent, content);
[email protected]189541ba2012-10-24 11:18:15389}
390
[email protected]d2c08b72013-07-02 08:23:48391TEST_F(SyncClientTest, RetryOnDisconnection) {
392 // Let the service go down.
393 drive_service_->set_offline(true);
394 // Change the network connection state after some delay, to test that
395 // FILE_ERROR_NO_CONNECTION is handled by SyncClient correctly.
396 // Without this delay, JobScheduler will keep the jobs unrun and SyncClient
397 // will receive no error.
pranay.kumar229290b52015-05-14 05:21:04398 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
[email protected]d2c08b72013-07-02 08:23:48399 FROM_HERE,
tzik2bcf8e42018-07-31 11:22:15400 base::BindOnce(&test_util::FakeNetworkChangeNotifier::SetConnectionType,
401 base::Unretained(fake_network_change_notifier_.get()),
402 net::NetworkChangeNotifier::CONNECTION_NONE),
[email protected]d2c08b72013-07-02 08:23:48403 TestTimeouts::tiny_timeout());
404
405 // Try fetch and upload.
[email protected]efd37ff2013-10-11 03:27:57406 sync_client_->AddFetchTask(GetLocalId("foo"));
[email protected]bfcaaf332014-01-27 06:34:59407 sync_client_->AddUpdateTask(ClientContext(USER_INITIATED),
[email protected]882e3202013-11-25 08:33:50408 GetLocalId("dirty"));
[email protected]d2c08b72013-07-02 08:23:48409 base::RunLoop().RunUntilIdle();
410
411 // Not yet fetched nor uploaded.
[email protected]9971db12014-05-16 06:57:12412 ResourceEntry entry;
[email protected]bbd72ec2014-05-12 09:27:04413 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12414 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
415 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present());
[email protected]bbd72ec2014-05-12 09:27:04416 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12417 metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry));
418 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
[email protected]d2c08b72013-07-02 08:23:48419
420 // Switch to online.
421 fake_network_change_notifier_->SetConnectionType(
422 net::NetworkChangeNotifier::CONNECTION_WIFI);
423 drive_service_->set_offline(false);
424 base::RunLoop().RunUntilIdle();
425
426 // Fetched and uploaded.
[email protected]bbd72ec2014-05-12 09:27:04427 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12428 metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
429 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
[email protected]bbd72ec2014-05-12 09:27:04430 EXPECT_EQ(FILE_ERROR_OK,
[email protected]9971db12014-05-16 06:57:12431 metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry));
432 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
[email protected]d2c08b72013-07-02 08:23:48433}
434
[email protected]799d4ec2013-12-06 07:39:23435TEST_F(SyncClientTest, ScheduleRerun) {
436 // Add a fetch task for "foo", this should result in being paused.
437 drive_service_->set_resource_id_to_be_paused(resource_ids_["foo"]);
438 sync_client_->AddFetchTask(GetLocalId("foo"));
439 base::RunLoop().RunUntilIdle();
440
441 // While the first task is paused, add a task again.
442 // This results in scheduling rerun of the task.
443 sync_client_->AddFetchTask(GetLocalId("foo"));
444 base::RunLoop().RunUntilIdle();
445
446 // Resume the paused task.
447 drive_service_->set_resource_id_to_be_paused(std::string());
448 ASSERT_FALSE(drive_service_->paused_action().is_null());
449 drive_service_->paused_action().Run();
450 base::RunLoop().RunUntilIdle();
451
452 // Task should be run twice.
453 EXPECT_EQ(2, drive_service_->download_file_count());
454}
455
[email protected]99bd5432014-02-03 06:53:38456TEST_F(SyncClientTest, Dependencies) {
457 // Create directories locally.
458 const base::FilePath kPath1(FILE_PATH_LITERAL("drive/root/dir1"));
459 const base::FilePath kPath2 = kPath1.AppendASCII("dir2");
460
461 ResourceEntry parent;
462 EXPECT_EQ(FILE_ERROR_OK,
463 metadata_->GetResourceEntryByPath(kPath1.DirName(), &parent));
464
465 ResourceEntry entry1;
466 entry1.set_parent_local_id(parent.local_id());
467 entry1.set_title(kPath1.BaseName().AsUTF8Unsafe());
468 entry1.mutable_file_info()->set_is_directory(true);
469 entry1.set_metadata_edit_state(ResourceEntry::DIRTY);
470 std::string local_id1;
471 EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry1, &local_id1));
472
473 ResourceEntry entry2;
474 entry2.set_parent_local_id(local_id1);
475 entry2.set_title(kPath2.BaseName().AsUTF8Unsafe());
476 entry2.mutable_file_info()->set_is_directory(true);
477 entry2.set_metadata_edit_state(ResourceEntry::DIRTY);
478 std::string local_id2;
479 EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry2, &local_id2));
480
481 // Start syncing the child first.
482 sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id2);
[email protected]99bd5432014-02-03 06:53:38483 // Start syncing the parent later.
484 sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id1);
485 base::RunLoop().RunUntilIdle();
486
487 // Both entries are synced.
488 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetResourceEntryById(local_id1, &entry1));
489 EXPECT_EQ(ResourceEntry::CLEAN, entry1.metadata_edit_state());
490 EXPECT_EQ(FILE_ERROR_OK, metadata_->GetResourceEntryById(local_id2, &entry2));
491 EXPECT_EQ(ResourceEntry::CLEAN, entry2.metadata_edit_state());
492}
493
[email protected]e849db52014-07-23 07:02:09494TEST_F(SyncClientTest, WaitForUpdateTaskToComplete) {
495 // Create a directory locally.
496 const base::FilePath kPath(FILE_PATH_LITERAL("drive/root/dir1"));
497
498 ResourceEntry parent;
499 EXPECT_EQ(FILE_ERROR_OK,
500 metadata_->GetResourceEntryByPath(kPath.DirName(), &parent));
501
502 ResourceEntry entry;
503 entry.set_parent_local_id(parent.local_id());
504 entry.set_title(kPath.BaseName().AsUTF8Unsafe());
505 entry.mutable_file_info()->set_is_directory(true);
506 entry.set_metadata_edit_state(ResourceEntry::DIRTY);
507 std::string local_id;
508 EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry, &local_id));
509
510 // Sync task is not yet avialable.
511 FileError error = FILE_ERROR_FAILED;
512 EXPECT_FALSE(sync_client_->WaitForUpdateTaskToComplete(
513 local_id, google_apis::test_util::CreateCopyResultCallback(&error)));
514
515 // Start syncing the directory and wait for it to complete.
516 sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id);
517
518 EXPECT_TRUE(sync_client_->WaitForUpdateTaskToComplete(
519 local_id, google_apis::test_util::CreateCopyResultCallback(&error)));
520
521 base::RunLoop().RunUntilIdle();
522
523 // The callback is called.
524 EXPECT_EQ(FILE_ERROR_OK, error);
525}
526
[email protected]4e87c4862013-05-20 04:06:32527} // namespace internal
[email protected]d9d04df2012-10-12 07:06:35528} // namespace drive