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