summaryrefslogtreecommitdiffstats
path: root/components/drive/file_system/remove_operation_unittest.cc
blob: 5401275281470b9be63f26aa4616895d014308c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/drive/file_system/remove_operation.h"

#include "components/drive/file_change.h"
#include "components/drive/file_system/operation_test_base.h"
#include "components/drive/file_system_core_util.h"
#include "content/public/test/test_utils.h"
#include "google_apis/drive/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace drive {
namespace file_system {

typedef OperationTestBase RemoveOperationTest;

TEST_F(RemoveOperationTest, RemoveFile) {
  RemoveOperation operation(blocking_task_runner(), delegate(), metadata(),
                            cache());

  base::FilePath nonexisting_file(
      FILE_PATH_LITERAL("drive/root/Dummy file.txt"));
  base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
  base::FilePath file_in_subdir(
      FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));

  // Remove a file in root.
  ResourceEntry entry;
  FileError error = FILE_ERROR_FAILED;
  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &entry));
  operation.Remove(file_in_root,
                   false,  // is_recursive
                   google_apis::test_util::CreateCopyResultCallback(&error));
  content::RunAllBlockingPoolTasksUntilIdle();
  EXPECT_EQ(FILE_ERROR_OK, error);
  EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(file_in_root, &entry));

  const std::string id_file_in_root = entry.local_id();
  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntryById(id_file_in_root, &entry));
  EXPECT_EQ(util::kDriveTrashDirLocalId, entry.parent_local_id());

  // Remove a file in subdirectory.
  error = FILE_ERROR_FAILED;
  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_subdir, &entry));
  const std::string resource_id = entry.resource_id();

  operation.Remove(file_in_subdir,
                   false,  // is_recursive
                   google_apis::test_util::CreateCopyResultCallback(&error));
  content::RunAllBlockingPoolTasksUntilIdle();
  EXPECT_EQ(FILE_ERROR_OK, error);
  EXPECT_EQ(FILE_ERROR_NOT_FOUND,
            GetLocalResourceEntry(file_in_subdir, &entry));

  const std::string id_file_in_subdir = entry.local_id();
  EXPECT_EQ(FILE_ERROR_OK,
            GetLocalResourceEntryById(id_file_in_subdir, &entry));
  EXPECT_EQ(util::kDriveTrashDirLocalId, entry.parent_local_id());

  // Try removing non-existing file.
  error = FILE_ERROR_FAILED;
  ASSERT_EQ(FILE_ERROR_NOT_FOUND,
            GetLocalResourceEntry(nonexisting_file, &entry));
  operation.Remove(nonexisting_file,
                   false,  // is_recursive
                   google_apis::test_util::CreateCopyResultCallback(&error));
  content::RunAllBlockingPoolTasksUntilIdle();
  EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);

  // Verify delegate notifications.
  EXPECT_EQ(2U, delegate()->get_changed_files().size());
  EXPECT_TRUE(delegate()->get_changed_files().count(file_in_root));
  EXPECT_TRUE(delegate()->get_changed_files().count(file_in_subdir));

  EXPECT_EQ(2U, delegate()->updated_local_ids().size());
  EXPECT_TRUE(delegate()->updated_local_ids().count(id_file_in_root));
  EXPECT_TRUE(delegate()->updated_local_ids().count(id_file_in_subdir));
}

TEST_F(RemoveOperationTest, RemoveDirectory) {
  RemoveOperation operation(blocking_task_runner(), delegate(), metadata(),
                            cache());

  base::FilePath empty_dir(FILE_PATH_LITERAL(
      "drive/root/Directory 1/Sub Directory Folder/Sub Sub Directory Folder"));
  base::FilePath non_empty_dir(FILE_PATH_LITERAL(
      "drive/root/Directory 1"));
  base::FilePath file_in_non_empty_dir(FILE_PATH_LITERAL(
      "drive/root/Directory 1/SubDirectory File 1.txt"));

  // Empty directory can be removed even with is_recursive = false.
  FileError error = FILE_ERROR_FAILED;
  ResourceEntry entry;
  operation.Remove(empty_dir,
                   false,  // is_recursive
                   google_apis::test_util::CreateCopyResultCallback(&error));
  content::RunAllBlockingPoolTasksUntilIdle();
  EXPECT_EQ(FILE_ERROR_OK, error);
  EXPECT_EQ(FILE_ERROR_NOT_FOUND,
            GetLocalResourceEntry(empty_dir, &entry));

  // Non-empty directory, cannot.
  error = FILE_ERROR_FAILED;
  ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(non_empty_dir, &entry));
  operation.Remove(non_empty_dir,
                   false,  // is_recursive
                   google_apis::test_util::CreateCopyResultCallback(&error));
  content::RunAllBlockingPoolTasksUntilIdle();
  EXPECT_EQ(FILE_ERROR_NOT_EMPTY, error);
  EXPECT_EQ(FILE_ERROR_OK,
            GetLocalResourceEntry(non_empty_dir, &entry));

  // With is_recursive = true, it can be deleted, however. Descendant entries
  // are removed together.
  error = FILE_ERROR_FAILED;
  ASSERT_EQ(FILE_ERROR_OK,
            GetLocalResourceEntry(file_in_non_empty_dir, &entry));
  operation.Remove(non_empty_dir,
                   true,  // is_recursive
                   google_apis::test_util::CreateCopyResultCallback(&error));
  content::RunAllBlockingPoolTasksUntilIdle();
  EXPECT_EQ(FILE_ERROR_OK, error);
  EXPECT_EQ(FILE_ERROR_NOT_FOUND,
            GetLocalResourceEntry(non_empty_dir, &entry));
  EXPECT_EQ(FILE_ERROR_NOT_FOUND,
            GetLocalResourceEntry(file_in_non_empty_dir, &entry));
}

}  // namespace file_system
}  // namespace drive