VisualExtensions_GetVisualsAt.cs 3.2 KB

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