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/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 "extensions/common/extension.h"
33 #include "google_apis/drive/drive_api_parser.h"
34 #include "testing/gtest/include/gtest/gtest.h"
35 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
36 #include "third_party/leveldatabase/src/include/leveldb/env.h"
37 #include "webkit/browser/fileapi/file_system_context.h"
39 #define FPL(a) FILE_PATH_LITERAL(a)
41 namespace sync_file_system {
42 namespace drive_backend {
44 typedef fileapi::FileSystemOperation::FileEntryList FileEntryList;
49 void SetValueAndCallClosure(const base::Closure& closure,
52 *arg_out = base::internal::CallbackForward(arg);
56 void SetSyncStatusAndUrl(const base::Closure& closure,
57 SyncStatusCode* status_out,
58 fileapi::FileSystemURL* url_out,
59 SyncStatusCode status,
60 const fileapi::FileSystemURL& url) {
68 class DriveBackendSyncTest : public testing::Test,
69 public LocalFileSyncService::Observer,
70 public RemoteFileSyncService::Observer {
72 DriveBackendSyncTest()
73 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
74 pending_remote_changes_(0),
75 pending_local_changes_(0) {}
76 virtual ~DriveBackendSyncTest() {}
78 virtual void SetUp() OVERRIDE {
79 ASSERT_TRUE(base_dir_.CreateUniqueTempDir());
80 in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
82 io_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread(
83 content::BrowserThread::IO);
84 scoped_refptr<base::SequencedWorkerPool> worker_pool(
85 content::BrowserThread::GetBlockingPool());
87 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
88 worker_pool->GetSequenceToken(),
89 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
90 file_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread(
91 content::BrowserThread::FILE);
92 scoped_refptr<base::SequencedTaskRunner> drive_task_runner =
93 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
94 worker_pool->GetSequenceToken(),
95 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
97 RegisterSyncableFileSystem();
98 local_sync_service_ = LocalFileSyncService::CreateForTesting(
99 &profile_, in_memory_env_.get());
100 local_sync_service_->AddChangeObserver(this);
102 scoped_ptr<drive::FakeDriveService>
103 drive_service(new drive::FakeDriveService);
104 drive_service->Initialize("test@example.com");
105 ASSERT_TRUE(drive::test_util::SetUpTestEntries(drive_service.get()));
107 scoped_ptr<drive::DriveUploaderInterface> uploader(
108 new drive::DriveUploader(drive_service.get(),
109 file_task_runner_.get()));
111 fake_drive_service_helper_.reset(new FakeDriveServiceHelper(
112 drive_service.get(), uploader.get(),
113 kSyncRootFolderTitle));
115 remote_sync_service_.reset(new SyncEngine(
116 base::ThreadTaskRunnerHandle::Get(), // ui_task_runner
121 NULL, // notification_manager
122 NULL, // extension_service
123 NULL, // signin_manager
124 NULL, // token_service
125 NULL, // request_context
126 scoped_ptr<SyncEngine::DriveServiceFactory>(),
127 in_memory_env_.get()));
128 remote_sync_service_->AddServiceObserver(this);
129 remote_sync_service_->InitializeForTesting(
130 drive_service.PassAs<drive::DriveServiceInterface>(),
132 scoped_ptr<SyncWorkerInterface>());
133 remote_sync_service_->SetSyncEnabled(true);
135 local_sync_service_->SetLocalChangeProcessor(remote_sync_service_.get());
136 remote_sync_service_->SetRemoteChangeProcessor(local_sync_service_.get());
139 virtual void TearDown() OVERRIDE {
140 typedef std::map<std::string, CannedSyncableFileSystem*>::iterator iterator;
141 for (iterator itr = file_systems_.begin();
142 itr != file_systems_.end(); ++itr) {
143 itr->second->TearDown();
146 file_systems_.clear();
148 local_sync_service_->Shutdown();
150 fake_drive_service_helper_.reset();
151 local_sync_service_.reset();
152 remote_sync_service_.reset();
154 content::BrowserThread::GetBlockingPool()->FlushForTesting();
155 base::RunLoop().RunUntilIdle();
156 RevokeSyncableFileSystem();
159 virtual void OnRemoteChangeQueueUpdated(int64 pending_changes_hint) OVERRIDE {
160 pending_remote_changes_ = pending_changes_hint;
163 virtual void OnLocalChangeAvailable(int64 pending_changes_hint) OVERRIDE {
164 pending_local_changes_ = pending_changes_hint;
168 fileapi::FileSystemURL CreateURL(const std::string& app_id,
169 const base::FilePath::StringType& path) {
170 return CreateURL(app_id, base::FilePath(path));
173 fileapi::FileSystemURL CreateURL(const std::string& app_id,
174 const base::FilePath& path) {
175 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id);
176 return CreateSyncableFileSystemURL(origin, path);
179 bool GetAppRootFolderID(const std::string& app_id,
180 std::string* folder_id) {
181 base::RunLoop run_loop;
182 bool success = false;
184 PostTaskAndReplyWithResult(
187 base::Bind(&MetadataDatabase::FindAppRootTracker,
188 base::Unretained(metadata_database()),
191 base::Bind(&SetValueAndCallClosure<bool>,
192 run_loop.QuitClosure(),
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(
216 base::Bind(&MetadataDatabase::FindNearestActiveAncestor,
217 base::Unretained(metadata_database()),
222 base::Bind(&SetValueAndCallClosure<bool>,
223 run_loop.QuitClosure(),
226 EXPECT_TRUE(success);
227 EXPECT_EQ(normalized_path, result_path);
228 return tracker.file_id();
231 SyncStatusCode RegisterApp(const std::string& app_id) {
232 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id);
233 if (!ContainsKey(file_systems_, app_id)) {
234 CannedSyncableFileSystem* file_system = new CannedSyncableFileSystem(
235 origin, in_memory_env_.get(),
236 io_task_runner_.get(), file_task_runner_.get());
237 file_system->SetUp(CannedSyncableFileSystem::QUOTA_DISABLED);
239 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
240 base::RunLoop run_loop;
241 local_sync_service_->MaybeInitializeFileSystemContext(
242 origin, file_system->file_system_context(),
243 base::Bind(&SetValueAndCallClosure<SyncStatusCode>,
244 run_loop.QuitClosure(), &status));
246 EXPECT_EQ(SYNC_STATUS_OK, status);
248 file_system->backend()->sync_context()->
249 set_mock_notify_changes_duration_in_sec(0);
251 EXPECT_EQ(base::File::FILE_OK, file_system->OpenFileSystem());
252 file_systems_[app_id] = file_system;
255 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
256 base::RunLoop run_loop;
257 remote_sync_service_->RegisterOrigin(
259 base::Bind(&SetValueAndCallClosure<SyncStatusCode>,
260 run_loop.QuitClosure(), &status));
265 void AddLocalFolder(const std::string& app_id,
266 const base::FilePath::StringType& path) {
267 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
268 EXPECT_EQ(base::File::FILE_OK,
269 file_systems_[app_id]->CreateDirectory(
270 CreateURL(app_id, path)));
273 void AddOrUpdateLocalFile(const std::string& app_id,
274 const base::FilePath::StringType& path,
275 const std::string& content) {
276 fileapi::FileSystemURL url(CreateURL(app_id, path));
277 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
278 EXPECT_EQ(base::File::FILE_OK, file_systems_[app_id]->CreateFile(url));
279 int64 bytes_written = file_systems_[app_id]->WriteString(url, content);
280 EXPECT_EQ(static_cast<int64>(content.size()), bytes_written);
281 base::RunLoop().RunUntilIdle();
284 void UpdateLocalFile(const std::string& app_id,
285 const base::FilePath::StringType& path,
286 const std::string& content) {
287 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
288 int64 bytes_written = file_systems_[app_id]->WriteString(
289 CreateURL(app_id, path), content);
290 EXPECT_EQ(static_cast<int64>(content.size()), bytes_written);
291 base::RunLoop().RunUntilIdle();
294 void RemoveLocal(const std::string& app_id,
295 const base::FilePath::StringType& path) {
296 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
297 EXPECT_EQ(base::File::FILE_OK,
298 file_systems_[app_id]->Remove(
299 CreateURL(app_id, path),
300 true /* recursive */));
301 base::RunLoop().RunUntilIdle();
304 SyncStatusCode ProcessLocalChange() {
305 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
306 fileapi::FileSystemURL url;
307 base::RunLoop run_loop;
308 local_sync_service_->ProcessLocalChange(base::Bind(
309 &SetSyncStatusAndUrl, run_loop.QuitClosure(), &status, &url));
314 SyncStatusCode ProcessRemoteChange() {
315 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
316 fileapi::FileSystemURL url;
317 base::RunLoop run_loop;
318 remote_sync_service_->ProcessRemoteChange(base::Bind(
319 &SetSyncStatusAndUrl, run_loop.QuitClosure(), &status, &url));
324 int64 GetLargestChangeID() {
325 scoped_ptr<google_apis::AboutResource> about_resource;
326 EXPECT_EQ(google_apis::HTTP_SUCCESS,
327 fake_drive_service_helper()->GetAboutResource(&about_resource));
330 return about_resource->largest_change_id();
333 void FetchRemoteChanges() {
334 remote_sync_service_->OnNotificationReceived();
338 SyncStatusCode ProcessChangesUntilDone() {
339 int task_limit = 100;
340 SyncStatusCode local_sync_status;
341 SyncStatusCode remote_sync_status;
343 base::RunLoop().RunUntilIdle();
347 return SYNC_STATUS_ABORT;
349 local_sync_status = ProcessLocalChange();
350 if (local_sync_status != SYNC_STATUS_OK &&
351 local_sync_status != SYNC_STATUS_NO_CHANGE_TO_SYNC &&
352 local_sync_status != SYNC_STATUS_FILE_BUSY)
353 return local_sync_status;
355 remote_sync_status = ProcessRemoteChange();
356 if (remote_sync_status != SYNC_STATUS_OK &&
357 remote_sync_status != SYNC_STATUS_NO_CHANGE_TO_SYNC &&
358 remote_sync_status != SYNC_STATUS_FILE_BUSY)
359 return remote_sync_status;
361 if (local_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC &&
362 remote_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC) {
365 base::RunLoop run_loop;
366 remote_sync_service_->PromoteDemotedChanges(run_loop.QuitClosure());
371 base::RunLoop run_loop;
372 local_sync_service_->PromoteDemotedChanges(run_loop.QuitClosure());
376 if (pending_remote_changes_ || pending_local_changes_)
379 base::RunLoop run_loop;
380 int64 largest_fetched_change_id = -1;
381 PostTaskAndReplyWithResult(
384 base::Bind(&MetadataDatabase::GetLargestFetchedChangeID,
385 base::Unretained(metadata_database())),
386 base::Bind(&SetValueAndCallClosure<int64>,
387 run_loop.QuitClosure(),
388 &largest_fetched_change_id));
390 if (largest_fetched_change_id != GetLargestChangeID()) {
391 FetchRemoteChanges();
397 return SYNC_STATUS_OK;
400 // Verifies local and remote files/folders are consistent.
401 // This function checks:
402 // - Each registered origin has corresponding remote folder.
403 // - Each local file/folder has corresponding remote one.
404 // - Each remote file/folder has corresponding local one.
405 // TODO(tzik): Handle conflict case. i.e. allow remote file has different
406 // file content if the corresponding local file conflicts to it.
407 void VerifyConsistency() {
408 std::string sync_root_folder_id;
409 google_apis::GDataErrorCode error =
410 fake_drive_service_helper_->GetSyncRootFolderID(&sync_root_folder_id);
411 if (sync_root_folder_id.empty()) {
412 EXPECT_EQ(google_apis::HTTP_NOT_FOUND, error);
413 EXPECT_TRUE(file_systems_.empty());
416 EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
418 ScopedVector<google_apis::ResourceEntry> remote_entries;
419 EXPECT_EQ(google_apis::HTTP_SUCCESS,
420 fake_drive_service_helper_->ListFilesInFolder(
421 sync_root_folder_id, &remote_entries));
422 std::map<std::string, const google_apis::ResourceEntry*> app_root_by_title;
423 for (ScopedVector<google_apis::ResourceEntry>::iterator itr =
424 remote_entries.begin();
425 itr != remote_entries.end();
427 const google_apis::ResourceEntry& remote_entry = **itr;
428 EXPECT_FALSE(ContainsKey(app_root_by_title, remote_entry.title()));
429 app_root_by_title[remote_entry.title()] = *itr;
432 for (std::map<std::string, CannedSyncableFileSystem*>::const_iterator itr =
433 file_systems_.begin();
434 itr != file_systems_.end(); ++itr) {
435 const std::string& app_id = itr->first;
436 SCOPED_TRACE(testing::Message() << "Verifying app: " << app_id);
437 CannedSyncableFileSystem* file_system = itr->second;
438 ASSERT_TRUE(ContainsKey(app_root_by_title, app_id));
439 VerifyConsistencyForFolder(
440 app_id, base::FilePath(),
441 app_root_by_title[app_id]->resource_id(),
446 void VerifyConsistencyForFolder(const std::string& app_id,
447 const base::FilePath& path,
448 const std::string& folder_id,
449 CannedSyncableFileSystem* file_system) {
450 SCOPED_TRACE(testing::Message() << "Verifying folder: " << path.value());
452 ScopedVector<google_apis::ResourceEntry> remote_entries;
453 EXPECT_EQ(google_apis::HTTP_SUCCESS,
454 fake_drive_service_helper_->ListFilesInFolder(
455 folder_id, &remote_entries));
456 std::map<std::string, const google_apis::ResourceEntry*>
457 remote_entry_by_title;
458 for (size_t i = 0; i < remote_entries.size(); ++i) {
459 google_apis::ResourceEntry* remote_entry = remote_entries[i];
460 EXPECT_FALSE(ContainsKey(remote_entry_by_title, remote_entry->title()))
461 << "title: " << remote_entry->title();
462 remote_entry_by_title[remote_entry->title()] = remote_entry;
465 fileapi::FileSystemURL url(CreateURL(app_id, path));
466 FileEntryList local_entries;
467 EXPECT_EQ(base::File::FILE_OK,
468 file_system->ReadDirectory(url, &local_entries));
469 for (FileEntryList::iterator itr = local_entries.begin();
470 itr != local_entries.end();
472 const fileapi::DirectoryEntry& local_entry = *itr;
473 fileapi::FileSystemURL entry_url(
474 CreateURL(app_id, path.Append(local_entry.name)));
476 fileapi::VirtualPath::BaseName(entry_url.path()).AsUTF8Unsafe();
477 SCOPED_TRACE(testing::Message() << "Verifying entry: " << title);
479 ASSERT_TRUE(ContainsKey(remote_entry_by_title, title));
480 const google_apis::ResourceEntry& remote_entry =
481 *remote_entry_by_title[title];
482 if (local_entry.is_directory) {
483 ASSERT_TRUE(remote_entry.is_folder());
484 VerifyConsistencyForFolder(app_id, entry_url.path(),
485 remote_entry.resource_id(),
488 ASSERT_TRUE(remote_entry.is_file());
489 VerifyConsistencyForFile(app_id, entry_url.path(),
490 remote_entry.resource_id(),
493 remote_entry_by_title.erase(title);
496 EXPECT_TRUE(remote_entry_by_title.empty());
499 void VerifyConsistencyForFile(const std::string& app_id,
500 const base::FilePath& path,
501 const std::string& file_id,
502 CannedSyncableFileSystem* file_system) {
503 fileapi::FileSystemURL url(CreateURL(app_id, path));
504 std::string file_content;
505 EXPECT_EQ(google_apis::HTTP_SUCCESS,
506 fake_drive_service_helper_->ReadFile(file_id, &file_content));
507 EXPECT_EQ(base::File::FILE_OK,
508 file_system->VerifyFile(url, file_content));
512 return file_systems_.size();
515 size_t CountLocalFile(const std::string& app_id) {
516 if (!ContainsKey(file_systems_, app_id))
519 CannedSyncableFileSystem* file_system = file_systems_[app_id];
520 std::stack<base::FilePath> folders;
521 folders.push(base::FilePath()); // root folder
524 while (!folders.empty()) {
525 fileapi::FileSystemURL url(CreateURL(app_id, folders.top()));
528 FileEntryList entries;
529 EXPECT_EQ(base::File::FILE_OK,
530 file_system->ReadDirectory(url, &entries));
531 for (FileEntryList::iterator itr = entries.begin();
532 itr != entries.end(); ++itr) {
534 if (itr->is_directory)
535 folders.push(url.path().Append(itr->name));
542 void VerifyLocalFile(const std::string& app_id,
543 const base::FilePath::StringType& path,
544 const std::string& content) {
545 SCOPED_TRACE(testing::Message() << "Verifying local file: "
546 << "app_id = " << app_id
547 << ", path = " << path);
548 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
549 EXPECT_EQ(base::File::FILE_OK,
550 file_systems_[app_id]->VerifyFile(
551 CreateURL(app_id, path), content));
554 void VerifyLocalFolder(const std::string& app_id,
555 const base::FilePath::StringType& path) {
556 SCOPED_TRACE(testing::Message() << "Verifying local file: "
557 << "app_id = " << app_id
558 << ", path = " << path);
559 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
560 EXPECT_EQ(base::File::FILE_OK,
561 file_systems_[app_id]->DirectoryExists(CreateURL(app_id, path)));
564 size_t CountMetadata() {
566 base::RunLoop run_loop;
567 PostTaskAndReplyWithResult(
570 base::Bind(&MetadataDatabase::CountFileMetadata,
571 base::Unretained(metadata_database())),
572 base::Bind(&SetValueAndCallClosure<size_t>,
573 run_loop.QuitClosure(),
579 size_t CountTracker() {
581 base::RunLoop run_loop;
582 PostTaskAndReplyWithResult(
585 base::Bind(&MetadataDatabase::CountFileTracker,
586 base::Unretained(metadata_database())),
587 base::Bind(&SetValueAndCallClosure<size_t>,
588 run_loop.QuitClosure(), &count));
593 drive::FakeDriveService* fake_drive_service() {
594 return static_cast<drive::FakeDriveService*>(
595 remote_sync_service_->drive_service_.get());
598 FakeDriveServiceHelper* fake_drive_service_helper() {
599 return fake_drive_service_helper_.get();
602 void WaitForIdleWorker() {
603 base::RunLoop run_loop;
604 worker_task_runner_->PostTask(
606 base::Bind(&SyncWorker::CallOnIdleForTesting,
607 base::Unretained(sync_worker()),
608 RelayCallbackToCurrentThread(
610 run_loop.QuitClosure())));
615 SyncWorker* sync_worker() {
616 return static_cast<SyncWorker*>(remote_sync_service_->sync_worker_.get());
619 // MetadataDatabase is normally used on the worker thread.
620 // Use this only when there is no task running on the worker.
621 MetadataDatabase* metadata_database() {
622 return sync_worker()->context_->metadata_database_.get();
625 content::TestBrowserThreadBundle thread_bundle_;
627 base::ScopedTempDir base_dir_;
628 scoped_ptr<leveldb::Env> in_memory_env_;
629 TestingProfile profile_;
631 scoped_ptr<SyncEngine> remote_sync_service_;
632 scoped_ptr<LocalFileSyncService> local_sync_service_;
634 int64 pending_remote_changes_;
635 int64 pending_local_changes_;
637 scoped_ptr<FakeDriveServiceHelper> fake_drive_service_helper_;
638 std::map<std::string, CannedSyncableFileSystem*> file_systems_;
641 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
642 scoped_refptr<base::SequencedTaskRunner> worker_task_runner_;
643 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
645 DISALLOW_COPY_AND_ASSIGN(DriveBackendSyncTest);
648 TEST_F(DriveBackendSyncTest, LocalToRemoteBasicTest) {
649 std::string app_id = "example";
652 AddOrUpdateLocalFile(app_id, FPL("file"), "abcde");
654 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
657 EXPECT_EQ(1u, CountApp());
658 EXPECT_EQ(2u, CountLocalFile(app_id));
659 VerifyLocalFile(app_id, FPL("file"), "abcde");
661 EXPECT_EQ(3u, CountMetadata());
662 EXPECT_EQ(3u, CountTracker());
665 TEST_F(DriveBackendSyncTest, RemoteToLocalBasicTest) {
666 std::string app_id = "example";
669 std::string app_root_folder_id;
670 EXPECT_TRUE(GetAppRootFolderID(app_id, &app_root_folder_id));
673 EXPECT_EQ(google_apis::HTTP_SUCCESS,
674 fake_drive_service_helper()->AddFile(
675 app_root_folder_id, "file", "abcde", &file_id));
677 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
680 EXPECT_EQ(1u, CountApp());
681 EXPECT_EQ(2u, CountLocalFile(app_id));
682 VerifyLocalFile(app_id, FPL("file"), "abcde");
684 EXPECT_EQ(3u, CountMetadata());
685 EXPECT_EQ(3u, CountTracker());
688 TEST_F(DriveBackendSyncTest, LocalFileUpdateTest) {
689 std::string app_id = "example";
690 const base::FilePath::StringType kPath(FPL("file"));
693 AddOrUpdateLocalFile(app_id, kPath, "abcde");
695 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
698 UpdateLocalFile(app_id, kPath, "1234567890");
700 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
703 EXPECT_EQ(1u, CountApp());
704 EXPECT_EQ(2u, CountLocalFile(app_id));
705 VerifyLocalFile(app_id, FPL("file"), "1234567890");
707 EXPECT_EQ(3u, CountMetadata());
708 EXPECT_EQ(3u, CountTracker());
711 TEST_F(DriveBackendSyncTest, RemoteFileUpdateTest) {
712 std::string app_id = "example";
715 std::string remote_file_id;
716 std::string app_root_folder_id;
717 EXPECT_TRUE(GetAppRootFolderID(app_id, &app_root_folder_id));
718 EXPECT_EQ(google_apis::HTTP_SUCCESS,
719 fake_drive_service_helper()->AddFile(
720 app_root_folder_id, "file", "abcde", &remote_file_id));
722 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
725 EXPECT_EQ(google_apis::HTTP_SUCCESS,
726 fake_drive_service_helper()->UpdateFile(
727 remote_file_id, "1234567890"));
729 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
732 EXPECT_EQ(1u, CountApp());
733 EXPECT_EQ(2u, CountLocalFile(app_id));
734 VerifyLocalFile(app_id, FPL("file"), "1234567890");
736 EXPECT_EQ(3u, CountMetadata());
737 EXPECT_EQ(3u, CountTracker());
740 TEST_F(DriveBackendSyncTest, LocalFileDeletionTest) {
741 std::string app_id = "example";
742 const base::FilePath::StringType path(FPL("file"));
745 AddOrUpdateLocalFile(app_id, path, "abcde");
747 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
750 RemoveLocal(app_id, path);
752 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
755 EXPECT_EQ(1u, CountApp());
756 EXPECT_EQ(1u, CountLocalFile(app_id));
758 EXPECT_EQ(2u, CountMetadata());
759 EXPECT_EQ(2u, CountTracker());
762 TEST_F(DriveBackendSyncTest, RemoteFileDeletionTest) {
763 std::string app_id = "example";
764 const base::FilePath::StringType path(FPL("file"));
767 AddOrUpdateLocalFile(app_id, path, "abcde");
769 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
772 std::string file_id = GetFileIDByPath(app_id, path);
773 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
774 fake_drive_service_helper()->DeleteResource(file_id));
776 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
779 EXPECT_EQ(1u, CountApp());
780 EXPECT_EQ(1u, CountLocalFile(app_id));
782 EXPECT_EQ(2u, CountMetadata());
783 EXPECT_EQ(2u, CountTracker());
786 TEST_F(DriveBackendSyncTest, RemoteRenameTest) {
787 std::string app_id = "example";
788 const base::FilePath::StringType path(FPL("file"));
791 AddOrUpdateLocalFile(app_id, path, "abcde");
793 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
796 std::string file_id = GetFileIDByPath(app_id, path);
797 EXPECT_EQ(google_apis::HTTP_SUCCESS,
798 fake_drive_service_helper()->RenameResource(
799 file_id, "renamed_file"));
801 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
804 EXPECT_EQ(1u, CountApp());
805 EXPECT_EQ(2u, CountLocalFile(app_id));
806 VerifyLocalFile(app_id, FPL("renamed_file"), "abcde");
808 EXPECT_EQ(3u, CountMetadata());
809 EXPECT_EQ(3u, CountTracker());
812 TEST_F(DriveBackendSyncTest, RemoteRenameAndRevertTest) {
813 std::string app_id = "example";
814 const base::FilePath::StringType path(FPL("file"));
817 AddOrUpdateLocalFile(app_id, path, "abcde");
819 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
822 std::string file_id = GetFileIDByPath(app_id, path);
823 EXPECT_EQ(google_apis::HTTP_SUCCESS,
824 fake_drive_service_helper()->RenameResource(
825 file_id, "renamed_file"));
827 FetchRemoteChanges();
829 EXPECT_EQ(google_apis::HTTP_SUCCESS,
830 fake_drive_service_helper()->RenameResource(
831 file_id, base::FilePath(path).AsUTF8Unsafe()));
833 FetchRemoteChanges();
835 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
838 EXPECT_EQ(1u, CountApp());
839 EXPECT_EQ(2u, CountLocalFile(app_id));
840 VerifyLocalFile(app_id, FPL("file"), "abcde");
842 EXPECT_EQ(3u, CountMetadata());
843 EXPECT_EQ(3u, CountTracker());
846 TEST_F(DriveBackendSyncTest, ReorganizeToOtherFolder) {
847 std::string app_id = "example";
848 const base::FilePath::StringType path(FPL("file"));
851 AddLocalFolder(app_id, FPL("folder_src"));
852 AddLocalFolder(app_id, FPL("folder_dest"));
853 AddOrUpdateLocalFile(app_id, FPL("folder_src/file"), "abcde");
855 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
858 std::string file_id = GetFileIDByPath(app_id, FPL("folder_src/file"));
859 std::string src_folder_id = GetFileIDByPath(app_id, FPL("folder_src"));
860 std::string dest_folder_id = GetFileIDByPath(app_id, FPL("folder_dest"));
861 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
862 fake_drive_service_helper()->RemoveResourceFromDirectory(
863 src_folder_id, file_id));
864 EXPECT_EQ(google_apis::HTTP_SUCCESS,
865 fake_drive_service_helper()->AddResourceToDirectory(
866 dest_folder_id, file_id));
868 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
871 EXPECT_EQ(1u, CountApp());
872 EXPECT_EQ(4u, CountLocalFile(app_id));
873 VerifyLocalFolder(app_id, FPL("folder_dest"));
874 VerifyLocalFile(app_id, FPL("folder_dest/file"), "abcde");
876 EXPECT_EQ(5u, CountMetadata());
877 EXPECT_EQ(5u, CountTracker());
880 TEST_F(DriveBackendSyncTest, ReorganizeToOtherApp) {
881 std::string src_app_id = "src_app";
882 std::string dest_app_id = "dest_app";
884 RegisterApp(src_app_id);
885 RegisterApp(dest_app_id);
887 AddLocalFolder(src_app_id, FPL("folder_src"));
888 AddLocalFolder(dest_app_id, FPL("folder_dest"));
889 AddOrUpdateLocalFile(src_app_id, FPL("folder_src/file"), "abcde");
891 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
894 std::string file_id = GetFileIDByPath(src_app_id, FPL("folder_src/file"));
895 std::string src_folder_id = GetFileIDByPath(src_app_id, FPL("folder_src"));
896 std::string dest_folder_id = GetFileIDByPath(dest_app_id, FPL("folder_dest"));
897 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
898 fake_drive_service_helper()->RemoveResourceFromDirectory(
899 src_folder_id, file_id));
900 EXPECT_EQ(google_apis::HTTP_SUCCESS,
901 fake_drive_service_helper()->AddResourceToDirectory(
902 dest_folder_id, file_id));
904 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
907 EXPECT_EQ(2u, CountApp());
908 EXPECT_EQ(2u, CountLocalFile(src_app_id));
909 EXPECT_EQ(3u, CountLocalFile(dest_app_id));
910 VerifyLocalFile(dest_app_id, FPL("folder_dest/file"), "abcde");
912 EXPECT_EQ(6u, CountMetadata());
913 EXPECT_EQ(6u, CountTracker());
916 TEST_F(DriveBackendSyncTest, ReorganizeToUnmanagedArea) {
917 std::string app_id = "example";
921 AddLocalFolder(app_id, FPL("folder_src"));
922 AddOrUpdateLocalFile(app_id, FPL("folder_src/file_orphaned"), "abcde");
923 AddOrUpdateLocalFile(app_id, FPL("folder_src/file_under_sync_root"), "123");
924 AddOrUpdateLocalFile(app_id, FPL("folder_src/file_under_drive_root"), "hoge");
926 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
929 std::string file_orphaned_id =
930 GetFileIDByPath(app_id, FPL("folder_src/file_orphaned"));
931 std::string file_under_sync_root_id =
932 GetFileIDByPath(app_id, FPL("folder_src/file_under_sync_root"));
933 std::string file_under_drive_root_id =
934 GetFileIDByPath(app_id, FPL("folder_src/file_under_drive_root"));
936 std::string folder_id = GetFileIDByPath(app_id, FPL("folder_src"));
937 std::string sync_root_folder_id;
938 EXPECT_EQ(google_apis::HTTP_SUCCESS,
939 fake_drive_service_helper()->GetSyncRootFolderID(
940 &sync_root_folder_id));
942 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
943 fake_drive_service_helper()->RemoveResourceFromDirectory(
944 folder_id, file_orphaned_id));
945 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
946 fake_drive_service_helper()->RemoveResourceFromDirectory(
947 folder_id, file_under_sync_root_id));
948 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
949 fake_drive_service_helper()->RemoveResourceFromDirectory(
950 folder_id, file_under_drive_root_id));
952 EXPECT_EQ(google_apis::HTTP_SUCCESS,
953 fake_drive_service_helper()->AddResourceToDirectory(
954 sync_root_folder_id, file_under_sync_root_id));
955 EXPECT_EQ(google_apis::HTTP_SUCCESS,
956 fake_drive_service_helper()->AddResourceToDirectory(
957 fake_drive_service()->GetRootResourceId(),
958 file_under_drive_root_id));
960 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
963 EXPECT_EQ(1u, CountApp());
964 EXPECT_EQ(2u, CountLocalFile(app_id));
966 EXPECT_EQ(4u, CountMetadata());
967 EXPECT_EQ(4u, CountTracker());
970 TEST_F(DriveBackendSyncTest, ReorganizeToMultipleParents) {
971 std::string app_id = "example";
975 AddLocalFolder(app_id, FPL("parent1"));
976 AddLocalFolder(app_id, FPL("parent2"));
977 AddOrUpdateLocalFile(app_id, FPL("parent1/file"), "abcde");
979 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
982 std::string file_id = GetFileIDByPath(app_id, FPL("parent1/file"));
983 std::string parent2_folder_id = GetFileIDByPath(app_id, FPL("parent2"));
984 EXPECT_EQ(google_apis::HTTP_SUCCESS,
985 fake_drive_service_helper()->AddResourceToDirectory(
986 parent2_folder_id, file_id));
988 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
991 EXPECT_EQ(1u, CountApp());
992 EXPECT_EQ(4u, CountLocalFile(app_id));
993 VerifyLocalFolder(app_id, FPL("parent1"));
994 VerifyLocalFolder(app_id, FPL("parent2"));
995 VerifyLocalFile(app_id, FPL("parent1/file"), "abcde");
997 EXPECT_EQ(5u, CountMetadata());
998 EXPECT_EQ(5u, CountTracker());
1001 TEST_F(DriveBackendSyncTest, ReorganizeAndRevert) {
1002 std::string app_id = "example";
1004 RegisterApp(app_id);
1006 AddLocalFolder(app_id, FPL("folder"));
1007 AddLocalFolder(app_id, FPL("folder_temp"));
1008 AddOrUpdateLocalFile(app_id, FPL("folder/file"), "abcde");
1010 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1011 VerifyConsistency();
1013 std::string file_id = GetFileIDByPath(app_id, FPL("folder/file"));
1014 std::string folder_id = GetFileIDByPath(app_id, FPL("folder"));
1015 std::string folder_temp_id = GetFileIDByPath(app_id, FPL("folder_temp"));
1016 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1017 fake_drive_service_helper()->RemoveResourceFromDirectory(
1018 folder_id, file_id));
1019 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1020 fake_drive_service_helper()->AddResourceToDirectory(
1021 folder_temp_id, file_id));
1023 FetchRemoteChanges();
1025 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1026 fake_drive_service_helper()->RemoveResourceFromDirectory(
1027 folder_temp_id, file_id));
1028 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1029 fake_drive_service_helper()->AddResourceToDirectory(
1030 folder_id, file_id));
1032 FetchRemoteChanges();
1034 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1035 VerifyConsistency();
1037 EXPECT_EQ(1u, CountApp());
1038 EXPECT_EQ(4u, CountLocalFile(app_id));
1039 VerifyLocalFolder(app_id, FPL("folder"));
1040 VerifyLocalFile(app_id, FPL("folder/file"), "abcde");
1042 EXPECT_EQ(5u, CountMetadata());
1043 EXPECT_EQ(5u, CountTracker());
1046 TEST_F(DriveBackendSyncTest, ConflictTest_ConflictTest_AddFolder_AddFolder) {
1047 std::string app_id = "example";
1049 RegisterApp(app_id);
1050 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1052 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1053 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1055 std::string remote_folder_id;
1056 EXPECT_EQ(google_apis::HTTP_CREATED,
1057 fake_drive_service_helper()->AddFolder(
1059 "conflict_to_pending_remote", &remote_folder_id));
1061 FetchRemoteChanges();
1063 EXPECT_EQ(google_apis::HTTP_CREATED,
1064 fake_drive_service_helper()->AddFolder(
1066 "conflict_to_existing_remote", &remote_folder_id));
1068 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1069 VerifyConsistency();
1071 EXPECT_EQ(1u, CountApp());
1072 EXPECT_EQ(3u, CountLocalFile(app_id));
1073 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1074 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1076 EXPECT_EQ(4u, CountMetadata());
1077 EXPECT_EQ(4u, CountTracker());
1080 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_DeleteFolder) {
1081 std::string app_id = "example";
1083 RegisterApp(app_id);
1085 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1086 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1088 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1089 VerifyConsistency();
1091 // Test body starts from here.
1092 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1093 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1094 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1095 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1097 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1098 fake_drive_service_helper()->DeleteResource(
1099 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1101 FetchRemoteChanges();
1103 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1104 fake_drive_service_helper()->DeleteResource(
1105 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1107 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1108 VerifyConsistency();
1110 EXPECT_EQ(1u, CountApp());
1111 EXPECT_EQ(2u, CountLocalFile(app_id));
1112 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1114 EXPECT_EQ(3u, CountMetadata());
1115 EXPECT_EQ(3u, CountTracker());
1118 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_AddFile) {
1119 std::string app_id = "example";
1121 RegisterApp(app_id);
1122 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1124 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1125 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1127 std::string file_id;
1128 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1129 fake_drive_service_helper()->AddFile(
1130 app_root_folder_id, "conflict_to_pending_remote", "foo",
1132 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1133 fake_drive_service_helper()->UpdateModificationTime(
1135 base::Time::Now() + base::TimeDelta::FromDays(1)));
1137 FetchRemoteChanges();
1139 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1140 fake_drive_service_helper()->AddFile(
1141 app_root_folder_id, "conflict_to_existing_remote", "foo",
1143 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1144 fake_drive_service_helper()->UpdateModificationTime(
1146 base::Time::Now() + base::TimeDelta::FromDays(1)));
1148 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1149 VerifyConsistency();
1151 EXPECT_EQ(1u, CountApp());
1152 EXPECT_EQ(3u, CountLocalFile(app_id));
1153 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1154 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1156 EXPECT_EQ(4u, CountMetadata());
1157 EXPECT_EQ(4u, CountTracker());
1160 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_DeleteFile) {
1161 std::string app_id = "example";
1163 RegisterApp(app_id);
1164 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1166 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1167 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1169 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1170 VerifyConsistency();
1172 // Test body starts from here.
1173 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1174 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1176 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1177 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1179 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1180 fake_drive_service_helper()->DeleteResource(
1181 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1183 FetchRemoteChanges();
1185 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1186 fake_drive_service_helper()->DeleteResource(
1187 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1189 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1190 VerifyConsistency();
1192 EXPECT_EQ(1u, CountApp());
1193 EXPECT_EQ(3u, CountLocalFile(app_id));
1194 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1195 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1197 EXPECT_EQ(4u, CountMetadata());
1198 EXPECT_EQ(4u, CountTracker());
1201 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_AddFolder) {
1202 std::string app_id = "example";
1204 RegisterApp(app_id);
1205 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1206 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1207 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1209 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1210 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1212 std::string file_id;
1213 EXPECT_EQ(google_apis::HTTP_CREATED,
1214 fake_drive_service_helper()->AddFolder(
1216 "conflict_to_pending_remote", &file_id));
1218 FetchRemoteChanges();
1220 EXPECT_EQ(google_apis::HTTP_CREATED,
1221 fake_drive_service_helper()->AddFolder(
1223 "conflict_to_existing_remote", NULL));
1225 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1226 VerifyConsistency();
1228 EXPECT_EQ(1u, CountApp());
1229 EXPECT_EQ(3u, CountLocalFile(app_id));
1230 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1231 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1233 EXPECT_EQ(4u, CountMetadata());
1234 EXPECT_EQ(4u, CountTracker());
1237 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_DeleteFolder) {
1238 std::string app_id = "example";
1240 RegisterApp(app_id);
1241 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1243 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1244 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1246 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1247 VerifyConsistency();
1249 // Test body starts from here.
1250 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1251 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1253 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1254 fake_drive_service_helper()->DeleteResource(
1255 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1257 FetchRemoteChanges();
1259 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1260 fake_drive_service_helper()->DeleteResource(
1261 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1263 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1264 VerifyConsistency();
1266 EXPECT_EQ(1u, CountApp());
1267 EXPECT_EQ(1u, CountLocalFile(app_id));
1269 EXPECT_EQ(2u, CountMetadata());
1270 EXPECT_EQ(2u, CountTracker());
1273 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_AddFile) {
1274 std::string app_id = "example";
1276 RegisterApp(app_id);
1277 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1279 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1280 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1281 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1282 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1284 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1285 fake_drive_service_helper()->AddFile(
1286 app_root_folder_id, "conflict_to_pending_remote", "foo", NULL));
1288 FetchRemoteChanges();
1290 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1291 fake_drive_service_helper()->AddFile(
1292 app_root_folder_id, "conflict_to_existing_remote", "bar",
1295 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1296 VerifyConsistency();
1298 EXPECT_EQ(1u, CountApp());
1299 EXPECT_EQ(3u, CountLocalFile(app_id));
1300 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1301 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1303 EXPECT_EQ(4u, CountMetadata());
1304 EXPECT_EQ(4u, CountTracker());
1307 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_DeleteFile) {
1308 std::string app_id = "example";
1310 RegisterApp(app_id);
1311 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1312 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1313 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1315 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1316 VerifyConsistency();
1318 // Test body starts from here.
1319 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1320 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1322 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1323 fake_drive_service_helper()->DeleteResource(
1324 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1326 FetchRemoteChanges();
1328 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1329 fake_drive_service_helper()->DeleteResource(
1330 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1332 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1333 VerifyConsistency();
1335 EXPECT_EQ(1u, CountApp());
1336 EXPECT_EQ(1u, CountLocalFile(app_id));
1338 EXPECT_EQ(2u, CountMetadata());
1339 EXPECT_EQ(2u, CountTracker());
1342 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_AddFolder) {
1343 std::string app_id = "example";
1345 RegisterApp(app_id);
1346 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1348 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1349 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1351 std::string file_id;
1352 EXPECT_EQ(google_apis::HTTP_CREATED,
1353 fake_drive_service_helper()->AddFolder(
1354 app_root_folder_id, "conflict_to_pending_remote",
1356 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1357 fake_drive_service_helper()->UpdateModificationTime(
1359 base::Time::Now() - base::TimeDelta::FromDays(1)));
1361 FetchRemoteChanges();
1363 EXPECT_EQ(google_apis::HTTP_CREATED,
1364 fake_drive_service_helper()->AddFolder(
1365 app_root_folder_id, "conflict_to_existing_remote",
1367 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1368 fake_drive_service_helper()->UpdateModificationTime(
1370 base::Time::Now() - base::TimeDelta::FromDays(1)));
1372 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1373 VerifyConsistency();
1375 EXPECT_EQ(1u, CountApp());
1376 EXPECT_EQ(3u, CountLocalFile(app_id));
1377 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1378 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1380 EXPECT_EQ(4u, CountMetadata());
1381 EXPECT_EQ(4u, CountTracker());
1384 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_DeleteFolder) {
1385 std::string app_id = "example";
1387 RegisterApp(app_id);
1388 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1390 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1391 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1393 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1394 VerifyConsistency();
1396 // Test body starts from here.
1397 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1398 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1399 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1400 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1402 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1403 fake_drive_service_helper()->DeleteResource(
1404 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1406 FetchRemoteChanges();
1408 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1409 fake_drive_service_helper()->DeleteResource(
1410 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1412 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1413 VerifyConsistency();
1415 EXPECT_EQ(1u, CountApp());
1416 EXPECT_EQ(3u, CountLocalFile(app_id));
1417 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1418 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1420 EXPECT_EQ(4u, CountMetadata());
1421 EXPECT_EQ(4u, CountTracker());
1424 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_AddFile) {
1425 std::string app_id = "example";
1427 RegisterApp(app_id);
1429 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1430 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1431 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1433 std::string file_id;
1434 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1435 fake_drive_service_helper()->AddFile(
1436 app_root_folder_id, "conflict_to_pending_remote", "foo",
1438 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1439 fake_drive_service_helper()->UpdateModificationTime(
1441 base::Time::Now() + base::TimeDelta::FromDays(1)));
1443 FetchRemoteChanges();
1445 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1446 fake_drive_service_helper()->AddFile(
1447 app_root_folder_id, "conflict_to_existing_remote", "bar",
1449 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1450 fake_drive_service_helper()->UpdateModificationTime(
1452 base::Time::Now() + base::TimeDelta::FromDays(1)));
1454 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1455 VerifyConsistency();
1457 EXPECT_EQ(1u, CountApp());
1458 EXPECT_EQ(3u, CountLocalFile(app_id));
1459 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1460 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1462 EXPECT_EQ(4u, CountMetadata());
1463 EXPECT_EQ(4u, CountTracker());
1466 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_DeleteFile) {
1467 std::string app_id = "example";
1469 RegisterApp(app_id);
1471 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1472 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1474 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1475 VerifyConsistency();
1477 // Test body starts from here.
1478 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1479 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1480 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1481 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1483 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1484 fake_drive_service_helper()->DeleteResource(
1485 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1487 FetchRemoteChanges();
1489 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1490 fake_drive_service_helper()->DeleteResource(
1491 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1493 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1494 VerifyConsistency();
1496 EXPECT_EQ(1u, CountApp());
1497 EXPECT_EQ(3u, CountLocalFile(app_id));
1498 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1499 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1501 EXPECT_EQ(4u, CountMetadata());
1502 EXPECT_EQ(4u, CountTracker());
1505 TEST_F(DriveBackendSyncTest, ConflictTest_UpdateFile_DeleteFile) {
1506 std::string app_id = "example";
1508 RegisterApp(app_id);
1510 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1511 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1513 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1514 VerifyConsistency();
1516 // Test body starts from here.
1517 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1518 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1520 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1521 fake_drive_service_helper()->DeleteResource(
1522 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1524 FetchRemoteChanges();
1526 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1527 fake_drive_service_helper()->DeleteResource(
1528 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1530 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1531 VerifyConsistency();
1533 EXPECT_EQ(1u, CountApp());
1534 EXPECT_EQ(3u, CountLocalFile(app_id));
1535 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1536 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1538 EXPECT_EQ(4u, CountMetadata());
1539 EXPECT_EQ(4u, CountTracker());
1542 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_AddFolder) {
1543 std::string app_id = "example";
1545 RegisterApp(app_id);
1547 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1548 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1549 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1551 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1552 VerifyConsistency();
1554 // Test body starts from here.
1555 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1556 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1558 EXPECT_EQ(google_apis::HTTP_CREATED,
1559 fake_drive_service_helper()->AddFolder(
1560 app_root_folder_id, "conflict_to_pending_remote", NULL));
1562 FetchRemoteChanges();
1564 EXPECT_EQ(google_apis::HTTP_CREATED,
1565 fake_drive_service_helper()->AddFolder(
1566 app_root_folder_id, "conflict_to_existing_remote", NULL));
1568 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1569 VerifyConsistency();
1571 EXPECT_EQ(1u, CountApp());
1572 EXPECT_EQ(3u, CountLocalFile(app_id));
1573 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1574 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1576 EXPECT_EQ(4u, CountMetadata());
1577 EXPECT_EQ(4u, CountTracker());
1580 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_DeleteFolder) {
1581 std::string app_id = "example";
1583 RegisterApp(app_id);
1585 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1586 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1588 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1589 VerifyConsistency();
1591 // Test body starts from here.
1592 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1593 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1595 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1596 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1598 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1599 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1601 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1602 fake_drive_service_helper()->DeleteResource(
1603 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1605 FetchRemoteChanges();
1607 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1608 fake_drive_service_helper()->DeleteResource(
1609 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1611 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1612 VerifyConsistency();
1614 EXPECT_EQ(1u, CountApp());
1615 EXPECT_EQ(1u, CountLocalFile(app_id));
1617 EXPECT_EQ(2u, CountMetadata());
1618 EXPECT_EQ(2u, CountTracker());
1621 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_AddFile) {
1622 std::string app_id = "example";
1624 RegisterApp(app_id);
1626 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1627 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1628 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1629 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1630 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1632 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1633 fake_drive_service_helper()->AddFile(
1634 app_root_folder_id, "conflict_to_pending_remote", "hoge",
1637 FetchRemoteChanges();
1639 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1640 fake_drive_service_helper()->AddFile(
1641 app_root_folder_id, "conflict_to_existing_remote", "fuga",
1644 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1645 VerifyConsistency();
1647 EXPECT_EQ(1u, CountApp());
1648 EXPECT_EQ(3u, CountLocalFile(app_id));
1649 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1650 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1652 EXPECT_EQ(4u, CountMetadata());
1653 EXPECT_EQ(4u, CountTracker());
1656 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_UpdateFile) {
1657 std::string app_id = "example";
1659 RegisterApp(app_id);
1661 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1662 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1663 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1665 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1666 VerifyConsistency();
1668 // Test body starts from here.
1669 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1670 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1672 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1673 fake_drive_service_helper()->UpdateFile(
1674 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote")),
1677 FetchRemoteChanges();
1679 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1680 fake_drive_service_helper()->UpdateFile(
1681 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote")),
1684 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1685 VerifyConsistency();
1687 EXPECT_EQ(1u, CountApp());
1688 EXPECT_EQ(3u, CountLocalFile(app_id));
1689 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1690 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1692 EXPECT_EQ(4u, CountMetadata());
1693 EXPECT_EQ(4u, CountTracker());
1696 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_DeleteFile) {
1697 std::string app_id = "example";
1699 RegisterApp(app_id);
1701 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1702 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1703 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1705 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1706 VerifyConsistency();
1708 // Test body starts from here.
1709 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1710 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1712 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1713 fake_drive_service_helper()->DeleteResource(
1714 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1716 FetchRemoteChanges();
1718 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1719 fake_drive_service_helper()->DeleteResource(
1720 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1722 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1723 VerifyConsistency();
1725 EXPECT_EQ(1u, CountApp());
1726 EXPECT_EQ(1u, CountLocalFile(app_id));
1728 EXPECT_EQ(2u, CountMetadata());
1729 EXPECT_EQ(2u, CountTracker());
1732 } // namespace drive_backend
1733 } // namespace sync_file_system