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/remove_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 "chrome/browser/chromeos/drive/file_system_util.h"
10#include "content/public/test/test_utils.h"
11#include "google_apis/drive/test_util.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace drive {
15namespace file_system {
16
17typedef OperationTestBase RemoveOperationTest;
18
19TEST_F(RemoveOperationTest, RemoveFile) {
20  RemoveOperation operation(blocking_task_runner(), delegate(), metadata(),
21                            cache());
22
23  base::FilePath nonexisting_file(
24      FILE_PATH_LITERAL("drive/root/Dummy file.txt"));
25  base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
26  base::FilePath file_in_subdir(
27      FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
28
29  // Remove a file in root.
30  ResourceEntry entry;
31  FileError error = FILE_ERROR_FAILED;
32  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &entry));
33  operation.Remove(file_in_root,
34                   false,  // is_recursive
35                   google_apis::test_util::CreateCopyResultCallback(&error));
36  content::RunAllBlockingPoolTasksUntilIdle();
37  EXPECT_EQ(FILE_ERROR_OK, error);
38  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(file_in_root, &entry));
39
40  const std::string id_file_in_root = entry.local_id();
41  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntryById(id_file_in_root, &entry));
42  EXPECT_EQ(util::kDriveTrashDirLocalId, entry.parent_local_id());
43
44  // Remove a file in subdirectory.
45  error = FILE_ERROR_FAILED;
46  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_subdir, &entry));
47  const std::string resource_id = entry.resource_id();
48
49  operation.Remove(file_in_subdir,
50                   false,  // is_recursive
51                   google_apis::test_util::CreateCopyResultCallback(&error));
52  content::RunAllBlockingPoolTasksUntilIdle();
53  EXPECT_EQ(FILE_ERROR_OK, error);
54  EXPECT_EQ(FILE_ERROR_NOT_FOUND,
55            GetLocalResourceEntry(file_in_subdir, &entry));
56
57  const std::string id_file_in_subdir = entry.local_id();
58  EXPECT_EQ(FILE_ERROR_OK,
59            GetLocalResourceEntryById(id_file_in_subdir, &entry));
60  EXPECT_EQ(util::kDriveTrashDirLocalId, entry.parent_local_id());
61
62  // Try removing non-existing file.
63  error = FILE_ERROR_FAILED;
64  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
65            GetLocalResourceEntry(nonexisting_file, &entry));
66  operation.Remove(nonexisting_file,
67                   false,  // is_recursive
68                   google_apis::test_util::CreateCopyResultCallback(&error));
69  content::RunAllBlockingPoolTasksUntilIdle();
70  EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
71
72  // Verify delegate notifications.
73  EXPECT_EQ(2U, delegate()->get_changed_files().size());
74  EXPECT_TRUE(delegate()->get_changed_files().count(file_in_root));
75  EXPECT_TRUE(delegate()->get_changed_files().count(file_in_subdir));
76
77  EXPECT_EQ(2U, delegate()->updated_local_ids().size());
78  EXPECT_TRUE(delegate()->updated_local_ids().count(id_file_in_root));
79  EXPECT_TRUE(delegate()->updated_local_ids().count(id_file_in_subdir));
80}
81
82TEST_F(RemoveOperationTest, RemoveDirectory) {
83  RemoveOperation operation(blocking_task_runner(), delegate(), metadata(),
84                            cache());
85
86  base::FilePath empty_dir(FILE_PATH_LITERAL(
87      "drive/root/Directory 1/Sub Directory Folder/Sub Sub Directory Folder"));
88  base::FilePath non_empty_dir(FILE_PATH_LITERAL(
89      "drive/root/Directory 1"));
90  base::FilePath file_in_non_empty_dir(FILE_PATH_LITERAL(
91      "drive/root/Directory 1/SubDirectory File 1.txt"));
92
93  // Empty directory can be removed even with is_recursive = false.
94  FileError error = FILE_ERROR_FAILED;
95  ResourceEntry entry;
96  operation.Remove(empty_dir,
97                   false,  // is_recursive
98                   google_apis::test_util::CreateCopyResultCallback(&error));
99  content::RunAllBlockingPoolTasksUntilIdle();
100  EXPECT_EQ(FILE_ERROR_OK, error);
101  EXPECT_EQ(FILE_ERROR_NOT_FOUND,
102            GetLocalResourceEntry(empty_dir, &entry));
103
104  // Non-empty directory, cannot.
105  error = FILE_ERROR_FAILED;
106  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(non_empty_dir, &entry));
107  operation.Remove(non_empty_dir,
108                   false,  // is_recursive
109                   google_apis::test_util::CreateCopyResultCallback(&error));
110  content::RunAllBlockingPoolTasksUntilIdle();
111  EXPECT_EQ(FILE_ERROR_NOT_EMPTY, error);
112  EXPECT_EQ(FILE_ERROR_OK,
113            GetLocalResourceEntry(non_empty_dir, &entry));
114
115  // With is_recursive = true, it can be deleted, however. Descendant entries
116  // are removed together.
117  error = FILE_ERROR_FAILED;
118  ASSERT_EQ(FILE_ERROR_OK,
119            GetLocalResourceEntry(file_in_non_empty_dir, &entry));
120  operation.Remove(non_empty_dir,
121                   true,  // is_recursive
122                   google_apis::test_util::CreateCopyResultCallback(&error));
123  content::RunAllBlockingPoolTasksUntilIdle();
124  EXPECT_EQ(FILE_ERROR_OK, error);
125  EXPECT_EQ(FILE_ERROR_NOT_FOUND,
126            GetLocalResourceEntry(non_empty_dir, &entry));
127  EXPECT_EQ(FILE_ERROR_NOT_FOUND,
128            GetLocalResourceEntry(file_in_non_empty_dir, &entry));
129}
130
131}  // namespace file_system
132}  // namespace drive
133