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_change.h"
8#include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
9#include "content/public/test/test_utils.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                                      delegate(),
22                                      metadata()));
23  }
24
25  scoped_ptr<MoveOperation> operation_;
26};
27
28TEST_F(MoveOperationTest, MoveFileInSameDirectory) {
29  const base::FilePath src_path(
30      FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
31  const base::FilePath dest_path(
32      FILE_PATH_LITERAL("drive/root/Directory 1/Test.log"));
33
34  ResourceEntry src_entry, dest_entry;
35  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &src_entry));
36  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
37            GetLocalResourceEntry(dest_path, &dest_entry));
38
39  FileError error = FILE_ERROR_FAILED;
40  operation_->Move(src_path,
41                   dest_path,
42                   google_apis::test_util::CreateCopyResultCallback(&error));
43  content::RunAllBlockingPoolTasksUntilIdle();
44  EXPECT_EQ(FILE_ERROR_OK, error);
45
46  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &dest_entry));
47  EXPECT_EQ(src_entry.local_id(), dest_entry.local_id());
48  EXPECT_EQ(ResourceEntry::DIRTY, dest_entry.metadata_edit_state());
49  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &src_entry));
50
51  EXPECT_EQ(2U, delegate()->get_changed_files().size());
52  EXPECT_TRUE(delegate()->get_changed_files().count(src_path));
53  EXPECT_TRUE(delegate()->get_changed_files().count(dest_path));
54
55  EXPECT_EQ(1U, delegate()->updated_local_ids().size());
56  EXPECT_TRUE(delegate()->updated_local_ids().count(src_entry.local_id()));
57}
58
59TEST_F(MoveOperationTest, MoveFileFromRootToSubDirectory) {
60  base::FilePath src_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
61  base::FilePath dest_path(
62      FILE_PATH_LITERAL("drive/root/Directory 1/Test.log"));
63
64  ResourceEntry src_entry, dest_entry;
65  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &src_entry));
66  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
67            GetLocalResourceEntry(dest_path, &dest_entry));
68
69  FileError error = FILE_ERROR_FAILED;
70  operation_->Move(src_path,
71                   dest_path,
72                   google_apis::test_util::CreateCopyResultCallback(&error));
73  content::RunAllBlockingPoolTasksUntilIdle();
74  EXPECT_EQ(FILE_ERROR_OK, error);
75
76  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &dest_entry));
77  EXPECT_EQ(src_entry.local_id(), dest_entry.local_id());
78  EXPECT_EQ(ResourceEntry::DIRTY, dest_entry.metadata_edit_state());
79  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &src_entry));
80
81  EXPECT_EQ(2U, delegate()->get_changed_files().size());
82  EXPECT_TRUE(delegate()->get_changed_files().count(src_path));
83  EXPECT_TRUE(delegate()->get_changed_files().count(dest_path));
84
85  EXPECT_EQ(1U, delegate()->updated_local_ids().size());
86  EXPECT_TRUE(delegate()->updated_local_ids().count(src_entry.local_id()));
87}
88
89TEST_F(MoveOperationTest, MoveNotExistingFile) {
90  base::FilePath src_path(FILE_PATH_LITERAL("drive/root/Dummy file.txt"));
91  base::FilePath dest_path(FILE_PATH_LITERAL("drive/root/Test.log"));
92
93  FileError error = FILE_ERROR_OK;
94  operation_->Move(src_path,
95                   dest_path,
96                   google_apis::test_util::CreateCopyResultCallback(&error));
97  content::RunAllBlockingPoolTasksUntilIdle();
98  EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
99
100  ResourceEntry entry;
101  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &entry));
102  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
103}
104
105TEST_F(MoveOperationTest, MoveFileToNonExistingDirectory) {
106  base::FilePath src_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
107  base::FilePath dest_path(FILE_PATH_LITERAL("drive/root/Dummy/Test.log"));
108
109  FileError error = FILE_ERROR_OK;
110  operation_->Move(src_path,
111                   dest_path,
112                   google_apis::test_util::CreateCopyResultCallback(&error));
113  content::RunAllBlockingPoolTasksUntilIdle();
114  EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
115
116  ResourceEntry entry;
117  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
118  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
119}
120
121// Test the case where the parent of |dest_file_path| is a existing file,
122// not a directory.
123TEST_F(MoveOperationTest, MoveFileToInvalidPath) {
124  base::FilePath src_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
125  base::FilePath dest_path(
126      FILE_PATH_LITERAL("drive/root/Duplicate Name.txt/Test.log"));
127
128  FileError error = FILE_ERROR_OK;
129  operation_->Move(src_path,
130                   dest_path,
131                   google_apis::test_util::CreateCopyResultCallback(&error));
132  content::RunAllBlockingPoolTasksUntilIdle();
133  EXPECT_EQ(FILE_ERROR_NOT_A_DIRECTORY, error);
134
135  ResourceEntry entry;
136  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
137  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
138}
139
140}  // namespace file_system
141}  // namespace drive
142