OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/drive/file_system/open_file_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/open_file_operation.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/task_runner_util.h" | 12 #include "base/task_runner_util.h" |
13 #include "chrome/browser/chromeos/drive/drive.pb.h" | 13 #include "chrome/browser/chromeos/drive/drive.pb.h" |
14 #include "chrome/browser/chromeos/drive/file_cache.h" | 14 #include "chrome/browser/chromeos/drive/file_cache.h" |
15 #include "chrome/browser/chromeos/drive/file_change.h" | 15 #include "chrome/browser/chromeos/drive/file_change.h" |
16 #include "chrome/browser/chromeos/drive/file_errors.h" | 16 #include "chrome/browser/chromeos/drive/file_errors.h" |
17 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" | 17 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" |
| 18 #include "content/public/test/test_utils.h" |
18 #include "google_apis/drive/test_util.h" | 19 #include "google_apis/drive/test_util.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 namespace drive { | 22 namespace drive { |
22 namespace file_system { | 23 namespace file_system { |
23 | 24 |
24 class OpenFileOperationTest : public OperationTestBase { | 25 class OpenFileOperationTest : public OperationTestBase { |
25 protected: | 26 protected: |
26 virtual void SetUp() OVERRIDE { | 27 virtual void SetUp() OVERRIDE { |
27 OperationTestBase::SetUp(); | 28 OperationTestBase::SetUp(); |
(...skipping 15 matching lines...) Expand all Loading... |
43 | 44 |
44 FileError error = FILE_ERROR_FAILED; | 45 FileError error = FILE_ERROR_FAILED; |
45 base::FilePath file_path; | 46 base::FilePath file_path; |
46 base::Closure close_callback; | 47 base::Closure close_callback; |
47 operation_->OpenFile( | 48 operation_->OpenFile( |
48 file_in_root, | 49 file_in_root, |
49 OPEN_FILE, | 50 OPEN_FILE, |
50 std::string(), // mime_type | 51 std::string(), // mime_type |
51 google_apis::test_util::CreateCopyResultCallback( | 52 google_apis::test_util::CreateCopyResultCallback( |
52 &error, &file_path, &close_callback)); | 53 &error, &file_path, &close_callback)); |
53 test_util::RunBlockingPoolTask(); | 54 content::RunAllBlockingPoolTasksUntilIdle(); |
54 | 55 |
55 EXPECT_EQ(FILE_ERROR_OK, error); | 56 EXPECT_EQ(FILE_ERROR_OK, error); |
56 ASSERT_TRUE(base::PathExists(file_path)); | 57 ASSERT_TRUE(base::PathExists(file_path)); |
57 int64 local_file_size; | 58 int64 local_file_size; |
58 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size)); | 59 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size)); |
59 EXPECT_EQ(file_size, local_file_size); | 60 EXPECT_EQ(file_size, local_file_size); |
60 | 61 |
61 ASSERT_FALSE(close_callback.is_null()); | 62 ASSERT_FALSE(close_callback.is_null()); |
62 close_callback.Run(); | 63 close_callback.Run(); |
63 EXPECT_EQ(1U, observer()->updated_local_ids().count(src_entry.local_id())); | 64 EXPECT_EQ(1U, observer()->updated_local_ids().count(src_entry.local_id())); |
64 } | 65 } |
65 | 66 |
66 TEST_F(OpenFileOperationTest, OpenNonExistingFile) { | 67 TEST_F(OpenFileOperationTest, OpenNonExistingFile) { |
67 const base::FilePath file_in_root( | 68 const base::FilePath file_in_root( |
68 FILE_PATH_LITERAL("drive/root/not-exist.txt")); | 69 FILE_PATH_LITERAL("drive/root/not-exist.txt")); |
69 | 70 |
70 FileError error = FILE_ERROR_FAILED; | 71 FileError error = FILE_ERROR_FAILED; |
71 base::FilePath file_path; | 72 base::FilePath file_path; |
72 base::Closure close_callback; | 73 base::Closure close_callback; |
73 operation_->OpenFile( | 74 operation_->OpenFile( |
74 file_in_root, | 75 file_in_root, |
75 OPEN_FILE, | 76 OPEN_FILE, |
76 std::string(), // mime_type | 77 std::string(), // mime_type |
77 google_apis::test_util::CreateCopyResultCallback( | 78 google_apis::test_util::CreateCopyResultCallback( |
78 &error, &file_path, &close_callback)); | 79 &error, &file_path, &close_callback)); |
79 test_util::RunBlockingPoolTask(); | 80 content::RunAllBlockingPoolTasksUntilIdle(); |
80 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); | 81 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); |
81 EXPECT_TRUE(close_callback.is_null()); | 82 EXPECT_TRUE(close_callback.is_null()); |
82 } | 83 } |
83 | 84 |
84 TEST_F(OpenFileOperationTest, CreateExistingFile) { | 85 TEST_F(OpenFileOperationTest, CreateExistingFile) { |
85 const base::FilePath file_in_root( | 86 const base::FilePath file_in_root( |
86 FILE_PATH_LITERAL("drive/root/File 1.txt")); | 87 FILE_PATH_LITERAL("drive/root/File 1.txt")); |
87 ResourceEntry src_entry; | 88 ResourceEntry src_entry; |
88 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | 89 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
89 | 90 |
90 FileError error = FILE_ERROR_FAILED; | 91 FileError error = FILE_ERROR_FAILED; |
91 base::FilePath file_path; | 92 base::FilePath file_path; |
92 base::Closure close_callback; | 93 base::Closure close_callback; |
93 operation_->OpenFile( | 94 operation_->OpenFile( |
94 file_in_root, | 95 file_in_root, |
95 CREATE_FILE, | 96 CREATE_FILE, |
96 std::string(), // mime_type | 97 std::string(), // mime_type |
97 google_apis::test_util::CreateCopyResultCallback( | 98 google_apis::test_util::CreateCopyResultCallback( |
98 &error, &file_path, &close_callback)); | 99 &error, &file_path, &close_callback)); |
99 test_util::RunBlockingPoolTask(); | 100 content::RunAllBlockingPoolTasksUntilIdle(); |
100 | 101 |
101 EXPECT_EQ(FILE_ERROR_EXISTS, error); | 102 EXPECT_EQ(FILE_ERROR_EXISTS, error); |
102 EXPECT_TRUE(close_callback.is_null()); | 103 EXPECT_TRUE(close_callback.is_null()); |
103 } | 104 } |
104 | 105 |
105 TEST_F(OpenFileOperationTest, CreateNonExistingFile) { | 106 TEST_F(OpenFileOperationTest, CreateNonExistingFile) { |
106 const base::FilePath file_in_root( | 107 const base::FilePath file_in_root( |
107 FILE_PATH_LITERAL("drive/root/not-exist.txt")); | 108 FILE_PATH_LITERAL("drive/root/not-exist.txt")); |
108 | 109 |
109 FileError error = FILE_ERROR_FAILED; | 110 FileError error = FILE_ERROR_FAILED; |
110 base::FilePath file_path; | 111 base::FilePath file_path; |
111 base::Closure close_callback; | 112 base::Closure close_callback; |
112 operation_->OpenFile( | 113 operation_->OpenFile( |
113 file_in_root, | 114 file_in_root, |
114 CREATE_FILE, | 115 CREATE_FILE, |
115 std::string(), // mime_type | 116 std::string(), // mime_type |
116 google_apis::test_util::CreateCopyResultCallback( | 117 google_apis::test_util::CreateCopyResultCallback( |
117 &error, &file_path, &close_callback)); | 118 &error, &file_path, &close_callback)); |
118 test_util::RunBlockingPoolTask(); | 119 content::RunAllBlockingPoolTasksUntilIdle(); |
119 | 120 |
120 EXPECT_EQ(1U, observer()->get_changed_files().size()); | 121 EXPECT_EQ(1U, observer()->get_changed_files().size()); |
121 EXPECT_TRUE(observer()->get_changed_files().count(file_in_root)); | 122 EXPECT_TRUE(observer()->get_changed_files().count(file_in_root)); |
122 | 123 |
123 EXPECT_EQ(FILE_ERROR_OK, error); | 124 EXPECT_EQ(FILE_ERROR_OK, error); |
124 ASSERT_TRUE(base::PathExists(file_path)); | 125 ASSERT_TRUE(base::PathExists(file_path)); |
125 int64 local_file_size; | 126 int64 local_file_size; |
126 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size)); | 127 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size)); |
127 EXPECT_EQ(0, local_file_size); // Should be an empty file. | 128 EXPECT_EQ(0, local_file_size); // Should be an empty file. |
128 | 129 |
(...skipping 12 matching lines...) Expand all Loading... |
141 | 142 |
142 FileError error = FILE_ERROR_FAILED; | 143 FileError error = FILE_ERROR_FAILED; |
143 base::FilePath file_path; | 144 base::FilePath file_path; |
144 base::Closure close_callback; | 145 base::Closure close_callback; |
145 operation_->OpenFile( | 146 operation_->OpenFile( |
146 file_in_root, | 147 file_in_root, |
147 OPEN_OR_CREATE_FILE, | 148 OPEN_OR_CREATE_FILE, |
148 std::string(), // mime_type | 149 std::string(), // mime_type |
149 google_apis::test_util::CreateCopyResultCallback( | 150 google_apis::test_util::CreateCopyResultCallback( |
150 &error, &file_path, &close_callback)); | 151 &error, &file_path, &close_callback)); |
151 test_util::RunBlockingPoolTask(); | 152 content::RunAllBlockingPoolTasksUntilIdle(); |
152 | 153 |
153 // Notified because 'available offline' status of the existing file changes. | 154 // Notified because 'available offline' status of the existing file changes. |
154 EXPECT_EQ(1U, observer()->get_changed_files().size()); | 155 EXPECT_EQ(1U, observer()->get_changed_files().size()); |
155 EXPECT_TRUE(observer()->get_changed_files().count(file_in_root)); | 156 EXPECT_TRUE(observer()->get_changed_files().count(file_in_root)); |
156 | 157 |
157 EXPECT_EQ(FILE_ERROR_OK, error); | 158 EXPECT_EQ(FILE_ERROR_OK, error); |
158 ASSERT_TRUE(base::PathExists(file_path)); | 159 ASSERT_TRUE(base::PathExists(file_path)); |
159 int64 local_file_size; | 160 int64 local_file_size; |
160 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size)); | 161 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size)); |
161 EXPECT_EQ(file_size, local_file_size); | 162 EXPECT_EQ(file_size, local_file_size); |
(...skipping 14 matching lines...) Expand all Loading... |
176 | 177 |
177 FileError error = FILE_ERROR_FAILED; | 178 FileError error = FILE_ERROR_FAILED; |
178 base::FilePath file_path; | 179 base::FilePath file_path; |
179 base::Closure close_callback; | 180 base::Closure close_callback; |
180 operation_->OpenFile( | 181 operation_->OpenFile( |
181 file_in_root, | 182 file_in_root, |
182 OPEN_OR_CREATE_FILE, | 183 OPEN_OR_CREATE_FILE, |
183 std::string(), // mime_type | 184 std::string(), // mime_type |
184 google_apis::test_util::CreateCopyResultCallback( | 185 google_apis::test_util::CreateCopyResultCallback( |
185 &error, &file_path, &close_callback)); | 186 &error, &file_path, &close_callback)); |
186 test_util::RunBlockingPoolTask(); | 187 content::RunAllBlockingPoolTasksUntilIdle(); |
187 | 188 |
188 EXPECT_EQ(FILE_ERROR_OK, error); | 189 EXPECT_EQ(FILE_ERROR_OK, error); |
189 ASSERT_TRUE(base::PathExists(file_path)); | 190 ASSERT_TRUE(base::PathExists(file_path)); |
190 int64 local_file_size; | 191 int64 local_file_size; |
191 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size)); | 192 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size)); |
192 EXPECT_EQ(0, local_file_size); // Should be an empty file. | 193 EXPECT_EQ(0, local_file_size); // Should be an empty file. |
193 | 194 |
194 ASSERT_FALSE(close_callback.is_null()); | 195 ASSERT_FALSE(close_callback.is_null()); |
195 close_callback.Run(); | 196 close_callback.Run(); |
196 EXPECT_EQ(1U, | 197 EXPECT_EQ(1U, |
197 observer()->updated_local_ids().count(GetLocalId(file_in_root))); | 198 observer()->updated_local_ids().count(GetLocalId(file_in_root))); |
198 } | 199 } |
199 | 200 |
200 TEST_F(OpenFileOperationTest, OpenFileTwice) { | 201 TEST_F(OpenFileOperationTest, OpenFileTwice) { |
201 const base::FilePath file_in_root( | 202 const base::FilePath file_in_root( |
202 FILE_PATH_LITERAL("drive/root/File 1.txt")); | 203 FILE_PATH_LITERAL("drive/root/File 1.txt")); |
203 ResourceEntry src_entry; | 204 ResourceEntry src_entry; |
204 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | 205 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
205 const int64 file_size = src_entry.file_info().size(); | 206 const int64 file_size = src_entry.file_info().size(); |
206 | 207 |
207 FileError error = FILE_ERROR_FAILED; | 208 FileError error = FILE_ERROR_FAILED; |
208 base::FilePath file_path; | 209 base::FilePath file_path; |
209 base::Closure close_callback; | 210 base::Closure close_callback; |
210 operation_->OpenFile( | 211 operation_->OpenFile( |
211 file_in_root, | 212 file_in_root, |
212 OPEN_FILE, | 213 OPEN_FILE, |
213 std::string(), // mime_type | 214 std::string(), // mime_type |
214 google_apis::test_util::CreateCopyResultCallback( | 215 google_apis::test_util::CreateCopyResultCallback( |
215 &error, &file_path, &close_callback)); | 216 &error, &file_path, &close_callback)); |
216 test_util::RunBlockingPoolTask(); | 217 content::RunAllBlockingPoolTasksUntilIdle(); |
217 | 218 |
218 EXPECT_EQ(FILE_ERROR_OK, error); | 219 EXPECT_EQ(FILE_ERROR_OK, error); |
219 ASSERT_TRUE(base::PathExists(file_path)); | 220 ASSERT_TRUE(base::PathExists(file_path)); |
220 int64 local_file_size; | 221 int64 local_file_size; |
221 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size)); | 222 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size)); |
222 EXPECT_EQ(file_size, local_file_size); | 223 EXPECT_EQ(file_size, local_file_size); |
223 | 224 |
224 // Open again. | 225 // Open again. |
225 error = FILE_ERROR_FAILED; | 226 error = FILE_ERROR_FAILED; |
226 base::Closure close_callback2; | 227 base::Closure close_callback2; |
227 operation_->OpenFile( | 228 operation_->OpenFile( |
228 file_in_root, | 229 file_in_root, |
229 OPEN_FILE, | 230 OPEN_FILE, |
230 std::string(), // mime_type | 231 std::string(), // mime_type |
231 google_apis::test_util::CreateCopyResultCallback( | 232 google_apis::test_util::CreateCopyResultCallback( |
232 &error, &file_path, &close_callback2)); | 233 &error, &file_path, &close_callback2)); |
233 test_util::RunBlockingPoolTask(); | 234 content::RunAllBlockingPoolTasksUntilIdle(); |
234 | 235 |
235 EXPECT_EQ(FILE_ERROR_OK, error); | 236 EXPECT_EQ(FILE_ERROR_OK, error); |
236 ASSERT_TRUE(base::PathExists(file_path)); | 237 ASSERT_TRUE(base::PathExists(file_path)); |
237 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size)); | 238 ASSERT_TRUE(base::GetFileSize(file_path, &local_file_size)); |
238 EXPECT_EQ(file_size, local_file_size); | 239 EXPECT_EQ(file_size, local_file_size); |
239 | 240 |
240 ASSERT_FALSE(close_callback.is_null()); | 241 ASSERT_FALSE(close_callback.is_null()); |
241 ASSERT_FALSE(close_callback2.is_null()); | 242 ASSERT_FALSE(close_callback2.is_null()); |
242 | 243 |
243 close_callback.Run(); | 244 close_callback.Run(); |
244 | 245 |
245 // There still remains a client opening the file, so it shouldn't be | 246 // There still remains a client opening the file, so it shouldn't be |
246 // uploaded yet. | 247 // uploaded yet. |
247 EXPECT_TRUE(observer()->updated_local_ids().empty()); | 248 EXPECT_TRUE(observer()->updated_local_ids().empty()); |
248 | 249 |
249 close_callback2.Run(); | 250 close_callback2.Run(); |
250 | 251 |
251 // Here, all the clients close the file, so it should be uploaded then. | 252 // Here, all the clients close the file, so it should be uploaded then. |
252 EXPECT_EQ(1U, observer()->updated_local_ids().count(src_entry.local_id())); | 253 EXPECT_EQ(1U, observer()->updated_local_ids().count(src_entry.local_id())); |
253 } | 254 } |
254 | 255 |
255 } // namespace file_system | 256 } // namespace file_system |
256 } // namespace drive | 257 } // namespace drive |
OLD | NEW |