Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / sync / remove_performer_unittest.cc
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/sync/remove_performer.h"
6
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"
14
15 namespace drive {
16 namespace internal {
17
18 typedef file_system::OperationTestBase RemovePerformerTest;
19
20 TEST_F(RemovePerformerTest, RemoveFile) {
21   RemovePerformer performer(blocking_task_runner(), observer(), scheduler(),
22                             metadata());
23
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"));
27
28   // Remove a file in root.
29   ResourceEntry entry;
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);
37
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();
42
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);
48
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(
53       resource_id,
54       google_apis::test_util::CreateCopyResultCallback(&gdata_error,
55                                                        &gdata_entry));
56   test_util::RunBlockingPoolTask();
57   ASSERT_EQ(google_apis::HTTP_SUCCESS, gdata_error);
58   EXPECT_TRUE(gdata_entry->deleted());
59
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);
67 }
68
69 TEST_F(RemovePerformerTest, RemoveShared) {
70   RemovePerformer performer(blocking_task_runner(), observer(), scheduler(),
71                             metadata());
72
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"));
77
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(
82       "text/plain",
83       "dummy content",
84       fake_service()->GetRootResourceId(),
85       kPathInMyDrive.BaseName().AsUTF8Unsafe(),
86       true,  // shared_with_me,
87       google_apis::test_util::CreateCopyResultCallback(&gdata_error,
88                                                        &gdata_entry));
89   test_util::RunBlockingPoolTask();
90   ASSERT_EQ(google_apis::HTTP_CREATED, gdata_error);
91   CheckForUpdates();
92
93   // Remove it. Locally, the file should be moved to drive/other.
94   ResourceEntry entry;
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));
105
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(
110       resource_id,
111       google_apis::test_util::CreateCopyResultCallback(&gdata_error,
112                                                        &gdata_entry));
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));
117 }
118
119 TEST_F(RemovePerformerTest, RemoveLocallyCreatedFile) {
120   RemovePerformer performer(blocking_task_runner(), observer(), scheduler(),
121                             metadata());
122
123   // Add an entry without resource ID.
124   ResourceEntry entry;
125   entry.set_title("New File.txt");
126   entry.set_parent_local_id(util::kDriveTrashDirLocalId);
127
128   FileError error = FILE_ERROR_FAILED;
129   std::string local_id;
130   base::PostTaskAndReplyWithResult(
131       blocking_task_runner(),
132       FROM_HERE,
133       base::Bind(&ResourceMetadata::AddEntry,
134                  base::Unretained(metadata()),
135                  entry,
136                  &local_id),
137       google_apis::test_util::CreateCopyResultCallback(&error));
138   test_util::RunBlockingPoolTask();
139   EXPECT_EQ(FILE_ERROR_OK, error);
140
141   // Remove the entry.
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));
147 }
148
149 }  // namespace internal
150 }  // namespace drive