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