MouseDeviceTests.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Avalonia.Controls;
  2. using Avalonia.Input.Raw;
  3. using Avalonia.Rendering;
  4. using Avalonia.UnitTests;
  5. using Avalonia.VisualTree;
  6. using Moq;
  7. using System;
  8. using Xunit;
  9. namespace Avalonia.Input.UnitTests
  10. {
  11. public class MouseDeviceTests
  12. {
  13. [Fact]
  14. public void Capture_Is_Cleared_When_Control_Removed()
  15. {
  16. Canvas control;
  17. var root = new TestRoot
  18. {
  19. Child = control = new Canvas(),
  20. };
  21. var target = new MouseDevice();
  22. target.Capture(control);
  23. Assert.Same(control, target.Captured);
  24. root.Child = null;
  25. Assert.Null(target.Captured);
  26. }
  27. [Fact]
  28. public void MouseMove_Should_Update_PointerOver()
  29. {
  30. var renderer = new Mock<IRenderer>();
  31. using (TestApplication(renderer.Object))
  32. {
  33. var inputManager = InputManager.Instance;
  34. Canvas canvas;
  35. Border border;
  36. Decorator decorator;
  37. var root = new TestRoot
  38. {
  39. MouseDevice = new MouseDevice(),
  40. Renderer = renderer.Object,
  41. Child = new Panel
  42. {
  43. Children =
  44. {
  45. (canvas = new Canvas()),
  46. (border = new Border
  47. {
  48. Child = decorator = new Decorator(),
  49. })
  50. }
  51. }
  52. };
  53. renderer.Setup(x => x.HitTest(It.IsAny<Point>(), It.IsAny<IVisual>(), It.IsAny<Func<IVisual, bool>>()))
  54. .Returns(new[] { decorator });
  55. inputManager.ProcessInput(new RawMouseEventArgs(
  56. root.MouseDevice,
  57. 0,
  58. root,
  59. RawMouseEventType.Move,
  60. new Point(),
  61. InputModifiers.None));
  62. Assert.True(decorator.IsPointerOver);
  63. Assert.True(border.IsPointerOver);
  64. Assert.False(canvas.IsPointerOver);
  65. Assert.True(root.IsPointerOver);
  66. renderer.Setup(x => x.HitTest(It.IsAny<Point>(), It.IsAny<IVisual>(), It.IsAny<Func<IVisual, bool>>()))
  67. .Returns(new[] { canvas });
  68. inputManager.ProcessInput(new RawMouseEventArgs(
  69. root.MouseDevice,
  70. 0,
  71. root,
  72. RawMouseEventType.Move,
  73. new Point(),
  74. InputModifiers.None));
  75. Assert.False(decorator.IsPointerOver);
  76. Assert.False(border.IsPointerOver);
  77. Assert.True(canvas.IsPointerOver);
  78. Assert.True(root.IsPointerOver);
  79. }
  80. }
  81. private IDisposable TestApplication(IRenderer renderer)
  82. {
  83. return UnitTestApplication.Start(
  84. new TestServices(inputManager: new InputManager()));
  85. }
  86. }
  87. }