ImmediateRendererTests_HitTesting.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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;
  4. using System.Linq;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.Presenters;
  7. using Avalonia.Layout;
  8. using Avalonia.Media;
  9. using Avalonia.Rendering;
  10. using Avalonia.UnitTests;
  11. using Avalonia.VisualTree;
  12. using Xunit;
  13. namespace Avalonia.Visuals.UnitTests.Rendering
  14. {
  15. public class ImmediateRendererTests_HitTesting
  16. {
  17. [Fact]
  18. public void HitTest_Should_Find_Controls_At_Point()
  19. {
  20. using (TestApplication())
  21. {
  22. var root = new TestRoot
  23. {
  24. Width = 200,
  25. Height = 200,
  26. Child = new Border
  27. {
  28. Width = 100,
  29. Height = 100,
  30. Background = Brushes.Red,
  31. HorizontalAlignment = HorizontalAlignment.Center,
  32. VerticalAlignment = VerticalAlignment.Center
  33. }
  34. };
  35. root.Renderer = new ImmediateRenderer(root);
  36. root.Measure(Size.Infinity);
  37. root.Arrange(new Rect(root.DesiredSize));
  38. root.Renderer.Paint(new Rect(root.ClientSize));
  39. var result = root.Renderer.HitTest(new Point(100, 100), root, null);
  40. Assert.Equal(new[] { root.Child, root }, result);
  41. }
  42. }
  43. [Fact]
  44. public void HitTest_Should_Not_Find_Invisible_Controls_At_Point()
  45. {
  46. using (TestApplication())
  47. {
  48. Border visible;
  49. var root = new TestRoot
  50. {
  51. Width = 200,
  52. Height = 200,
  53. Child = new Border
  54. {
  55. Width = 100,
  56. Height = 100,
  57. Background = Brushes.Red,
  58. HorizontalAlignment = HorizontalAlignment.Center,
  59. VerticalAlignment = VerticalAlignment.Center,
  60. IsVisible = false,
  61. Child = visible = new Border
  62. {
  63. Background = Brushes.Red,
  64. HorizontalAlignment = HorizontalAlignment.Stretch,
  65. VerticalAlignment = VerticalAlignment.Stretch,
  66. }
  67. }
  68. };
  69. root.Renderer = new ImmediateRenderer(root);
  70. root.Measure(Size.Infinity);
  71. root.Arrange(new Rect(root.DesiredSize));
  72. root.Renderer.Paint(new Rect(root.ClientSize));
  73. var result = root.Renderer.HitTest(new Point(100, 100), root, null);
  74. Assert.Equal(new[] { root }, result);
  75. }
  76. }
  77. [Fact]
  78. public void HitTest_Should_Not_Find_Control_Outside_Point()
  79. {
  80. using (TestApplication())
  81. {
  82. var root = new TestRoot
  83. {
  84. Width = 200,
  85. Height = 200,
  86. Child = new Border
  87. {
  88. Width = 100,
  89. Height = 100,
  90. Background = Brushes.Red,
  91. HorizontalAlignment = HorizontalAlignment.Center,
  92. VerticalAlignment = VerticalAlignment.Center
  93. }
  94. };
  95. root.Renderer = new ImmediateRenderer(root);
  96. root.Measure(Size.Infinity);
  97. root.Arrange(new Rect(root.DesiredSize));
  98. root.Renderer.Paint(new Rect(root.ClientSize));
  99. var result = root.Renderer.HitTest(new Point(10, 10), root, null);
  100. Assert.Equal(new[] { root }, result);
  101. }
  102. }
  103. [Fact]
  104. public void HitTest_Should_Return_Top_Controls_First()
  105. {
  106. using (TestApplication())
  107. {
  108. Panel container;
  109. var root = new TestRoot
  110. {
  111. Child = container = new Panel
  112. {
  113. Width = 200,
  114. Height = 200,
  115. Children =
  116. {
  117. new Border
  118. {
  119. Width = 100,
  120. Height = 100,
  121. Background = Brushes.Red,
  122. HorizontalAlignment = HorizontalAlignment.Center,
  123. VerticalAlignment = VerticalAlignment.Center
  124. },
  125. new Border
  126. {
  127. Width = 50,
  128. Height = 50,
  129. Background = Brushes.Red,
  130. HorizontalAlignment = HorizontalAlignment.Center,
  131. VerticalAlignment = VerticalAlignment.Center
  132. }
  133. }
  134. }
  135. };
  136. root.Renderer = new ImmediateRenderer(root);
  137. root.Measure(Size.Infinity);
  138. root.Arrange(new Rect(container.DesiredSize));
  139. root.Renderer.Paint(new Rect(root.ClientSize));
  140. var result = root.Renderer.HitTest(new Point(100, 100), root, null);
  141. Assert.Equal(new[] { container.Children[1], container.Children[0], container, root }, result);
  142. }
  143. }
  144. [Fact]
  145. public void HitTest_Should_Return_Top_Controls_First_With_ZIndex()
  146. {
  147. using (TestApplication())
  148. {
  149. Panel container;
  150. var root = new TestRoot
  151. {
  152. Child = container = new Panel
  153. {
  154. Width = 200,
  155. Height = 200,
  156. Children =
  157. {
  158. new Border
  159. {
  160. Width = 100,
  161. Height = 100,
  162. ZIndex = 1,
  163. Background = Brushes.Red,
  164. HorizontalAlignment = HorizontalAlignment.Center,
  165. VerticalAlignment = VerticalAlignment.Center
  166. },
  167. new Border
  168. {
  169. Width = 50,
  170. Height = 50,
  171. Background = Brushes.Red,
  172. HorizontalAlignment = HorizontalAlignment.Center,
  173. VerticalAlignment = VerticalAlignment.Center
  174. },
  175. new Border
  176. {
  177. Width = 75,
  178. Height = 75,
  179. ZIndex = 2,
  180. Background = Brushes.Red,
  181. HorizontalAlignment = HorizontalAlignment.Center,
  182. VerticalAlignment = VerticalAlignment.Center
  183. }
  184. }
  185. }
  186. };
  187. root.Renderer = new ImmediateRenderer(root);
  188. root.Measure(Size.Infinity);
  189. root.Arrange(new Rect(container.DesiredSize));
  190. root.Renderer.Paint(new Rect(root.ClientSize));
  191. var result = root.Renderer.HitTest(new Point(100, 100), root, null);
  192. Assert.Equal(
  193. new[]
  194. {
  195. container.Children[2],
  196. container.Children[0],
  197. container.Children[1],
  198. container,
  199. root
  200. },
  201. result);
  202. }
  203. }
  204. [Fact]
  205. public void HitTest_Should_Find_Control_Translated_Outside_Parent_Bounds()
  206. {
  207. using (TestApplication())
  208. {
  209. Border target;
  210. Panel container;
  211. var root = new TestRoot
  212. {
  213. Child = container = new Panel
  214. {
  215. Width = 200,
  216. Height = 200,
  217. Background = Brushes.Red,
  218. ClipToBounds = false,
  219. Children =
  220. {
  221. new Border
  222. {
  223. Width = 100,
  224. Height = 100,
  225. ZIndex = 1,
  226. Background = Brushes.Red,
  227. HorizontalAlignment = HorizontalAlignment.Left,
  228. VerticalAlignment = VerticalAlignment.Top,
  229. Child = target = new Border
  230. {
  231. Width = 50,
  232. Height = 50,
  233. Background = Brushes.Red,
  234. HorizontalAlignment = HorizontalAlignment.Left,
  235. VerticalAlignment = VerticalAlignment.Top,
  236. RenderTransform = new TranslateTransform(110, 110),
  237. }
  238. },
  239. }
  240. }
  241. };
  242. root.Renderer = new ImmediateRenderer(root);
  243. container.Measure(Size.Infinity);
  244. container.Arrange(new Rect(container.DesiredSize));
  245. root.Renderer.Paint(new Rect(root.ClientSize));
  246. var result = root.Renderer.HitTest(new Point(120, 120), root, null);
  247. Assert.Equal(new IVisual[] { target, container }, result);
  248. }
  249. }
  250. [Fact]
  251. public void HitTest_Should_Not_Find_Control_Outside_Parent_Bounds_When_Clipped()
  252. {
  253. using (TestApplication())
  254. {
  255. Border target;
  256. Panel container;
  257. var root = new TestRoot
  258. {
  259. Child = container = new Panel
  260. {
  261. Width = 100,
  262. Height = 200,
  263. Background = Brushes.Red,
  264. Children =
  265. {
  266. new Panel()
  267. {
  268. Width = 100,
  269. Height = 100,
  270. Background = Brushes.Red,
  271. Margin = new Thickness(0, 100, 0, 0),
  272. ClipToBounds = true,
  273. Children =
  274. {
  275. (target = new Border()
  276. {
  277. Width = 100,
  278. Height = 100,
  279. Background = Brushes.Red,
  280. Margin = new Thickness(0, -100, 0, 0)
  281. })
  282. }
  283. }
  284. }
  285. }
  286. };
  287. root.Renderer = new ImmediateRenderer(root);
  288. root.Measure(Size.Infinity);
  289. root.Arrange(new Rect(container.DesiredSize));
  290. root.Renderer.Paint(new Rect(root.ClientSize));
  291. var result = root.Renderer.HitTest(new Point(50, 50), root, null);
  292. Assert.Equal(new IVisual[] { container, root }, result);
  293. }
  294. }
  295. [Fact]
  296. public void HitTest_Should_Not_Find_Control_Outside_Scroll_Viewport()
  297. {
  298. using (TestApplication())
  299. {
  300. Border target;
  301. Border item1;
  302. Border item2;
  303. ScrollContentPresenter scroll;
  304. Panel container;
  305. var root = new TestRoot
  306. {
  307. Child = container = new Panel
  308. {
  309. Width = 100,
  310. Height = 200,
  311. Background = Brushes.Red,
  312. Children =
  313. {
  314. (target = new Border()
  315. {
  316. Name = "b1",
  317. Width = 100,
  318. Height = 100,
  319. Background = Brushes.Red,
  320. }),
  321. new Border()
  322. {
  323. Name = "b2",
  324. Width = 100,
  325. Height = 100,
  326. Background = Brushes.Red,
  327. Margin = new Thickness(0, 100, 0, 0),
  328. Child = scroll = new ScrollContentPresenter()
  329. {
  330. Content = new StackPanel()
  331. {
  332. Children =
  333. {
  334. (item1 = new Border()
  335. {
  336. Name = "b3",
  337. Width = 100,
  338. Height = 100,
  339. Background = Brushes.Red,
  340. }),
  341. (item2 = new Border()
  342. {
  343. Name = "b4",
  344. Width = 100,
  345. Height = 100,
  346. Background = Brushes.Red,
  347. }),
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }
  354. };
  355. scroll.UpdateChild();
  356. root.Renderer = new ImmediateRenderer(root);
  357. root.Measure(Size.Infinity);
  358. root.Arrange(new Rect(container.DesiredSize));
  359. root.Renderer.Paint(new Rect(root.ClientSize));
  360. var result = root.Renderer.HitTest(new Point(50, 150), root, null).First();
  361. Assert.Equal(item1, result);
  362. result = root.Renderer.HitTest(new Point(50, 50), root, null).First();
  363. Assert.Equal(target, result);
  364. scroll.Offset = new Vector(0, 100);
  365. // We don't have LayoutManager set up so do the layout pass manually.
  366. scroll.Parent.InvalidateArrange();
  367. container.InvalidateArrange();
  368. container.Arrange(new Rect(container.DesiredSize));
  369. root.Renderer.Paint(new Rect(root.ClientSize));
  370. result = root.Renderer.HitTest(new Point(50, 150), root, null).First();
  371. Assert.Equal(item2, result);
  372. result = root.Renderer.HitTest(new Point(50, 50), root, null).First();
  373. Assert.Equal(target, result);
  374. }
  375. }
  376. private IDisposable TestApplication()
  377. {
  378. return UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  379. }
  380. }
  381. }