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 <fstream> | |
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 // Random text to write into a file. | |
grt (UTC plus 2)
2017/03/22 12:07:22
nit: blank line before this and before the close o
chengx
2017/03/22 21:16:37
Done.
| |
16 const wchar_t file_content[] = L"I'm random context."; | |
grt (UTC plus 2)
2017/03/22 12:07:22
constexpr wchar_t kFileContent[] = ...
chengx
2017/03/22 21:16:37
Done.
| |
17 | |
18 // Simple function to dump some text into a new file. | |
19 void CreateTextFile(const base::FilePath& filename, | |
20 const std::wstring& contents) { | |
21 std::wofstream file; | |
grt (UTC plus 2)
2017/03/22 12:07:22
ASSERT_EQ(contents.length(), base::WriteFile(filen
chengx
2017/03/22 21:16:36
Done.
| |
22 file.open(filename.value().c_str()); | |
23 ASSERT_TRUE(file.is_open()); | |
24 file << contents; | |
25 file.close(); | |
26 } | |
27 } // namespace | |
28 | |
29 class JumpListFileUtilTest : public testing::Test { | |
30 public: | |
grt (UTC plus 2)
2017/03/22 12:07:22
protected
chengx
2017/03/22 21:16:36
Done.
| |
31 JumpListFileUtilTest() {} | |
32 }; | |
33 | |
34 TEST_F(JumpListFileUtilTest, DeleteDirectoryContent) { | |
35 // Create a temporarily directory. | |
36 base::ScopedTempDir temp_dir; | |
grt (UTC plus 2)
2017/03/22 12:07:22
put these two lines in a SetUp() override in the t
chengx
2017/03/22 21:16:36
Done.
| |
37 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
38 | |
39 // Create a file. | |
40 base::FilePath file_name = | |
41 temp_dir.GetPath().Append(FILE_PATH_LITERAL("TestDeleteFile.txt")); | |
42 CreateTextFile(file_name, file_content); | |
grt (UTC plus 2)
2017/03/22 12:07:22
ASSERT_NO_FATAL_FAILURE(CreateTextFile(...)); here
chengx
2017/03/22 21:16:36
Done.
| |
43 ASSERT_TRUE(PathExists(file_name)); | |
grt (UTC plus 2)
2017/03/22 12:07:22
move this assert into CreateTextFile
chengx
2017/03/22 21:16:36
Done.
| |
44 | |
45 // Delete the directory using DeleteDirectoryContent(). The file should be | |
46 // deleted and the directory remains. | |
47 FolderDeleteResult delete_status = SUCCEED; | |
48 EXPECT_TRUE(DeleteDirectoryContent(temp_dir.GetPath(), true, &delete_status)); | |
49 EXPECT_FALSE(PathExists(file_name)); | |
50 EXPECT_TRUE(DirectoryExists(temp_dir.GetPath())); | |
51 | |
52 // Delete the directory using DeleteDirectory() and the directory should be | |
53 // deleted. | |
54 DeleteDirectory(temp_dir.GetPath(), true, &delete_status); | |
55 EXPECT_FALSE(DirectoryExists(temp_dir.GetPath())); | |
56 } | |
57 | |
58 TEST_F(JumpListFileUtilTest, DeleteDirectory) { | |
59 // Create a temporarily directory. | |
60 base::ScopedTempDir temp_dir; | |
61 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
62 | |
63 // Create a file. | |
64 base::FilePath file_name = | |
65 temp_dir.GetPath().Append(FILE_PATH_LITERAL("TestDeleteFile.txt")); | |
66 CreateTextFile(file_name, file_content); | |
67 ASSERT_TRUE(PathExists(file_name)); | |
68 | |
69 // Delete the directory using DeleteDirectory(). Both the directory and its | |
70 // contents should be deleted. | |
71 FolderDeleteResult delete_status = SUCCEED; | |
72 DeleteDirectory(temp_dir.GetPath(), true, &delete_status); | |
73 EXPECT_FALSE(PathExists(file_name)); | |
74 EXPECT_FALSE(DirectoryExists(temp_dir.GetPath())); | |
75 } | |
76 | |
77 TEST_F(JumpListFileUtilTest, DeleteSubDirectory) { | |
78 // Create a temporarily directory. | |
79 base::ScopedTempDir temp_dir; | |
80 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
81 | |
82 // Create a subdirectory. | |
83 base::FilePath test_subdir = | |
84 temp_dir.GetPath().Append(FILE_PATH_LITERAL("TestSubDirectory")); | |
85 CreateDirectory(test_subdir); | |
86 ASSERT_TRUE(PathExists(test_subdir)); | |
87 | |
88 // Delete the directory using DeleteDirectory(), which should fail because | |
89 // subdirectory exists.. | |
90 FolderDeleteResult delete_status = SUCCEED; | |
91 DeleteDirectory(temp_dir.GetPath(), false, &delete_status); | |
92 EXPECT_TRUE(DirectoryExists(temp_dir.GetPath())); | |
93 EXPECT_TRUE(DirectoryExists(test_subdir)); | |
94 EXPECT_TRUE(delete_status == FAIL_SUBDIRECTORY_EXISTS); | |
95 | |
96 // Delete the directories using base::Delete() method. | |
97 base::DeleteFile(temp_dir.GetPath(), true); | |
98 EXPECT_FALSE(DirectoryExists(temp_dir.GetPath())); | |
99 EXPECT_FALSE(DirectoryExists(test_subdir)); | |
100 } | |
OLD | NEW |