|  | 
|  | 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 | +} | 
0 commit comments