FindDeepestViewTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. 
  2. #nullable enable
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewTests;
  5. /// <summary>
  6. /// Tests View.FindDeepestView
  7. /// </summary>
  8. /// <param name="output"></param>
  9. public class FindDeepestViewTests (ITestOutputHelper output)
  10. {
  11. [Theory]
  12. [InlineData (0, 0, 0, 0, 0, -1, -1, null)]
  13. [InlineData (0, 0, 0, 0, 0, 0, 0, typeof (View))]
  14. [InlineData (0, 0, 0, 0, 0, 1, 1, typeof (View))]
  15. [InlineData (0, 0, 0, 0, 0, 4, 4, typeof (View))]
  16. [InlineData (0, 0, 0, 0, 0, 9, 9, typeof (View))]
  17. [InlineData (0, 0, 0, 0, 0, 10, 10, null)]
  18. [InlineData (1, 1, 0, 0, 0, -1, -1, null)]
  19. [InlineData (1, 1, 0, 0, 0, 0, 0, null)]
  20. [InlineData (1, 1, 0, 0, 0, 1, 1, typeof (View))]
  21. [InlineData (1, 1, 0, 0, 0, 4, 4, typeof (View))]
  22. [InlineData (1, 1, 0, 0, 0, 9, 9, typeof (View))]
  23. [InlineData (1, 1, 0, 0, 0, 10, 10, typeof (View))]
  24. [InlineData (0, 0, 1, 0, 0, -1, -1, null)]
  25. [InlineData (0, 0, 1, 0, 0, 0, 0, typeof (Margin))]
  26. [InlineData (0, 0, 1, 0, 0, 1, 1, typeof (View))]
  27. [InlineData (0, 0, 1, 0, 0, 4, 4, typeof (View))]
  28. [InlineData (0, 0, 1, 0, 0, 9, 9, typeof (Margin))]
  29. [InlineData (0, 0, 1, 0, 0, 10, 10, null)]
  30. [InlineData (0, 0, 1, 1, 0, -1, -1, null)]
  31. [InlineData (0, 0, 1, 1, 0, 0, 0, typeof (Margin))]
  32. [InlineData (0, 0, 1, 1, 0, 1, 1, typeof (Border))]
  33. [InlineData (0, 0, 1, 1, 0, 4, 4, typeof (View))]
  34. [InlineData (0, 0, 1, 1, 0, 9, 9, typeof (Margin))]
  35. [InlineData (0, 0, 1, 1, 0, 10, 10, null)]
  36. [InlineData (0, 0, 1, 1, 1, -1, -1, null)]
  37. [InlineData (0, 0, 1, 1, 1, 0, 0, typeof (Margin))]
  38. [InlineData (0, 0, 1, 1, 1, 1, 1, typeof (Border))]
  39. [InlineData (0, 0, 1, 1, 1, 2, 2, typeof (Padding))]
  40. [InlineData (0, 0, 1, 1, 1, 4, 4, typeof (View))]
  41. [InlineData (0, 0, 1, 1, 1, 9, 9, typeof (Margin))]
  42. [InlineData (0, 0, 1, 1, 1, 10, 10, null)]
  43. [InlineData (1, 1, 1, 0, 0, -1, -1, null)]
  44. [InlineData (1, 1, 1, 0, 0, 0, 0, null)]
  45. [InlineData (1, 1, 1, 0, 0, 1, 1, typeof (Margin))]
  46. [InlineData (1, 1, 1, 0, 0, 4, 4, typeof (View))]
  47. [InlineData (1, 1, 1, 0, 0, 9, 9, typeof (View))]
  48. [InlineData (1, 1, 1, 0, 0, 10, 10, typeof (Margin))]
  49. [InlineData (1, 1, 1, 1, 0, -1, -1, null)]
  50. [InlineData (1, 1, 1, 1, 0, 0, 0, null)]
  51. [InlineData (1, 1, 1, 1, 0, 1, 1, typeof (Margin))]
  52. [InlineData (1, 1, 1, 1, 0, 4, 4, typeof (View))]
  53. [InlineData (1, 1, 1, 1, 0, 9, 9, typeof (Border))]
  54. [InlineData (1, 1, 1, 1, 0, 10, 10, typeof (Margin))]
  55. [InlineData (1, 1, 1, 1, 1, -1, -1, null)]
  56. [InlineData (1, 1, 1, 1, 1, 0, 0, null)]
  57. [InlineData (1, 1, 1, 1, 1, 1, 1, typeof (Margin))]
  58. [InlineData (1, 1, 1, 1, 1, 2, 2, typeof (Border))]
  59. [InlineData (1, 1, 1, 1, 1, 3, 3, typeof (Padding))]
  60. [InlineData (1, 1, 1, 1, 1, 4, 4, typeof (View))]
  61. [InlineData (1, 1, 1, 1, 1, 8, 8, typeof (Padding))]
  62. [InlineData (1, 1, 1, 1, 1, 9, 9, typeof (Border))]
  63. [InlineData (1, 1, 1, 1, 1, 10, 10, typeof (Margin))]
  64. public void Contains (int frameX, int frameY, int marginThickness, int borderThickness, int paddingThinkcness, int testX, int testY, Type? expectedAdornmentType)
  65. {
  66. var view = new View ()
  67. {
  68. X = frameX, Y = frameY,
  69. Width = 10, Height = 10,
  70. };
  71. view.Margin.Thickness = new Thickness (marginThickness);
  72. view.Border.Thickness = new Thickness (borderThickness);
  73. view.Padding.Thickness = new Thickness (paddingThinkcness);
  74. Type? containedType = null;
  75. if (view.Contains (testX, testY))
  76. {
  77. containedType = view.GetType ();
  78. }
  79. if (view.Margin.Contains (testX, testY))
  80. {
  81. containedType = view.Margin.GetType ();
  82. }
  83. if (view.Border.Contains (testX, testY))
  84. {
  85. containedType = view.Border.GetType ();
  86. }
  87. if (view.Padding.Contains (testX, testY))
  88. {
  89. containedType = view.Padding.GetType ();
  90. }
  91. Assert.Equal (expectedAdornmentType, containedType);
  92. }
  93. // Test that FindDeepestView returns the correct view if the start view has no subviews
  94. [Theory]
  95. [InlineData (0, 0)]
  96. [InlineData (1, 1)]
  97. [InlineData (2, 2)]
  98. public void Returns_Start_If_No_SubViews (int testX, int testY)
  99. {
  100. var start = new View ()
  101. {
  102. Width = 10, Height = 10,
  103. };
  104. Assert.Same (start, View.FindDeepestView (start, testX, testY));
  105. }
  106. // Test that FindDeepestView returns null if the start view has no subviews and coords are outside the view
  107. [Theory]
  108. [InlineData (0, 0)]
  109. [InlineData (2, 1)]
  110. [InlineData (20, 20)]
  111. public void Returns_Null_If_No_SubViews_Coords_Outside (int testX, int testY)
  112. {
  113. var start = new View ()
  114. {
  115. X = 1, Y = 2,
  116. Width = 10, Height = 10,
  117. };
  118. Assert.Null (View.FindDeepestView (start, testX, testY));
  119. }
  120. [Theory]
  121. [InlineData (0, 0)]
  122. [InlineData (2, 1)]
  123. [InlineData (20, 20)]
  124. public void Returns_Null_If_Start_Not_Visible (int testX, int testY)
  125. {
  126. var start = new View ()
  127. {
  128. X = 1, Y = 2,
  129. Width = 10, Height = 10,
  130. Visible = false,
  131. };
  132. Assert.Null (View.FindDeepestView (start, testX, testY));
  133. }
  134. // Test that FindDeepestView returns the correct view if the start view has subviews
  135. [Theory]
  136. [InlineData (0, 0, false)]
  137. [InlineData (1, 1, false)]
  138. [InlineData (9, 9, false)]
  139. [InlineData (10, 10, false)]
  140. [InlineData (6, 7, false)]
  141. [InlineData (1, 2, true)]
  142. [InlineData (5, 6, true)]
  143. public void Returns_Correct_If_SubViews (int testX, int testY, bool expectedSubViewFound)
  144. {
  145. var start = new View ()
  146. {
  147. Width = 10, Height = 10,
  148. };
  149. var subview = new View ()
  150. {
  151. X = 1, Y = 2,
  152. Width = 5, Height = 5,
  153. };
  154. start.Add (subview);
  155. var found = View.FindDeepestView (start, testX, testY);
  156. Assert.Equal (expectedSubViewFound, found == subview);
  157. }
  158. [Theory]
  159. [InlineData (0, 0, false)]
  160. [InlineData (1, 1, false)]
  161. [InlineData (9, 9, false)]
  162. [InlineData (10, 10, false)]
  163. [InlineData (6, 7, false)]
  164. [InlineData (1, 2, false)]
  165. [InlineData (5, 6, false)]
  166. public void Returns_Null_If_SubView_NotVisible (int testX, int testY, bool expectedSubViewFound)
  167. {
  168. var start = new View ()
  169. {
  170. Width = 10, Height = 10,
  171. };
  172. var subview = new View ()
  173. {
  174. X = 1, Y = 2,
  175. Width = 5, Height = 5,
  176. Visible = false
  177. };
  178. start.Add (subview);
  179. var found = View.FindDeepestView (start, testX, testY);
  180. Assert.Equal (expectedSubViewFound, found == subview);
  181. }
  182. [Theory]
  183. [InlineData (0, 0, false)]
  184. [InlineData (1, 1, false)]
  185. [InlineData (9, 9, false)]
  186. [InlineData (10, 10, false)]
  187. [InlineData (6, 7, false)]
  188. [InlineData (1, 2, false)]
  189. [InlineData (5, 6, false)]
  190. public void Returns_Null_If_Not_Visible_And_SubView_Visible (int testX, int testY, bool expectedSubViewFound)
  191. {
  192. var start = new View ()
  193. {
  194. Width = 10, Height = 10,
  195. Visible = false
  196. };
  197. var subview = new View ()
  198. {
  199. X = 1, Y = 2,
  200. Width = 5, Height = 5,
  201. };
  202. start.Add (subview);
  203. subview.Visible = true;
  204. Assert.True (subview.Visible);
  205. Assert.False (start.Visible);
  206. var found = View.FindDeepestView (start, testX, testY);
  207. Assert.Equal (expectedSubViewFound, found == subview);
  208. }
  209. // Test that FindDeepestView works if the start view has positive Adornments
  210. [Theory]
  211. [InlineData (0, 0, false)]
  212. [InlineData (1, 1, false)]
  213. [InlineData (9, 9, false)]
  214. [InlineData (10, 10, false)]
  215. [InlineData (7, 8, false)]
  216. [InlineData (1, 2, false)]
  217. [InlineData (2, 3, true)]
  218. [InlineData (5, 6, true)]
  219. [InlineData (2, 3, true)]
  220. [InlineData (6, 7, true)]
  221. public void Returns_Correct_If_Start_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
  222. {
  223. var start = new View ()
  224. {
  225. Width = 10, Height = 10,
  226. };
  227. start.Margin.Thickness = new Thickness (1);
  228. var subview = new View ()
  229. {
  230. X = 1, Y = 2,
  231. Width = 5, Height = 5,
  232. };
  233. start.Add (subview);
  234. var found = View.FindDeepestView (start, testX, testY);
  235. Assert.Equal (expectedSubViewFound, found == subview);
  236. }
  237. [Theory]
  238. [InlineData (0, 0, false)]
  239. [InlineData (1, 1, false)]
  240. [InlineData (9, 9, true)]
  241. [InlineData (10, 10, false)]
  242. [InlineData (7, 8, false)]
  243. [InlineData (1, 2, false)]
  244. [InlineData (2, 3, false)]
  245. [InlineData (5, 6, false)]
  246. [InlineData (2, 3, false)]
  247. [InlineData (6, 7, false)]
  248. public void Returns_Correct_If_Start_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
  249. {
  250. var start = new View ()
  251. {
  252. Width = 10, Height = 10,
  253. };
  254. start.Padding.Thickness = new Thickness (1);
  255. var subview = new View ()
  256. {
  257. X = Pos.AnchorEnd(1), Y = Pos.AnchorEnd(1),
  258. Width = 1, Height = 1,
  259. };
  260. start.Padding.Add (subview);
  261. start.BeginInit();
  262. start.EndInit();
  263. var found = View.FindDeepestView (start, testX, testY);
  264. Assert.Equal (expectedSubViewFound, found == subview);
  265. }
  266. [Theory]
  267. [InlineData (0, 0, typeof (Margin))]
  268. [InlineData (9, 9, typeof (Margin))]
  269. [InlineData (1, 1, typeof (Border))]
  270. [InlineData (8, 8, typeof (Border))]
  271. [InlineData (2, 2, typeof (Padding))]
  272. [InlineData (7, 7, typeof (Padding))]
  273. [InlineData (5, 5, typeof (View))]
  274. public void Returns_Adornment_If_Start_Has_Adornments (int testX, int testY, Type expectedAdornmentType)
  275. {
  276. var start = new View ()
  277. {
  278. Width = 10, Height = 10,
  279. };
  280. start.Margin.Thickness = new Thickness (1);
  281. start.Border.Thickness = new Thickness (1);
  282. start.Padding.Thickness = new Thickness (1);
  283. var subview = new View ()
  284. {
  285. X = 1, Y = 1,
  286. Width = 1, Height = 1,
  287. };
  288. start.Add (subview);
  289. var found = View.FindDeepestView (start, testX, testY);
  290. Assert.Equal (expectedAdornmentType, found.GetType ());
  291. }
  292. // Test that FindDeepestView works if the subview has positive Adornments
  293. [Theory]
  294. [InlineData (0, 0, false)]
  295. [InlineData (1, 1, false)]
  296. [InlineData (9, 9, false)]
  297. [InlineData (10, 10, false)]
  298. [InlineData (7, 8, false)]
  299. [InlineData (6, 7, false)]
  300. [InlineData (1, 2, false)]
  301. [InlineData (5, 6, false)]
  302. [InlineData (2, 3, true)]
  303. [InlineData (2, 3, true)]
  304. public void Returns_Correct_If_SubView_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
  305. {
  306. var start = new View ()
  307. {
  308. Width = 10, Height = 10,
  309. };
  310. var subview = new View ()
  311. {
  312. X = 1, Y = 2,
  313. Width = 5, Height = 5,
  314. };
  315. subview.Margin.Thickness = new Thickness (1);
  316. start.Add (subview);
  317. var found = View.FindDeepestView (start, testX, testY);
  318. Assert.Equal (expectedSubViewFound, found == subview);
  319. }
  320. [Theory]
  321. [InlineData (0, 0, false)]
  322. [InlineData (1, 1, false)]
  323. [InlineData (9, 9, false)]
  324. [InlineData (10, 10, false)]
  325. [InlineData (7, 8, false)]
  326. [InlineData (6, 7, false)]
  327. [InlineData (1, 2, false)]
  328. [InlineData (5, 6, false)]
  329. [InlineData (6, 5, false)]
  330. [InlineData (5, 5, true)]
  331. public void Returns_Correct_If_SubView_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
  332. {
  333. var start = new View ()
  334. {
  335. Width = 10, Height = 10,
  336. };
  337. // A subview with + Padding
  338. var subview = new View ()
  339. {
  340. X = 1, Y = 1,
  341. Width = 5, Height = 5,
  342. };
  343. subview.Padding.Thickness = new (1);
  344. // This subview will be at the bottom-right-corner of subview
  345. // So screen-relative location will be X + Width - 1 = 5
  346. var paddingSubview = new View ()
  347. {
  348. X = Pos.AnchorEnd (1),
  349. Y = Pos.AnchorEnd (1),
  350. Width = 1,
  351. Height = 1,
  352. };
  353. subview.Padding.Add (paddingSubview);
  354. start.Add (subview);
  355. start.BeginInit();
  356. start.EndInit();
  357. var found = View.FindDeepestView (start, testX, testY);
  358. Assert.Equal (expectedSubViewFound, found == paddingSubview);
  359. }
  360. [Theory]
  361. [InlineData (0, 0, false)]
  362. [InlineData (1, 1, false)]
  363. [InlineData (9, 9, false)]
  364. [InlineData (10, 10, false)]
  365. [InlineData (7, 8, false)]
  366. [InlineData (6, 7, false)]
  367. [InlineData (1, 2, false)]
  368. [InlineData (5, 6, false)]
  369. [InlineData (6, 5, false)]
  370. [InlineData (5, 5, true)]
  371. public void Returns_Correct_If_SubView_Is_Scrolled_And_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
  372. {
  373. var start = new View ()
  374. {
  375. Width = 10, Height = 10,
  376. };
  377. // A subview with + Padding
  378. var subview = new View ()
  379. {
  380. X = 1, Y = 1,
  381. Width = 5, Height = 5,
  382. };
  383. subview.Padding.Thickness = new (1);
  384. // Scroll the subview
  385. subview.ContentSize = new Size (10, 10);
  386. subview.Viewport = subview.Viewport with { Location = new (1, 1) };
  387. // This subview will be at the bottom-right-corner of subview
  388. // So screen-relative location will be X + Width - 1 = 5
  389. var paddingSubview = new View ()
  390. {
  391. X = Pos.AnchorEnd (1),
  392. Y = Pos.AnchorEnd (1),
  393. Width = 1,
  394. Height = 1,
  395. };
  396. subview.Padding.Add (paddingSubview);
  397. start.Add (subview);
  398. start.BeginInit ();
  399. start.EndInit ();
  400. var found = View.FindDeepestView (start, testX, testY);
  401. Assert.Equal (expectedSubViewFound, found == paddingSubview);
  402. }
  403. // Test that FindDeepestView works with nested subviews
  404. [Theory]
  405. [InlineData (0, 0, -1)]
  406. [InlineData (9, 9, -1)]
  407. [InlineData (10, 10, -1)]
  408. [InlineData (1, 1, 0)]
  409. [InlineData (1, 2, 0)]
  410. [InlineData (2, 2, 1)]
  411. [InlineData (3, 3, 2)]
  412. [InlineData (5, 5, 2)]
  413. public void Returns_Correct_With_NestedSubViews (int testX, int testY, int expectedSubViewFound)
  414. {
  415. var start = new View ()
  416. {
  417. Width = 10, Height = 10
  418. };
  419. int numSubViews = 3;
  420. List<View> subviews = new List<View> ();
  421. for (int i = 0; i < numSubViews; i++)
  422. {
  423. var subview = new View ()
  424. {
  425. X = 1, Y = 1,
  426. Width = 5, Height = 5,
  427. };
  428. subviews.Add (subview);
  429. if (i > 0)
  430. {
  431. subviews [i - 1].Add (subview);
  432. }
  433. }
  434. start.Add (subviews [0]);
  435. var found = View.FindDeepestView (start, testX, testY);
  436. Assert.Equal (expectedSubViewFound, subviews.IndexOf (found));
  437. }
  438. }