Skip to content

Commit

Permalink
[Test] Add a basic SwitchPresenter test case
Browse files Browse the repository at this point in the history
Validates the value changes, the case, and the content
  • Loading branch information
michael-hawker committed Nov 8, 2024
1 parent 319e152 commit dc02efa
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
10 changes: 10 additions & 0 deletions components/Primitives/tests/Primitives.Tests.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<Import_RootNamespace>PrimitivesExperiment.Tests</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)SwitchPresenter\SwitchPresenterLayoutSample.xaml.cs">
<DependentUpon>%(Filename)</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)SwitchPresenter\SwitchPresenterTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Test_ConstrainedBox.Alignment.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Test_ConstrainedBox.AspectRatio.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Test_ConstrainedBox.Coerce.cs" />
Expand All @@ -33,4 +37,10 @@
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Page Include="$(MSBuildThisFileDirectory)SwitchPresenter\SwitchPresenterLayoutSample.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<Page x:Class="PrimitivesExperiment.Tests.SwitchPresenterLayoutSample"
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:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="using:CommunityToolkit.WinUI"
mc:Ignorable="d">

<StackPanel>
<ComboBox x:Name="Lookup"
Margin="0,0,0,8"
Header="Look up reservation"
SelectedIndex="0">
<x:String>Select an option</x:String>
<x:String>Confirmation Code</x:String>
<x:String>E-ticket number</x:String>
<x:String>Mileage Plan number</x:String>
</ComboBox>
<!-- SwitchPresenter binds to a value -->
<controls:SwitchPresenter Value="{Binding SelectedItem, ElementName=Lookup}">
<!-- And then only dynamically displays the Case with the matching Value -->
<controls:Case Value="Confirmation Code">
<StackPanel>
<TextBox Name="ConfirmationCodeValidator"
ui:TextBoxExtensions.Regex="^[a-zA-Z]{6}$"
Header="Confirmation code"
PlaceholderText="6 letters" />
<TextBlock Text="Thanks for entering a valid code!"
Visibility="{Binding (ui:TextBoxExtensions.IsValid), ElementName=ConfirmationCodeValidator}" />
</StackPanel>
</controls:Case>
<controls:Case Value="E-ticket number">
<StackPanel>
<TextBox Name="TicketValidator"
ui:TextBoxExtensions.Regex="(^\d{10}$)|(^\d{13}$)"
Header="E-ticket number"
PlaceholderText="10 or 13 numbers" />
<TextBlock Text="Thanks for entering a valid code!"
Visibility="{Binding (ui:TextBoxExtensions.IsValid), ElementName=TicketValidator}" />
</StackPanel>
</controls:Case>
<controls:Case Value="Mileage Plan number">
<TextBox Name="PlanValidator"
Header="Mileage Plan #"
PlaceholderText="Mileage Plan #" />
</controls:Case>
<!-- You can also provide a default case if no match is found -->
<controls:Case IsDefault="True">
<TextBlock Text="Please select a way to lookup your reservation above..." />
</controls:Case>
</controls:SwitchPresenter>
</StackPanel>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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 SwitchPresenterLayoutSample : Page
{
public SwitchPresenterLayoutSample()
{
this.InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using CommunityToolkit.Tests;
using CommunityToolkit.Tooling.TestGen;
using CommunityToolkit.WinUI.Controls;

namespace PrimitivesExperiment.Tests;

[TestClass]
public partial class SwitchPresenterTests : VisualUITestBase
{
[UIThreadTestMethod]
public async Task SwitchPresenterLayoutTest(SwitchPresenterLayoutSample page)
{
var spresenter = page.FindDescendant<SwitchPresenter>();
var combobox = page.FindDescendant<ComboBox>();

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

var dcase = spresenter.SwitchCases.OfType<Case>().FirstOrDefault(@case => @case.IsDefault);

Assert.IsNotNull(dcase, "Couldn't find default case");

// Are we in our initial case?
Assert.AreEqual("Select an option", spresenter.Value, "SwitchPresenter not expected starting value");
Assert.AreEqual(dcase, spresenter.CurrentCase, "SwitchPresenter not at current default case");

// Can we find our matching textbox?
var tbox = spresenter.FindDescendant<TextBlock>();
Assert.IsNotNull(tbox, "Couldn't find inner textbox");
Assert.AreEqual("Please select a way to lookup your reservation above...", tbox.Text, "Unexpected content for SwitchPresenter default case");

// Update combobox
combobox.SelectedIndex = 1;

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

// Are we in the new case?
Assert.AreEqual("Confirmation Code", spresenter.Value);

var ccase = spresenter.SwitchCases.OfType<Case>().FirstOrDefault(@case => @case.Value?.ToString() == "Confirmation Code");

Assert.IsNotNull(ccase, "Couldn't find expected case");

Assert.AreEqual(ccase, spresenter.CurrentCase, "SwitchPresenter didn't change cases");

var txtbox = spresenter.FindDescendant<TextBox>();
Assert.IsNotNull(txtbox, "Couldn't find new textbox");
Assert.AreEqual("Confirmation code", txtbox.Header, "Textbox header not expected value");
}
}

0 comments on commit dc02efa

Please sign in to comment.