Home | History | Annotate | Download | only in drive_backend
      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.
      4 
      5 #include <algorithm>
      6 #include <stack>
      7 
      8 #include "base/file_util.h"
      9 #include "base/message_loop/message_loop.h"
     10 #include "base/run_loop.h"
     11 #include "chrome/browser/drive/drive_uploader.h"
     12 #include "chrome/browser/drive/fake_drive_service.h"
     13 #include "chrome/browser/drive/test_util.h"
     14 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.h"
     15 #include "chrome/browser/sync_file_system/drive_backend/fake_drive_service_helper.h"
     16 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
     17 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
     18 #include "chrome/browser/sync_file_system/drive_backend/sync_engine.h"
     19 #include "chrome/browser/sync_file_system/drive_backend/sync_engine_context.h"
     20 #include "chrome/browser/sync_file_system/drive_backend/sync_worker.h"
     21 #include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h"
     22 #include "chrome/browser/sync_file_system/local/local_file_sync_context.h"
     23 #include "chrome/browser/sync_file_system/local/local_file_sync_service.h"
     24 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h"
     25 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h"
     26 #include "chrome/browser/sync_file_system/syncable_file_system_util.h"
     27 #include "chrome/test/base/testing_profile.h"
     28 #include "content/public/test/test_browser_thread.h"
     29 #include "content/public/test/test_browser_thread_bundle.h"
     30 #include "extensions/common/extension.h"
     31 #include "google_apis/drive/drive_api_parser.h"
     32 #include "testing/gtest/include/gtest/gtest.h"
     33 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
     34 #include "third_party/leveldatabase/src/include/leveldb/env.h"
     35 #include "webkit/browser/fileapi/file_system_context.h"
     36 
     37 #define FPL(a) FILE_PATH_LITERAL(a)
     38 
     39 namespace sync_file_system {
     40 namespace drive_backend {
     41 
     42 typedef fileapi::FileSystemOperation::FileEntryList FileEntryList;
     43 
     44 namespace {
     45 
     46 template <typename T>
     47 void SetValueAndCallClosure(const base::Closure& closure,
     48                             T* arg_out,
     49                             T arg) {
     50   *arg_out = base::internal::CallbackForward(arg);
     51   closure.Run();
     52 }
     53 
     54 void SetSyncStatusAndUrl(const base::Closure& closure,
     55                          SyncStatusCode* status_out,
     56                          fileapi::FileSystemURL* url_out,
     57                          SyncStatusCode status,
     58                          const fileapi::FileSystemURL& url) {
     59   *status_out = status;
     60   *url_out = url;
     61   closure.Run();
     62 }
     63 
     64 }  // namespace
     65 
     66 class DriveBackendSyncTest : public testing::Test,
     67                              public LocalFileSyncService::Observer,
     68                              public RemoteFileSyncService::Observer {
     69  public:
     70   DriveBackendSyncTest()
     71       : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
     72         pending_remote_changes_(0),
     73         pending_local_changes_(0) {}
     74   virtual ~DriveBackendSyncTest() {}
     75 
     76   virtual void SetUp() OVERRIDE {
     77     ASSERT_TRUE(base_dir_.CreateUniqueTempDir());
     78     in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
     79 
     80     io_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread(
     81         content::BrowserThread::IO);
     82     scoped_refptr<base::SequencedWorkerPool> worker_pool(
     83         content::BrowserThread::GetBlockingPool());
     84     worker_task_runner_ =
     85         worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
     86             worker_pool->GetSequenceToken(),
     87             base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
     88     file_task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread(
     89         content::BrowserThread::FILE);
     90     scoped_refptr<base::SequencedTaskRunner> drive_task_runner =
     91         worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
     92             worker_pool->GetSequenceToken(),
     93             base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
     94 
     95     RegisterSyncableFileSystem();
     96     local_sync_service_ = LocalFileSyncService::CreateForTesting(
     97         &profile_, in_memory_env_.get());
     98     local_sync_service_->AddChangeObserver(this);
     99 
    100     scoped_ptr<drive::FakeDriveService>
    101         drive_service(new drive::FakeDriveService);
    102     drive_service->Initialize("test (at) example.com");
    103     ASSERT_TRUE(drive::test_util::SetUpTestEntries(drive_service.get()));
    104 
    105     scoped_ptr<drive::DriveUploaderInterface> uploader(
    106         new drive::DriveUploader(drive_service.get(),
    107                                  file_task_runner_.get()));
    108 
    109     fake_drive_service_helper_.reset(new FakeDriveServiceHelper(
    110         drive_service.get(), uploader.get(),
    111         kSyncRootFolderTitle));
    112 
    113     remote_sync_service_.reset(new SyncEngine(
    114         base::MessageLoopProxy::current(),  // ui_task_runner
    115         worker_task_runner_,
    116         file_task_runner_,
    117         drive_task_runner,
    118         base_dir_.path(),
    119         NULL,  // task_logger
    120         NULL,  // notification_manager
    121         NULL,  // extension_service
    122         NULL,  // signin_manager
    123         NULL,  // token_service
    124         NULL,  // request_context
    125         in_memory_env_.get()));
    126     remote_sync_service_->AddServiceObserver(this);
    127     remote_sync_service_->InitializeForTesting(
    128         drive_service.PassAs<drive::DriveServiceInterface>(),
    129         uploader.Pass(),
    130         scoped_ptr<SyncWorkerInterface>());
    131     remote_sync_service_->SetSyncEnabled(true);
    132 
    133     local_sync_service_->SetLocalChangeProcessor(remote_sync_service_.get());
    134     remote_sync_service_->SetRemoteChangeProcessor(local_sync_service_.get());
    135   }
    136 
    137   virtual void TearDown() OVERRIDE {
    138     typedef std::map<std::string, CannedSyncableFileSystem*>::iterator iterator;
    139     for (iterator itr = file_systems_.begin();
    140          itr != file_systems_.end(); ++itr) {
    141       itr->second->TearDown();
    142       delete itr->second;
    143     }
    144     file_systems_.clear();
    145 
    146     local_sync_service_->Shutdown();
    147 
    148     fake_drive_service_helper_.reset();
    149     local_sync_service_.reset();
    150     remote_sync_service_.reset();
    151 
    152     content::BrowserThread::GetBlockingPool()->FlushForTesting();
    153     base::RunLoop().RunUntilIdle();
    154     RevokeSyncableFileSystem();
    155   }
    156 
    157   virtual void OnRemoteChangeQueueUpdated(int64 pending_changes_hint) OVERRIDE {
    158     pending_remote_changes_ = pending_changes_hint;
    159   }
    160 
    161   virtual void OnLocalChangeAvailable(int64 pending_changes_hint) OVERRIDE {
    162     pending_local_changes_ = pending_changes_hint;
    163   }
    164 
    165  protected:
    166   fileapi::FileSystemURL CreateURL(const std::string& app_id,
    167                                    const base::FilePath::StringType& path) {
    168     return CreateURL(app_id, base::FilePath(path));
    169   }
    170 
    171   fileapi::FileSystemURL CreateURL(const std::string& app_id,
    172                                    const base::FilePath& path) {
    173     GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id);
    174     return CreateSyncableFileSystemURL(origin, path);
    175   }
    176 
    177   bool GetAppRootFolderID(const std::string& app_id,
    178                           std::string* folder_id) {
    179     base::RunLoop run_loop;
    180     bool success = false;
    181     FileTracker tracker;
    182     PostTaskAndReplyWithResult(
    183         worker_task_runner_,
    184         FROM_HERE,
    185         base::Bind(&MetadataDatabase::FindAppRootTracker,
    186                    base::Unretained(metadata_database()),
    187                    app_id,
    188                    &tracker),
    189         base::Bind(&SetValueAndCallClosure<bool>,
    190                    run_loop.QuitClosure(),
    191                    &success));
    192     run_loop.Run();
    193     if (!success)
    194       return false;
    195     *folder_id = tracker.file_id();
    196     return true;
    197   }
    198 
    199   std::string GetFileIDByPath(const std::string& app_id,
    200                               const base::FilePath::StringType& path) {
    201     return GetFileIDByPath(app_id, base::FilePath(path));
    202   }
    203 
    204   std::string GetFileIDByPath(const std::string& app_id,
    205                               const base::FilePath& path) {
    206     base::RunLoop run_loop;
    207     bool success = false;
    208     FileTracker tracker;
    209     base::FilePath result_path;
    210     base::FilePath normalized_path = path.NormalizePathSeparators();
    211     PostTaskAndReplyWithResult(
    212         worker_task_runner_,
    213         FROM_HERE,
    214         base::Bind(&MetadataDatabase::FindNearestActiveAncestor,
    215                    base::Unretained(metadata_database()),
    216                    app_id,
    217                    normalized_path,
    218                    &tracker,
    219                    &result_path),
    220         base::Bind(&SetValueAndCallClosure<bool>,
    221                    run_loop.QuitClosure(),
    222                    &success));
    223     run_loop.Run();
    224     EXPECT_TRUE(success);
    225     EXPECT_EQ(normalized_path, result_path);
    226     return tracker.file_id();
    227   }
    228 
    229   SyncStatusCode RegisterApp(const std::string& app_id) {
    230     GURL origin = extensions::Extension::GetBaseURLFromExtensionId(app_id);
    231     if (!ContainsKey(file_systems_, app_id)) {
    232       CannedSyncableFileSystem* file_system = new CannedSyncableFileSystem(
    233           origin, in_memory_env_.get(),
    234           io_task_runner_.get(), file_task_runner_.get());
    235       file_system->SetUp(CannedSyncableFileSystem::QUOTA_DISABLED);
    236 
    237       SyncStatusCode status = SYNC_STATUS_UNKNOWN;
    238       base::RunLoop run_loop;
    239       local_sync_service_->MaybeInitializeFileSystemContext(
    240           origin, file_system->file_system_context(),
    241           base::Bind(&SetValueAndCallClosure<SyncStatusCode>,
    242                      run_loop.QuitClosure(), &status));
    243       run_loop.Run();
    244       EXPECT_EQ(SYNC_STATUS_OK, status);
    245 
    246       file_system->backend()->sync_context()->
    247           set_mock_notify_changes_duration_in_sec(0);
    248 
    249       EXPECT_EQ(base::File::FILE_OK, file_system->OpenFileSystem());
    250       file_systems_[app_id] = file_system;
    251     }
    252 
    253     SyncStatusCode status = SYNC_STATUS_UNKNOWN;
    254     base::RunLoop run_loop;
    255     remote_sync_service_->RegisterOrigin(
    256         origin,
    257         base::Bind(&SetValueAndCallClosure<SyncStatusCode>,
    258                    run_loop.QuitClosure(), &status));
    259     run_loop.Run();
    260     return status;
    261   }
    262 
    263   void AddLocalFolder(const std::string& app_id,
    264                       const base::FilePath::StringType& path) {
    265     ASSERT_TRUE(ContainsKey(file_systems_, app_id));
    266     EXPECT_EQ(base::File::FILE_OK,
    267               file_systems_[app_id]->CreateDirectory(
    268                   CreateURL(app_id, path)));
    269   }
    270 
    271   void AddOrUpdateLocalFile(const std::string& app_id,
    272                             const base::FilePath::StringType& path,
    273                             const std::string& content) {
    274     fileapi::FileSystemURL url(CreateURL(app_id, path));
    275     ASSERT_TRUE(ContainsKey(file_systems_, app_id));
    276     EXPECT_EQ(base::File::FILE_OK, file_systems_[app_id]->CreateFile(url));
    277     int64 bytes_written = file_systems_[app_id]->WriteString(url, content);
    278     EXPECT_EQ(static_cast<int64>(content.size()), bytes_written);
    279     base::RunLoop().RunUntilIdle();
    280   }
    281 
    282   void UpdateLocalFile(const std::string& app_id,
    283                        const base::FilePath::StringType& path,
    284                        const std::string& content) {
    285     ASSERT_TRUE(ContainsKey(file_systems_, app_id));
    286     int64 bytes_written = file_systems_[app_id]->WriteString(
    287         CreateURL(app_id, path), content);
    288     EXPECT_EQ(static_cast<int64>(content.size()), bytes_written);
    289     base::RunLoop().RunUntilIdle();
    290   }
    291 
    292   void RemoveLocal(const std::string& app_id,
    293                    const base::FilePath::StringType& path) {
    294     ASSERT_TRUE(ContainsKey(file_systems_, app_id));
    295     EXPECT_EQ(base::File::FILE_OK,
    296               file_systems_[app_id]->Remove(
    297                   CreateURL(app_id, path),
    298                   true /* recursive */));
    299     base::RunLoop().RunUntilIdle();
    300   }
    301 
    302   SyncStatusCode ProcessLocalChange() {
    303     SyncStatusCode status = SYNC_STATUS_UNKNOWN;
    304     fileapi::FileSystemURL url;
    305     base::RunLoop run_loop;
    306     local_sync_service_->ProcessLocalChange(base::Bind(
    307         &SetSyncStatusAndUrl, run_loop.QuitClosure(), &status, &url));
    308     run_loop.Run();
    309     return status;
    310   }
    311 
    312   SyncStatusCode ProcessRemoteChange() {
    313     SyncStatusCode status = SYNC_STATUS_UNKNOWN;
    314     fileapi::FileSystemURL url;
    315     base::RunLoop run_loop;
    316     remote_sync_service_->ProcessRemoteChange(base::Bind(
    317         &SetSyncStatusAndUrl, run_loop.QuitClosure(), &status, &url));
    318     run_loop.Run();
    319     return status;
    320   }
    321 
    322   int64 GetLargestChangeID() {
    323     scoped_ptr<google_apis::AboutResource> about_resource;
    324     EXPECT_EQ(google_apis::HTTP_SUCCESS,
    325               fake_drive_service_helper()->GetAboutResource(&about_resource));
    326     if (!about_resource)
    327       return 0;
    328     return about_resource->largest_change_id();
    329   }
    330 
    331   void FetchRemoteChanges() {
    332     remote_sync_service_->OnNotificationReceived();
    333     base::RunLoop().RunUntilIdle();
    334   }
    335 
    336   SyncStatusCode ProcessChangesUntilDone() {
    337     SyncStatusCode local_sync_status;
    338     SyncStatusCode remote_sync_status;
    339     while (true) {
    340       local_sync_status = ProcessLocalChange();
    341       if (local_sync_status != SYNC_STATUS_OK &&
    342           local_sync_status != SYNC_STATUS_NO_CHANGE_TO_SYNC &&
    343           local_sync_status != SYNC_STATUS_FILE_BUSY)
    344         return local_sync_status;
    345 
    346       remote_sync_status = ProcessRemoteChange();
    347       if (remote_sync_status != SYNC_STATUS_OK &&
    348           remote_sync_status != SYNC_STATUS_NO_CHANGE_TO_SYNC &&
    349           remote_sync_status != SYNC_STATUS_FILE_BUSY)
    350         return remote_sync_status;
    351 
    352       if (local_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC &&
    353           remote_sync_status == SYNC_STATUS_NO_CHANGE_TO_SYNC) {
    354         remote_sync_service_->PromoteDemotedChanges();
    355         local_sync_service_->PromoteDemotedChanges();
    356 
    357         if (pending_remote_changes_ || pending_local_changes_)
    358           continue;
    359 
    360         base::RunLoop run_loop;
    361         int64 largest_fetched_change_id = -1;
    362         PostTaskAndReplyWithResult(
    363             worker_task_runner_,
    364             FROM_HERE,
    365             base::Bind(&MetadataDatabase::GetLargestFetchedChangeID,
    366                        base::Unretained(metadata_database())),
    367             base::Bind(&SetValueAndCallClosure<int64>,
    368                        run_loop.QuitClosure(),
    369                        &largest_fetched_change_id));
    370         run_loop.Run();
    371         if (largest_fetched_change_id != GetLargestChangeID()) {
    372           FetchRemoteChanges();
    373           continue;
    374         }
    375         break;
    376       }
    377     }
    378     return SYNC_STATUS_OK;
    379   }
    380 
    381   // Verifies local and remote files/folders are consistent.
    382   // This function checks:
    383   //  - Each registered origin has corresponding remote folder.
    384   //  - Each local file/folder has corresponding remote one.
    385   //  - Each remote file/folder has corresponding local one.
    386   // TODO(tzik): Handle conflict case. i.e. allow remote file has different
    387   // file content if the corresponding local file conflicts to it.
    388   void VerifyConsistency() {
    389     std::string sync_root_folder_id;
    390     google_apis::GDataErrorCode error =
    391         fake_drive_service_helper_->GetSyncRootFolderID(&sync_root_folder_id);
    392     if (sync_root_folder_id.empty()) {
    393       EXPECT_EQ(google_apis::HTTP_NOT_FOUND, error);
    394       EXPECT_TRUE(file_systems_.empty());
    395       return;
    396     }
    397     EXPECT_EQ(google_apis::HTTP_SUCCESS, error);
    398 
    399     ScopedVector<google_apis::ResourceEntry> remote_entries;
    400     EXPECT_EQ(google_apis::HTTP_SUCCESS,
    401               fake_drive_service_helper_->ListFilesInFolder(
    402                   sync_root_folder_id, &remote_entries));
    403     std::map<std::string, const google_apis::ResourceEntry*> app_root_by_title;
    404     for (ScopedVector<google_apis::ResourceEntry>::iterator itr =
    405              remote_entries.begin();
    406          itr != remote_entries.end();
    407          ++itr) {
    408       const google_apis::ResourceEntry& remote_entry = **itr;
    409       EXPECT_FALSE(ContainsKey(app_root_by_title, remote_entry.title()));
    410       app_root_by_title[remote_entry.title()] = *itr;
    411     }
    412 
    413     for (std::map<std::string, CannedSyncableFileSystem*>::const_iterator itr =
    414              file_systems_.begin();
    415          itr != file_systems_.end(); ++itr) {
    416       const std::string& app_id = itr->first;
    417       SCOPED_TRACE(testing::Message() << "Verifying app: " << app_id);
    418       CannedSyncableFileSystem* file_system = itr->second;
    419       ASSERT_TRUE(ContainsKey(app_root_by_title, app_id));
    420       VerifyConsistencyForFolder(
    421           app_id, base::FilePath(),
    422           app_root_by_title[app_id]->resource_id(),
    423           file_system);
    424     }
    425   }
    426 
    427   void VerifyConsistencyForFolder(const std::string& app_id,
    428                                   const base::FilePath& path,
    429                                   const std::string& folder_id,
    430                                   CannedSyncableFileSystem* file_system) {
    431     SCOPED_TRACE(testing::Message() << "Verifying folder: " << path.value());
    432 
    433     ScopedVector<google_apis::ResourceEntry> remote_entries;
    434     EXPECT_EQ(google_apis::HTTP_SUCCESS,
    435               fake_drive_service_helper_->ListFilesInFolder(
    436                   folder_id, &remote_entries));
    437     std::map<std::string, const google_apis::ResourceEntry*>
    438         remote_entry_by_title;
    439     for (size_t i = 0; i < remote_entries.size(); ++i) {
    440       google_apis::ResourceEntry* remote_entry = remote_entries[i];
    441       EXPECT_FALSE(ContainsKey(remote_entry_by_title, remote_entry->title()))
    442           << "title: " << remote_entry->title();
    443       remote_entry_by_title[remote_entry->title()] = remote_entry;
    444     }
    445 
    446     fileapi::FileSystemURL url(CreateURL(app_id, path));
    447     FileEntryList local_entries;
    448     EXPECT_EQ(base::File::FILE_OK,
    449               file_system->ReadDirectory(url, &local_entries));
    450     for (FileEntryList::iterator itr = local_entries.begin();
    451          itr != local_entries.end();
    452          ++itr) {
    453       const fileapi::DirectoryEntry& local_entry = *itr;
    454       fileapi::FileSystemURL entry_url(
    455           CreateURL(app_id, path.Append(local_entry.name)));
    456       std::string title =
    457           fileapi::VirtualPath::BaseName(entry_url.path()).AsUTF8Unsafe();
    458       SCOPED_TRACE(testing::Message() << "Verifying entry: " << title);
    459 
    460       ASSERT_TRUE(ContainsKey(remote_entry_by_title, title));
    461       const google_apis::ResourceEntry& remote_entry =
    462           *remote_entry_by_title[title];
    463       if (local_entry.is_directory) {
    464         ASSERT_TRUE(remote_entry.is_folder());
    465         VerifyConsistencyForFolder(app_id, entry_url.path(),
    466                                    remote_entry.resource_id(),
    467                                    file_system);
    468       } else {
    469         ASSERT_TRUE(remote_entry.is_file());
    470         VerifyConsistencyForFile(app_id, entry_url.path(),
    471                                  remote_entry.resource_id(),
    472                                  file_system);
    473       }
    474       remote_entry_by_title.erase(title);
    475     }
    476 
    477     EXPECT_TRUE(remote_entry_by_title.empty());
    478   }
    479 
    480   void VerifyConsistencyForFile(const std::string& app_id,
    481                                 const base::FilePath& path,
    482                                 const std::string& file_id,
    483                                 CannedSyncableFileSystem* file_system) {
    484     fileapi::FileSystemURL url(CreateURL(app_id, path));
    485     std::string file_content;
    486     EXPECT_EQ(google_apis::HTTP_SUCCESS,
    487               fake_drive_service_helper_->ReadFile(file_id, &file_content));
    488     EXPECT_EQ(base::File::FILE_OK,
    489               file_system->VerifyFile(url, file_content));
    490   }
    491 
    492   size_t CountApp() {
    493     return file_systems_.size();
    494   }
    495 
    496   size_t CountLocalFile(const std::string& app_id) {
    497     if (!ContainsKey(file_systems_, app_id))
    498       return 0;
    499 
    500     CannedSyncableFileSystem* file_system = file_systems_[app_id];
    501     std::stack<base::FilePath> folders;
    502     folders.push(base::FilePath());  // root folder
    503 
    504     size_t result = 1;
    505     while (!folders.empty()) {
    506       fileapi::FileSystemURL url(CreateURL(app_id, folders.top()));
    507       folders.pop();
    508 
    509       FileEntryList entries;
    510       EXPECT_EQ(base::File::FILE_OK,
    511                 file_system->ReadDirectory(url, &entries));
    512       for (FileEntryList::iterator itr = entries.begin();
    513            itr != entries.end(); ++itr) {
    514         ++result;
    515         if (itr->is_directory)
    516           folders.push(url.path().Append(itr->name));
    517       }
    518     }
    519 
    520     return result;
    521   }
    522 
    523   void VerifyLocalFile(const std::string& app_id,
    524                        const base::FilePath::StringType& path,
    525                        const std::string& content) {
    526     SCOPED_TRACE(testing::Message() << "Verifying local file: "
    527                                     << "app_id = " << app_id
    528                                     << ", path = " << path);
    529     ASSERT_TRUE(ContainsKey(file_systems_, app_id));
    530     EXPECT_EQ(base::File::FILE_OK,
    531               file_systems_[app_id]->VerifyFile(
    532                   CreateURL(app_id, path), content));
    533   }
    534 
    535   void VerifyLocalFolder(const std::string& app_id,
    536                          const base::FilePath::StringType& path) {
    537     SCOPED_TRACE(testing::Message() << "Verifying local file: "
    538                                     << "app_id = " << app_id
    539                                     << ", path = " << path);
    540     ASSERT_TRUE(ContainsKey(file_systems_, app_id));
    541     EXPECT_EQ(base::File::FILE_OK,
    542               file_systems_[app_id]->DirectoryExists(CreateURL(app_id, path)));
    543   }
    544 
    545   size_t CountMetadata() {
    546     size_t count = 0;
    547     base::RunLoop run_loop;
    548     PostTaskAndReplyWithResult(
    549         worker_task_runner_,
    550         FROM_HERE,
    551         base::Bind(&MetadataDatabase::CountFileMetadata,
    552                    base::Unretained(metadata_database())),
    553         base::Bind(&SetValueAndCallClosure<size_t>,
    554                    run_loop.QuitClosure(),
    555                    &count));
    556     run_loop.Run();
    557     return count;
    558   }
    559 
    560   size_t CountTracker() {
    561     size_t count = 0;
    562     base::RunLoop run_loop;
    563     PostTaskAndReplyWithResult(
    564         worker_task_runner_,
    565         FROM_HERE,
    566         base::Bind(&MetadataDatabase::CountFileTracker,
    567                    base::Unretained(metadata_database())),
    568         base::Bind(&SetValueAndCallClosure<size_t>,
    569                    run_loop.QuitClosure(), &count));
    570     run_loop.Run();
    571     return count;
    572   }
    573 
    574   drive::FakeDriveService* fake_drive_service() {
    575     return static_cast<drive::FakeDriveService*>(
    576         remote_sync_service_->drive_service_.get());
    577   }
    578 
    579   FakeDriveServiceHelper* fake_drive_service_helper() {
    580     return fake_drive_service_helper_.get();
    581   }
    582 
    583  private:
    584   // MetadataDatabase is normally used on the worker thread.
    585   // Use this only when there is no task running on the worker.
    586   MetadataDatabase* metadata_database() {
    587     SyncWorker* worker = static_cast<SyncWorker*>(
    588         remote_sync_service_->sync_worker_.get());
    589     return worker->context_->metadata_database_.get();
    590   }
    591 
    592   content::TestBrowserThreadBundle thread_bundle_;
    593 
    594   base::ScopedTempDir base_dir_;
    595   scoped_ptr<leveldb::Env> in_memory_env_;
    596   TestingProfile profile_;
    597 
    598   scoped_ptr<SyncEngine> remote_sync_service_;
    599   scoped_ptr<LocalFileSyncService> local_sync_service_;
    600 
    601   int64 pending_remote_changes_;
    602   int64 pending_local_changes_;
    603 
    604   scoped_ptr<FakeDriveServiceHelper> fake_drive_service_helper_;
    605   std::map<std::string, CannedSyncableFileSystem*> file_systems_;
    606 
    607 
    608   scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
    609   scoped_refptr<base::SequencedTaskRunner> worker_task_runner_;
    610   scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
    611 
    612   DISALLOW_COPY_AND_ASSIGN(DriveBackendSyncTest);
    613 };
    614 
    615 TEST_F(DriveBackendSyncTest, LocalToRemoteBasicTest) {
    616   std::string app_id = "example";
    617 
    618   RegisterApp(app_id);
    619   AddOrUpdateLocalFile(app_id, FPL("file"), "abcde");
    620 
    621   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    622   VerifyConsistency();
    623 
    624   EXPECT_EQ(1u, CountApp());
    625   EXPECT_EQ(2u, CountLocalFile(app_id));
    626   VerifyLocalFile(app_id, FPL("file"), "abcde");
    627 
    628   EXPECT_EQ(3u, CountMetadata());
    629   EXPECT_EQ(3u, CountTracker());
    630 }
    631 
    632 TEST_F(DriveBackendSyncTest, RemoteToLocalBasicTest) {
    633   std::string app_id = "example";
    634   RegisterApp(app_id);
    635 
    636   std::string app_root_folder_id;
    637   EXPECT_TRUE(GetAppRootFolderID(app_id, &app_root_folder_id));
    638 
    639   std::string file_id;
    640   EXPECT_EQ(google_apis::HTTP_SUCCESS,
    641             fake_drive_service_helper()->AddFile(
    642                 app_root_folder_id, "file", "abcde", &file_id));
    643 
    644   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    645   VerifyConsistency();
    646 
    647   EXPECT_EQ(1u, CountApp());
    648   EXPECT_EQ(2u, CountLocalFile(app_id));
    649   VerifyLocalFile(app_id, FPL("file"), "abcde");
    650 
    651   EXPECT_EQ(3u, CountMetadata());
    652   EXPECT_EQ(3u, CountTracker());
    653 }
    654 
    655 TEST_F(DriveBackendSyncTest, LocalFileUpdateTest) {
    656   std::string app_id = "example";
    657   const base::FilePath::StringType kPath(FPL("file"));
    658 
    659   RegisterApp(app_id);
    660   AddOrUpdateLocalFile(app_id, kPath, "abcde");
    661 
    662   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    663   VerifyConsistency();
    664 
    665   UpdateLocalFile(app_id, kPath, "1234567890");
    666 
    667   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    668   VerifyConsistency();
    669 
    670   EXPECT_EQ(1u, CountApp());
    671   EXPECT_EQ(2u, CountLocalFile(app_id));
    672   VerifyLocalFile(app_id, FPL("file"), "1234567890");
    673 
    674   EXPECT_EQ(3u, CountMetadata());
    675   EXPECT_EQ(3u, CountTracker());
    676 }
    677 
    678 TEST_F(DriveBackendSyncTest, RemoteFileUpdateTest) {
    679   std::string app_id = "example";
    680 
    681   RegisterApp(app_id);
    682   std::string remote_file_id;
    683   std::string app_root_folder_id;
    684   EXPECT_TRUE(GetAppRootFolderID(app_id, &app_root_folder_id));
    685   EXPECT_EQ(google_apis::HTTP_SUCCESS,
    686             fake_drive_service_helper()->AddFile(
    687                 app_root_folder_id, "file", "abcde", &remote_file_id));
    688 
    689   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    690   VerifyConsistency();
    691 
    692   EXPECT_EQ(google_apis::HTTP_SUCCESS,
    693             fake_drive_service_helper()->UpdateFile(
    694                 remote_file_id, "1234567890"));
    695 
    696   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    697   VerifyConsistency();
    698 
    699   EXPECT_EQ(1u, CountApp());
    700   EXPECT_EQ(2u, CountLocalFile(app_id));
    701   VerifyLocalFile(app_id, FPL("file"), "1234567890");
    702 
    703   EXPECT_EQ(3u, CountMetadata());
    704   EXPECT_EQ(3u, CountTracker());
    705 }
    706 
    707 TEST_F(DriveBackendSyncTest, LocalFileDeletionTest) {
    708   std::string app_id = "example";
    709   const base::FilePath::StringType path(FPL("file"));
    710 
    711   RegisterApp(app_id);
    712   AddOrUpdateLocalFile(app_id, path, "abcde");
    713 
    714   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    715   VerifyConsistency();
    716 
    717   RemoveLocal(app_id, path);
    718 
    719   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    720   VerifyConsistency();
    721 
    722   EXPECT_EQ(1u, CountApp());
    723   EXPECT_EQ(1u, CountLocalFile(app_id));
    724 
    725   EXPECT_EQ(2u, CountMetadata());
    726   EXPECT_EQ(2u, CountTracker());
    727 }
    728 
    729 TEST_F(DriveBackendSyncTest, RemoteFileDeletionTest) {
    730   std::string app_id = "example";
    731   const base::FilePath::StringType path(FPL("file"));
    732 
    733   RegisterApp(app_id);
    734   AddOrUpdateLocalFile(app_id, path, "abcde");
    735 
    736   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    737   VerifyConsistency();
    738 
    739   std::string file_id = GetFileIDByPath(app_id, path);
    740   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
    741             fake_drive_service_helper()->DeleteResource(file_id));
    742 
    743   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    744   VerifyConsistency();
    745 
    746   EXPECT_EQ(1u, CountApp());
    747   EXPECT_EQ(1u, CountLocalFile(app_id));
    748 
    749   EXPECT_EQ(2u, CountMetadata());
    750   EXPECT_EQ(2u, CountTracker());
    751 }
    752 
    753 TEST_F(DriveBackendSyncTest, RemoteRenameTest) {
    754   std::string app_id = "example";
    755   const base::FilePath::StringType path(FPL("file"));
    756 
    757   RegisterApp(app_id);
    758   AddOrUpdateLocalFile(app_id, path, "abcde");
    759 
    760   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    761   VerifyConsistency();
    762 
    763   std::string file_id = GetFileIDByPath(app_id, path);
    764   EXPECT_EQ(google_apis::HTTP_SUCCESS,
    765             fake_drive_service_helper()->RenameResource(
    766                 file_id, "renamed_file"));
    767 
    768   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    769   VerifyConsistency();
    770 
    771   EXPECT_EQ(1u, CountApp());
    772   EXPECT_EQ(2u, CountLocalFile(app_id));
    773   VerifyLocalFile(app_id, FPL("renamed_file"), "abcde");
    774 
    775   EXPECT_EQ(3u, CountMetadata());
    776   EXPECT_EQ(3u, CountTracker());
    777 }
    778 
    779 TEST_F(DriveBackendSyncTest, RemoteRenameAndRevertTest) {
    780   std::string app_id = "example";
    781   const base::FilePath::StringType path(FPL("file"));
    782 
    783   RegisterApp(app_id);
    784   AddOrUpdateLocalFile(app_id, path, "abcde");
    785 
    786   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    787   VerifyConsistency();
    788 
    789   std::string file_id = GetFileIDByPath(app_id, path);
    790   EXPECT_EQ(google_apis::HTTP_SUCCESS,
    791             fake_drive_service_helper()->RenameResource(
    792                 file_id, "renamed_file"));
    793 
    794   FetchRemoteChanges();
    795 
    796   EXPECT_EQ(google_apis::HTTP_SUCCESS,
    797             fake_drive_service_helper()->RenameResource(
    798                 file_id, base::FilePath(path).AsUTF8Unsafe()));
    799 
    800   FetchRemoteChanges();
    801 
    802   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    803   VerifyConsistency();
    804 
    805   EXPECT_EQ(1u, CountApp());
    806   EXPECT_EQ(2u, CountLocalFile(app_id));
    807   VerifyLocalFile(app_id, FPL("file"), "abcde");
    808 
    809   EXPECT_EQ(3u, CountMetadata());
    810   EXPECT_EQ(3u, CountTracker());
    811 }
    812 
    813 TEST_F(DriveBackendSyncTest, ReorganizeToOtherFolder) {
    814   std::string app_id = "example";
    815   const base::FilePath::StringType path(FPL("file"));
    816 
    817   RegisterApp(app_id);
    818   AddLocalFolder(app_id, FPL("folder_src"));
    819   AddLocalFolder(app_id, FPL("folder_dest"));
    820   AddOrUpdateLocalFile(app_id, FPL("folder_src/file"), "abcde");
    821 
    822   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    823   VerifyConsistency();
    824 
    825   std::string file_id = GetFileIDByPath(app_id, FPL("folder_src/file"));
    826   std::string src_folder_id = GetFileIDByPath(app_id, FPL("folder_src"));
    827   std::string dest_folder_id = GetFileIDByPath(app_id, FPL("folder_dest"));
    828   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
    829             fake_drive_service_helper()->RemoveResourceFromDirectory(
    830                 src_folder_id, file_id));
    831   EXPECT_EQ(google_apis::HTTP_SUCCESS,
    832             fake_drive_service_helper()->AddResourceToDirectory(
    833                 dest_folder_id, file_id));
    834 
    835   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    836   VerifyConsistency();
    837 
    838   EXPECT_EQ(1u, CountApp());
    839   EXPECT_EQ(4u, CountLocalFile(app_id));
    840   VerifyLocalFolder(app_id, FPL("folder_dest"));
    841   VerifyLocalFile(app_id, FPL("folder_dest/file"), "abcde");
    842 
    843   EXPECT_EQ(5u, CountMetadata());
    844   EXPECT_EQ(5u, CountTracker());
    845 }
    846 
    847 TEST_F(DriveBackendSyncTest, ReorganizeToOtherApp) {
    848   std::string src_app_id = "src_app";
    849   std::string dest_app_id = "dest_app";
    850 
    851   RegisterApp(src_app_id);
    852   RegisterApp(dest_app_id);
    853 
    854   AddLocalFolder(src_app_id, FPL("folder_src"));
    855   AddLocalFolder(dest_app_id, FPL("folder_dest"));
    856   AddOrUpdateLocalFile(src_app_id, FPL("folder_src/file"), "abcde");
    857 
    858   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    859   VerifyConsistency();
    860 
    861   std::string file_id = GetFileIDByPath(src_app_id, FPL("folder_src/file"));
    862   std::string src_folder_id = GetFileIDByPath(src_app_id, FPL("folder_src"));
    863   std::string dest_folder_id = GetFileIDByPath(dest_app_id, FPL("folder_dest"));
    864   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
    865             fake_drive_service_helper()->RemoveResourceFromDirectory(
    866                 src_folder_id, file_id));
    867   EXPECT_EQ(google_apis::HTTP_SUCCESS,
    868             fake_drive_service_helper()->AddResourceToDirectory(
    869                 dest_folder_id, file_id));
    870 
    871   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    872   VerifyConsistency();
    873 
    874   EXPECT_EQ(2u, CountApp());
    875   EXPECT_EQ(2u, CountLocalFile(src_app_id));
    876   EXPECT_EQ(3u, CountLocalFile(dest_app_id));
    877   VerifyLocalFile(dest_app_id, FPL("folder_dest/file"), "abcde");
    878 
    879   EXPECT_EQ(6u, CountMetadata());
    880   EXPECT_EQ(6u, CountTracker());
    881 }
    882 
    883 TEST_F(DriveBackendSyncTest, ReorganizeToUnmanagedArea) {
    884   std::string app_id = "example";
    885 
    886   RegisterApp(app_id);
    887 
    888   AddLocalFolder(app_id, FPL("folder_src"));
    889   AddOrUpdateLocalFile(app_id, FPL("folder_src/file_orphaned"), "abcde");
    890   AddOrUpdateLocalFile(app_id, FPL("folder_src/file_under_sync_root"), "123");
    891   AddOrUpdateLocalFile(app_id, FPL("folder_src/file_under_drive_root"), "hoge");
    892 
    893   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    894   VerifyConsistency();
    895 
    896   std::string file_orphaned_id =
    897       GetFileIDByPath(app_id, FPL("folder_src/file_orphaned"));
    898   std::string file_under_sync_root_id =
    899       GetFileIDByPath(app_id, FPL("folder_src/file_under_sync_root"));
    900   std::string file_under_drive_root_id =
    901       GetFileIDByPath(app_id, FPL("folder_src/file_under_drive_root"));
    902 
    903   std::string folder_id = GetFileIDByPath(app_id, FPL("folder_src"));
    904   std::string sync_root_folder_id;
    905   EXPECT_EQ(google_apis::HTTP_SUCCESS,
    906             fake_drive_service_helper()->GetSyncRootFolderID(
    907                 &sync_root_folder_id));
    908 
    909   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
    910             fake_drive_service_helper()->RemoveResourceFromDirectory(
    911                 folder_id, file_orphaned_id));
    912   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
    913             fake_drive_service_helper()->RemoveResourceFromDirectory(
    914                 folder_id, file_under_sync_root_id));
    915   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
    916             fake_drive_service_helper()->RemoveResourceFromDirectory(
    917                 folder_id, file_under_drive_root_id));
    918 
    919   EXPECT_EQ(google_apis::HTTP_SUCCESS,
    920             fake_drive_service_helper()->AddResourceToDirectory(
    921                 sync_root_folder_id, file_under_sync_root_id));
    922   EXPECT_EQ(google_apis::HTTP_SUCCESS,
    923             fake_drive_service_helper()->AddResourceToDirectory(
    924                 fake_drive_service()->GetRootResourceId(),
    925                 file_under_drive_root_id));
    926 
    927   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    928   VerifyConsistency();
    929 
    930   EXPECT_EQ(1u, CountApp());
    931   EXPECT_EQ(2u, CountLocalFile(app_id));
    932 
    933   EXPECT_EQ(4u, CountMetadata());
    934   EXPECT_EQ(4u, CountTracker());
    935 }
    936 
    937 TEST_F(DriveBackendSyncTest, ReorganizeToMultipleParents) {
    938   std::string app_id = "example";
    939 
    940   RegisterApp(app_id);
    941 
    942   AddLocalFolder(app_id, FPL("parent1"));
    943   AddLocalFolder(app_id, FPL("parent2"));
    944   AddOrUpdateLocalFile(app_id, FPL("parent1/file"), "abcde");
    945 
    946   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    947   VerifyConsistency();
    948 
    949   std::string file_id = GetFileIDByPath(app_id, FPL("parent1/file"));
    950   std::string parent2_folder_id = GetFileIDByPath(app_id, FPL("parent2"));
    951   EXPECT_EQ(google_apis::HTTP_SUCCESS,
    952             fake_drive_service_helper()->AddResourceToDirectory(
    953                 parent2_folder_id, file_id));
    954 
    955   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    956   VerifyConsistency();
    957 
    958   EXPECT_EQ(1u, CountApp());
    959   EXPECT_EQ(4u, CountLocalFile(app_id));
    960   VerifyLocalFolder(app_id, FPL("parent1"));
    961   VerifyLocalFolder(app_id, FPL("parent2"));
    962   VerifyLocalFile(app_id, FPL("parent1/file"), "abcde");
    963 
    964   EXPECT_EQ(5u, CountMetadata());
    965   EXPECT_EQ(5u, CountTracker());
    966 }
    967 
    968 TEST_F(DriveBackendSyncTest, ReorganizeAndRevert) {
    969   std::string app_id = "example";
    970 
    971   RegisterApp(app_id);
    972 
    973   AddLocalFolder(app_id, FPL("folder"));
    974   AddLocalFolder(app_id, FPL("folder_temp"));
    975   AddOrUpdateLocalFile(app_id, FPL("folder/file"), "abcde");
    976 
    977   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
    978   VerifyConsistency();
    979 
    980   std::string file_id = GetFileIDByPath(app_id, FPL("folder/file"));
    981   std::string folder_id = GetFileIDByPath(app_id, FPL("folder"));
    982   std::string folder_temp_id = GetFileIDByPath(app_id, FPL("folder_temp"));
    983   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
    984             fake_drive_service_helper()->RemoveResourceFromDirectory(
    985                 folder_id, file_id));
    986   EXPECT_EQ(google_apis::HTTP_SUCCESS,
    987             fake_drive_service_helper()->AddResourceToDirectory(
    988                 folder_temp_id, file_id));
    989 
    990   FetchRemoteChanges();
    991 
    992   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
    993             fake_drive_service_helper()->RemoveResourceFromDirectory(
    994                 folder_temp_id, file_id));
    995   EXPECT_EQ(google_apis::HTTP_SUCCESS,
    996             fake_drive_service_helper()->AddResourceToDirectory(
    997                 folder_id, file_id));
    998 
    999   FetchRemoteChanges();
   1000 
   1001   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1002   VerifyConsistency();
   1003 
   1004   EXPECT_EQ(1u, CountApp());
   1005   EXPECT_EQ(4u, CountLocalFile(app_id));
   1006   VerifyLocalFolder(app_id, FPL("folder"));
   1007   VerifyLocalFile(app_id, FPL("folder/file"), "abcde");
   1008 
   1009   EXPECT_EQ(5u, CountMetadata());
   1010   EXPECT_EQ(5u, CountTracker());
   1011 }
   1012 
   1013 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_AddFolder) {
   1014   std::string app_id = "example";
   1015 
   1016   RegisterApp(app_id);
   1017   std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
   1018 
   1019   AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1020   AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1021 
   1022   std::string remote_folder_id;
   1023   EXPECT_EQ(google_apis::HTTP_CREATED,
   1024             fake_drive_service_helper()->AddFolder(
   1025                 app_root_folder_id,
   1026                 "conflict_to_pending_remote", &remote_folder_id));
   1027 
   1028   FetchRemoteChanges();
   1029 
   1030   EXPECT_EQ(google_apis::HTTP_CREATED,
   1031             fake_drive_service_helper()->AddFolder(
   1032                 app_root_folder_id,
   1033                 "conflict_to_existing_remote", &remote_folder_id));
   1034 
   1035   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1036   VerifyConsistency();
   1037 
   1038   EXPECT_EQ(1u, CountApp());
   1039   EXPECT_EQ(3u, CountLocalFile(app_id));
   1040   VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1041   VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1042 
   1043   EXPECT_EQ(4u, CountMetadata());
   1044   EXPECT_EQ(4u, CountTracker());
   1045 }
   1046 
   1047 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_DeleteFolder) {
   1048   std::string app_id = "example";
   1049 
   1050   RegisterApp(app_id);
   1051 
   1052   AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1053   AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1054 
   1055   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1056   VerifyConsistency();
   1057 
   1058   // Test body starts from here.
   1059   RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
   1060   AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1061   RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
   1062   AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1063 
   1064   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1065             fake_drive_service_helper()->DeleteResource(
   1066                 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
   1067 
   1068   FetchRemoteChanges();
   1069 
   1070   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1071             fake_drive_service_helper()->DeleteResource(
   1072                 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
   1073 
   1074   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1075   VerifyConsistency();
   1076 
   1077   EXPECT_EQ(1u, CountApp());
   1078   EXPECT_EQ(2u, CountLocalFile(app_id));
   1079   VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1080 
   1081   EXPECT_EQ(3u, CountMetadata());
   1082   EXPECT_EQ(3u, CountTracker());
   1083 }
   1084 
   1085 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_AddFile) {
   1086   std::string app_id = "example";
   1087 
   1088   RegisterApp(app_id);
   1089   std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
   1090 
   1091   AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1092   AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1093 
   1094   std::string file_id;
   1095   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1096             fake_drive_service_helper()->AddFile(
   1097                 app_root_folder_id, "conflict_to_pending_remote", "foo",
   1098                 &file_id));
   1099   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1100             fake_drive_service_helper()->UpdateModificationTime(
   1101                 file_id,
   1102                 base::Time::Now() + base::TimeDelta::FromDays(1)));
   1103 
   1104   FetchRemoteChanges();
   1105 
   1106   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1107             fake_drive_service_helper()->AddFile(
   1108                 app_root_folder_id, "conflict_to_existing_remote", "foo",
   1109                 &file_id));
   1110   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1111             fake_drive_service_helper()->UpdateModificationTime(
   1112                 file_id,
   1113                 base::Time::Now() + base::TimeDelta::FromDays(1)));
   1114 
   1115   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1116   VerifyConsistency();
   1117 
   1118   EXPECT_EQ(1u, CountApp());
   1119   EXPECT_EQ(3u, CountLocalFile(app_id));
   1120   VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1121   VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1122 
   1123   EXPECT_EQ(4u, CountMetadata());
   1124   EXPECT_EQ(4u, CountTracker());
   1125 }
   1126 
   1127 TEST_F(DriveBackendSyncTest, ConflictTest_AddFolder_DeleteFile) {
   1128   std::string app_id = "example";
   1129 
   1130   RegisterApp(app_id);
   1131   std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
   1132 
   1133   AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
   1134   AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
   1135 
   1136   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1137   VerifyConsistency();
   1138 
   1139   // Test body starts from here.
   1140   RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
   1141   AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1142 
   1143   RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
   1144   AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1145 
   1146   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1147             fake_drive_service_helper()->DeleteResource(
   1148                 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
   1149 
   1150   FetchRemoteChanges();
   1151 
   1152   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1153             fake_drive_service_helper()->DeleteResource(
   1154                 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
   1155 
   1156   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1157   VerifyConsistency();
   1158 
   1159   EXPECT_EQ(1u, CountApp());
   1160   EXPECT_EQ(3u, CountLocalFile(app_id));
   1161   VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1162   VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1163 
   1164   EXPECT_EQ(4u, CountMetadata());
   1165   EXPECT_EQ(4u, CountTracker());
   1166 }
   1167 
   1168 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_AddFolder) {
   1169   std::string app_id = "example";
   1170 
   1171   RegisterApp(app_id);
   1172   std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
   1173   AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1174   AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1175 
   1176   RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
   1177   RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
   1178 
   1179   std::string file_id;
   1180   EXPECT_EQ(google_apis::HTTP_CREATED,
   1181             fake_drive_service_helper()->AddFolder(
   1182                 app_root_folder_id,
   1183                 "conflict_to_pending_remote", &file_id));
   1184 
   1185   FetchRemoteChanges();
   1186 
   1187   EXPECT_EQ(google_apis::HTTP_CREATED,
   1188             fake_drive_service_helper()->AddFolder(
   1189                 app_root_folder_id,
   1190                 "conflict_to_existing_remote", NULL));
   1191 
   1192   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1193   VerifyConsistency();
   1194 
   1195   EXPECT_EQ(1u, CountApp());
   1196   EXPECT_EQ(3u, CountLocalFile(app_id));
   1197   VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1198   VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1199 
   1200   EXPECT_EQ(4u, CountMetadata());
   1201   EXPECT_EQ(4u, CountTracker());
   1202 }
   1203 
   1204 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_DeleteFolder) {
   1205   std::string app_id = "example";
   1206 
   1207   RegisterApp(app_id);
   1208   std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
   1209 
   1210   AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1211   AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1212 
   1213   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1214   VerifyConsistency();
   1215 
   1216   // Test body starts from here.
   1217   RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
   1218   RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
   1219 
   1220   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1221             fake_drive_service_helper()->DeleteResource(
   1222                 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
   1223 
   1224   FetchRemoteChanges();
   1225 
   1226   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1227             fake_drive_service_helper()->DeleteResource(
   1228                 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
   1229 
   1230   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1231   VerifyConsistency();
   1232 
   1233   EXPECT_EQ(1u, CountApp());
   1234   EXPECT_EQ(1u, CountLocalFile(app_id));
   1235 
   1236   EXPECT_EQ(2u, CountMetadata());
   1237   EXPECT_EQ(2u, CountTracker());
   1238 }
   1239 
   1240 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_AddFile) {
   1241   std::string app_id = "example";
   1242 
   1243   RegisterApp(app_id);
   1244   std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
   1245 
   1246   AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1247   AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1248   RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
   1249   RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
   1250 
   1251   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1252             fake_drive_service_helper()->AddFile(
   1253                 app_root_folder_id, "conflict_to_pending_remote", "foo", NULL));
   1254 
   1255   FetchRemoteChanges();
   1256 
   1257   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1258             fake_drive_service_helper()->AddFile(
   1259                 app_root_folder_id, "conflict_to_existing_remote", "bar",
   1260                 NULL));
   1261 
   1262   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1263   VerifyConsistency();
   1264 
   1265   EXPECT_EQ(1u, CountApp());
   1266   EXPECT_EQ(3u, CountLocalFile(app_id));
   1267   VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
   1268   VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
   1269 
   1270   EXPECT_EQ(4u, CountMetadata());
   1271   EXPECT_EQ(4u, CountTracker());
   1272 }
   1273 
   1274 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFolder_DeleteFile) {
   1275   std::string app_id = "example";
   1276 
   1277   RegisterApp(app_id);
   1278   std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
   1279   AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
   1280   AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
   1281 
   1282   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1283   VerifyConsistency();
   1284 
   1285   // Test body starts from here.
   1286   RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
   1287   RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
   1288 
   1289   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1290             fake_drive_service_helper()->DeleteResource(
   1291                 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
   1292 
   1293   FetchRemoteChanges();
   1294 
   1295   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1296             fake_drive_service_helper()->DeleteResource(
   1297                 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
   1298 
   1299   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1300   VerifyConsistency();
   1301 
   1302   EXPECT_EQ(1u, CountApp());
   1303   EXPECT_EQ(1u, CountLocalFile(app_id));
   1304 
   1305   EXPECT_EQ(2u, CountMetadata());
   1306   EXPECT_EQ(2u, CountTracker());
   1307 }
   1308 
   1309 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_AddFolder) {
   1310   std::string app_id = "example";
   1311 
   1312   RegisterApp(app_id);
   1313   std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
   1314 
   1315   AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
   1316   AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
   1317 
   1318   std::string file_id;
   1319   EXPECT_EQ(google_apis::HTTP_CREATED,
   1320             fake_drive_service_helper()->AddFolder(
   1321                 app_root_folder_id, "conflict_to_pending_remote",
   1322                 &file_id));
   1323   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1324             fake_drive_service_helper()->UpdateModificationTime(
   1325                 file_id,
   1326                 base::Time::Now() - base::TimeDelta::FromDays(1)));
   1327 
   1328   FetchRemoteChanges();
   1329 
   1330   EXPECT_EQ(google_apis::HTTP_CREATED,
   1331             fake_drive_service_helper()->AddFolder(
   1332                 app_root_folder_id, "conflict_to_existing_remote",
   1333                 &file_id));
   1334   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1335             fake_drive_service_helper()->UpdateModificationTime(
   1336                 file_id,
   1337                 base::Time::Now() - base::TimeDelta::FromDays(1)));
   1338 
   1339   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1340   VerifyConsistency();
   1341 
   1342   EXPECT_EQ(1u, CountApp());
   1343   EXPECT_EQ(3u, CountLocalFile(app_id));
   1344   VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1345   VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1346 
   1347   EXPECT_EQ(4u, CountMetadata());
   1348   EXPECT_EQ(4u, CountTracker());
   1349 }
   1350 
   1351 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_DeleteFolder) {
   1352   std::string app_id = "example";
   1353 
   1354   RegisterApp(app_id);
   1355   std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
   1356 
   1357   AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1358   AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1359 
   1360   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1361   VerifyConsistency();
   1362 
   1363   // Test body starts from here.
   1364   RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
   1365   RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
   1366   AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
   1367   AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
   1368 
   1369   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1370             fake_drive_service_helper()->DeleteResource(
   1371                 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
   1372 
   1373   FetchRemoteChanges();
   1374 
   1375   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1376             fake_drive_service_helper()->DeleteResource(
   1377                 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
   1378 
   1379   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1380   VerifyConsistency();
   1381 
   1382   EXPECT_EQ(1u, CountApp());
   1383   EXPECT_EQ(3u, CountLocalFile(app_id));
   1384   VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
   1385   VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
   1386 
   1387   EXPECT_EQ(4u, CountMetadata());
   1388   EXPECT_EQ(4u, CountTracker());
   1389 }
   1390 
   1391 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_AddFile) {
   1392   std::string app_id = "example";
   1393 
   1394   RegisterApp(app_id);
   1395 
   1396   std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
   1397   AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
   1398   AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
   1399 
   1400   std::string file_id;
   1401   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1402             fake_drive_service_helper()->AddFile(
   1403                 app_root_folder_id, "conflict_to_pending_remote", "foo",
   1404                 &file_id));
   1405   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1406             fake_drive_service_helper()->UpdateModificationTime(
   1407                 file_id,
   1408                 base::Time::Now() + base::TimeDelta::FromDays(1)));
   1409 
   1410   FetchRemoteChanges();
   1411 
   1412   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1413             fake_drive_service_helper()->AddFile(
   1414                 app_root_folder_id, "conflict_to_existing_remote", "bar",
   1415                 &file_id));
   1416   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1417             fake_drive_service_helper()->UpdateModificationTime(
   1418                 file_id,
   1419                 base::Time::Now() + base::TimeDelta::FromDays(1)));
   1420 
   1421   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1422   VerifyConsistency();
   1423 
   1424   EXPECT_EQ(1u, CountApp());
   1425   EXPECT_EQ(3u, CountLocalFile(app_id));
   1426   VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
   1427   VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
   1428 
   1429   EXPECT_EQ(4u, CountMetadata());
   1430   EXPECT_EQ(4u, CountTracker());
   1431 }
   1432 
   1433 TEST_F(DriveBackendSyncTest, ConflictTest_AddFile_DeleteFile) {
   1434   std::string app_id = "example";
   1435 
   1436   RegisterApp(app_id);
   1437 
   1438   AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
   1439   AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
   1440 
   1441   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1442   VerifyConsistency();
   1443 
   1444   // Test body starts from here.
   1445   RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
   1446   RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
   1447   AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
   1448   AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
   1449 
   1450   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1451             fake_drive_service_helper()->DeleteResource(
   1452                 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
   1453 
   1454   FetchRemoteChanges();
   1455 
   1456   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1457             fake_drive_service_helper()->DeleteResource(
   1458                 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
   1459 
   1460   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1461   VerifyConsistency();
   1462 
   1463   EXPECT_EQ(1u, CountApp());
   1464   EXPECT_EQ(3u, CountLocalFile(app_id));
   1465   VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
   1466   VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
   1467 
   1468   EXPECT_EQ(4u, CountMetadata());
   1469   EXPECT_EQ(4u, CountTracker());
   1470 }
   1471 
   1472 TEST_F(DriveBackendSyncTest, ConflictTest_UpdateFile_DeleteFile) {
   1473   std::string app_id = "example";
   1474 
   1475   RegisterApp(app_id);
   1476 
   1477   AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
   1478   AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
   1479 
   1480   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1481   VerifyConsistency();
   1482 
   1483   // Test body starts from here.
   1484   AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
   1485   AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
   1486 
   1487   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1488             fake_drive_service_helper()->DeleteResource(
   1489                 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
   1490 
   1491   FetchRemoteChanges();
   1492 
   1493   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1494             fake_drive_service_helper()->DeleteResource(
   1495                 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
   1496 
   1497   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1498   VerifyConsistency();
   1499 
   1500   EXPECT_EQ(1u, CountApp());
   1501   EXPECT_EQ(3u, CountLocalFile(app_id));
   1502   VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
   1503   VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
   1504 
   1505   EXPECT_EQ(4u, CountMetadata());
   1506   EXPECT_EQ(4u, CountTracker());
   1507 }
   1508 
   1509 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_AddFolder) {
   1510   std::string app_id = "example";
   1511 
   1512   RegisterApp(app_id);
   1513 
   1514   std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
   1515   AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
   1516   AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
   1517 
   1518   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1519   VerifyConsistency();
   1520 
   1521   // Test body starts from here.
   1522   RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
   1523   RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
   1524 
   1525   EXPECT_EQ(google_apis::HTTP_CREATED,
   1526             fake_drive_service_helper()->AddFolder(
   1527                 app_root_folder_id, "conflict_to_pending_remote", NULL));
   1528 
   1529   FetchRemoteChanges();
   1530 
   1531   EXPECT_EQ(google_apis::HTTP_CREATED,
   1532             fake_drive_service_helper()->AddFolder(
   1533                 app_root_folder_id, "conflict_to_existing_remote", NULL));
   1534 
   1535   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1536   VerifyConsistency();
   1537 
   1538   EXPECT_EQ(1u, CountApp());
   1539   EXPECT_EQ(3u, CountLocalFile(app_id));
   1540   VerifyLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1541   VerifyLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1542 
   1543   EXPECT_EQ(4u, CountMetadata());
   1544   EXPECT_EQ(4u, CountTracker());
   1545 }
   1546 
   1547 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_DeleteFolder) {
   1548   std::string app_id = "example";
   1549 
   1550   RegisterApp(app_id);
   1551 
   1552   AddLocalFolder(app_id, FPL("conflict_to_pending_remote"));
   1553   AddLocalFolder(app_id, FPL("conflict_to_existing_remote"));
   1554 
   1555   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1556   VerifyConsistency();
   1557 
   1558   // Test body starts from here.
   1559   RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
   1560   RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
   1561 
   1562   AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
   1563   AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
   1564 
   1565   RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
   1566   RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
   1567 
   1568   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1569             fake_drive_service_helper()->DeleteResource(
   1570                 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
   1571 
   1572   FetchRemoteChanges();
   1573 
   1574   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1575             fake_drive_service_helper()->DeleteResource(
   1576                 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
   1577 
   1578   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1579   VerifyConsistency();
   1580 
   1581   EXPECT_EQ(1u, CountApp());
   1582   EXPECT_EQ(1u, CountLocalFile(app_id));
   1583 
   1584   EXPECT_EQ(2u, CountMetadata());
   1585   EXPECT_EQ(2u, CountTracker());
   1586 }
   1587 
   1588 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_AddFile) {
   1589   std::string app_id = "example";
   1590 
   1591   RegisterApp(app_id);
   1592 
   1593   std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
   1594   AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
   1595   AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
   1596   RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
   1597   RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
   1598 
   1599   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1600             fake_drive_service_helper()->AddFile(
   1601                 app_root_folder_id, "conflict_to_pending_remote", "hoge",
   1602                 NULL));
   1603 
   1604   FetchRemoteChanges();
   1605 
   1606   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1607             fake_drive_service_helper()->AddFile(
   1608                 app_root_folder_id, "conflict_to_existing_remote", "fuga",
   1609                 NULL));
   1610 
   1611   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1612   VerifyConsistency();
   1613 
   1614   EXPECT_EQ(1u, CountApp());
   1615   EXPECT_EQ(3u, CountLocalFile(app_id));
   1616   VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
   1617   VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
   1618 
   1619   EXPECT_EQ(4u, CountMetadata());
   1620   EXPECT_EQ(4u, CountTracker());
   1621 }
   1622 
   1623 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_UpdateFile) {
   1624   std::string app_id = "example";
   1625 
   1626   RegisterApp(app_id);
   1627 
   1628   std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
   1629   AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
   1630   AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
   1631 
   1632   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1633   VerifyConsistency();
   1634 
   1635   // Test body starts from here.
   1636   RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
   1637   RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
   1638 
   1639   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1640             fake_drive_service_helper()->UpdateFile(
   1641                 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote")),
   1642                 "hoge"));
   1643 
   1644   FetchRemoteChanges();
   1645 
   1646   EXPECT_EQ(google_apis::HTTP_SUCCESS,
   1647             fake_drive_service_helper()->UpdateFile(
   1648                 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote")),
   1649                 "fuga"));
   1650 
   1651   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1652   VerifyConsistency();
   1653 
   1654   EXPECT_EQ(1u, CountApp());
   1655   EXPECT_EQ(3u, CountLocalFile(app_id));
   1656   VerifyLocalFile(app_id, FPL("conflict_to_pending_remote"), "hoge");
   1657   VerifyLocalFile(app_id, FPL("conflict_to_existing_remote"), "fuga");
   1658 
   1659   EXPECT_EQ(4u, CountMetadata());
   1660   EXPECT_EQ(4u, CountTracker());
   1661 }
   1662 
   1663 TEST_F(DriveBackendSyncTest, ConflictTest_DeleteFile_DeleteFile) {
   1664   std::string app_id = "example";
   1665 
   1666   RegisterApp(app_id);
   1667 
   1668   std::string app_root_folder_id = GetFileIDByPath(app_id, FPL(""));
   1669   AddOrUpdateLocalFile(app_id, FPL("conflict_to_pending_remote"), "foo");
   1670   AddOrUpdateLocalFile(app_id, FPL("conflict_to_existing_remote"), "bar");
   1671 
   1672   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1673   VerifyConsistency();
   1674 
   1675   // Test body starts from here.
   1676   RemoveLocal(app_id, FPL("conflict_to_pending_remote"));
   1677   RemoveLocal(app_id, FPL("conflict_to_existing_remote"));
   1678 
   1679   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1680             fake_drive_service_helper()->DeleteResource(
   1681                 GetFileIDByPath(app_id, FPL("conflict_to_pending_remote"))));
   1682 
   1683   FetchRemoteChanges();
   1684 
   1685   EXPECT_EQ(google_apis::HTTP_NO_CONTENT,
   1686             fake_drive_service_helper()->DeleteResource(
   1687                 GetFileIDByPath(app_id, FPL("conflict_to_existing_remote"))));
   1688 
   1689   EXPECT_EQ(SYNC_STATUS_OK, ProcessChangesUntilDone());
   1690   VerifyConsistency();
   1691 
   1692   EXPECT_EQ(1u, CountApp());
   1693   EXPECT_EQ(1u, CountLocalFile(app_id));
   1694 
   1695   EXPECT_EQ(2u, CountMetadata());
   1696   EXPECT_EQ(2u, CountTracker());
   1697 }
   1698 
   1699 }  // namespace drive_backend
   1700 }  // namespace sync_file_system
   1701