VisualExtensions_GetVisualsAt.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. Border target;
  18. using var services = new CompositorTestServices(new Size(200, 200))
  19. {
  20. TopLevel =
  21. {
  22. Content = new StackPanel
  23. {
  24. Background = null,
  25. Children =
  26. {
  27. (target = new Border
  28. {
  29. Width = 100,
  30. Height = 200,
  31. Background = Brushes.Red,
  32. }),
  33. new Border
  34. {
  35. Width = 100,
  36. Height = 200,
  37. Background = Brushes.Green,
  38. }
  39. },
  40. Orientation = Orientation.Horizontal,
  41. }
  42. }
  43. };
  44. services.RunJobs();
  45. var result = target.GetVisualsAt(new Point(50, 50));
  46. Assert.Same(target, result.Single());
  47. }
  48. [Fact]
  49. public void Should_Not_Find_Sibling_Control()
  50. {
  51. Border target;
  52. using var services = new CompositorTestServices(new Size(200, 200))
  53. {
  54. TopLevel =
  55. {
  56. Content = new StackPanel
  57. {
  58. Background = Brushes.White,
  59. Children =
  60. {
  61. (target = new Border
  62. {
  63. Width = 100,
  64. Height = 200,
  65. Background = Brushes.Red,
  66. }),
  67. new Border
  68. {
  69. Width = 100,
  70. Height = 200,
  71. Background = Brushes.Green,
  72. }
  73. },
  74. Orientation = Orientation.Horizontal,
  75. }
  76. }
  77. };
  78. services.RunJobs();
  79. var result = target.GetVisualsAt(new Point(150, 50));
  80. Assert.Empty(result);
  81. }
  82. }
  83. }