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