OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 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. |
| 4 |
| 5 #include "chrome/browser/win/jumplist_file_util.h" |
| 6 |
| 7 #include <Shlwapi.h> |
| 8 #include <string> |
| 9 |
| 10 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 // Random text to write into a file. |
| 17 constexpr char kFileContent[] = "I'm random context."; |
| 18 |
| 19 // Maximum files allowed to delete. For test purpose only. |
| 20 const int kMaxFilesDeletedForTest = 1; |
| 21 |
| 22 // Simple function to dump some text into a new file. |
| 23 void CreateTextFile(const base::FilePath& file_name, |
| 24 const std::string& contents) { |
| 25 ASSERT_EQ(contents.length(), |
| 26 base::WriteFile(file_name, contents.data(), contents.length())); |
| 27 ASSERT_TRUE(base::PathExists(file_name)); |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 class JumpListFileUtilTest : public testing::Test { |
| 33 public: |
| 34 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
| 35 |
| 36 protected: |
| 37 // A temporary directory. |
| 38 base::ScopedTempDir temp_dir_; |
| 39 |
| 40 // Get the path to the temporary directory. |
| 41 const base::FilePath& temp_dir_path() { return temp_dir_.GetPath(); } |
| 42 }; |
| 43 |
| 44 TEST_F(JumpListFileUtilTest, DeleteDirectoryContent) { |
| 45 base::FilePath dir_path = temp_dir_path(); |
| 46 |
| 47 // Create a file. |
| 48 base::FilePath file_name = |
| 49 dir_path.Append(FILE_PATH_LITERAL("TestDeleteFile.txt")); |
| 50 ASSERT_NO_FATAL_FAILURE(CreateTextFile(file_name, kFileContent)); |
| 51 |
| 52 // Delete the directory content using DeleteDirectoryContent(). The file |
| 53 // should be deleted and the directory remains. |
| 54 ASSERT_EQ(DeleteDirectoryContent(dir_path, kMaxFilesDeleted), SUCCEED); |
| 55 EXPECT_FALSE(PathExists(file_name)); |
| 56 EXPECT_TRUE(DirectoryExists(dir_path)); |
| 57 } |
| 58 |
| 59 TEST_F(JumpListFileUtilTest, DeleteSubDirectory) { |
| 60 base::FilePath dir_path = temp_dir_path(); |
| 61 |
| 62 // Create a subdirectory. |
| 63 base::FilePath test_subdir = |
| 64 dir_path.Append(FILE_PATH_LITERAL("TestSubDirectory")); |
| 65 ASSERT_NO_FATAL_FAILURE(CreateDirectory(test_subdir)); |
| 66 |
| 67 // Delete the directory using DeleteDirectory(), which should fail because |
| 68 // a subdirectory exists. |
| 69 ASSERT_EQ(DeleteDirectory(dir_path, kMaxFilesDeleted), |
| 70 FAIL_SUBDIRECTORY_EXISTS); |
| 71 EXPECT_TRUE(DirectoryExists(dir_path)); |
| 72 EXPECT_TRUE(DirectoryExists(test_subdir)); |
| 73 |
| 74 // Delete the subdirectory alone should be working. |
| 75 ASSERT_EQ(DeleteDirectory(test_subdir, kMaxFilesDeleted), SUCCEED); |
| 76 EXPECT_TRUE(DirectoryExists(dir_path)); |
| 77 EXPECT_FALSE(DirectoryExists(test_subdir)); |
| 78 } |
| 79 |
| 80 TEST_F(JumpListFileUtilTest, DeleteMaxFilesAllowed) { |
| 81 base::FilePath dir_path = temp_dir_path(); |
| 82 |
| 83 // Create 2 files. |
| 84 base::FilePath file_name = |
| 85 dir_path.Append(FILE_PATH_LITERAL("TestDeleteFile1.txt")); |
| 86 ASSERT_NO_FATAL_FAILURE(CreateTextFile(file_name, kFileContent)); |
| 87 |
| 88 file_name = dir_path.Append(FILE_PATH_LITERAL("TestDeleteFile2.txt")); |
| 89 ASSERT_NO_FATAL_FAILURE(CreateTextFile(file_name, kFileContent)); |
| 90 |
| 91 // Delete the directory content using DeleteDirectoryContent(). |
| 92 // Sine the maximum files allowed to delete is one, only one out of the two |
| 93 // files created is deleted. Therefore, the directory is not empty yet. |
| 94 ASSERT_EQ(DeleteDirectoryContent(dir_path, kMaxFilesDeletedForTest), SUCCEED); |
| 95 EXPECT_FALSE(::PathIsDirectoryEmpty(dir_path.value().c_str())); |
| 96 |
| 97 // Delete another file, and now the directory is empty. |
| 98 ASSERT_EQ(DeleteDirectoryContent(dir_path, kMaxFilesDeletedForTest), SUCCEED); |
| 99 EXPECT_TRUE(::PathIsDirectoryEmpty(dir_path.value().c_str())); |
| 100 EXPECT_TRUE(DirectoryExists(dir_path)); |
| 101 } |
OLD | NEW |