Add some initial helpers for the InputSimulator

Fix issue with UWP and WeakReference
This commit is contained in:
michael-hawker 2022-11-18 14:54:49 -08:00
Родитель aa0950388e
Коммит ea94942380
4 изменённых файлов: 49 добавлений и 3 удалений

Просмотреть файл

@ -18,6 +18,7 @@
<Compile Include="$(MSBuildThisFileDirectory)App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Input\InputHelpers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Input\InputSimulator.Bounds.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Log.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Input\InputSimulator.cs" />

Просмотреть файл

@ -0,0 +1,30 @@
// 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.
#if WINAPPSDK
using MainWindow = Microsoft.UI.Xaml.Window;
#else
using MainWindow = Windows.UI.Xaml.Window;
#endif
namespace CommunityToolkit.Labs.Tests;
public static class InputHelpers
{
/// <summary>
/// Helper extension method to create a new <see cref="InputSimulator"/> chain for the current window.
/// </summary>
/// <param name="window"><see cref="Window"/> class for your application.</param>
/// <returns>A new <see cref="InputSimulator"/> instance for that window.</returns>
public static InputSimulator InjectInput(this MainWindow window)
{
return new InputSimulator(window);
}
public static Point CoordinatesToCenter(this UIElement parent, UIElement target)
{
var location = target.TransformToVisual(parent).TransformPoint(default(Point));
return new(location.X + target.ActualSize.X / 2, location.Y + target.ActualSize.Y / 2);
}
}

Просмотреть файл

@ -41,7 +41,7 @@ public partial class InputSimulator
}
}
#else
private Rect Bounds => currentWindow.Bounds;
private Rect Bounds => _currentWindowRef.TryGetTarget(out Window window) ? window.Bounds : default;
#endif
private Point TranslatePointForWindow(Point point)

Просмотреть файл

@ -58,6 +58,21 @@ public partial class ExampleSizerBaseTestClass : VisualUITestBase
Assert.AreEqual(200, propertySizer.Binding, "Property Sizer not at expected changed value.");
}
[LabsUITestMethod]
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();
}
[LabsUITestMethod]
public async Task InputInjection_TestClickButton(TouchInjectionTest testControl)
{
@ -67,14 +82,14 @@ public partial class ExampleSizerBaseTestClass : VisualUITestBase
Assert.IsFalse(testControl.WasButtonClicked, "Initial state unexpected. Button shouldn't be clicked yet.");
// Get location to button.
var location = App.ContentRoot.CoordinatesTo(button); // TODO: Write a `CoordinatesToCenter` helper?
var location = App.ContentRoot!.CoordinatesToCenter(button); // TODO: Write a `CoordinatesToCenter` helper?
// TODO: Make an extension method on window for this to be like: App.CurrentWindow.InjectInput()...
InputSimulator sim = new(App.CurrentWindow);
sim.StartTouch();
// Offset location slightly to ensure we're inside the button.
var pointerId = sim.TouchDown(new Point(location.X + 25, location.Y + 25));
var pointerId = sim.TouchDown(new Point(location.X, location.Y));
await Task.Delay(50);
sim.TouchUp(pointerId);