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