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