| 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 if (ofn->GetOPENFILENAME()->nMaxFile <= result.size()) { | 15 if (ofn->GetOPENFILENAME()->nMaxFile <= result.size()) { |
| 16 ADD_FAILURE() << "filename buffer insufficient."; | 16 ADD_FAILURE() << "filename buffer insufficient."; |
| 17 return; | 17 return; |
| 18 } | 18 } |
| 19 if (!result.size()) { | 19 if (!result.size()) { |
| 20 ofn->GetOPENFILENAME()->lpstrFile[0] = 0; | 20 ofn->GetOPENFILENAME()->lpstrFile[0] = 0; |
| 21 } else { | 21 } else { |
| 22 // Because the result has embedded nulls, we must memcpy. | 22 // Because the result has embedded nulls, we must memcpy. |
| 23 memcpy(ofn->GetOPENFILENAME()->lpstrFile, | 23 memcpy(ofn->GetOPENFILENAME()->lpstrFile, |
| 24 result.c_str(), | 24 result.c_str(), |
| 25 (result.size() + 1) * sizeof(result[0])); | 25 (result.size() + 1) * sizeof(result[0])); |
| 26 } | 26 } |
| 27 } | 27 } |
| 28 | 28 |
| 29 void CheckFilters( | 29 void CheckFilters( |
| 30 const std::vector<Tuple2<base::string16, base::string16> >& expected, | 30 const std::vector<Tuple<base::string16, base::string16>>& expected, |
| 31 const std::vector<Tuple2<base::string16, base::string16> >& actual) { | 31 const std::vector<Tuple<base::string16, base::string16>>& actual) { |
| 32 if (expected.size() != actual.size()) { | 32 if (expected.size() != actual.size()) { |
| 33 ADD_FAILURE() << "filter count mismatch. Got " << actual.size() | 33 ADD_FAILURE() << "filter count mismatch. Got " << actual.size() |
| 34 << " expected " << expected.size() << "."; | 34 << " expected " << expected.size() << "."; |
| 35 return; | 35 return; |
| 36 } | 36 } |
| 37 | 37 |
| 38 for (size_t i = 0; i < expected.size(); ++i) { | 38 for (size_t i = 0; i < expected.size(); ++i) { |
| 39 EXPECT_EQ(expected[i].a, actual[i].a) << "Mismatch at index " << i; | 39 EXPECT_EQ(get<0>(expected[i]), get<0>(actual[i])) |
| 40 EXPECT_EQ(expected[i].b, actual[i].b) << "Mismatch at index " << i; | 40 << "Mismatch at index " << i; |
| 41 EXPECT_EQ(get<1>(expected[i]), get<1>(actual[i])) |
| 42 << "Mismatch at index " << i; |
| 41 } | 43 } |
| 42 } | 44 } |
| 43 | 45 |
| 44 void CheckFilterString(const base::string16& expected, | 46 void CheckFilterString(const base::string16& expected, |
| 45 const ui::win::OpenFileName& ofn) { | 47 const ui::win::OpenFileName& ofn) { |
| 46 if (!ofn.GetOPENFILENAME()->lpstrFilter) { | 48 if (!ofn.GetOPENFILENAME()->lpstrFilter) { |
| 47 ADD_FAILURE() << "Filter string is NULL."; | 49 ADD_FAILURE() << "Filter string is NULL."; |
| 48 return; | 50 return; |
| 49 } | 51 } |
| 50 if (expected.size() == 0) { | 52 if (expected.size() == 0) { |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 SetResult(L"", &ofn); | 196 SetResult(L"", &ofn); |
| 195 ofn.GetResult(&directory, &filenames); | 197 ofn.GetResult(&directory, &filenames); |
| 196 EXPECT_EQ(base::FilePath(), directory); | 198 EXPECT_EQ(base::FilePath(), directory); |
| 197 ASSERT_EQ(0u, filenames.size()); | 199 ASSERT_EQ(0u, filenames.size()); |
| 198 } | 200 } |
| 199 | 201 |
| 200 TEST(OpenFileNameTest, SetAndGetFilters) { | 202 TEST(OpenFileNameTest, SetAndGetFilters) { |
| 201 const base::string16 kNull(L"\0", 1); | 203 const base::string16 kNull(L"\0", 1); |
| 202 | 204 |
| 203 ui::win::OpenFileName ofn(kHwnd, kFlags); | 205 ui::win::OpenFileName ofn(kHwnd, kFlags); |
| 204 std::vector<Tuple2<base::string16, base::string16> > filters; | 206 std::vector<Tuple<base::string16, base::string16>> filters; |
| 205 ofn.SetFilters(filters); | 207 ofn.SetFilters(filters); |
| 206 EXPECT_FALSE(ofn.GetOPENFILENAME()->lpstrFilter); | 208 EXPECT_FALSE(ofn.GetOPENFILENAME()->lpstrFilter); |
| 207 CheckFilters(filters, | 209 CheckFilters(filters, |
| 208 ui::win::OpenFileName::GetFilters(ofn.GetOPENFILENAME())); | 210 ui::win::OpenFileName::GetFilters(ofn.GetOPENFILENAME())); |
| 209 | 211 |
| 210 filters.push_back(MakeTuple(base::string16(L"a"), base::string16(L"b"))); | 212 filters.push_back(MakeTuple(base::string16(L"a"), base::string16(L"b"))); |
| 211 ofn.SetFilters(filters); | 213 ofn.SetFilters(filters); |
| 212 CheckFilterString(L"a" + kNull + L"b" + kNull, ofn); | 214 CheckFilterString(L"a" + kNull + L"b" + kNull, ofn); |
| 213 CheckFilters(filters, | 215 CheckFilters(filters, |
| 214 ui::win::OpenFileName::GetFilters(ofn.GetOPENFILENAME())); | 216 ui::win::OpenFileName::GetFilters(ofn.GetOPENFILENAME())); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 240 ui::win::OpenFileName::SetResult(directory, filenames, ofn.GetOPENFILENAME()); | 242 ui::win::OpenFileName::SetResult(directory, filenames, ofn.GetOPENFILENAME()); |
| 241 CheckResult(L"C:\\dir" + kNull + L"file" + kNull + L"otherfile" + kNull, ofn); | 243 CheckResult(L"C:\\dir" + kNull + L"file" + kNull + L"otherfile" + kNull, ofn); |
| 242 | 244 |
| 243 base::char16 short_buffer[10] = L""; | 245 base::char16 short_buffer[10] = L""; |
| 244 | 246 |
| 245 ofn.GetOPENFILENAME()->lpstrFile = short_buffer; | 247 ofn.GetOPENFILENAME()->lpstrFile = short_buffer; |
| 246 ofn.GetOPENFILENAME()->nMaxFile = arraysize(short_buffer); | 248 ofn.GetOPENFILENAME()->nMaxFile = arraysize(short_buffer); |
| 247 ui::win::OpenFileName::SetResult(directory, filenames, ofn.GetOPENFILENAME()); | 249 ui::win::OpenFileName::SetResult(directory, filenames, ofn.GetOPENFILENAME()); |
| 248 CheckResult(L"", ofn); | 250 CheckResult(L"", ofn); |
| 249 } | 251 } |
| OLD | NEW |