OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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> |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 std::wstring result = value.path; | 140 std::wstring result = value.path; |
141 file_util::AppendToPath(&result, value.ending); | 141 file_util::AppendToPath(&result, value.ending); |
142 EXPECT_EQ(value.result, result); | 142 EXPECT_EQ(value.result, result); |
143 } | 143 } |
144 | 144 |
145 #ifdef NDEBUG | 145 #ifdef NDEBUG |
146 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode | 146 file_util::AppendToPath(NULL, L"path"); // asserts in debug mode |
147 #endif | 147 #endif |
148 } | 148 } |
149 | 149 |
| 150 // TODO(port): enable this test for non-Windows. |
| 151 #if defined(OS_WIN) |
150 static const struct InsertBeforeExtensionCase { | 152 static const struct InsertBeforeExtensionCase { |
151 std::wstring path; | 153 std::wstring path; |
152 std::wstring suffix; | 154 std::wstring suffix; |
153 std::wstring result; | 155 std::wstring result; |
154 } kInsertBeforeExtension[] = { | 156 } kInsertBeforeExtension[] = { |
155 {L"", L"", L""}, | 157 {L"", L"", L""}, |
156 {L"", L"txt", L"txt"}, | 158 {L"", L"txt", L"txt"}, |
157 {L".", L"txt", L"txt."}, | 159 {L".", L"txt", L"txt."}, |
158 {L".", L"", L"."}, | 160 {L".", L"", L"."}, |
159 {L"foo.dll", L"txt", L"footxt.dll"}, | 161 {L"foo.dll", L"txt", L"footxt.dll"}, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 #endif | 199 #endif |
198 }; | 200 }; |
199 | 201 |
200 TEST_F(FileUtilTest, InsertBeforeExtensionTest) { | 202 TEST_F(FileUtilTest, InsertBeforeExtensionTest) { |
201 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) { | 203 for (unsigned int i = 0; i < arraysize(kInsertBeforeExtension); ++i) { |
202 std::wstring path(kInsertBeforeExtension[i].path); | 204 std::wstring path(kInsertBeforeExtension[i].path); |
203 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix); | 205 file_util::InsertBeforeExtension(&path, kInsertBeforeExtension[i].suffix); |
204 EXPECT_EQ(path, kInsertBeforeExtension[i].result); | 206 EXPECT_EQ(path, kInsertBeforeExtension[i].result); |
205 } | 207 } |
206 } | 208 } |
| 209 #endif // defined(OS_WIN) |
207 | 210 |
208 static const struct filename_case { | 211 static const struct filename_case { |
209 const wchar_t* path; | 212 const wchar_t* path; |
210 const wchar_t* filename; | 213 const wchar_t* filename; |
211 } filename_cases[] = { | 214 } filename_cases[] = { |
212 #if defined(OS_WIN) | 215 #if defined(OS_WIN) |
213 {L"c:\\colon\\backslash", L"backslash"}, | 216 {L"c:\\colon\\backslash", L"backslash"}, |
214 {L"c:\\colon\\backslash\\", L""}, | 217 {L"c:\\colon\\backslash\\", L""}, |
215 {L"\\\\filename.exe", L"filename.exe"}, | 218 {L"\\\\filename.exe", L"filename.exe"}, |
216 {L"filename.exe", L"filename.exe"}, | 219 {L"filename.exe", L"filename.exe"}, |
(...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
778 }; | 781 }; |
779 | 782 |
780 TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) { | 783 TEST_F(FileUtilTest, ReplaceIllegalCharactersTest) { |
781 for (unsigned int i = 0; i < arraysize(kIllegalCharacterCases); ++i) { | 784 for (unsigned int i = 0; i < arraysize(kIllegalCharacterCases); ++i) { |
782 std::wstring bad_name(kIllegalCharacterCases[i].bad_name); | 785 std::wstring bad_name(kIllegalCharacterCases[i].bad_name); |
783 file_util::ReplaceIllegalCharacters(&bad_name, L'-'); | 786 file_util::ReplaceIllegalCharacters(&bad_name, L'-'); |
784 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name); | 787 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name); |
785 } | 788 } |
786 } | 789 } |
787 | 790 |
| 791 // TODO(port): enable this test for non-windows. |
| 792 #if defined(OS_WIN) |
788 static const struct ReplaceExtensionCase { | 793 static const struct ReplaceExtensionCase { |
789 std::wstring file_name; | 794 std::wstring file_name; |
790 std::wstring extension; | 795 std::wstring extension; |
791 std::wstring result; | 796 std::wstring result; |
792 } kReplaceExtension[] = { | 797 } kReplaceExtension[] = { |
793 {L"", L"", L""}, | 798 {L"", L"", L""}, |
794 {L"", L"txt", L".txt"}, | 799 {L"", L"txt", L".txt"}, |
795 {L".", L"txt", L".txt"}, | 800 {L".", L"txt", L".txt"}, |
796 {L".", L"", L""}, | 801 {L".", L"", L""}, |
797 {L"foo.dll", L"txt", L"foo.txt"}, | 802 {L"foo.dll", L"txt", L"foo.txt"}, |
(...skipping 22 matching lines...) Expand all Loading... |
820 // the directory names of the path. | 825 // the directory names of the path. |
821 TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) { | 826 TEST_F(FileUtilTest, ReplaceExtensionTestWithPathSeparators) { |
822 std::wstring path; | 827 std::wstring path; |
823 file_util::AppendToPath(&path, L"foo.bar"); | 828 file_util::AppendToPath(&path, L"foo.bar"); |
824 file_util::AppendToPath(&path, L"foo"); | 829 file_util::AppendToPath(&path, L"foo"); |
825 // '/foo.bar/foo' with extension '.baz' | 830 // '/foo.bar/foo' with extension '.baz' |
826 std::wstring result_path = path; | 831 std::wstring result_path = path; |
827 file_util::ReplaceExtension(&result_path, L".baz"); | 832 file_util::ReplaceExtension(&result_path, L".baz"); |
828 EXPECT_EQ(path + L".baz", result_path); | 833 EXPECT_EQ(path + L".baz", result_path); |
829 } | 834 } |
| 835 #endif // defined(OS_WIN) |
830 | 836 |
831 TEST_F(FileUtilTest, FileEnumeratorTest) { | 837 TEST_F(FileUtilTest, FileEnumeratorTest) { |
832 // Test an empty directory. | 838 // Test an empty directory. |
833 file_util::FileEnumerator f0(test_dir_.ToWStringHack(), true, | 839 file_util::FileEnumerator f0(test_dir_.ToWStringHack(), true, |
834 file_util::FileEnumerator::FILES_AND_DIRECTORIES); | 840 file_util::FileEnumerator::FILES_AND_DIRECTORIES); |
835 EXPECT_EQ(f0.Next(), L""); | 841 EXPECT_EQ(f0.Next(), L""); |
836 EXPECT_EQ(f0.Next(), L""); | 842 EXPECT_EQ(f0.Next(), L""); |
837 | 843 |
838 // create the directories | 844 // create the directories |
839 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1")); | 845 FilePath dir1 = test_dir_.Append(FILE_PATH_LITERAL("dir1")); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
918 EXPECT_EQ(c5.size(), 5); | 924 EXPECT_EQ(c5.size(), 5); |
919 | 925 |
920 // Make sure the destructor closes the find handle while in the middle of a | 926 // Make sure the destructor closes the find handle while in the middle of a |
921 // query to allow TearDown to delete the directory. | 927 // query to allow TearDown to delete the directory. |
922 file_util::FileEnumerator f6(test_dir_.ToWStringHack(), true, | 928 file_util::FileEnumerator f6(test_dir_.ToWStringHack(), true, |
923 file_util::FileEnumerator::FILES_AND_DIRECTORIES); | 929 file_util::FileEnumerator::FILES_AND_DIRECTORIES); |
924 EXPECT_FALSE(f6.Next().empty()); // Should have found something | 930 EXPECT_FALSE(f6.Next().empty()); // Should have found something |
925 // (we don't care what). | 931 // (we don't care what). |
926 } | 932 } |
927 | 933 |
| 934 |
| 935 void PathComponents(const std::wstring& path, |
| 936 std::vector<std::wstring>* components) { |
| 937 DCHECK(components != NULL); |
| 938 if (components == NULL) |
| 939 return; |
| 940 std::wstring::size_type start = 0; |
| 941 std::wstring::size_type end = path.find('/', start); |
| 942 |
| 943 // Special case the "/" or "\" directory. On Windows with a drive letter, |
| 944 // this code path won't hit, but the right thing should still happen. |
| 945 // "E:\foo" will turn into "E:","foo". |
| 946 if (end == start) { |
| 947 components->push_back(std::wstring(path, 0, 1)); |
| 948 start = end + 1; |
| 949 end = path.find('/', start); |
| 950 } |
| 951 while (end != std::wstring::npos) { |
| 952 std::wstring component = std::wstring(path, start, end - start); |
| 953 components->push_back(component); |
| 954 start = end + 1; |
| 955 end = path.find('/', start); |
| 956 } |
| 957 std::wstring component = std::wstring(path, start); |
| 958 components->push_back(component); |
| 959 } |
| 960 |
| 961 static const struct PathComponentsCase { |
| 962 std::wstring path; |
| 963 FilePath::StringType result; |
| 964 } kPathComponents[] = { |
| 965 {L"/foo/bar/baz/", FILE_PATH_LITERAL("/|foo|bar|baz|")}, |
| 966 {L"/foo/bar/baz", FILE_PATH_LITERAL("/|foo|bar|baz")}, |
| 967 {L"e:/foo", FILE_PATH_LITERAL("e:|foo")}, |
| 968 }; |
| 969 |
| 970 TEST_F(FileUtilTest, PathComponentsTest) { |
| 971 for (size_t i = 0; i < arraysize(kPathComponents); ++i) { |
| 972 FilePath path = FilePath::FromWStringHack(kPathComponents[i].path); |
| 973 std::vector<FilePath::StringType> comps; |
| 974 file_util::PathComponents(path, &comps); |
| 975 |
| 976 FilePath::StringType result; |
| 977 for (size_t j = 0; j < comps.size(); ++j) { |
| 978 result.append(comps[j]); |
| 979 if (j < comps.size() - 1) |
| 980 result.append(FILE_PATH_LITERAL("|"), 1); |
| 981 } |
| 982 EXPECT_EQ(kPathComponents[i].result, result); |
| 983 } |
| 984 } |
| 985 |
928 } // namespace | 986 } // namespace |
OLD | NEW |