Skip to content

Commit 548cbe6

Browse files
committed
tests
1 parent 60c0d78 commit 548cbe6

File tree

10 files changed

+237
-9
lines changed

10 files changed

+237
-9
lines changed

dev/Interop/StoragePickers/FileOpenPicker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
5959
}
6060
winrt::Windows::Foundation::Collections::IVector<hstring> FileOpenPicker::FileTypeFilter()
6161
{
62-
return m_fileTypeFilter;
62+
return m_fileTypeFilter.as<winrt::Windows::Foundation::Collections::IVector<hstring>>();
6363
}
6464

6565
void FileOpenPicker::OnFileTypeFilterChanged(

dev/Interop/StoragePickers/FileOpenPicker.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "Microsoft.Windows.Storage.Pickers.FileOpenPicker.g.h"
66
#include "PickerCommon.h"
77
#include "StoragePickersTelemetryHelper.h"
8+
#include <winrt/Windows.Foundation.Collections.h>
89

910
namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
1011
{
@@ -35,7 +36,11 @@ namespace winrt::Microsoft::Windows::Storage::Pickers::implementation
3536
winrt::hstring m_settingsIdentifier{};
3637
PickerLocationId m_suggestedStartLocation{ PickerLocationId::Unspecified };
3738
winrt::hstring m_commitButtonText{};
38-
winrt::Windows::Foundation::Collections::IVector<hstring> m_fileTypeFilter{ winrt::single_threaded_vector<hstring>() };
39+
40+
winrt::Windows::Foundation::Collections::IObservableVector<hstring> m_fileTypeFilter{
41+
winrt::single_threaded_observable_vector<hstring>()
42+
};
43+
3944
StoragePickersTelemetryHelper m_telemetryHelper{};
4045

4146
void CaptureParameters(PickerCommon::PickerParameters& parameters);

dev/Interop/StoragePickers/PickerCommon.cpp

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "pch.h"
55
#include "shellapi.h"
66
#include "PickerCommon.h"
7+
#include "PickerLocalization.h"
78
#include <wil/resource.h>
89
#include "ShObjIdl.h"
910
#include "shobjidl_core.h"
@@ -151,9 +152,10 @@ namespace PickerCommon {
151152
{
152153
case winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode::List:
153154
case winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode::Thumbnail:
155+
return;
154156
default:
155-
// TODO to xiaomgao: localization
156-
throw winrt::hresult_invalid_argument(L"IDS_APIERROR_INVALIDVIEWMODEVALUE");
157+
throw winrt::hresult_invalid_argument(
158+
PickerLocalization::GetStoragePickersLocalizationText(L"IDS_APIERROR_INVALIDVIEWMODEVALUE"));
157159
}
158160
}
159161

@@ -172,17 +174,26 @@ namespace PickerCommon {
172174
case winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId::Unspecified:
173175
return;
174176
default:
175-
// TODO to xiaomgao: localization
176-
throw winrt::hresult_invalid_argument(L"IDS_APIERROR_INVALIDSUGGESTEDSTARTLOCATIONVALUE");
177+
throw winrt::hresult_invalid_argument(
178+
PickerLocalization::GetStoragePickersLocalizationText(L"IDS_APIERROR_INVALIDSUGGESTEDSTARTLOCATIONVALUE"));
177179
}
178180
}
179181

180182
void ValidateSingleFileTypeFilterElement(winrt::hstring filter)
181183
{
182184
if (filter.empty() || (filter[0] != L'.' && filter != L"*"))
183185
{
184-
// TODO to xiaomgao: localization
185-
throw winrt::hresult_invalid_argument(L"IDS_APIERROR_IMPROPERFILEEXTENSION");
186+
throw winrt::hresult_invalid_argument(
187+
PickerLocalization::GetStoragePickersLocalizationText(L"IDS_APIERROR_IMPROPERFILEEXTENSION"));
188+
}
189+
190+
for (const auto& ch : filter)
191+
{
192+
if (ch == L'\0')
193+
{
194+
throw winrt::hresult_invalid_argument(
195+
PickerLocalization::GetStoragePickersLocalizationText(L"IDS_APIERROR_STRINGSNOEMBEDDEDNULLS"));
196+
}
186197
}
187198
}
188199

@@ -208,6 +219,24 @@ namespace PickerCommon {
208219
}
209220
}
210221

222+
void ValidateSuggestedFileName(winrt::hstring const& suggestedFileName)
223+
{
224+
if (suggestedFileName.size() > MAX_PATH)
225+
{
226+
throw winrt::hresult_invalid_argument(
227+
PickerLocalization::GetStoragePickersLocalizationText(L"IDS_APIERROR_MAXSAVEFILELENGTHEXCEEDED"));
228+
}
229+
230+
for (const auto& ch : suggestedFileName)
231+
{
232+
if (ch == L'\0')
233+
{
234+
throw winrt::hresult_invalid_argument(
235+
PickerLocalization::GetStoragePickersLocalizationText(L"IDS_APIERROR_STRINGSNOEMBEDDEDNULLS"));
236+
}
237+
}
238+
}
239+
211240
winrt::hstring PickerParameters::FormatExtensionWithWildcard(winrt::hstring extension)
212241
{
213242
if (!extension.empty() && extension[0] == L'*')

dev/Interop/StoragePickers/PickerCommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace PickerCommon {
2222
void ValidateSingleFileTypeFilterElement(winrt::hstring filter);
2323
void ValidateFileTypeFilter(winrt::Windows::Foundation::Collections::IVectorView<winrt::hstring> filters);
2424
void ValidateFileTypeChoices(winrt::Windows::Foundation::Collections::IMapView<winrt::hstring, winrt::Windows::Foundation::Collections::IVector<winrt::hstring>> filters);
25+
void ValidateSuggestedFileName(winrt::hstring const& suggestedFileName);
2526

2627
struct PickerParameters {
2728
HWND HWnd{};

dev/Interop/StoragePickers/PickerLocalization.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
#pragma once
5-
#include "PickerCommon.h"
65

76
namespace PickerLocalization {
87
winrt::hstring GetStoragePickersLocalizationText(winrt::hstring key);

test/StoragePickersTests/PickerCommonTests.cpp

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,5 +300,170 @@ namespace Test::PickerCommonTests
300300
}
301301

302302
}
303+
304+
TEST_METHOD(VerifyValidateViewMode)
305+
{
306+
// Arrange.
307+
std::vector<std::tuple<int, bool>> test_cases {
308+
{0, true}, // PickerViewMode::List
309+
{1, true}, // PickerViewMode::Thumbnail
310+
{2, false}, // An invalid value out of range
311+
{-1, false}, // Negative value
312+
{100, false} // An very large value out of range
313+
};
314+
winrt::Microsoft::UI::WindowId windowId{};
315+
winrt::Microsoft::Windows::Storage::Pickers::FileOpenPicker picker(windowId);
316+
317+
// Act & Assert
318+
for (const auto& test_case : test_cases)
319+
{
320+
int viewModeValue = std::get<0>(test_case);
321+
bool expectValid = std::get<1>(test_case);
322+
323+
if (expectValid)
324+
{
325+
picker.ViewMode((winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode)viewModeValue);
326+
VERIFY_ARE_EQUAL(picker.ViewMode(), (winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode)viewModeValue);
327+
}
328+
else
329+
{
330+
try
331+
{
332+
picker.ViewMode((winrt::Microsoft::Windows::Storage::Pickers::PickerViewMode)viewModeValue);
333+
VERIFY_FAIL(L"Expected exception for invalid view mode");
334+
}
335+
catch (...)
336+
{
337+
// Expected exception for invalid view mode
338+
}
339+
}
340+
}
341+
}
342+
343+
TEST_METHOD(VerifyValidateSuggestedStartLocation)
344+
{
345+
// Arrange.
346+
std::vector<std::tuple<int, bool>> test_cases {
347+
{0, true}, // DocumentsLibrary
348+
{1, true}, // ComputerFolder
349+
{2, true}, // Desktop
350+
{3, true}, // Downloads
351+
{4, false}, // HomeGroup is removed.
352+
{5, true}, // MusicLibrary
353+
{6, true}, // PicturesLibrary
354+
{7, true}, // VideosLibrary
355+
{8, true}, // Objects3D
356+
{9, true}, // Unspecified
357+
{10, false}, // An invalid value out of range
358+
{-1, false}, // Negative value
359+
{100, false}, // An very large value out of range
360+
};
361+
winrt::Microsoft::UI::WindowId windowId{};
362+
winrt::Microsoft::Windows::Storage::Pickers::FileOpenPicker picker(windowId);
363+
364+
// Act & Assert
365+
for (const auto& test_case : test_cases)
366+
{
367+
int locationValue = std::get<0>(test_case);
368+
bool expectValid = std::get<1>(test_case);
369+
370+
if (expectValid)
371+
{
372+
picker.SuggestedStartLocation((winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId)locationValue);
373+
VERIFY_ARE_EQUAL(picker.SuggestedStartLocation(), (winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId)locationValue);
374+
}
375+
else
376+
{
377+
try
378+
{
379+
picker.SuggestedStartLocation((winrt::Microsoft::Windows::Storage::Pickers::PickerLocationId)locationValue);
380+
VERIFY_FAIL(L"Expected exception for invalid suggested start location");
381+
}
382+
catch (...)
383+
{
384+
// Expected exception for invalid suggested start location
385+
}
386+
}
387+
}
388+
}
389+
390+
TEST_METHOD(VerifyValidateSingleFileTypeFilterElement)
391+
{
392+
// Arrange.
393+
std::vector<std::tuple<winrt::hstring, bool>> test_cases {
394+
{L".txt", true},
395+
{L"*", true}, // One asterisk is valid
396+
{L"**", false}, // More than one asterisk is invalid
397+
{L"*.docx", false}, // Filter must be '*' or start with '.'
398+
{L"", false}, // Empty string is invalid
399+
{L"invalid", false}, // Invalid format
400+
};
401+
// Act & Assert
402+
for (const auto& test_case : test_cases)
403+
{
404+
winrt::hstring filter = std::get<0>(test_case);
405+
bool expectValid = std::get<1>(test_case);
406+
407+
if (expectValid)
408+
{
409+
PickerCommon::ValidateSingleFileTypeFilterElement(filter);
410+
}
411+
else
412+
{
413+
try
414+
{
415+
PickerCommon::ValidateSingleFileTypeFilterElement(filter);
416+
VERIFY_FAIL(L"Expected exception for invalid single file type filter element");
417+
}
418+
catch (...)
419+
{
420+
// Expected exception for invalid single file type filter element
421+
}
422+
}
423+
}
424+
}
425+
426+
TEST_METHOD(VerifyValidateFileTypeFilter)
427+
{
428+
// Arrange.
429+
std::vector<std::tuple<winrt::hstring, bool>> test_cases {
430+
{L".txt", true},
431+
{L"*", true}, // One asterisk is valid
432+
{L"**", false}, // More than one asterisk is invalid
433+
{L"*.docx", false}, // *.ext is not a valid filter
434+
{L".docx", true},
435+
{L"", false}, // Empty filter is invalid
436+
{L"invalid", false}, // Invalid format
437+
};
438+
439+
winrt::Microsoft::UI::WindowId windowId{};
440+
winrt::Microsoft::Windows::Storage::Pickers::FileOpenPicker picker(windowId);
441+
442+
// Act & Assert
443+
for (const auto& test_case : test_cases)
444+
{
445+
auto filter = std::get<0>(test_case);
446+
bool expectValid = std::get<1>(test_case);
447+
448+
if (expectValid)
449+
{
450+
picker.FileTypeFilter().Append(filter);
451+
auto newFilters = picker.FileTypeFilter().GetView();
452+
VERIFY_ARE_EQUAL(newFilters.GetAt(newFilters.Size() - 1), filter);
453+
}
454+
else
455+
{
456+
try
457+
{
458+
picker.FileTypeFilter().Append(filter);
459+
VERIFY_FAIL(L"Expected exception for invalid file type filter.");
460+
}
461+
catch (...)
462+
{
463+
// Expected exception for invalid file type filter
464+
}
465+
}
466+
}
467+
}
303468
};
304469
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This is a mocked PickderLocalization head file for test purpose.
2+
3+
#pragma once
4+
#include "pch.h"
5+
6+
7+
namespace PickerLocalization
8+
{
9+
winrt::hstring GetStoragePickersLocalizationText(winrt::hstring key);
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This is a mocked PickerLocalization source file for test purpose.
2+
#include "pch.h"
3+
#include "PickerLocalization.h"
4+
5+
namespace PickerLocalization
6+
{
7+
winrt::hstring GetStoragePickersLocalizationText(winrt::hstring key)
8+
{
9+
return key;
10+
}
11+
}

test/StoragePickersTests/StoragePickersTests.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,15 @@
109109
<PrecompiledHeader>Create</PrecompiledHeader>
110110
</ClCompile>
111111
<ClCompile Include="PickerCommonTests.cpp" />
112+
<ClCompile Include="Pickerlocalization.cpp" />
112113
<ClCompile Include="StoragePickersTests.cpp">
113114
<AdditionalIncludeDirectories>$(RepoRoot)\build\VersionInfo;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
114115
</ClCompile>
115116
</ItemGroup>
116117
<ItemGroup>
117118
<ClInclude Include="..\..\dev\Interop\StoragePickers\PickerCommon.h" />
118119
<ClInclude Include="pch.h" />
120+
<ClInclude Include="PickerLocalization.h" />
119121
</ItemGroup>
120122
<ItemGroup>
121123
<None Include="packages.config" />

test/StoragePickersTests/StoragePickersTests.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
<ClCompile Include="PickerCommonTests.cpp">
3434
<Filter>Source Files</Filter>
3535
</ClCompile>
36+
<ClCompile Include="Pickerlocalization.cpp">
37+
<Filter>Source Files</Filter>
38+
</ClCompile>
3639
</ItemGroup>
3740
<ItemGroup>
3841
<ClInclude Include="pch.h">
@@ -41,5 +44,8 @@
4144
<ClInclude Include="..\..\dev\Interop\StoragePickers\PickerCommon.h">
4245
<Filter>Source Files</Filter>
4346
</ClInclude>
47+
<ClInclude Include="PickerLocalization.h">
48+
<Filter>Source Files</Filter>
49+
</ClInclude>
4450
</ItemGroup>
4551
</Project>

0 commit comments

Comments
 (0)