VisualExtensionsTests_GetVisualsAt.cs 18 KB

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