ImmediateRendererTests_HitTesting.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. using System;
  2. using System.Linq;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Presenters;
  5. using Avalonia.Layout;
  6. using Avalonia.Media;
  7. using Avalonia.Rendering;
  8. using Avalonia.UnitTests;
  9. using Avalonia.VisualTree;
  10. using Xunit;
  11. namespace Avalonia.Base.UnitTests.Rendering
  12. {
  13. public class ImmediateRendererTests_HitTesting
  14. {
  15. [Fact]
  16. public void HitTest_Should_Find_Controls_At_Point()
  17. {
  18. using (TestApplication())
  19. {
  20. var root = new TestRoot
  21. {
  22. Width = 200,
  23. Height = 200,
  24. Child = new Border
  25. {
  26. Width = 100,
  27. Height = 100,
  28. Background = Brushes.Red,
  29. HorizontalAlignment = HorizontalAlignment.Center,
  30. VerticalAlignment = VerticalAlignment.Center
  31. }
  32. };
  33. root.Renderer = new ImmediateRenderer(root);
  34. root.Measure(Size.Infinity);
  35. root.Arrange(new Rect(root.DesiredSize));
  36. root.Renderer.Paint(new Rect(root.ClientSize));
  37. var result = root.Renderer.HitTest(new Point(100, 100), root, null);
  38. Assert.Equal(new[] { root.Child, root }, result);
  39. }
  40. }
  41. [Fact]
  42. public void HitTest_Should_Not_Find_Invisible_Controls_At_Point()
  43. {
  44. using (TestApplication())
  45. {
  46. Border visible;
  47. var root = new TestRoot
  48. {
  49. Width = 200,
  50. Height = 200,
  51. Child = new Border
  52. {
  53. Width = 100,
  54. Height = 100,
  55. Background = Brushes.Red,
  56. HorizontalAlignment = HorizontalAlignment.Center,
  57. VerticalAlignment = VerticalAlignment.Center,
  58. IsVisible = false,
  59. Child = visible = new Border
  60. {
  61. Background = Brushes.Red,
  62. HorizontalAlignment = HorizontalAlignment.Stretch,
  63. VerticalAlignment = VerticalAlignment.Stretch,
  64. }
  65. }
  66. };
  67. root.Renderer = new ImmediateRenderer(root);
  68. root.Measure(Size.Infinity);
  69. root.Arrange(new Rect(root.DesiredSize));
  70. root.Renderer.Paint(new Rect(root.ClientSize));
  71. var result = root.Renderer.HitTest(new Point(100, 100), root, null);
  72. Assert.Equal(new[] { root }, result);
  73. }
  74. }
  75. [Fact]
  76. public void HitTest_Should_Not_Find_Control_Outside_Point()
  77. {
  78. using (TestApplication())
  79. {
  80. var root = new TestRoot
  81. {
  82. Width = 200,
  83. Height = 200,
  84. Child = new Border
  85. {
  86. Width = 100,
  87. Height = 100,
  88. Background = Brushes.Red,
  89. HorizontalAlignment = HorizontalAlignment.Center,
  90. VerticalAlignment = VerticalAlignment.Center
  91. }
  92. };
  93. root.Renderer = new ImmediateRenderer(root);
  94. root.Measure(Size.Infinity);
  95. root.Arrange(new Rect(root.DesiredSize));
  96. root.Renderer.Paint(new Rect(root.ClientSize));
  97. var result = root.Renderer.HitTest(new Point(10, 10), root, null);
  98. Assert.Equal(new[] { root }, result);
  99. }
  100. }
  101. [Fact]
  102. public void HitTest_Should_Return_Top_Controls_First()
  103. {
  104. using (TestApplication())
  105. {
  106. Panel container;
  107. var root = new TestRoot
  108. {
  109. Child = container = new Panel
  110. {
  111. Width = 200,
  112. Height = 200,
  113. Children =
  114. {
  115. new Border
  116. {
  117. Width = 100,
  118. Height = 100,
  119. Background = Brushes.Red,
  120. HorizontalAlignment = HorizontalAlignment.Center,
  121. VerticalAlignment = VerticalAlignment.Center
  122. },
  123. new Border
  124. {
  125. Width = 50,
  126. Height = 50,
  127. Background = Brushes.Red,
  128. HorizontalAlignment = HorizontalAlignment.Center,
  129. VerticalAlignment = VerticalAlignment.Center
  130. }
  131. }
  132. }
  133. };
  134. root.Renderer = new ImmediateRenderer(root);
  135. root.Measure(Size.Infinity);
  136. root.Arrange(new Rect(container.DesiredSize));
  137. root.Renderer.Paint(new Rect(root.ClientSize));
  138. var result = root.Renderer.HitTest(new Point(100, 100), root, null);
  139. Assert.Equal(new[] { container.Children[1], container.Children[0], container, root }, result);
  140. }
  141. }
  142. [Fact]
  143. public void HitTest_Should_Return_Top_Controls_First_With_ZIndex()
  144. {
  145. using (TestApplication())
  146. {
  147. Panel container;
  148. var root = new TestRoot
  149. {
  150. Child = container = new Panel
  151. {
  152. Width = 200,
  153. Height = 200,
  154. Children =
  155. {
  156. new Border
  157. {
  158. Width = 100,
  159. Height = 100,
  160. ZIndex = 1,
  161. Background = Brushes.Red,
  162. HorizontalAlignment = HorizontalAlignment.Center,
  163. VerticalAlignment = VerticalAlignment.Center
  164. },
  165. new Border
  166. {
  167. Width = 50,
  168. Height = 50,
  169. Background = Brushes.Red,
  170. HorizontalAlignment = HorizontalAlignment.Center,
  171. VerticalAlignment = VerticalAlignment.Center
  172. },
  173. new Border
  174. {
  175. Width = 75,
  176. Height = 75,
  177. ZIndex = 2,
  178. Background = Brushes.Red,
  179. HorizontalAlignment = HorizontalAlignment.Center,
  180. VerticalAlignment = VerticalAlignment.Center
  181. }
  182. }
  183. }
  184. };
  185. root.Renderer = new ImmediateRenderer(root);
  186. root.Measure(Size.Infinity);
  187. root.Arrange(new Rect(container.DesiredSize));
  188. root.Renderer.Paint(new Rect(root.ClientSize));
  189. var result = root.Renderer.HitTest(new Point(100, 100), root, null);
  190. Assert.Equal(
  191. new[]
  192. {
  193. container.Children[2],
  194. container.Children[0],
  195. container.Children[1],
  196. container,
  197. root
  198. },
  199. result);
  200. }
  201. }
  202. [Fact]
  203. public void HitTest_Should_Find_Control_Translated_Outside_Parent_Bounds()
  204. {
  205. using (TestApplication())
  206. {
  207. Border target;
  208. Panel container;
  209. var root = new TestRoot
  210. {
  211. Child = container = new Panel
  212. {
  213. Width = 200,
  214. Height = 200,
  215. Background = Brushes.Red,
  216. ClipToBounds = false,
  217. Children =
  218. {
  219. new Border
  220. {
  221. Width = 100,
  222. Height = 100,
  223. ZIndex = 1,
  224. Background = Brushes.Red,
  225. HorizontalAlignment = HorizontalAlignment.Left,
  226. VerticalAlignment = VerticalAlignment.Top,
  227. Child = target = new Border
  228. {
  229. Width = 50,
  230. Height = 50,
  231. Background = Brushes.Red,
  232. HorizontalAlignment = HorizontalAlignment.Left,
  233. VerticalAlignment = VerticalAlignment.Top,
  234. RenderTransform = new TranslateTransform(110, 110),
  235. }
  236. },
  237. }
  238. }
  239. };
  240. root.Renderer = new ImmediateRenderer(root);
  241. container.Measure(Size.Infinity);
  242. container.Arrange(new Rect(container.DesiredSize));
  243. root.Renderer.Paint(new Rect(root.ClientSize));
  244. var result = root.Renderer.HitTest(new Point(120, 120), root, null);
  245. Assert.Equal(new Visual[] { target, container }, result);
  246. }
  247. }
  248. [Fact]
  249. public void HitTest_Should_Not_Find_Control_Outside_Parent_Bounds_When_Clipped()
  250. {
  251. using (TestApplication())
  252. {
  253. Border target;
  254. Panel container;
  255. var root = new TestRoot
  256. {
  257. Child = container = new Panel
  258. {
  259. Width = 100,
  260. Height = 200,
  261. Background = Brushes.Red,
  262. Children =
  263. {
  264. new Panel()
  265. {
  266. Width = 100,
  267. Height = 100,
  268. Background = Brushes.Red,
  269. Margin = new Thickness(0, 100, 0, 0),
  270. ClipToBounds = true,
  271. Children =
  272. {
  273. (target = new Border()
  274. {
  275. Width = 100,
  276. Height = 100,
  277. Background = Brushes.Red,
  278. Margin = new Thickness(0, -100, 0, 0)
  279. })
  280. }
  281. }
  282. }
  283. }
  284. };
  285. root.Renderer = new ImmediateRenderer(root);
  286. root.Measure(Size.Infinity);
  287. root.Arrange(new Rect(container.DesiredSize));
  288. root.Renderer.Paint(new Rect(root.ClientSize));
  289. var result = root.Renderer.HitTest(new Point(50, 50), root, null);
  290. Assert.Equal(new Visual[] { container, root }, result);
  291. }
  292. }
  293. [Fact]
  294. public void HitTest_Should_Not_Find_Control_Outside_Scroll_Viewport()
  295. {
  296. using (TestApplication())
  297. {
  298. Border target;
  299. Border item1;
  300. Border item2;
  301. ScrollContentPresenter scroll;
  302. Panel container;
  303. var root = new TestRoot
  304. {
  305. Child = container = new Panel
  306. {
  307. Width = 100,
  308. Height = 200,
  309. Background = Brushes.Red,
  310. Children =
  311. {
  312. (target = new Border()
  313. {
  314. Name = "b1",
  315. Width = 100,
  316. Height = 100,
  317. Background = Brushes.Red,
  318. }),
  319. new Border()
  320. {
  321. Name = "b2",
  322. Width = 100,
  323. Height = 100,
  324. Background = Brushes.Red,
  325. Margin = new Thickness(0, 100, 0, 0),
  326. Child = scroll = new ScrollContentPresenter()
  327. {
  328. CanHorizontallyScroll = true,
  329. CanVerticallyScroll = true,
  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. [Fact]
  377. public void HitTest_Should_Accommodate_ICustomHitTest()
  378. {
  379. using (TestApplication())
  380. {
  381. Border border;
  382. var root = new TestRoot
  383. {
  384. Width = 300,
  385. Height = 200,
  386. Child = border = new CustomHitTestBorder
  387. {
  388. Width = 100,
  389. Height = 100,
  390. Background = Brushes.Red,
  391. HorizontalAlignment = HorizontalAlignment.Center,
  392. VerticalAlignment = VerticalAlignment.Center
  393. }
  394. };
  395. root.Renderer = new ImmediateRenderer(root);
  396. root.Measure(Size.Infinity);
  397. root.Arrange(new Rect(root.DesiredSize));
  398. root.Renderer.Paint(new Rect(root.ClientSize));
  399. var result = root.Renderer.HitTest(new Point(75, 100), root, null).First();
  400. Assert.Equal(border, result);
  401. result = root.Renderer.HitTest(new Point(125, 100), root, null).First();
  402. Assert.Equal(border, result);
  403. result = root.Renderer.HitTest(new Point(175, 100), root, null).First();
  404. Assert.Equal(root, result);
  405. }
  406. }
  407. private static IDisposable TestApplication()
  408. {
  409. return UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  410. }
  411. }
  412. }