FindDeepestViewTests.cs 16 KB

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