OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "webkit/fileapi/file_system_operation.h" | 5 #include "webkit/fileapi/file_system_operation.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 | 82 |
83 fileapi::FileSystemOperation* operation() { | 83 fileapi::FileSystemOperation* operation() { |
84 request_id_ = ++last_request_id; | 84 request_id_ = ++last_request_id; |
85 mock_dispatcher_ = new MockDispatcher(request_id_); | 85 mock_dispatcher_ = new MockDispatcher(request_id_); |
86 operation_.reset(new fileapi::FileSystemOperation( | 86 operation_.reset(new fileapi::FileSystemOperation( |
87 mock_dispatcher_, base::MessageLoopProxy::CreateForCurrentThread())); | 87 mock_dispatcher_, base::MessageLoopProxy::CreateForCurrentThread())); |
88 return operation_.get(); | 88 return operation_.get(); |
89 } | 89 } |
90 | 90 |
91 protected: | 91 protected: |
92 // Common temp base for all non-existing file/dir path test cases. | 92 // Common temp base for nondestructive uses. |
93 // This is in case a dir on test machine exists. It's better to | |
94 // create temp and then create non-existing file paths under it. | |
95 ScopedTempDir base_; | 93 ScopedTempDir base_; |
96 | 94 |
97 int request_id_; | 95 int request_id_; |
98 scoped_ptr<fileapi::FileSystemOperation> operation_; | 96 scoped_ptr<fileapi::FileSystemOperation> operation_; |
99 | 97 |
100 // Owned by |operation_|. | 98 // Owned by |operation_|. |
101 MockDispatcher* mock_dispatcher_; | 99 MockDispatcher* mock_dispatcher_; |
102 | 100 |
103 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationTest); | 101 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationTest); |
104 }; | 102 }; |
105 | 103 |
106 TEST_F(FileSystemOperationTest, TestMoveFailureSrcDoesntExist) { | 104 TEST_F(FileSystemOperationTest, TestMoveFailureSrcDoesntExist) { |
107 FilePath src(base_.path().Append(FILE_PATH_LITERAL("a"))); | 105 FilePath src(base_.path().Append(FILE_PATH_LITERAL("a"))); |
108 FilePath dest(base_.path().Append(FILE_PATH_LITERAL("b"))); | 106 FilePath dest(base_.path().Append(FILE_PATH_LITERAL("b"))); |
109 operation()->Move(src, dest); | 107 operation()->Move(src, dest); |
110 MessageLoop::current()->RunAllPending(); | 108 MessageLoop::current()->RunAllPending(); |
111 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, mock_dispatcher_->status()); | 109 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, mock_dispatcher_->status()); |
112 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 110 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
113 } | 111 } |
114 | 112 |
115 // crbug.com/57940 | 113 TEST_F(FileSystemOperationTest, TestMoveFailureContainsPath) { |
116 #if defined(OS_WINDOWS) | 114 ScopedTempDir src_dir; |
117 #define MAYBE_TestMoveFailureContainsPath FAILS_TestMoveFailureContainsPath | 115 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
118 #else | 116 FilePath dest_dir_path; |
119 #define MAYBE_TestMoveFailureContainsPath TestMoveFailureContainsPath | 117 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(src_dir.path(), |
120 #endif | 118 FILE_PATH_LITERAL("child_dir"), |
121 | 119 &dest_dir_path)); |
122 TEST_F(FileSystemOperationTest, MAYBE_TestMoveFailureContainsPath) { | 120 operation()->Move(src_dir.path(), dest_dir_path); |
123 ScopedTempDir dir_under_base; | |
124 dir_under_base.CreateUniqueTempDirUnderPath(base_.path()); | |
125 operation()->Move(base_.path(), dir_under_base.path()); | |
126 MessageLoop::current()->RunAllPending(); | 121 MessageLoop::current()->RunAllPending(); |
127 EXPECT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, | 122 EXPECT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, |
128 mock_dispatcher_->status()); | 123 mock_dispatcher_->status()); |
129 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 124 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
130 } | 125 } |
131 | 126 |
132 TEST_F(FileSystemOperationTest, TestMoveFailureSrcDirExistsDestFile) { | 127 TEST_F(FileSystemOperationTest, TestMoveFailureSrcDirExistsDestFile) { |
133 // Src exists and is dir. Dest is a file. | 128 // Src exists and is dir. Dest is a file. |
| 129 ScopedTempDir src_dir; |
| 130 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
| 131 |
134 ScopedTempDir dest_dir; | 132 ScopedTempDir dest_dir; |
135 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); | 133 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); |
136 FilePath dest_file; | 134 FilePath dest_file; |
137 file_util::CreateTemporaryFileInDir(dest_dir.path(), &dest_file); | 135 file_util::CreateTemporaryFileInDir(dest_dir.path(), &dest_file); |
138 | 136 |
139 operation()->Move(base_.path(), dest_file); | 137 operation()->Move(src_dir.path(), dest_file); |
140 MessageLoop::current()->RunAllPending(); | 138 MessageLoop::current()->RunAllPending(); |
141 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, mock_dispatcher_->status()); | 139 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY, |
| 140 mock_dispatcher_->status()); |
142 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 141 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
143 } | 142 } |
144 | 143 |
| 144 TEST_F(FileSystemOperationTest, TestMoveFailureSrcFileExistsDestNonEmptyDir) { |
| 145 // Src exists and is a directory. Dest is a non-empty directory. |
| 146 ScopedTempDir src_dir; |
| 147 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
| 148 |
| 149 ScopedTempDir dest_dir; |
| 150 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); |
| 151 FilePath child_file; |
| 152 file_util::CreateTemporaryFileInDir(dest_dir.path(), &child_file); |
| 153 |
| 154 operation()->Move(src_dir.path(), dest_dir.path()); |
| 155 MessageLoop::current()->RunAllPending(); |
| 156 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, mock_dispatcher_->status()); |
| 157 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
| 158 } |
| 159 |
| 160 TEST_F(FileSystemOperationTest, TestMoveFailureSrcFileExistsDestDir) { |
| 161 // Src exists and is a file. Dest is a directory. |
| 162 ScopedTempDir src_dir; |
| 163 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
| 164 FilePath src_file; |
| 165 file_util::CreateTemporaryFileInDir(src_dir.path(), &src_file); |
| 166 |
| 167 ScopedTempDir dest_dir; |
| 168 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); |
| 169 |
| 170 operation()->Move(src_file, dest_dir.path()); |
| 171 MessageLoop::current()->RunAllPending(); |
| 172 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE, |
| 173 mock_dispatcher_->status()); |
| 174 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
| 175 } |
| 176 |
145 TEST_F(FileSystemOperationTest, TestMoveFailureDestParentDoesntExist) { | 177 TEST_F(FileSystemOperationTest, TestMoveFailureDestParentDoesntExist) { |
146 // Dest. parent path does not exist. | 178 // Dest. parent path does not exist. |
147 ScopedTempDir src_dir; | 179 ScopedTempDir src_dir; |
148 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); | 180 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
149 FilePath nonexisting_file = base_.path().Append( | 181 FilePath nonexisting_file = base_.path().Append( |
150 FILE_PATH_LITERAL("NonexistingDir")).Append( | 182 FILE_PATH_LITERAL("NonexistingDir")).Append( |
151 FILE_PATH_LITERAL("NonexistingFile"));; | 183 FILE_PATH_LITERAL("NonexistingFile")); |
152 | 184 |
153 operation()->Move(src_dir.path(), nonexisting_file); | 185 operation()->Move(src_dir.path(), nonexisting_file); |
154 MessageLoop::current()->RunAllPending(); | 186 MessageLoop::current()->RunAllPending(); |
155 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, mock_dispatcher_->status()); | 187 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, mock_dispatcher_->status()); |
156 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 188 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
157 } | 189 } |
158 | 190 |
159 TEST_F(FileSystemOperationTest, TestMoveSuccessSrcFileAndOverwrite) { | 191 TEST_F(FileSystemOperationTest, TestMoveSuccessSrcFileAndOverwrite) { |
160 ScopedTempDir src_dir; | 192 ScopedTempDir src_dir; |
161 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); | 193 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
(...skipping 22 matching lines...) Expand all Loading... |
184 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); | 216 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); |
185 FilePath dest_file(dest_dir.path().Append(FILE_PATH_LITERAL("NewFile"))); | 217 FilePath dest_file(dest_dir.path().Append(FILE_PATH_LITERAL("NewFile"))); |
186 | 218 |
187 operation()->Move(src_file, dest_file); | 219 operation()->Move(src_file, dest_file); |
188 MessageLoop::current()->RunAllPending(); | 220 MessageLoop::current()->RunAllPending(); |
189 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); | 221 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
190 EXPECT_TRUE(FileExists(dest_file)); | 222 EXPECT_TRUE(FileExists(dest_file)); |
191 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 223 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
192 } | 224 } |
193 | 225 |
| 226 TEST_F(FileSystemOperationTest, TestMoveSuccessSrcDirAndOverwrite) { |
| 227 ScopedTempDir src_dir; |
| 228 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
| 229 |
| 230 ScopedTempDir dest_dir; |
| 231 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); |
| 232 |
| 233 operation()->Move(src_dir.path(), dest_dir.path()); |
| 234 MessageLoop::current()->RunAllPending(); |
| 235 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
| 236 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
| 237 EXPECT_FALSE(file_util::DirectoryExists(src_dir.path())); |
| 238 |
| 239 // Make sure we've overwritten but not moved the source under the |dest_dir|. |
| 240 EXPECT_TRUE(file_util::DirectoryExists(dest_dir.path())); |
| 241 EXPECT_FALSE(file_util::DirectoryExists( |
| 242 dest_dir.path().Append(src_dir.path().BaseName()))); |
| 243 } |
| 244 |
| 245 TEST_F(FileSystemOperationTest, TestMoveSuccessSrcDirAndNew) { |
| 246 ScopedTempDir src_dir; |
| 247 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
| 248 |
| 249 ScopedTempDir dir; |
| 250 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 251 FilePath dest_dir_path(dir.path().Append(FILE_PATH_LITERAL("NewDirectory"))); |
| 252 |
| 253 operation()->Move(src_dir.path(), dest_dir_path); |
| 254 MessageLoop::current()->RunAllPending(); |
| 255 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
| 256 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
| 257 EXPECT_FALSE(file_util::DirectoryExists(src_dir.path())); |
| 258 EXPECT_TRUE(file_util::DirectoryExists(dest_dir_path)); |
| 259 } |
| 260 |
| 261 TEST_F(FileSystemOperationTest, TestMoveSuccessSrcDirRecursive) { |
| 262 ScopedTempDir src_dir; |
| 263 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
| 264 FilePath child_file; |
| 265 file_util::CreateTemporaryFileInDir(src_dir.path(), &child_file); |
| 266 |
| 267 ScopedTempDir dest_dir; |
| 268 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); |
| 269 |
| 270 operation()->Move(src_dir.path(), dest_dir.path()); |
| 271 MessageLoop::current()->RunAllPending(); |
| 272 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
| 273 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
| 274 EXPECT_TRUE(FileExists(dest_dir.path().Append(child_file.BaseName()))); |
| 275 } |
| 276 |
194 TEST_F(FileSystemOperationTest, TestCopyFailureSrcDoesntExist) { | 277 TEST_F(FileSystemOperationTest, TestCopyFailureSrcDoesntExist) { |
195 FilePath src(base_.path().Append(FILE_PATH_LITERAL("a"))); | 278 FilePath src(base_.path().Append(FILE_PATH_LITERAL("a"))); |
196 FilePath dest(base_.path().Append(FILE_PATH_LITERAL("b"))); | 279 FilePath dest(base_.path().Append(FILE_PATH_LITERAL("b"))); |
197 operation()->Copy(src, dest); | 280 operation()->Copy(src, dest); |
198 MessageLoop::current()->RunAllPending(); | 281 MessageLoop::current()->RunAllPending(); |
199 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, mock_dispatcher_->status()); | 282 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, mock_dispatcher_->status()); |
200 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 283 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
201 } | 284 } |
202 | 285 |
203 // crbug.com/57940 | 286 TEST_F(FileSystemOperationTest, TestCopyFailureContainsPath) { |
204 #if defined(OS_WINDOWS) | 287 ScopedTempDir src_dir; |
205 #define MAYBE_TestCopyFailureContainsPath FAILS_TestCopyFailureContainsPath | 288 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
206 #else | 289 FilePath dest_dir_path; |
207 #define MAYBE_TestCopyFailureContainsPath TestCopyFailureContainsPath | 290 ASSERT_TRUE(file_util::CreateTemporaryDirInDir(src_dir.path(), |
208 #endif | 291 FILE_PATH_LITERAL("child_dir"), |
209 | 292 &dest_dir_path)); |
210 TEST_F(FileSystemOperationTest, MAYBE_TestCopyFailureContainsPath) { | 293 operation()->Copy(src_dir.path(), dest_dir_path); |
211 FilePath file_under_base = base_.path().Append(FILE_PATH_LITERAL("b")); | |
212 operation()->Copy(base_.path(), file_under_base); | |
213 MessageLoop::current()->RunAllPending(); | 294 MessageLoop::current()->RunAllPending(); |
214 EXPECT_EQ(base::PLATFORM_FILE_ERROR_FAILED, mock_dispatcher_->status()); | 295 EXPECT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, |
| 296 mock_dispatcher_->status()); |
215 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 297 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
216 } | 298 } |
217 | 299 |
218 TEST_F(FileSystemOperationTest, TestCopyFailureSrcDirExistsDestFile) { | 300 TEST_F(FileSystemOperationTest, TestCopyFailureSrcDirExistsDestFile) { |
219 // Src exists and is dir. Dest is a file. | 301 // Src exists and is dir. Dest is a file. |
220 ScopedTempDir dir; | 302 ScopedTempDir src_dir; |
221 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 303 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
| 304 |
| 305 ScopedTempDir dest_dir; |
| 306 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); |
222 FilePath dest_file; | 307 FilePath dest_file; |
223 file_util::CreateTemporaryFileInDir(dir.path(), &dest_file); | 308 file_util::CreateTemporaryFileInDir(dest_dir.path(), &dest_file); |
224 | 309 |
225 operation()->Copy(base_.path(), dest_file); | 310 operation()->Copy(src_dir.path(), dest_file); |
226 MessageLoop::current()->RunAllPending(); | 311 MessageLoop::current()->RunAllPending(); |
227 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY, | 312 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY, |
228 mock_dispatcher_->status()); | 313 mock_dispatcher_->status()); |
229 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 314 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
230 } | 315 } |
231 | 316 |
| 317 TEST_F(FileSystemOperationTest, TestCopyFailureSrcFileExistsDestNonEmptyDir) { |
| 318 // Src exists and is a directory. Dest is a non-empty directory. |
| 319 ScopedTempDir src_dir; |
| 320 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
| 321 |
| 322 ScopedTempDir dest_dir; |
| 323 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); |
| 324 FilePath child_file; |
| 325 file_util::CreateTemporaryFileInDir(dest_dir.path(), &child_file); |
| 326 |
| 327 operation()->Copy(src_dir.path(), dest_dir.path()); |
| 328 MessageLoop::current()->RunAllPending(); |
| 329 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, mock_dispatcher_->status()); |
| 330 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
| 331 } |
| 332 |
| 333 TEST_F(FileSystemOperationTest, TestCopyFailureSrcFileExistsDestDir) { |
| 334 // Src exists and is a file. Dest is a directory. |
| 335 ScopedTempDir src_dir; |
| 336 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
| 337 FilePath src_file; |
| 338 file_util::CreateTemporaryFileInDir(src_dir.path(), &src_file); |
| 339 |
| 340 ScopedTempDir dest_dir; |
| 341 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); |
| 342 |
| 343 operation()->Copy(src_file, dest_dir.path()); |
| 344 MessageLoop::current()->RunAllPending(); |
| 345 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE, |
| 346 mock_dispatcher_->status()); |
| 347 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
| 348 } |
| 349 |
232 TEST_F(FileSystemOperationTest, TestCopyFailureDestParentDoesntExist) { | 350 TEST_F(FileSystemOperationTest, TestCopyFailureDestParentDoesntExist) { |
233 // Dest. parent path does not exist. | 351 // Dest. parent path does not exist. |
234 ScopedTempDir dir; | 352 ScopedTempDir dir; |
235 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 353 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
236 FilePath src_dir = dir.path(); | 354 FilePath src_dir = dir.path(); |
237 | 355 |
238 FilePath nonexisting(base_.path().Append(FILE_PATH_LITERAL("DontExistDir"))); | 356 FilePath nonexisting(base_.path().Append(FILE_PATH_LITERAL("DontExistDir"))); |
239 file_util::EnsureEndsWithSeparator(&nonexisting); | 357 file_util::EnsureEndsWithSeparator(&nonexisting); |
240 FilePath nonexisting_file = nonexisting.Append( | 358 FilePath nonexisting_file = nonexisting.Append( |
241 FILE_PATH_LITERAL("DontExistFile")); | 359 FILE_PATH_LITERAL("DontExistFile")); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); | 392 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); |
275 FilePath dest_file(dest_dir.path().Append(FILE_PATH_LITERAL("NewFile"))); | 393 FilePath dest_file(dest_dir.path().Append(FILE_PATH_LITERAL("NewFile"))); |
276 | 394 |
277 operation()->Copy(src_file, dest_file); | 395 operation()->Copy(src_file, dest_file); |
278 MessageLoop::current()->RunAllPending(); | 396 MessageLoop::current()->RunAllPending(); |
279 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); | 397 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
280 EXPECT_TRUE(FileExists(dest_file)); | 398 EXPECT_TRUE(FileExists(dest_file)); |
281 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 399 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
282 } | 400 } |
283 | 401 |
284 TEST_F(FileSystemOperationTest, TestCopySuccessSrcDir) { | 402 TEST_F(FileSystemOperationTest, TestCopySuccessSrcDirAndOverwrite) { |
285 ScopedTempDir src_dir; | 403 ScopedTempDir src_dir; |
286 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); | 404 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
287 | 405 |
288 ScopedTempDir dest_dir; | 406 ScopedTempDir dest_dir; |
289 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); | 407 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); |
290 | 408 |
291 operation()->Copy(src_dir.path(), dest_dir.path()); | 409 operation()->Copy(src_dir.path(), dest_dir.path()); |
292 MessageLoop::current()->RunAllPending(); | 410 MessageLoop::current()->RunAllPending(); |
293 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); | 411 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
294 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 412 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
| 413 |
| 414 // Make sure we've overwritten but not copied the source under the |dest_dir|. |
| 415 EXPECT_TRUE(file_util::DirectoryExists(dest_dir.path())); |
| 416 EXPECT_FALSE(file_util::DirectoryExists( |
| 417 dest_dir.path().Append(src_dir.path().BaseName()))); |
295 } | 418 } |
296 | 419 |
297 TEST_F(FileSystemOperationTest, TestCopyDestParentExistsSuccess) { | 420 TEST_F(FileSystemOperationTest, TestCopySuccessSrcDirAndNew) { |
298 ScopedTempDir src_dir; | 421 ScopedTempDir src_dir; |
299 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); | 422 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
300 FilePath src_file; | 423 |
301 file_util::CreateTemporaryFileInDir(src_dir.path(), &src_file); | 424 ScopedTempDir dir; |
| 425 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 426 FilePath dest_dir(dir.path().Append(FILE_PATH_LITERAL("NewDirectory"))); |
| 427 |
| 428 operation()->Copy(src_dir.path(), dest_dir); |
| 429 MessageLoop::current()->RunAllPending(); |
| 430 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
| 431 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
| 432 EXPECT_TRUE(file_util::DirectoryExists(dest_dir)); |
| 433 } |
| 434 |
| 435 TEST_F(FileSystemOperationTest, TestCopySuccessSrcDirRecursive) { |
| 436 ScopedTempDir src_dir; |
| 437 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
| 438 FilePath child_file; |
| 439 file_util::CreateTemporaryFileInDir(src_dir.path(), &child_file); |
302 | 440 |
303 ScopedTempDir dest_dir; | 441 ScopedTempDir dest_dir; |
304 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); | 442 ASSERT_TRUE(dest_dir.CreateUniqueTempDir()); |
305 | 443 |
306 operation()->Copy(src_file, dest_dir.path()); | 444 operation()->Copy(src_dir.path(), dest_dir.path()); |
307 MessageLoop::current()->RunAllPending(); | 445 MessageLoop::current()->RunAllPending(); |
308 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); | 446 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
309 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 447 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
310 | 448 EXPECT_TRUE(FileExists(dest_dir.path().Append(child_file.BaseName()))); |
311 FilePath src_filename(src_file.BaseName()); | |
312 EXPECT_TRUE(FileExists(dest_dir.path().Append(src_filename))); | |
313 } | 449 } |
314 | 450 |
315 TEST_F(FileSystemOperationTest, TestCreateFileFailure) { | 451 TEST_F(FileSystemOperationTest, TestCreateFileFailure) { |
316 // Already existing file and exclusive true. | 452 // Already existing file and exclusive true. |
317 ScopedTempDir dir; | 453 ScopedTempDir dir; |
318 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 454 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
319 FilePath file; | 455 FilePath file; |
320 | 456 |
321 file_util::CreateTemporaryFileInDir(dir.path(), &file); | 457 file_util::CreateTemporaryFileInDir(dir.path(), &file); |
322 operation()->CreateFile(file, true); | 458 operation()->CreateFile(file, true); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 MessageLoop::current()->RunAllPending(); | 496 MessageLoop::current()->RunAllPending(); |
361 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); | 497 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
362 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 498 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
363 } | 499 } |
364 | 500 |
365 TEST_F(FileSystemOperationTest, | 501 TEST_F(FileSystemOperationTest, |
366 TestCreateDirFailureDestParentDoesntExist) { | 502 TestCreateDirFailureDestParentDoesntExist) { |
367 // Dest. parent path does not exist. | 503 // Dest. parent path does not exist. |
368 FilePath nonexisting(base_.path().Append( | 504 FilePath nonexisting(base_.path().Append( |
369 FILE_PATH_LITERAL("DirDoesntExist"))); | 505 FILE_PATH_LITERAL("DirDoesntExist"))); |
370 file_util::EnsureEndsWithSeparator(&nonexisting); | |
371 FilePath nonexisting_file = nonexisting.Append( | 506 FilePath nonexisting_file = nonexisting.Append( |
372 FILE_PATH_LITERAL("FileDoesntExist")); | 507 FILE_PATH_LITERAL("FileDoesntExist")); |
373 operation()->CreateDirectory(nonexisting_file, false, false); | 508 operation()->CreateDirectory(nonexisting_file, false, false); |
374 MessageLoop::current()->RunAllPending(); | 509 MessageLoop::current()->RunAllPending(); |
375 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, mock_dispatcher_->status()); | 510 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, mock_dispatcher_->status()); |
376 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 511 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
377 } | 512 } |
378 | 513 |
379 TEST_F(FileSystemOperationTest, TestCreateDirFailureDirExists) { | 514 TEST_F(FileSystemOperationTest, TestCreateDirFailureDirExists) { |
380 // Exclusive and dir existing at path. | 515 // Exclusive and dir existing at path. |
381 operation()->CreateDirectory(base_.path(), true, false); | 516 ScopedTempDir src_dir; |
| 517 ASSERT_TRUE(src_dir.CreateUniqueTempDir()); |
| 518 operation()->CreateDirectory(src_dir.path(), true, false); |
382 MessageLoop::current()->RunAllPending(); | 519 MessageLoop::current()->RunAllPending(); |
383 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, mock_dispatcher_->status()); | 520 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, mock_dispatcher_->status()); |
384 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 521 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
385 } | 522 } |
386 | 523 |
387 TEST_F(FileSystemOperationTest, TestCreateDirFailureFileExists) { | 524 TEST_F(FileSystemOperationTest, TestCreateDirFailureFileExists) { |
388 // Exclusive true and file existing at path. | 525 // Exclusive true and file existing at path. |
389 ScopedTempDir dir; | 526 ScopedTempDir dir; |
390 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 527 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
391 FilePath file; | 528 FilePath file; |
392 file_util::CreateTemporaryFileInDir(dir.path(), &file); | 529 file_util::CreateTemporaryFileInDir(dir.path(), &file); |
393 operation()->CreateDirectory(file, true, false); | 530 operation()->CreateDirectory(file, true, false); |
394 MessageLoop::current()->RunAllPending(); | 531 MessageLoop::current()->RunAllPending(); |
395 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, mock_dispatcher_->status()); | 532 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, mock_dispatcher_->status()); |
396 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 533 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
397 } | 534 } |
398 | 535 |
399 TEST_F(FileSystemOperationTest, TestCreateDirSuccess) { | 536 TEST_F(FileSystemOperationTest, TestCreateDirSuccess) { |
400 // Dir exists and exclusive is false. | 537 // Dir exists and exclusive is false. |
401 ScopedTempDir dir; | 538 ScopedTempDir dir; |
402 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 539 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
403 operation()->CreateDirectory(dir.path(), false, false); | 540 operation()->CreateDirectory(dir.path(), false, false); |
404 MessageLoop::current()->RunAllPending(); | 541 MessageLoop::current()->RunAllPending(); |
405 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); | 542 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
406 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 543 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
407 | 544 |
408 // Dir doesn't exist. | 545 // Dir doesn't exist. |
409 FilePath nonexisting_dir_path(FILE_PATH_LITERAL("nonexistingdir")); | 546 FilePath nonexisting_dir_path(base_.path().Append( |
| 547 FILE_PATH_LITERAL("nonexistingdir"))); |
410 operation()->CreateDirectory(nonexisting_dir_path, false, false); | 548 operation()->CreateDirectory(nonexisting_dir_path, false, false); |
411 MessageLoop::current()->RunAllPending(); | 549 MessageLoop::current()->RunAllPending(); |
412 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); | 550 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
413 EXPECT_TRUE(file_util::DirectoryExists(nonexisting_dir_path)); | 551 EXPECT_TRUE(file_util::DirectoryExists(nonexisting_dir_path)); |
414 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 552 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
415 } | 553 } |
416 | 554 |
417 TEST_F(FileSystemOperationTest, TestCreateDirSuccessExclusive) { | 555 TEST_F(FileSystemOperationTest, TestCreateDirSuccessExclusive) { |
418 // Dir doesn't exist. | 556 // Dir doesn't exist. |
419 ScopedTempDir dir; | 557 FilePath nonexisting_dir_path(base_.path().Append( |
420 ASSERT_TRUE(dir.CreateUniqueTempDir()); | |
421 FilePath nonexisting_dir_path(dir.path().Append( | |
422 FILE_PATH_LITERAL("nonexistingdir"))); | 558 FILE_PATH_LITERAL("nonexistingdir"))); |
423 | 559 |
424 operation()->CreateDirectory(nonexisting_dir_path, true, false); | 560 operation()->CreateDirectory(nonexisting_dir_path, true, false); |
425 MessageLoop::current()->RunAllPending(); | 561 MessageLoop::current()->RunAllPending(); |
426 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); | 562 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
427 EXPECT_TRUE(file_util::DirectoryExists(nonexisting_dir_path)); | 563 EXPECT_TRUE(file_util::DirectoryExists(nonexisting_dir_path)); |
428 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 564 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
429 } | 565 } |
430 | 566 |
431 TEST_F(FileSystemOperationTest, TestExistsAndMetadataFailure) { | 567 TEST_F(FileSystemOperationTest, TestExistsAndMetadataFailure) { |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 } | 663 } |
528 } | 664 } |
529 } | 665 } |
530 | 666 |
531 TEST_F(FileSystemOperationTest, TestRemoveFailure) { | 667 TEST_F(FileSystemOperationTest, TestRemoveFailure) { |
532 // Path doesn't exist. | 668 // Path doesn't exist. |
533 FilePath nonexisting(base_.path().Append( | 669 FilePath nonexisting(base_.path().Append( |
534 FILE_PATH_LITERAL("NonExistingDir"))); | 670 FILE_PATH_LITERAL("NonExistingDir"))); |
535 file_util::EnsureEndsWithSeparator(&nonexisting); | 671 file_util::EnsureEndsWithSeparator(&nonexisting); |
536 | 672 |
537 operation()->Remove(nonexisting); | 673 operation()->Remove(nonexisting, false /* recursive */); |
538 MessageLoop::current()->RunAllPending(); | 674 MessageLoop::current()->RunAllPending(); |
539 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, mock_dispatcher_->status()); | 675 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, mock_dispatcher_->status()); |
540 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 676 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
541 | 677 |
542 // By spec recursive is always false. Non-empty dir should fail. | 678 // It's an error to try to remove a non-empty directory if recursive flag |
| 679 // is false. |
543 // parent_dir | 680 // parent_dir |
544 // | | | 681 // | | |
545 // child_dir child_file | 682 // child_dir child_file |
546 // Verify deleting parent_dir. | 683 // Verify deleting parent_dir. |
547 ScopedTempDir parent_dir; | 684 ScopedTempDir parent_dir; |
548 ASSERT_TRUE(parent_dir.CreateUniqueTempDir()); | 685 ASSERT_TRUE(parent_dir.CreateUniqueTempDir()); |
549 FilePath child_file; | 686 FilePath child_file; |
550 file_util::CreateTemporaryFileInDir(parent_dir.path(), &child_file); | 687 file_util::CreateTemporaryFileInDir(parent_dir.path(), &child_file); |
551 FilePath child_dir; | 688 FilePath child_dir; |
552 ASSERT_TRUE(file_util::CreateTemporaryDirInDir( | 689 ASSERT_TRUE(file_util::CreateTemporaryDirInDir( |
553 parent_dir.path(), FILE_PATH_LITERAL("child_dir"), &child_dir)); | 690 parent_dir.path(), FILE_PATH_LITERAL("child_dir"), &child_dir)); |
554 | 691 |
555 operation()->Remove(parent_dir.path()); | 692 operation()->Remove(parent_dir.path(), false /* recursive */); |
556 MessageLoop::current()->RunAllPending(); | 693 MessageLoop::current()->RunAllPending(); |
557 EXPECT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, | 694 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, |
558 mock_dispatcher_->status()); | 695 mock_dispatcher_->status()); |
559 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 696 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
560 } | 697 } |
561 | 698 |
562 TEST_F(FileSystemOperationTest, TestRemoveSuccess) { | 699 TEST_F(FileSystemOperationTest, TestRemoveSuccess) { |
563 ScopedTempDir empty_dir; | 700 ScopedTempDir empty_dir; |
564 ASSERT_TRUE(empty_dir.CreateUniqueTempDir()); | 701 ASSERT_TRUE(empty_dir.CreateUniqueTempDir()); |
565 EXPECT_TRUE(file_util::DirectoryExists(empty_dir.path())); | 702 EXPECT_TRUE(file_util::DirectoryExists(empty_dir.path())); |
566 | 703 |
567 operation()->Remove(empty_dir.path()); | 704 operation()->Remove(empty_dir.path(), false /* recursive */); |
568 MessageLoop::current()->RunAllPending(); | 705 MessageLoop::current()->RunAllPending(); |
569 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); | 706 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
570 EXPECT_FALSE(file_util::DirectoryExists(empty_dir.path())); | 707 EXPECT_FALSE(file_util::DirectoryExists(empty_dir.path())); |
571 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 708 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
| 709 |
| 710 // Removing a non-empty directory with recursive flag == true should be ok. |
| 711 // parent_dir |
| 712 // | | |
| 713 // child_dir child_file |
| 714 // Verify deleting parent_dir. |
| 715 ScopedTempDir parent_dir; |
| 716 ASSERT_TRUE(parent_dir.CreateUniqueTempDir()); |
| 717 FilePath child_file; |
| 718 file_util::CreateTemporaryFileInDir(parent_dir.path(), &child_file); |
| 719 FilePath child_dir; |
| 720 ASSERT_TRUE(file_util::CreateTemporaryDirInDir( |
| 721 parent_dir.path(), FILE_PATH_LITERAL("child_dir"), &child_dir)); |
| 722 |
| 723 operation()->Remove(parent_dir.path(), true /* recursive */); |
| 724 MessageLoop::current()->RunAllPending(); |
| 725 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
| 726 EXPECT_FALSE(file_util::DirectoryExists(parent_dir.path())); |
| 727 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
572 } | 728 } |
573 | 729 |
574 TEST_F(FileSystemOperationTest, TestTruncate) { | 730 TEST_F(FileSystemOperationTest, TestTruncate) { |
575 ScopedTempDir dir; | 731 ScopedTempDir dir; |
576 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 732 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
577 FilePath file; | 733 FilePath file; |
578 file_util::CreateTemporaryFileInDir(dir.path(), &file); | 734 file_util::CreateTemporaryFileInDir(dir.path(), &file); |
579 | 735 |
580 char test_data[] = "test data"; | 736 char test_data[] = "test data"; |
581 int data_size = static_cast<int>(sizeof(test_data)); | 737 int data_size = static_cast<int>(sizeof(test_data)); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); | 775 EXPECT_EQ(kFileOperationSucceeded, mock_dispatcher_->status()); |
620 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); | 776 EXPECT_EQ(request_id_, mock_dispatcher_->request_id()); |
621 | 777 |
622 // Check that its length is now 3 and that it contains only bits of test data. | 778 // Check that its length is now 3 and that it contains only bits of test data. |
623 EXPECT_TRUE(file_util::GetFileInfo(file, &info)); | 779 EXPECT_TRUE(file_util::GetFileInfo(file, &info)); |
624 EXPECT_EQ(length, info.size); | 780 EXPECT_EQ(length, info.size); |
625 EXPECT_EQ(length, file_util::ReadFile(file, data, length)); | 781 EXPECT_EQ(length, file_util::ReadFile(file, data, length)); |
626 for (int i = 0; i < length; ++i) | 782 for (int i = 0; i < length; ++i) |
627 EXPECT_EQ(test_data[i], data[i]); | 783 EXPECT_EQ(test_data[i], data[i]); |
628 } | 784 } |
OLD | NEW |