Index: chrome/browser/win/jumplist_file_util_unittest.cc |
diff --git a/chrome/browser/win/jumplist_file_util_unittest.cc b/chrome/browser/win/jumplist_file_util_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..653bbb7974d9fe66ce6d6af5856b5465301af5cf |
--- /dev/null |
+++ b/chrome/browser/win/jumplist_file_util_unittest.cc |
@@ -0,0 +1,100 @@ |
+// Copyright (c) 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/win/jumplist_file_util.h" |
+ |
+#include <fstream> |
+#include <string> |
+ |
+#include "base/files/file_util.h" |
+#include "base/files/scoped_temp_dir.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+// 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.
|
+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.
|
+ |
+// Simple function to dump some text into a new file. |
+void CreateTextFile(const base::FilePath& filename, |
+ const std::wstring& contents) { |
+ 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.
|
+ file.open(filename.value().c_str()); |
+ ASSERT_TRUE(file.is_open()); |
+ file << contents; |
+ file.close(); |
+} |
+} // namespace |
+ |
+class JumpListFileUtilTest : public testing::Test { |
+ public: |
grt (UTC plus 2)
2017/03/22 12:07:22
protected
chengx
2017/03/22 21:16:36
Done.
|
+ JumpListFileUtilTest() {} |
+}; |
+ |
+TEST_F(JumpListFileUtilTest, DeleteDirectoryContent) { |
+ // Create a temporarily directory. |
+ 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.
|
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
+ |
+ // Create a file. |
+ base::FilePath file_name = |
+ temp_dir.GetPath().Append(FILE_PATH_LITERAL("TestDeleteFile.txt")); |
+ 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.
|
+ 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.
|
+ |
+ // Delete the directory using DeleteDirectoryContent(). The file should be |
+ // deleted and the directory remains. |
+ FolderDeleteResult delete_status = SUCCEED; |
+ EXPECT_TRUE(DeleteDirectoryContent(temp_dir.GetPath(), true, &delete_status)); |
+ EXPECT_FALSE(PathExists(file_name)); |
+ EXPECT_TRUE(DirectoryExists(temp_dir.GetPath())); |
+ |
+ // Delete the directory using DeleteDirectory() and the directory should be |
+ // deleted. |
+ DeleteDirectory(temp_dir.GetPath(), true, &delete_status); |
+ EXPECT_FALSE(DirectoryExists(temp_dir.GetPath())); |
+} |
+ |
+TEST_F(JumpListFileUtilTest, DeleteDirectory) { |
+ // Create a temporarily directory. |
+ base::ScopedTempDir temp_dir; |
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
+ |
+ // Create a file. |
+ base::FilePath file_name = |
+ temp_dir.GetPath().Append(FILE_PATH_LITERAL("TestDeleteFile.txt")); |
+ CreateTextFile(file_name, file_content); |
+ ASSERT_TRUE(PathExists(file_name)); |
+ |
+ // Delete the directory using DeleteDirectory(). Both the directory and its |
+ // contents should be deleted. |
+ FolderDeleteResult delete_status = SUCCEED; |
+ DeleteDirectory(temp_dir.GetPath(), true, &delete_status); |
+ EXPECT_FALSE(PathExists(file_name)); |
+ EXPECT_FALSE(DirectoryExists(temp_dir.GetPath())); |
+} |
+ |
+TEST_F(JumpListFileUtilTest, DeleteSubDirectory) { |
+ // Create a temporarily directory. |
+ base::ScopedTempDir temp_dir; |
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
+ |
+ // Create a subdirectory. |
+ base::FilePath test_subdir = |
+ temp_dir.GetPath().Append(FILE_PATH_LITERAL("TestSubDirectory")); |
+ CreateDirectory(test_subdir); |
+ ASSERT_TRUE(PathExists(test_subdir)); |
+ |
+ // Delete the directory using DeleteDirectory(), which should fail because |
+ // subdirectory exists.. |
+ FolderDeleteResult delete_status = SUCCEED; |
+ DeleteDirectory(temp_dir.GetPath(), false, &delete_status); |
+ EXPECT_TRUE(DirectoryExists(temp_dir.GetPath())); |
+ EXPECT_TRUE(DirectoryExists(test_subdir)); |
+ EXPECT_TRUE(delete_status == FAIL_SUBDIRECTORY_EXISTS); |
+ |
+ // Delete the directories using base::Delete() method. |
+ base::DeleteFile(temp_dir.GetPath(), true); |
+ EXPECT_FALSE(DirectoryExists(temp_dir.GetPath())); |
+ EXPECT_FALSE(DirectoryExists(test_subdir)); |
+} |