1 // Copyright 2014 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.
8 #include "base/files/file_util.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "chrome/browser/drive/drive_uploader.h"
13 #include "chrome/browser/drive/fake_drive_service.h"
14 #include "chrome/browser/drive/test_util.h"
15 #include "chrome/browser/sync_file_system/drive_backend/callback_helper.h"
16 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.h"
17 #include "chrome/browser/sync_file_system/drive_backend/fake_drive_service_helper.h"
18 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
19 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
20 #include "chrome/browser/sync_file_system/drive_backend/sync_engine.h"
21 #include "chrome/browser/sync_file_system/drive_backend/sync_engine_context.h"
22 #include "chrome/browser/sync_file_system/drive_backend/sync_worker.h"
23 #include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h"
24 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h"
25 #include "chrome/browser/sync_file_system/local/local_file_sync_service.h"
26 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h"
27 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h"
28 #include "chrome/browser/sync_file_system/syncable_file_system_util.h"
29 #include "chrome/test/base/testing_profile.h"
30 #include "content/public/test/test_browser_thread.h"
31 #include "content/public/test/test_browser_thread_bundle.h"
32 #include "content/public/test/test_utils.h"
33 #include "extensions/common/extension.h"
34 #include "google_apis/drive/drive_api_parser.h"
35 #include "net/url_request/url_request_context_getter.h"
36 #include "storage/browser/fileapi/file_system_context.h"
37 #include "testing/gtest/include/gtest/gtest.h"
38 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
39 #include "third_party/leveldatabase/src/include/leveldb/env.h"
41 #define FPL(a) FILE_PATH_LITERAL(a)
43 namespace sync_file_system {
44 namespace drive_backend {
46 typedef storage::FileSystemOperation::FileEntryList FileEntryList;
51 void SetValueAndCallClosure(const base::Closure& closure,
54 *arg_out = base::internal::CallbackForward(arg);
58 void SetSyncStatusAndUrl(const base::Closure& closure,
59 SyncStatusCode* status_out,
60 storage::FileSystemURL* url_out,
61 SyncStatusCode status,
62 const storage::FileSystemURL& url) {
70 class DriveBackendSyncTest : public testing::Test,
71 public LocalFileSyncService::Observer,
72 public RemoteFileSyncService::Observer {
74 DriveBackendSyncTest()
75 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
76 pending_remote_changes_(0),
77 pending_local_changes_(0) {}
78 virtual ~DriveBackendSyncTest() {}
80 virtual void SetUp() OVERRIDE {
81 ASSERT_TRUE(base_dir_.CreateUniqueTempDir());
82 in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
84 io_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread(
85 content::BrowserThread::IO);
86 scoped_refptr<base::SequencedWorkerPool> worker_pool(
87 content::BrowserThread::GetBlockingPool());
89 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
90 worker_pool->GetSequenceToken(),
91 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
92 file_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread(
93 content::BrowserThread::FILE);
94 scoped_refptr<base::SequencedTaskRunner> drive_task_runner =
95 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
96 worker_pool->GetSequenceToken(),
97 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
99 RegisterSyncableFileSystem();
100 local_sync_service_ = LocalFileSyncService::CreateForTesting(
101 &profile_, in_memory_env_.get());
102 local_sync_service_->AddChangeObserver(this);
104 scoped_ptr<drive::FakeDriveService>
105 drive_service(new drive::FakeDriveService);
106 drive_service->Initialize("test@example.com");
107 ASSERT_TRUE(drive::test_util::SetUpTestEntries(drive_service.get()));
109 scoped_ptr<drive::DriveUploaderInterface> uploader(
110 new drive::DriveUploader(drive_service.get(),
111 file_task_runner_.get()));
113 fake_drive_service_helper_.reset(new FakeDriveServiceHelper(
114 drive_service.get(), uploader.get(),
115 kSyncRootFolderTitle));
117 remote_sync_service_.reset(
118 new SyncEngine(base::ThreadTaskRunnerHandle::Get(), // ui_task_runner
119 worker_task_runner_.get(),
120 drive_task_runner.get(),
123 NULL, // notification_manager
124 NULL, // extension_service
125 NULL, // signin_manager
126 NULL, // token_service
127 NULL, // request_context
128 scoped_ptr<SyncEngine::DriveServiceFactory>(),
129 in_memory_env_.get()));
130 remote_sync_service_->AddServiceObserver(this);
131 remote_sync_service_->InitializeForTesting(
132 drive_service.PassAs<drive::DriveServiceInterface>(),
134 scoped_ptr<SyncWorkerInterface>());
135 remote_sync_service_->SetSyncEnabled(true);
137 local_sync_service_->SetLocalChangeProcessor(remote_sync_service_.get());
138 remote_sync_service_->SetRemoteChangeProcessor(local_sync_service_.get());
141 virtual void TearDown() OVERRIDE {
142 typedef std::map<std::string, CannedSyncableFileSystem*>::iterator iterator;
143 for (iterator itr = file_systems_.begin();
144 itr != file_systems_.end(); ++itr) {
145 itr->second->TearDown();
148 file_systems_.clear();
150 local_sync_service_->Shutdown();
152 fake_drive_service_helper_.reset();
153 local_sync_service_.reset();
154 remote_sync_service_.reset();
156 content::RunAllBlockingPoolTasksUntilIdle();
157 RevokeSyncableFileSystem();
160 virtual void OnRemoteChangeQueueUpdated(int64 pending_changes_hint) OVERRIDE {
161 pending_remote_changes_ = pending_changes_hint;
164 virtual void OnLocalChangeAvailable(int64 pending_changes_hint) OVERRIDE {
165 pending_local_changes_ = pending_changes_hint;
169 storage::FileSystemURL CreateURL(const std::string& app_id,
170 const base::FilePath::StringType& path) {
171 return CreateURL(app_id, base::FilePath(path));
174 storage::FileSystemURL CreateURL(const std::string& app_id,
175 const base::FilePath& path) {
176 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id);
177 return CreateSyncableFileSystemURL(origin, path);
180 bool GetAppRootFolderID(const std::string& app_id,
181 std::string* folder_id) {
182 base::RunLoop run_loop;
183 bool success = false;
185 PostTaskAndReplyWithResult(
186 worker_task_runner_.get(),
188 base::Bind(&MetadataDatabase::FindAppRootTracker,
189 base::Unretained(metadata_database()),
193 &SetValueAndCallClosure<bool>, run_loop.QuitClosure(), &success));
197 *folder_id = tracker.file_id();
201 std::string GetFileIDByPath(const std::string& app_id,
202 const base::FilePath::StringType& path) {
203 return GetFileIDByPath(app_id, base::FilePath(path));
206 std::string GetFileIDByPath(const std::string& app_id,
207 const base::FilePath& path) {
208 base::RunLoop run_loop;
209 bool success = false;
211 base::FilePath result_path;
212 base::FilePath normalized_path = path.NormalizePathSeparators();
213 PostTaskAndReplyWithResult(
214 worker_task_runner_.get(),
216 base::Bind(&MetadataDatabase::FindNearestActiveAncestor,
217 base::Unretained(metadata_database()),
223 &SetValueAndCallClosure<bool>, run_loop.QuitClosure(), &success));
225 EXPECT_TRUE(success);
226 EXPECT_EQ(normalized_path, result_path);
227 return tracker.file_id();
230 SyncStatusCode RegisterApp(const std::string& app_id) {
231 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id);
232 if (!ContainsKey(file_systems_, app_id)) {
233 CannedSyncableFileSystem* file_system = new CannedSyncableFileSystem(
234 origin, in_memory_env_.get(),
235 io_task_runner_.get(), file_task_runner_.get());
236 file_system->SetUp(CannedSyncableFileSystem::QUOTA_DISABLED);
238 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
239 base::RunLoop run_loop;
240 local_sync_service_->MaybeInitializeFileSystemContext(
241 origin, file_system->file_system_context(),
242 base::Bind(&SetValueAndCallClosure<SyncStatusCode>,
243 run_loop.QuitClosure(), &status));
245 EXPECT_EQ(SYNC_STATUS_OK, status);
247 file_system->backend()->sync_context()->
248 set_mock_notify_changes_duration_in_sec(0);
250 EXPECT_EQ(base::File::FILE_OK, file_system->OpenFileSystem());
251 file_systems_[app_id] = file_system;
254 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
255 base::RunLoop run_loop;
256 remote_sync_service_->RegisterOrigin(
258 base::Bind(&SetValueAndCallClosure<SyncStatusCode>,
259 run_loop.QuitClosure(), &status));
264 void AddLocalFolder(const std::string& app_id,
265 const base::FilePath::StringType& path) {
266 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
267 EXPECT_EQ(base::File::FILE_OK,
268 file_systems_[app_id]->CreateDirectory(
269 CreateURL(app_id, path)));
272 void AddOrUpdateLocalFile(const std::string& app_id,
273 const base::FilePath::StringType& path,
274 const std::string& content) {
275 storage::FileSystemURL url(CreateURL(app_id, path));
276 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
277 EXPECT_EQ(base::File::FILE_OK, file_systems_[app_id]->CreateFile(url));
278 int64 bytes_written = file_systems_[app_id]->WriteString(url, content);
279 EXPECT_EQ(static_cast<int64>(content.size()), bytes_written);
280 base::RunLoop().RunUntilIdle();
283 void UpdateLocalFile(const std::string& app_id,
284 const base::FilePath::StringType& path,
285 const std::string& content) {
286 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
287 int64 bytes_written = file_systems_[app_id]->WriteString(
288 CreateURL(app_id, path), content);
289 EXPECT_EQ(static_cast<int64>(content.size()), bytes_written);
290 base::RunLoop().RunUntilIdle();
293 void RemoveLocal(const std::string& app_id,
294 const base::FilePath::StringType& path) {
295 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
296 EXPECT_EQ(base::File::FILE_OK,
297 file_systems_[app_id]->Remove(
298 CreateURL(app_id, path),
299 true /* recursive */));
300 base::RunLoop().RunUntilIdle();
303 SyncStatusCode ProcessLocalChange() {
304 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
305 storage::FileSystemURL url;
306 base::RunLoop run_loop;
307 local_sync_service_->ProcessLocalChange(base::Bind(
308 &SetSyncStatusAndUrl, run_loop.QuitClosure(), &status, &url));
313 SyncStatusCode ProcessRemoteChange() {
314 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
315 storage::FileSystemURL url;
316 base::RunLoop run_loop;
317 remote_sync_service_->ProcessRemoteChange(base::Bind(
318 &SetSyncStatusAndUrl, run_loop.QuitClosure(), &status, &url));
323 int64 GetLargestChangeID() {
324 scoped_ptr<google_apis::AboutResource> about_resource;
325 EXPECT_EQ(google_apis::HTTP_SUCCESS,
326 fake_drive_service_helper()->GetAboutResource(&about_resource));
329 return about_resource->largest_change_id();
332 void FetchRemoteChanges() {
333 remote_sync_service_->OnNotificationReceived();
337 SyncStatusCode ProcessChangesUntilDone() {
338 int task_limit = 100;
339 SyncStatusCode local_sync_status;
340 SyncStatusCode remote_sync_status;
342 base::RunLoop().RunUntilIdle();
346 return SYNC_STATUS_ABORT;
348 local_sync_status = ProcessLocalChange();
349 if (local_sync_status != SYNC_STATUS_OK &&
350 local_sync_status != SYNC_STATUS_NO_CHANGE_TO_SYNC &&
351 local_sync_status != SYNC_STATUS_FILE_BUSY)
352 return local_sync_status;
354 remote_sync_status = ProcessRemoteChange();
355 if (remote_sync_status != SYNC_STATUS_OK &&
356 remote_sync_status != SYNC_STATUS_NO_CHANGE_TO_SYNC &&
357 remote_sync_status != SYNC_STATUS_FILE_BUSY)
358 return remote_sync_status;
360 if (local_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC &&
361 remote_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC) {
363 base::RunLoop run_loop;
364 remote_sync_service_->PromoteDemotedChanges(run_loop.QuitClosure());
369 base::RunLoop run_loop;
370 local_sync_service_->PromoteDemotedChanges(run_loop.QuitClosure());
374 if (pending_remote_changes_ || pending_local_changes_)
377 base::RunLoop run_loop;
378 int64 largest_fetched_change_id = -1;
379 PostTaskAndReplyWithResult(
380 worker_task_runner_.get(),
382 base::Bind(&MetadataDatabase::GetLargestFetchedChangeID,
383 base::Unretained(metadata_database())),
384 base::Bind(&SetValueAndCallClosure<int64>,
385 run_loop.QuitClosure(),
386 &largest_fetched_change_id));
388 if (largest_fetched_change_id != GetLargestChangeID()) {
389 FetchRemoteChanges();
395 return SYNC_STATUS_OK;
398 // Verifies local and remote files/folders are consistent.
399 // This function checks:
400 // - Each registered origin has corresponding remote folder.
401 // - Each local file/folder has corresponding remote one.
402 // - Each remote file/folder has corresponding local one.
403 // TODO(tzik): Handle conflict case. i.e. allow remote file has different
404 // file content if the corresponding local file conflicts to it.
405 void VerifyConsistency() {
406 std::string sync_root_folder_id;
407 google_apis::GDataErrorCode error =
408 fake_drive_service_helper_->GetSyncRootFolderID(&sync_root_folder_id);
409 if (sync_root_folder_id.empty()) {
410 EXPECT_EQ(google_apis::HTTP_NOT_FOUND, error);
411 EXPECT_TRUE(file_systems_.empty());
414 EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
416 ScopedVector<google_apis::ResourceEntry> remote_entries;
417 EXPECT_EQ(google_apis::HTTP_SUCCESS,
418 fake_drive_service_helper_->ListFilesInFolder(
419 sync_root_folder_id, &remote_entries));
420 std::map<std::string, const google_apis::ResourceEntry*> app_root_by_title;
421 for (ScopedVector<google_apis::ResourceEntry>::iterator itr =
422 remote_entries.begin();
423 itr != remote_entries.end();
425 const google_apis::ResourceEntry& remote_entry = **itr;
426 EXPECT_FALSE(ContainsKey(app_root_by_title, remote_entry.title()));
427 app_root_by_title[remote_entry.title()] = *itr;
430 for (std::map<std::string, CannedSyncableFileSystem*>::const_iterator itr =
431 file_systems_.begin();
432 itr != file_systems_.end(); ++itr) {
433 const std::string& app_id = itr->first;
434 SCOPED_TRACE(testing::Message() << "Verifying app: " << app_id);
435 CannedSyncableFileSystem* file_system = itr->second;
436 ASSERT_TRUE(ContainsKey(app_root_by_title, app_id));
437 VerifyConsistencyForFolder(
438 app_id, base::FilePath(),
439 app_root_by_title[app_id]->resource_id(),
444 void VerifyConsistencyForFolder(const std::string& app_id,
445 const base::FilePath& path,
446 const std::string& folder_id,
447 CannedSyncableFileSystem* file_system) {
448 SCOPED_TRACE(testing::Message() << "Verifying folder: " << path.value());
450 ScopedVector<google_apis::ResourceEntry> remote_entries;
451 EXPECT_EQ(google_apis::HTTP_SUCCESS,
452 fake_drive_service_helper_->ListFilesInFolder(
453 folder_id, &remote_entries));
454 std::map<std::string, const google_apis::ResourceEntry*>
455 remote_entry_by_title;
456 for (size_t i = 0; i < remote_entries.size(); ++i) {
457 google_apis::ResourceEntry* remote_entry = remote_entries[i];
458 EXPECT_FALSE(ContainsKey(remote_entry_by_title, remote_entry->title()))
459 << "title: " << remote_entry->title();
460 remote_entry_by_title[remote_entry->title()] = remote_entry;
463 storage::FileSystemURL url(CreateURL(app_id, path));
464 FileEntryList local_entries;
465 EXPECT_EQ(base::File::FILE_OK,
466 file_system->ReadDirectory(url, &local_entries));
467 for (FileEntryList::iterator itr = local_entries.begin();
468 itr != local_entries.end();
470 const storage::DirectoryEntry& local_entry = *itr;
471 storage::FileSystemURL entry_url(
472 CreateURL(app_id, path.Append(local_entry.name)));
474 storage::VirtualPath::BaseName(entry_url.path()).AsUTF8Unsafe();
475 SCOPED_TRACE(testing::Message() << "Verifying entry: " << title);
477 ASSERT_TRUE(ContainsKey(remote_entry_by_title, title));
478 const google_apis::ResourceEntry& remote_entry =
479 *remote_entry_by_title[title];
480 if (local_entry.is_directory) {
481 ASSERT_TRUE(remote_entry.is_folder());
482 VerifyConsistencyForFolder(app_id, entry_url.path(),
483 remote_entry.resource_id(),
486 ASSERT_TRUE(remote_entry.is_file());
487 VerifyConsistencyForFile(app_id, entry_url.path(),
488 remote_entry.resource_id(),
491 remote_entry_by_title.erase(title);
494 EXPECT_TRUE(remote_entry_by_title.empty());
497 void VerifyConsistencyForFile(const std::string& app_id,
498 const base::FilePath& path,
499 const std::string& file_id,
500 CannedSyncableFileSystem* file_system) {
501 storage::FileSystemURL url(CreateURL(app_id, path));
502 std::string file_content;
503 EXPECT_EQ(google_apis::HTTP_SUCCESS,
504 fake_drive_service_helper_->ReadFile(file_id, &file_content));
505 EXPECT_EQ(base::File::FILE_OK,
506 file_system->VerifyFile(url, file_content));
510 return file_systems_.size();
513 size_t CountLocalFile(const std::string& app_id) {
514 if (!ContainsKey(file_systems_, app_id))
517 CannedSyncableFileSystem* file_system = file_systems_[app_id];
518 std::stack<base::FilePath> folders;
519 folders.push(base::FilePath()); // root folder
522 while (!folders.empty()) {
523 storage::FileSystemURL url(CreateURL(app_id, folders.top()));
526 FileEntryList entries;
527 EXPECT_EQ(base::File::FILE_OK,
528 file_system->ReadDirectory(url, &entries));
529 for (FileEntryList::iterator itr = entries.begin();
530 itr != entries.end(); ++itr) {
532 if (itr->is_directory)
533 folders.push(url.path().Append(itr->name));
540 void VerifyLocalFile(const std::string& app_id,
541 const base::FilePath::StringType& path,
542 const std::string& content) {
543 SCOPED_TRACE(testing::Message() << "Verifying local file: "
544 << "app_id = " << app_id
545 << ", path = " << path);
546 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
547 EXPECT_EQ(base::File::FILE_OK,
548 file_systems_[app_id]->VerifyFile(
549 CreateURL(app_id, path), content));
552 void VerifyLocalFolder(const std::string& app_id,
553 const base::FilePath::StringType& path) {
554 SCOPED_TRACE(testing::Message() << "Verifying local file: "
555 << "app_id = " << app_id
556 << ", path = " << path);
557 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
558 EXPECT_EQ(base::File::FILE_OK,
559 file_systems_[app_id]->DirectoryExists(CreateURL(app_id, path)));
562 size_t CountMetadata() {
564 base::RunLoop run_loop;
565 PostTaskAndReplyWithResult(
566 worker_task_runner_.get(),
568 base::Bind(&MetadataDatabase::CountFileMetadata,
569 base::Unretained(metadata_database())),
571 &SetValueAndCallClosure<size_t>, run_loop.QuitClosure(), &count));
576 size_t CountTracker() {
578 base::RunLoop run_loop;
579 PostTaskAndReplyWithResult(
580 worker_task_runner_.get(),
582 base::Bind(&MetadataDatabase::CountFileTracker,
583 base::Unretained(metadata_database())),
585 &SetValueAndCallClosure<size_t>, run_loop.QuitClosure(), &count));
590 drive::FakeDriveService* fake_drive_service() {
591 return static_cast<drive::FakeDriveService*>(
592 remote_sync_service_->drive_service_.get());
595 FakeDriveServiceHelper* fake_drive_service_helper() {
596 return fake_drive_service_helper_.get();
599 void WaitForIdleWorker() {
600 base::RunLoop run_loop;
601 worker_task_runner_->PostTask(
603 base::Bind(&SyncWorker::CallOnIdleForTesting,
604 base::Unretained(sync_worker()),
605 RelayCallbackToCurrentThread(
607 run_loop.QuitClosure())));
612 SyncWorker* sync_worker() {
613 return static_cast<SyncWorker*>(remote_sync_service_->sync_worker_.get());
616 // MetadataDatabase is normally used on the worker thread.
617 // Use this only when there is no task running on the worker.
618 MetadataDatabase* metadata_database() {
619 return sync_worker()->context_->metadata_database_.get();
622 content::TestBrowserThreadBundle thread_bundle_;
624 base::ScopedTempDir base_dir_;
625 scoped_ptr<leveldb::Env> in_memory_env_;
626 TestingProfile profile_;
628 scoped_ptr<SyncEngine> remote_sync_service_;
629 scoped_ptr<LocalFileSyncService> local_sync_service_;
631 int64 pending_remote_changes_;
632 int64 pending_local_changes_;
634 scoped_ptr<FakeDriveServiceHelper> fake_drive_service_helper_;
635 std::map<std::string, CannedSyncableFileSystem*> file_systems_;
638 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
639 scoped_refptr<base::SequencedTaskRunner> worker_task_runner_;
640 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
642 DISALLOW_COPY_AND_ASSIGN(DriveBackendSyncTest);
645 TEST_F(DriveBackendSyncTest, LocalToRemoteBasicTest) {
646 std::string app_id = "example";
649 AddOrUpdateLocalFile(app_id, FPL("file"), "abcde");
651 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
654 EXPECT_EQ(1u, CountApp());
655 EXPECT_EQ(2u, CountLocalFile(app_id));
656 VerifyLocalFile(app_id, FPL("file"), "abcde");
658 EXPECT_EQ(3u, CountMetadata());
659 EXPECT_EQ(3u, CountTracker());
662 TEST_F(DriveBackendSyncTest, RemoteToLocalBasicTest) {
663 std::string app_id = "example";
666 std::string app_root_folder_id;
667 EXPECT_TRUE(GetAppRootFolderID(app_id, &app_root_folder_id));
670 EXPECT_EQ(google_apis::HTTP_SUCCESS,
671 fake_drive_service_helper()->AddFile(
672 app_root_folder_id, "file", "abcde", &file_id));
674 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
677 EXPECT_EQ(1u, CountApp());
678 EXPECT_EQ(2u, CountLocalFile(app_id));
679 VerifyLocalFile(app_id, FPL("file"), "abcde");
681 EXPECT_EQ(3u, CountMetadata());
682 EXPECT_EQ(3u, CountTracker());
685 TEST_F(DriveBackendSyncTest, LocalFileUpdateTest) {
686 std::string app_id = "example";
687 const base::FilePath::StringType kPath(FPL("file"));
690 AddOrUpdateLocalFile(app_id, kPath, "abcde");
692 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
695 UpdateLocalFile(app_id, kPath, "1234567890");
697 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
700 EXPECT_EQ(1u, CountApp());
701 EXPECT_EQ(2u, CountLocalFile(app_id));
702 VerifyLocalFile(app_id, FPL("file"), "1234567890");
704 EXPECT_EQ(3u, CountMetadata());
705 EXPECT_EQ(3u, CountTracker());
708 TEST_F(DriveBackendSyncTest, RemoteFileUpdateTest) {
709 std::string app_id = "example";
712 std::string remote_file_id;
713 std::string app_root_folder_id;
714 EXPECT_TRUE(GetAppRootFolderID(app_id, &app_root_folder_id));
715 EXPECT_EQ(google_apis::HTTP_SUCCESS,
716 fake_drive_service_helper()->AddFile(
717 app_root_folder_id, "file", "abcde", &remote_file_id));
719 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
722 EXPECT_EQ(google_apis::HTTP_SUCCESS,
723 fake_drive_service_helper()->UpdateFile(
724 remote_file_id, "1234567890"));
726 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
729 EXPECT_EQ(1u, CountApp());
730 EXPECT_EQ(2u, CountLocalFile(app_id));
731 VerifyLocalFile(app_id, FPL("file"), "1234567890");
733 EXPECT_EQ(3u, CountMetadata());
734 EXPECT_EQ(3u, CountTracker());
737 TEST_F(DriveBackendSyncTest, LocalFileDeletionTest) {
738 std::string app_id = "example";
739 const base::FilePath::StringType path(FPL("file"));
742 AddOrUpdateLocalFile(app_id, path, "abcde");
744 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
747 RemoveLocal(app_id, path);
749 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
752 EXPECT_EQ(1u, CountApp());
753 EXPECT_EQ(1u, CountLocalFile(app_id));
755 EXPECT_EQ(2u, CountMetadata());
756 EXPECT_EQ(2u, CountTracker());
759 TEST_F(DriveBackendSyncTest, RemoteFileDeletionTest) {
760 std::string app_id = "example";
761 const base::FilePath::StringType path(FPL("file"));
764 AddOrUpdateLocalFile(app_id, path, "abcde");
766 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
769 std::string file_id = GetFileIDByPath(app_id, path);
770 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
771 fake_drive_service_helper()->DeleteResource(file_id));
773 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
776 EXPECT_EQ(1u, CountApp());
777 EXPECT_EQ(1u, CountLocalFile(app_id));
779 EXPECT_EQ(2u, CountMetadata());
780 EXPECT_EQ(2u, CountTracker());
783 TEST_F(DriveBackendSyncTest, RemoteRenameTest) {
784 std::string app_id = "example";
785 const base::FilePath::StringType path(FPL("file"));
788 AddOrUpdateLocalFile(app_id, path, "abcde");
790 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
793 std::string file_id = GetFileIDByPath(app_id, path);
794 EXPECT_EQ(google_apis::HTTP_SUCCESS,
795 fake_drive_service_helper()->RenameResource(
796 file_id, "renamed_file"));
798 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
801 EXPECT_EQ(1u, CountApp());
802 EXPECT_EQ(2u, CountLocalFile(app_id));
803 VerifyLocalFile(app_id, FPL("renamed_file"), "abcde");
805 EXPECT_EQ(3u, CountMetadata());
806 EXPECT_EQ(3u, CountTracker());
809 TEST_F(DriveBackendSyncTest, RemoteRenameAndRevertTest) {
810 std::string app_id = "example";
811 const base::FilePath::StringType path(FPL("file"));
814 AddOrUpdateLocalFile(app_id, path, "abcde");
816 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
819 std::string file_id = GetFileIDByPath(app_id, path);
820 EXPECT_EQ(google_apis::HTTP_SUCCESS,
821 fake_drive_service_helper()->RenameResource(
822 file_id, "renamed_file"));
824 FetchRemoteChanges();
826 EXPECT_EQ(google_apis::HTTP_SUCCESS,
827 fake_drive_service_helper()->RenameResource(
828 file_id, base::FilePath(path).AsUTF8Unsafe()));
830 FetchRemoteChanges();
832 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
835 EXPECT_EQ(1u, CountApp());
836 EXPECT_EQ(2u, CountLocalFile(app_id));
837 VerifyLocalFile(app_id, FPL("file"), "abcde");
839 EXPECT_EQ(3u, CountMetadata());
840 EXPECT_EQ(3u, CountTracker());
843 TEST_F(DriveBackendSyncTest, ReorganizeToOtherFolder) {
844 std::string app_id = "example";
845 const base::FilePath::StringType path(FPL("file"));
848 AddLocalFolder(app_id, FPL("folder_src"));
849 AddLocalFolder(app_id, FPL("folder_dest"));
850 AddOrUpdateLocalFile(app_id, FPL("folder_src/file"), "abcde");
852 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
855 std::string file_id = GetFileIDByPath(app_id, FPL("folder_src/file"));
856 std::string src_folder_id = GetFileIDByPath(app_id, FPL("folder_src"));
857 std::string dest_folder_id = GetFileIDByPath(app_id, FPL("folder_dest"));
858 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
859 fake_drive_service_helper()->RemoveResourceFromDirectory(
860 src_folder_id, file_id));
861 EXPECT_EQ(google_apis::HTTP_SUCCESS,
862 fake_drive_service_helper()->AddResourceToDirectory(
863 dest_folder_id, file_id));
865 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
868 EXPECT_EQ(1u, CountApp());
869 EXPECT_EQ(4u, CountLocalFile(app_id));
870 VerifyLocalFolder(app_id, FPL("folder_dest"));
871 VerifyLocalFile(app_id, FPL("folder_dest/file"), "abcde");
873 EXPECT_EQ(5u, CountMetadata());
874 EXPECT_EQ(5u, CountTracker());
877 TEST_F(DriveBackendSyncTest, ReorganizeToOtherApp) {
878 std::string src_app_id = "src_app";
879 std::string dest_app_id = "dest_app";
881 RegisterApp(src_app_id);
882 RegisterApp(dest_app_id);
884 AddLocalFolder(src_app_id, FPL("folder_src"));
885 AddLocalFolder(dest_app_id, FPL("folder_dest"));
886 AddOrUpdateLocalFile(src_app_id, FPL("folder_src/file"), "abcde");
888 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
891 std::string file_id = GetFileIDByPath(src_app_id, FPL("folder_src/file"));
892 std::string src_folder_id = GetFileIDByPath(src_app_id, FPL("folder_src"));
893 std::string dest_folder_id = GetFileIDByPath(dest_app_id, FPL("folder_dest"));
894 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
895 fake_drive_service_helper()->RemoveResourceFromDirectory(
896 src_folder_id, file_id));
897 EXPECT_EQ(google_apis::HTTP_SUCCESS,
898 fake_drive_service_helper()->AddResourceToDirectory(
899 dest_folder_id, file_id));
901 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
904 EXPECT_EQ(2u, CountApp());
905 EXPECT_EQ(2u, CountLocalFile(src_app_id));
906 EXPECT_EQ(3u, CountLocalFile(dest_app_id));
907 VerifyLocalFile(dest_app_id, FPL("folder_dest/file"), "abcde");
909 EXPECT_EQ(6u, CountMetadata());
910 EXPECT_EQ(6u, CountTracker());
913 TEST_F(DriveBackendSyncTest, ReorganizeToUnmanagedArea) {
914 std::string app_id = "example";
918 AddLocalFolder(app_id, FPL("folder_src"));
919 AddOrUpdateLocalFile(app_id, FPL("folder_src/file_orphaned"), "abcde");
920 AddOrUpdateLocalFile(app_id, FPL("folder_src/file_under_sync_root"), "123");
921 AddOrUpdateLocalFile(app_id, FPL("folder_src/file_under_drive_root"), "hoge");
923 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
926 std::string file_orphaned_id =
927 GetFileIDByPath(app_id, FPL("folder_src/file_orphaned"));
928 std::string file_under_sync_root_id =
929 GetFileIDByPath(app_id, FPL("folder_src/file_under_sync_root"));
930 std::string file_under_drive_root_id =
931 GetFileIDByPath(app_id, FPL("folder_src/file_under_drive_root"));
933 std::string folder_id = GetFileIDByPath(app_id, FPL("folder_src"));
934 std::string sync_root_folder_id;
935 EXPECT_EQ(google_apis::HTTP_SUCCESS,
936 fake_drive_service_helper()->GetSyncRootFolderID(
937 &sync_root_folder_id));
939 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
940 fake_drive_service_helper()->RemoveResourceFromDirectory(
941 folder_id, file_orphaned_id));
942 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
943 fake_drive_service_helper()->RemoveResourceFromDirectory(
944 folder_id, file_under_sync_root_id));
945 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
946 fake_drive_service_helper()->RemoveResourceFromDirectory(
947 folder_id, file_under_drive_root_id));
949 EXPECT_EQ(google_apis::HTTP_SUCCESS,
950 fake_drive_service_helper()->AddResourceToDirectory(
951 sync_root_folder_id, file_under_sync_root_id));
952 EXPECT_EQ(google_apis::HTTP_SUCCESS,
953 fake_drive_service_helper()->AddResourceToDirectory(
954 fake_drive_service()->GetRootResourceId(),
955 file_under_drive_root_id));
957 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
960 EXPECT_EQ(1u, CountApp());
961 EXPECT_EQ(2u, CountLocalFile(app_id));
963 EXPECT_EQ(4u, CountMetadata());
964 EXPECT_EQ(4u, CountTracker());
967 TEST_F(DriveBackendSyncTest, ReorganizeToMultipleParents) {
968 std::string app_id = "example";
972 AddLocalFolder(app_id, FPL("parent1"));
973 AddLocalFolder(app_id, FPL("parent2"));
974 AddOrUpdateLocalFile(app_id, FPL("parent1/file"), "abcde");
976 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
979 std::string file_id = GetFileIDByPath(app_id, FPL("parent1/file"));
980 std::string parent2_folder_id = GetFileIDByPath(app_id, FPL("parent2"));
981 EXPECT_EQ(google_apis::HTTP_SUCCESS,
982 fake_drive_service_helper()->AddResourceToDirectory(
983 parent2_folder_id, file_id));
985 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
988 EXPECT_EQ(1u, CountApp());
989 EXPECT_EQ(4u, CountLocalFile(app_id));
990 VerifyLocalFolder(app_id, FPL("parent1"));
991 VerifyLocalFolder(app_id, FPL("parent2"));
992 VerifyLocalFile(app_id, FPL("parent1/file"), "abcde");
994 EXPECT_EQ(5u, CountMetadata());
995 EXPECT_EQ(5u, CountTracker());
998 TEST_F(DriveBackendSyncTest, ReorganizeAndRevert) {
999 std::string app_id = "example";
1001 RegisterApp(app_id);
1003 AddLocalFolder(app_id, FPL("folder"));
1004 AddLocalFolder(app_id, FPL("folder_temp"));
1005 AddOrUpdateLocalFile(app_id, FPL("folder/file"), "abcde");
1007 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1008 VerifyConsistency();
1010 std::string file_id = GetFileIDByPath(app_id, FPL("folder/file"));
1011 std::string folder_id = GetFileIDByPath(app_id, FPL("folder"));
1012 std::string folder_temp_id = GetFileIDByPath(app_id, FPL("folder_temp"));
1013 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1014 fake_drive_service_helper()->RemoveResourceFromDirectory(
1015 folder_id, file_id));
1016 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1017 fake_drive_service_helper()->AddResourceToDirectory(
1018 folder_temp_id, file_id));
1020 FetchRemoteChanges();
1022 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1023 fake_drive_service_helper()->RemoveResourceFromDirectory(
1024 folder_temp_id, file_id));
1025 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1026 fake_drive_service_helper()->AddResourceToDirectory(
1027 folder_id, file_id));
1029 FetchRemoteChanges();
1031 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1032 VerifyConsistency();
1034 EXPECT_EQ(1u, CountApp());
1035 EXPECT_EQ(4u, CountLocalFile(app_id));
1036 VerifyLocalFolder(app_id, FPL("folder"));
1037 VerifyLocalFile(app_id, FPL("folder/file"), "abcde");
1039 EXPECT_EQ(5u, CountMetadata());
1040 EXPECT_EQ(5u, CountTracker());
1043 TEST_F(DriveBackendSyncTest, ConflictTest_ConflictTest_AddFolder_AddFolder) {
1044 std::string app_id = "example";
1046 RegisterApp(app_id);
1047 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1049 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1050 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1052 std::string remote_folder_id;
1053 EXPECT_EQ(google_apis::HTTP_CREATED,
1054 fake_drive_service_helper()->AddFolder(
1056 "conflict_to_pending_remote", &remote_folder_id));
1058 FetchRemoteChanges();
1060 EXPECT_EQ(google_apis::HTTP_CREATED,
1061 fake_drive_service_helper()->AddFolder(
1063 "conflict_to_existing_remote", &remote_folder_id));
1065 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1066 VerifyConsistency();
1068 EXPECT_EQ(1u, CountApp());
1069 EXPECT_EQ(3u, CountLocalFile(app_id));
1070 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1071 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1073 EXPECT_EQ(4u, CountMetadata());
1074 EXPECT_EQ(4u, CountTracker());
1077 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_DeleteFolder) {
1078 std::string app_id = "example";
1080 RegisterApp(app_id);
1082 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1083 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1085 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1086 VerifyConsistency();
1088 // Test body starts from here.
1089 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1090 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1091 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1092 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1094 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1095 fake_drive_service_helper()->DeleteResource(
1096 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1098 FetchRemoteChanges();
1100 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1101 fake_drive_service_helper()->DeleteResource(
1102 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1104 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1105 VerifyConsistency();
1107 EXPECT_EQ(1u, CountApp());
1108 EXPECT_EQ(2u, CountLocalFile(app_id));
1109 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1111 EXPECT_EQ(3u, CountMetadata());
1112 EXPECT_EQ(3u, CountTracker());
1115 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_AddFile) {
1116 std::string app_id = "example";
1118 RegisterApp(app_id);
1119 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1121 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1122 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1124 std::string file_id;
1125 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1126 fake_drive_service_helper()->AddFile(
1127 app_root_folder_id, "conflict_to_pending_remote", "foo",
1129 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1130 fake_drive_service_helper()->UpdateModificationTime(
1132 base::Time::Now() + base::TimeDelta::FromDays(1)));
1134 FetchRemoteChanges();
1136 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1137 fake_drive_service_helper()->AddFile(
1138 app_root_folder_id, "conflict_to_existing_remote", "foo",
1140 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1141 fake_drive_service_helper()->UpdateModificationTime(
1143 base::Time::Now() + base::TimeDelta::FromDays(1)));
1145 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1146 VerifyConsistency();
1148 EXPECT_EQ(1u, CountApp());
1149 EXPECT_EQ(3u, CountLocalFile(app_id));
1150 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1151 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1153 EXPECT_EQ(4u, CountMetadata());
1154 EXPECT_EQ(4u, CountTracker());
1157 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_DeleteFile) {
1158 std::string app_id = "example";
1160 RegisterApp(app_id);
1161 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1163 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1164 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1166 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1167 VerifyConsistency();
1169 // Test body starts from here.
1170 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1171 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1173 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1174 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1176 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1177 fake_drive_service_helper()->DeleteResource(
1178 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1180 FetchRemoteChanges();
1182 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1183 fake_drive_service_helper()->DeleteResource(
1184 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1186 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1187 VerifyConsistency();
1189 EXPECT_EQ(1u, CountApp());
1190 EXPECT_EQ(3u, CountLocalFile(app_id));
1191 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1192 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1194 EXPECT_EQ(4u, CountMetadata());
1195 EXPECT_EQ(4u, CountTracker());
1198 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_AddFolder) {
1199 std::string app_id = "example";
1201 RegisterApp(app_id);
1202 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1203 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1204 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1206 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1207 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1209 std::string file_id;
1210 EXPECT_EQ(google_apis::HTTP_CREATED,
1211 fake_drive_service_helper()->AddFolder(
1213 "conflict_to_pending_remote", &file_id));
1215 FetchRemoteChanges();
1217 EXPECT_EQ(google_apis::HTTP_CREATED,
1218 fake_drive_service_helper()->AddFolder(
1220 "conflict_to_existing_remote", NULL));
1222 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1223 VerifyConsistency();
1225 EXPECT_EQ(1u, CountApp());
1226 EXPECT_EQ(3u, CountLocalFile(app_id));
1227 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1228 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1230 EXPECT_EQ(4u, CountMetadata());
1231 EXPECT_EQ(4u, CountTracker());
1234 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_DeleteFolder) {
1235 std::string app_id = "example";
1237 RegisterApp(app_id);
1238 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1240 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1241 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1243 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1244 VerifyConsistency();
1246 // Test body starts from here.
1247 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1248 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1250 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1251 fake_drive_service_helper()->DeleteResource(
1252 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1254 FetchRemoteChanges();
1256 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1257 fake_drive_service_helper()->DeleteResource(
1258 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1260 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1261 VerifyConsistency();
1263 EXPECT_EQ(1u, CountApp());
1264 EXPECT_EQ(1u, CountLocalFile(app_id));
1266 EXPECT_EQ(2u, CountMetadata());
1267 EXPECT_EQ(2u, CountTracker());
1270 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_AddFile) {
1271 std::string app_id = "example";
1273 RegisterApp(app_id);
1274 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1276 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1277 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1278 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1279 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1281 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1282 fake_drive_service_helper()->AddFile(
1283 app_root_folder_id, "conflict_to_pending_remote", "foo", NULL));
1285 FetchRemoteChanges();
1287 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1288 fake_drive_service_helper()->AddFile(
1289 app_root_folder_id, "conflict_to_existing_remote", "bar",
1292 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1293 VerifyConsistency();
1295 EXPECT_EQ(1u, CountApp());
1296 EXPECT_EQ(3u, CountLocalFile(app_id));
1297 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1298 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1300 EXPECT_EQ(4u, CountMetadata());
1301 EXPECT_EQ(4u, CountTracker());
1304 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_DeleteFile) {
1305 std::string app_id = "example";
1307 RegisterApp(app_id);
1308 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1309 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1310 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1312 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1313 VerifyConsistency();
1315 // Test body starts from here.
1316 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1317 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1319 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1320 fake_drive_service_helper()->DeleteResource(
1321 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1323 FetchRemoteChanges();
1325 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1326 fake_drive_service_helper()->DeleteResource(
1327 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1329 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1330 VerifyConsistency();
1332 EXPECT_EQ(1u, CountApp());
1333 EXPECT_EQ(1u, CountLocalFile(app_id));
1335 EXPECT_EQ(2u, CountMetadata());
1336 EXPECT_EQ(2u, CountTracker());
1339 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_AddFolder) {
1340 std::string app_id = "example";
1342 RegisterApp(app_id);
1343 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1345 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1346 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1348 std::string file_id;
1349 EXPECT_EQ(google_apis::HTTP_CREATED,
1350 fake_drive_service_helper()->AddFolder(
1351 app_root_folder_id, "conflict_to_pending_remote",
1353 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1354 fake_drive_service_helper()->UpdateModificationTime(
1356 base::Time::Now() - base::TimeDelta::FromDays(1)));
1358 FetchRemoteChanges();
1360 EXPECT_EQ(google_apis::HTTP_CREATED,
1361 fake_drive_service_helper()->AddFolder(
1362 app_root_folder_id, "conflict_to_existing_remote",
1364 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1365 fake_drive_service_helper()->UpdateModificationTime(
1367 base::Time::Now() - base::TimeDelta::FromDays(1)));
1369 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1370 VerifyConsistency();
1372 EXPECT_EQ(1u, CountApp());
1373 EXPECT_EQ(3u, CountLocalFile(app_id));
1374 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1375 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1377 EXPECT_EQ(4u, CountMetadata());
1378 EXPECT_EQ(4u, CountTracker());
1381 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_DeleteFolder) {
1382 std::string app_id = "example";
1384 RegisterApp(app_id);
1385 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1387 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1388 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1390 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1391 VerifyConsistency();
1393 // Test body starts from here.
1394 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1395 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1396 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1397 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1399 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1400 fake_drive_service_helper()->DeleteResource(
1401 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1403 FetchRemoteChanges();
1405 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1406 fake_drive_service_helper()->DeleteResource(
1407 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1409 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1410 VerifyConsistency();
1412 EXPECT_EQ(1u, CountApp());
1413 EXPECT_EQ(3u, CountLocalFile(app_id));
1414 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1415 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1417 EXPECT_EQ(4u, CountMetadata());
1418 EXPECT_EQ(4u, CountTracker());
1421 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_AddFile) {
1422 std::string app_id = "example";
1424 RegisterApp(app_id);
1426 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1427 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1428 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1430 std::string file_id;
1431 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1432 fake_drive_service_helper()->AddFile(
1433 app_root_folder_id, "conflict_to_pending_remote", "foo",
1435 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1436 fake_drive_service_helper()->UpdateModificationTime(
1438 base::Time::Now() + base::TimeDelta::FromDays(1)));
1440 FetchRemoteChanges();
1442 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1443 fake_drive_service_helper()->AddFile(
1444 app_root_folder_id, "conflict_to_existing_remote", "bar",
1446 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1447 fake_drive_service_helper()->UpdateModificationTime(
1449 base::Time::Now() + base::TimeDelta::FromDays(1)));
1451 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1452 VerifyConsistency();
1454 EXPECT_EQ(1u, CountApp());
1455 EXPECT_EQ(3u, CountLocalFile(app_id));
1456 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1457 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1459 EXPECT_EQ(4u, CountMetadata());
1460 EXPECT_EQ(4u, CountTracker());
1463 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_DeleteFile) {
1464 std::string app_id = "example";
1466 RegisterApp(app_id);
1468 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1469 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1471 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1472 VerifyConsistency();
1474 // Test body starts from here.
1475 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1476 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1477 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1478 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1480 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1481 fake_drive_service_helper()->DeleteResource(
1482 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1484 FetchRemoteChanges();
1486 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1487 fake_drive_service_helper()->DeleteResource(
1488 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1490 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1491 VerifyConsistency();
1493 EXPECT_EQ(1u, CountApp());
1494 EXPECT_EQ(3u, CountLocalFile(app_id));
1495 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1496 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1498 EXPECT_EQ(4u, CountMetadata());
1499 EXPECT_EQ(4u, CountTracker());
1502 TEST_F(DriveBackendSyncTest, ConflictTest_UpdateFile_DeleteFile) {
1503 std::string app_id = "example";
1505 RegisterApp(app_id);
1507 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1508 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1510 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1511 VerifyConsistency();
1513 // Test body starts from here.
1514 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1515 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1517 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1518 fake_drive_service_helper()->DeleteResource(
1519 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1521 FetchRemoteChanges();
1523 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1524 fake_drive_service_helper()->DeleteResource(
1525 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1527 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1528 VerifyConsistency();
1530 EXPECT_EQ(1u, CountApp());
1531 EXPECT_EQ(3u, CountLocalFile(app_id));
1532 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1533 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1535 EXPECT_EQ(4u, CountMetadata());
1536 EXPECT_EQ(4u, CountTracker());
1539 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_AddFolder) {
1540 std::string app_id = "example";
1542 RegisterApp(app_id);
1544 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1545 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1546 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1548 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1549 VerifyConsistency();
1551 // Test body starts from here.
1552 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1553 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1555 EXPECT_EQ(google_apis::HTTP_CREATED,
1556 fake_drive_service_helper()->AddFolder(
1557 app_root_folder_id, "conflict_to_pending_remote", NULL));
1559 FetchRemoteChanges();
1561 EXPECT_EQ(google_apis::HTTP_CREATED,
1562 fake_drive_service_helper()->AddFolder(
1563 app_root_folder_id, "conflict_to_existing_remote", NULL));
1565 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1566 VerifyConsistency();
1568 EXPECT_EQ(1u, CountApp());
1569 EXPECT_EQ(3u, CountLocalFile(app_id));
1570 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1571 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1573 EXPECT_EQ(4u, CountMetadata());
1574 EXPECT_EQ(4u, CountTracker());
1577 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_DeleteFolder) {
1578 std::string app_id = "example";
1580 RegisterApp(app_id);
1582 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1583 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1585 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1586 VerifyConsistency();
1588 // Test body starts from here.
1589 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1590 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1592 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1593 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1595 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1596 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1598 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1599 fake_drive_service_helper()->DeleteResource(
1600 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1602 FetchRemoteChanges();
1604 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1605 fake_drive_service_helper()->DeleteResource(
1606 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1608 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1609 VerifyConsistency();
1611 EXPECT_EQ(1u, CountApp());
1612 EXPECT_EQ(1u, CountLocalFile(app_id));
1614 EXPECT_EQ(2u, CountMetadata());
1615 EXPECT_EQ(2u, CountTracker());
1618 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_AddFile) {
1619 std::string app_id = "example";
1621 RegisterApp(app_id);
1623 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1624 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1625 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1626 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1627 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1629 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1630 fake_drive_service_helper()->AddFile(
1631 app_root_folder_id, "conflict_to_pending_remote", "hoge",
1634 FetchRemoteChanges();
1636 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1637 fake_drive_service_helper()->AddFile(
1638 app_root_folder_id, "conflict_to_existing_remote", "fuga",
1641 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1642 VerifyConsistency();
1644 EXPECT_EQ(1u, CountApp());
1645 EXPECT_EQ(3u, CountLocalFile(app_id));
1646 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1647 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1649 EXPECT_EQ(4u, CountMetadata());
1650 EXPECT_EQ(4u, CountTracker());
1653 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_UpdateFile) {
1654 std::string app_id = "example";
1656 RegisterApp(app_id);
1658 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1659 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1660 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1662 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1663 VerifyConsistency();
1665 // Test body starts from here.
1666 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1667 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1669 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1670 fake_drive_service_helper()->UpdateFile(
1671 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote")),
1674 FetchRemoteChanges();
1676 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1677 fake_drive_service_helper()->UpdateFile(
1678 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote")),
1681 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1682 VerifyConsistency();
1684 EXPECT_EQ(1u, CountApp());
1685 EXPECT_EQ(3u, CountLocalFile(app_id));
1686 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1687 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1689 EXPECT_EQ(4u, CountMetadata());
1690 EXPECT_EQ(4u, CountTracker());
1693 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_DeleteFile) {
1694 std::string app_id = "example";
1696 RegisterApp(app_id);
1698 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1699 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1700 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1702 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1703 VerifyConsistency();
1705 // Test body starts from here.
1706 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1707 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1709 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1710 fake_drive_service_helper()->DeleteResource(
1711 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1713 FetchRemoteChanges();
1715 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1716 fake_drive_service_helper()->DeleteResource(
1717 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1719 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1720 VerifyConsistency();
1722 EXPECT_EQ(1u, CountApp());
1723 EXPECT_EQ(1u, CountLocalFile(app_id));
1725 EXPECT_EQ(2u, CountMetadata());
1726 EXPECT_EQ(2u, CountTracker());
1729 } // namespace drive_backend
1730 } // namespace sync_file_system