FindDeepestViewTests.cs 16 KB

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