VisualExtensionsTests_GetVisualsAt.cs 14 KB

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