VisualExtensionsTests_GetVisualsAt.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System.Linq;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Layout;
  7. using Avalonia.Media;
  8. using Avalonia.Rendering;
  9. using Avalonia.UnitTests;
  10. using Avalonia.VisualTree;
  11. using Moq;
  12. using Xunit;
  13. namespace Avalonia.Visuals.UnitTests.VisualTree
  14. {
  15. public class VisualExtensionsTests_GetVisualsAt
  16. {
  17. [Fact]
  18. public void GetVisualsAt_Should_Find_Controls_At_Point()
  19. {
  20. using (var application = UnitTestApplication.Start(new TestServices(renderInterface: new MockRenderInterface())))
  21. {
  22. var container = new Decorator
  23. {
  24. Width = 200,
  25. Height = 200,
  26. Child = new Border
  27. {
  28. Width = 100,
  29. Height = 100,
  30. HorizontalAlignment = HorizontalAlignment.Center,
  31. VerticalAlignment = VerticalAlignment.Center
  32. }
  33. };
  34. container.Measure(Size.Infinity);
  35. container.Arrange(new Rect(container.DesiredSize));
  36. var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
  37. context.Render(container);
  38. var result = container.GetVisualsAt(new Point(100, 100));
  39. Assert.Equal(new[] { container.Child, container }, result);
  40. }
  41. }
  42. [Fact]
  43. public void GetVisualsAt_Should_Not_Find_Invisible_Controls_At_Point()
  44. {
  45. using (var application = UnitTestApplication.Start(new TestServices(renderInterface: new MockRenderInterface())))
  46. {
  47. var container = new Decorator
  48. {
  49. Width = 200,
  50. Height = 200,
  51. Child = new Border
  52. {
  53. Width = 100,
  54. Height = 100,
  55. HorizontalAlignment = HorizontalAlignment.Center,
  56. VerticalAlignment = VerticalAlignment.Center,
  57. IsVisible = false,
  58. Child = new Border
  59. {
  60. HorizontalAlignment = HorizontalAlignment.Stretch,
  61. VerticalAlignment = VerticalAlignment.Stretch,
  62. }
  63. }
  64. };
  65. container.Measure(Size.Infinity);
  66. container.Arrange(new Rect(container.DesiredSize));
  67. var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
  68. context.Render(container);
  69. var result = container.GetVisualsAt(new Point(100, 100));
  70. Assert.Equal(new[] { container }, result);
  71. }
  72. }
  73. [Fact]
  74. public void GetVisualsAt_Should_Not_Find_Control_Outside_Point()
  75. {
  76. using (UnitTestApplication.Start(new TestServices(renderInterface: new MockRenderInterface())))
  77. {
  78. var container = new Decorator
  79. {
  80. Width = 200,
  81. Height = 200,
  82. Child = new Border
  83. {
  84. Width = 100,
  85. Height = 100,
  86. HorizontalAlignment = HorizontalAlignment.Center,
  87. VerticalAlignment = VerticalAlignment.Center
  88. }
  89. };
  90. container.Measure(Size.Infinity);
  91. container.Arrange(new Rect(container.DesiredSize));
  92. var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
  93. context.Render(container);
  94. var result = container.GetVisualsAt(new Point(10, 10));
  95. Assert.Equal(new[] { container }, result);
  96. }
  97. }
  98. [Fact]
  99. public void GetVisualsAt_Should_Return_Top_Controls_First()
  100. {
  101. using (UnitTestApplication.Start(new TestServices(renderInterface: new MockRenderInterface())))
  102. {
  103. var container = new Panel
  104. {
  105. Width = 200,
  106. Height = 200,
  107. Children = new Controls.Controls
  108. {
  109. new Border
  110. {
  111. Width = 100,
  112. Height = 100,
  113. HorizontalAlignment = HorizontalAlignment.Center,
  114. VerticalAlignment = VerticalAlignment.Center
  115. },
  116. new Border
  117. {
  118. Width = 50,
  119. Height = 50,
  120. HorizontalAlignment = HorizontalAlignment.Center,
  121. VerticalAlignment = VerticalAlignment.Center
  122. }
  123. }
  124. };
  125. container.Measure(Size.Infinity);
  126. container.Arrange(new Rect(container.DesiredSize));
  127. var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
  128. context.Render(container);
  129. var result = container.GetVisualsAt(new Point(100, 100));
  130. Assert.Equal(new[] { container.Children[1], container.Children[0], container }, result);
  131. }
  132. }
  133. [Fact]
  134. public void GetVisualsAt_Should_Return_Top_Controls_First_With_ZIndex()
  135. {
  136. using (UnitTestApplication.Start(new TestServices(renderInterface: new MockRenderInterface())))
  137. {
  138. var container = new Panel
  139. {
  140. Width = 200,
  141. Height = 200,
  142. Children = new Controls.Controls
  143. {
  144. new Border
  145. {
  146. Width = 100,
  147. Height = 100,
  148. ZIndex = 1,
  149. HorizontalAlignment = HorizontalAlignment.Center,
  150. VerticalAlignment = VerticalAlignment.Center
  151. },
  152. new Border
  153. {
  154. Width = 50,
  155. Height = 50,
  156. HorizontalAlignment = HorizontalAlignment.Center,
  157. VerticalAlignment = VerticalAlignment.Center
  158. },
  159. new Border
  160. {
  161. Width = 75,
  162. Height = 75,
  163. ZIndex = 2,
  164. HorizontalAlignment = HorizontalAlignment.Center,
  165. VerticalAlignment = VerticalAlignment.Center
  166. }
  167. }
  168. };
  169. container.Measure(Size.Infinity);
  170. container.Arrange(new Rect(container.DesiredSize));
  171. var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
  172. context.Render(container);
  173. var result = container.GetVisualsAt(new Point(100, 100));
  174. Assert.Equal(new[] { container.Children[2], container.Children[0], container.Children[1], container }, result);
  175. }
  176. }
  177. [Fact]
  178. public void GetVisualsAt_Should_Find_Control_Translated_Outside_Parent_Bounds()
  179. {
  180. using (UnitTestApplication.Start(new TestServices(renderInterface: new MockRenderInterface())))
  181. {
  182. Border target;
  183. var container = new Panel
  184. {
  185. Width = 200,
  186. Height = 200,
  187. ClipToBounds = false,
  188. Children = new Controls.Controls
  189. {
  190. new Border
  191. {
  192. Width = 100,
  193. Height = 100,
  194. ZIndex = 1,
  195. HorizontalAlignment = HorizontalAlignment.Left,
  196. VerticalAlignment = VerticalAlignment.Top,
  197. Child = target = new Border
  198. {
  199. Width = 50,
  200. Height = 50,
  201. HorizontalAlignment = HorizontalAlignment.Left,
  202. VerticalAlignment = VerticalAlignment.Top,
  203. RenderTransform = new TranslateTransform(110, 110),
  204. }
  205. },
  206. }
  207. };
  208. container.Measure(Size.Infinity);
  209. container.Arrange(new Rect(container.DesiredSize));
  210. var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
  211. context.Render(container);
  212. var result = container.GetVisualsAt(new Point(120, 120));
  213. Assert.Equal(new IVisual[] { target, container }, result);
  214. }
  215. }
  216. [Fact]
  217. public void GetVisualsAt_Should_Not_Find_Control_Outside_Parent_Bounds_When_Clipped()
  218. {
  219. using (UnitTestApplication.Start(new TestServices(renderInterface: new MockRenderInterface())))
  220. {
  221. Border target;
  222. var container = new Panel
  223. {
  224. Width = 100,
  225. Height = 200,
  226. Children = new Controls.Controls
  227. {
  228. new Panel()
  229. {
  230. Width = 100,
  231. Height = 100,
  232. Margin = new Thickness(0, 100, 0, 0),
  233. ClipToBounds = true,
  234. Children = new Controls.Controls
  235. {
  236. (target = new Border()
  237. {
  238. Width = 100,
  239. Height = 100,
  240. Margin = new Thickness(0, -100, 0, 0)
  241. })
  242. }
  243. }
  244. }
  245. };
  246. container.Measure(Size.Infinity);
  247. container.Arrange(new Rect(container.DesiredSize));
  248. var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
  249. context.Render(container);
  250. var result = container.GetVisualsAt(new Point(50, 50));
  251. Assert.Equal(new[] { container }, result);
  252. }
  253. }
  254. [Fact]
  255. public void GetVisualsAt_Should_Not_Find_Control_Outside_Scroll_Viewport()
  256. {
  257. using (UnitTestApplication.Start(new TestServices(renderInterface: new MockRenderInterface())))
  258. {
  259. Border target;
  260. Border item1;
  261. Border item2;
  262. ScrollContentPresenter scroll;
  263. var container = new Panel
  264. {
  265. Width = 100,
  266. Height = 200,
  267. Children = new Controls.Controls
  268. {
  269. (target = new Border()
  270. {
  271. Width = 100,
  272. Height = 100
  273. }),
  274. new Border()
  275. {
  276. Width = 100,
  277. Height = 100,
  278. Margin = new Thickness(0, 100, 0, 0),
  279. Child = scroll = new ScrollContentPresenter()
  280. {
  281. Content = new StackPanel()
  282. {
  283. Children = new Controls.Controls
  284. {
  285. (item1 = new Border()
  286. {
  287. Width = 100,
  288. Height = 100,
  289. }),
  290. (item2 = new Border()
  291. {
  292. Width = 100,
  293. Height = 100,
  294. }),
  295. }
  296. }
  297. }
  298. }
  299. }
  300. };
  301. scroll.UpdateChild();
  302. container.Measure(Size.Infinity);
  303. container.Arrange(new Rect(container.DesiredSize));
  304. var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
  305. context.Render(container);
  306. var result = container.GetVisualsAt(new Point(50, 150)).First();
  307. Assert.Equal(item1, result);
  308. result = container.GetVisualsAt(new Point(50, 50)).First();
  309. Assert.Equal(target, result);
  310. scroll.Offset = new Vector(0, 100);
  311. //we don't have setup LayoutManager so we will make it manually
  312. scroll.Parent.InvalidateArrange();
  313. container.InvalidateArrange();
  314. container.Arrange(new Rect(container.DesiredSize));
  315. context.Render(container);
  316. result = container.GetVisualsAt(new Point(50, 150)).First();
  317. Assert.Equal(item2, result);
  318. result = container.GetVisualsAt(new Point(50, 50)).First();
  319. Assert.NotEqual(item1, result);
  320. Assert.Equal(target, result);
  321. }
  322. }
  323. }
  324. }