Skip to content

Commit 71d0016

Browse files
committed
ValidateSuggestedFileName
1 parent dd2c623 commit 71d0016

File tree

4 files changed

+51
-28
lines changed

4 files changed

+51
-28
lines changed

dev/Interop/StoragePickers/FileSavePicker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
9090
}
9191
void FileSavePicker::SuggestedFileName(hstring const& value)
9292
{
93+
PickerCommon::ValidateSuggestedFileName(value);
9394
m_suggestedFileName = value;
9495
}
9596

dev/Interop/StoragePickers/PickerCommon.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ namespace PickerCommon {
179179
}
180180
}
181181

182-
void ValidateSingleFileTypeFilterElement(winrt::hstring filter)
182+
void ValidateSingleFileTypeFilterElement(winrt::hstring const& filter)
183183
{
184184
if (filter.empty() || (filter[0] != L'.' && filter != L"*"))
185185
{
@@ -197,28 +197,6 @@ namespace PickerCommon {
197197
}
198198
}
199199

200-
//void ValidateFileTypeFilter(winrt::Windows::Foundation::Collections::IVectorView<winrt::hstring> filters)
201-
//{
202-
// if (filters.Size() > 0)
203-
// {
204-
// for (const auto& filter : filters)
205-
// {
206-
// ValidateSingleFileTypeFilterElement(filter);
207-
// }
208-
// }
209-
//}
210-
211-
//void ValidateFileTypeChoices(winrt::Windows::Foundation::Collections::IMapView<winrt::hstring, winrt::Windows::Foundation::Collections::IVector<winrt::hstring>> filters)
212-
//{
213-
// if (filters.Size() > 0)
214-
// {
215-
// for (const auto& filter : filters)
216-
// {
217-
// ValidateFileTypeFilter(filter.Value().GetView());
218-
// }
219-
// }
220-
//}
221-
222200
void ValidateSuggestedFileName(winrt::hstring const& suggestedFileName)
223201
{
224202
if (suggestedFileName.size() > MAX_PATH)

dev/Interop/StoragePickers/PickerCommon.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ namespace PickerCommon {
1919

2020
void ValidateViewMode(winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode const& value);
2121
void ValidateSuggestedStartLocation(winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId const& value);
22-
void ValidateSingleFileTypeFilterElement(winrt::hstring filter);
23-
//void ValidateFileTypeFilter(winrt::Windows::Foundation::Collections::IVectorView<winrt::hstring> filters);
24-
//void ValidateFileTypeChoices(winrt::Windows::Foundation::Collections::IMapView<winrt::hstring, winrt::Windows::Foundation::Collections::IVector<winrt::hstring>> filters);
22+
void ValidateSingleFileTypeFilterElement(winrt::hstring const& filter);
2523
void ValidateSuggestedFileName(winrt::hstring const& suggestedFileName);
2624

2725
struct PickerParameters {

test/StoragePickersTests/PickerCommonTests.cpp

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ namespace Test::PickerCommonTests
348348
{1, true}, // ComputerFolder
349349
{2, true}, // Desktop
350350
{3, true}, // Downloads
351-
{4, false}, // HomeGroup is removed.
351+
{4, false}, // HomeGroup is removed, should return false.
352352
{5, true}, // MusicLibrary
353353
{6, true}, // PicturesLibrary
354354
{7, true}, // VideosLibrary
@@ -387,6 +387,50 @@ namespace Test::PickerCommonTests
387387
}
388388
}
389389

390+
TEST_METHOD(VerifySuggestedFileName)
391+
{
392+
// Arrange.
393+
auto test_cases = std::vector<std::tuple<winrt::hstring, bool>>{
394+
{L"validFileName.txt", true}, // Valid file name
395+
{L"in\0valid.txt", false}, // embedded null
396+
{L"", true}, // Allow Empty string
397+
{L"validFileNameWithSpaces .txt", true},// Allow spaces
398+
{
399+
L"too_long_file_name_that_exceeds_the_maximum_length_of_a_file_name_usually_260_characters_too_long_file_name_that_exceeds_the_maximum_length_of_a_file_name_usually_260_characters_too_long_file_name_that_exceeds_the_maximum_length_of_a_file_name_usually_260_characters.txt",
400+
false, // File name too long
401+
},
402+
};
403+
404+
winrt::Microsoft::UI::WindowId windowId{};
405+
winrt::Microsoft::Windows::Storage::Pickers::FileSavePicker picker(windowId);
406+
407+
// Act & Assert
408+
for (const auto& test_case : test_cases)
409+
{
410+
winrt::hstring suggestedFileName = std::get<0>(test_case);
411+
bool expectValid = std::get<1>(test_case);
412+
if (expectValid)
413+
{
414+
picker.SuggestedFileName(suggestedFileName);
415+
VERIFY_ARE_EQUAL(picker.SuggestedFileName(), suggestedFileName);
416+
}
417+
else
418+
{
419+
try
420+
{
421+
picker.SuggestedFileName(suggestedFileName);
422+
423+
std::wstring errorMessage = L"Expected exception for invalid suggested file name: " + std::wstring(suggestedFileName);
424+
VERIFY_FAIL(errorMessage.c_str());
425+
}
426+
catch (...)
427+
{
428+
// Expected exception for invalid suggested file name
429+
}
430+
}
431+
}
432+
}
433+
390434
std::vector<std::tuple<winrt::hstring, bool>> file_extension_validation_test_cases{
391435
{L".txt", true},
392436
{L"*", true}, // One asterisk is valid
@@ -412,7 +456,9 @@ namespace Test::PickerCommonTests
412456
try
413457
{
414458
PickerCommon::ValidateSingleFileTypeFilterElement(filter);
415-
VERIFY_FAIL(L"Expected exception for invalid single file type filter element");
459+
460+
std::wstring errorMessage = L"Expected exception for invalid single file type filter element: " + std::wstring(filter);
461+
VERIFY_FAIL(errorMessage.c_str());
416462
}
417463
catch (...)
418464
{

0 commit comments

Comments
 (0)