Skip to content

Commit cd19f55

Browse files
[Test] Add SwitchConverter tests to the primitives component
Fix test header (failure in CI from last commit)
1 parent 4f8f858 commit cd19f55

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

components/Primitives/tests/Primitives.Tests.projitems

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
<Import_RootNamespace>PrimitivesExperiment.Tests</Import_RootNamespace>
1010
</PropertyGroup>
1111
<ItemGroup>
12+
<Compile Include="$(MSBuildThisFileDirectory)SwitchPresenter\SwitchConverterBrushSample.xaml.cs">
13+
<DependentUpon>%(Filename)</DependentUpon>
14+
</Compile>
1215
<Compile Include="$(MSBuildThisFileDirectory)SwitchPresenter\SwitchPresenterLayoutSample.xaml.cs">
1316
<DependentUpon>%(Filename)</DependentUpon>
1417
</Compile>
@@ -38,6 +41,10 @@
3841
</Page>
3942
</ItemGroup>
4043
<ItemGroup>
44+
<Page Include="$(MSBuildThisFileDirectory)SwitchPresenter\SwitchConverterBrushSample.xaml">
45+
<SubType>Designer</SubType>
46+
<Generator>MSBuild:Compile</Generator>
47+
</Page>
4148
<Page Include="$(MSBuildThisFileDirectory)SwitchPresenter\SwitchPresenterLayoutSample.xaml">
4249
<SubType>Designer</SubType>
4350
<Generator>MSBuild:Compile</Generator>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<Page x:Class="PrimitivesExperiment.Tests.SwitchConverterBrushSample"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:controls="using:CommunityToolkit.WinUI.Controls"
5+
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:local="using:PrimitivesExperiment.Tests"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
xmlns:ui="using:CommunityToolkit.WinUI"
10+
mc:Ignorable="d">
11+
12+
<Page.Resources>
13+
<!--
14+
If you reference an enum directly in UWP, you need to use it somewhere for the XamlTypeInfo reference to be generated...
15+
-->
16+
<local:CheckStatus x:Key="MyChecks">Warning</local:CheckStatus>
17+
18+
<!-- Make it easier for us to retrieve these to compare from test -->
19+
<StaticResource x:Key="SystemFillColorSuccessBrush"
20+
ResourceKey="SystemFillColorSuccessBrush" />
21+
<StaticResource x:Key="SystemFillColorCautionBrush"
22+
ResourceKey="SystemFillColorCautionBrush" />
23+
<StaticResource x:Key="SystemFillColorCriticalBrush"
24+
ResourceKey="SystemFillColorCriticalBrush" />
25+
26+
<!-- SwitchConverter lets you easily convert general values to resources -->
27+
<!-- Note: This is in the converters namespace -->
28+
<converters:SwitchConverter x:Key="StatusToColorSwitchConverter"
29+
TargetType="local:CheckStatus">
30+
<!-- Note: These are reused from the controls namespace from SwitchPresenter -->
31+
<controls:Case Content="{ThemeResource SystemFillColorSuccessBrush}"
32+
Value="Success" />
33+
<controls:Case Content="{ThemeResource SystemFillColorCautionBrush}"
34+
Value="Warning" />
35+
<controls:Case Content="{ThemeResource SystemFillColorCriticalBrush}"
36+
Value="Error" />
37+
</converters:SwitchConverter>
38+
</Page.Resources>
39+
40+
<StackPanel Spacing="8">
41+
<ComboBox x:Name="StatusPicker"
42+
Header="Pick a status"
43+
SelectedIndex="0">
44+
<x:String>Success</x:String>
45+
<x:String>Warning</x:String>
46+
<x:String>Error</x:String>
47+
</ComboBox>
48+
<TextBlock x:Name="ResultBlock"
49+
FontWeight="SemiBold"
50+
Foreground="{x:Bind StatusPicker.SelectedItem, Converter={StaticResource StatusToColorSwitchConverter}, Mode=OneWay}"
51+
Text="{x:Bind StatusPicker.SelectedItem, Mode=OneWay}" />
52+
</StackPanel>
53+
</Page>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace PrimitivesExperiment.Tests;
6+
7+
public sealed partial class SwitchConverterBrushSample : Page
8+
{
9+
public SwitchConverterBrushSample()
10+
{
11+
this.InitializeComponent();
12+
}
13+
}
14+
15+
public enum CheckStatus
16+
{
17+
Error,
18+
Warning,
19+
Success,
20+
}

components/Primitives/tests/SwitchPresenter/SwitchPresenterTests.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
15
using CommunityToolkit.Tests;
26
using CommunityToolkit.Tooling.TestGen;
37
using CommunityToolkit.WinUI.Controls;
8+
using CommunityToolkit.WinUI.Converters;
49

510
namespace PrimitivesExperiment.Tests;
611

@@ -48,4 +53,90 @@ public async Task SwitchPresenterLayoutTest(SwitchPresenterLayoutSample page)
4853
Assert.IsNotNull(txtbox, "Couldn't find new textbox");
4954
Assert.AreEqual("Confirmation code", txtbox.Header, "Textbox header not expected value");
5055
}
56+
57+
[UIThreadTestMethod]
58+
public async Task SwitchConverterBrushTest(SwitchConverterBrushSample page)
59+
{
60+
var combobox = page.FindDescendant<ComboBox>();
61+
var textblock = page.FindDescendant("ResultBlock") as TextBlock;
62+
63+
Assert.IsNotNull(combobox, "Couldn't find ComboBox");
64+
Assert.IsNotNull(textblock, "Couldn't find SwitchPresenter");
65+
66+
// Are we in our initial case?
67+
Assert.AreEqual(0, combobox.SelectedIndex, "ComboBox not initialized");
68+
Assert.AreEqual("Success", combobox.SelectedItem, "Not expected starting value in ComboBox");
69+
70+
// Check the TextBlock's brush
71+
Assert.AreEqual(((SolidColorBrush)page.Resources["SystemFillColorSuccessBrush"]).Color, ((SolidColorBrush)textblock.Foreground).Color, "TextBlock not in initial success state");
72+
73+
// Update combobox
74+
combobox.SelectedIndex = 1;
75+
76+
// Wait for update
77+
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });
78+
79+
// Are we in the new case?
80+
Assert.AreEqual("Warning", combobox.SelectedItem, "ComboBox didn't change");
81+
82+
// Check the TextBlock's brush
83+
Assert.AreEqual(((SolidColorBrush)page.Resources["SystemFillColorCautionBrush"]).Color, ((SolidColorBrush)textblock.Foreground).Color, "TextBlock not in new state");
84+
85+
// Update combobox
86+
combobox.SelectedIndex = 2;
87+
88+
// Wait for update
89+
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });
90+
91+
// Are we in the new case?
92+
Assert.AreEqual("Error", combobox.SelectedItem, "ComboBox didn't change 2");
93+
94+
// Check the TextBlock's brush
95+
Assert.AreEqual(((SolidColorBrush)page.Resources["SystemFillColorCriticalBrush"]).Color, ((SolidColorBrush)textblock.Foreground).Color, "TextBlock not in final state");
96+
}
97+
98+
[UIThreadTestMethod]
99+
public void SwitchConverterDirectTest()
100+
{
101+
// Multiply by 10
102+
SwitchConverter sconverter = new()
103+
{
104+
SwitchCases = new CaseCollection {
105+
new Case()
106+
{
107+
Content = 10,
108+
Value = 1,
109+
},
110+
new Case()
111+
{
112+
Content = 50,
113+
Value = 5,
114+
IsDefault = true
115+
},
116+
new Case()
117+
{
118+
Content = 30,
119+
Value = 3,
120+
},
121+
new Case()
122+
{
123+
Content = 20,
124+
Value = 2,
125+
},
126+
},
127+
TargetType = typeof(int)
128+
};
129+
130+
Assert.IsNotNull(sconverter);
131+
Assert.AreEqual(4, sconverter.SwitchCases.Count, "Not 4 cases");
132+
133+
var @default = sconverter.Convert(100, typeof(int), string.Empty, string.Empty);
134+
135+
Assert.AreEqual(50, @default, "Unexpected default return");
136+
137+
Assert.AreEqual(10, sconverter.Convert(1, typeof(int), string.Empty, string.Empty), "Unexpected result with 1");
138+
Assert.AreEqual(20, sconverter.Convert(2, typeof(int), string.Empty, string.Empty), "Unexpected result with 2");
139+
Assert.AreEqual(30, sconverter.Convert(3, typeof(int), string.Empty, string.Empty), "Unexpected result with 3");
140+
Assert.AreEqual(50, sconverter.Convert(5, typeof(int), string.Empty, string.Empty), "Unexpected result with 5");
141+
}
51142
}

0 commit comments

Comments
 (0)