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 "chrome/browser/drive/drive_uploader.h"
12 #include "chrome/browser/drive/fake_drive_service.h"
13 #include "chrome/browser/drive/test_util.h"
14 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.h"
15 #include "chrome/browser/sync_file_system/drive_backend/fake_drive_service_helper.h"
16 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
17 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
18 #include "chrome/browser/sync_file_system/drive_backend/sync_engine.h"
19 #include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h"
20 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h"
21 #include "chrome/browser/sync_file_system/local/local_file_sync_service.h"
22 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h"
23 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h"
24 #include "chrome/browser/sync_file_system/syncable_file_system_util.h"
25 #include "chrome/test/base/testing_profile.h"
26 #include "content/public/test/test_browser_thread.h"
27 #include "content/public/test/test_browser_thread_bundle.h"
28 #include "extensions/common/extension.h"
29 #include "google_apis/drive/drive_api_parser.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
32 #include "third_party/leveldatabase/src/include/leveldb/env.h"
33 #include "webkit/browser/fileapi/file_system_context.h"
35 #define FPL(a) FILE_PATH_LITERAL(a)
37 namespace sync_file_system {
38 namespace drive_backend {
40 typedef fileapi::FileSystemOperation::FileEntryList FileEntryList;
42 class DriveBackendSyncTest : public testing::Test,
43 public LocalFileSyncService::Observer,
44 public RemoteFileSyncService::Observer {
46 DriveBackendSyncTest()
47 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
48 pending_remote_changes_(0),
49 pending_local_changes_(0) {}
50 virtual ~DriveBackendSyncTest() {}
52 virtual void SetUp() OVERRIDE {
53 ASSERT_TRUE(base_dir_.CreateUniqueTempDir());
54 in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
56 io_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread(
57 content::BrowserThread::IO);
58 file_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread(
59 content::BrowserThread::FILE);
61 RegisterSyncableFileSystem();
62 local_sync_service_ = LocalFileSyncService::CreateForTesting(
63 &profile_, in_memory_env_.get());
64 local_sync_service_->AddChangeObserver(this);
66 scoped_ptr<drive::FakeDriveService>
67 drive_service(new drive::FakeDriveService);
68 drive_service->Initialize("test@example.com");
69 ASSERT_TRUE(drive::test_util::SetUpTestEntries(drive_service.get()));
71 scoped_ptr<drive::DriveUploaderInterface> uploader(
72 new drive::DriveUploader(drive_service.get(),
73 file_task_runner_.get()));
75 fake_drive_service_helper_.reset(new FakeDriveServiceHelper(
76 drive_service.get(), uploader.get(),
77 kSyncRootFolderTitle));
79 remote_sync_service_.reset(new SyncEngine(
80 drive_service.PassAs<drive::DriveServiceInterface>(),
82 file_task_runner_.get(),
84 remote_sync_service_->AddServiceObserver(this);
85 remote_sync_service_->Initialize(base_dir_.path(),
86 base::MessageLoopProxy::current(),
87 in_memory_env_.get());
88 remote_sync_service_->SetSyncEnabled(true);
90 local_sync_service_->SetLocalChangeProcessor(remote_sync_service_.get());
91 remote_sync_service_->SetRemoteChangeProcessor(local_sync_service_.get());
94 virtual void TearDown() OVERRIDE {
95 typedef std::map<std::string, CannedSyncableFileSystem*>::iterator iterator;
96 for (iterator itr = file_systems_.begin();
97 itr != file_systems_.end(); ++itr) {
98 itr->second->TearDown();
101 file_systems_.clear();
103 fake_drive_service_helper_.reset();
104 remote_sync_service_.reset();
106 base::RunLoop().RunUntilIdle();
107 RevokeSyncableFileSystem();
110 virtual void OnRemoteChangeQueueUpdated(int64 pending_changes_hint) OVERRIDE {
111 pending_remote_changes_ = pending_changes_hint;
114 virtual void OnLocalChangeAvailable(int64 pending_changes_hint) OVERRIDE {
115 pending_local_changes_ = pending_changes_hint;
119 fileapi::FileSystemURL CreateURL(const std::string& app_id,
120 const base::FilePath::StringType& path) {
121 return CreateURL(app_id, base::FilePath(path));
124 fileapi::FileSystemURL CreateURL(const std::string& app_id,
125 const base::FilePath& path) {
126 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id);
127 return CreateSyncableFileSystemURL(origin, path);
130 bool GetAppRootFolderID(const std::string& app_id,
131 std::string* folder_id) {
133 if (!metadata_database()->FindAppRootTracker(app_id, &tracker))
135 *folder_id = tracker.file_id();
139 std::string GetFileIDByPath(const std::string& app_id,
140 const base::FilePath::StringType& path) {
141 return GetFileIDByPath(app_id, base::FilePath(path));
144 std::string GetFileIDByPath(const std::string& app_id,
145 const base::FilePath& path) {
147 base::FilePath result_path;
148 base::FilePath normalized_path = path.NormalizePathSeparators();
149 EXPECT_TRUE(metadata_database()->FindNearestActiveAncestor(
150 app_id, normalized_path, &tracker, &result_path));
151 EXPECT_EQ(normalized_path, result_path);
152 return tracker.file_id();
155 SyncStatusCode RegisterApp(const std::string& app_id) {
156 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id);
157 if (!ContainsKey(file_systems_, app_id)) {
158 CannedSyncableFileSystem* file_system = new CannedSyncableFileSystem(
159 origin, in_memory_env_.get(),
160 io_task_runner_.get(), file_task_runner_.get());
161 file_system->SetUp(CannedSyncableFileSystem::QUOTA_DISABLED);
163 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
164 local_sync_service_->MaybeInitializeFileSystemContext(
165 origin, file_system->file_system_context(),
166 CreateResultReceiver(&status));
167 base::RunLoop().RunUntilIdle();
168 EXPECT_EQ(SYNC_STATUS_OK, status);
170 file_system->backend()->sync_context()->
171 set_mock_notify_changes_duration_in_sec(0);
173 EXPECT_EQ(base::File::FILE_OK, file_system->OpenFileSystem());
174 file_systems_[app_id] = file_system;
177 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
178 remote_sync_service_->RegisterOrigin(origin, CreateResultReceiver(&status));
179 base::RunLoop().RunUntilIdle();
183 void AddLocalFolder(const std::string& app_id,
184 const base::FilePath::StringType& path) {
185 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
186 EXPECT_EQ(base::File::FILE_OK,
187 file_systems_[app_id]->CreateDirectory(
188 CreateURL(app_id, path)));
191 void AddOrUpdateLocalFile(const std::string& app_id,
192 const base::FilePath::StringType& path,
193 const std::string& content) {
194 fileapi::FileSystemURL url(CreateURL(app_id, path));
195 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
196 EXPECT_EQ(base::File::FILE_OK, file_systems_[app_id]->CreateFile(url));
197 int64 bytes_written = file_systems_[app_id]->WriteString(url, content);
198 EXPECT_EQ(static_cast<int64>(content.size()), bytes_written);
199 base::RunLoop().RunUntilIdle();
202 void UpdateLocalFile(const std::string& app_id,
203 const base::FilePath::StringType& path,
204 const std::string& content) {
205 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
206 int64 bytes_written = file_systems_[app_id]->WriteString(
207 CreateURL(app_id, path), content);
208 EXPECT_EQ(static_cast<int64>(content.size()), bytes_written);
209 base::RunLoop().RunUntilIdle();
212 void RemoveLocal(const std::string& app_id,
213 const base::FilePath::StringType& path) {
214 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
215 EXPECT_EQ(base::File::FILE_OK,
216 file_systems_[app_id]->Remove(
217 CreateURL(app_id, path),
218 true /* recursive */));
219 base::RunLoop().RunUntilIdle();
222 SyncStatusCode ProcessLocalChange() {
223 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
224 fileapi::FileSystemURL url;
225 local_sync_service_->ProcessLocalChange(
226 CreateResultReceiver(&status, &url));
227 base::RunLoop().RunUntilIdle();
231 SyncStatusCode ProcessRemoteChange() {
232 SyncStatusCode status = SYNC_STATUS_UNKNOWN;
233 fileapi::FileSystemURL url;
234 remote_sync_service_->ProcessRemoteChange(
235 CreateResultReceiver(&status, &url));
236 base::RunLoop().RunUntilIdle();
240 int64 GetLargestChangeID() {
241 scoped_ptr<google_apis::AboutResource> about_resource;
242 EXPECT_EQ(google_apis::HTTP_SUCCESS,
243 fake_drive_service_helper()->GetAboutResource(&about_resource));
246 return about_resource->largest_change_id();
249 void FetchRemoteChanges() {
250 remote_sync_service_->OnNotificationReceived();
251 base::RunLoop().RunUntilIdle();
254 SyncStatusCode ProcessChangesUntilDone() {
255 SyncStatusCode local_sync_status;
256 SyncStatusCode remote_sync_status;
258 local_sync_status = ProcessLocalChange();
259 if (local_sync_status != SYNC_STATUS_OK &&
260 local_sync_status != SYNC_STATUS_NO_CHANGE_TO_SYNC &&
261 local_sync_status != SYNC_STATUS_FILE_BUSY)
262 return local_sync_status;
264 remote_sync_status = ProcessRemoteChange();
265 if (remote_sync_status != SYNC_STATUS_OK &&
266 remote_sync_status != SYNC_STATUS_NO_CHANGE_TO_SYNC &&
267 remote_sync_status != SYNC_STATUS_FILE_BUSY)
268 return remote_sync_status;
270 if (local_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC &&
271 remote_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC) {
272 remote_sync_service_->PromoteDemotedChanges();
273 local_sync_service_->PromoteDemotedChanges();
275 if (pending_remote_changes_ || pending_local_changes_)
278 int64 largest_fetched_change_id =
279 metadata_database()->GetLargestFetchedChangeID();
280 if (largest_fetched_change_id != GetLargestChangeID()) {
281 FetchRemoteChanges();
287 return SYNC_STATUS_OK;
290 // Verifies local and remote files/folders are consistent.
291 // This function checks:
292 // - Each registered origin has corresponding remote folder.
293 // - Each local file/folder has corresponding remote one.
294 // - Each remote file/folder has corresponding local one.
295 // TODO(tzik): Handle conflict case. i.e. allow remote file has different
296 // file content if the corresponding local file conflicts to it.
297 void VerifyConsistency() {
298 std::string sync_root_folder_id;
299 google_apis::GDataErrorCode error =
300 fake_drive_service_helper_->GetSyncRootFolderID(&sync_root_folder_id);
301 if (sync_root_folder_id.empty()) {
302 EXPECT_EQ(google_apis::HTTP_NOT_FOUND, error);
303 EXPECT_TRUE(file_systems_.empty());
306 EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
308 ScopedVector<google_apis::ResourceEntry> remote_entries;
309 EXPECT_EQ(google_apis::HTTP_SUCCESS,
310 fake_drive_service_helper_->ListFilesInFolder(
311 sync_root_folder_id, &remote_entries));
312 std::map<std::string, const google_apis::ResourceEntry*> app_root_by_title;
313 for (ScopedVector<google_apis::ResourceEntry>::iterator itr =
314 remote_entries.begin();
315 itr != remote_entries.end();
317 const google_apis::ResourceEntry& remote_entry = **itr;
318 EXPECT_FALSE(ContainsKey(app_root_by_title, remote_entry.title()));
319 app_root_by_title[remote_entry.title()] = *itr;
322 for (std::map<std::string, CannedSyncableFileSystem*>::const_iterator itr =
323 file_systems_.begin();
324 itr != file_systems_.end(); ++itr) {
325 const std::string& app_id = itr->first;
326 SCOPED_TRACE(testing::Message() << "Verifying app: " << app_id);
327 CannedSyncableFileSystem* file_system = itr->second;
328 ASSERT_TRUE(ContainsKey(app_root_by_title, app_id));
329 VerifyConsistencyForFolder(
330 app_id, base::FilePath(),
331 app_root_by_title[app_id]->resource_id(),
336 void VerifyConsistencyForFolder(const std::string& app_id,
337 const base::FilePath& path,
338 const std::string& folder_id,
339 CannedSyncableFileSystem* file_system) {
340 SCOPED_TRACE(testing::Message() << "Verifying folder: " << path.value());
342 ScopedVector<google_apis::ResourceEntry> remote_entries;
343 EXPECT_EQ(google_apis::HTTP_SUCCESS,
344 fake_drive_service_helper_->ListFilesInFolder(
345 folder_id, &remote_entries));
346 std::map<std::string, const google_apis::ResourceEntry*>
347 remote_entry_by_title;
348 for (size_t i = 0; i < remote_entries.size(); ++i) {
349 google_apis::ResourceEntry* remote_entry = remote_entries[i];
350 EXPECT_FALSE(ContainsKey(remote_entry_by_title, remote_entry->title()))
351 << "title: " << remote_entry->title();
352 remote_entry_by_title[remote_entry->title()] = remote_entry;
355 fileapi::FileSystemURL url(CreateURL(app_id, path));
356 FileEntryList local_entries;
357 EXPECT_EQ(base::File::FILE_OK,
358 file_system->ReadDirectory(url, &local_entries));
359 for (FileEntryList::iterator itr = local_entries.begin();
360 itr != local_entries.end();
362 const fileapi::DirectoryEntry& local_entry = *itr;
363 fileapi::FileSystemURL entry_url(
364 CreateURL(app_id, path.Append(local_entry.name)));
366 fileapi::VirtualPath::BaseName(entry_url.path()).AsUTF8Unsafe();
367 SCOPED_TRACE(testing::Message() << "Verifying entry: " << title);
369 ASSERT_TRUE(ContainsKey(remote_entry_by_title, title));
370 const google_apis::ResourceEntry& remote_entry =
371 *remote_entry_by_title[title];
372 if (local_entry.is_directory) {
373 ASSERT_TRUE(remote_entry.is_folder());
374 VerifyConsistencyForFolder(app_id, entry_url.path(),
375 remote_entry.resource_id(),
378 ASSERT_TRUE(remote_entry.is_file());
379 VerifyConsistencyForFile(app_id, entry_url.path(),
380 remote_entry.resource_id(),
383 remote_entry_by_title.erase(title);
386 EXPECT_TRUE(remote_entry_by_title.empty());
389 void VerifyConsistencyForFile(const std::string& app_id,
390 const base::FilePath& path,
391 const std::string& file_id,
392 CannedSyncableFileSystem* file_system) {
393 fileapi::FileSystemURL url(CreateURL(app_id, path));
394 std::string file_content;
395 EXPECT_EQ(google_apis::HTTP_SUCCESS,
396 fake_drive_service_helper_->ReadFile(file_id, &file_content));
397 EXPECT_EQ(base::File::FILE_OK,
398 file_system->VerifyFile(url, file_content));
402 return file_systems_.size();
405 size_t CountLocalFile(const std::string& app_id) {
406 if (!ContainsKey(file_systems_, app_id))
409 CannedSyncableFileSystem* file_system = file_systems_[app_id];
410 std::stack<base::FilePath> folders;
411 folders.push(base::FilePath()); // root folder
414 while (!folders.empty()) {
415 fileapi::FileSystemURL url(CreateURL(app_id, folders.top()));
418 FileEntryList entries;
419 EXPECT_EQ(base::File::FILE_OK,
420 file_system->ReadDirectory(url, &entries));
421 for (FileEntryList::iterator itr = entries.begin();
422 itr != entries.end(); ++itr) {
424 if (itr->is_directory)
425 folders.push(url.path().Append(itr->name));
432 void VerifyLocalFile(const std::string& app_id,
433 const base::FilePath::StringType& path,
434 const std::string& content) {
435 SCOPED_TRACE(testing::Message() << "Verifying local file: "
436 << "app_id = " << app_id
437 << ", path = " << path);
438 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
439 EXPECT_EQ(base::File::FILE_OK,
440 file_systems_[app_id]->VerifyFile(
441 CreateURL(app_id, path), content));
444 void VerifyLocalFolder(const std::string& app_id,
445 const base::FilePath::StringType& path) {
446 SCOPED_TRACE(testing::Message() << "Verifying local file: "
447 << "app_id = " << app_id
448 << ", path = " << path);
449 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
450 EXPECT_EQ(base::File::FILE_OK,
451 file_systems_[app_id]->DirectoryExists(CreateURL(app_id, path)));
454 size_t CountMetadata() {
455 return metadata_database()->CountFileMetadata();
458 size_t CountTracker() {
459 return metadata_database()->CountFileTracker();
462 drive::FakeDriveService* fake_drive_service() {
463 return static_cast<drive::FakeDriveService*>(
464 remote_sync_service_->GetDriveService());
467 FakeDriveServiceHelper* fake_drive_service_helper() {
468 return fake_drive_service_helper_.get();
471 MetadataDatabase* metadata_database() {
472 return remote_sync_service_->GetMetadataDatabase();
476 content::TestBrowserThreadBundle thread_bundle_;
477 ScopedEnableSyncFSV2 enable_syncfs_v2_;
479 base::ScopedTempDir base_dir_;
480 scoped_ptr<leveldb::Env> in_memory_env_;
481 TestingProfile profile_;
483 scoped_ptr<SyncEngine> remote_sync_service_;
484 scoped_ptr<LocalFileSyncService> local_sync_service_;
486 int64 pending_remote_changes_;
487 int64 pending_local_changes_;
489 scoped_ptr<FakeDriveServiceHelper> fake_drive_service_helper_;
490 std::map<std::string, CannedSyncableFileSystem*> file_systems_;
493 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
494 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
496 DISALLOW_COPY_AND_ASSIGN(DriveBackendSyncTest);
499 TEST_F(DriveBackendSyncTest, LocalToRemoteBasicTest) {
500 std::string app_id = "example";
503 AddOrUpdateLocalFile(app_id, FPL("file"), "abcde");
505 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
508 EXPECT_EQ(1u, CountApp());
509 EXPECT_EQ(2u, CountLocalFile(app_id));
510 VerifyLocalFile(app_id, FPL("file"), "abcde");
512 EXPECT_EQ(3u, CountMetadata());
513 EXPECT_EQ(3u, CountTracker());
516 TEST_F(DriveBackendSyncTest, RemoteToLocalBasicTest) {
517 std::string app_id = "example";
520 std::string app_root_folder_id;
521 EXPECT_TRUE(GetAppRootFolderID(app_id, &app_root_folder_id));
524 EXPECT_EQ(google_apis::HTTP_SUCCESS,
525 fake_drive_service_helper()->AddFile(
526 app_root_folder_id, "file", "abcde", &file_id));
528 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
531 EXPECT_EQ(1u, CountApp());
532 EXPECT_EQ(2u, CountLocalFile(app_id));
533 VerifyLocalFile(app_id, FPL("file"), "abcde");
535 EXPECT_EQ(3u, CountMetadata());
536 EXPECT_EQ(3u, CountTracker());
539 TEST_F(DriveBackendSyncTest, LocalFileUpdateTest) {
540 std::string app_id = "example";
541 const base::FilePath::StringType kPath(FPL("file"));
544 AddOrUpdateLocalFile(app_id, kPath, "abcde");
546 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
549 UpdateLocalFile(app_id, kPath, "1234567890");
551 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
554 EXPECT_EQ(1u, CountApp());
555 EXPECT_EQ(2u, CountLocalFile(app_id));
556 VerifyLocalFile(app_id, FPL("file"), "1234567890");
558 EXPECT_EQ(3u, CountMetadata());
559 EXPECT_EQ(3u, CountTracker());
562 TEST_F(DriveBackendSyncTest, RemoteFileUpdateTest) {
563 std::string app_id = "example";
566 std::string remote_file_id;
567 std::string app_root_folder_id;
568 EXPECT_TRUE(GetAppRootFolderID(app_id, &app_root_folder_id));
569 EXPECT_EQ(google_apis::HTTP_SUCCESS,
570 fake_drive_service_helper()->AddFile(
571 app_root_folder_id, "file", "abcde", &remote_file_id));
573 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
576 EXPECT_EQ(google_apis::HTTP_SUCCESS,
577 fake_drive_service_helper()->UpdateFile(
578 remote_file_id, "1234567890"));
580 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
583 EXPECT_EQ(1u, CountApp());
584 EXPECT_EQ(2u, CountLocalFile(app_id));
585 VerifyLocalFile(app_id, FPL("file"), "1234567890");
587 EXPECT_EQ(3u, CountMetadata());
588 EXPECT_EQ(3u, CountTracker());
591 TEST_F(DriveBackendSyncTest, LocalFileDeletionTest) {
592 std::string app_id = "example";
593 const base::FilePath::StringType path(FPL("file"));
596 AddOrUpdateLocalFile(app_id, path, "abcde");
598 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
601 RemoveLocal(app_id, path);
603 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
606 EXPECT_EQ(1u, CountApp());
607 EXPECT_EQ(1u, CountLocalFile(app_id));
609 EXPECT_EQ(2u, CountMetadata());
610 EXPECT_EQ(2u, CountTracker());
613 TEST_F(DriveBackendSyncTest, RemoteFileDeletionTest) {
614 std::string app_id = "example";
615 const base::FilePath::StringType path(FPL("file"));
618 AddOrUpdateLocalFile(app_id, path, "abcde");
620 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
623 std::string file_id = GetFileIDByPath(app_id, path);
624 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
625 fake_drive_service_helper()->DeleteResource(file_id));
627 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
630 EXPECT_EQ(1u, CountApp());
631 EXPECT_EQ(1u, CountLocalFile(app_id));
633 EXPECT_EQ(2u, CountMetadata());
634 EXPECT_EQ(2u, CountTracker());
637 TEST_F(DriveBackendSyncTest, RemoteRenameTest) {
638 std::string app_id = "example";
639 const base::FilePath::StringType path(FPL("file"));
642 AddOrUpdateLocalFile(app_id, path, "abcde");
644 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
647 std::string file_id = GetFileIDByPath(app_id, path);
648 EXPECT_EQ(google_apis::HTTP_SUCCESS,
649 fake_drive_service_helper()->RenameResource(
650 file_id, "renamed_file"));
652 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
655 EXPECT_EQ(1u, CountApp());
656 EXPECT_EQ(2u, CountLocalFile(app_id));
657 VerifyLocalFile(app_id, FPL("renamed_file"), "abcde");
659 EXPECT_EQ(3u, CountMetadata());
660 EXPECT_EQ(3u, CountTracker());
663 TEST_F(DriveBackendSyncTest, RemoteRenameAndRevertTest) {
664 std::string app_id = "example";
665 const base::FilePath::StringType path(FPL("file"));
668 AddOrUpdateLocalFile(app_id, path, "abcde");
670 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
673 std::string file_id = GetFileIDByPath(app_id, path);
674 EXPECT_EQ(google_apis::HTTP_SUCCESS,
675 fake_drive_service_helper()->RenameResource(
676 file_id, "renamed_file"));
678 FetchRemoteChanges();
680 EXPECT_EQ(google_apis::HTTP_SUCCESS,
681 fake_drive_service_helper()->RenameResource(
682 file_id, base::FilePath(path).AsUTF8Unsafe()));
684 FetchRemoteChanges();
686 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
689 EXPECT_EQ(1u, CountApp());
690 EXPECT_EQ(2u, CountLocalFile(app_id));
691 VerifyLocalFile(app_id, FPL("file"), "abcde");
693 EXPECT_EQ(3u, CountMetadata());
694 EXPECT_EQ(3u, CountTracker());
697 TEST_F(DriveBackendSyncTest, ReorganizeToOtherFolder) {
698 std::string app_id = "example";
699 const base::FilePath::StringType path(FPL("file"));
702 AddLocalFolder(app_id, FPL("folder_src"));
703 AddLocalFolder(app_id, FPL("folder_dest"));
704 AddOrUpdateLocalFile(app_id, FPL("folder_src/file"), "abcde");
706 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
709 std::string file_id = GetFileIDByPath(app_id, FPL("folder_src/file"));
710 std::string src_folder_id = GetFileIDByPath(app_id, FPL("folder_src"));
711 std::string dest_folder_id = GetFileIDByPath(app_id, FPL("folder_dest"));
712 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
713 fake_drive_service_helper()->RemoveResourceFromDirectory(
714 src_folder_id, file_id));
715 EXPECT_EQ(google_apis::HTTP_SUCCESS,
716 fake_drive_service_helper()->AddResourceToDirectory(
717 dest_folder_id, file_id));
719 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
722 EXPECT_EQ(1u, CountApp());
723 EXPECT_EQ(4u, CountLocalFile(app_id));
724 VerifyLocalFolder(app_id, FPL("folder_dest"));
725 VerifyLocalFile(app_id, FPL("folder_dest/file"), "abcde");
727 EXPECT_EQ(5u, CountMetadata());
728 EXPECT_EQ(5u, CountTracker());
731 TEST_F(DriveBackendSyncTest, ReorganizeToOtherApp) {
732 std::string src_app_id = "src_app";
733 std::string dest_app_id = "dest_app";
735 RegisterApp(src_app_id);
736 RegisterApp(dest_app_id);
738 AddLocalFolder(src_app_id, FPL("folder_src"));
739 AddLocalFolder(dest_app_id, FPL("folder_dest"));
740 AddOrUpdateLocalFile(src_app_id, FPL("folder_src/file"), "abcde");
742 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
745 std::string file_id = GetFileIDByPath(src_app_id, FPL("folder_src/file"));
746 std::string src_folder_id = GetFileIDByPath(src_app_id, FPL("folder_src"));
747 std::string dest_folder_id = GetFileIDByPath(dest_app_id, FPL("folder_dest"));
748 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
749 fake_drive_service_helper()->RemoveResourceFromDirectory(
750 src_folder_id, file_id));
751 EXPECT_EQ(google_apis::HTTP_SUCCESS,
752 fake_drive_service_helper()->AddResourceToDirectory(
753 dest_folder_id, file_id));
755 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
758 EXPECT_EQ(2u, CountApp());
759 EXPECT_EQ(2u, CountLocalFile(src_app_id));
760 EXPECT_EQ(3u, CountLocalFile(dest_app_id));
761 VerifyLocalFile(dest_app_id, FPL("folder_dest/file"), "abcde");
763 EXPECT_EQ(6u, CountMetadata());
764 EXPECT_EQ(6u, CountTracker());
767 TEST_F(DriveBackendSyncTest, ReorganizeToUnmanagedArea) {
768 std::string app_id = "example";
772 AddLocalFolder(app_id, FPL("folder_src"));
773 AddOrUpdateLocalFile(app_id, FPL("folder_src/file_orphaned"), "abcde");
774 AddOrUpdateLocalFile(app_id, FPL("folder_src/file_under_sync_root"), "123");
775 AddOrUpdateLocalFile(app_id, FPL("folder_src/file_under_drive_root"), "hoge");
777 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
780 std::string file_orphaned_id =
781 GetFileIDByPath(app_id, FPL("folder_src/file_orphaned"));
782 std::string file_under_sync_root_id =
783 GetFileIDByPath(app_id, FPL("folder_src/file_under_sync_root"));
784 std::string file_under_drive_root_id =
785 GetFileIDByPath(app_id, FPL("folder_src/file_under_drive_root"));
787 std::string folder_id = GetFileIDByPath(app_id, FPL("folder_src"));
788 std::string sync_root_folder_id;
789 EXPECT_EQ(google_apis::HTTP_SUCCESS,
790 fake_drive_service_helper()->GetSyncRootFolderID(
791 &sync_root_folder_id));
793 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
794 fake_drive_service_helper()->RemoveResourceFromDirectory(
795 folder_id, file_orphaned_id));
796 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
797 fake_drive_service_helper()->RemoveResourceFromDirectory(
798 folder_id, file_under_sync_root_id));
799 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
800 fake_drive_service_helper()->RemoveResourceFromDirectory(
801 folder_id, file_under_drive_root_id));
803 EXPECT_EQ(google_apis::HTTP_SUCCESS,
804 fake_drive_service_helper()->AddResourceToDirectory(
805 sync_root_folder_id, file_under_sync_root_id));
806 EXPECT_EQ(google_apis::HTTP_SUCCESS,
807 fake_drive_service_helper()->AddResourceToDirectory(
808 fake_drive_service()->GetRootResourceId(),
809 file_under_drive_root_id));
811 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
814 EXPECT_EQ(1u, CountApp());
815 EXPECT_EQ(2u, CountLocalFile(app_id));
817 EXPECT_EQ(4u, CountMetadata());
818 EXPECT_EQ(4u, CountTracker());
821 TEST_F(DriveBackendSyncTest, ReorganizeToMultipleParents) {
822 std::string app_id = "example";
826 AddLocalFolder(app_id, FPL("parent1"));
827 AddLocalFolder(app_id, FPL("parent2"));
828 AddOrUpdateLocalFile(app_id, FPL("parent1/file"), "abcde");
830 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
833 std::string file_id = GetFileIDByPath(app_id, FPL("parent1/file"));
834 std::string parent2_folder_id = GetFileIDByPath(app_id, FPL("parent2"));
835 EXPECT_EQ(google_apis::HTTP_SUCCESS,
836 fake_drive_service_helper()->AddResourceToDirectory(
837 parent2_folder_id, file_id));
839 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
842 EXPECT_EQ(1u, CountApp());
843 EXPECT_EQ(4u, CountLocalFile(app_id));
844 VerifyLocalFolder(app_id, FPL("parent1"));
845 VerifyLocalFolder(app_id, FPL("parent2"));
846 VerifyLocalFile(app_id, FPL("parent1/file"), "abcde");
848 EXPECT_EQ(5u, CountMetadata());
849 EXPECT_EQ(5u, CountTracker());
852 TEST_F(DriveBackendSyncTest, ReorganizeAndRevert) {
853 std::string app_id = "example";
857 AddLocalFolder(app_id, FPL("folder"));
858 AddLocalFolder(app_id, FPL("folder_temp"));
859 AddOrUpdateLocalFile(app_id, FPL("folder/file"), "abcde");
861 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
864 std::string file_id = GetFileIDByPath(app_id, FPL("folder/file"));
865 std::string folder_id = GetFileIDByPath(app_id, FPL("folder"));
866 std::string folder_temp_id = GetFileIDByPath(app_id, FPL("folder_temp"));
867 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
868 fake_drive_service_helper()->RemoveResourceFromDirectory(
869 folder_id, file_id));
870 EXPECT_EQ(google_apis::HTTP_SUCCESS,
871 fake_drive_service_helper()->AddResourceToDirectory(
872 folder_temp_id, file_id));
874 FetchRemoteChanges();
876 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
877 fake_drive_service_helper()->RemoveResourceFromDirectory(
878 folder_temp_id, file_id));
879 EXPECT_EQ(google_apis::HTTP_SUCCESS,
880 fake_drive_service_helper()->AddResourceToDirectory(
881 folder_id, file_id));
883 FetchRemoteChanges();
885 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
888 EXPECT_EQ(1u, CountApp());
889 EXPECT_EQ(4u, CountLocalFile(app_id));
890 VerifyLocalFolder(app_id, FPL("folder"));
891 VerifyLocalFile(app_id, FPL("folder/file"), "abcde");
893 EXPECT_EQ(5u, CountMetadata());
894 EXPECT_EQ(5u, CountTracker());
897 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_AddFolder) {
898 std::string app_id = "example";
901 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
903 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
904 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
906 std::string remote_folder_id;
907 EXPECT_EQ(google_apis::HTTP_CREATED,
908 fake_drive_service_helper()->AddFolder(
910 "conflict_to_pending_remote", &remote_folder_id));
912 FetchRemoteChanges();
914 EXPECT_EQ(google_apis::HTTP_CREATED,
915 fake_drive_service_helper()->AddFolder(
917 "conflict_to_existing_remote", &remote_folder_id));
919 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
922 EXPECT_EQ(1u, CountApp());
923 EXPECT_EQ(3u, CountLocalFile(app_id));
924 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
925 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
927 EXPECT_EQ(4u, CountMetadata());
928 EXPECT_EQ(4u, CountTracker());
931 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_DeleteFolder) {
932 std::string app_id = "example";
936 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
937 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
939 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
942 // Test body starts from here.
943 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
944 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
945 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
946 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
948 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
949 fake_drive_service_helper()->DeleteResource(
950 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
952 FetchRemoteChanges();
954 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
955 fake_drive_service_helper()->DeleteResource(
956 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
958 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
961 EXPECT_EQ(1u, CountApp());
962 EXPECT_EQ(2u, CountLocalFile(app_id));
963 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
965 EXPECT_EQ(3u, CountMetadata());
966 EXPECT_EQ(3u, CountTracker());
969 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_AddFile) {
970 std::string app_id = "example";
973 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
975 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
976 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
979 EXPECT_EQ(google_apis::HTTP_SUCCESS,
980 fake_drive_service_helper()->AddFile(
981 app_root_folder_id, "conflict_to_pending_remote", "foo",
983 EXPECT_EQ(google_apis::HTTP_SUCCESS,
984 fake_drive_service_helper()->UpdateModificationTime(
986 base::Time::Now() + base::TimeDelta::FromDays(1)));
988 FetchRemoteChanges();
990 EXPECT_EQ(google_apis::HTTP_SUCCESS,
991 fake_drive_service_helper()->AddFile(
992 app_root_folder_id, "conflict_to_existing_remote", "foo",
994 EXPECT_EQ(google_apis::HTTP_SUCCESS,
995 fake_drive_service_helper()->UpdateModificationTime(
997 base::Time::Now() + base::TimeDelta::FromDays(1)));
999 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1000 VerifyConsistency();
1002 EXPECT_EQ(1u, CountApp());
1003 EXPECT_EQ(3u, CountLocalFile(app_id));
1004 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1005 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1007 EXPECT_EQ(4u, CountMetadata());
1008 EXPECT_EQ(4u, CountTracker());
1011 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_DeleteFile) {
1012 std::string app_id = "example";
1014 RegisterApp(app_id);
1015 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1017 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1018 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1020 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1021 VerifyConsistency();
1023 // Test body starts from here.
1024 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1025 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1027 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1028 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1030 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1031 fake_drive_service_helper()->DeleteResource(
1032 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1034 FetchRemoteChanges();
1036 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1037 fake_drive_service_helper()->DeleteResource(
1038 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1040 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1041 VerifyConsistency();
1043 EXPECT_EQ(1u, CountApp());
1044 EXPECT_EQ(3u, CountLocalFile(app_id));
1045 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1046 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1048 EXPECT_EQ(4u, CountMetadata());
1049 EXPECT_EQ(4u, CountTracker());
1052 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_AddFolder) {
1053 std::string app_id = "example";
1055 RegisterApp(app_id);
1056 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1057 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1058 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1060 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1061 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1063 std::string file_id;
1064 EXPECT_EQ(google_apis::HTTP_CREATED,
1065 fake_drive_service_helper()->AddFolder(
1067 "conflict_to_pending_remote", &file_id));
1069 FetchRemoteChanges();
1071 EXPECT_EQ(google_apis::HTTP_CREATED,
1072 fake_drive_service_helper()->AddFolder(
1074 "conflict_to_existing_remote", NULL));
1076 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1077 VerifyConsistency();
1079 EXPECT_EQ(1u, CountApp());
1080 EXPECT_EQ(3u, CountLocalFile(app_id));
1081 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1082 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1084 EXPECT_EQ(4u, CountMetadata());
1085 EXPECT_EQ(4u, CountTracker());
1088 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_DeleteFolder) {
1089 std::string app_id = "example";
1091 RegisterApp(app_id);
1092 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1094 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1095 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1097 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1098 VerifyConsistency();
1100 // Test body starts from here.
1101 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1102 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1104 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1105 fake_drive_service_helper()->DeleteResource(
1106 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1108 FetchRemoteChanges();
1110 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1111 fake_drive_service_helper()->DeleteResource(
1112 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1114 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1115 VerifyConsistency();
1117 EXPECT_EQ(1u, CountApp());
1118 EXPECT_EQ(1u, CountLocalFile(app_id));
1120 EXPECT_EQ(2u, CountMetadata());
1121 EXPECT_EQ(2u, CountTracker());
1124 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_AddFile) {
1125 std::string app_id = "example";
1127 RegisterApp(app_id);
1128 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1130 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1131 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1132 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1133 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1135 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1136 fake_drive_service_helper()->AddFile(
1137 app_root_folder_id, "conflict_to_pending_remote", "foo", NULL));
1139 FetchRemoteChanges();
1141 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1142 fake_drive_service_helper()->AddFile(
1143 app_root_folder_id, "conflict_to_existing_remote", "bar",
1146 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1147 VerifyConsistency();
1149 EXPECT_EQ(1u, CountApp());
1150 EXPECT_EQ(3u, CountLocalFile(app_id));
1151 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1152 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1154 EXPECT_EQ(4u, CountMetadata());
1155 EXPECT_EQ(4u, CountTracker());
1158 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_DeleteFile) {
1159 std::string app_id = "example";
1161 RegisterApp(app_id);
1162 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 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1173 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1174 fake_drive_service_helper()->DeleteResource(
1175 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1177 FetchRemoteChanges();
1179 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1180 fake_drive_service_helper()->DeleteResource(
1181 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1183 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1184 VerifyConsistency();
1186 EXPECT_EQ(1u, CountApp());
1187 EXPECT_EQ(1u, CountLocalFile(app_id));
1189 EXPECT_EQ(2u, CountMetadata());
1190 EXPECT_EQ(2u, CountTracker());
1193 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_AddFolder) {
1194 std::string app_id = "example";
1196 RegisterApp(app_id);
1197 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1199 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1200 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1202 std::string file_id;
1203 EXPECT_EQ(google_apis::HTTP_CREATED,
1204 fake_drive_service_helper()->AddFolder(
1205 app_root_folder_id, "conflict_to_pending_remote",
1207 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1208 fake_drive_service_helper()->UpdateModificationTime(
1210 base::Time::Now() - base::TimeDelta::FromDays(1)));
1212 FetchRemoteChanges();
1214 EXPECT_EQ(google_apis::HTTP_CREATED,
1215 fake_drive_service_helper()->AddFolder(
1216 app_root_folder_id, "conflict_to_existing_remote",
1218 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1219 fake_drive_service_helper()->UpdateModificationTime(
1221 base::Time::Now() - base::TimeDelta::FromDays(1)));
1223 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1224 VerifyConsistency();
1226 EXPECT_EQ(1u, CountApp());
1227 EXPECT_EQ(3u, CountLocalFile(app_id));
1228 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1229 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1231 EXPECT_EQ(4u, CountMetadata());
1232 EXPECT_EQ(4u, CountTracker());
1235 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_DeleteFolder) {
1236 std::string app_id = "example";
1238 RegisterApp(app_id);
1239 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1241 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1242 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1244 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1245 VerifyConsistency();
1247 // Test body starts from here.
1248 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1249 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1250 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1251 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
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(3u, CountLocalFile(app_id));
1268 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1269 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1271 EXPECT_EQ(4u, CountMetadata());
1272 EXPECT_EQ(4u, CountTracker());
1275 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_AddFile) {
1276 std::string app_id = "example";
1278 RegisterApp(app_id);
1280 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1281 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1282 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1284 std::string file_id;
1285 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1286 fake_drive_service_helper()->AddFile(
1287 app_root_folder_id, "conflict_to_pending_remote", "foo",
1289 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1290 fake_drive_service_helper()->UpdateModificationTime(
1292 base::Time::Now() + base::TimeDelta::FromDays(1)));
1294 FetchRemoteChanges();
1296 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1297 fake_drive_service_helper()->AddFile(
1298 app_root_folder_id, "conflict_to_existing_remote", "bar",
1300 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1301 fake_drive_service_helper()->UpdateModificationTime(
1303 base::Time::Now() + base::TimeDelta::FromDays(1)));
1305 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1306 VerifyConsistency();
1308 EXPECT_EQ(1u, CountApp());
1309 EXPECT_EQ(3u, CountLocalFile(app_id));
1310 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1311 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1313 EXPECT_EQ(4u, CountMetadata());
1314 EXPECT_EQ(4u, CountTracker());
1317 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_DeleteFile) {
1318 std::string app_id = "example";
1320 RegisterApp(app_id);
1322 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1323 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1325 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1326 VerifyConsistency();
1328 // Test body starts from here.
1329 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1330 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1331 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1332 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1334 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1335 fake_drive_service_helper()->DeleteResource(
1336 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1338 FetchRemoteChanges();
1340 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1341 fake_drive_service_helper()->DeleteResource(
1342 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1344 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1345 VerifyConsistency();
1347 EXPECT_EQ(1u, CountApp());
1348 EXPECT_EQ(3u, CountLocalFile(app_id));
1349 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1350 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1352 EXPECT_EQ(4u, CountMetadata());
1353 EXPECT_EQ(4u, CountTracker());
1356 TEST_F(DriveBackendSyncTest, ConflictTest_UpdateFile_DeleteFile) {
1357 std::string app_id = "example";
1359 RegisterApp(app_id);
1361 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1362 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1364 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1365 VerifyConsistency();
1367 // Test body starts from here.
1368 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1369 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1371 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1372 fake_drive_service_helper()->DeleteResource(
1373 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1375 FetchRemoteChanges();
1377 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1378 fake_drive_service_helper()->DeleteResource(
1379 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1381 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1382 VerifyConsistency();
1384 EXPECT_EQ(1u, CountApp());
1385 EXPECT_EQ(3u, CountLocalFile(app_id));
1386 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1387 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1389 EXPECT_EQ(4u, CountMetadata());
1390 EXPECT_EQ(4u, CountTracker());
1393 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_AddFolder) {
1394 std::string app_id = "example";
1396 RegisterApp(app_id);
1398 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1399 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1400 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1402 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1403 VerifyConsistency();
1405 // Test body starts from here.
1406 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1407 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1409 EXPECT_EQ(google_apis::HTTP_CREATED,
1410 fake_drive_service_helper()->AddFolder(
1411 app_root_folder_id, "conflict_to_pending_remote", NULL));
1413 FetchRemoteChanges();
1415 EXPECT_EQ(google_apis::HTTP_CREATED,
1416 fake_drive_service_helper()->AddFolder(
1417 app_root_folder_id, "conflict_to_existing_remote", NULL));
1419 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1420 VerifyConsistency();
1422 EXPECT_EQ(1u, CountApp());
1423 EXPECT_EQ(3u, CountLocalFile(app_id));
1424 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1425 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1427 EXPECT_EQ(4u, CountMetadata());
1428 EXPECT_EQ(4u, CountTracker());
1431 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_DeleteFolder) {
1432 std::string app_id = "example";
1434 RegisterApp(app_id);
1436 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1437 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1439 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1440 VerifyConsistency();
1442 // Test body starts from here.
1443 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1444 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1446 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1447 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1449 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1450 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1452 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1453 fake_drive_service_helper()->DeleteResource(
1454 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1456 FetchRemoteChanges();
1458 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1459 fake_drive_service_helper()->DeleteResource(
1460 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1462 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1463 VerifyConsistency();
1465 EXPECT_EQ(1u, CountApp());
1466 EXPECT_EQ(1u, CountLocalFile(app_id));
1468 EXPECT_EQ(2u, CountMetadata());
1469 EXPECT_EQ(2u, CountTracker());
1472 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_AddFile) {
1473 std::string app_id = "example";
1475 RegisterApp(app_id);
1477 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1478 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1479 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1480 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1481 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1483 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1484 fake_drive_service_helper()->AddFile(
1485 app_root_folder_id, "conflict_to_pending_remote", "hoge",
1488 FetchRemoteChanges();
1490 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1491 fake_drive_service_helper()->AddFile(
1492 app_root_folder_id, "conflict_to_existing_remote", "fuga",
1495 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1496 VerifyConsistency();
1498 EXPECT_EQ(1u, CountApp());
1499 EXPECT_EQ(3u, CountLocalFile(app_id));
1500 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1501 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1503 EXPECT_EQ(4u, CountMetadata());
1504 EXPECT_EQ(4u, CountTracker());
1507 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_UpdateFile) {
1508 std::string app_id = "example";
1510 RegisterApp(app_id);
1512 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1513 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1514 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1516 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1517 VerifyConsistency();
1519 // Test body starts from here.
1520 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1521 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1523 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1524 fake_drive_service_helper()->UpdateFile(
1525 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote")),
1528 FetchRemoteChanges();
1530 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1531 fake_drive_service_helper()->UpdateFile(
1532 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote")),
1535 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1536 VerifyConsistency();
1538 EXPECT_EQ(1u, CountApp());
1539 EXPECT_EQ(3u, CountLocalFile(app_id));
1540 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1541 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1543 EXPECT_EQ(4u, CountMetadata());
1544 EXPECT_EQ(4u, CountTracker());
1547 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_DeleteFile) {
1548 std::string app_id = "example";
1550 RegisterApp(app_id);
1552 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1553 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1554 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1556 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1557 VerifyConsistency();
1559 // Test body starts from here.
1560 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1561 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1563 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1564 fake_drive_service_helper()->DeleteResource(
1565 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1567 FetchRemoteChanges();
1569 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1570 fake_drive_service_helper()->DeleteResource(
1571 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1573 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1574 VerifyConsistency();
1576 EXPECT_EQ(1u, CountApp());
1577 EXPECT_EQ(1u, CountLocalFile(app_id));
1579 EXPECT_EQ(2u, CountMetadata());
1580 EXPECT_EQ(2u, CountTracker());
1583 } // namespace drive_backend
1584 } // namespace sync_file_system