Skip to content

Commit 4f8f858

Browse files
[Test] Add a basic SwitchPresenter test case
Validates the value changes, the case, and the content
1 parent 69c28a1 commit 4f8f858

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

components/Primitives/tests/Primitives.Tests.projitems

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
<Import_RootNamespace>PrimitivesExperiment.Tests</Import_RootNamespace>
1010
</PropertyGroup>
1111
<ItemGroup>
12+
<Compile Include="$(MSBuildThisFileDirectory)SwitchPresenter\SwitchPresenterLayoutSample.xaml.cs">
13+
<DependentUpon>%(Filename)</DependentUpon>
14+
</Compile>
15+
<Compile Include="$(MSBuildThisFileDirectory)SwitchPresenter\SwitchPresenterTests.cs" />
1216
<Compile Include="$(MSBuildThisFileDirectory)Test_ConstrainedBox.Alignment.cs" />
1317
<Compile Include="$(MSBuildThisFileDirectory)Test_ConstrainedBox.AspectRatio.cs" />
1418
<Compile Include="$(MSBuildThisFileDirectory)Test_ConstrainedBox.Coerce.cs" />
@@ -33,4 +37,10 @@
3337
<Generator>MSBuild:Compile</Generator>
3438
</Page>
3539
</ItemGroup>
40+
<ItemGroup>
41+
<Page Include="$(MSBuildThisFileDirectory)SwitchPresenter\SwitchPresenterLayoutSample.xaml">
42+
<SubType>Designer</SubType>
43+
<Generator>MSBuild:Compile</Generator>
44+
</Page>
45+
</ItemGroup>
3646
</Project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<Page x:Class="PrimitivesExperiment.Tests.SwitchPresenterLayoutSample"
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:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
xmlns:ui="using:CommunityToolkit.WinUI"
8+
mc:Ignorable="d">
9+
10+
<StackPanel>
11+
<ComboBox x:Name="Lookup"
12+
Margin="0,0,0,8"
13+
Header="Look up reservation"
14+
SelectedIndex="0">
15+
<x:String>Select an option</x:String>
16+
<x:String>Confirmation Code</x:String>
17+
<x:String>E-ticket number</x:String>
18+
<x:String>Mileage Plan number</x:String>
19+
</ComboBox>
20+
<!-- SwitchPresenter binds to a value -->
21+
<controls:SwitchPresenter Value="{Binding SelectedItem, ElementName=Lookup}">
22+
<!-- And then only dynamically displays the Case with the matching Value -->
23+
<controls:Case Value="Confirmation Code">
24+
<StackPanel>
25+
<TextBox Name="ConfirmationCodeValidator"
26+
ui:TextBoxExtensions.Regex="^[a-zA-Z]{6}$"
27+
Header="Confirmation code"
28+
PlaceholderText="6 letters" />
29+
<TextBlock Text="Thanks for entering a valid code!"
30+
Visibility="{Binding (ui:TextBoxExtensions.IsValid), ElementName=ConfirmationCodeValidator}" />
31+
</StackPanel>
32+
</controls:Case>
33+
<controls:Case Value="E-ticket number">
34+
<StackPanel>
35+
<TextBox Name="TicketValidator"
36+
ui:TextBoxExtensions.Regex="(^\d{10}$)|(^\d{13}$)"
37+
Header="E-ticket number"
38+
PlaceholderText="10 or 13 numbers" />
39+
<TextBlock Text="Thanks for entering a valid code!"
40+
Visibility="{Binding (ui:TextBoxExtensions.IsValid), ElementName=TicketValidator}" />
41+
</StackPanel>
42+
</controls:Case>
43+
<controls:Case Value="Mileage Plan number">
44+
<TextBox Name="PlanValidator"
45+
Header="Mileage Plan #"
46+
PlaceholderText="Mileage Plan #" />
47+
</controls:Case>
48+
<!-- You can also provide a default case if no match is found -->
49+
<controls:Case IsDefault="True">
50+
<TextBlock Text="Please select a way to lookup your reservation above..." />
51+
</controls:Case>
52+
</controls:SwitchPresenter>
53+
</StackPanel>
54+
</Page>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 SwitchPresenterLayoutSample : Page
8+
{
9+
public SwitchPresenterLayoutSample()
10+
{
11+
this.InitializeComponent();
12+
}
13+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using CommunityToolkit.Tests;
2+
using CommunityToolkit.Tooling.TestGen;
3+
using CommunityToolkit.WinUI.Controls;
4+
5+
namespace PrimitivesExperiment.Tests;
6+
7+
[TestClass]
8+
public partial class SwitchPresenterTests : VisualUITestBase
9+
{
10+
[UIThreadTestMethod]
11+
public async Task SwitchPresenterLayoutTest(SwitchPresenterLayoutSample page)
12+
{
13+
var spresenter = page.FindDescendant<SwitchPresenter>();
14+
var combobox = page.FindDescendant<ComboBox>();
15+
16+
Assert.IsNotNull(spresenter, "Couldn't find SwitchPresenter");
17+
Assert.IsNotNull(combobox, "Couldn't find ComboBox");
18+
19+
var dcase = spresenter.SwitchCases.OfType<Case>().FirstOrDefault(@case => @case.IsDefault);
20+
21+
Assert.IsNotNull(dcase, "Couldn't find default case");
22+
23+
// Are we in our initial case?
24+
Assert.AreEqual("Select an option", spresenter.Value, "SwitchPresenter not expected starting value");
25+
Assert.AreEqual(dcase, spresenter.CurrentCase, "SwitchPresenter not at current default case");
26+
27+
// Can we find our matching textbox?
28+
var tbox = spresenter.FindDescendant<TextBlock>();
29+
Assert.IsNotNull(tbox, "Couldn't find inner textbox");
30+
Assert.AreEqual("Please select a way to lookup your reservation above...", tbox.Text, "Unexpected content for SwitchPresenter default case");
31+
32+
// Update combobox
33+
combobox.SelectedIndex = 1;
34+
35+
// Wait for update
36+
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });
37+
38+
// Are we in the new case?
39+
Assert.AreEqual("Confirmation Code", spresenter.Value);
40+
41+
var ccase = spresenter.SwitchCases.OfType<Case>().FirstOrDefault(@case => @case.Value?.ToString() == "Confirmation Code");
42+
43+
Assert.IsNotNull(ccase, "Couldn't find expected case");
44+
45+
Assert.AreEqual(ccase, spresenter.CurrentCase, "SwitchPresenter didn't change cases");
46+
47+
var txtbox = spresenter.FindDescendant<TextBox>();
48+
Assert.IsNotNull(txtbox, "Couldn't find new textbox");
49+
Assert.AreEqual("Confirmation code", txtbox.Header, "Textbox header not expected value");
50+
}
51+
}

0 commit comments

Comments
 (0)