FindDeepestViewTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 ()
  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 paddingThickness, 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 (paddingThickness);
  74. Type? containedType = null;
  75. if (view.Contains (new (testX, testY)))
  76. {
  77. containedType = view.GetType ();
  78. }
  79. if (view.Margin.Contains (new (testX, testY)))
  80. {
  81. containedType = view.Margin.GetType ();
  82. }
  83. if (view.Border.Contains (new (testX, testY)))
  84. {
  85. containedType = view.Border.GetType ();
  86. }
  87. if (view.Padding.Contains (new (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, new (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, new (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, new (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, new (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, new (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, new (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 (6, 7, true)]
  220. public void Returns_Correct_If_Start_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
  221. {
  222. var start = new View ()
  223. {
  224. Width = 10, Height = 10,
  225. };
  226. start.Margin.Thickness = new Thickness (1);
  227. var subview = new View ()
  228. {
  229. X = 1, Y = 2,
  230. Width = 5, Height = 5,
  231. };
  232. start.Add (subview);
  233. var found = View.FindDeepestView (start, new (testX, testY));
  234. Assert.Equal (expectedSubViewFound, found == subview);
  235. }
  236. // Test that FindDeepestView works if the start view has offset Viewport location
  237. [Theory]
  238. [InlineData (1, 0, 0, true)]
  239. [InlineData (1, 1, 1, true)]
  240. [InlineData (1, 2, 2, false)]
  241. [InlineData (-1, 3, 3, true)]
  242. [InlineData (-1, 2, 2, true)]
  243. [InlineData (-1, 1, 1, false)]
  244. [InlineData (-1, 0, 0, false)]
  245. public void Returns_Correct_If_Start_Has_Offset_Viewport (int offset, int testX, int testY, bool expectedSubViewFound)
  246. {
  247. var start = new View ()
  248. {
  249. Width = 10, Height = 10,
  250. ViewportSettings = ViewportSettings.AllowNegativeLocation
  251. };
  252. start.Viewport = new (offset, offset, 10, 10);
  253. var subview = new View ()
  254. {
  255. X = 1, Y = 1,
  256. Width = 2, Height = 2,
  257. };
  258. start.Add (subview);
  259. var found = View.FindDeepestView (start, new (testX, testY));
  260. Assert.Equal (expectedSubViewFound, found == subview);
  261. }
  262. [Theory]
  263. [InlineData (9, 9, true)]
  264. [InlineData (0, 0, false)]
  265. [InlineData (1, 1, false)]
  266. [InlineData (10, 10, false)]
  267. [InlineData (7, 8, false)]
  268. [InlineData (1, 2, false)]
  269. [InlineData (2, 3, false)]
  270. [InlineData (5, 6, false)]
  271. [InlineData (6, 7, false)]
  272. public void Returns_Correct_If_Start_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
  273. {
  274. var start = new View ()
  275. {
  276. Width = 10, Height = 10,
  277. };
  278. start.Padding.Thickness = new Thickness (1);
  279. var subview = new View ()
  280. {
  281. X = Pos.AnchorEnd(1), Y = Pos.AnchorEnd(1),
  282. Width = 1, Height = 1,
  283. };
  284. start.Padding.Add (subview);
  285. start.BeginInit();
  286. start.EndInit();
  287. var found = View.FindDeepestView (start, new (testX, testY));
  288. Assert.Equal (expectedSubViewFound, found == subview);
  289. }
  290. [Theory]
  291. [InlineData (0, 0, typeof (Margin))]
  292. [InlineData (9, 9, typeof (Margin))]
  293. [InlineData (1, 1, typeof (Border))]
  294. [InlineData (8, 8, typeof (Border))]
  295. [InlineData (2, 2, typeof (Padding))]
  296. [InlineData (7, 7, typeof (Padding))]
  297. [InlineData (5, 5, typeof (View))]
  298. public void Returns_Adornment_If_Start_Has_Adornments (int testX, int testY, Type expectedAdornmentType)
  299. {
  300. var start = new View ()
  301. {
  302. Width = 10, Height = 10,
  303. };
  304. start.Margin.Thickness = new Thickness (1);
  305. start.Border.Thickness = new Thickness (1);
  306. start.Padding.Thickness = new Thickness (1);
  307. var subview = new View ()
  308. {
  309. X = 1, Y = 1,
  310. Width = 1, Height = 1,
  311. };
  312. start.Add (subview);
  313. var found = View.FindDeepestView (start, new (testX, testY));
  314. Assert.Equal (expectedAdornmentType, found!.GetType ());
  315. }
  316. // Test that FindDeepestView works if the subview has positive Adornments
  317. [Theory]
  318. [InlineData (0, 0, false)]
  319. [InlineData (1, 1, false)]
  320. [InlineData (9, 9, false)]
  321. [InlineData (10, 10, false)]
  322. [InlineData (7, 8, false)]
  323. [InlineData (6, 7, false)]
  324. [InlineData (1, 2, false)]
  325. [InlineData (5, 6, false)]
  326. [InlineData (2, 3, true)]
  327. public void Returns_Correct_If_SubView_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
  328. {
  329. var start = new View ()
  330. {
  331. Width = 10, Height = 10,
  332. };
  333. var subview = new View ()
  334. {
  335. X = 1, Y = 2,
  336. Width = 5, Height = 5,
  337. };
  338. subview.Margin.Thickness = new Thickness (1);
  339. start.Add (subview);
  340. var found = View.FindDeepestView (start, new (testX, testY));
  341. Assert.Equal (expectedSubViewFound, found == subview);
  342. }
  343. [Theory]
  344. [InlineData (0, 0, false)]
  345. [InlineData (1, 1, false)]
  346. [InlineData (9, 9, false)]
  347. [InlineData (10, 10, false)]
  348. [InlineData (7, 8, false)]
  349. [InlineData (6, 7, false)]
  350. [InlineData (1, 2, false)]
  351. [InlineData (5, 6, false)]
  352. [InlineData (6, 5, false)]
  353. [InlineData (5, 5, true)]
  354. public void Returns_Correct_If_SubView_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
  355. {
  356. var start = new View ()
  357. {
  358. Width = 10, Height = 10,
  359. };
  360. // A subview with + Padding
  361. var subview = new View ()
  362. {
  363. X = 1, Y = 1,
  364. Width = 5, Height = 5,
  365. };
  366. subview.Padding.Thickness = new (1);
  367. // This subview will be at the bottom-right-corner of subview
  368. // So screen-relative location will be X + Width - 1 = 5
  369. var paddingSubview = new View ()
  370. {
  371. X = Pos.AnchorEnd (1),
  372. Y = Pos.AnchorEnd (1),
  373. Width = 1,
  374. Height = 1,
  375. };
  376. subview.Padding.Add (paddingSubview);
  377. start.Add (subview);
  378. start.BeginInit();
  379. start.EndInit();
  380. var found = View.FindDeepestView (start, new (testX, testY));
  381. Assert.Equal (expectedSubViewFound, found == paddingSubview);
  382. }
  383. [Theory]
  384. [InlineData (0, 0, false)]
  385. [InlineData (1, 1, false)]
  386. [InlineData (9, 9, false)]
  387. [InlineData (10, 10, false)]
  388. [InlineData (7, 8, false)]
  389. [InlineData (6, 7, false)]
  390. [InlineData (1, 2, false)]
  391. [InlineData (5, 6, false)]
  392. [InlineData (6, 5, false)]
  393. [InlineData (5, 5, true)]
  394. public void Returns_Correct_If_SubView_Is_Scrolled_And_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
  395. {
  396. var start = new View ()
  397. {
  398. Width = 10, Height = 10,
  399. };
  400. // A subview with + Padding
  401. var subview = new View ()
  402. {
  403. X = 1, Y = 1,
  404. Width = 5, Height = 5,
  405. };
  406. subview.Padding.Thickness = new (1);
  407. // Scroll the subview
  408. subview.SetContentSize (new (10, 10));
  409. subview.Viewport = subview.Viewport with { Location = new (1, 1) };
  410. // This subview will be at the bottom-right-corner of subview
  411. // So screen-relative location will be X + Width - 1 = 5
  412. var paddingSubview = new View ()
  413. {
  414. X = Pos.AnchorEnd (1),
  415. Y = Pos.AnchorEnd (1),
  416. Width = 1,
  417. Height = 1,
  418. };
  419. subview.Padding.Add (paddingSubview);
  420. start.Add (subview);
  421. start.BeginInit ();
  422. start.EndInit ();
  423. var found = View.FindDeepestView (start, new (testX, testY));
  424. Assert.Equal (expectedSubViewFound, found == paddingSubview);
  425. }
  426. // Test that FindDeepestView works with nested subviews
  427. [Theory]
  428. [InlineData (0, 0, -1)]
  429. [InlineData (9, 9, -1)]
  430. [InlineData (10, 10, -1)]
  431. [InlineData (1, 1, 0)]
  432. [InlineData (1, 2, 0)]
  433. [InlineData (2, 2, 1)]
  434. [InlineData (3, 3, 2)]
  435. [InlineData (5, 5, 2)]
  436. public void Returns_Correct_With_NestedSubViews (int testX, int testY, int expectedSubViewFound)
  437. {
  438. var start = new View ()
  439. {
  440. Width = 10, Height = 10
  441. };
  442. int numSubViews = 3;
  443. List<View> subviews = new List<View> ();
  444. for (int i = 0; i < numSubViews; i++)
  445. {
  446. var subview = new View ()
  447. {
  448. X = 1, Y = 1,
  449. Width = 5, Height = 5,
  450. };
  451. subviews.Add (subview);
  452. if (i > 0)
  453. {
  454. subviews [i - 1].Add (subview);
  455. }
  456. }
  457. start.Add (subviews [0]);
  458. var found = View.FindDeepestView (start, new (testX, testY));
  459. Assert.Equal (expectedSubViewFound, subviews.IndexOf (found!));
  460. }
  461. }