VisualExtensionsTests_GetVisualsAt.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. using System;
  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 (TestApplication())
  22. {
  23. var container = new TestRoot
  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 (TestApplication())
  47. {
  48. var container = new TestRoot
  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 (TestApplication())
  78. {
  79. var container = new TestRoot
  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 (TestApplication())
  103. {
  104. Panel container;
  105. var root = new TestRoot
  106. {
  107. Child = container = new Panel
  108. {
  109. Width = 200,
  110. Height = 200,
  111. Children = new Controls.Controls
  112. {
  113. new Border
  114. {
  115. Width = 100,
  116. Height = 100,
  117. HorizontalAlignment = HorizontalAlignment.Center,
  118. VerticalAlignment = VerticalAlignment.Center
  119. },
  120. new Border
  121. {
  122. Width = 50,
  123. Height = 50,
  124. HorizontalAlignment = HorizontalAlignment.Center,
  125. VerticalAlignment = VerticalAlignment.Center
  126. }
  127. }
  128. }
  129. };
  130. container.Measure(Size.Infinity);
  131. container.Arrange(new Rect(container.DesiredSize));
  132. var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
  133. context.Render(container);
  134. var result = container.GetVisualsAt(new Point(100, 100));
  135. Assert.Equal(new[] { container.Children[1], container.Children[0], container }, result);
  136. }
  137. }
  138. [Fact]
  139. public void GetVisualsAt_Should_Return_Top_Controls_First_With_ZIndex()
  140. {
  141. using (TestApplication())
  142. {
  143. Panel container;
  144. var root = new TestRoot
  145. {
  146. Child = container = new Panel
  147. {
  148. Width = 200,
  149. Height = 200,
  150. Children = new Controls.Controls
  151. {
  152. new Border
  153. {
  154. Width = 100,
  155. Height = 100,
  156. ZIndex = 1,
  157. HorizontalAlignment = HorizontalAlignment.Center,
  158. VerticalAlignment = VerticalAlignment.Center
  159. },
  160. new Border
  161. {
  162. Width = 50,
  163. Height = 50,
  164. HorizontalAlignment = HorizontalAlignment.Center,
  165. VerticalAlignment = VerticalAlignment.Center
  166. },
  167. new Border
  168. {
  169. Width = 75,
  170. Height = 75,
  171. ZIndex = 2,
  172. HorizontalAlignment = HorizontalAlignment.Center,
  173. VerticalAlignment = VerticalAlignment.Center
  174. }
  175. }
  176. }
  177. };
  178. container.Measure(Size.Infinity);
  179. container.Arrange(new Rect(container.DesiredSize));
  180. var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
  181. context.Render(container);
  182. var result = container.GetVisualsAt(new Point(100, 100));
  183. Assert.Equal(new[] { container.Children[2], container.Children[0], container.Children[1], container }, result);
  184. }
  185. }
  186. [Fact]
  187. public void GetVisualsAt_Should_Find_Control_Translated_Outside_Parent_Bounds()
  188. {
  189. using (TestApplication())
  190. {
  191. Border target;
  192. Panel container;
  193. var root = new TestRoot
  194. {
  195. Child = container = new Panel
  196. {
  197. Width = 200,
  198. Height = 200,
  199. ClipToBounds = false,
  200. Children = new Controls.Controls
  201. {
  202. new Border
  203. {
  204. Width = 100,
  205. Height = 100,
  206. ZIndex = 1,
  207. HorizontalAlignment = HorizontalAlignment.Left,
  208. VerticalAlignment = VerticalAlignment.Top,
  209. Child = target = new Border
  210. {
  211. Width = 50,
  212. Height = 50,
  213. HorizontalAlignment = HorizontalAlignment.Left,
  214. VerticalAlignment = VerticalAlignment.Top,
  215. RenderTransform = new TranslateTransform(110, 110),
  216. }
  217. },
  218. }
  219. }
  220. };
  221. container.Measure(Size.Infinity);
  222. container.Arrange(new Rect(container.DesiredSize));
  223. var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
  224. context.Render(container);
  225. var result = container.GetVisualsAt(new Point(120, 120));
  226. Assert.Equal(new IVisual[] { target, container }, result);
  227. }
  228. }
  229. [Fact]
  230. public void GetVisualsAt_Should_Not_Find_Control_Outside_Parent_Bounds_When_Clipped()
  231. {
  232. using (TestApplication())
  233. {
  234. Border target;
  235. Panel container;
  236. var root = new TestRoot
  237. {
  238. Child = container = new Panel
  239. {
  240. Width = 100,
  241. Height = 200,
  242. Children = new Controls.Controls
  243. {
  244. new Panel()
  245. {
  246. Width = 100,
  247. Height = 100,
  248. Margin = new Thickness(0, 100, 0, 0),
  249. ClipToBounds = true,
  250. Children = new Controls.Controls
  251. {
  252. (target = new Border()
  253. {
  254. Width = 100,
  255. Height = 100,
  256. Margin = new Thickness(0, -100, 0, 0)
  257. })
  258. }
  259. }
  260. }
  261. }
  262. };
  263. container.Measure(Size.Infinity);
  264. container.Arrange(new Rect(container.DesiredSize));
  265. var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
  266. context.Render(container);
  267. var result = container.GetVisualsAt(new Point(50, 50));
  268. Assert.Equal(new[] { container }, result);
  269. }
  270. }
  271. [Fact]
  272. public void GetVisualsAt_Should_Not_Find_Control_Outside_Scroll_Viewport()
  273. {
  274. using (TestApplication())
  275. {
  276. Border target;
  277. Border item1;
  278. Border item2;
  279. ScrollContentPresenter scroll;
  280. Panel container;
  281. var root = new TestRoot
  282. {
  283. Child = container = new Panel
  284. {
  285. Width = 100,
  286. Height = 200,
  287. Children = new Controls.Controls
  288. {
  289. (target = new Border()
  290. {
  291. Width = 100,
  292. Height = 100
  293. }),
  294. new Border()
  295. {
  296. Width = 100,
  297. Height = 100,
  298. Margin = new Thickness(0, 100, 0, 0),
  299. Child = scroll = new ScrollContentPresenter()
  300. {
  301. Content = new StackPanel()
  302. {
  303. Children = new Controls.Controls
  304. {
  305. (item1 = new Border()
  306. {
  307. Width = 100,
  308. Height = 100,
  309. }),
  310. (item2 = new Border()
  311. {
  312. Width = 100,
  313. Height = 100,
  314. }),
  315. }
  316. }
  317. }
  318. }
  319. }
  320. }
  321. };
  322. scroll.UpdateChild();
  323. container.Measure(Size.Infinity);
  324. container.Arrange(new Rect(container.DesiredSize));
  325. var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
  326. context.Render(container);
  327. var result = container.GetVisualsAt(new Point(50, 150)).First();
  328. Assert.Equal(item1, result);
  329. result = container.GetVisualsAt(new Point(50, 50)).First();
  330. Assert.Equal(target, result);
  331. scroll.Offset = new Vector(0, 100);
  332. //we don't have setup LayoutManager so we will make it manually
  333. scroll.Parent.InvalidateArrange();
  334. container.InvalidateArrange();
  335. container.Arrange(new Rect(container.DesiredSize));
  336. context.Render(container);
  337. result = container.GetVisualsAt(new Point(50, 150)).First();
  338. Assert.Equal(item2, result);
  339. result = container.GetVisualsAt(new Point(50, 50)).First();
  340. Assert.NotEqual(item1, result);
  341. Assert.Equal(target, result);
  342. }
  343. }
  344. private IDisposable TestApplication()
  345. {
  346. return UnitTestApplication.Start(
  347. new TestServices(
  348. renderInterface: new MockRenderInterface(),
  349. renderLoop: Mock.Of<IRenderLoop>(),
  350. renderer: (root, loop) => new DeferredRenderer(root, loop)));
  351. }
  352. }
  353. }