move_operation_unittest.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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/file_system/move_operation.h"
6
7#include "chrome/browser/chromeos/drive/file_system/copy_operation.h"
8#include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
9#include "chrome/browser/drive/drive_api_util.h"
10#include "google_apis/drive/test_util.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace drive {
14namespace file_system {
15
16class MoveOperationTest : public OperationTestBase {
17 protected:
18  virtual void SetUp() OVERRIDE {
19   OperationTestBase::SetUp();
20   operation_.reset(new MoveOperation(blocking_task_runner(),
21                                      observer(),
22                                      metadata()));
23   copy_operation_.reset(new CopyOperation(
24       blocking_task_runner(),
25       observer(),
26       scheduler(),
27       metadata(),
28       cache(),
29       util::GetIdentityResourceIdCanonicalizer()));
30  }
31
32  scoped_ptr<MoveOperation> operation_;
33  scoped_ptr<CopyOperation> copy_operation_;
34};
35
36TEST_F(MoveOperationTest, MoveFileInSameDirectory) {
37  const base::FilePath src_path(
38      FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
39  const base::FilePath dest_path(
40      FILE_PATH_LITERAL("drive/root/Directory 1/Test.log"));
41
42  ResourceEntry src_entry, dest_entry;
43  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &src_entry));
44  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
45            GetLocalResourceEntry(dest_path, &dest_entry));
46
47  FileError error = FILE_ERROR_FAILED;
48  operation_->Move(src_path,
49                   dest_path,
50                   false,
51                   google_apis::test_util::CreateCopyResultCallback(&error));
52  test_util::RunBlockingPoolTask();
53  EXPECT_EQ(FILE_ERROR_OK, error);
54
55  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &dest_entry));
56  EXPECT_EQ(src_entry.local_id(), dest_entry.local_id());
57  EXPECT_EQ(ResourceEntry::DIRTY, dest_entry.metadata_edit_state());
58  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &src_entry));
59
60  EXPECT_EQ(1U, observer()->get_changed_paths().size());
61  EXPECT_TRUE(observer()->get_changed_paths().count(src_path.DirName()));
62
63  EXPECT_EQ(1U, observer()->updated_local_ids().size());
64  EXPECT_TRUE(observer()->updated_local_ids().count(src_entry.local_id()));
65}
66
67TEST_F(MoveOperationTest, MoveFileFromRootToSubDirectory) {
68  base::FilePath src_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
69  base::FilePath dest_path(
70      FILE_PATH_LITERAL("drive/root/Directory 1/Test.log"));
71
72  ResourceEntry src_entry, dest_entry;
73  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &src_entry));
74  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
75            GetLocalResourceEntry(dest_path, &dest_entry));
76
77  FileError error = FILE_ERROR_FAILED;
78  operation_->Move(src_path,
79                   dest_path,
80                   false,
81                   google_apis::test_util::CreateCopyResultCallback(&error));
82  test_util::RunBlockingPoolTask();
83  EXPECT_EQ(FILE_ERROR_OK, error);
84
85  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &dest_entry));
86  EXPECT_EQ(src_entry.local_id(), dest_entry.local_id());
87  EXPECT_EQ(ResourceEntry::DIRTY, dest_entry.metadata_edit_state());
88  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &src_entry));
89
90  EXPECT_EQ(2U, observer()->get_changed_paths().size());
91  EXPECT_TRUE(observer()->get_changed_paths().count(src_path.DirName()));
92  EXPECT_TRUE(observer()->get_changed_paths().count(dest_path.DirName()));
93
94  EXPECT_EQ(1U, observer()->updated_local_ids().size());
95  EXPECT_TRUE(observer()->updated_local_ids().count(src_entry.local_id()));
96}
97
98TEST_F(MoveOperationTest, MoveFileBetweenSubDirectoriesRenameWithTitle) {
99  base::FilePath src_path(
100      FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
101  base::FilePath dest_path(FILE_PATH_LITERAL(
102      "drive/root/Directory 1/Sub Directory Folder/"
103      "SubDirectory File 1 (1).txt"));
104
105  ResourceEntry src_entry, dest_entry;
106  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &src_entry));
107  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
108            GetLocalResourceEntry(dest_path, &dest_entry));
109
110  FileError error = FILE_ERROR_FAILED;
111  // Copy the src file into the same directory. This will make inconsistency
112  // between title and path of the copied file.
113  copy_operation_->Copy(
114      src_path,
115      src_path,
116      false,
117      google_apis::test_util::CreateCopyResultCallback(&error));
118  test_util::RunBlockingPoolTask();
119  EXPECT_EQ(FILE_ERROR_OK, error);
120  base::FilePath copied_path(
121      FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1 (1).txt"));
122  ResourceEntry copied_entry;
123  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(copied_path, &copied_entry));
124  ASSERT_EQ("SubDirectory File 1.txt", copied_entry.title());
125
126  // Move the copied file.
127  operation_->Move(copied_path,
128                   dest_path,
129                   false,
130                   google_apis::test_util::CreateCopyResultCallback(&error));
131  test_util::RunBlockingPoolTask();
132  EXPECT_EQ(FILE_ERROR_OK, error);
133
134  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &dest_entry));
135  EXPECT_EQ("SubDirectory File 1 (1).txt", dest_entry.title());
136  EXPECT_EQ(copied_entry.local_id(), dest_entry.local_id());
137  EXPECT_EQ(ResourceEntry::DIRTY, dest_entry.metadata_edit_state());
138  EXPECT_EQ(FILE_ERROR_NOT_FOUND,
139            GetLocalResourceEntry(copied_path, &copied_entry));
140
141  EXPECT_EQ(2U, observer()->get_changed_paths().size());
142  EXPECT_TRUE(observer()->get_changed_paths().count(copied_path.DirName()));
143  EXPECT_TRUE(observer()->get_changed_paths().count(dest_path.DirName()));
144
145  EXPECT_EQ(1U, observer()->updated_local_ids().size());
146  EXPECT_TRUE(observer()->updated_local_ids().count(copied_entry.local_id()));
147}
148
149TEST_F(MoveOperationTest, MoveNotExistingFile) {
150  base::FilePath src_path(FILE_PATH_LITERAL("drive/root/Dummy file.txt"));
151  base::FilePath dest_path(FILE_PATH_LITERAL("drive/root/Test.log"));
152
153  FileError error = FILE_ERROR_OK;
154  operation_->Move(src_path,
155                   dest_path,
156                   false,
157                   google_apis::test_util::CreateCopyResultCallback(&error));
158  test_util::RunBlockingPoolTask();
159  EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
160
161  ResourceEntry entry;
162  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &entry));
163  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
164}
165
166TEST_F(MoveOperationTest, MoveFileToNonExistingDirectory) {
167  base::FilePath src_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
168  base::FilePath dest_path(FILE_PATH_LITERAL("drive/root/Dummy/Test.log"));
169
170  FileError error = FILE_ERROR_OK;
171  operation_->Move(src_path,
172                   dest_path,
173                   false,
174                   google_apis::test_util::CreateCopyResultCallback(&error));
175  test_util::RunBlockingPoolTask();
176  EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
177
178  ResourceEntry entry;
179  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
180  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
181}
182
183// Test the case where the parent of |dest_file_path| is a existing file,
184// not a directory.
185TEST_F(MoveOperationTest, MoveFileToInvalidPath) {
186  base::FilePath src_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
187  base::FilePath dest_path(
188      FILE_PATH_LITERAL("drive/root/Duplicate Name.txt/Test.log"));
189
190  FileError error = FILE_ERROR_OK;
191  operation_->Move(src_path,
192                   dest_path,
193                   false,
194                   google_apis::test_util::CreateCopyResultCallback(&error));
195  test_util::RunBlockingPoolTask();
196  EXPECT_EQ(FILE_ERROR_NOT_A_DIRECTORY, error);
197
198  ResourceEntry entry;
199  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
200  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
201}
202
203TEST_F(MoveOperationTest, PreserveLastModified) {
204  const base::FilePath src_path(
205      FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
206  const base::FilePath dest_path(
207      FILE_PATH_LITERAL("drive/root/Directory 1/Test.log"));
208
209  ResourceEntry src_entry, dest_entry;
210  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &src_entry));
211  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
212            GetLocalResourceEntry(dest_path, &dest_entry));
213
214  FileError error = FILE_ERROR_FAILED;
215  operation_->Move(src_path,
216                   dest_path,
217                   true,  // Preserve last modified.
218                   google_apis::test_util::CreateCopyResultCallback(&error));
219  test_util::RunBlockingPoolTask();
220  EXPECT_EQ(FILE_ERROR_OK, error);
221
222  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &dest_entry));
223  EXPECT_EQ(src_entry.local_id(), dest_entry.local_id());
224  EXPECT_EQ(src_entry.file_info().last_modified(),
225            dest_entry.file_info().last_modified());
226  EXPECT_EQ(ResourceEntry::DIRTY, dest_entry.metadata_edit_state());
227  EXPECT_EQ(FILE_ERROR_NOT_FOUND,
228            GetLocalResourceEntry(src_path, &src_entry));
229}
230
231}  // namespace file_system
232}  // namespace drive
233