Home | History | Annotate | Download | only in sync
      1 // Copyright 2013 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 "chrome/browser/chromeos/drive/sync/entry_revert_performer.h"
      6 
      7 #include "base/task_runner_util.h"
      8 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
      9 #include "chrome/browser/chromeos/drive/file_system_util.h"
     10 #include "chrome/browser/chromeos/drive/job_scheduler.h"
     11 #include "chrome/browser/chromeos/drive/resource_metadata.h"
     12 #include "chrome/browser/drive/fake_drive_service.h"
     13 #include "google_apis/drive/test_util.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 
     16 namespace drive {
     17 namespace internal {
     18 
     19 class EntryRevertPerformerTest : public file_system::OperationTestBase {
     20  protected:
     21   virtual void SetUp() OVERRIDE {
     22    OperationTestBase::SetUp();
     23    performer_.reset(new EntryRevertPerformer(blocking_task_runner(),
     24                                              observer(),
     25                                              scheduler(),
     26                                              metadata()));
     27   }
     28 
     29   scoped_ptr<EntryRevertPerformer> performer_;
     30 };
     31 
     32 TEST_F(EntryRevertPerformerTest, RevertEntry) {
     33   base::FilePath path(
     34       FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
     35 
     36   ResourceEntry src_entry;
     37   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(path, &src_entry));
     38 
     39   // Update local entry.
     40   ResourceEntry updated_entry = src_entry;
     41   updated_entry.set_title("Updated" + src_entry.title());
     42   updated_entry.set_metadata_edit_state(ResourceEntry::DIRTY);
     43 
     44   FileError error = FILE_ERROR_FAILED;
     45   base::PostTaskAndReplyWithResult(
     46       blocking_task_runner(),
     47       FROM_HERE,
     48       base::Bind(&ResourceMetadata::RefreshEntry,
     49                  base::Unretained(metadata()), updated_entry),
     50       google_apis::test_util::CreateCopyResultCallback(&error));
     51   test_util::RunBlockingPoolTask();
     52   EXPECT_EQ(FILE_ERROR_OK, error);
     53 
     54   // Revert local change.
     55   error = FILE_ERROR_FAILED;
     56   performer_->RevertEntry(
     57       src_entry.local_id(),
     58       ClientContext(USER_INITIATED),
     59       google_apis::test_util::CreateCopyResultCallback(&error));
     60   test_util::RunBlockingPoolTask();
     61   EXPECT_EQ(FILE_ERROR_OK, error);
     62 
     63   // Verify local change is reverted.
     64   ResourceEntry result_entry;
     65   EXPECT_EQ(FILE_ERROR_OK,
     66             GetLocalResourceEntryById(src_entry.local_id(), &result_entry));
     67   EXPECT_EQ(src_entry.title(), result_entry.title());
     68   EXPECT_EQ(ResourceEntry::CLEAN, result_entry.metadata_edit_state());
     69 
     70   EXPECT_EQ(1U, observer()->get_changed_paths().size());
     71   EXPECT_TRUE(observer()->get_changed_paths().count(path.DirName()));
     72 }
     73 
     74 TEST_F(EntryRevertPerformerTest, RevertEntry_NotFoundOnServer) {
     75   ResourceEntry my_drive;
     76   EXPECT_EQ(FILE_ERROR_OK,
     77             GetLocalResourceEntry(util::GetDriveMyDriveRootPath(), &my_drive));
     78 
     79   // Add a new entry with a nonexistent resource ID.
     80   ResourceEntry entry;
     81   entry.set_title("New File.txt");
     82   entry.set_resource_id("non-existent-resource-id");
     83   entry.set_parent_local_id(my_drive.local_id());
     84 
     85   FileError error = FILE_ERROR_FAILED;
     86   std::string local_id;
     87   base::PostTaskAndReplyWithResult(
     88       blocking_task_runner(),
     89       FROM_HERE,
     90       base::Bind(&ResourceMetadata::AddEntry,
     91                  base::Unretained(metadata()), entry, &local_id),
     92       google_apis::test_util::CreateCopyResultCallback(&error));
     93   test_util::RunBlockingPoolTask();
     94   EXPECT_EQ(FILE_ERROR_OK, error);
     95 
     96   // Revert local change. The added entry should be removed.
     97   error = FILE_ERROR_FAILED;
     98   performer_->RevertEntry(
     99       local_id,
    100       ClientContext(USER_INITIATED),
    101       google_apis::test_util::CreateCopyResultCallback(&error));
    102   test_util::RunBlockingPoolTask();
    103   EXPECT_EQ(FILE_ERROR_OK, error);
    104 
    105   // Verify the entry was deleted locally.
    106   EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntryById(local_id, &entry));
    107 
    108   EXPECT_EQ(1U, observer()->get_changed_paths().size());
    109   EXPECT_TRUE(observer()->get_changed_paths().count(
    110       util::GetDriveMyDriveRootPath()));
    111 }
    112 
    113 TEST_F(EntryRevertPerformerTest, RevertEntry_TrashedOnServer) {
    114   base::FilePath path(
    115       FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
    116 
    117   ResourceEntry entry;
    118   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(path, &entry));
    119 
    120   // Trash the entry on the server.
    121   google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR;
    122   fake_service()->TrashResource(
    123       entry.resource_id(),
    124       google_apis::test_util::CreateCopyResultCallback(&gdata_error));
    125   test_util::RunBlockingPoolTask();
    126   EXPECT_EQ(google_apis::HTTP_SUCCESS, gdata_error);
    127 
    128   // Revert local change. The entry should be removed.
    129   FileError error = FILE_ERROR_FAILED;
    130   performer_->RevertEntry(
    131       entry.local_id(),
    132       ClientContext(USER_INITIATED),
    133       google_apis::test_util::CreateCopyResultCallback(&error));
    134   test_util::RunBlockingPoolTask();
    135   EXPECT_EQ(FILE_ERROR_OK, error);
    136 
    137   // Verify the entry was deleted locally.
    138   EXPECT_EQ(FILE_ERROR_NOT_FOUND,
    139             GetLocalResourceEntryById(entry.local_id(), &entry));
    140 
    141   EXPECT_EQ(1U, observer()->get_changed_paths().size());
    142   EXPECT_TRUE(observer()->get_changed_paths().count(path.DirName()));
    143 }
    144 
    145 }  // namespace internal
    146 }  // namespace drive
    147