Skip to content

🚧 Input Injection Test API Tests... #681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions components/Sizers/src/SizerBase.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- 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. -->
<!-- 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. -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:CommunityToolkit.WinUI.Controls">
Expand Down Expand Up @@ -46,7 +46,11 @@
<x:Double x:Key="SizerBaseThumbRadius">2</x:Double>
<Thickness x:Key="SizerBasePadding">4</Thickness>

<Style TargetType="controls:SizerBase">
<Style BasedOn="{StaticResource DefaultSizerBaseStyle}"
TargetType="controls:SizerBase" />

<Style x:Key="DefaultSizerBaseStyle"
TargetType="controls:SizerBase">
<Setter Property="IsTabStop" Value="True" />
<Setter Property="UseSystemFocusVisuals" Value="True" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
Expand Down
60 changes: 60 additions & 0 deletions components/Sizers/tests/SizerInputTestClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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.WinUI.Controls;
using CommunityToolkit.Tooling.TestGen;
using CommunityToolkit.Tests;
using CommunityToolkit.WinUI.Controls.Automation.Peers;
using CommunityToolkit.Tests.Input;

namespace SizersTests;

[TestClass]
public partial class SizerInputTestClass : VisualUITestBase
{
[UIThreadTestMethod]
public void PropertySizer_TestTouchDrag(PropertySizerTestInitialBinding testControl)
{
var propertySizer = testControl.FindDescendant<PropertySizer>();

Assert.IsNotNull(propertySizer, "Could not find PropertySizer control.");
Assert.IsNotNull(App.ContentRoot, "Could not find ContentRoot.");
// Set in XAML Page LINK: PropertySizerTestInitialBinding.xaml#L14
Assert.AreEqual(300, propertySizer.Binding, "Property Sizer not at expected initial value.");

var location = App.ContentRoot.CoordinatesToCenter(propertySizer);

App.CurrentWindow.InjectInput();
}

[UIThreadTestMethod]
public async Task InputInjection_TestClickButton(TouchInjectionTest testControl)
{
var button = testControl.FindDescendant<Button>();

Assert.IsNotNull(button, "Could not find button control.");
Assert.IsFalse(testControl.WasButtonClicked, "Initial state unexpected. Button shouldn't be clicked yet.");

// Get location to button.
var location = App.ContentRoot!.CoordinatesToCenter(button); // TODO: Write a `CoordinatesToCenter` helper?

InputSimulator sim = App.CurrentWindow.InjectInput();

sim.StartTouch();
// Offset location slightly to ensure we're inside the button.
var pointerId = sim.TouchDown(new Point(location.X, location.Y));
await Task.Delay(50);
sim.TouchUp(pointerId);

// Ensure UI event is processed by our button
await CompositionTargetHelper.ExecuteAfterCompositionRenderingAsync(() => { });

// Optional delay for us to be able to see button content change before test shuts down.
await Task.Delay(250);

Assert.IsTrue(testControl.WasButtonClicked, "Button wasn't clicked.");

sim.StopTouch();
}
}
8 changes: 8 additions & 0 deletions components/Sizers/tests/Sizers.Tests.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
<Compile Include="$(MSBuildThisFileDirectory)ContentSizerTestInitialLayout.xaml.cs">
<DependentUpon>ContentSizerTestInitialLayout.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)SizerInputTestClass.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ExampleSizerBaseTestClass.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PropertySizerTestInitialBinding.xaml.cs">
<DependentUpon>PropertySizerTestInitialBinding.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)TouchInjectionTest.xaml.cs">
<DependentUpon>TouchInjectionTest.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<Page Include="$(MSBuildThisFileDirectory)ContentSizerTestInitialLayout.xaml">
Expand All @@ -26,5 +30,9 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)TouchInjectionTest.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
</Project>
18 changes: 18 additions & 0 deletions components/Sizers/tests/TouchInjectionTest.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Page x:Class="SizersTests.TouchInjectionTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:SizersTests"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">

<Grid>
<Button x:Name="TestButton"
Width="100"
Height="50"
Margin="100,100,0,0"
Click="TestButton_Click"
Content="Not Clicked" />
</Grid>
</Page>
25 changes: 25 additions & 0 deletions components/Sizers/tests/TouchInjectionTest.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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 SizersTests;

/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class TouchInjectionTest : Page
{
public bool WasButtonClicked { get; set; }

public TouchInjectionTest()
{
this.InitializeComponent();
}

private void TestButton_Click(object sender, RoutedEventArgs args)
{
WasButtonClicked = true;

TestButton.Content = "Clicked!";
}
}
Loading