PointerTestsBase.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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
  16. {
  17. private protected static void SetHit(Mock<IHitTester> renderer, Control? hit)
  18. {
  19. renderer.Setup(x => x.HitTest(It.IsAny<Point>(), It.IsAny<Visual>(), It.IsAny<Func<Visual, bool>>()))
  20. .Returns(hit is null ? Array.Empty<Control>() : new[] { hit });
  21. renderer.Setup(x => x.HitTestFirst(It.IsAny<Point>(), It.IsAny<Visual>(), It.IsAny<Func<Visual, bool>>()))
  22. .Returns(hit);
  23. }
  24. protected static void SetMove(Mock<IPointerDevice> deviceMock, IInputRoot root, IInputElement element)
  25. {
  26. deviceMock.Setup(d => d.ProcessRawEvent(It.IsAny<RawPointerEventArgs>()))
  27. .Callback(() => element.RaiseEvent(CreatePointerMovedArgs(root, element)));
  28. }
  29. private protected static Mock<IWindowImpl> CreateTopLevelImplMock()
  30. {
  31. var impl = new Mock<IWindowImpl>();
  32. impl.DefaultValue = DefaultValue.Mock;
  33. impl.SetupAllProperties();
  34. impl.SetupGet(r => r.RenderScaling).Returns(1);
  35. impl.Setup(r => r.TryGetFeature(It.IsAny<Type>())).Returns(null);
  36. impl.Setup(r => r.Compositor).Returns(RendererMocks.CreateDummyCompositor());
  37. impl.Setup(r => r.PointToScreen(It.IsAny<Point>())).Returns<Point>(p => new PixelPoint((int)p.X, (int)p.Y));
  38. impl.Setup(r => r.PointToClient(It.IsAny<PixelPoint>())).Returns<PixelPoint>(p => new Point(p.X, p.Y));
  39. return impl;
  40. }
  41. private protected static TopLevel CreateInputRoot(IWindowImpl impl, Control child, IHitTester hitTester)
  42. {
  43. var root = new Window(impl)
  44. {
  45. Width = 100,
  46. Height = 100,
  47. Content = child,
  48. Template = new FuncControlTemplate<Window>((w, _) => new ContentPresenter { Content = w.Content }),
  49. HitTesterOverride = hitTester
  50. };
  51. root.Show();
  52. return root;
  53. }
  54. protected static RawPointerEventArgs CreateRawPointerArgs(
  55. IPointerDevice pointerDevice,
  56. IInputRoot root,
  57. RawPointerEventType type,
  58. Point? position = default)
  59. {
  60. return new RawPointerEventArgs(pointerDevice, 0, root, type, position ?? default, default);
  61. }
  62. protected static RawPointerEventArgs CreateRawPointerMovedArgs(
  63. IPointerDevice pointerDevice,
  64. IInputRoot root,
  65. Point? position = null)
  66. {
  67. return new RawPointerEventArgs(pointerDevice, 0, root, RawPointerEventType.Move,
  68. position ?? default, default);
  69. }
  70. protected static PointerEventArgs CreatePointerMovedArgs(
  71. IInputRoot root, IInputElement? source, Point? position = null)
  72. {
  73. return new PointerEventArgs(InputElement.PointerMovedEvent, source, new Mock<IPointer>().Object, (Visual)root,
  74. position ?? default, default, PointerPointProperties.None, KeyModifiers.None);
  75. }
  76. protected static Mock<IPointerDevice> CreatePointerDeviceMock(
  77. IPointer? pointer = null,
  78. PointerType pointerType = PointerType.Mouse)
  79. {
  80. if (pointer is null)
  81. {
  82. var pointerMock = new Mock<IPointer>();
  83. pointerMock.SetupGet(p => p.Type).Returns(pointerType);
  84. pointer = pointerMock.Object;
  85. }
  86. var pointerDevice = new Mock<IPointerDevice>();
  87. pointerDevice.Setup(d => d.TryGetPointer(It.IsAny<RawPointerEventArgs>()))
  88. .Returns(pointer);
  89. return pointerDevice;
  90. }
  91. }