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 and maximum attempt failures allowed. | |
20 // For unit tests purpose only. | |
21 const int kMaxFilesDeletedForTest = 1; | |
22 | |
23 // Simple function to dump some text into a new file. | |
24 void CreateTextFile(const base::FilePath& file_name, | |
25 const std::string& contents) { | |
26 // Since |contents|'s length is small here, static_cast won't cause overflow. | |
27 ASSERT_EQ(static_cast<int>(contents.length()), | |
28 base::WriteFile(file_name, contents.data(), contents.length())); | |
29 ASSERT_TRUE(base::PathExists(file_name)); | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 class JumpListFileUtilTest : public testing::Test { | |
35 public: | |
grt (UTC plus 2)
2017/03/23 11:24:58
make this protected: and remove protected: on line
chengx
2017/03/23 21:16:12
Done.
| |
36 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } | |
37 | |
38 protected: | |
39 // A temporary directory where all file IO operations take place . | |
40 base::ScopedTempDir temp_dir_; | |
41 | |
42 // Get the path to the temporary directory. | |
43 const base::FilePath& temp_dir_path() { return temp_dir_.GetPath(); } | |
44 }; | |
45 | |
46 TEST_F(JumpListFileUtilTest, DeleteDirectoryContent) { | |
47 base::FilePath dir_path = temp_dir_path(); | |
48 | |
49 // Create a file. | |
50 base::FilePath file_name = | |
51 dir_path.Append(FILE_PATH_LITERAL("TestDeleteFile.txt")); | |
52 ASSERT_NO_FATAL_FAILURE(CreateTextFile(file_name, kFileContent)); | |
53 | |
54 // Delete the directory content using DeleteDirectoryContent(). The file | |
55 // should be deleted and the directory remains. | |
56 ASSERT_EQ(DeleteDirectoryContent(dir_path, kMaxFilesDeleted), SUCCEED); | |
57 EXPECT_FALSE(PathExists(file_name)); | |
58 EXPECT_TRUE(DirectoryExists(dir_path)); | |
59 } | |
60 | |
61 TEST_F(JumpListFileUtilTest, DeleteSubDirectory) { | |
62 base::FilePath dir_path = temp_dir_path(); | |
63 | |
64 // Create a subdirectory. | |
65 base::FilePath test_subdir = | |
66 dir_path.Append(FILE_PATH_LITERAL("TestSubDirectory")); | |
67 ASSERT_NO_FATAL_FAILURE(CreateDirectory(test_subdir)); | |
68 | |
69 // Delete the directory using DeleteDirectory(), which should fail because | |
70 // a subdirectory exists. | |
71 ASSERT_EQ(DeleteDirectory(dir_path, kMaxFilesDeleted), | |
72 FAIL_SUBDIRECTORY_EXISTS); | |
73 EXPECT_TRUE(DirectoryExists(dir_path)); | |
74 EXPECT_TRUE(DirectoryExists(test_subdir)); | |
75 | |
76 // Delete the subdirectory alone should be working. | |
77 ASSERT_EQ(DeleteDirectory(test_subdir, kMaxFilesDeleted), SUCCEED); | |
78 EXPECT_TRUE(DirectoryExists(dir_path)); | |
79 EXPECT_FALSE(DirectoryExists(test_subdir)); | |
80 } | |
81 | |
82 TEST_F(JumpListFileUtilTest, DeleteMaxFilesAllowed) { | |
83 base::FilePath dir_path = temp_dir_path(); | |
84 | |
85 // Create 2 files. | |
86 base::FilePath file_name = | |
87 dir_path.Append(FILE_PATH_LITERAL("TestDeleteFile1.txt")); | |
88 ASSERT_NO_FATAL_FAILURE(CreateTextFile(file_name, kFileContent)); | |
89 | |
90 file_name = dir_path.Append(FILE_PATH_LITERAL("TestDeleteFile2.txt")); | |
91 ASSERT_NO_FATAL_FAILURE(CreateTextFile(file_name, kFileContent)); | |
92 | |
93 // Delete the directory content using DeleteDirectoryContent(). | |
94 // Sine the maximum files allowed to delete is 1, only 1 out of the 2 | |
95 // files is deleted. Therefore, the directory is not empty yet. | |
96 ASSERT_EQ(DeleteDirectoryContent(dir_path, kMaxFilesDeletedForTest), SUCCEED); | |
97 EXPECT_FALSE(::PathIsDirectoryEmpty(dir_path.value().c_str())); | |
98 | |
99 // Delete another file, and now the directory is empty. | |
100 ASSERT_EQ(DeleteDirectoryContent(dir_path, kMaxFilesDeletedForTest), SUCCEED); | |
101 EXPECT_TRUE(::PathIsDirectoryEmpty(dir_path.value().c_str())); | |
102 EXPECT_TRUE(DirectoryExists(dir_path)); | |
103 } | |
OLD | NEW |