Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync_file_system / drive_backend / drive_backend_sync_unittest.cc
index 819a2a2..3a0cbd7 100644 (file)
@@ -3,6 +3,7 @@
 // found in the LICENSE file.
 
 #include <algorithm>
+#include <stack>
 
 #include "base/file_util.h"
 #include "base/message_loop/message_loop.h"
 #include "content/public/test/test_browser_thread.h"
 #include "content/public/test/test_browser_thread_bundle.h"
 #include "extensions/common/extension.h"
+#include "google_apis/drive/drive_api_parser.h"
 #include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
+#include "third_party/leveldatabase/src/include/leveldb/env.h"
 #include "webkit/browser/fileapi/file_system_context.h"
 
 #define FPL(a) FILE_PATH_LITERAL(a)
 namespace sync_file_system {
 namespace drive_backend {
 
-class DriveBackendSyncTest : public testing::Test {
+typedef fileapi::FileSystemOperation::FileEntryList FileEntryList;
+
+class DriveBackendSyncTest : public testing::Test,
+                             public LocalFileSyncService::Observer,
+                             public RemoteFileSyncService::Observer {
  public:
   DriveBackendSyncTest()
-      : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {}
+      : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
+        pending_remote_changes_(0),
+        pending_local_changes_(0) {}
   virtual ~DriveBackendSyncTest() {}
 
   virtual void SetUp() OVERRIDE {
     ASSERT_TRUE(base_dir_.CreateUniqueTempDir());
+    in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
 
     io_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread(
         content::BrowserThread::IO);
@@ -47,7 +58,9 @@ class DriveBackendSyncTest : public testing::Test {
         content::BrowserThread::FILE);
 
     RegisterSyncableFileSystem();
-    local_sync_service_.reset(new LocalFileSyncService(&profile_));
+    local_sync_service_ = LocalFileSyncService::CreateForTesting(
+        &profile_, in_memory_env_.get());
+    local_sync_service_->AddChangeObserver(this);
 
     scoped_ptr<drive::FakeDriveService> drive_service(
         new drive::FakeDriveService());
@@ -66,7 +79,8 @@ class DriveBackendSyncTest : public testing::Test {
         file_task_runner_.get(),
         drive_service.PassAs<drive::DriveServiceInterface>(),
         uploader.Pass(),
-        NULL, NULL, NULL));
+        NULL, NULL, NULL, in_memory_env_.get()));
+    remote_sync_service_->AddServiceObserver(this);
     remote_sync_service_->Initialize();
 
     fake_drive_service_helper_.reset(new FakeDriveServiceHelper(
@@ -93,6 +107,14 @@ class DriveBackendSyncTest : public testing::Test {
     RevokeSyncableFileSystem();
   }
 
+  virtual void OnRemoteChangeQueueUpdated(int64 pending_changes_hint) OVERRIDE {
+    pending_remote_changes_ = pending_changes_hint;
+  }
+
+  virtual void OnLocalChangeAvailable(int64 pending_changes_hint) OVERRIDE {
+    pending_local_changes_ = pending_changes_hint;
+  }
+
  protected:
   fileapi::FileSystemURL CreateURL(const std::string& app_id,
                                    const base::FilePath::StringType& path) {
@@ -114,25 +136,29 @@ class DriveBackendSyncTest : public testing::Test {
     return true;
   }
 
-  bool GetFileIDByPath(const std::string& app_id,
-                       const base::FilePath& path,
-                       std::string* file_id) {
+  std::string GetFileIDByPath(const std::string& app_id,
+                              const base::FilePath::StringType& path) {
+    return GetFileIDByPath(app_id, base::FilePath(path));
+  }
+
+  std::string GetFileIDByPath(const std::string& app_id,
+                              const base::FilePath& path) {
     FileTracker tracker;
     base::FilePath result_path;
-    if (!metadata_database()->FindNearestActiveAncestor(
-            app_id, path, &tracker, &result_path) ||
-        path != result_path)
-      return false;
-    *file_id = tracker.file_id();
-    return true;
+    base::FilePath normalized_path = path.NormalizePathSeparators();
+    EXPECT_TRUE(metadata_database()->FindNearestActiveAncestor(
+        app_id, normalized_path, &tracker, &result_path));
+    EXPECT_EQ(normalized_path, result_path);
+    return tracker.file_id();
   }
 
   SyncStatusCode RegisterApp(const std::string& app_id) {
     GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id);
     if (!ContainsKey(file_systems_, app_id)) {
       CannedSyncableFileSystem* file_system = new CannedSyncableFileSystem(
-          origin, io_task_runner_.get(), file_task_runner_.get());
-      file_system->SetUp();
+          origin, in_memory_env_.get(),
+          io_task_runner_.get(), file_task_runner_.get());
+      file_system->SetUp(CannedSyncableFileSystem::QUOTA_DISABLED);
 
       SyncStatusCode status = SYNC_STATUS_UNKNOWN;
       local_sync_service_->MaybeInitializeFileSystemContext(
@@ -211,6 +237,15 @@ class DriveBackendSyncTest : public testing::Test {
     return status;
   }
 
+  int64 GetLargestChangeID() {
+    scoped_ptr<google_apis::AboutResource> about_resource;
+    EXPECT_EQ(google_apis::HTTP_SUCCESS,
+              fake_drive_service_helper()->GetAboutResource(&about_resource));
+    if (!about_resource)
+      return 0;
+    return about_resource->largest_change_id();
+  }
+
   void FetchRemoteChanges() {
     remote_sync_service_->OnNotificationReceived();
     base::RunLoop().RunUntilIdle();
@@ -219,20 +254,36 @@ class DriveBackendSyncTest : public testing::Test {
   SyncStatusCode ProcessChangesUntilDone() {
     SyncStatusCode local_sync_status;
     SyncStatusCode remote_sync_status;
-    do {
-      FetchRemoteChanges();
-
+    while (true) {
       local_sync_status = ProcessLocalChange();
       if (local_sync_status != SYNC_STATUS_OK &&
-          local_sync_status != SYNC_STATUS_NO_CHANGE_TO_SYNC)
+          local_sync_status != SYNC_STATUS_NO_CHANGE_TO_SYNC &&
+          local_sync_status != SYNC_STATUS_FILE_BUSY)
         return local_sync_status;
 
       remote_sync_status = ProcessRemoteChange();
       if (remote_sync_status != SYNC_STATUS_OK &&
-          remote_sync_status != SYNC_STATUS_NO_CHANGE_TO_SYNC)
+          remote_sync_status != SYNC_STATUS_NO_CHANGE_TO_SYNC &&
+          remote_sync_status != SYNC_STATUS_FILE_BUSY)
         return remote_sync_status;
-    } while (local_sync_status != SYNC_STATUS_NO_CHANGE_TO_SYNC ||
-             remote_sync_status != SYNC_STATUS_NO_CHANGE_TO_SYNC);
+
+      if (local_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC &&
+          remote_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC) {
+        remote_sync_service_->PromoteDemotedChanges();
+        local_sync_service_->PromoteDemotedChanges();
+
+        if (pending_remote_changes_ || pending_local_changes_)
+          continue;
+
+        int64 largest_fetched_change_id =
+            metadata_database()->GetLargestFetchedChangeID();
+        if (largest_fetched_change_id != GetLargestChangeID()) {
+          FetchRemoteChanges();
+          continue;
+        }
+        break;
+      }
+    }
     return SYNC_STATUS_OK;
   }
 
@@ -304,17 +355,19 @@ class DriveBackendSyncTest : public testing::Test {
     }
 
     fileapi::FileSystemURL url(CreateURL(app_id, path));
-    CannedSyncableFileSystem::FileEntryList local_entries;
+    FileEntryList local_entries;
     EXPECT_EQ(base::File::FILE_OK,
               file_system->ReadDirectory(url, &local_entries));
-    for (CannedSyncableFileSystem::FileEntryList::iterator itr =
-             local_entries.begin();
+    for (FileEntryList::iterator itr = local_entries.begin();
          itr != local_entries.end();
          ++itr) {
       const fileapi::DirectoryEntry& local_entry = *itr;
       fileapi::FileSystemURL entry_url(
           CreateURL(app_id, path.Append(local_entry.name)));
-      std::string title = entry_url.path().AsUTF8Unsafe();
+      std::string title =
+          fileapi::VirtualPath::BaseName(entry_url.path()).AsUTF8Unsafe();
+      SCOPED_TRACE(testing::Message() << "Verifying entry: " << title);
+
       ASSERT_TRUE(ContainsKey(remote_entry_by_title, title));
       const google_apis::ResourceEntry& remote_entry =
           *remote_entry_by_title[title];
@@ -347,6 +400,67 @@ class DriveBackendSyncTest : public testing::Test {
               file_system->VerifyFile(url, file_content));
   }
 
+  size_t CountApp() {
+    return file_systems_.size();
+  }
+
+  size_t CountLocalFile(const std::string& app_id) {
+    if (!ContainsKey(file_systems_, app_id))
+      return 0;
+
+    CannedSyncableFileSystem* file_system = file_systems_[app_id];
+    std::stack<base::FilePath> folders;
+    folders.push(base::FilePath()); // root folder
+
+    size_t result = 1;
+    while (!folders.empty()) {
+      fileapi::FileSystemURL url(CreateURL(app_id, folders.top()));
+      folders.pop();
+
+      FileEntryList entries;
+      EXPECT_EQ(base::File::FILE_OK,
+                file_system->ReadDirectory(url, &entries));
+      for (FileEntryList::iterator itr = entries.begin();
+           itr != entries.end(); ++itr) {
+        ++result;
+        if (itr->is_directory)
+          folders.push(url.path().Append(itr->name));
+      }
+    }
+
+    return result;
+  }
+
+  void VerifyLocalFile(const std::string& app_id,
+                       const base::FilePath::StringType& path,
+                       const std::string& content) {
+    SCOPED_TRACE(testing::Message() << "Verifying local file: "
+                                    << "app_id = " << app_id
+                                    << ", path = " << path);
+    ASSERT_TRUE(ContainsKey(file_systems_, app_id));
+    EXPECT_EQ(base::File::FILE_OK,
+              file_systems_[app_id]->VerifyFile(
+                  CreateURL(app_id, path), content));
+  }
+
+  void VerifyLocalFolder(const std::string& app_id,
+                         const base::FilePath::StringType& path) {
+    SCOPED_TRACE(testing::Message() << "Verifying local file: "
+                                    << "app_id = " << app_id
+                                    << ", path = " << path);
+    ASSERT_TRUE(ContainsKey(file_systems_, app_id));
+    EXPECT_EQ(base::File::FILE_OK,
+              file_systems_[app_id]->DirectoryExists(CreateURL(app_id, path)));
+  }
+
+  size_t CountMetadata() {
+    return metadata_database()->file_by_id_.size();
+  }
+
+  size_t CountTracker() {
+    return metadata_database()->tracker_by_id_.size();
+  }
+
   drive::FakeDriveService* fake_drive_service() {
     return static_cast<drive::FakeDriveService*>(
         remote_sync_service_->GetDriveService());
@@ -366,13 +480,18 @@ class DriveBackendSyncTest : public testing::Test {
 
  private:
   content::TestBrowserThreadBundle thread_bundle_;
+  ScopedEnableSyncFSV2 enable_syncfs_v2_;
 
   base::ScopedTempDir base_dir_;
+  scoped_ptr<leveldb::Env> in_memory_env_;
   TestingProfile profile_;
 
   scoped_ptr<SyncEngine> remote_sync_service_;
   scoped_ptr<LocalFileSyncService> local_sync_service_;
 
+  int64 pending_remote_changes_;
+  int64 pending_local_changes_;
+
   scoped_ptr<FakeDriveServiceHelper> fake_drive_service_helper_;
   std::map<std::string, CannedSyncableFileSystem*> file_systems_;
 
@@ -391,6 +510,13 @@ TEST_F(DriveBackendSyncTest, LocalToRemoteBasicTest) {
 
   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(2u, CountLocalFile(app_id));
+  VerifyLocalFile(app_id, FPL("file"), "abcde");
+
+  EXPECT_EQ(3u, CountMetadata());
+  EXPECT_EQ(3u, CountTracker());
 }
 
 TEST_F(DriveBackendSyncTest, RemoteToLocalBasicTest) {
@@ -407,6 +533,13 @@ TEST_F(DriveBackendSyncTest, RemoteToLocalBasicTest) {
 
   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(2u, CountLocalFile(app_id));
+  VerifyLocalFile(app_id, FPL("file"), "abcde");
+
+  EXPECT_EQ(3u, CountMetadata());
+  EXPECT_EQ(3u, CountTracker());
 }
 
 TEST_F(DriveBackendSyncTest, LocalFileUpdateTest) {
@@ -423,6 +556,13 @@ TEST_F(DriveBackendSyncTest, LocalFileUpdateTest) {
 
   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(2u, CountLocalFile(app_id));
+  VerifyLocalFile(app_id, FPL("file"), "1234567890");
+
+  EXPECT_EQ(3u, CountMetadata());
+  EXPECT_EQ(3u, CountTracker());
 }
 
 TEST_F(DriveBackendSyncTest, RemoteFileUpdateTest) {
@@ -445,6 +585,13 @@ TEST_F(DriveBackendSyncTest, RemoteFileUpdateTest) {
 
   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(2u, CountLocalFile(app_id));
+  VerifyLocalFile(app_id, FPL("file"), "1234567890");
+
+  EXPECT_EQ(3u, CountMetadata());
+  EXPECT_EQ(3u, CountTracker());
 }
 
 TEST_F(DriveBackendSyncTest, LocalFileDeletionTest) {
@@ -461,6 +608,12 @@ TEST_F(DriveBackendSyncTest, LocalFileDeletionTest) {
 
   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(1u, CountLocalFile(app_id));
+
+  EXPECT_EQ(2u, CountMetadata());
+  EXPECT_EQ(2u, CountTracker());
 }
 
 TEST_F(DriveBackendSyncTest, RemoteFileDeletionTest) {
@@ -473,13 +626,18 @@ TEST_F(DriveBackendSyncTest, RemoteFileDeletionTest) {
   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   VerifyConsistency();
 
-  std::string file_id;
-  EXPECT_TRUE(GetFileIDByPath(app_id, base::FilePath(path), &file_id));
+  std::string file_id = GetFileIDByPath(app_id, path);
   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
             fake_drive_service_helper()->DeleteResource(file_id));
 
   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(1u, CountLocalFile(app_id));
+
+  EXPECT_EQ(2u, CountMetadata());
+  EXPECT_EQ(2u, CountTracker());
 }
 
 TEST_F(DriveBackendSyncTest, RemoteRenameTest) {
@@ -492,14 +650,20 @@ TEST_F(DriveBackendSyncTest, RemoteRenameTest) {
   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   VerifyConsistency();
 
-  std::string file_id;
-  EXPECT_TRUE(GetFileIDByPath(app_id, base::FilePath(path), &file_id));
+  std::string file_id = GetFileIDByPath(app_id, path);
   EXPECT_EQ(google_apis::HTTP_SUCCESS,
             fake_drive_service_helper()->RenameResource(
                 file_id, "renamed_file"));
 
   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(2u, CountLocalFile(app_id));
+  VerifyLocalFile(app_id, FPL("renamed_file"), "abcde");
+
+  EXPECT_EQ(3u, CountMetadata());
+  EXPECT_EQ(3u, CountTracker());
 }
 
 TEST_F(DriveBackendSyncTest, RemoteRenameAndRevertTest) {
@@ -512,8 +676,7 @@ TEST_F(DriveBackendSyncTest, RemoteRenameAndRevertTest) {
   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   VerifyConsistency();
 
-  std::string file_id;
-  EXPECT_TRUE(GetFileIDByPath(app_id, base::FilePath(path), &file_id));
+  std::string file_id = GetFileIDByPath(app_id, path);
   EXPECT_EQ(google_apis::HTTP_SUCCESS,
             fake_drive_service_helper()->RenameResource(
                 file_id, "renamed_file"));
@@ -524,8 +687,903 @@ TEST_F(DriveBackendSyncTest, RemoteRenameAndRevertTest) {
             fake_drive_service_helper()->RenameResource(
                 file_id, base::FilePath(path).AsUTF8Unsafe()));
 
+  FetchRemoteChanges();
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(2u, CountLocalFile(app_id));
+  VerifyLocalFile(app_id, FPL("file"), "abcde");
+
+  EXPECT_EQ(3u, CountMetadata());
+  EXPECT_EQ(3u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ReorganizeToOtherFolder) {
+  std::string app_id = "example";
+  const base::FilePath::StringType path(FPL("file"));
+
+  RegisterApp(app_id);
+  AddLocalFolder(app_id, FPL("folder_src"));
+  AddLocalFolder(app_id, FPL("folder_dest"));
+  AddOrUpdateLocalFile(app_id, FPL("folder_src/file"), "abcde");
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  std::string file_id = GetFileIDByPath(app_id, FPL("folder_src/file"));
+  std::string src_folder_id = GetFileIDByPath(app_id, FPL("folder_src"));
+  std::string dest_folder_id = GetFileIDByPath(app_id, FPL("folder_dest"));
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->RemoveResourceFromDirectory(
+                src_folder_id, file_id));
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddResourceToDirectory(
+                dest_folder_id, file_id));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(4u, CountLocalFile(app_id));
+  VerifyLocalFolder(app_id, FPL("folder_dest"));
+  VerifyLocalFile(app_id, FPL("folder_dest/file"), "abcde");
+
+  EXPECT_EQ(5u, CountMetadata());
+  EXPECT_EQ(5u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ReorganizeToOtherApp) {
+  std::string src_app_id = "src_app";
+  std::string dest_app_id = "dest_app";
+
+  RegisterApp(src_app_id);
+  RegisterApp(dest_app_id);
+
+  AddLocalFolder(src_app_id, FPL("folder_src"));
+  AddLocalFolder(dest_app_id, FPL("folder_dest"));
+  AddOrUpdateLocalFile(src_app_id, FPL("folder_src/file"), "abcde");
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  std::string file_id = GetFileIDByPath(src_app_id, FPL("folder_src/file"));
+  std::string src_folder_id = GetFileIDByPath(src_app_id, FPL("folder_src"));
+  std::string dest_folder_id = GetFileIDByPath(dest_app_id, FPL("folder_dest"));
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->RemoveResourceFromDirectory(
+                src_folder_id, file_id));
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddResourceToDirectory(
+                dest_folder_id, file_id));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(2u, CountApp());
+  EXPECT_EQ(2u, CountLocalFile(src_app_id));
+  EXPECT_EQ(3u, CountLocalFile(dest_app_id));
+  VerifyLocalFile(dest_app_id, FPL("folder_dest/file"), "abcde");
+
+  EXPECT_EQ(6u, CountMetadata());
+  EXPECT_EQ(6u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ReorganizeToUnmanagedArea) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+
+  AddLocalFolder(app_id, FPL("folder_src"));
+  AddOrUpdateLocalFile(app_id, FPL("folder_src/file_orphaned"), "abcde");
+  AddOrUpdateLocalFile(app_id, FPL("folder_src/file_under_sync_root"), "123");
+  AddOrUpdateLocalFile(app_id, FPL("folder_src/file_under_drive_root"), "hoge");
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  std::string file_orphaned_id =
+      GetFileIDByPath(app_id, FPL("folder_src/file_orphaned"));
+  std::string file_under_sync_root_id =
+      GetFileIDByPath(app_id, FPL("folder_src/file_under_sync_root"));
+  std::string file_under_drive_root_id =
+      GetFileIDByPath(app_id, FPL("folder_src/file_under_drive_root"));
+
+  std::string folder_id = GetFileIDByPath(app_id, FPL("folder_src"));
+  std::string sync_root_folder_id;
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->GetSyncRootFolderID(
+                &sync_root_folder_id));
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->RemoveResourceFromDirectory(
+                folder_id, file_orphaned_id));
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->RemoveResourceFromDirectory(
+                folder_id, file_under_sync_root_id));
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->RemoveResourceFromDirectory(
+                folder_id, file_under_drive_root_id));
+
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddResourceToDirectory(
+                sync_root_folder_id, file_under_sync_root_id));
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddResourceToDirectory(
+                fake_drive_service()->GetRootResourceId(),
+                file_under_drive_root_id));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(2u, CountLocalFile(app_id));
+
+  EXPECT_EQ(4u, CountMetadata());
+  EXPECT_EQ(4u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ReorganizeToMultipleParents) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+
+  AddLocalFolder(app_id, FPL("parent1"));
+  AddLocalFolder(app_id, FPL("parent2"));
+  AddOrUpdateLocalFile(app_id, FPL("parent1/file"), "abcde");
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  std::string file_id = GetFileIDByPath(app_id, FPL("parent1/file"));
+  std::string parent2_folder_id = GetFileIDByPath(app_id, FPL("parent2"));
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddResourceToDirectory(
+                parent2_folder_id, file_id));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(4u, CountLocalFile(app_id));
+  VerifyLocalFolder(app_id, FPL("parent1"));
+  VerifyLocalFolder(app_id, FPL("parent2"));
+  VerifyLocalFile(app_id, FPL("parent1/file"), "abcde");
+
+  EXPECT_EQ(5u, CountMetadata());
+  EXPECT_EQ(5u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ReorganizeAndRevert) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+
+  AddLocalFolder(app_id, FPL("folder"));
+  AddLocalFolder(app_id, FPL("folder_temp"));
+  AddOrUpdateLocalFile(app_id, FPL("folder/file"), "abcde");
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  std::string file_id = GetFileIDByPath(app_id, FPL("folder/file"));
+  std::string folder_id = GetFileIDByPath(app_id, FPL("folder"));
+  std::string folder_temp_id = GetFileIDByPath(app_id, FPL("folder_temp"));
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->RemoveResourceFromDirectory(
+                folder_id, file_id));
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddResourceToDirectory(
+                folder_temp_id, file_id));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->RemoveResourceFromDirectory(
+                folder_temp_id, file_id));
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddResourceToDirectory(
+                folder_id, file_id));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(4u, CountLocalFile(app_id));
+  VerifyLocalFolder(app_id, FPL("folder"));
+  VerifyLocalFile(app_id, FPL("folder/file"), "abcde");
+
+  EXPECT_EQ(5u, CountMetadata());
+  EXPECT_EQ(5u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_AddFolder) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+  std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
+
+  AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  std::string remote_folder_id;
+  EXPECT_EQ(google_apis::HTTP_CREATED,
+            fake_drive_service_helper()->AddFolder(
+                app_root_folder_id,
+                "conflict_to_pending_remote", &remote_folder_id));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_CREATED,
+            fake_drive_service_helper()->AddFolder(
+                app_root_folder_id,
+                "conflict_to_existing_remote", &remote_folder_id));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(3u, CountLocalFile(app_id));
+  VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(4u, CountMetadata());
+  EXPECT_EQ(4u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_DeleteFolder) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+
+  AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  // Test body starts from here.
+  RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
+  AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
+  AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(2u, CountLocalFile(app_id));
+  VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+
+  EXPECT_EQ(3u, CountMetadata());
+  EXPECT_EQ(3u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_AddFile) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+  std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
+
+  AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  std::string file_id;
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddFile(
+                app_root_folder_id, "conflict_to_pending_remote", "foo",
+                &file_id));
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->UpdateModificationTime(
+                file_id,
+                base::Time::Now() + base::TimeDelta::FromDays(1)));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddFile(
+                app_root_folder_id, "conflict_to_existing_remote", "foo",
+                &file_id));
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->UpdateModificationTime(
+                file_id,
+                base::Time::Now() + base::TimeDelta::FromDays(1)));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(3u, CountLocalFile(app_id));
+  VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(4u, CountMetadata());
+  EXPECT_EQ(4u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_DeleteFile) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+  std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
+
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  // Test body starts from here.
+  RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
+  AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+
+  RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
+  AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(3u, CountLocalFile(app_id));
+  VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(4u, CountMetadata());
+  EXPECT_EQ(4u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_AddFolder) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+  std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
+  AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
+  RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
+
+  std::string file_id;
+  EXPECT_EQ(google_apis::HTTP_CREATED,
+            fake_drive_service_helper()->AddFolder(
+                app_root_folder_id,
+                "conflict_to_pending_remote", &file_id));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_CREATED,
+            fake_drive_service_helper()->AddFolder(
+                app_root_folder_id,
+                "conflict_to_existing_remote", NULL));
+
   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(3u, CountLocalFile(app_id));
+  VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(4u, CountMetadata());
+  EXPECT_EQ(4u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_DeleteFolder) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+  std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
+
+  AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  // Test body starts from here.
+  RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
+  RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(1u, CountLocalFile(app_id));
+
+  EXPECT_EQ(2u, CountMetadata());
+  EXPECT_EQ(2u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_AddFile) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+  std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
+
+  AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+  RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
+  RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddFile(
+                app_root_folder_id, "conflict_to_pending_remote", "foo", NULL));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddFile(
+                app_root_folder_id, "conflict_to_existing_remote", "bar",
+                NULL));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(3u, CountLocalFile(app_id));
+  VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
+  VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
+
+  EXPECT_EQ(4u, CountMetadata());
+  EXPECT_EQ(4u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_DeleteFile) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+  std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  // Test body starts from here.
+  RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
+  RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(1u, CountLocalFile(app_id));
+
+  EXPECT_EQ(2u, CountMetadata());
+  EXPECT_EQ(2u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_AddFolder) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+  std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
+
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
+
+  std::string file_id;
+  EXPECT_EQ(google_apis::HTTP_CREATED,
+            fake_drive_service_helper()->AddFolder(
+                app_root_folder_id, "conflict_to_pending_remote",
+                &file_id));
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->UpdateModificationTime(
+                file_id,
+                base::Time::Now() - base::TimeDelta::FromDays(1)));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_CREATED,
+            fake_drive_service_helper()->AddFolder(
+                app_root_folder_id, "conflict_to_existing_remote",
+                &file_id));
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->UpdateModificationTime(
+                file_id,
+                base::Time::Now() - base::TimeDelta::FromDays(1)));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(3u, CountLocalFile(app_id));
+  VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(4u, CountMetadata());
+  EXPECT_EQ(4u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_DeleteFolder) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+  std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
+
+  AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  // Test body starts from here.
+  RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
+  RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(3u, CountLocalFile(app_id));
+  VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
+  VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
+
+  EXPECT_EQ(4u, CountMetadata());
+  EXPECT_EQ(4u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_AddFile) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+
+  std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
+
+  std::string file_id;
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddFile(
+                app_root_folder_id, "conflict_to_pending_remote", "foo",
+                &file_id));
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->UpdateModificationTime(
+                file_id,
+                base::Time::Now() + base::TimeDelta::FromDays(1)));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddFile(
+                app_root_folder_id, "conflict_to_existing_remote", "bar",
+                &file_id));
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->UpdateModificationTime(
+                file_id,
+                base::Time::Now() + base::TimeDelta::FromDays(1)));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(3u, CountLocalFile(app_id));
+  VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
+  VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
+
+  EXPECT_EQ(4u, CountMetadata());
+  EXPECT_EQ(4u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_DeleteFile) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  // Test body starts from here.
+  RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
+  RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(3u, CountLocalFile(app_id));
+  VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
+  VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
+
+  EXPECT_EQ(4u, CountMetadata());
+  EXPECT_EQ(4u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_UpdateFile_DeleteFile) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  // Test body starts from here.
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(3u, CountLocalFile(app_id));
+  VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
+  VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
+
+  EXPECT_EQ(4u, CountMetadata());
+  EXPECT_EQ(4u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_AddFolder) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+
+  std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  // Test body starts from here.
+  RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
+  RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(google_apis::HTTP_CREATED,
+            fake_drive_service_helper()->AddFolder(
+                app_root_folder_id, "conflict_to_pending_remote", NULL));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_CREATED,
+            fake_drive_service_helper()->AddFolder(
+                app_root_folder_id, "conflict_to_existing_remote", NULL));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(3u, CountLocalFile(app_id));
+  VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(4u, CountMetadata());
+  EXPECT_EQ(4u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_DeleteFolder) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+
+  AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
+  AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  // Test body starts from here.
+  RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
+  RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
+
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
+
+  RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
+  RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(1u, CountLocalFile(app_id));
+
+  EXPECT_EQ(2u, CountMetadata());
+  EXPECT_EQ(2u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_AddFile) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+
+  std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
+  RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
+  RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddFile(
+                app_root_folder_id, "conflict_to_pending_remote", "hoge",
+                NULL));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->AddFile(
+                app_root_folder_id, "conflict_to_existing_remote", "fuga",
+                NULL));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(3u, CountLocalFile(app_id));
+  VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
+  VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
+
+  EXPECT_EQ(4u, CountMetadata());
+  EXPECT_EQ(4u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_UpdateFile) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+
+  std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  // Test body starts from here.
+  RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
+  RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->UpdateFile(
+                GetFileIDByPath(app_id, FPL("conflict_to_pending_remote")),
+                "hoge"));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_SUCCESS,
+            fake_drive_service_helper()->UpdateFile(
+                GetFileIDByPath(app_id, FPL("conflict_to_existing_remote")),
+                "fuga"));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(3u, CountLocalFile(app_id));
+  VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
+  VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
+
+  EXPECT_EQ(4u, CountMetadata());
+  EXPECT_EQ(4u, CountTracker());
+}
+
+TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_DeleteFile) {
+  std::string app_id = "example";
+
+  RegisterApp(app_id);
+
+  std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
+  AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  // Test body starts from here.
+  RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
+  RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
+
+  FetchRemoteChanges();
+
+  EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
+            fake_drive_service_helper()->DeleteResource(
+                GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
+
+  EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
+  VerifyConsistency();
+
+  EXPECT_EQ(1u, CountApp());
+  EXPECT_EQ(1u, CountLocalFile(app_id));
+
+  EXPECT_EQ(2u, CountMetadata());
+  EXPECT_EQ(2u, CountTracker());
 }
 
 }  // namespace drive_backend