Skip to content

Commit c6db641

Browse files
Bring over initial test cases for Input Injector in order to be able to test Sizer controls
See: CommunityToolkit/Labs-Windows#183 And Tooling Branch: https://github.com/CommunityToolkit/Tooling-Windows-Submodule/tree/llama/input-injection-2025 TODO: More fluent API surface and figuring out how to do a Drag operation instead of just Click... 😅
1 parent 6d537b4 commit c6db641

File tree

6 files changed

+134
-3
lines changed

6 files changed

+134
-3
lines changed

components/Sizers/src/SizerBase.xaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- 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. -->
1+
<!-- 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. -->
22
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:controls="using:CommunityToolkit.WinUI.Controls">
@@ -46,7 +46,11 @@
4646
<x:Double x:Key="SizerBaseThumbRadius">2</x:Double>
4747
<Thickness x:Key="SizerBasePadding">4</Thickness>
4848

49-
<Style TargetType="controls:SizerBase">
49+
<Style BasedOn="{StaticResource DefaultSizerBaseStyle}"
50+
TargetType="controls:SizerBase" />
51+
52+
<Style x:Key="DefaultSizerBaseStyle"
53+
TargetType="controls:SizerBase">
5054
<Setter Property="IsTabStop" Value="True" />
5155
<Setter Property="UseSystemFocusVisuals" Value="True" />
5256
<Setter Property="HorizontalAlignment" Value="Stretch" />
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
using CommunityToolkit.WinUI.Controls;
6+
using CommunityToolkit.Tooling.TestGen;
7+
using CommunityToolkit.Tests;
8+
using CommunityToolkit.WinUI.Controls.Automation.Peers;
9+
using CommunityToolkit.Tests.Input;
10+
11+
namespace SizersTests;
12+
13+
[TestClass]
14+
public partial class SizerInputTestClass : VisualUITestBase
15+
{
16+
[UIThreadTestMethod]
17+
public void PropertySizer_TestTouchDrag(PropertySizerTestInitialBinding testControl)
18+
{
19+
var propertySizer = testControl.FindDescendant<PropertySizer>();
20+
21+
Assert.IsNotNull(propertySizer, "Could not find PropertySizer control.");
22+
Assert.IsNotNull(App.ContentRoot, "Could not find ContentRoot.");
23+
// Set in XAML Page LINK: PropertySizerTestInitialBinding.xaml#L14
24+
Assert.AreEqual(300, propertySizer.Binding, "Property Sizer not at expected initial value.");
25+
26+
var location = App.ContentRoot.CoordinatesToCenter(propertySizer);
27+
28+
App.CurrentWindow.InjectInput();
29+
}
30+
31+
[UIThreadTestMethod]
32+
public async Task InputInjection_TestClickButton(TouchInjectionTest testControl)
33+
{
34+
var button = testControl.FindDescendant<Button>();
35+
36+
Assert.IsNotNull(button, "Could not find button control.");
37+
Assert.IsFalse(testControl.WasButtonClicked, "Initial state unexpected. Button shouldn't be clicked yet.");
38+
39+
// Get location to button.
40+
var location = App.ContentRoot!.CoordinatesToCenter(button); // TODO: Write a `CoordinatesToCenter` helper?
41+
42+
InputSimulator sim = App.CurrentWindow.InjectInput();
43+
44+
sim.StartTouch();
45+
// Offset location slightly to ensure we're inside the button.
46+
var pointerId = sim.TouchDown(new Point(location.X, location.Y));
47+
await Task.Delay(50);
48+
sim.TouchUp(pointerId);
49+
50+
// Ensure UI event is processed by our button
51+
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });
52+
53+
// Optional delay for us to be able to see button content change before test shuts down.
54+
await Task.Delay(250);
55+
56+
Assert.IsTrue(testControl.WasButtonClicked, "Button wasn't clicked.");
57+
58+
sim.StopTouch();
59+
}
60+
}

components/Sizers/tests/Sizers.Tests.projitems

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
<Compile Include="$(MSBuildThisFileDirectory)ContentSizerTestInitialLayout.xaml.cs">
1313
<DependentUpon>ContentSizerTestInitialLayout.xaml</DependentUpon>
1414
</Compile>
15+
<Compile Include="$(MSBuildThisFileDirectory)SizerInputTestClass.cs" />
1516
<Compile Include="$(MSBuildThisFileDirectory)ExampleSizerBaseTestClass.cs" />
1617
<Compile Include="$(MSBuildThisFileDirectory)PropertySizerTestInitialBinding.xaml.cs">
1718
<DependentUpon>PropertySizerTestInitialBinding.xaml</DependentUpon>
1819
</Compile>
20+
<Compile Include="$(MSBuildThisFileDirectory)TouchInjectionTest.xaml.cs">
21+
<DependentUpon>TouchInjectionTest.xaml</DependentUpon>
22+
</Compile>
1923
</ItemGroup>
2024
<ItemGroup>
2125
<Page Include="$(MSBuildThisFileDirectory)ContentSizerTestInitialLayout.xaml">
@@ -26,5 +30,9 @@
2630
<SubType>Designer</SubType>
2731
<Generator>MSBuild:Compile</Generator>
2832
</Page>
33+
<Page Include="$(MSBuildThisFileDirectory)TouchInjectionTest.xaml">
34+
<SubType>Designer</SubType>
35+
<Generator>MSBuild:Compile</Generator>
36+
</Page>
2937
</ItemGroup>
3038
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Page x:Class="SizersTests.TouchInjectionTest"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:local="using:SizersTests"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
8+
mc:Ignorable="d">
9+
10+
<Grid>
11+
<Button x:Name="TestButton"
12+
Width="100"
13+
Height="50"
14+
Margin="100,100,0,0"
15+
Click="TestButton_Click"
16+
Content="Not Clicked" />
17+
</Grid>
18+
</Page>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
using Microsoft.UI.Xaml;
6+
using Microsoft.UI.Xaml.Controls;
7+
using Microsoft.UI.Xaml.Controls.Primitives;
8+
using Microsoft.UI.Xaml.Data;
9+
using Microsoft.UI.Xaml.Input;
10+
using Microsoft.UI.Xaml.Media;
11+
using Microsoft.UI.Xaml.Navigation;
12+
using System;
13+
using System.Collections.Generic;
14+
using System.IO;
15+
using System.Linq;
16+
using System.Runtime.InteropServices.WindowsRuntime;
17+
using Windows.Foundation;
18+
using Windows.Foundation.Collections;
19+
20+
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
21+
22+
namespace SizersTests;
23+
/// <summary>
24+
/// An empty page that can be used on its own or navigated to within a Frame.
25+
/// </summary>
26+
public sealed partial class TouchInjectionTest : Page
27+
{
28+
public bool WasButtonClicked { get; set; }
29+
30+
public TouchInjectionTest()
31+
{
32+
this.InitializeComponent();
33+
}
34+
35+
private void TestButton_Click(object sender, RoutedEventArgs args)
36+
{
37+
WasButtonClicked = true;
38+
39+
TestButton.Content = "Clicked!";
40+
}
41+
}

0 commit comments

Comments
 (0)