| 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<Tuple<base::string16, base::string16>>& expected, | 30 const std::vector<base::Tuple<base::string16, base::string16>>& expected, |
| 31 const std::vector<Tuple<base::string16, base::string16>>& actual) { | 31 const std::vector<base::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(get<0>(expected[i]), get<0>(actual[i])) | 39 EXPECT_EQ(base::get<0>(expected[i]), base::get<0>(actual[i])) |
| 40 << "Mismatch at index " << i; | 40 << "Mismatch at index " << i; |
| 41 EXPECT_EQ(get<1>(expected[i]), get<1>(actual[i])) | 41 EXPECT_EQ(base::get<1>(expected[i]), base::get<1>(actual[i])) |
| 42 << "Mismatch at index " << i; | 42 << "Mismatch at index " << i; |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 | 45 |
| 46 void CheckFilterString(const base::string16& expected, | 46 void CheckFilterString(const base::string16& expected, |
| 47 const ui::win::OpenFileName& ofn) { | 47 const ui::win::OpenFileName& ofn) { |
| 48 if (!ofn.GetOPENFILENAME()->lpstrFilter) { | 48 if (!ofn.GetOPENFILENAME()->lpstrFilter) { |
| 49 ADD_FAILURE() << "Filter string is NULL."; | 49 ADD_FAILURE() << "Filter string is NULL."; |
| 50 return; | 50 return; |
| 51 } | 51 } |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 SetResult(L"", &ofn); | 196 SetResult(L"", &ofn); |
| 197 ofn.GetResult(&directory, &filenames); | 197 ofn.GetResult(&directory, &filenames); |
| 198 EXPECT_EQ(base::FilePath(), directory); | 198 EXPECT_EQ(base::FilePath(), directory); |
| 199 ASSERT_EQ(0u, filenames.size()); | 199 ASSERT_EQ(0u, filenames.size()); |
| 200 } | 200 } |
| 201 | 201 |
| 202 TEST(OpenFileNameTest, SetAndGetFilters) { | 202 TEST(OpenFileNameTest, SetAndGetFilters) { |
| 203 const base::string16 kNull(L"\0", 1); | 203 const base::string16 kNull(L"\0", 1); |
| 204 | 204 |
| 205 ui::win::OpenFileName ofn(kHwnd, kFlags); | 205 ui::win::OpenFileName ofn(kHwnd, kFlags); |
| 206 std::vector<Tuple<base::string16, base::string16>> filters; | 206 std::vector<base::Tuple<base::string16, base::string16>> filters; |
| 207 ofn.SetFilters(filters); | 207 ofn.SetFilters(filters); |
| 208 EXPECT_FALSE(ofn.GetOPENFILENAME()->lpstrFilter); | 208 EXPECT_FALSE(ofn.GetOPENFILENAME()->lpstrFilter); |
| 209 CheckFilters(filters, | 209 CheckFilters(filters, |
| 210 ui::win::OpenFileName::GetFilters(ofn.GetOPENFILENAME())); | 210 ui::win::OpenFileName::GetFilters(ofn.GetOPENFILENAME())); |
| 211 | 211 |
| 212 filters.push_back(MakeTuple(base::string16(L"a"), base::string16(L"b"))); | 212 filters.push_back(base::MakeTuple(base::string16(L"a"), base::string16(L"b")))
; |
| 213 ofn.SetFilters(filters); | 213 ofn.SetFilters(filters); |
| 214 CheckFilterString(L"a" + kNull + L"b" + kNull, ofn); | 214 CheckFilterString(L"a" + kNull + L"b" + kNull, ofn); |
| 215 CheckFilters(filters, | 215 CheckFilters(filters, |
| 216 ui::win::OpenFileName::GetFilters(ofn.GetOPENFILENAME())); | 216 ui::win::OpenFileName::GetFilters(ofn.GetOPENFILENAME())); |
| 217 | 217 |
| 218 filters.push_back(MakeTuple(base::string16(L"X"), base::string16(L"Y"))); | 218 filters.push_back(base::MakeTuple(base::string16(L"X"), base::string16(L"Y")))
; |
| 219 ofn.SetFilters(filters); | 219 ofn.SetFilters(filters); |
| 220 CheckFilterString(L"a" + kNull + L"b" + kNull + L"X" + kNull + L"Y" + kNull, | 220 CheckFilterString(L"a" + kNull + L"b" + kNull + L"X" + kNull + L"Y" + kNull, |
| 221 ofn); | 221 ofn); |
| 222 CheckFilters(filters, | 222 CheckFilters(filters, |
| 223 ui::win::OpenFileName::GetFilters(ofn.GetOPENFILENAME())); | 223 ui::win::OpenFileName::GetFilters(ofn.GetOPENFILENAME())); |
| 224 } | 224 } |
| 225 | 225 |
| 226 TEST(OpenFileNameTest, SetResult) { | 226 TEST(OpenFileNameTest, SetResult) { |
| 227 const base::string16 kNull(L"\0", 1); | 227 const base::string16 kNull(L"\0", 1); |
| 228 | 228 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 242 ui::win::OpenFileName::SetResult(directory, filenames, ofn.GetOPENFILENAME()); | 242 ui::win::OpenFileName::SetResult(directory, filenames, ofn.GetOPENFILENAME()); |
| 243 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); |
| 244 | 244 |
| 245 base::char16 short_buffer[10] = L""; | 245 base::char16 short_buffer[10] = L""; |
| 246 | 246 |
| 247 ofn.GetOPENFILENAME()->lpstrFile = short_buffer; | 247 ofn.GetOPENFILENAME()->lpstrFile = short_buffer; |
| 248 ofn.GetOPENFILENAME()->nMaxFile = arraysize(short_buffer); | 248 ofn.GetOPENFILENAME()->nMaxFile = arraysize(short_buffer); |
| 249 ui::win::OpenFileName::SetResult(directory, filenames, ofn.GetOPENFILENAME()); | 249 ui::win::OpenFileName::SetResult(directory, filenames, ofn.GetOPENFILENAME()); |
| 250 CheckResult(L"", ofn); | 250 CheckResult(L"", ofn); |
| 251 } | 251 } |
| OLD | NEW |