From e5f2b145a7d926348376afa34dc9a944aeea9aff Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sat, 23 Jul 2022 01:20:33 -0400 Subject: [PATCH] Avoid deprecated input API in tests --- .../Input/MouseDeviceTests.cs | 93 +++++++++++-------- .../Input/PointerOverTests.cs | 88 +----------------- .../Input/PointerTestsBase.cs | 90 ++++++++++++++++++ 3 files changed, 143 insertions(+), 128 deletions(-) create mode 100644 tests/Avalonia.Base.UnitTests/Input/PointerTestsBase.cs diff --git a/tests/Avalonia.Base.UnitTests/Input/MouseDeviceTests.cs b/tests/Avalonia.Base.UnitTests/Input/MouseDeviceTests.cs index 88abb4a6fa..466aba43ee 100644 --- a/tests/Avalonia.Base.UnitTests/Input/MouseDeviceTests.cs +++ b/tests/Avalonia.Base.UnitTests/Input/MouseDeviceTests.cs @@ -1,9 +1,12 @@ using System; using System.Collections.Generic; using Avalonia.Controls; +using Avalonia.Controls.Presenters; +using Avalonia.Controls.Templates; using Avalonia.Input; using Avalonia.Input.Raw; using Avalonia.Media; +using Avalonia.Platform; using Avalonia.Rendering; using Avalonia.UnitTests; using Moq; @@ -11,71 +14,79 @@ using Xunit; namespace Avalonia.Base.UnitTests.Input { - public class MouseDeviceTests + public class MouseDeviceTests : PointerTestsBase { -#pragma warning disable CS0618 // Type or member is obsolete [Fact] public void Capture_Is_Transferred_To_Parent_When_Control_Removed() { + using var app = UnitTestApplication.Start(new TestServices(inputManager: new InputManager())); + + var renderer = new Mock(); + var device = new MouseDevice(); + var impl = CreateTopLevelImplMock(renderer.Object); + Canvas control; - var root = new TestRoot + Panel rootChild; + var root = CreateInputRoot(impl.Object, rootChild = new Panel + { + Children = + { + (control = new Canvas()) + } + }); + + // Synthesize event to receive a pointer. + IPointer result = null; + root.PointerMoved += (_, a) => { - Child = control = new Canvas(), + result = a.Pointer; }; - var target = new MouseDevice(); + SetHit(renderer, control); + impl.Object.Input!(CreateRawPointerMovedArgs(device, root)); - target.Capture(control); - Assert.Same(control, target.Captured); + Assert.NotNull(result); + + result.Capture(control); + Assert.Same(control, result.Captured); - root.Child = null; + rootChild.Children.Clear(); - Assert.Same(root, target.Captured); + Assert.Same(rootChild, result.Captured); } -#pragma warning restore CS0618 // Type or member is obsolete [Fact] public void GetPosition_Should_Respect_Control_RenderTransform() { + using var app = UnitTestApplication.Start(new TestServices(inputManager: new InputManager())); + var renderer = new Mock(); + var device = new MouseDevice(); + var impl = CreateTopLevelImplMock(renderer.Object); - using (TestApplication(renderer.Object)) + Border border; + var root = CreateInputRoot(impl.Object, new Panel { - var inputManager = InputManager.Instance; - - var root = new TestRoot + Children = { - MouseDevice = new MouseDevice(), - Child = new Border + (border = new Border { Background = Brushes.Black, RenderTransform = new TranslateTransform(10, 0), - } - }; - - SendMouseMove(inputManager, root, new Point(11, 11)); - -#pragma warning disable CS0618 // Type or member is obsolete - var result = root.MouseDevice.GetPosition(root.Child); -#pragma warning restore CS0618 // Type or member is obsolete - Assert.Equal(new Point(1, 11), result); - } - } + }) + } + }); + + + Point? result = null; + root.PointerMoved += (_, a) => + { + result = a.GetPosition(border); + }; - private void SendMouseMove(IInputManager inputManager, TestRoot root, Point p = new Point()) - { - inputManager.ProcessInput(new RawPointerEventArgs( - root.MouseDevice, - 0, - root, - RawPointerEventType.Move, - p, - RawInputModifiers.None)); - } + SetHit(renderer, border); + impl.Object.Input!(CreateRawPointerMovedArgs(device, root, new Point(11, 11))); - private IDisposable TestApplication(IRenderer renderer) - { - return UnitTestApplication.Start( - new TestServices(inputManager: new InputManager())); + Assert.Equal(new Point(1, 11), result); } } } diff --git a/tests/Avalonia.Base.UnitTests/Input/PointerOverTests.cs b/tests/Avalonia.Base.UnitTests/Input/PointerOverTests.cs index a5ca2aef4a..a83b176484 100644 --- a/tests/Avalonia.Base.UnitTests/Input/PointerOverTests.cs +++ b/tests/Avalonia.Base.UnitTests/Input/PointerOverTests.cs @@ -3,14 +3,10 @@ using System; using System.Collections.Generic; using Avalonia.Controls; -using Avalonia.Controls.Presenters; -using Avalonia.Controls.Templates; using Avalonia.Input; using Avalonia.Input.Raw; -using Avalonia.Platform; using Avalonia.Rendering; using Avalonia.UnitTests; -using Avalonia.VisualTree; using Moq; @@ -18,7 +14,7 @@ using Xunit; namespace Avalonia.Base.UnitTests.Input { - public class PointerOverTests + public class PointerOverTests : PointerTestsBase { // https://github.com/AvaloniaUI/Avalonia/issues/2821 [Fact] @@ -448,87 +444,5 @@ namespace Avalonia.Base.UnitTests.Input c.PointerMoved += handler; } } - - private static void SetHit(Mock renderer, IControl? hit) - { - renderer.Setup(x => x.HitTest(It.IsAny(), It.IsAny(), It.IsAny>())) - .Returns(hit is null ? Array.Empty() : new[] { hit }); - - renderer.Setup(x => x.HitTestFirst(It.IsAny(), It.IsAny(), It.IsAny>())) - .Returns(hit); - } - - private static void SetMove(Mock deviceMock, IInputRoot root, IInputElement element) - { - deviceMock.Setup(d => d.ProcessRawEvent(It.IsAny())) - .Callback(() => element.RaiseEvent(CreatePointerMovedArgs(root, element))); - } - - private static Mock CreateTopLevelImplMock(IRenderer renderer) - { - var impl = new Mock(); - impl.DefaultValue = DefaultValue.Mock; - impl.SetupAllProperties(); - impl.SetupGet(r => r.RenderScaling).Returns(1); - impl.Setup(r => r.CreateRenderer(It.IsAny())).Returns(renderer); - impl.Setup(r => r.PointToScreen(It.IsAny())).Returns(p => new PixelPoint((int)p.X, (int)p.Y)); - impl.Setup(r => r.PointToClient(It.IsAny())).Returns(p => new Point(p.X, p.Y)); - return impl; - } - - private static IInputRoot CreateInputRoot(IWindowImpl impl, IControl child) - { - var root = new Window(impl) - { - Width = 100, - Height = 100, - Content = child, - Template = new FuncControlTemplate((w, _) => new ContentPresenter - { - Content = w.Content - }) - }; - root.Show(); - return root; - } - - private static IInputRoot CreateInputRoot(IRenderer renderer, IControl child) - { - return CreateInputRoot(CreateTopLevelImplMock(renderer).Object, child); - } - - private static RawPointerEventArgs CreateRawPointerMovedArgs( - IPointerDevice pointerDevice, - IInputRoot root, - Point? positition = null) - { - return new RawPointerEventArgs(pointerDevice, 0, root, RawPointerEventType.Move, - positition ?? default, default); - } - - private static PointerEventArgs CreatePointerMovedArgs( - IInputRoot root, IInputElement? source, Point? positition = null) - { - return new PointerEventArgs(InputElement.PointerMovedEvent, source, new Mock().Object, root, - positition ?? default, default, PointerPointProperties.None, KeyModifiers.None); - } - - private static Mock CreatePointerDeviceMock( - IPointer? pointer = null, - PointerType pointerType = PointerType.Mouse) - { - if (pointer is null) - { - var pointerMock = new Mock(); - pointerMock.SetupGet(p => p.Type).Returns(pointerType); - pointer = pointerMock.Object; - } - - var pointerDevice = new Mock(); - pointerDevice.Setup(d => d.TryGetPointer(It.IsAny())) - .Returns(pointer); - - return pointerDevice; - } } } diff --git a/tests/Avalonia.Base.UnitTests/Input/PointerTestsBase.cs b/tests/Avalonia.Base.UnitTests/Input/PointerTestsBase.cs new file mode 100644 index 0000000000..1ca6678aef --- /dev/null +++ b/tests/Avalonia.Base.UnitTests/Input/PointerTestsBase.cs @@ -0,0 +1,90 @@ +#nullable enable +using System; +using Avalonia.Controls; +using Avalonia.Controls.Presenters; +using Avalonia.Controls.Templates; +using Avalonia.Input; +using Avalonia.Input.Raw; +using Avalonia.Platform; +using Avalonia.Rendering; +using Avalonia.VisualTree; +using Moq; + +namespace Avalonia.Base.UnitTests.Input; + +public abstract class PointerTestsBase +{ + protected static void SetHit(Mock renderer, IControl? hit) + { + renderer.Setup(x => x.HitTest(It.IsAny(), It.IsAny(), It.IsAny>())) + .Returns(hit is null ? Array.Empty() : new[] { hit }); + + renderer.Setup(x => x.HitTestFirst(It.IsAny(), It.IsAny(), It.IsAny>())) + .Returns(hit); + } + + protected static void SetMove(Mock deviceMock, IInputRoot root, IInputElement element) + { + deviceMock.Setup(d => d.ProcessRawEvent(It.IsAny())) + .Callback(() => element.RaiseEvent(CreatePointerMovedArgs(root, element))); + } + + protected static Mock CreateTopLevelImplMock(IRenderer renderer) + { + var impl = new Mock(); + impl.DefaultValue = DefaultValue.Mock; + impl.SetupAllProperties(); + impl.SetupGet(r => r.RenderScaling).Returns(1); + impl.Setup(r => r.CreateRenderer(It.IsAny())).Returns(renderer); + impl.Setup(r => r.PointToScreen(It.IsAny())).Returns(p => new PixelPoint((int)p.X, (int)p.Y)); + impl.Setup(r => r.PointToClient(It.IsAny())).Returns(p => new Point(p.X, p.Y)); + return impl; + } + + protected static IInputRoot CreateInputRoot(IWindowImpl impl, IControl child) + { + var root = new Window(impl) + { + Width = 100, + Height = 100, + Content = child, + Template = new FuncControlTemplate((w, _) => new ContentPresenter { Content = w.Content }) + }; + root.Show(); + return root; + } + + protected static RawPointerEventArgs CreateRawPointerMovedArgs( + IPointerDevice pointerDevice, + IInputRoot root, + Point? positition = null) + { + return new RawPointerEventArgs(pointerDevice, 0, root, RawPointerEventType.Move, + positition ?? default, default); + } + + protected static PointerEventArgs CreatePointerMovedArgs( + IInputRoot root, IInputElement? source, Point? positition = null) + { + return new PointerEventArgs(InputElement.PointerMovedEvent, source, new Mock().Object, root, + positition ?? default, default, PointerPointProperties.None, KeyModifiers.None); + } + + protected static Mock CreatePointerDeviceMock( + IPointer? pointer = null, + PointerType pointerType = PointerType.Mouse) + { + if (pointer is null) + { + var pointerMock = new Mock(); + pointerMock.SetupGet(p => p.Type).Returns(pointerType); + pointer = pointerMock.Object; + } + + var pointerDevice = new Mock(); + pointerDevice.Setup(d => d.TryGetPointer(It.IsAny())) + .Returns(pointer); + + return pointerDevice; + } +}