Skip to content

Commit

Permalink
[Test] Add SwitchConverter tests to the primitives component
Browse files Browse the repository at this point in the history
Fix test header (failure in CI from last commit)
  • Loading branch information
michael-hawker committed Nov 8, 2024
1 parent dc02efa commit 7558d36
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
7 changes: 7 additions & 0 deletions components/Primitives/tests/Primitives.Tests.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<Import_RootNamespace>PrimitivesExperiment.Tests</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)SwitchPresenter\SwitchConverterBrushSample.xaml.cs">
<DependentUpon>%(Filename)</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)SwitchPresenter\SwitchPresenterLayoutSample.xaml.cs">
<DependentUpon>%(Filename)</DependentUpon>
</Compile>
Expand Down Expand Up @@ -38,6 +41,10 @@
</Page>
</ItemGroup>
<ItemGroup>
<Page Include="$(MSBuildThisFileDirectory)SwitchPresenter\SwitchConverterBrushSample.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)SwitchPresenter\SwitchPresenterLayoutSample.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<Page x:Class="PrimitivesExperiment.Tests.SwitchConverterBrushSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:CommunityToolkit.WinUI.Controls"
xmlns:converters="using:CommunityToolkit.WinUI.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:PrimitivesExperiment.Tests"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="using:CommunityToolkit.WinUI"
mc:Ignorable="d">

<Page.Resources>
<!--
If you reference an enum directly in UWP, you need to use it somewhere for the XamlTypeInfo reference to be generated...
-->
<local:CheckStatus x:Key="MyChecks">Warning</local:CheckStatus>

<!-- Make it easier for us to retrieve these to compare from test -->
<StaticResource x:Key="SystemFillColorSuccessBrush"
ResourceKey="SystemFillColorSuccessBrush" />
<StaticResource x:Key="SystemFillColorCautionBrush"
ResourceKey="SystemFillColorCautionBrush" />
<StaticResource x:Key="SystemFillColorCriticalBrush"
ResourceKey="SystemFillColorCriticalBrush" />

<!-- SwitchConverter lets you easily convert general values to resources -->
<!-- Note: This is in the converters namespace -->
<converters:SwitchConverter x:Key="StatusToColorSwitchConverter"
TargetType="local:CheckStatus">
<!-- Note: These are reused from the controls namespace from SwitchPresenter -->
<controls:Case Content="{ThemeResource SystemFillColorSuccessBrush}"
Value="Success" />
<controls:Case Content="{ThemeResource SystemFillColorCautionBrush}"
Value="Warning" />
<controls:Case Content="{ThemeResource SystemFillColorCriticalBrush}"
Value="Error" />
</converters:SwitchConverter>
</Page.Resources>

<StackPanel Spacing="8">
<ComboBox x:Name="StatusPicker"
Header="Pick a status"
SelectedIndex="0">
<x:String>Success</x:String>
<x:String>Warning</x:String>
<x:String>Error</x:String>
</ComboBox>
<TextBlock x:Name="ResultBlock"
FontWeight="SemiBold"
Foreground="{x:Bind StatusPicker.SelectedItem, Converter={StaticResource StatusToColorSwitchConverter}, Mode=OneWay}"
Text="{x:Bind StatusPicker.SelectedItem, Mode=OneWay}" />
</StackPanel>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace PrimitivesExperiment.Tests;

public sealed partial class SwitchConverterBrushSample : Page
{
public SwitchConverterBrushSample()
{
this.InitializeComponent();
}
}

public enum CheckStatus
{
Error,
Warning,
Success,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Tests;
using CommunityToolkit.Tooling.TestGen;
using CommunityToolkit.WinUI.Controls;
using CommunityToolkit.WinUI.Converters;

namespace PrimitivesExperiment.Tests;

Expand Down Expand Up @@ -48,4 +53,90 @@ public async Task SwitchPresenterLayoutTest(SwitchPresenterLayoutSample page)
Assert.IsNotNull(txtbox, "Couldn't find new textbox");
Assert.AreEqual("Confirmation code", txtbox.Header, "Textbox header not expected value");
}

[UIThreadTestMethod]
public async Task SwitchConverterBrushTest(SwitchConverterBrushSample page)
{
var combobox = page.FindDescendant<ComboBox>();
var textblock = page.FindDescendant("ResultBlock") as TextBlock;

Assert.IsNotNull(combobox, "Couldn't find ComboBox");
Assert.IsNotNull(textblock, "Couldn't find SwitchPresenter");

// Are we in our initial case?
Assert.AreEqual(0, combobox.SelectedIndex, "ComboBox not initialized");
Assert.AreEqual("Success", combobox.SelectedItem, "Not expected starting value in ComboBox");

// Check the TextBlock's brush
Assert.AreEqual(((SolidColorBrush)page.Resources["SystemFillColorSuccessBrush"]).Color, ((SolidColorBrush)textblock.Foreground).Color, "TextBlock not in initial success state");

// Update combobox
combobox.SelectedIndex = 1;

// Wait for update
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });

// Are we in the new case?
Assert.AreEqual("Warning", combobox.SelectedItem, "ComboBox didn't change");

// Check the TextBlock's brush
Assert.AreEqual(((SolidColorBrush)page.Resources["SystemFillColorCautionBrush"]).Color, ((SolidColorBrush)textblock.Foreground).Color, "TextBlock not in new state");

// Update combobox
combobox.SelectedIndex = 2;

// Wait for update
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });

// Are we in the new case?
Assert.AreEqual("Error", combobox.SelectedItem, "ComboBox didn't change 2");

// Check the TextBlock's brush
Assert.AreEqual(((SolidColorBrush)page.Resources["SystemFillColorCriticalBrush"]).Color, ((SolidColorBrush)textblock.Foreground).Color, "TextBlock not in final state");
}

[UIThreadTestMethod]
public void SwitchConverterDirectTest()
{
// Multiply by 10
SwitchConverter sconverter = new()
{
SwitchCases = new CaseCollection {
new Case()
{
Content = 10,
Value = 1,
},
new Case()
{
Content = 50,
Value = 5,
IsDefault = true
},
new Case()
{
Content = 30,
Value = 3,
},
new Case()
{
Content = 20,
Value = 2,
},
},
TargetType = typeof(int)
};

Assert.IsNotNull(sconverter);
Assert.AreEqual(4, sconverter.SwitchCases.Count, "Not 4 cases");

var @default = sconverter.Convert(100, typeof(int), string.Empty, string.Empty);

Assert.AreEqual(50, @default, "Unexpected default return");

Assert.AreEqual(10, sconverter.Convert(1, typeof(int), string.Empty, string.Empty), "Unexpected result with 1");
Assert.AreEqual(20, sconverter.Convert(2, typeof(int), string.Empty, string.Empty), "Unexpected result with 2");
Assert.AreEqual(30, sconverter.Convert(3, typeof(int), string.Empty, string.Empty), "Unexpected result with 3");
Assert.AreEqual(50, sconverter.Convert(5, typeof(int), string.Empty, string.Empty), "Unexpected result with 5");
}
}

0 comments on commit 7558d36

Please sign in to comment.