-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Test] Add a basic SwitchPresenter test case
Validates the value changes, the case, and the content
- Loading branch information
1 parent
319e152
commit dc02efa
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
components/Primitives/tests/SwitchPresenter/SwitchPresenterLayoutSample.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
13 changes: 13 additions & 0 deletions
13
components/Primitives/tests/SwitchPresenter/SwitchPresenterLayoutSample.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
components/Primitives/tests/SwitchPresenter/SwitchPresenterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |