| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/platform_file.h" | 6 #include "base/platform_file.h" |
| 7 #include "base/scoped_temp_dir.h" | 7 #include "base/scoped_temp_dir.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 TEST(ScopedTempDir, FullPath) { | 10 TEST(ScopedTempDir, FullPath) { |
| 11 FilePath test_path; | 11 FilePath test_path; |
| 12 file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_temp_dir"), | 12 file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_temp_dir"), |
| 13 &test_path); | 13 &test_path); |
| 14 | 14 |
| 15 // Against an existing dir, it should get destroyed when leaving scope. | 15 // Against an existing dir, it should get destroyed when leaving scope. |
| 16 EXPECT_TRUE(file_util::DirectoryExists(test_path)); | 16 EXPECT_TRUE(file_util::DirectoryExists(test_path)); |
| 17 { | 17 { |
| 18 ScopedTempDir dir; | 18 ScopedTempDir dir; |
| 19 EXPECT_TRUE(dir.Set(test_path)); | 19 EXPECT_TRUE(dir.Set(test_path)); |
| 20 EXPECT_TRUE(dir.IsValid()); | 20 EXPECT_TRUE(dir.IsValid()); |
| 21 } | 21 } |
| 22 EXPECT_FALSE(file_util::DirectoryExists(test_path)); | 22 EXPECT_FALSE(file_util::DirectoryExists(test_path)); |
| 23 | 23 |
| 24 { | 24 { |
| 25 ScopedTempDir dir; | 25 ScopedTempDir dir; |
| 26 dir.Set(test_path); | 26 EXPECT_TRUE(dir.Set(test_path)); |
| 27 // Now the dir doesn't exist, so ensure that it gets created. | 27 // Now the dir doesn't exist, so ensure that it gets created. |
| 28 EXPECT_TRUE(file_util::DirectoryExists(test_path)); | 28 EXPECT_TRUE(file_util::DirectoryExists(test_path)); |
| 29 // When we call Release(), it shouldn't get destroyed when leaving scope. | 29 // When we call Release(), it shouldn't get destroyed when leaving scope. |
| 30 FilePath path = dir.Take(); | 30 FilePath path = dir.Take(); |
| 31 EXPECT_EQ(path.value(), test_path.value()); | 31 EXPECT_EQ(path.value(), test_path.value()); |
| 32 EXPECT_FALSE(dir.IsValid()); | 32 EXPECT_FALSE(dir.IsValid()); |
| 33 } | 33 } |
| 34 EXPECT_TRUE(file_util::DirectoryExists(test_path)); | 34 EXPECT_TRUE(file_util::DirectoryExists(test_path)); |
| 35 | 35 |
| 36 // Clean up. | 36 // Clean up. |
| 37 { | 37 { |
| 38 ScopedTempDir dir; | 38 ScopedTempDir dir; |
| 39 dir.Set(test_path); | 39 EXPECT_TRUE(dir.Set(test_path)); |
| 40 } | 40 } |
| 41 EXPECT_FALSE(file_util::DirectoryExists(test_path)); | 41 EXPECT_FALSE(file_util::DirectoryExists(test_path)); |
| 42 } | 42 } |
| 43 | 43 |
| 44 TEST(ScopedTempDir, TempDir) { | 44 TEST(ScopedTempDir, TempDir) { |
| 45 // In this case, just verify that a directory was created and that it's a | 45 // In this case, just verify that a directory was created and that it's a |
| 46 // child of TempDir. | 46 // child of TempDir. |
| 47 FilePath test_path; | 47 FilePath test_path; |
| 48 { | 48 { |
| 49 ScopedTempDir dir; | 49 ScopedTempDir dir; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 76 } | 76 } |
| 77 | 77 |
| 78 TEST(ScopedTempDir, MultipleInvocations) { | 78 TEST(ScopedTempDir, MultipleInvocations) { |
| 79 ScopedTempDir dir; | 79 ScopedTempDir dir; |
| 80 EXPECT_TRUE(dir.CreateUniqueTempDir()); | 80 EXPECT_TRUE(dir.CreateUniqueTempDir()); |
| 81 EXPECT_FALSE(dir.CreateUniqueTempDir()); | 81 EXPECT_FALSE(dir.CreateUniqueTempDir()); |
| 82 EXPECT_TRUE(dir.Delete()); | 82 EXPECT_TRUE(dir.Delete()); |
| 83 EXPECT_TRUE(dir.CreateUniqueTempDir()); | 83 EXPECT_TRUE(dir.CreateUniqueTempDir()); |
| 84 EXPECT_FALSE(dir.CreateUniqueTempDir()); | 84 EXPECT_FALSE(dir.CreateUniqueTempDir()); |
| 85 ScopedTempDir other_dir; | 85 ScopedTempDir other_dir; |
| 86 other_dir.Set(dir.Take()); | 86 EXPECT_TRUE(other_dir.Set(dir.Take())); |
| 87 EXPECT_TRUE(dir.CreateUniqueTempDir()); | 87 EXPECT_TRUE(dir.CreateUniqueTempDir()); |
| 88 EXPECT_FALSE(dir.CreateUniqueTempDir()); | 88 EXPECT_FALSE(dir.CreateUniqueTempDir()); |
| 89 EXPECT_FALSE(other_dir.CreateUniqueTempDir()); | 89 EXPECT_FALSE(other_dir.CreateUniqueTempDir()); |
| 90 } | 90 } |
| 91 | 91 |
| 92 #if defined(OS_WIN) | 92 #if defined(OS_WIN) |
| 93 TEST(ScopedTempDir, LockedTempDir) { | 93 TEST(ScopedTempDir, LockedTempDir) { |
| 94 ScopedTempDir dir; | 94 ScopedTempDir dir; |
| 95 EXPECT_TRUE(dir.CreateUniqueTempDir()); | 95 EXPECT_TRUE(dir.CreateUniqueTempDir()); |
| 96 int file_flags = base::PLATFORM_FILE_CREATE_ALWAYS | | 96 int file_flags = base::PLATFORM_FILE_CREATE_ALWAYS | |
| 97 base::PLATFORM_FILE_WRITE; | 97 base::PLATFORM_FILE_WRITE; |
| 98 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | 98 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
| 99 FilePath file_path(dir.path().Append(FILE_PATH_LITERAL("temp"))); | 99 FilePath file_path(dir.path().Append(FILE_PATH_LITERAL("temp"))); |
| 100 base::PlatformFile file = base::CreatePlatformFile(file_path, file_flags, | 100 base::PlatformFile file = base::CreatePlatformFile(file_path, file_flags, |
| 101 NULL, &error_code); | 101 NULL, &error_code); |
| 102 EXPECT_NE(base::kInvalidPlatformFileValue, file); | 102 EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 103 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); | 103 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); |
| 104 EXPECT_FALSE(dir.Delete()); // We should not be able to delete. | 104 EXPECT_FALSE(dir.Delete()); // We should not be able to delete. |
| 105 EXPECT_FALSE(dir.path().empty()); // We should still have a valid path. | 105 EXPECT_FALSE(dir.path().empty()); // We should still have a valid path. |
| 106 EXPECT_TRUE(base::ClosePlatformFile(file)); | 106 EXPECT_TRUE(base::ClosePlatformFile(file)); |
| 107 // Now, we should be able to delete. | 107 // Now, we should be able to delete. |
| 108 EXPECT_TRUE(dir.Delete()); | 108 EXPECT_TRUE(dir.Delete()); |
| 109 } | 109 } |
| 110 #endif // defined(OS_WIN) | 110 #endif // defined(OS_WIN) |
| OLD | NEW |