FindDeepestViewTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. Application.Top = new ()
  102. {
  103. Width = 10, Height = 10,
  104. };
  105. Assert.Same (Application.Top, View.FindDeepestView (new (testX, testY)));
  106. Application.Top.Dispose ();
  107. Application.ResetState (ignoreDisposed: true);
  108. }
  109. // Test that FindDeepestView returns null if the start view has no subviews and coords are outside the view
  110. [Theory]
  111. [InlineData (0, 0)]
  112. [InlineData (2, 1)]
  113. [InlineData (20, 20)]
  114. public void Returns_Null_If_No_SubViews_Coords_Outside (int testX, int testY)
  115. {
  116. Application.Top = new ()
  117. {
  118. X = 1, Y = 2,
  119. Width = 10, Height = 10,
  120. };
  121. Assert.Null (View.FindDeepestView (new (testX, testY)));
  122. Application.Top.Dispose ();
  123. Application.ResetState (ignoreDisposed: true);
  124. }
  125. [Theory]
  126. [InlineData (0, 0)]
  127. [InlineData (2, 1)]
  128. [InlineData (20, 20)]
  129. public void Returns_Null_If_Start_Not_Visible (int testX, int testY)
  130. {
  131. Application.Top = new ()
  132. {
  133. X = 1, Y = 2,
  134. Width = 10, Height = 10,
  135. Visible = false,
  136. };
  137. Assert.Null (View.FindDeepestView (new (testX, testY)));
  138. Application.Top.Dispose ();
  139. Application.ResetState (ignoreDisposed: true);
  140. }
  141. // Test that FindDeepestView returns the correct view if the start view has subviews
  142. [Theory]
  143. [InlineData (0, 0, false)]
  144. [InlineData (1, 1, false)]
  145. [InlineData (9, 9, false)]
  146. [InlineData (10, 10, false)]
  147. [InlineData (6, 7, false)]
  148. [InlineData (1, 2, true)]
  149. [InlineData (5, 6, true)]
  150. public void Returns_Correct_If_SubViews (int testX, int testY, bool expectedSubViewFound)
  151. {
  152. Application.Top = new ()
  153. {
  154. Width = 10, Height = 10,
  155. };
  156. var subview = new View ()
  157. {
  158. X = 1, Y = 2,
  159. Width = 5, Height = 5,
  160. };
  161. Application.Top.Add (subview);
  162. var found = View.FindDeepestView (new (testX, testY));
  163. Assert.Equal (expectedSubViewFound, found == subview);
  164. Application.Top.Dispose ();
  165. Application.ResetState (ignoreDisposed: true);
  166. }
  167. [Theory]
  168. [InlineData (0, 0, false)]
  169. [InlineData (1, 1, false)]
  170. [InlineData (9, 9, false)]
  171. [InlineData (10, 10, false)]
  172. [InlineData (6, 7, false)]
  173. [InlineData (1, 2, false)]
  174. [InlineData (5, 6, false)]
  175. public void Returns_Null_If_SubView_NotVisible (int testX, int testY, bool expectedSubViewFound)
  176. {
  177. Application.Top = new ()
  178. {
  179. Width = 10, Height = 10,
  180. };
  181. var subview = new View ()
  182. {
  183. X = 1, Y = 2,
  184. Width = 5, Height = 5,
  185. Visible = false
  186. };
  187. Application.Top.Add (subview);
  188. var found = View.FindDeepestView (new (testX, testY));
  189. Assert.Equal (expectedSubViewFound, found == subview);
  190. Application.Top.Dispose ();
  191. Application.ResetState (ignoreDisposed: true);
  192. }
  193. [Theory]
  194. [InlineData (0, 0, false)]
  195. [InlineData (1, 1, false)]
  196. [InlineData (9, 9, false)]
  197. [InlineData (10, 10, false)]
  198. [InlineData (6, 7, false)]
  199. [InlineData (1, 2, false)]
  200. [InlineData (5, 6, false)]
  201. public void Returns_Null_If_Not_Visible_And_SubView_Visible (int testX, int testY, bool expectedSubViewFound)
  202. {
  203. Application.Top = new ()
  204. {
  205. Width = 10, Height = 10,
  206. Visible = false
  207. };
  208. var subview = new View ()
  209. {
  210. X = 1, Y = 2,
  211. Width = 5, Height = 5,
  212. };
  213. Application.Top.Add (subview);
  214. subview.Visible = true;
  215. Assert.True (subview.Visible);
  216. Assert.False (Application.Top.Visible);
  217. var found = View.FindDeepestView (new (testX, testY));
  218. Assert.Equal (expectedSubViewFound, found == subview);
  219. Application.Top.Dispose ();
  220. Application.ResetState (ignoreDisposed: true);
  221. }
  222. // Test that FindDeepestView works if the start view has positive Adornments
  223. [Theory]
  224. [InlineData (0, 0, false)]
  225. [InlineData (1, 1, false)]
  226. [InlineData (9, 9, false)]
  227. [InlineData (10, 10, false)]
  228. [InlineData (7, 8, false)]
  229. [InlineData (1, 2, false)]
  230. [InlineData (2, 3, true)]
  231. [InlineData (5, 6, true)]
  232. [InlineData (6, 7, true)]
  233. public void Returns_Correct_If_Start_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
  234. {
  235. Application.Top = new ()
  236. {
  237. Width = 10, Height = 10,
  238. };
  239. Application.Top.Margin.Thickness = new Thickness (1);
  240. var subview = new View ()
  241. {
  242. X = 1, Y = 2,
  243. Width = 5, Height = 5,
  244. };
  245. Application.Top.Add (subview);
  246. var found = View.FindDeepestView (new (testX, testY));
  247. Assert.Equal (expectedSubViewFound, found == subview);
  248. Application.Top.Dispose ();
  249. Application.ResetState (ignoreDisposed: true);
  250. }
  251. // Test that FindDeepestView works if the start view has offset Viewport location
  252. [Theory]
  253. [InlineData (1, 0, 0, true)]
  254. [InlineData (1, 1, 1, true)]
  255. [InlineData (1, 2, 2, false)]
  256. [InlineData (-1, 3, 3, true)]
  257. [InlineData (-1, 2, 2, true)]
  258. [InlineData (-1, 1, 1, false)]
  259. [InlineData (-1, 0, 0, false)]
  260. public void Returns_Correct_If_Start_Has_Offset_Viewport (int offset, int testX, int testY, bool expectedSubViewFound)
  261. {
  262. Application.Top = new ()
  263. {
  264. Width = 10, Height = 10,
  265. ViewportSettings = ViewportSettings.AllowNegativeLocation
  266. };
  267. Application.Top.Viewport = new (offset, offset, 10, 10);
  268. var subview = new View ()
  269. {
  270. X = 1, Y = 1,
  271. Width = 2, Height = 2,
  272. };
  273. Application.Top.Add (subview);
  274. var found = View.FindDeepestView (new (testX, testY));
  275. Assert.Equal (expectedSubViewFound, found == subview);
  276. Application.Top.Dispose ();
  277. Application.ResetState (ignoreDisposed: true);
  278. }
  279. [Theory]
  280. [InlineData (9, 9, true)]
  281. [InlineData (0, 0, false)]
  282. [InlineData (1, 1, false)]
  283. [InlineData (10, 10, false)]
  284. [InlineData (7, 8, false)]
  285. [InlineData (1, 2, false)]
  286. [InlineData (2, 3, false)]
  287. [InlineData (5, 6, false)]
  288. [InlineData (6, 7, false)]
  289. public void Returns_Correct_If_Start_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
  290. {
  291. Application.Top = new ()
  292. {
  293. Width = 10, Height = 10,
  294. };
  295. Application.Top.Padding.Thickness = new Thickness (1);
  296. var subview = new View ()
  297. {
  298. X = Pos.AnchorEnd(1), Y = Pos.AnchorEnd(1),
  299. Width = 1, Height = 1,
  300. };
  301. Application.Top.Padding.Add (subview);
  302. Application.Top.BeginInit();
  303. Application.Top.EndInit();
  304. var found = View.FindDeepestView (new (testX, testY));
  305. Assert.Equal (expectedSubViewFound, found == subview);
  306. Application.Top.Dispose ();
  307. Application.ResetState (ignoreDisposed: true);
  308. }
  309. [Theory]
  310. [InlineData (0, 0, typeof (Margin))]
  311. [InlineData (9, 9, typeof (Margin))]
  312. [InlineData (1, 1, typeof (Border))]
  313. [InlineData (8, 8, typeof (Border))]
  314. [InlineData (2, 2, typeof (Padding))]
  315. [InlineData (7, 7, typeof (Padding))]
  316. [InlineData (5, 5, typeof (Toplevel))]
  317. public void Returns_Adornment_If_Start_Has_Adornments (int testX, int testY, Type expectedAdornmentType)
  318. {
  319. Application.Top = new ()
  320. {
  321. Width = 10, Height = 10,
  322. };
  323. Application.Top.Margin.Thickness = new Thickness (1);
  324. Application.Top.Border.Thickness = new Thickness (1);
  325. Application.Top.Padding.Thickness = new Thickness (1);
  326. var subview = new View ()
  327. {
  328. X = 1, Y = 1,
  329. Width = 1, Height = 1,
  330. };
  331. Application.Top.Add (subview);
  332. var found = View.FindDeepestView (new (testX, testY));
  333. Assert.Equal (expectedAdornmentType, found!.GetType ());
  334. Application.Top.Dispose ();
  335. Application.ResetState (ignoreDisposed: true);
  336. }
  337. // Test that FindDeepestView works if the subview has positive Adornments
  338. [Theory]
  339. [InlineData (0, 0, false)]
  340. [InlineData (1, 1, false)]
  341. [InlineData (9, 9, false)]
  342. [InlineData (10, 10, false)]
  343. [InlineData (7, 8, false)]
  344. [InlineData (6, 7, false)]
  345. [InlineData (1, 2, false)]
  346. [InlineData (5, 6, false)]
  347. [InlineData (2, 3, true)]
  348. public void Returns_Correct_If_SubView_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
  349. {
  350. Application.Top = new ()
  351. {
  352. Width = 10, Height = 10,
  353. };
  354. var subview = new View ()
  355. {
  356. X = 1, Y = 2,
  357. Width = 5, Height = 5,
  358. };
  359. subview.Margin.Thickness = new Thickness (1);
  360. Application.Top.Add (subview);
  361. var found = View.FindDeepestView (new (testX, testY));
  362. Assert.Equal (expectedSubViewFound, found == subview);
  363. Application.Top.Dispose ();
  364. Application.ResetState (ignoreDisposed: true);
  365. }
  366. [Theory]
  367. [InlineData (0, 0, false)]
  368. [InlineData (1, 1, false)]
  369. [InlineData (9, 9, false)]
  370. [InlineData (10, 10, false)]
  371. [InlineData (7, 8, false)]
  372. [InlineData (6, 7, false)]
  373. [InlineData (1, 2, false)]
  374. [InlineData (5, 6, false)]
  375. [InlineData (6, 5, false)]
  376. [InlineData (5, 5, true)]
  377. public void Returns_Correct_If_SubView_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
  378. {
  379. Application.Top = new ()
  380. {
  381. Width = 10, Height = 10,
  382. };
  383. // A subview with + Padding
  384. var subview = new View ()
  385. {
  386. X = 1, Y = 1,
  387. Width = 5, Height = 5,
  388. };
  389. subview.Padding.Thickness = new (1);
  390. // This subview will be at the bottom-right-corner of subview
  391. // So screen-relative location will be X + Width - 1 = 5
  392. var paddingSubview = new View ()
  393. {
  394. X = Pos.AnchorEnd (1),
  395. Y = Pos.AnchorEnd (1),
  396. Width = 1,
  397. Height = 1,
  398. };
  399. subview.Padding.Add (paddingSubview);
  400. Application.Top.Add (subview);
  401. Application.Top.BeginInit();
  402. Application.Top.EndInit();
  403. var found = View.FindDeepestView (new (testX, testY));
  404. Assert.Equal (expectedSubViewFound, found == paddingSubview);
  405. Application.Top.Dispose ();
  406. Application.ResetState (ignoreDisposed: true);
  407. }
  408. [Theory]
  409. [InlineData (0, 0, false)]
  410. [InlineData (1, 1, false)]
  411. [InlineData (9, 9, false)]
  412. [InlineData (10, 10, false)]
  413. [InlineData (7, 8, false)]
  414. [InlineData (6, 7, false)]
  415. [InlineData (1, 2, false)]
  416. [InlineData (5, 6, false)]
  417. [InlineData (6, 5, false)]
  418. [InlineData (5, 5, true)]
  419. public void Returns_Correct_If_SubView_Is_Scrolled_And_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
  420. {
  421. Application.Top = new ()
  422. {
  423. Width = 10, Height = 10,
  424. };
  425. // A subview with + Padding
  426. var subview = new View ()
  427. {
  428. X = 1, Y = 1,
  429. Width = 5, Height = 5,
  430. };
  431. subview.Padding.Thickness = new (1);
  432. // Scroll the subview
  433. subview.SetContentSize (new (10, 10));
  434. subview.Viewport = subview.Viewport with { Location = new (1, 1) };
  435. // This subview will be at the bottom-right-corner of subview
  436. // So screen-relative location will be X + Width - 1 = 5
  437. var paddingSubview = new View ()
  438. {
  439. X = Pos.AnchorEnd (1),
  440. Y = Pos.AnchorEnd (1),
  441. Width = 1,
  442. Height = 1,
  443. };
  444. subview.Padding.Add (paddingSubview);
  445. Application.Top.Add (subview);
  446. Application.Top.BeginInit ();
  447. Application.Top.EndInit ();
  448. var found = View.FindDeepestView (new (testX, testY));
  449. Assert.Equal (expectedSubViewFound, found == paddingSubview);
  450. Application.Top.Dispose ();
  451. Application.ResetState (ignoreDisposed: true);
  452. }
  453. // Test that FindDeepestView works with nested subviews
  454. [Theory]
  455. [InlineData (0, 0, -1)]
  456. [InlineData (9, 9, -1)]
  457. [InlineData (10, 10, -1)]
  458. [InlineData (1, 1, 0)]
  459. [InlineData (1, 2, 0)]
  460. [InlineData (2, 2, 1)]
  461. [InlineData (3, 3, 2)]
  462. [InlineData (5, 5, 2)]
  463. public void Returns_Correct_With_NestedSubViews (int testX, int testY, int expectedSubViewFound)
  464. {
  465. Application.Top = new ()
  466. {
  467. Width = 10, Height = 10
  468. };
  469. int numSubViews = 3;
  470. List<View> subviews = new List<View> ();
  471. for (int i = 0; i < numSubViews; i++)
  472. {
  473. var subview = new View ()
  474. {
  475. X = 1, Y = 1,
  476. Width = 5, Height = 5,
  477. };
  478. subviews.Add (subview);
  479. if (i > 0)
  480. {
  481. subviews [i - 1].Add (subview);
  482. }
  483. }
  484. Application.Top.Add (subviews [0]);
  485. var found = View.FindDeepestView (new (testX, testY));
  486. Assert.Equal (expectedSubViewFound, subviews.IndexOf (found!));
  487. Application.Top.Dispose ();
  488. Application.ResetState (ignoreDisposed: true);
  489. }
  490. }