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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <shellapi.h> | 9 #include <shellapi.h> |
10 #include <shlobj.h> | 10 #include <shlobj.h> |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "base/time.h" | 24 #include "base/time.h" |
25 #include "base/utf_string_conversions.h" | 25 #include "base/utf_string_conversions.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
27 #include "testing/platform_test.h" | 27 #include "testing/platform_test.h" |
28 | 28 |
29 // This macro helps avoid wrapped lines in the test structs. | 29 // This macro helps avoid wrapped lines in the test structs. |
30 #define FPL(x) FILE_PATH_LITERAL(x) | 30 #define FPL(x) FILE_PATH_LITERAL(x) |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
| 34 const wchar_t bogus_content[] = L"I'm cannon fodder."; |
| 35 |
34 const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES = | 36 const file_util::FileEnumerator::FILE_TYPE FILES_AND_DIRECTORIES = |
35 static_cast<file_util::FileEnumerator::FILE_TYPE>( | 37 static_cast<file_util::FileEnumerator::FILE_TYPE>( |
36 file_util::FileEnumerator::FILES | | 38 file_util::FileEnumerator::FILES | |
37 file_util::FileEnumerator::DIRECTORIES); | 39 file_util::FileEnumerator::DIRECTORIES); |
38 | 40 |
39 // file_util winds up using autoreleased objects on the Mac, so this needs | 41 // file_util winds up using autoreleased objects on the Mac, so this needs |
40 // to be a PlatformTest | 42 // to be a PlatformTest |
41 class FileUtilTest : public PlatformTest { | 43 class FileUtilTest : public PlatformTest { |
42 protected: | 44 protected: |
43 virtual void SetUp() { | 45 virtual void SetUp() { |
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 FilePath subsubdir_path = subdir_path.Append(FPL("Level3")); | 380 FilePath subsubdir_path = subdir_path.Append(FPL("Level3")); |
379 file_util::CreateDirectory(subsubdir_path); | 381 file_util::CreateDirectory(subsubdir_path); |
380 | 382 |
381 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt")); | 383 FilePath file_03 = subsubdir_path.Append(FPL("The file 03.txt")); |
382 CreateTextFile(file_03, L"123"); | 384 CreateTextFile(file_03, L"123"); |
383 | 385 |
384 int64 computed_size = file_util::ComputeDirectorySize(test_dir_); | 386 int64 computed_size = file_util::ComputeDirectorySize(test_dir_); |
385 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size); | 387 EXPECT_EQ(size_f1 + size_f2 + 3, computed_size); |
386 } | 388 } |
387 | 389 |
388 // Tests that the Delete function works as expected, especially | 390 TEST_F(FileUtilTest, DeleteNonExistent) { |
389 // the recursion flag. Also coincidentally tests PathExists. | 391 FilePath non_existent = test_dir_.AppendASCII("bogus_file_dne.foobar"); |
390 TEST_F(FileUtilTest, Delete) { | 392 ASSERT_FALSE(file_util::PathExists(non_existent)); |
| 393 |
| 394 EXPECT_TRUE(file_util::Delete(non_existent, false)); |
| 395 ASSERT_FALSE(file_util::PathExists(non_existent)); |
| 396 EXPECT_TRUE(file_util::Delete(non_existent, true)); |
| 397 ASSERT_FALSE(file_util::PathExists(non_existent)); |
| 398 } |
| 399 |
| 400 TEST_F(FileUtilTest, DeleteFile) { |
391 // Create a file | 401 // Create a file |
392 FilePath file_name = test_dir_.Append(FILE_PATH_LITERAL("Test File.txt")); | 402 FilePath file_name = test_dir_.Append(FPL("Test DeleteFile 1.txt")); |
393 CreateTextFile(file_name, L"I'm cannon fodder."); | 403 CreateTextFile(file_name, bogus_content); |
394 | |
395 ASSERT_TRUE(file_util::PathExists(file_name)); | 404 ASSERT_TRUE(file_util::PathExists(file_name)); |
396 | 405 |
397 FilePath subdir_path = test_dir_.Append(FILE_PATH_LITERAL("Subdirectory")); | 406 // Make sure it's deleted |
| 407 EXPECT_TRUE(file_util::Delete(file_name, false)); |
| 408 EXPECT_FALSE(file_util::PathExists(file_name)); |
| 409 |
| 410 // Test recursive case, create a new file |
| 411 file_name = test_dir_.Append(FPL("Test DeleteFile 2.txt")); |
| 412 CreateTextFile(file_name, bogus_content); |
| 413 ASSERT_TRUE(file_util::PathExists(file_name)); |
| 414 |
| 415 // Make sure it's deleted |
| 416 EXPECT_TRUE(file_util::Delete(file_name, true)); |
| 417 EXPECT_FALSE(file_util::PathExists(file_name)); |
| 418 } |
| 419 |
| 420 #if defined(OS_WIN) |
| 421 // Tests that the Delete function works for wild cards, especially |
| 422 // with the recursion flag. Also coincidentally tests PathExists. |
| 423 // TODO(erikkay): see if anyone's actually using this feature of the API |
| 424 TEST_F(FileUtilTest, DeleteWildCard) { |
| 425 // Create a file and a directory |
| 426 FilePath file_name = test_dir_.Append(FPL("Test DeleteWildCard.txt")); |
| 427 CreateTextFile(file_name, bogus_content); |
| 428 ASSERT_TRUE(file_util::PathExists(file_name)); |
| 429 |
| 430 FilePath subdir_path = test_dir_.Append(FPL("DeleteWildCardDir")); |
398 file_util::CreateDirectory(subdir_path); | 431 file_util::CreateDirectory(subdir_path); |
399 | |
400 ASSERT_TRUE(file_util::PathExists(subdir_path)); | 432 ASSERT_TRUE(file_util::PathExists(subdir_path)); |
401 | 433 |
| 434 // Create the wildcard path |
402 FilePath directory_contents = test_dir_; | 435 FilePath directory_contents = test_dir_; |
403 #if defined(OS_WIN) | 436 directory_contents = directory_contents.Append(FPL("*")); |
404 // TODO(erikkay): see if anyone's actually using this feature of the API | 437 |
405 directory_contents = directory_contents.Append(FILE_PATH_LITERAL("*")); | |
406 // Delete non-recursively and check that only the file is deleted | 438 // Delete non-recursively and check that only the file is deleted |
407 ASSERT_TRUE(file_util::Delete(directory_contents, false)); | 439 EXPECT_TRUE(file_util::Delete(directory_contents, false)); |
408 EXPECT_FALSE(file_util::PathExists(file_name)); | 440 EXPECT_FALSE(file_util::PathExists(file_name)); |
409 EXPECT_TRUE(file_util::PathExists(subdir_path)); | 441 EXPECT_TRUE(file_util::PathExists(subdir_path)); |
| 442 |
| 443 // Delete recursively and make sure all contents are deleted |
| 444 EXPECT_TRUE(file_util::Delete(directory_contents, true)); |
| 445 EXPECT_FALSE(file_util::PathExists(file_name)); |
| 446 EXPECT_FALSE(file_util::PathExists(subdir_path)); |
| 447 } |
| 448 |
| 449 // TODO(erikkay): see if anyone's actually using this feature of the API |
| 450 TEST_F(FileUtilTest, DeleteNonExistantWildCard) { |
| 451 // Create a file and a directory |
| 452 FilePath subdir_path = test_dir_.Append(FPL("DeleteNonExistantWildCard")); |
| 453 file_util::CreateDirectory(subdir_path); |
| 454 ASSERT_TRUE(file_util::PathExists(subdir_path)); |
| 455 |
| 456 // Create the wildcard path |
| 457 FilePath directory_contents = subdir_path; |
| 458 directory_contents = directory_contents.Append(FPL("*")); |
| 459 |
| 460 // Delete non-recursively and check nothing got deleted |
| 461 EXPECT_TRUE(file_util::Delete(directory_contents, false)); |
| 462 EXPECT_TRUE(file_util::PathExists(subdir_path)); |
| 463 |
| 464 // Delete recursively and check nothing got deleted |
| 465 EXPECT_TRUE(file_util::Delete(directory_contents, true)); |
| 466 EXPECT_TRUE(file_util::PathExists(subdir_path)); |
| 467 } |
410 #endif | 468 #endif |
411 | 469 |
412 // Delete recursively and make sure all contents are deleted | 470 // Tests non-recursive Delete() for a directory. |
413 ASSERT_TRUE(file_util::Delete(directory_contents, true)); | 471 TEST_F(FileUtilTest, DeleteDirNonRecursive) { |
| 472 // Create a subdirectory and put a file and two directories inside. |
| 473 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirNonRecursive")); |
| 474 file_util::CreateDirectory(test_subdir); |
| 475 ASSERT_TRUE(file_util::PathExists(test_subdir)); |
| 476 |
| 477 FilePath file_name = test_subdir.Append(FPL("Test DeleteDir.txt")); |
| 478 CreateTextFile(file_name, bogus_content); |
| 479 ASSERT_TRUE(file_util::PathExists(file_name)); |
| 480 |
| 481 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1")); |
| 482 file_util::CreateDirectory(subdir_path1); |
| 483 ASSERT_TRUE(file_util::PathExists(subdir_path1)); |
| 484 |
| 485 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2")); |
| 486 file_util::CreateDirectory(subdir_path2); |
| 487 ASSERT_TRUE(file_util::PathExists(subdir_path2)); |
| 488 |
| 489 // Delete non-recursively and check that the empty dir got deleted |
| 490 EXPECT_TRUE(file_util::Delete(subdir_path2, false)); |
| 491 EXPECT_FALSE(file_util::PathExists(subdir_path2)); |
| 492 |
| 493 // Delete non-recursively and check that nothing got deleted |
| 494 EXPECT_FALSE(file_util::Delete(test_subdir, false)); |
| 495 EXPECT_TRUE(file_util::PathExists(test_subdir)); |
| 496 EXPECT_TRUE(file_util::PathExists(file_name)); |
| 497 EXPECT_TRUE(file_util::PathExists(subdir_path1)); |
| 498 } |
| 499 |
| 500 // Tests recursive Delete() for a directory. |
| 501 TEST_F(FileUtilTest, DeleteDirRecursive) { |
| 502 // Create a subdirectory and put a file and two directories inside. |
| 503 FilePath test_subdir = test_dir_.Append(FPL("DeleteDirRecursive")); |
| 504 file_util::CreateDirectory(test_subdir); |
| 505 ASSERT_TRUE(file_util::PathExists(test_subdir)); |
| 506 |
| 507 FilePath file_name = test_subdir.Append(FPL("Test DeleteDirRecursive.txt")); |
| 508 CreateTextFile(file_name, bogus_content); |
| 509 ASSERT_TRUE(file_util::PathExists(file_name)); |
| 510 |
| 511 FilePath subdir_path1 = test_subdir.Append(FPL("TestSubDir1")); |
| 512 file_util::CreateDirectory(subdir_path1); |
| 513 ASSERT_TRUE(file_util::PathExists(subdir_path1)); |
| 514 |
| 515 FilePath subdir_path2 = test_subdir.Append(FPL("TestSubDir2")); |
| 516 file_util::CreateDirectory(subdir_path2); |
| 517 ASSERT_TRUE(file_util::PathExists(subdir_path2)); |
| 518 |
| 519 // Delete recursively and check that the empty dir got deleted |
| 520 EXPECT_TRUE(file_util::Delete(subdir_path2, true)); |
| 521 EXPECT_FALSE(file_util::PathExists(subdir_path2)); |
| 522 |
| 523 // Delete recursively and check that everything got deleted |
| 524 EXPECT_TRUE(file_util::Delete(test_subdir, true)); |
414 EXPECT_FALSE(file_util::PathExists(file_name)); | 525 EXPECT_FALSE(file_util::PathExists(file_name)); |
415 EXPECT_FALSE(file_util::PathExists(subdir_path)); | 526 EXPECT_FALSE(file_util::PathExists(subdir_path1)); |
| 527 EXPECT_FALSE(file_util::PathExists(test_subdir)); |
416 } | 528 } |
417 | 529 |
418 TEST_F(FileUtilTest, MoveFileNew) { | 530 TEST_F(FileUtilTest, MoveFileNew) { |
419 // Create a file | 531 // Create a file |
420 FilePath file_name_from = | 532 FilePath file_name_from = |
421 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); | 533 test_dir_.Append(FILE_PATH_LITERAL("Move_Test_File.txt")); |
422 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); | 534 CreateTextFile(file_name_from, L"Gooooooooooooooooooooogle"); |
423 ASSERT_TRUE(file_util::PathExists(file_name_from)); | 535 ASSERT_TRUE(file_util::PathExists(file_name_from)); |
424 | 536 |
425 // The destination | 537 // The destination |
(...skipping 1027 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1453 // modification times with 2s resolution. | 1565 // modification times with 2s resolution. |
1454 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT", | 1566 ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT", |
1455 &modification_time)); | 1567 &modification_time)); |
1456 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time)); | 1568 ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time)); |
1457 file_util::FileInfo file_info; | 1569 file_util::FileInfo file_info; |
1458 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info)); | 1570 ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info)); |
1459 ASSERT_TRUE(file_info.last_modified == modification_time); | 1571 ASSERT_TRUE(file_info.last_modified == modification_time); |
1460 } | 1572 } |
1461 | 1573 |
1462 } // namespace | 1574 } // namespace |
OLD | NEW |