VisualExtensions_GetVisualsAt.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Linq;
  3. using Avalonia.Controls;
  4. using Avalonia.Layout;
  5. using Avalonia.Media;
  6. using Avalonia.Rendering;
  7. using Avalonia.UnitTests;
  8. using Avalonia.VisualTree;
  9. using Xunit;
  10. namespace Avalonia.Base.UnitTests.VisualTree
  11. {
  12. public class VisualExtensions_GetVisualsAt
  13. {
  14. [Fact]
  15. public void Should_Find_Control()
  16. {
  17. using (TestApplication())
  18. {
  19. Border target;
  20. var root = new TestRoot
  21. {
  22. Width = 200,
  23. Height = 200,
  24. Child = new StackPanel
  25. {
  26. Background = Brushes.White,
  27. Children =
  28. {
  29. (target = new Border
  30. {
  31. Width = 100,
  32. Height = 200,
  33. Background = Brushes.Red,
  34. }),
  35. new Border
  36. {
  37. Width = 100,
  38. Height = 200,
  39. Background = Brushes.Green,
  40. }
  41. },
  42. Orientation = Orientation.Horizontal,
  43. }
  44. };
  45. root.Renderer = new DeferredRenderer((IRenderRoot)root, null);
  46. root.Measure(Size.Infinity);
  47. root.Arrange(new Rect(root.DesiredSize));
  48. var result = target.GetVisualsAt(new Point(50, 50));
  49. Assert.Same(target, result.Single());
  50. }
  51. }
  52. [Fact]
  53. public void Should_Not_Find_Sibling_Control()
  54. {
  55. using (TestApplication())
  56. {
  57. Border target;
  58. var root = new TestRoot
  59. {
  60. Width = 200,
  61. Height = 200,
  62. Child = new StackPanel
  63. {
  64. Background = Brushes.White,
  65. Children =
  66. {
  67. (target = new Border
  68. {
  69. Width = 100,
  70. Height = 200,
  71. Background = Brushes.Red,
  72. }),
  73. new Border
  74. {
  75. Width = 100,
  76. Height = 200,
  77. Background = Brushes.Green,
  78. }
  79. },
  80. Orientation = Orientation.Horizontal,
  81. }
  82. };
  83. root.Renderer = new DeferredRenderer((IRenderRoot)root, null);
  84. root.Measure(Size.Infinity);
  85. root.Arrange(new Rect(root.DesiredSize));
  86. var result = target.GetVisualsAt(new Point(150, 50));
  87. Assert.Empty(result);
  88. }
  89. }
  90. private static IDisposable TestApplication()
  91. {
  92. return UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  93. }
  94. }
  95. }