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/sync_file_system/drive_backend/drive_backend_constants.h"
14 #include "chrome/browser/sync_file_system/drive_backend/fake_drive_service_helper.h"
15 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
16 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
17 #include "chrome/browser/sync_file_system/drive_backend/sync_engine.h"
18 #include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h"
19 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h"
20 #include "chrome/browser/sync_file_system/local/local_file_sync_service.h"
21 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h"
22 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h"
23 #include "chrome/browser/sync_file_system/syncable_file_system_util.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "content/public/test/test_browser_thread.h"
26 #include "content/public/test/test_browser_thread_bundle.h"
27 #include "extensions/common/extension.h"
28 #include "google_apis/drive/drive_api_parser.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
31 #include "third_party/leveldatabase/src/include/leveldb/env.h"
32 #include "webkit/browser/fileapi/file_system_context.h"
34 #define FPL(a) FILE_PATH_LITERAL(a)
36 namespace sync_file_system {
37 namespace drive_backend {
39 typedef fileapi::FileSystemOperation::FileEntryList FileEntryList;
41 class DriveBackendSyncTest : public testing::Test,
42 public LocalFileSyncService::Observer,
43 public RemoteFileSyncService::Observer {
45 DriveBackendSyncTest()
46 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
47 pending_remote_changes_(0),
48 pending_local_changes_(0) {}
49 virtual ~DriveBackendSyncTest() {}
51 virtual void SetUp() OVERRIDE {
52 ASSERT_TRUE(base_dir_.CreateUniqueTempDir());
53 in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
55 io_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread(
56 content::BrowserThread::IO);
57 file_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread(
58 content::BrowserThread::FILE);
60 RegisterSyncableFileSystem();
61 local_sync_service_ = LocalFileSyncService::CreateForTesting(
62 &profile_, in_memory_env_.get());
63 local_sync_service_->AddChangeObserver(this);
65 scoped_ptr<drive::FakeDriveService> drive_service(
66 new drive::FakeDriveService());
67 drive_service->Initialize("test@example.com");
68 ASSERT_TRUE(drive_service->LoadAccountMetadataForWapi(
69 "sync_file_system/account_metadata.json"));
70 ASSERT_TRUE(drive_service->LoadResourceListForWapi(
71 "gdata/root_feed.json"));
73 scoped_ptr<drive::DriveUploaderInterface> uploader(
74 new drive::DriveUploader(drive_service.get(),
75 file_task_runner_.get()));
77 remote_sync_service_.reset(new SyncEngine(
79 file_task_runner_.get(),
80 drive_service.PassAs<drive::DriveServiceInterface>(),
82 NULL, NULL, NULL, in_memory_env_.get()));
83 remote_sync_service_->AddServiceObserver(this);
84 remote_sync_service_->Initialize();
86 fake_drive_service_helper_.reset(new FakeDriveServiceHelper(
87 fake_drive_service(), drive_uploader(),
88 kSyncRootFolderTitle));
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 (ScopedVector<google_apis::ResourceEntry>::iterator itr =
349 remote_entries.begin();
350 itr != remote_entries.end();
352 const google_apis::ResourceEntry& remote_entry = **itr;
353 EXPECT_FALSE(ContainsKey(remote_entry_by_title, remote_entry.title()));
354 remote_entry_by_title[remote_entry.title()] = *itr;
357 fileapi::FileSystemURL url(CreateURL(app_id, path));
358 FileEntryList local_entries;
359 EXPECT_EQ(base::File::FILE_OK,
360 file_system->ReadDirectory(url, &local_entries));
361 for (FileEntryList::iterator itr = local_entries.begin();
362 itr != local_entries.end();
364 const fileapi::DirectoryEntry& local_entry = *itr;
365 fileapi::FileSystemURL entry_url(
366 CreateURL(app_id, path.Append(local_entry.name)));
368 fileapi::VirtualPath::BaseName(entry_url.path()).AsUTF8Unsafe();
369 SCOPED_TRACE(testing::Message() << "Verifying entry: " << title);
371 ASSERT_TRUE(ContainsKey(remote_entry_by_title, title));
372 const google_apis::ResourceEntry& remote_entry =
373 *remote_entry_by_title[title];
374 if (local_entry.is_directory) {
375 ASSERT_TRUE(remote_entry.is_folder());
376 VerifyConsistencyForFolder(app_id, entry_url.path(),
377 remote_entry.resource_id(),
380 ASSERT_TRUE(remote_entry.is_file());
381 VerifyConsistencyForFile(app_id, entry_url.path(),
382 remote_entry.resource_id(),
385 remote_entry_by_title.erase(title);
388 EXPECT_TRUE(remote_entry_by_title.empty());
391 void VerifyConsistencyForFile(const std::string& app_id,
392 const base::FilePath& path,
393 const std::string& file_id,
394 CannedSyncableFileSystem* file_system) {
395 fileapi::FileSystemURL url(CreateURL(app_id, path));
396 std::string file_content;
397 EXPECT_EQ(google_apis::HTTP_SUCCESS,
398 fake_drive_service_helper_->ReadFile(file_id, &file_content));
399 EXPECT_EQ(base::File::FILE_OK,
400 file_system->VerifyFile(url, file_content));
404 return file_systems_.size();
407 size_t CountLocalFile(const std::string& app_id) {
408 if (!ContainsKey(file_systems_, app_id))
411 CannedSyncableFileSystem* file_system = file_systems_[app_id];
412 std::stack<base::FilePath> folders;
413 folders.push(base::FilePath()); // root folder
416 while (!folders.empty()) {
417 fileapi::FileSystemURL url(CreateURL(app_id, folders.top()));
420 FileEntryList entries;
421 EXPECT_EQ(base::File::FILE_OK,
422 file_system->ReadDirectory(url, &entries));
423 for (FileEntryList::iterator itr = entries.begin();
424 itr != entries.end(); ++itr) {
426 if (itr->is_directory)
427 folders.push(url.path().Append(itr->name));
434 void VerifyLocalFile(const std::string& app_id,
435 const base::FilePath::StringType& path,
436 const std::string& content) {
437 SCOPED_TRACE(testing::Message() << "Verifying local file: "
438 << "app_id = " << app_id
439 << ", path = " << path);
440 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
441 EXPECT_EQ(base::File::FILE_OK,
442 file_systems_[app_id]->VerifyFile(
443 CreateURL(app_id, path), content));
446 void VerifyLocalFolder(const std::string& app_id,
447 const base::FilePath::StringType& path) {
448 SCOPED_TRACE(testing::Message() << "Verifying local file: "
449 << "app_id = " << app_id
450 << ", path = " << path);
451 ASSERT_TRUE(ContainsKey(file_systems_, app_id));
452 EXPECT_EQ(base::File::FILE_OK,
453 file_systems_[app_id]->DirectoryExists(CreateURL(app_id, path)));
456 size_t CountMetadata() {
457 return metadata_database()->file_by_id_.size();
460 size_t CountTracker() {
461 return metadata_database()->tracker_by_id_.size();
464 drive::FakeDriveService* fake_drive_service() {
465 return static_cast<drive::FakeDriveService*>(
466 remote_sync_service_->GetDriveService());
469 drive::DriveUploaderInterface* drive_uploader() {
470 return remote_sync_service_->GetDriveUploader();
473 FakeDriveServiceHelper* fake_drive_service_helper() {
474 return fake_drive_service_helper_.get();
477 MetadataDatabase* metadata_database() {
478 return remote_sync_service_->GetMetadataDatabase();
482 content::TestBrowserThreadBundle thread_bundle_;
483 ScopedEnableSyncFSV2 enable_syncfs_v2_;
485 base::ScopedTempDir base_dir_;
486 scoped_ptr<leveldb::Env> in_memory_env_;
487 TestingProfile profile_;
489 scoped_ptr<SyncEngine> remote_sync_service_;
490 scoped_ptr<LocalFileSyncService> local_sync_service_;
492 int64 pending_remote_changes_;
493 int64 pending_local_changes_;
495 scoped_ptr<FakeDriveServiceHelper> fake_drive_service_helper_;
496 std::map<std::string, CannedSyncableFileSystem*> file_systems_;
499 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
500 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
502 DISALLOW_COPY_AND_ASSIGN(DriveBackendSyncTest);
505 TEST_F(DriveBackendSyncTest, LocalToRemoteBasicTest) {
506 std::string app_id = "example";
509 AddOrUpdateLocalFile(app_id, FPL("file"), "abcde");
511 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
514 EXPECT_EQ(1u, CountApp());
515 EXPECT_EQ(2u, CountLocalFile(app_id));
516 VerifyLocalFile(app_id, FPL("file"), "abcde");
518 EXPECT_EQ(3u, CountMetadata());
519 EXPECT_EQ(3u, CountTracker());
522 TEST_F(DriveBackendSyncTest, RemoteToLocalBasicTest) {
523 std::string app_id = "example";
526 std::string app_root_folder_id;
527 EXPECT_TRUE(GetAppRootFolderID(app_id, &app_root_folder_id));
530 EXPECT_EQ(google_apis::HTTP_SUCCESS,
531 fake_drive_service_helper()->AddFile(
532 app_root_folder_id, "file", "abcde", &file_id));
534 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
537 EXPECT_EQ(1u, CountApp());
538 EXPECT_EQ(2u, CountLocalFile(app_id));
539 VerifyLocalFile(app_id, FPL("file"), "abcde");
541 EXPECT_EQ(3u, CountMetadata());
542 EXPECT_EQ(3u, CountTracker());
545 TEST_F(DriveBackendSyncTest, LocalFileUpdateTest) {
546 std::string app_id = "example";
547 const base::FilePath::StringType kPath(FPL("file"));
550 AddOrUpdateLocalFile(app_id, kPath, "abcde");
552 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
555 UpdateLocalFile(app_id, kPath, "1234567890");
557 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
560 EXPECT_EQ(1u, CountApp());
561 EXPECT_EQ(2u, CountLocalFile(app_id));
562 VerifyLocalFile(app_id, FPL("file"), "1234567890");
564 EXPECT_EQ(3u, CountMetadata());
565 EXPECT_EQ(3u, CountTracker());
568 TEST_F(DriveBackendSyncTest, RemoteFileUpdateTest) {
569 std::string app_id = "example";
572 std::string remote_file_id;
573 std::string app_root_folder_id;
574 EXPECT_TRUE(GetAppRootFolderID(app_id, &app_root_folder_id));
575 EXPECT_EQ(google_apis::HTTP_SUCCESS,
576 fake_drive_service_helper()->AddFile(
577 app_root_folder_id, "file", "abcde", &remote_file_id));
579 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
582 EXPECT_EQ(google_apis::HTTP_SUCCESS,
583 fake_drive_service_helper()->UpdateFile(
584 remote_file_id, "1234567890"));
586 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
589 EXPECT_EQ(1u, CountApp());
590 EXPECT_EQ(2u, CountLocalFile(app_id));
591 VerifyLocalFile(app_id, FPL("file"), "1234567890");
593 EXPECT_EQ(3u, CountMetadata());
594 EXPECT_EQ(3u, CountTracker());
597 TEST_F(DriveBackendSyncTest, LocalFileDeletionTest) {
598 std::string app_id = "example";
599 const base::FilePath::StringType path(FPL("file"));
602 AddOrUpdateLocalFile(app_id, path, "abcde");
604 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
607 RemoveLocal(app_id, path);
609 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
612 EXPECT_EQ(1u, CountApp());
613 EXPECT_EQ(1u, CountLocalFile(app_id));
615 EXPECT_EQ(2u, CountMetadata());
616 EXPECT_EQ(2u, CountTracker());
619 TEST_F(DriveBackendSyncTest, RemoteFileDeletionTest) {
620 std::string app_id = "example";
621 const base::FilePath::StringType path(FPL("file"));
624 AddOrUpdateLocalFile(app_id, path, "abcde");
626 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
629 std::string file_id = GetFileIDByPath(app_id, path);
630 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
631 fake_drive_service_helper()->DeleteResource(file_id));
633 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
636 EXPECT_EQ(1u, CountApp());
637 EXPECT_EQ(1u, CountLocalFile(app_id));
639 EXPECT_EQ(2u, CountMetadata());
640 EXPECT_EQ(2u, CountTracker());
643 TEST_F(DriveBackendSyncTest, RemoteRenameTest) {
644 std::string app_id = "example";
645 const base::FilePath::StringType path(FPL("file"));
648 AddOrUpdateLocalFile(app_id, path, "abcde");
650 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
653 std::string file_id = GetFileIDByPath(app_id, path);
654 EXPECT_EQ(google_apis::HTTP_SUCCESS,
655 fake_drive_service_helper()->RenameResource(
656 file_id, "renamed_file"));
658 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
661 EXPECT_EQ(1u, CountApp());
662 EXPECT_EQ(2u, CountLocalFile(app_id));
663 VerifyLocalFile(app_id, FPL("renamed_file"), "abcde");
665 EXPECT_EQ(3u, CountMetadata());
666 EXPECT_EQ(3u, CountTracker());
669 TEST_F(DriveBackendSyncTest, RemoteRenameAndRevertTest) {
670 std::string app_id = "example";
671 const base::FilePath::StringType path(FPL("file"));
674 AddOrUpdateLocalFile(app_id, path, "abcde");
676 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
679 std::string file_id = GetFileIDByPath(app_id, path);
680 EXPECT_EQ(google_apis::HTTP_SUCCESS,
681 fake_drive_service_helper()->RenameResource(
682 file_id, "renamed_file"));
684 FetchRemoteChanges();
686 EXPECT_EQ(google_apis::HTTP_SUCCESS,
687 fake_drive_service_helper()->RenameResource(
688 file_id, base::FilePath(path).AsUTF8Unsafe()));
690 FetchRemoteChanges();
692 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
695 EXPECT_EQ(1u, CountApp());
696 EXPECT_EQ(2u, CountLocalFile(app_id));
697 VerifyLocalFile(app_id, FPL("file"), "abcde");
699 EXPECT_EQ(3u, CountMetadata());
700 EXPECT_EQ(3u, CountTracker());
703 TEST_F(DriveBackendSyncTest, ReorganizeToOtherFolder) {
704 std::string app_id = "example";
705 const base::FilePath::StringType path(FPL("file"));
708 AddLocalFolder(app_id, FPL("folder_src"));
709 AddLocalFolder(app_id, FPL("folder_dest"));
710 AddOrUpdateLocalFile(app_id, FPL("folder_src/file"), "abcde");
712 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
715 std::string file_id = GetFileIDByPath(app_id, FPL("folder_src/file"));
716 std::string src_folder_id = GetFileIDByPath(app_id, FPL("folder_src"));
717 std::string dest_folder_id = GetFileIDByPath(app_id, FPL("folder_dest"));
718 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
719 fake_drive_service_helper()->RemoveResourceFromDirectory(
720 src_folder_id, file_id));
721 EXPECT_EQ(google_apis::HTTP_SUCCESS,
722 fake_drive_service_helper()->AddResourceToDirectory(
723 dest_folder_id, file_id));
725 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
728 EXPECT_EQ(1u, CountApp());
729 EXPECT_EQ(4u, CountLocalFile(app_id));
730 VerifyLocalFolder(app_id, FPL("folder_dest"));
731 VerifyLocalFile(app_id, FPL("folder_dest/file"), "abcde");
733 EXPECT_EQ(5u, CountMetadata());
734 EXPECT_EQ(5u, CountTracker());
737 TEST_F(DriveBackendSyncTest, ReorganizeToOtherApp) {
738 std::string src_app_id = "src_app";
739 std::string dest_app_id = "dest_app";
741 RegisterApp(src_app_id);
742 RegisterApp(dest_app_id);
744 AddLocalFolder(src_app_id, FPL("folder_src"));
745 AddLocalFolder(dest_app_id, FPL("folder_dest"));
746 AddOrUpdateLocalFile(src_app_id, FPL("folder_src/file"), "abcde");
748 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
751 std::string file_id = GetFileIDByPath(src_app_id, FPL("folder_src/file"));
752 std::string src_folder_id = GetFileIDByPath(src_app_id, FPL("folder_src"));
753 std::string dest_folder_id = GetFileIDByPath(dest_app_id, FPL("folder_dest"));
754 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
755 fake_drive_service_helper()->RemoveResourceFromDirectory(
756 src_folder_id, file_id));
757 EXPECT_EQ(google_apis::HTTP_SUCCESS,
758 fake_drive_service_helper()->AddResourceToDirectory(
759 dest_folder_id, file_id));
761 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
764 EXPECT_EQ(2u, CountApp());
765 EXPECT_EQ(2u, CountLocalFile(src_app_id));
766 EXPECT_EQ(3u, CountLocalFile(dest_app_id));
767 VerifyLocalFile(dest_app_id, FPL("folder_dest/file"), "abcde");
769 EXPECT_EQ(6u, CountMetadata());
770 EXPECT_EQ(6u, CountTracker());
773 TEST_F(DriveBackendSyncTest, ReorganizeToUnmanagedArea) {
774 std::string app_id = "example";
778 AddLocalFolder(app_id, FPL("folder_src"));
779 AddOrUpdateLocalFile(app_id, FPL("folder_src/file_orphaned"), "abcde");
780 AddOrUpdateLocalFile(app_id, FPL("folder_src/file_under_sync_root"), "123");
781 AddOrUpdateLocalFile(app_id, FPL("folder_src/file_under_drive_root"), "hoge");
783 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
786 std::string file_orphaned_id =
787 GetFileIDByPath(app_id, FPL("folder_src/file_orphaned"));
788 std::string file_under_sync_root_id =
789 GetFileIDByPath(app_id, FPL("folder_src/file_under_sync_root"));
790 std::string file_under_drive_root_id =
791 GetFileIDByPath(app_id, FPL("folder_src/file_under_drive_root"));
793 std::string folder_id = GetFileIDByPath(app_id, FPL("folder_src"));
794 std::string sync_root_folder_id;
795 EXPECT_EQ(google_apis::HTTP_SUCCESS,
796 fake_drive_service_helper()->GetSyncRootFolderID(
797 &sync_root_folder_id));
799 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
800 fake_drive_service_helper()->RemoveResourceFromDirectory(
801 folder_id, file_orphaned_id));
802 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
803 fake_drive_service_helper()->RemoveResourceFromDirectory(
804 folder_id, file_under_sync_root_id));
805 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
806 fake_drive_service_helper()->RemoveResourceFromDirectory(
807 folder_id, file_under_drive_root_id));
809 EXPECT_EQ(google_apis::HTTP_SUCCESS,
810 fake_drive_service_helper()->AddResourceToDirectory(
811 sync_root_folder_id, file_under_sync_root_id));
812 EXPECT_EQ(google_apis::HTTP_SUCCESS,
813 fake_drive_service_helper()->AddResourceToDirectory(
814 fake_drive_service()->GetRootResourceId(),
815 file_under_drive_root_id));
817 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
820 EXPECT_EQ(1u, CountApp());
821 EXPECT_EQ(2u, CountLocalFile(app_id));
823 EXPECT_EQ(4u, CountMetadata());
824 EXPECT_EQ(4u, CountTracker());
827 TEST_F(DriveBackendSyncTest, ReorganizeToMultipleParents) {
828 std::string app_id = "example";
832 AddLocalFolder(app_id, FPL("parent1"));
833 AddLocalFolder(app_id, FPL("parent2"));
834 AddOrUpdateLocalFile(app_id, FPL("parent1/file"), "abcde");
836 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
839 std::string file_id = GetFileIDByPath(app_id, FPL("parent1/file"));
840 std::string parent2_folder_id = GetFileIDByPath(app_id, FPL("parent2"));
841 EXPECT_EQ(google_apis::HTTP_SUCCESS,
842 fake_drive_service_helper()->AddResourceToDirectory(
843 parent2_folder_id, file_id));
845 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
848 EXPECT_EQ(1u, CountApp());
849 EXPECT_EQ(4u, CountLocalFile(app_id));
850 VerifyLocalFolder(app_id, FPL("parent1"));
851 VerifyLocalFolder(app_id, FPL("parent2"));
852 VerifyLocalFile(app_id, FPL("parent1/file"), "abcde");
854 EXPECT_EQ(5u, CountMetadata());
855 EXPECT_EQ(5u, CountTracker());
858 TEST_F(DriveBackendSyncTest, ReorganizeAndRevert) {
859 std::string app_id = "example";
863 AddLocalFolder(app_id, FPL("folder"));
864 AddLocalFolder(app_id, FPL("folder_temp"));
865 AddOrUpdateLocalFile(app_id, FPL("folder/file"), "abcde");
867 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
870 std::string file_id = GetFileIDByPath(app_id, FPL("folder/file"));
871 std::string folder_id = GetFileIDByPath(app_id, FPL("folder"));
872 std::string folder_temp_id = GetFileIDByPath(app_id, FPL("folder_temp"));
873 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
874 fake_drive_service_helper()->RemoveResourceFromDirectory(
875 folder_id, file_id));
876 EXPECT_EQ(google_apis::HTTP_SUCCESS,
877 fake_drive_service_helper()->AddResourceToDirectory(
878 folder_temp_id, file_id));
880 FetchRemoteChanges();
882 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
883 fake_drive_service_helper()->RemoveResourceFromDirectory(
884 folder_temp_id, file_id));
885 EXPECT_EQ(google_apis::HTTP_SUCCESS,
886 fake_drive_service_helper()->AddResourceToDirectory(
887 folder_id, file_id));
889 FetchRemoteChanges();
891 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
894 EXPECT_EQ(1u, CountApp());
895 EXPECT_EQ(4u, CountLocalFile(app_id));
896 VerifyLocalFolder(app_id, FPL("folder"));
897 VerifyLocalFile(app_id, FPL("folder/file"), "abcde");
899 EXPECT_EQ(5u, CountMetadata());
900 EXPECT_EQ(5u, CountTracker());
903 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_AddFolder) {
904 std::string app_id = "example";
907 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
909 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
910 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
912 std::string remote_folder_id;
913 EXPECT_EQ(google_apis::HTTP_CREATED,
914 fake_drive_service_helper()->AddFolder(
916 "conflict_to_pending_remote", &remote_folder_id));
918 FetchRemoteChanges();
920 EXPECT_EQ(google_apis::HTTP_CREATED,
921 fake_drive_service_helper()->AddFolder(
923 "conflict_to_existing_remote", &remote_folder_id));
925 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
928 EXPECT_EQ(1u, CountApp());
929 EXPECT_EQ(3u, CountLocalFile(app_id));
930 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
931 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
933 EXPECT_EQ(4u, CountMetadata());
934 EXPECT_EQ(4u, CountTracker());
937 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_DeleteFolder) {
938 std::string app_id = "example";
942 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
943 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
945 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
948 // Test body starts from here.
949 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
950 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
951 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
952 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
954 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
955 fake_drive_service_helper()->DeleteResource(
956 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
958 FetchRemoteChanges();
960 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
961 fake_drive_service_helper()->DeleteResource(
962 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
964 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
967 EXPECT_EQ(1u, CountApp());
968 EXPECT_EQ(2u, CountLocalFile(app_id));
969 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
971 EXPECT_EQ(3u, CountMetadata());
972 EXPECT_EQ(3u, CountTracker());
975 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_AddFile) {
976 std::string app_id = "example";
979 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
981 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
982 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
985 EXPECT_EQ(google_apis::HTTP_SUCCESS,
986 fake_drive_service_helper()->AddFile(
987 app_root_folder_id, "conflict_to_pending_remote", "foo",
989 EXPECT_EQ(google_apis::HTTP_SUCCESS,
990 fake_drive_service_helper()->UpdateModificationTime(
992 base::Time::Now() + base::TimeDelta::FromDays(1)));
994 FetchRemoteChanges();
996 EXPECT_EQ(google_apis::HTTP_SUCCESS,
997 fake_drive_service_helper()->AddFile(
998 app_root_folder_id, "conflict_to_existing_remote", "foo",
1000 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1001 fake_drive_service_helper()->UpdateModificationTime(
1003 base::Time::Now() + base::TimeDelta::FromDays(1)));
1005 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1006 VerifyConsistency();
1008 EXPECT_EQ(1u, CountApp());
1009 EXPECT_EQ(3u, CountLocalFile(app_id));
1010 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1011 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1013 EXPECT_EQ(4u, CountMetadata());
1014 EXPECT_EQ(4u, CountTracker());
1017 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_DeleteFile) {
1018 std::string app_id = "example";
1020 RegisterApp(app_id);
1021 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1023 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1024 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1026 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1027 VerifyConsistency();
1029 // Test body starts from here.
1030 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1031 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1033 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1034 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1036 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1037 fake_drive_service_helper()->DeleteResource(
1038 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1040 FetchRemoteChanges();
1042 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1043 fake_drive_service_helper()->DeleteResource(
1044 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1046 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1047 VerifyConsistency();
1049 EXPECT_EQ(1u, CountApp());
1050 EXPECT_EQ(3u, CountLocalFile(app_id));
1051 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1052 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1054 EXPECT_EQ(4u, CountMetadata());
1055 EXPECT_EQ(4u, CountTracker());
1058 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_AddFolder) {
1059 std::string app_id = "example";
1061 RegisterApp(app_id);
1062 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1063 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1064 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1066 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1067 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1069 std::string file_id;
1070 EXPECT_EQ(google_apis::HTTP_CREATED,
1071 fake_drive_service_helper()->AddFolder(
1073 "conflict_to_pending_remote", &file_id));
1075 FetchRemoteChanges();
1077 EXPECT_EQ(google_apis::HTTP_CREATED,
1078 fake_drive_service_helper()->AddFolder(
1080 "conflict_to_existing_remote", NULL));
1082 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1083 VerifyConsistency();
1085 EXPECT_EQ(1u, CountApp());
1086 EXPECT_EQ(3u, CountLocalFile(app_id));
1087 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1088 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1090 EXPECT_EQ(4u, CountMetadata());
1091 EXPECT_EQ(4u, CountTracker());
1094 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_DeleteFolder) {
1095 std::string app_id = "example";
1097 RegisterApp(app_id);
1098 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1100 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1101 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1103 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1104 VerifyConsistency();
1106 // Test body starts from here.
1107 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1108 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1110 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1111 fake_drive_service_helper()->DeleteResource(
1112 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1114 FetchRemoteChanges();
1116 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1117 fake_drive_service_helper()->DeleteResource(
1118 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1120 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1121 VerifyConsistency();
1123 EXPECT_EQ(1u, CountApp());
1124 EXPECT_EQ(1u, CountLocalFile(app_id));
1126 EXPECT_EQ(2u, CountMetadata());
1127 EXPECT_EQ(2u, CountTracker());
1130 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_AddFile) {
1131 std::string app_id = "example";
1133 RegisterApp(app_id);
1134 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1136 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1137 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1138 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1139 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1141 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1142 fake_drive_service_helper()->AddFile(
1143 app_root_folder_id, "conflict_to_pending_remote", "foo", NULL));
1145 FetchRemoteChanges();
1147 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1148 fake_drive_service_helper()->AddFile(
1149 app_root_folder_id, "conflict_to_existing_remote", "bar",
1152 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1153 VerifyConsistency();
1155 EXPECT_EQ(1u, CountApp());
1156 EXPECT_EQ(3u, CountLocalFile(app_id));
1157 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1158 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1160 EXPECT_EQ(4u, CountMetadata());
1161 EXPECT_EQ(4u, CountTracker());
1164 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_DeleteFile) {
1165 std::string app_id = "example";
1167 RegisterApp(app_id);
1168 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1169 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1170 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1172 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1173 VerifyConsistency();
1175 // Test body starts from here.
1176 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1177 RemoveLocal(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(1u, CountLocalFile(app_id));
1195 EXPECT_EQ(2u, CountMetadata());
1196 EXPECT_EQ(2u, CountTracker());
1199 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_AddFolder) {
1200 std::string app_id = "example";
1202 RegisterApp(app_id);
1203 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1205 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1206 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1208 std::string file_id;
1209 EXPECT_EQ(google_apis::HTTP_CREATED,
1210 fake_drive_service_helper()->AddFolder(
1211 app_root_folder_id, "conflict_to_pending_remote",
1213 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1214 fake_drive_service_helper()->UpdateModificationTime(
1216 base::Time::Now() - base::TimeDelta::FromDays(1)));
1218 FetchRemoteChanges();
1220 EXPECT_EQ(google_apis::HTTP_CREATED,
1221 fake_drive_service_helper()->AddFolder(
1222 app_root_folder_id, "conflict_to_existing_remote",
1224 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1225 fake_drive_service_helper()->UpdateModificationTime(
1227 base::Time::Now() - base::TimeDelta::FromDays(1)));
1229 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1230 VerifyConsistency();
1232 EXPECT_EQ(1u, CountApp());
1233 EXPECT_EQ(3u, CountLocalFile(app_id));
1234 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1235 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1237 EXPECT_EQ(4u, CountMetadata());
1238 EXPECT_EQ(4u, CountTracker());
1241 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_DeleteFolder) {
1242 std::string app_id = "example";
1244 RegisterApp(app_id);
1245 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1247 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1248 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1250 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1251 VerifyConsistency();
1253 // Test body starts from here.
1254 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1255 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1256 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1257 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1259 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1260 fake_drive_service_helper()->DeleteResource(
1261 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1263 FetchRemoteChanges();
1265 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1266 fake_drive_service_helper()->DeleteResource(
1267 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1269 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1270 VerifyConsistency();
1272 EXPECT_EQ(1u, CountApp());
1273 EXPECT_EQ(3u, CountLocalFile(app_id));
1274 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1275 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1277 EXPECT_EQ(4u, CountMetadata());
1278 EXPECT_EQ(4u, CountTracker());
1281 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_AddFile) {
1282 std::string app_id = "example";
1284 RegisterApp(app_id);
1286 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1287 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1288 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1290 std::string file_id;
1291 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1292 fake_drive_service_helper()->AddFile(
1293 app_root_folder_id, "conflict_to_pending_remote", "foo",
1295 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1296 fake_drive_service_helper()->UpdateModificationTime(
1298 base::Time::Now() + base::TimeDelta::FromDays(1)));
1300 FetchRemoteChanges();
1302 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1303 fake_drive_service_helper()->AddFile(
1304 app_root_folder_id, "conflict_to_existing_remote", "bar",
1306 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1307 fake_drive_service_helper()->UpdateModificationTime(
1309 base::Time::Now() + base::TimeDelta::FromDays(1)));
1311 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1312 VerifyConsistency();
1314 EXPECT_EQ(1u, CountApp());
1315 EXPECT_EQ(3u, CountLocalFile(app_id));
1316 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1317 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1319 EXPECT_EQ(4u, CountMetadata());
1320 EXPECT_EQ(4u, CountTracker());
1323 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_DeleteFile) {
1324 std::string app_id = "example";
1326 RegisterApp(app_id);
1328 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1329 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1331 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1332 VerifyConsistency();
1334 // Test body starts from here.
1335 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1336 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1337 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1338 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1340 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1341 fake_drive_service_helper()->DeleteResource(
1342 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1344 FetchRemoteChanges();
1346 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1347 fake_drive_service_helper()->DeleteResource(
1348 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1350 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1351 VerifyConsistency();
1353 EXPECT_EQ(1u, CountApp());
1354 EXPECT_EQ(3u, CountLocalFile(app_id));
1355 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1356 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1358 EXPECT_EQ(4u, CountMetadata());
1359 EXPECT_EQ(4u, CountTracker());
1362 TEST_F(DriveBackendSyncTest, ConflictTest_UpdateFile_DeleteFile) {
1363 std::string app_id = "example";
1365 RegisterApp(app_id);
1367 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1368 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1370 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1371 VerifyConsistency();
1373 // Test body starts from here.
1374 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1375 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1377 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1378 fake_drive_service_helper()->DeleteResource(
1379 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1381 FetchRemoteChanges();
1383 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1384 fake_drive_service_helper()->DeleteResource(
1385 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1387 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1388 VerifyConsistency();
1390 EXPECT_EQ(1u, CountApp());
1391 EXPECT_EQ(3u, CountLocalFile(app_id));
1392 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1393 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1395 EXPECT_EQ(4u, CountMetadata());
1396 EXPECT_EQ(4u, CountTracker());
1399 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_AddFolder) {
1400 std::string app_id = "example";
1402 RegisterApp(app_id);
1404 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1405 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1406 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1408 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1409 VerifyConsistency();
1411 // Test body starts from here.
1412 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1413 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1415 EXPECT_EQ(google_apis::HTTP_CREATED,
1416 fake_drive_service_helper()->AddFolder(
1417 app_root_folder_id, "conflict_to_pending_remote", NULL));
1419 FetchRemoteChanges();
1421 EXPECT_EQ(google_apis::HTTP_CREATED,
1422 fake_drive_service_helper()->AddFolder(
1423 app_root_folder_id, "conflict_to_existing_remote", NULL));
1425 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1426 VerifyConsistency();
1428 EXPECT_EQ(1u, CountApp());
1429 EXPECT_EQ(3u, CountLocalFile(app_id));
1430 VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1431 VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1433 EXPECT_EQ(4u, CountMetadata());
1434 EXPECT_EQ(4u, CountTracker());
1437 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_DeleteFolder) {
1438 std::string app_id = "example";
1440 RegisterApp(app_id);
1442 AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
1443 AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
1445 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1446 VerifyConsistency();
1448 // Test body starts from here.
1449 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1450 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1452 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1453 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1455 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1456 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1458 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1459 fake_drive_service_helper()->DeleteResource(
1460 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1462 FetchRemoteChanges();
1464 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1465 fake_drive_service_helper()->DeleteResource(
1466 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1468 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1469 VerifyConsistency();
1471 EXPECT_EQ(1u, CountApp());
1472 EXPECT_EQ(1u, CountLocalFile(app_id));
1474 EXPECT_EQ(2u, CountMetadata());
1475 EXPECT_EQ(2u, CountTracker());
1478 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_AddFile) {
1479 std::string app_id = "example";
1481 RegisterApp(app_id);
1483 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1484 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1485 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1486 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1487 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1489 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1490 fake_drive_service_helper()->AddFile(
1491 app_root_folder_id, "conflict_to_pending_remote", "hoge",
1494 FetchRemoteChanges();
1496 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1497 fake_drive_service_helper()->AddFile(
1498 app_root_folder_id, "conflict_to_existing_remote", "fuga",
1501 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1502 VerifyConsistency();
1504 EXPECT_EQ(1u, CountApp());
1505 EXPECT_EQ(3u, CountLocalFile(app_id));
1506 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1507 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1509 EXPECT_EQ(4u, CountMetadata());
1510 EXPECT_EQ(4u, CountTracker());
1513 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_UpdateFile) {
1514 std::string app_id = "example";
1516 RegisterApp(app_id);
1518 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1519 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1520 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1522 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1523 VerifyConsistency();
1525 // Test body starts from here.
1526 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1527 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1529 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1530 fake_drive_service_helper()->UpdateFile(
1531 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote")),
1534 FetchRemoteChanges();
1536 EXPECT_EQ(google_apis::HTTP_SUCCESS,
1537 fake_drive_service_helper()->UpdateFile(
1538 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote")),
1541 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1542 VerifyConsistency();
1544 EXPECT_EQ(1u, CountApp());
1545 EXPECT_EQ(3u, CountLocalFile(app_id));
1546 VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
1547 VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
1549 EXPECT_EQ(4u, CountMetadata());
1550 EXPECT_EQ(4u, CountTracker());
1553 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_DeleteFile) {
1554 std::string app_id = "example";
1556 RegisterApp(app_id);
1558 std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
1559 AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
1560 AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
1562 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1563 VerifyConsistency();
1565 // Test body starts from here.
1566 RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
1567 RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
1569 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1570 fake_drive_service_helper()->DeleteResource(
1571 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
1573 FetchRemoteChanges();
1575 EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
1576 fake_drive_service_helper()->DeleteResource(
1577 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
1579 EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
1580 VerifyConsistency();
1582 EXPECT_EQ(1u, CountApp());
1583 EXPECT_EQ(1u, CountLocalFile(app_id));
1585 EXPECT_EQ(2u, CountMetadata());
1586 EXPECT_EQ(2u, CountTracker());
1589 } // namespace drive_backend
1590 } // namespace sync_file_system