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.
5 #include "chrome/browser/chromeos/drive/sync/remove_performer.h"
7 #include "base/task_runner_util.h"
8 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
9 #include "chrome/browser/chromeos/drive/file_system_interface.h"
10 #include "chrome/browser/chromeos/drive/file_system_util.h"
11 #include "chrome/browser/drive/fake_drive_service.h"
12 #include "google_apis/drive/gdata_wapi_parser.h"
13 #include "google_apis/drive/test_util.h"
18 typedef file_system::OperationTestBase RemovePerformerTest;
20 TEST_F(RemovePerformerTest, RemoveFile) {
21 RemovePerformer performer(blocking_task_runner(), observer(), scheduler(),
24 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
25 base::FilePath file_in_subdir(
26 FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt"));
28 // Remove a file in root.
30 FileError error = FILE_ERROR_FAILED;
31 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &entry));
32 performer.Remove(entry.local_id(),
33 ClientContext(USER_INITIATED),
34 google_apis::test_util::CreateCopyResultCallback(&error));
35 test_util::RunBlockingPoolTask();
36 EXPECT_EQ(FILE_ERROR_OK, error);
38 // Remove a file in subdirectory.
39 error = FILE_ERROR_FAILED;
40 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_subdir, &entry));
41 const std::string resource_id = entry.resource_id();
43 performer.Remove(entry.local_id(),
44 ClientContext(USER_INITIATED),
45 google_apis::test_util::CreateCopyResultCallback(&error));
46 test_util::RunBlockingPoolTask();
47 EXPECT_EQ(FILE_ERROR_OK, error);
49 // Verify the file is indeed removed in the server.
50 google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR;
51 scoped_ptr<google_apis::ResourceEntry> gdata_entry;
52 fake_service()->GetResourceEntry(
54 google_apis::test_util::CreateCopyResultCallback(&gdata_error,
56 test_util::RunBlockingPoolTask();
57 ASSERT_EQ(google_apis::HTTP_SUCCESS, gdata_error);
58 EXPECT_TRUE(gdata_entry->deleted());
60 // Try removing non-existing file.
61 error = FILE_ERROR_FAILED;
62 performer.Remove("non-existing-id",
63 ClientContext(USER_INITIATED),
64 google_apis::test_util::CreateCopyResultCallback(&error));
65 test_util::RunBlockingPoolTask();
66 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
69 TEST_F(RemovePerformerTest, RemoveShared) {
70 RemovePerformer performer(blocking_task_runner(), observer(), scheduler(),
73 const base::FilePath kPathInMyDrive(FILE_PATH_LITERAL(
74 "drive/root/shared.txt"));
75 const base::FilePath kPathInOther(FILE_PATH_LITERAL(
76 "drive/other/shared.txt"));
78 // Prepare a shared file to the root folder.
79 google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR;
80 scoped_ptr<google_apis::ResourceEntry> gdata_entry;
81 fake_service()->AddNewFile(
84 fake_service()->GetRootResourceId(),
85 kPathInMyDrive.BaseName().AsUTF8Unsafe(),
86 true, // shared_with_me,
87 google_apis::test_util::CreateCopyResultCallback(&gdata_error,
89 test_util::RunBlockingPoolTask();
90 ASSERT_EQ(google_apis::HTTP_CREATED, gdata_error);
93 // Remove it. Locally, the file should be moved to drive/other.
95 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPathInMyDrive, &entry));
96 FileError error = FILE_ERROR_FAILED;
97 performer.Remove(entry.local_id(),
98 ClientContext(USER_INITIATED),
99 google_apis::test_util::CreateCopyResultCallback(&error));
100 test_util::RunBlockingPoolTask();
101 EXPECT_EQ(FILE_ERROR_OK, error);
102 EXPECT_EQ(FILE_ERROR_NOT_FOUND,
103 GetLocalResourceEntry(kPathInMyDrive, &entry));
104 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPathInOther, &entry));
106 // Remotely, the entry should have lost its parent.
107 gdata_error = google_apis::GDATA_OTHER_ERROR;
108 const std::string resource_id = gdata_entry->resource_id();
109 fake_service()->GetResourceEntry(
111 google_apis::test_util::CreateCopyResultCallback(&gdata_error,
113 test_util::RunBlockingPoolTask();
114 ASSERT_EQ(google_apis::HTTP_SUCCESS, gdata_error);
115 EXPECT_FALSE(gdata_entry->deleted()); // It's not deleted.
116 EXPECT_FALSE(gdata_entry->GetLinkByType(google_apis::Link::LINK_PARENT));
119 TEST_F(RemovePerformerTest, RemoveLocallyCreatedFile) {
120 RemovePerformer performer(blocking_task_runner(), observer(), scheduler(),
123 // Add an entry without resource ID.
125 entry.set_title("New File.txt");
126 entry.set_parent_local_id(util::kDriveTrashDirLocalId);
128 FileError error = FILE_ERROR_FAILED;
129 std::string local_id;
130 base::PostTaskAndReplyWithResult(
131 blocking_task_runner(),
133 base::Bind(&ResourceMetadata::AddEntry,
134 base::Unretained(metadata()),
137 google_apis::test_util::CreateCopyResultCallback(&error));
138 test_util::RunBlockingPoolTask();
139 EXPECT_EQ(FILE_ERROR_OK, error);
142 performer.Remove(local_id, ClientContext(USER_INITIATED),
143 google_apis::test_util::CreateCopyResultCallback(&error));
144 test_util::RunBlockingPoolTask();
145 EXPECT_EQ(FILE_ERROR_OK, error);
146 EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntryById(local_id, &entry));
149 } // namespace internal