DimAutoTests.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. using System;
  2. using System.Globalization;
  3. using System.Threading;
  4. using Xunit;
  5. using Xunit.Abstractions;
  6. // Alias Console to MockConsole so we don't accidentally use Console
  7. using Console = Terminal.Gui.FakeConsole;
  8. namespace Terminal.Gui.ViewTests;
  9. public class DimAutoTests {
  10. readonly ITestOutputHelper _output;
  11. public DimAutoTests (ITestOutputHelper output)
  12. {
  13. _output = output;
  14. Console.OutputEncoding = System.Text.Encoding.Default;
  15. // Change current culture
  16. var culture = CultureInfo.CreateSpecificCulture ("en-US");
  17. Thread.CurrentThread.CurrentCulture = culture;
  18. Thread.CurrentThread.CurrentUICulture = culture;
  19. }
  20. [Fact]
  21. public void NoSubViews_Does_Nothing ()
  22. {
  23. var superView = new View () {
  24. X = 0,
  25. Y = 0,
  26. Width = Dim.Auto (),
  27. Height = Dim.Auto (),
  28. ValidatePosDim = true,
  29. };
  30. superView.BeginInit ();
  31. superView.EndInit ();
  32. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  33. Assert.Equal (new Rect (0, 0, 0, 0), superView.Frame);
  34. superView.SetRelativeLayout (new Rect (0, 0, 10, 10));
  35. Assert.Equal (new Rect (0, 0, 0, 0), superView.Frame);
  36. superView.SetRelativeLayout (new Rect (10, 10, 10, 10));
  37. Assert.Equal (new Rect (0, 0, 0, 0), superView.Frame);
  38. }
  39. [Theory]
  40. [InlineData (0, 0, 0, 0, 0, 0)]
  41. [InlineData (0, 0, 5, 0, 5, 0)]
  42. [InlineData (0, 0, 0, 5, 0, 5)]
  43. [InlineData (0, 0, 5, 5, 5, 5)]
  44. [InlineData (1, 0, 5, 0, 6, 0)]
  45. [InlineData (1, 0, 0, 5, 1, 5)]
  46. [InlineData (1, 0, 5, 5, 6, 5)]
  47. [InlineData (1, 1, 5, 5, 6, 6)]
  48. [InlineData (-1, 0, 5, 0, 4, 0)]
  49. [InlineData (-1, 0, 0, 5, 0, 5)]
  50. [InlineData (-1, 0, 5, 5, 4, 5)]
  51. [InlineData (-1, -1, 5, 5, 4, 4)]
  52. public void SubView_ChangesSuperViewSize (int subX, int subY, int subWidth, int subHeight, int expectedWidth, int expectedHeight)
  53. {
  54. var superView = new View () {
  55. X = 0,
  56. Y = 0,
  57. Width = Dim.Auto (),
  58. Height = Dim.Auto (),
  59. ValidatePosDim = true,
  60. };
  61. var subView = new View () {
  62. X = subX,
  63. Y = subY,
  64. Width = subWidth,
  65. Height = subHeight,
  66. ValidatePosDim = true,
  67. };
  68. superView.Add (subView);
  69. superView.BeginInit ();
  70. superView.EndInit ();
  71. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  72. Assert.Equal (new Rect (0, 0, expectedWidth, expectedHeight), superView.Frame);
  73. }
  74. [Theory]
  75. [InlineData (0, 0, 0, 0, 0)]
  76. [InlineData (0, 0, 5, 0, 5)]
  77. [InlineData (0, 0, 0, 5, 0)]
  78. [InlineData (0, 0, 5, 5, 5)]
  79. [InlineData (1, 0, 5, 0, 6)]
  80. [InlineData (1, 0, 0, 5, 1)]
  81. [InlineData (1, 0, 5, 5, 6)]
  82. [InlineData (1, 1, 5, 5, 6)]
  83. [InlineData (-1, 0, 5, 0, 4)]
  84. [InlineData (-1, 0, 0, 5, 0)]
  85. [InlineData (-1, 0, 5, 5, 4)]
  86. [InlineData (-1, -1, 5, 5, 4)]
  87. public void Width_Auto_Height_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedWidth)
  88. {
  89. var superView = new View () {
  90. X = 0,
  91. Y = 0,
  92. Width = Dim.Auto (),
  93. Height = 10,
  94. ValidatePosDim = true,
  95. };
  96. var subView = new View () {
  97. X = subX,
  98. Y = subY,
  99. Width = subWidth,
  100. Height = subHeight,
  101. ValidatePosDim = true,
  102. };
  103. superView.Add (subView);
  104. superView.BeginInit ();
  105. superView.EndInit ();
  106. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  107. Assert.Equal (new Rect (0, 0, expectedWidth, 10), superView.Frame);
  108. }
  109. [Theory]
  110. [InlineData (0, 0, 0, 0, 0)]
  111. [InlineData (0, 0, 5, 0, 0)]
  112. [InlineData (0, 0, 0, 5, 5)]
  113. [InlineData (0, 0, 5, 5, 5)]
  114. [InlineData (1, 0, 5, 0, 0)]
  115. [InlineData (1, 0, 0, 5, 5)]
  116. [InlineData (1, 0, 5, 5, 5)]
  117. [InlineData (1, 1, 5, 5, 6)]
  118. [InlineData (-1, 0, 5, 0, 0)]
  119. [InlineData (-1, 0, 0, 5, 5)]
  120. [InlineData (-1, 0, 5, 5, 5)]
  121. [InlineData (-1, -1, 5, 5, 4)]
  122. public void Height_Auto_Width_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedHeight)
  123. {
  124. var superView = new View () {
  125. X = 0,
  126. Y = 0,
  127. Width = 10,
  128. Height = Dim.Auto (),
  129. ValidatePosDim = true,
  130. };
  131. var subView = new View () {
  132. X = subX,
  133. Y = subY,
  134. Width = subWidth,
  135. Height = subHeight,
  136. ValidatePosDim = true,
  137. };
  138. superView.Add (subView);
  139. superView.BeginInit ();
  140. superView.EndInit ();
  141. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  142. Assert.Equal (new Rect (0, 0, 10, expectedHeight), superView.Frame);
  143. }
  144. // Test validation
  145. [Fact]
  146. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims ()
  147. {
  148. var superView = new View () {
  149. X = 0,
  150. Y = 0,
  151. Width = Dim.Auto (),
  152. Height = Dim.Auto (),
  153. ValidatePosDim = true,
  154. };
  155. var subView = new View () {
  156. X = 0,
  157. Y = 0,
  158. Width = Dim.Fill (),
  159. Height = 10
  160. };
  161. superView.Add (subView);
  162. superView.BeginInit ();
  163. superView.EndInit ();
  164. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  165. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  166. subView.Width = 10;
  167. subView.Height = Dim.Fill ();
  168. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  169. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  170. subView.Width = 10;
  171. subView.Height = Dim.Percent(50);
  172. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  173. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  174. subView.Width = 10;
  175. subView.Height = 10;
  176. subView.X = Pos.Center();
  177. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  178. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  179. subView.Width = 10;
  180. subView.Height = 10;
  181. subView.X = 0;
  182. subView.Y = Pos.Center ();
  183. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  184. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  185. subView.Width = 10;
  186. subView.Height = 10;
  187. subView.X = 0;
  188. subView.Y = 0;
  189. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  190. superView.LayoutSubviews ();
  191. }
  192. // Test validation
  193. [Fact]
  194. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims_Combine ()
  195. {
  196. var superView = new View () {
  197. X = 0,
  198. Y = 0,
  199. Width = Dim.Auto (),
  200. Height = Dim.Auto (),
  201. ValidatePosDim = true,
  202. };
  203. var subView = new View () {
  204. X = 0,
  205. Y = 0,
  206. Width = 10,
  207. Height = 10
  208. };
  209. var subView2 = new View () {
  210. X = 0,
  211. Y = 0,
  212. Width = 10,
  213. Height = 10
  214. };
  215. superView.Add (subView, subView2);
  216. superView.BeginInit ();
  217. superView.EndInit ();
  218. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  219. superView.LayoutSubviews (); // no throw
  220. subView.Height = Dim.Fill () + 3;
  221. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  222. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  223. subView.Height = 3 + Dim.Fill ();
  224. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  225. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  226. subView.Height = 3 + 5 + Dim.Fill ();
  227. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  228. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  229. subView.Height = 3 + 5 + Dim.Percent (10);
  230. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  231. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  232. // Tests nested Combine
  233. subView.Height = 5 + new Dim.DimCombine (true, 3, new Dim.DimCombine (true, Dim.Percent(10), 9));
  234. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  235. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  236. }
  237. [Fact]
  238. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Pos_Combine ()
  239. {
  240. var superView = new View () {
  241. X = 0,
  242. Y = 0,
  243. Width = Dim.Auto (),
  244. Height = Dim.Auto (),
  245. ValidatePosDim = true,
  246. };
  247. var subView = new View () {
  248. X = 0,
  249. Y = 0,
  250. Width = 10,
  251. Height = 10
  252. };
  253. var subView2 = new View () {
  254. X = 0,
  255. Y = 0,
  256. Width = 10,
  257. Height = 10
  258. };
  259. superView.Add (subView, subView2);
  260. superView.BeginInit ();
  261. superView.EndInit ();
  262. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  263. superView.LayoutSubviews (); // no throw
  264. subView.X = Pos.Right(subView2);
  265. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  266. superView.LayoutSubviews (); // no throw
  267. subView.X = 3 + Pos.Right (subView2);
  268. superView.SetRelativeLayout (new Rect (0, 0, 0, 0)); // no throw
  269. superView.LayoutSubviews (); // no throw
  270. subView.X = Pos.Right (subView2) + 3;
  271. superView.SetRelativeLayout (new Rect (0, 0, 0, 0)); // no throw
  272. superView.LayoutSubviews (); // no throw
  273. subView.X = new Pos.PosCombine (true, Pos.Right (subView2), new Pos.PosCombine (true, 7, 9));
  274. superView.SetRelativeLayout (new Rect (0, 0, 0, 0)); // no throw
  275. subView.X = Pos.Center () + 3;
  276. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  277. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  278. subView.X = 3 + Pos.Center ();
  279. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  280. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  281. subView.X = 3 + 5 + Pos.Center ();
  282. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  283. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  284. subView.X = 3 + 5 + Pos.Percent (10);
  285. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  286. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  287. subView.X = Pos.Percent (10) + Pos.Center();
  288. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  289. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  290. // Tests nested Combine
  291. subView.X = 5 + new Pos.PosCombine (true, Pos.Right (subView2), new Pos.PosCombine (true, Pos.Center (), 9));
  292. superView.SetRelativeLayout (new Rect (0, 0, 0, 0));
  293. Assert.Throws<InvalidOperationException> (() => superView.LayoutSubviews ());
  294. }
  295. // Test variations of Frame
  296. // test PosCombine (can DimAuto be combined??!)
  297. }