| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/base/win/open_file_name_win.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace { |
| 11 const HWND kHwnd = reinterpret_cast<HWND>(0xDEADBEEF); |
| 12 const DWORD kFlags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLESIZING; |
| 13 |
| 14 void SetResult(const base::string16& result, ui::win::OpenFileName* ofn) { |
| 15 EXPECT_GT(ofn->Get()->nMaxFile, result.size()); |
| 16 if (ofn->Get()->nMaxFile > result.size()) { |
| 17 if (!result.size()) { |
| 18 ofn->Get()->lpstrFile[0] = 0; |
| 19 } else { |
| 20 // Because the result has embedded nulls, we must memcpy. |
| 21 memcpy(ofn->Get()->lpstrFile, |
| 22 result.c_str(), |
| 23 (result.size() + 1) * sizeof(result[0])); |
| 24 } |
| 25 } |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
| 30 TEST(OpenFileNameTest, Initialization) { |
| 31 ui::win::OpenFileName ofn(kHwnd, kFlags); |
| 32 EXPECT_EQ(kHwnd, ofn.Get()->hwndOwner); |
| 33 EXPECT_EQ(kFlags, ofn.Get()->Flags); |
| 34 EXPECT_EQ(sizeof(OPENFILENAME), ofn.Get()->lStructSize); |
| 35 ASSERT_TRUE(ofn.Get()->lpstrFile); |
| 36 ASSERT_GT(ofn.Get()->nMaxFile, 0u); |
| 37 EXPECT_EQ(0, ofn.Get()->lpstrFile[0]); |
| 38 } |
| 39 |
| 40 TEST(OpenFileNameTest, SetInitialSelection) { |
| 41 const base::FilePath kDirectory(L"C:\\directory\\child_directory"); |
| 42 const base::FilePath kFile(L"file_name.ext"); |
| 43 ui::win::OpenFileName ofn(kHwnd, kFlags); |
| 44 ofn.SetInitialSelection(kDirectory, kFile); |
| 45 EXPECT_EQ(kDirectory, base::FilePath(ofn.Get()->lpstrInitialDir)); |
| 46 EXPECT_EQ(kFile, base::FilePath(ofn.Get()->lpstrFile)); |
| 47 |
| 48 ofn.SetInitialSelection(kDirectory, base::FilePath()); |
| 49 EXPECT_EQ(kDirectory, base::FilePath(ofn.Get()->lpstrInitialDir)); |
| 50 // Filename buffer will still be a valid pointer, to receive a result. |
| 51 ASSERT_TRUE(ofn.Get()->lpstrFile); |
| 52 EXPECT_EQ(base::FilePath(), base::FilePath(ofn.Get()->lpstrFile)); |
| 53 |
| 54 ofn.SetInitialSelection(base::FilePath(), base::FilePath()); |
| 55 // No initial directory will lead to a NULL buffer. |
| 56 ASSERT_FALSE(ofn.Get()->lpstrInitialDir); |
| 57 ASSERT_TRUE(ofn.Get()->lpstrFile); |
| 58 EXPECT_EQ(base::FilePath(), base::FilePath(ofn.Get()->lpstrFile)); |
| 59 |
| 60 // Make sure that both values are cleared when directory is missing. |
| 61 ofn.SetInitialSelection(kDirectory, kFile); |
| 62 EXPECT_EQ(kDirectory, base::FilePath(ofn.Get()->lpstrInitialDir)); |
| 63 EXPECT_EQ(kFile, base::FilePath(ofn.Get()->lpstrFile)); |
| 64 ofn.SetInitialSelection(base::FilePath(), base::FilePath()); |
| 65 ASSERT_FALSE(ofn.Get()->lpstrInitialDir); |
| 66 ASSERT_TRUE(ofn.Get()->lpstrFile); |
| 67 EXPECT_EQ(base::FilePath(), base::FilePath(ofn.Get()->lpstrFile)); |
| 68 |
| 69 // File is ignored in absence of a directory. |
| 70 ofn.SetInitialSelection(base::FilePath(), kFile); |
| 71 ASSERT_FALSE(ofn.Get()->lpstrInitialDir); |
| 72 ASSERT_TRUE(ofn.Get()->lpstrFile); |
| 73 EXPECT_EQ(base::FilePath(), base::FilePath(ofn.Get()->lpstrFile)); |
| 74 } |
| 75 |
| 76 TEST(OpenFileNameTest, GetSingleResult) { |
| 77 const base::string16 kNull(L"\0", 1); |
| 78 ui::win::OpenFileName ofn(kHwnd, kFlags); |
| 79 base::FilePath result; |
| 80 |
| 81 SetResult(L"C:\\dir\\file" + kNull, &ofn); |
| 82 result = ofn.GetSingleResult(); |
| 83 EXPECT_EQ(base::FilePath(L"C:\\dir\\file"), result); |
| 84 |
| 85 SetResult(L"C:\\dir"+ kNull + L"file" + kNull, &ofn); |
| 86 result = ofn.GetSingleResult(); |
| 87 EXPECT_EQ(base::FilePath(L"C:\\dir\\file"), result); |
| 88 |
| 89 SetResult(L"C:\\dir"+ kNull + L"file"+ kNull + L"otherfile"+ kNull, &ofn); |
| 90 result = ofn.GetSingleResult(); |
| 91 EXPECT_EQ(base::FilePath(), result); |
| 92 |
| 93 SetResult(L"", &ofn); |
| 94 result = ofn.GetSingleResult(); |
| 95 EXPECT_EQ(base::FilePath(), result); |
| 96 } |
| 97 |
| 98 TEST(OpenFileNameTest, GetResult) { |
| 99 const base::string16 kNull(L"\0", 1); |
| 100 |
| 101 ui::win::OpenFileName ofn(kHwnd, kFlags); |
| 102 base::FilePath directory; |
| 103 std::vector<base::FilePath> filenames; |
| 104 |
| 105 SetResult(L"C:\\dir\\file" + kNull, &ofn); |
| 106 ofn.GetResult(&directory, &filenames); |
| 107 EXPECT_EQ(base::FilePath(L"C:\\dir"), directory); |
| 108 ASSERT_EQ(1u, filenames.size()); |
| 109 EXPECT_EQ(base::FilePath(L"file"), filenames[0]); |
| 110 |
| 111 directory.clear(); |
| 112 filenames.clear(); |
| 113 |
| 114 SetResult(L"C:\\dir" + kNull + L"file" + kNull, &ofn); |
| 115 ofn.GetResult(&directory, &filenames); |
| 116 EXPECT_EQ(base::FilePath(L"C:\\dir"), directory); |
| 117 ASSERT_EQ(1u, filenames.size()); |
| 118 EXPECT_EQ(base::FilePath(L"file"), filenames[0]); |
| 119 |
| 120 directory.clear(); |
| 121 filenames.clear(); |
| 122 |
| 123 SetResult(L"C:\\dir" + kNull + L"file" + kNull + L"otherfile" + kNull, &ofn); |
| 124 ofn.GetResult(&directory, &filenames); |
| 125 EXPECT_EQ(base::FilePath(L"C:\\dir"), directory); |
| 126 ASSERT_EQ(2u, filenames.size()); |
| 127 EXPECT_EQ(base::FilePath(L"file"), filenames[0]); |
| 128 EXPECT_EQ(base::FilePath(L"otherfile"), filenames[1]); |
| 129 |
| 130 directory.clear(); |
| 131 filenames.clear(); |
| 132 |
| 133 SetResult(L"", &ofn); |
| 134 ofn.GetResult(&directory, &filenames); |
| 135 EXPECT_EQ(base::FilePath(), directory); |
| 136 ASSERT_EQ(0u, filenames.size()); |
| 137 } |
| OLD | NEW |