FindDeepestViewTests.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using UICatalog.Scenarios;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewTests;
  4. /// <summary>
  5. /// Tests View.FindDeepestView
  6. /// </summary>
  7. /// <param name="output"></param>
  8. public class FindDeepestViewTests (ITestOutputHelper output)
  9. {
  10. // Test that FindDeepestView returns the correct view if the start view has no subviews
  11. [Theory]
  12. [InlineData (0, 0)]
  13. [InlineData (1, 1)]
  14. [InlineData (2, 2)]
  15. public void Returns_Start_If_No_SubViews (int testX, int testY)
  16. {
  17. var start = new View ()
  18. {
  19. Width = 10, Height = 10,
  20. };
  21. Assert.Same (start, View.FindDeepestView (start, testX, testY));
  22. }
  23. // Test that FindDeepestView returns null if the start view has no subviews and coords are outside the view
  24. [Theory]
  25. [InlineData (0, 0)]
  26. [InlineData (2, 1)]
  27. [InlineData (20, 20)]
  28. public void Returns_Null_If_No_SubViews_Coords_Outside (int testX, int testY)
  29. {
  30. var start = new View ()
  31. {
  32. X = 1, Y = 2,
  33. Width = 10, Height = 10,
  34. };
  35. Assert.Null(View.FindDeepestView (start, testX, testY));
  36. }
  37. [Theory]
  38. [InlineData (0, 0)]
  39. [InlineData (2, 1)]
  40. [InlineData (20, 20)]
  41. public void Returns_Null_If_Start_Not_Visible (int testX, int testY)
  42. {
  43. var start = new View ()
  44. {
  45. X = 1, Y = 2,
  46. Width = 10, Height = 10,
  47. Visible = false,
  48. };
  49. Assert.Null (View.FindDeepestView (start, testX, testY));
  50. }
  51. // Test that FindDeepestView returns the correct view if the start view has subviews
  52. [Theory]
  53. [InlineData (0, 0, false)]
  54. [InlineData (1, 1, false)]
  55. [InlineData (9, 9, false)]
  56. [InlineData (10, 10, false)]
  57. [InlineData (6, 7, false)]
  58. [InlineData (1, 2, true)]
  59. [InlineData (5, 6, true)]
  60. public void Returns_Correct_If_SubViews (int testX, int testY, bool expectedSubViewFound)
  61. {
  62. var start = new View ()
  63. {
  64. Width = 10, Height = 10,
  65. };
  66. var subview = new View ()
  67. {
  68. X = 1, Y = 2,
  69. Width = 5, Height = 5,
  70. };
  71. start.Add (subview);
  72. var found = View.FindDeepestView (start, testX, testY);
  73. Assert.Equal (expectedSubViewFound, found == subview);
  74. }
  75. [Theory]
  76. [InlineData (0, 0, false)]
  77. [InlineData (1, 1, false)]
  78. [InlineData (9, 9, false)]
  79. [InlineData (10, 10, false)]
  80. [InlineData (6, 7, false)]
  81. [InlineData (1, 2, false)]
  82. [InlineData (5, 6, false)]
  83. public void Returns_Null_If_SubView_NotVisible (int testX, int testY, bool expectedSubViewFound)
  84. {
  85. var start = new View ()
  86. {
  87. Width = 10, Height = 10,
  88. };
  89. var subview = new View ()
  90. {
  91. X = 1, Y = 2,
  92. Width = 5, Height = 5,
  93. Visible = false
  94. };
  95. start.Add (subview);
  96. var found = View.FindDeepestView (start, testX, testY);
  97. Assert.Equal (expectedSubViewFound, found == subview);
  98. }
  99. [Theory]
  100. [InlineData (0, 0, false)]
  101. [InlineData (1, 1, false)]
  102. [InlineData (9, 9, false)]
  103. [InlineData (10, 10, false)]
  104. [InlineData (6, 7, false)]
  105. [InlineData (1, 2, false)]
  106. [InlineData (5, 6, false)]
  107. public void Returns_Null_If_Not_Visible_And_SubView_Visible (int testX, int testY, bool expectedSubViewFound)
  108. {
  109. var start = new View ()
  110. {
  111. Width = 10, Height = 10,
  112. Visible = false
  113. };
  114. var subview = new View ()
  115. {
  116. X = 1, Y = 2,
  117. Width = 5, Height = 5,
  118. };
  119. start.Add (subview);
  120. subview.Visible = true;
  121. Assert.True (subview.Visible);
  122. Assert.False (start.Visible);
  123. var found = View.FindDeepestView (start, testX, testY);
  124. Assert.Equal (expectedSubViewFound, found == subview);
  125. }
  126. // Test that FindDeepestView works if the start view has positive Adornments
  127. [Theory]
  128. [InlineData (0, 0, false)]
  129. [InlineData (1, 1, false)]
  130. [InlineData (9, 9, false)]
  131. [InlineData (10, 10, false)]
  132. [InlineData (7, 8, false)]
  133. [InlineData (1, 2, false)]
  134. [InlineData (2, 3, true)]
  135. [InlineData (5, 6, true)]
  136. [InlineData (2, 3, true)]
  137. [InlineData (6, 7, true)]
  138. public void Returns_Correct_If_Start_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
  139. {
  140. var start = new View ()
  141. {
  142. Width = 10, Height = 10,
  143. };
  144. start.Margin.Thickness = new Thickness (1);
  145. var subview = new View ()
  146. {
  147. X = 1, Y = 2,
  148. Width = 5, Height = 5,
  149. };
  150. start.Add (subview);
  151. var found = View.FindDeepestView (start, testX, testY, true);
  152. Assert.Equal (expectedSubViewFound, found == subview);
  153. }
  154. [Theory]
  155. [InlineData (0, 0, typeof(Margin))]
  156. [InlineData (9, 9, typeof (Margin))]
  157. [InlineData (1, 1, typeof (Border))]
  158. [InlineData (8, 8, typeof (Border))]
  159. [InlineData (2, 2, typeof (Padding))]
  160. [InlineData (7, 7, typeof (Padding))]
  161. [InlineData (5, 5, typeof (View))]
  162. public void Returns_Adornment_If_Start_Has_Adornments (int testX, int testY, Type expectedAdornmentType)
  163. {
  164. var start = new View ()
  165. {
  166. Width = 10, Height = 10,
  167. };
  168. start.Margin.Thickness = new Thickness (1);
  169. start.Border.Thickness = new Thickness (1);
  170. start.Padding.Thickness = new Thickness (1);
  171. var subview = new View ()
  172. {
  173. X = 1, Y = 1,
  174. Width = 1, Height = 1,
  175. };
  176. start.Add (subview);
  177. var found = View.FindDeepestView (start, testX, testY, true);
  178. Assert.Equal(expectedAdornmentType, found.GetType());
  179. }
  180. // Test that FindDeepestView works if the subview has positive Adornments
  181. [Theory]
  182. [InlineData (0, 0, false)]
  183. [InlineData (1, 1, false)]
  184. [InlineData (9, 9, false)]
  185. [InlineData (10, 10, false)]
  186. [InlineData (7, 8, false)]
  187. [InlineData (6, 7, false)]
  188. [InlineData (1, 2, true)]
  189. [InlineData (2, 3, true)]
  190. [InlineData (5, 6, true)]
  191. [InlineData (2, 3, true)]
  192. public void Returns_Correct_If_SubView_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
  193. {
  194. var start = new View ()
  195. {
  196. Width = 10, Height = 10,
  197. };
  198. var subview = new View ()
  199. {
  200. X = 1, Y = 2,
  201. Width = 5, Height = 5,
  202. };
  203. subview.Margin.Thickness = new Thickness (1);
  204. start.Add (subview);
  205. var found = View.FindDeepestView (start, testX, testY);
  206. Assert.Equal (expectedSubViewFound, found == subview);
  207. }
  208. // Test that FindDeepestView works with nested subviews
  209. [Theory]
  210. [InlineData (0, 0, -1)]
  211. [InlineData (9, 9, -1)]
  212. [InlineData (10, 10, -1)]
  213. [InlineData (1, 1, 0)]
  214. [InlineData (1, 2, 0)]
  215. [InlineData (2, 2, 1)]
  216. [InlineData (3, 3, 2)]
  217. [InlineData (5, 5, 2)]
  218. public void Returns_Correct_With_NestedSubViews (int testX, int testY, int expectedSubViewFound)
  219. {
  220. var start = new View ()
  221. {
  222. Width = 10, Height = 10
  223. };
  224. int numSubViews = 3;
  225. List<View> subviews = new List<View> ();
  226. for (int i = 0; i < numSubViews; i++)
  227. {
  228. var subview = new View ()
  229. {
  230. X = 1, Y = 1,
  231. Width = 5, Height = 5,
  232. };
  233. subviews.Add (subview);
  234. if (i > 0)
  235. {
  236. subviews [i - 1].Add (subview);
  237. }
  238. }
  239. start.Add (subviews [0]);
  240. var found = View.FindDeepestView (start, testX, testY);
  241. Assert.Equal (expectedSubViewFound, subviews.IndexOf(found));
  242. }
  243. }