FindDeepestViewTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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, typeof (Margin))]
  239. [InlineData (9, 9, typeof (Margin))]
  240. [InlineData (1, 1, typeof (Border))]
  241. [InlineData (8, 8, typeof (Border))]
  242. [InlineData (2, 2, typeof (Padding))]
  243. [InlineData (7, 7, typeof (Padding))]
  244. [InlineData (5, 5, typeof (View))]
  245. public void Returns_Adornment_If_Start_Has_Adornments (int testX, int testY, Type expectedAdornmentType)
  246. {
  247. var start = new View ()
  248. {
  249. Width = 10, Height = 10,
  250. };
  251. start.Margin.Thickness = new Thickness (1);
  252. start.Border.Thickness = new Thickness (1);
  253. start.Padding.Thickness = new Thickness (1);
  254. var subview = new View ()
  255. {
  256. X = 1, Y = 1,
  257. Width = 1, Height = 1,
  258. };
  259. start.Add (subview);
  260. var found = View.FindDeepestView (start, testX, testY);
  261. Assert.Equal (expectedAdornmentType, found.GetType ());
  262. }
  263. // Test that FindDeepestView works if the subview has positive Adornments
  264. [Theory]
  265. [InlineData (0, 0, false)]
  266. [InlineData (1, 1, false)]
  267. [InlineData (9, 9, false)]
  268. [InlineData (10, 10, false)]
  269. [InlineData (7, 8, false)]
  270. [InlineData (6, 7, false)]
  271. [InlineData (1, 2, false)]
  272. [InlineData (5, 6, false)]
  273. [InlineData (2, 3, true)]
  274. [InlineData (2, 3, true)]
  275. public void Returns_Correct_If_SubView_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
  276. {
  277. var start = new View ()
  278. {
  279. Width = 10, Height = 10,
  280. };
  281. var subview = new View ()
  282. {
  283. X = 1, Y = 2,
  284. Width = 5, Height = 5,
  285. };
  286. subview.Margin.Thickness = new Thickness (1);
  287. start.Add (subview);
  288. var found = View.FindDeepestView (start, testX, testY);
  289. Assert.Equal (expectedSubViewFound, found == subview);
  290. }
  291. // Test that FindDeepestView works with nested subviews
  292. [Theory]
  293. [InlineData (0, 0, -1)]
  294. [InlineData (9, 9, -1)]
  295. [InlineData (10, 10, -1)]
  296. [InlineData (1, 1, 0)]
  297. [InlineData (1, 2, 0)]
  298. [InlineData (2, 2, 1)]
  299. [InlineData (3, 3, 2)]
  300. [InlineData (5, 5, 2)]
  301. public void Returns_Correct_With_NestedSubViews (int testX, int testY, int expectedSubViewFound)
  302. {
  303. var start = new View ()
  304. {
  305. Width = 10, Height = 10
  306. };
  307. int numSubViews = 3;
  308. List<View> subviews = new List<View> ();
  309. for (int i = 0; i < numSubViews; i++)
  310. {
  311. var subview = new View ()
  312. {
  313. X = 1, Y = 1,
  314. Width = 5, Height = 5,
  315. };
  316. subviews.Add (subview);
  317. if (i > 0)
  318. {
  319. subviews [i - 1].Add (subview);
  320. }
  321. }
  322. start.Add (subviews [0]);
  323. var found = View.FindDeepestView (start, testX, testY);
  324. Assert.Equal (expectedSubViewFound, subviews.IndexOf (found));
  325. }
  326. }