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