Browse Source

Avoid deprecated input API in tests

pull/8577/head
Max Katz 4 years ago
parent
commit
e5f2b145a7
  1. 93
      tests/Avalonia.Base.UnitTests/Input/MouseDeviceTests.cs
  2. 88
      tests/Avalonia.Base.UnitTests/Input/PointerOverTests.cs
  3. 90
      tests/Avalonia.Base.UnitTests/Input/PointerTestsBase.cs

93
tests/Avalonia.Base.UnitTests/Input/MouseDeviceTests.cs

@ -1,9 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Templates;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Input.Raw; using Avalonia.Input.Raw;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Rendering; using Avalonia.Rendering;
using Avalonia.UnitTests; using Avalonia.UnitTests;
using Moq; using Moq;
@ -11,71 +14,79 @@ using Xunit;
namespace Avalonia.Base.UnitTests.Input namespace Avalonia.Base.UnitTests.Input
{ {
public class MouseDeviceTests public class MouseDeviceTests : PointerTestsBase
{ {
#pragma warning disable CS0618 // Type or member is obsolete
[Fact] [Fact]
public void Capture_Is_Transferred_To_Parent_When_Control_Removed() public void Capture_Is_Transferred_To_Parent_When_Control_Removed()
{ {
using var app = UnitTestApplication.Start(new TestServices(inputManager: new InputManager()));
var renderer = new Mock<IRenderer>();
var device = new MouseDevice();
var impl = CreateTopLevelImplMock(renderer.Object);
Canvas control; 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.NotNull(result);
Assert.Same(control, target.Captured);
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] [Fact]
public void GetPosition_Should_Respect_Control_RenderTransform() public void GetPosition_Should_Respect_Control_RenderTransform()
{ {
using var app = UnitTestApplication.Start(new TestServices(inputManager: new InputManager()));
var renderer = new Mock<IRenderer>(); var renderer = new Mock<IRenderer>();
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; Children =
var root = new TestRoot
{ {
MouseDevice = new MouseDevice(), (border = new Border
Child = new Border
{ {
Background = Brushes.Black, Background = Brushes.Black,
RenderTransform = new TranslateTransform(10, 0), RenderTransform = new TranslateTransform(10, 0),
} })
}; }
});
SendMouseMove(inputManager, root, new Point(11, 11));
#pragma warning disable CS0618 // Type or member is obsolete Point? result = null;
var result = root.MouseDevice.GetPosition(root.Child); root.PointerMoved += (_, a) =>
#pragma warning restore CS0618 // Type or member is obsolete {
Assert.Equal(new Point(1, 11), result); result = a.GetPosition(border);
} };
}
private void SendMouseMove(IInputManager inputManager, TestRoot root, Point p = new Point()) SetHit(renderer, border);
{ impl.Object.Input!(CreateRawPointerMovedArgs(device, root, new Point(11, 11)));
inputManager.ProcessInput(new RawPointerEventArgs(
root.MouseDevice,
0,
root,
RawPointerEventType.Move,
p,
RawInputModifiers.None));
}
private IDisposable TestApplication(IRenderer renderer) Assert.Equal(new Point(1, 11), result);
{
return UnitTestApplication.Start(
new TestServices(inputManager: new InputManager()));
} }
} }
} }

88
tests/Avalonia.Base.UnitTests/Input/PointerOverTests.cs

@ -3,14 +3,10 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Templates;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Input.Raw; using Avalonia.Input.Raw;
using Avalonia.Platform;
using Avalonia.Rendering; using Avalonia.Rendering;
using Avalonia.UnitTests; using Avalonia.UnitTests;
using Avalonia.VisualTree;
using Moq; using Moq;
@ -18,7 +14,7 @@ using Xunit;
namespace Avalonia.Base.UnitTests.Input namespace Avalonia.Base.UnitTests.Input
{ {
public class PointerOverTests public class PointerOverTests : PointerTestsBase
{ {
// https://github.com/AvaloniaUI/Avalonia/issues/2821 // https://github.com/AvaloniaUI/Avalonia/issues/2821
[Fact] [Fact]
@ -448,87 +444,5 @@ namespace Avalonia.Base.UnitTests.Input
c.PointerMoved += handler; c.PointerMoved += handler;
} }
} }
private static void SetHit(Mock<IRenderer> renderer, IControl? hit)
{
renderer.Setup(x => x.HitTest(It.IsAny<Point>(), It.IsAny<IVisual>(), It.IsAny<Func<IVisual, bool>>()))
.Returns(hit is null ? Array.Empty<IControl>() : new[] { hit });
renderer.Setup(x => x.HitTestFirst(It.IsAny<Point>(), It.IsAny<IVisual>(), It.IsAny<Func<IVisual, bool>>()))
.Returns(hit);
}
private static void SetMove(Mock<IPointerDevice> deviceMock, IInputRoot root, IInputElement element)
{
deviceMock.Setup(d => d.ProcessRawEvent(It.IsAny<RawPointerEventArgs>()))
.Callback(() => element.RaiseEvent(CreatePointerMovedArgs(root, element)));
}
private static Mock<IWindowImpl> CreateTopLevelImplMock(IRenderer renderer)
{
var impl = new Mock<IWindowImpl>();
impl.DefaultValue = DefaultValue.Mock;
impl.SetupAllProperties();
impl.SetupGet(r => r.RenderScaling).Returns(1);
impl.Setup(r => r.CreateRenderer(It.IsAny<IRenderRoot>())).Returns(renderer);
impl.Setup(r => r.PointToScreen(It.IsAny<Point>())).Returns<Point>(p => new PixelPoint((int)p.X, (int)p.Y));
impl.Setup(r => r.PointToClient(It.IsAny<PixelPoint>())).Returns<PixelPoint>(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<Window>((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<IPointer>().Object, root,
positition ?? default, default, PointerPointProperties.None, KeyModifiers.None);
}
private static Mock<IPointerDevice> CreatePointerDeviceMock(
IPointer? pointer = null,
PointerType pointerType = PointerType.Mouse)
{
if (pointer is null)
{
var pointerMock = new Mock<IPointer>();
pointerMock.SetupGet(p => p.Type).Returns(pointerType);
pointer = pointerMock.Object;
}
var pointerDevice = new Mock<IPointerDevice>();
pointerDevice.Setup(d => d.TryGetPointer(It.IsAny<RawPointerEventArgs>()))
.Returns(pointer);
return pointerDevice;
}
} }
} }

90
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<IRenderer> renderer, IControl? hit)
{
renderer.Setup(x => x.HitTest(It.IsAny<Point>(), It.IsAny<IVisual>(), It.IsAny<Func<IVisual, bool>>()))
.Returns(hit is null ? Array.Empty<IControl>() : new[] { hit });
renderer.Setup(x => x.HitTestFirst(It.IsAny<Point>(), It.IsAny<IVisual>(), It.IsAny<Func<IVisual, bool>>()))
.Returns(hit);
}
protected static void SetMove(Mock<IPointerDevice> deviceMock, IInputRoot root, IInputElement element)
{
deviceMock.Setup(d => d.ProcessRawEvent(It.IsAny<RawPointerEventArgs>()))
.Callback(() => element.RaiseEvent(CreatePointerMovedArgs(root, element)));
}
protected static Mock<IWindowImpl> CreateTopLevelImplMock(IRenderer renderer)
{
var impl = new Mock<IWindowImpl>();
impl.DefaultValue = DefaultValue.Mock;
impl.SetupAllProperties();
impl.SetupGet(r => r.RenderScaling).Returns(1);
impl.Setup(r => r.CreateRenderer(It.IsAny<IRenderRoot>())).Returns(renderer);
impl.Setup(r => r.PointToScreen(It.IsAny<Point>())).Returns<Point>(p => new PixelPoint((int)p.X, (int)p.Y));
impl.Setup(r => r.PointToClient(It.IsAny<PixelPoint>())).Returns<PixelPoint>(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<Window>((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<IPointer>().Object, root,
positition ?? default, default, PointerPointProperties.None, KeyModifiers.None);
}
protected static Mock<IPointerDevice> CreatePointerDeviceMock(
IPointer? pointer = null,
PointerType pointerType = PointerType.Mouse)
{
if (pointer is null)
{
var pointerMock = new Mock<IPointer>();
pointerMock.SetupGet(p => p.Type).Returns(pointerType);
pointer = pointerMock.Object;
}
var pointerDevice = new Mock<IPointerDevice>();
pointerDevice.Setup(d => d.TryGetPointer(It.IsAny<RawPointerEventArgs>()))
.Returns(pointer);
return pointerDevice;
}
}
Loading…
Cancel
Save