BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / components / drive / file_system / open_file_operation_unittest.cc
blob2e6f674a68b615dde8a6342ed4eedd13ef03770b
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 "components/drive/file_system/open_file_operation.h"
7 #include <map>
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/task_runner_util.h"
13 #include "components/drive/drive.pb.h"
14 #include "components/drive/file_cache.h"
15 #include "components/drive/file_change.h"
16 #include "components/drive/file_errors.h"
17 #include "components/drive/file_system/operation_test_base.h"
18 #include "content/public/test/test_utils.h"
19 #include "google_apis/drive/test_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace drive {
23 namespace file_system {
25 class OpenFileOperationTest : public OperationTestBase {
26 protected:
27 void SetUp() override {
28 OperationTestBase::SetUp();
30 operation_.reset(new OpenFileOperation(
31 blocking_task_runner(), delegate(), scheduler(), metadata(), cache(),
32 temp_dir()));
35 scoped_ptr<OpenFileOperation> operation_;
38 TEST_F(OpenFileOperationTest, OpenExistingFile) {
39 const base::FilePath file_in_root(
40 FILE_PATH_LITERAL("drive/root/File 1.txt"));
41 ResourceEntry src_entry;
42 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
43 const int64 file_size = src_entry.file_info().size();
45 FileError error = FILE_ERROR_FAILED;
46 base::FilePath file_path;
47 base::Closure close_callback;
48 operation_->OpenFile(
49 file_in_root,
50 OPEN_FILE,
51 std::string(), // mime_type
52 google_apis::test_util::CreateCopyResultCallback(
53 &error, &file_path, &close_callback));
54 content::RunAllBlockingPoolTasksUntilIdle();
56 EXPECT_EQ(FILE_ERROR_OK, error);
57 ASSERT_TRUE(base::PathExists(file_path));
58 int64 local_file_size;
59 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size));
60 EXPECT_EQ(file_size, local_file_size);
62 ASSERT_FALSE(close_callback.is_null());
63 close_callback.Run();
64 EXPECT_EQ(1U, delegate()->updated_local_ids().count(src_entry.local_id()));
67 TEST_F(OpenFileOperationTest, OpenNonExistingFile) {
68 const base::FilePath file_in_root(
69 FILE_PATH_LITERAL("drive/root/not-exist.txt"));
71 FileError error = FILE_ERROR_FAILED;
72 base::FilePath file_path;
73 base::Closure close_callback;
74 operation_->OpenFile(
75 file_in_root,
76 OPEN_FILE,
77 std::string(), // mime_type
78 google_apis::test_util::CreateCopyResultCallback(
79 &error, &file_path, &close_callback));
80 content::RunAllBlockingPoolTasksUntilIdle();
81 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
82 EXPECT_TRUE(close_callback.is_null());
85 TEST_F(OpenFileOperationTest, CreateExistingFile) {
86 const base::FilePath file_in_root(
87 FILE_PATH_LITERAL("drive/root/File 1.txt"));
88 ResourceEntry src_entry;
89 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
91 FileError error = FILE_ERROR_FAILED;
92 base::FilePath file_path;
93 base::Closure close_callback;
94 operation_->OpenFile(
95 file_in_root,
96 CREATE_FILE,
97 std::string(), // mime_type
98 google_apis::test_util::CreateCopyResultCallback(
99 &error, &file_path, &close_callback));
100 content::RunAllBlockingPoolTasksUntilIdle();
102 EXPECT_EQ(FILE_ERROR_EXISTS, error);
103 EXPECT_TRUE(close_callback.is_null());
106 TEST_F(OpenFileOperationTest, CreateNonExistingFile) {
107 const base::FilePath file_in_root(
108 FILE_PATH_LITERAL("drive/root/not-exist.txt"));
110 FileError error = FILE_ERROR_FAILED;
111 base::FilePath file_path;
112 base::Closure close_callback;
113 operation_->OpenFile(
114 file_in_root,
115 CREATE_FILE,
116 std::string(), // mime_type
117 google_apis::test_util::CreateCopyResultCallback(
118 &error, &file_path, &close_callback));
119 content::RunAllBlockingPoolTasksUntilIdle();
121 EXPECT_EQ(1U, delegate()->get_changed_files().size());
122 EXPECT_TRUE(delegate()->get_changed_files().count(file_in_root));
124 EXPECT_EQ(FILE_ERROR_OK, error);
125 ASSERT_TRUE(base::PathExists(file_path));
126 int64 local_file_size;
127 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size));
128 EXPECT_EQ(0, local_file_size); // Should be an empty file.
130 ASSERT_FALSE(close_callback.is_null());
131 close_callback.Run();
132 EXPECT_EQ(1U,
133 delegate()->updated_local_ids().count(GetLocalId(file_in_root)));
136 TEST_F(OpenFileOperationTest, OpenOrCreateExistingFile) {
137 const base::FilePath file_in_root(
138 FILE_PATH_LITERAL("drive/root/File 1.txt"));
139 ResourceEntry src_entry;
140 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
141 const int64 file_size = src_entry.file_info().size();
143 FileError error = FILE_ERROR_FAILED;
144 base::FilePath file_path;
145 base::Closure close_callback;
146 operation_->OpenFile(
147 file_in_root,
148 OPEN_OR_CREATE_FILE,
149 std::string(), // mime_type
150 google_apis::test_util::CreateCopyResultCallback(
151 &error, &file_path, &close_callback));
152 content::RunAllBlockingPoolTasksUntilIdle();
154 // Notified because 'available offline' status of the existing file changes.
155 EXPECT_EQ(1U, delegate()->get_changed_files().size());
156 EXPECT_TRUE(delegate()->get_changed_files().count(file_in_root));
158 EXPECT_EQ(FILE_ERROR_OK, error);
159 ASSERT_TRUE(base::PathExists(file_path));
160 int64 local_file_size;
161 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size));
162 EXPECT_EQ(file_size, local_file_size);
164 ASSERT_FALSE(close_callback.is_null());
165 close_callback.Run();
166 EXPECT_EQ(1U, delegate()->updated_local_ids().count(src_entry.local_id()));
168 ResourceEntry result_entry;
169 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &result_entry));
170 EXPECT_TRUE(result_entry.file_specific_info().cache_state().is_present());
171 EXPECT_TRUE(result_entry.file_specific_info().cache_state().is_dirty());
174 TEST_F(OpenFileOperationTest, OpenOrCreateNonExistingFile) {
175 const base::FilePath file_in_root(
176 FILE_PATH_LITERAL("drive/root/not-exist.txt"));
178 FileError error = FILE_ERROR_FAILED;
179 base::FilePath file_path;
180 base::Closure close_callback;
181 operation_->OpenFile(
182 file_in_root,
183 OPEN_OR_CREATE_FILE,
184 std::string(), // mime_type
185 google_apis::test_util::CreateCopyResultCallback(
186 &error, &file_path, &close_callback));
187 content::RunAllBlockingPoolTasksUntilIdle();
189 EXPECT_EQ(FILE_ERROR_OK, error);
190 ASSERT_TRUE(base::PathExists(file_path));
191 int64 local_file_size;
192 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size));
193 EXPECT_EQ(0, local_file_size); // Should be an empty file.
195 ASSERT_FALSE(close_callback.is_null());
196 close_callback.Run();
197 EXPECT_EQ(1U,
198 delegate()->updated_local_ids().count(GetLocalId(file_in_root)));
201 TEST_F(OpenFileOperationTest, OpenFileTwice) {
202 const base::FilePath file_in_root(
203 FILE_PATH_LITERAL("drive/root/File 1.txt"));
204 ResourceEntry src_entry;
205 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
206 const int64 file_size = src_entry.file_info().size();
208 FileError error = FILE_ERROR_FAILED;
209 base::FilePath file_path;
210 base::Closure close_callback;
211 operation_->OpenFile(
212 file_in_root,
213 OPEN_FILE,
214 std::string(), // mime_type
215 google_apis::test_util::CreateCopyResultCallback(
216 &error, &file_path, &close_callback));
217 content::RunAllBlockingPoolTasksUntilIdle();
219 EXPECT_EQ(FILE_ERROR_OK, error);
220 ASSERT_TRUE(base::PathExists(file_path));
221 int64 local_file_size;
222 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size));
223 EXPECT_EQ(file_size, local_file_size);
225 // Open again.
226 error = FILE_ERROR_FAILED;
227 base::Closure close_callback2;
228 operation_->OpenFile(
229 file_in_root,
230 OPEN_FILE,
231 std::string(), // mime_type
232 google_apis::test_util::CreateCopyResultCallback(
233 &error, &file_path, &close_callback2));
234 content::RunAllBlockingPoolTasksUntilIdle();
236 EXPECT_EQ(FILE_ERROR_OK, error);
237 ASSERT_TRUE(base::PathExists(file_path));
238 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size));
239 EXPECT_EQ(file_size, local_file_size);
241 ASSERT_FALSE(close_callback.is_null());
242 ASSERT_FALSE(close_callback2.is_null());
244 close_callback.Run();
246 // There still remains a client opening the file, so it shouldn't be
247 // uploaded yet.
248 EXPECT_TRUE(delegate()->updated_local_ids().empty());
250 close_callback2.Run();
252 // Here, all the clients close the file, so it should be uploaded then.
253 EXPECT_EQ(1U, delegate()->updated_local_ids().count(src_entry.local_id()));
256 } // namespace file_system
257 } // namespace drive