OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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 "ui/base/win/open_file_name_win.h" | 5 #include "ui/base/win/open_file_name_win.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace { | 10 namespace { |
11 const HWND kHwnd = reinterpret_cast<HWND>(0xDEADBEEF); | 11 const HWND kHwnd = reinterpret_cast<HWND>(0xDEADBEEF); |
12 const DWORD kFlags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING; | 12 const DWORD kFlags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING; |
13 | 13 |
14 void SetResult(const base::string16& result, ui::win::OpenFileName* ofn) { | 14 void SetResult(const base::string16& result, ui::win::OpenFileName* ofn) { |
15 EXPECT_GT(ofn->Get()->nMaxFile, result.size()); | 15 if (ofn->Get()->nMaxFile <= result.size()) { |
16 if (ofn->Get()->nMaxFile > result.size()) { | 16 ADD_FAILURE() << "filename buffer insufficient."; |
17 if (!result.size()) { | 17 return; |
18 ofn->Get()->lpstrFile[0] = 0; | 18 } |
19 } else { | 19 if (!result.size()) { |
20 // Because the result has embedded nulls, we must memcpy. | 20 ofn->Get()->lpstrFile[0] = 0; |
21 memcpy(ofn->Get()->lpstrFile, | 21 } else { |
22 result.c_str(), | 22 // Because the result has embedded nulls, we must memcpy. |
23 (result.size() + 1) * sizeof(result[0])); | 23 memcpy(ofn->Get()->lpstrFile, |
24 } | 24 result.c_str(), |
| 25 (result.size() + 1) * sizeof(result[0])); |
25 } | 26 } |
26 } | 27 } |
27 | 28 |
| 29 void CheckFilters( |
| 30 const std::vector<Tuple2<base::string16, base::string16> >& expected, |
| 31 const std::vector<Tuple2<base::string16, base::string16> >& actual) { |
| 32 if (expected.size() != actual.size()) { |
| 33 ADD_FAILURE() << "filter count mismatch. Got " << actual.size() |
| 34 << " expected " << expected.size() << "."; |
| 35 return; |
| 36 } |
| 37 |
| 38 for (size_t i = 0; i < expected.size(); ++i) { |
| 39 EXPECT_EQ(expected[i].a, actual[i].a) << "Mismatch at index " << i; |
| 40 EXPECT_EQ(expected[i].b, actual[i].b) << "Mismatch at index " << i; |
| 41 } |
| 42 } |
| 43 |
| 44 void CheckFilterString(const base::string16& expected, |
| 45 const ui::win::OpenFileName& ofn) { |
| 46 if (!ofn.Get()->lpstrFilter) { |
| 47 ADD_FAILURE() << "Filter string is NULL."; |
| 48 return; |
| 49 } |
| 50 if (expected.size() == 0) { |
| 51 EXPECT_EQ(0, ofn.Get()->lpstrFilter[0]); |
| 52 } else { |
| 53 EXPECT_EQ(0, |
| 54 memcmp(expected.c_str(), |
| 55 ofn.Get()->lpstrFilter, |
| 56 expected.size() + 1 * sizeof(expected[0]))); |
| 57 } |
| 58 } |
| 59 |
| 60 void CheckResult(const base::string16& expected, |
| 61 const ui::win::OpenFileName& ofn) { |
| 62 if (!ofn.Get()->lpstrFile) { |
| 63 ADD_FAILURE() << "File string is NULL."; |
| 64 return; |
| 65 } |
| 66 if (expected.size() == 0) { |
| 67 EXPECT_EQ(0, ofn.Get()->lpstrFile[0]); |
| 68 } else { |
| 69 EXPECT_EQ(0, |
| 70 memcmp(expected.c_str(), |
| 71 ofn.Get()->lpstrFile, |
| 72 expected.size() + 1 * sizeof(expected[0]))); |
| 73 } |
| 74 } |
| 75 |
28 } // namespace | 76 } // namespace |
29 | 77 |
30 TEST(OpenFileNameTest, Initialization) { | 78 TEST(OpenFileNameTest, Initialization) { |
31 ui::win::OpenFileName ofn(kHwnd, kFlags); | 79 ui::win::OpenFileName ofn(kHwnd, kFlags); |
32 EXPECT_EQ(kHwnd, ofn.Get()->hwndOwner); | 80 EXPECT_EQ(kHwnd, ofn.Get()->hwndOwner); |
33 EXPECT_EQ(kFlags, ofn.Get()->Flags); | 81 EXPECT_EQ(kFlags, ofn.Get()->Flags); |
34 EXPECT_EQ(sizeof(OPENFILENAME), ofn.Get()->lStructSize); | 82 EXPECT_EQ(sizeof(OPENFILENAME), ofn.Get()->lStructSize); |
35 ASSERT_TRUE(ofn.Get()->lpstrFile); | 83 ASSERT_TRUE(ofn.Get()->lpstrFile); |
36 ASSERT_GT(ofn.Get()->nMaxFile, 0u); | 84 ASSERT_GT(ofn.Get()->nMaxFile, 0u); |
37 EXPECT_EQ(0, ofn.Get()->lpstrFile[0]); | 85 EXPECT_EQ(0, ofn.Get()->lpstrFile[0]); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 EXPECT_EQ(base::FilePath(L"otherfile"), filenames[1]); | 176 EXPECT_EQ(base::FilePath(L"otherfile"), filenames[1]); |
129 | 177 |
130 directory.clear(); | 178 directory.clear(); |
131 filenames.clear(); | 179 filenames.clear(); |
132 | 180 |
133 SetResult(L"", &ofn); | 181 SetResult(L"", &ofn); |
134 ofn.GetResult(&directory, &filenames); | 182 ofn.GetResult(&directory, &filenames); |
135 EXPECT_EQ(base::FilePath(), directory); | 183 EXPECT_EQ(base::FilePath(), directory); |
136 ASSERT_EQ(0u, filenames.size()); | 184 ASSERT_EQ(0u, filenames.size()); |
137 } | 185 } |
| 186 |
| 187 TEST(OpenFileNameTest, SetAndGetFilters) { |
| 188 const base::string16 kNull(L"\0", 1); |
| 189 |
| 190 ui::win::OpenFileName ofn(kHwnd, kFlags); |
| 191 std::vector<Tuple2<base::string16, base::string16> > filters; |
| 192 ofn.SetFilters(filters); |
| 193 EXPECT_FALSE(ofn.Get()->lpstrFilter); |
| 194 CheckFilters(filters, ui::win::OpenFileName::GetFilters(ofn.Get())); |
| 195 |
| 196 filters.push_back(MakeTuple(base::string16(L"a"), base::string16(L"b"))); |
| 197 ofn.SetFilters(filters); |
| 198 CheckFilterString(L"a" + kNull + L"b" + kNull, ofn); |
| 199 CheckFilters(filters, ui::win::OpenFileName::GetFilters(ofn.Get())); |
| 200 |
| 201 filters.push_back(MakeTuple(base::string16(L"X"), base::string16(L"Y"))); |
| 202 ofn.SetFilters(filters); |
| 203 CheckFilterString(L"a" + kNull + L"b" + kNull + L"X" + kNull + L"Y" + kNull, |
| 204 ofn); |
| 205 CheckFilters(filters, ui::win::OpenFileName::GetFilters(ofn.Get())); |
| 206 } |
| 207 |
| 208 TEST(OpenFileNameTest, SetResult) { |
| 209 const base::string16 kNull(L"\0", 1); |
| 210 |
| 211 ui::win::OpenFileName ofn(kHwnd, kFlags); |
| 212 base::FilePath directory; |
| 213 std::vector<base::FilePath> filenames; |
| 214 |
| 215 ui::win::OpenFileName::SetResult(directory, filenames, ofn.Get()); |
| 216 CheckResult(L"", ofn); |
| 217 |
| 218 directory = base::FilePath(L"C:\\dir"); |
| 219 filenames.push_back(base::FilePath(L"file")); |
| 220 ui::win::OpenFileName::SetResult(directory, filenames, ofn.Get()); |
| 221 CheckResult(L"C:\\dir\\file" + kNull, ofn); |
| 222 |
| 223 filenames.push_back(base::FilePath(L"otherfile")); |
| 224 ui::win::OpenFileName::SetResult(directory, filenames, ofn.Get()); |
| 225 CheckResult(L"C:\\dir" + kNull + L"file" + kNull + L"otherfile" + kNull, ofn); |
| 226 } |
OLD | NEW |