| OLD | NEW |
| 1 // Copyright (c) 2006-2008 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 "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <shellapi.h> | 9 #include <shellapi.h> |
| 10 #include <shlobj.h> | 10 #include <shlobj.h> |
| 11 #endif | 11 #endif |
| (...skipping 857 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 869 test_root.Append(FILE_PATH_LITERAL("foobar.txt")); | 869 test_root.Append(FILE_PATH_LITERAL("foobar.txt")); |
| 870 EXPECT_FALSE(file_util::PathExists(test_path)); | 870 EXPECT_FALSE(file_util::PathExists(test_path)); |
| 871 CreateTextFile(test_path, L"test file"); | 871 CreateTextFile(test_path, L"test file"); |
| 872 EXPECT_TRUE(file_util::PathExists(test_path)); | 872 EXPECT_TRUE(file_util::PathExists(test_path)); |
| 873 EXPECT_FALSE(file_util::DirectoryExists(test_path)); | 873 EXPECT_FALSE(file_util::DirectoryExists(test_path)); |
| 874 EXPECT_TRUE(file_util::Delete(test_path, false)); | 874 EXPECT_TRUE(file_util::Delete(test_path, false)); |
| 875 | 875 |
| 876 EXPECT_TRUE(file_util::Delete(test_root, true)); | 876 EXPECT_TRUE(file_util::Delete(test_root, true)); |
| 877 } | 877 } |
| 878 | 878 |
| 879 static const struct goodbad_pair { | |
| 880 std::wstring bad_name; | |
| 881 std::wstring good_name; | |
| 882 } kIllegalCharacterCases[] = { | |
| 883 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"}, | |
| 884 {L"**********::::.txt", L"--------------.txt"}, | |
| 885 // We can't use UCNs (universal character names) for C0/C1 characters and | |
| 886 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend. | |
| 887 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"}, | |
| 888 #if defined(OS_WIN) | |
| 889 {L"bad*file\\name.jpg", L"bad-file-name.jpg"}, | |
| 890 {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"}, | |
| 891 #elif defined(OS_POSIX) | |
| 892 {L"bad*file?name.jpg", L"bad-file-name.jpg"}, | |
| 893 {L"\t bad*file?name/.jpg ", L"bad-file-name-.jpg"}, | |
| 894 #endif | |
| 895 {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"}, | |
| 896 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"}, | |
| 897 {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"}, | |
| 898 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"}, | |
| 899 // Unassigned codepoints are ok. | |
| 900 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"}, | |
| 901 // Non-characters are not allowed. | |
| 902 {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"}, | |
| 903 {L"bad\uFDD0file\uFDEFname.jpg ", L"bad-file-name.jpg"}, | |
| 904 }; | |
| 905 | |
| 906 TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) { | |
| 907 for (unsigned int i = 0; i < arraysize(kIllegalCharacterCases); ++i) { | |
| 908 std::wstring bad_name(kIllegalCharacterCases[i].bad_name); | |
| 909 file_util::ReplaceIllegalCharacters(&bad_name, L'-'); | |
| 910 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name); | |
| 911 } | |
| 912 } | |
| 913 | |
| 914 static const struct ReplaceExtensionCase { | 879 static const struct ReplaceExtensionCase { |
| 915 std::wstring file_name; | 880 std::wstring file_name; |
| 916 FilePath::StringType extension; | 881 FilePath::StringType extension; |
| 917 std::wstring result; | 882 std::wstring result; |
| 918 } kReplaceExtension[] = { | 883 } kReplaceExtension[] = { |
| 919 {L"", FILE_PATH_LITERAL(""), L""}, | 884 {L"", FILE_PATH_LITERAL(""), L""}, |
| 920 {L"", FILE_PATH_LITERAL("txt"), L".txt"}, | 885 {L"", FILE_PATH_LITERAL("txt"), L".txt"}, |
| 921 {L".", FILE_PATH_LITERAL("txt"), L".txt"}, | 886 {L".", FILE_PATH_LITERAL("txt"), L".txt"}, |
| 922 {L".", FILE_PATH_LITERAL(""), L""}, | 887 {L".", FILE_PATH_LITERAL(""), L""}, |
| 923 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"}, | 888 {L"foo.dll", FILE_PATH_LITERAL("txt"), L"foo.txt"}, |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1062 EXPECT_TRUE(c5.HasFile(dir2innerfile)); | 1027 EXPECT_TRUE(c5.HasFile(dir2innerfile)); |
| 1063 EXPECT_EQ(c5.size(), 5); | 1028 EXPECT_EQ(c5.size(), 5); |
| 1064 | 1029 |
| 1065 // Make sure the destructor closes the find handle while in the middle of a | 1030 // Make sure the destructor closes the find handle while in the middle of a |
| 1066 // query to allow TearDown to delete the directory. | 1031 // query to allow TearDown to delete the directory. |
| 1067 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES); | 1032 file_util::FileEnumerator f6(test_dir_, true, FILES_AND_DIRECTORIES); |
| 1068 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something | 1033 EXPECT_FALSE(f6.Next().value().empty()); // Should have found something |
| 1069 // (we don't care what). | 1034 // (we don't care what). |
| 1070 } | 1035 } |
| 1071 | 1036 |
| 1072 TEST_F(FileUtilTest, FileEnumeratorOrderTest) { | |
| 1073 FilePath fileA = test_dir_.Append(FILE_PATH_LITERAL("a")); | |
| 1074 FilePath fileB = test_dir_.Append(FILE_PATH_LITERAL("B")); | |
| 1075 FilePath dirC = test_dir_.Append(FILE_PATH_LITERAL("C")); | |
| 1076 FilePath dirD = test_dir_.Append(FILE_PATH_LITERAL("d")); | |
| 1077 FilePath dirE = test_dir_.Append(FILE_PATH_LITERAL("e")); | |
| 1078 FilePath fileF = test_dir_.Append(FILE_PATH_LITERAL("f")); | |
| 1079 | |
| 1080 // Create files/directories in near random order. | |
| 1081 CreateTextFile(fileF, L""); | |
| 1082 CreateTextFile(fileA, L""); | |
| 1083 CreateTextFile(fileB, L""); | |
| 1084 EXPECT_TRUE(file_util::CreateDirectory(dirE)); | |
| 1085 EXPECT_TRUE(file_util::CreateDirectory(dirC)); | |
| 1086 EXPECT_TRUE(file_util::CreateDirectory(dirD)); | |
| 1087 | |
| 1088 // On Windows, files and directories are enumerated in the lexicographical | |
| 1089 // order, ignoring case and whether they are files or directories. On posix, | |
| 1090 // we order directories before files. | |
| 1091 file_util::FileEnumerator enumerator(test_dir_, false, FILES_AND_DIRECTORIES); | |
| 1092 FilePath cur_file = enumerator.Next(); | |
| 1093 #if defined(OS_WIN) | |
| 1094 EXPECT_EQ(fileA.value(), cur_file.value()); | |
| 1095 cur_file = enumerator.Next(); | |
| 1096 EXPECT_EQ(fileB.value(), cur_file.value()); | |
| 1097 cur_file = enumerator.Next(); | |
| 1098 EXPECT_EQ(dirC.value(), cur_file.value()); | |
| 1099 cur_file = enumerator.Next(); | |
| 1100 EXPECT_EQ(dirD.value(), cur_file.value()); | |
| 1101 cur_file = enumerator.Next(); | |
| 1102 EXPECT_EQ(dirE.value(), cur_file.value()); | |
| 1103 cur_file = enumerator.Next(); | |
| 1104 EXPECT_EQ(fileF.value(), cur_file.value()); | |
| 1105 cur_file = enumerator.Next(); | |
| 1106 #elif defined(OS_POSIX) | |
| 1107 EXPECT_EQ(dirC.value(), cur_file.value()); | |
| 1108 cur_file = enumerator.Next(); | |
| 1109 EXPECT_EQ(dirD.value(), cur_file.value()); | |
| 1110 cur_file = enumerator.Next(); | |
| 1111 EXPECT_EQ(dirE.value(), cur_file.value()); | |
| 1112 cur_file = enumerator.Next(); | |
| 1113 EXPECT_EQ(fileA.value(), cur_file.value()); | |
| 1114 cur_file = enumerator.Next(); | |
| 1115 EXPECT_EQ(fileB.value(), cur_file.value()); | |
| 1116 cur_file = enumerator.Next(); | |
| 1117 EXPECT_EQ(fileF.value(), cur_file.value()); | |
| 1118 cur_file = enumerator.Next(); | |
| 1119 #endif | |
| 1120 | |
| 1121 EXPECT_EQ(FILE_PATH_LITERAL(""), cur_file.value()); | |
| 1122 } | |
| 1123 | |
| 1124 TEST_F(FileUtilTest, Contains) { | 1037 TEST_F(FileUtilTest, Contains) { |
| 1125 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest")); | 1038 FilePath data_dir = test_dir_.Append(FILE_PATH_LITERAL("FilePathTest")); |
| 1126 | 1039 |
| 1127 // Create a fresh, empty copy of this directory. | 1040 // Create a fresh, empty copy of this directory. |
| 1128 if (file_util::PathExists(data_dir)) { | 1041 if (file_util::PathExists(data_dir)) { |
| 1129 ASSERT_TRUE(file_util::Delete(data_dir, true)); | 1042 ASSERT_TRUE(file_util::Delete(data_dir, true)); |
| 1130 } | 1043 } |
| 1131 ASSERT_TRUE(file_util::CreateDirectory(data_dir)); | 1044 ASSERT_TRUE(file_util::CreateDirectory(data_dir)); |
| 1132 | 1045 |
| 1133 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo"))); | 1046 FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo"))); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1158 #elif defined(OS_LINUX) | 1071 #elif defined(OS_LINUX) |
| 1159 EXPECT_FALSE(file_util::ContainsPath(foo, | 1072 EXPECT_FALSE(file_util::ContainsPath(foo, |
| 1160 foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); | 1073 foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); |
| 1161 #else | 1074 #else |
| 1162 // We can't really do this test on osx since the case-sensitivity of the | 1075 // We can't really do this test on osx since the case-sensitivity of the |
| 1163 // filesystem is configurable. | 1076 // filesystem is configurable. |
| 1164 #endif | 1077 #endif |
| 1165 } | 1078 } |
| 1166 | 1079 |
| 1167 } // namespace | 1080 } // namespace |
| OLD | NEW |