DeferredRendererTests_HitTesting.cs 19 KB

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