VisualExtensionsTests_GetVisualsAt.cs 18 KB

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