PointerTestsBase.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #nullable enable
  2. using System;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Presenters;
  5. using Avalonia.Controls.Templates;
  6. using Avalonia.Input;
  7. using Avalonia.Input.Raw;
  8. using Avalonia.Platform;
  9. using Avalonia.Rendering;
  10. using Avalonia.Rendering.Composition;
  11. using Avalonia.UnitTests;
  12. using Avalonia.VisualTree;
  13. using Moq;
  14. namespace Avalonia.Base.UnitTests.Input;
  15. public abstract class PointerTestsBase : ScopedTestBase
  16. {
  17. protected class TestPointer : Pointer
  18. {
  19. internal int PlatformCaptureCalled = 0;
  20. internal TestPointer(int id, PointerType type, bool isPrimary) : base(id, type, isPrimary) { }
  21. protected override void PlatformCapture(IInputElement? element)
  22. {
  23. PlatformCaptureCalled++;
  24. }
  25. }
  26. private protected static void SetHit(Mock<IHitTester> renderer, Control? hit)
  27. {
  28. renderer.Setup(x => x.HitTest(It.IsAny<Point>(), It.IsAny<Visual>(), It.IsAny<Func<Visual, bool>>()))
  29. .Returns(hit is null ? Array.Empty<Control>() : new[] { hit });
  30. renderer.Setup(x => x.HitTestFirst(It.IsAny<Point>(), It.IsAny<Visual>(), It.IsAny<Func<Visual, bool>>()))
  31. .Returns(hit);
  32. }
  33. protected static void SetMove(Mock<IPointerDevice> deviceMock, IInputRoot root, IInputElement element)
  34. {
  35. deviceMock.Setup(d => d.ProcessRawEvent(It.IsAny<RawPointerEventArgs>()))
  36. .Callback(() => element.RaiseEvent(CreatePointerMovedArgs(root, element)));
  37. }
  38. private protected static Mock<IWindowImpl> CreateTopLevelImplMock()
  39. {
  40. var impl = new Mock<IWindowImpl>();
  41. impl.DefaultValue = DefaultValue.Mock;
  42. impl.SetupAllProperties();
  43. impl.SetupGet(r => r.RenderScaling).Returns(1);
  44. impl.Setup(r => r.TryGetFeature(It.IsAny<Type>())).Returns(null);
  45. impl.Setup(r => r.Compositor).Returns(RendererMocks.CreateDummyCompositor());
  46. impl.Setup(r => r.PointToScreen(It.IsAny<Point>())).Returns<Point>(p => new PixelPoint((int)p.X, (int)p.Y));
  47. impl.Setup(r => r.PointToClient(It.IsAny<PixelPoint>())).Returns<PixelPoint>(p => new Point(p.X, p.Y));
  48. var screen1 = new Mock<Screen>(1.75, new PixelRect(new PixelSize(1920, 1080)), new PixelRect(new PixelSize(1920, 966)), true);
  49. var screens = new Mock<IScreenImpl>();
  50. screens.Setup(x => x.ScreenFromWindow(It.IsAny<IWindowBaseImpl>())).Returns(screen1.Object);
  51. impl.Setup(x => x.TryGetFeature(It.Is<Type>(t => t == typeof(IScreenImpl)))).Returns(screens.Object);
  52. return impl;
  53. }
  54. private protected static TopLevel CreateInputRoot(IWindowImpl impl, Control child, IHitTester hitTester)
  55. {
  56. var root = new Window(impl)
  57. {
  58. Width = 100,
  59. Height = 100,
  60. Content = child,
  61. Template = new FuncControlTemplate<Window>((w, _) => new ContentPresenter { Content = w.Content }),
  62. HitTesterOverride = hitTester
  63. };
  64. root.Show();
  65. return root;
  66. }
  67. protected static RawPointerEventArgs CreateRawPointerArgs(
  68. IPointerDevice pointerDevice,
  69. IInputRoot root,
  70. RawPointerEventType type,
  71. Point? position = default)
  72. {
  73. return new RawPointerEventArgs(pointerDevice, 0, root, type, position ?? default, default);
  74. }
  75. protected static RawPointerEventArgs CreateRawPointerMovedArgs(
  76. IPointerDevice pointerDevice,
  77. IInputRoot root,
  78. Point? position = null)
  79. {
  80. return new RawPointerEventArgs(pointerDevice, 0, root, RawPointerEventType.Move,
  81. position ?? default, default);
  82. }
  83. protected static PointerEventArgs CreatePointerMovedArgs(
  84. IInputRoot root, IInputElement? source, Point? position = null)
  85. {
  86. return new PointerEventArgs(InputElement.PointerMovedEvent, source, new Mock<IPointer>().Object, (Visual)root,
  87. position ?? default, default, PointerPointProperties.None, KeyModifiers.None);
  88. }
  89. protected static Mock<IPointerDevice> CreatePointerDeviceMock(
  90. IPointer? pointer = null,
  91. PointerType pointerType = PointerType.Mouse)
  92. {
  93. if (pointer is null)
  94. {
  95. var pointerMock = new Mock<IPointer>();
  96. pointerMock.SetupGet(p => p.Type).Returns(pointerType);
  97. pointer = pointerMock.Object;
  98. }
  99. var pointerDevice = new Mock<IPointerDevice>();
  100. pointerDevice.Setup(d => d.TryGetPointer(It.IsAny<RawPointerEventArgs>()))
  101. .Returns(pointer);
  102. return pointerDevice;
  103. }
  104. }