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_util.h"
10 #include "chrome/browser/chromeos/drive/job_scheduler.h"
11 #include "chrome/browser/chromeos/drive/resource_metadata.h"
12 #include "chrome/browser/drive/fake_drive_service.h"
13 #include "google_apis/drive/drive_api_parser.h"
14 #include "google_apis/drive/test_util.h"
19 typedef file_system::OperationTestBase RemovePerformerTest;
21 TEST_F(RemovePerformerTest, RemoveFile) {
22 RemovePerformer performer(blocking_task_runner(), observer(), scheduler(),
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"));
29 // Remove a file in root.
31 FileError error = FILE_ERROR_FAILED;
32 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &entry));
33 performer.Remove(entry.local_id(),
34 ClientContext(USER_INITIATED),
35 google_apis::test_util::CreateCopyResultCallback(&error));
36 test_util::RunBlockingPoolTask();
37 EXPECT_EQ(FILE_ERROR_OK, error);
39 // Remove a file in subdirectory.
40 error = FILE_ERROR_FAILED;
41 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_subdir, &entry));
42 const std::string resource_id = entry.resource_id();
44 performer.Remove(entry.local_id(),
45 ClientContext(USER_INITIATED),
46 google_apis::test_util::CreateCopyResultCallback(&error));
47 test_util::RunBlockingPoolTask();
48 EXPECT_EQ(FILE_ERROR_OK, error);
50 // Verify the file is indeed removed in the server.
51 google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR;
52 scoped_ptr<google_apis::FileResource> gdata_entry;
53 fake_service()->GetFileResource(
55 google_apis::test_util::CreateCopyResultCallback(&gdata_error,
57 test_util::RunBlockingPoolTask();
58 ASSERT_EQ(google_apis::HTTP_SUCCESS, gdata_error);
59 EXPECT_TRUE(gdata_entry->labels().is_trashed());
61 // Try removing non-existing file.
62 error = FILE_ERROR_FAILED;
63 performer.Remove("non-existing-id",
64 ClientContext(USER_INITIATED),
65 google_apis::test_util::CreateCopyResultCallback(&error));
66 test_util::RunBlockingPoolTask();
67 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
70 TEST_F(RemovePerformerTest, RemoveShared) {
71 RemovePerformer performer(blocking_task_runner(), observer(), scheduler(),
74 const base::FilePath kPathInMyDrive(FILE_PATH_LITERAL(
75 "drive/root/shared.txt"));
76 const base::FilePath kPathInOther(FILE_PATH_LITERAL(
77 "drive/other/shared.txt"));
79 // Prepare a shared file to the root folder.
80 google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR;
81 scoped_ptr<google_apis::FileResource> gdata_entry;
82 fake_service()->AddNewFile(
85 fake_service()->GetRootResourceId(),
86 kPathInMyDrive.BaseName().AsUTF8Unsafe(),
87 true, // shared_with_me,
88 google_apis::test_util::CreateCopyResultCallback(&gdata_error,
90 test_util::RunBlockingPoolTask();
91 ASSERT_EQ(google_apis::HTTP_CREATED, gdata_error);
94 // Remove it. Locally, the file should be moved to drive/other.
96 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPathInMyDrive, &entry));
97 FileError error = FILE_ERROR_FAILED;
98 performer.Remove(entry.local_id(),
99 ClientContext(USER_INITIATED),
100 google_apis::test_util::CreateCopyResultCallback(&error));
101 test_util::RunBlockingPoolTask();
102 EXPECT_EQ(FILE_ERROR_OK, error);
103 EXPECT_EQ(FILE_ERROR_NOT_FOUND,
104 GetLocalResourceEntry(kPathInMyDrive, &entry));
105 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPathInOther, &entry));
107 // Remotely, the entry should have lost its parent.
108 gdata_error = google_apis::GDATA_OTHER_ERROR;
109 const std::string resource_id = gdata_entry->file_id();
110 fake_service()->GetFileResource(
112 google_apis::test_util::CreateCopyResultCallback(&gdata_error,
114 test_util::RunBlockingPoolTask();
115 ASSERT_EQ(google_apis::HTTP_SUCCESS, gdata_error);
116 EXPECT_FALSE(gdata_entry->labels().is_trashed()); // It's not deleted.
117 EXPECT_TRUE(gdata_entry->parents().empty());
120 TEST_F(RemovePerformerTest, RemoveLocallyCreatedFile) {
121 RemovePerformer performer(blocking_task_runner(), observer(), scheduler(),
124 // Add an entry without resource ID.
126 entry.set_title("New File.txt");
127 entry.set_parent_local_id(util::kDriveTrashDirLocalId);
129 FileError error = FILE_ERROR_FAILED;
130 std::string local_id;
131 base::PostTaskAndReplyWithResult(
132 blocking_task_runner(),
134 base::Bind(&ResourceMetadata::AddEntry,
135 base::Unretained(metadata()),
138 google_apis::test_util::CreateCopyResultCallback(&error));
139 test_util::RunBlockingPoolTask();
140 EXPECT_EQ(FILE_ERROR_OK, error);
143 performer.Remove(local_id, ClientContext(USER_INITIATED),
144 google_apis::test_util::CreateCopyResultCallback(&error));
145 test_util::RunBlockingPoolTask();
146 EXPECT_EQ(FILE_ERROR_OK, error);
147 EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntryById(local_id, &entry));
150 } // namespace internal