PointerTests.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Avalonia.Controls;
  4. using Avalonia.Input;
  5. using Avalonia.UnitTests;
  6. using Avalonia.VisualTree;
  7. using Xunit;
  8. namespace Avalonia.Base.UnitTests.Input
  9. {
  10. public class PointerTests : PointerTestsBase
  11. {
  12. [Fact]
  13. public void On_Capture_Transfer_PointerCaptureLost_Should_Propagate_Up_To_The_Common_Parent()
  14. {
  15. Border initialParent, initialCapture, newParent, newCapture;
  16. var el = new StackPanel
  17. {
  18. Children =
  19. {
  20. (initialParent = new Border { Child = initialCapture = new Border() }),
  21. (newParent = new Border { Child = newCapture = new Border() })
  22. }
  23. };
  24. var receivers = new List<object>();
  25. var root = new TestRoot(el);
  26. foreach (InputElement d in root.GetSelfAndVisualDescendants())
  27. d.PointerCaptureLost += (s, e) => receivers.Add(s);
  28. var pointer = new Pointer(Pointer.GetNextFreeId(), PointerType.Mouse, true);
  29. pointer.Capture(initialCapture);
  30. pointer.Capture(newCapture);
  31. Assert.True(receivers.SequenceEqual(new[] { initialCapture, initialParent }));
  32. receivers.Clear();
  33. pointer.Capture(null);
  34. Assert.True(receivers.SequenceEqual(new object[] { newCapture, newParent, el, root }));
  35. }
  36. [Fact]
  37. public void Capture_Captured_ShouldNot_Call_Platform()
  38. {
  39. var pointer = new TestPointer(Pointer.GetNextFreeId(), PointerType.Mouse, true);
  40. Border capture = new Border();
  41. pointer.Capture(capture);
  42. pointer.Capture(capture);
  43. Assert.Equal(1, pointer.PlatformCaptureCalled);
  44. pointer.Capture(null);
  45. pointer.Capture(null);
  46. Assert.Equal(2, pointer.PlatformCaptureCalled);
  47. }
  48. }
  49. }