Dim.Tests.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System.Globalization;
  2. using System.Text;
  3. using UnitTests;
  4. using Xunit.Abstractions;
  5. using static Terminal.Gui.Dim;
  6. namespace Terminal.Gui.LayoutTests;
  7. public class DimTests
  8. {
  9. private readonly ITestOutputHelper _output;
  10. public DimTests (ITestOutputHelper output)
  11. {
  12. _output = output;
  13. Console.OutputEncoding = Encoding.Default;
  14. // Change current culture
  15. var culture = CultureInfo.CreateSpecificCulture ("en-US");
  16. Thread.CurrentThread.CurrentCulture = culture;
  17. Thread.CurrentThread.CurrentUICulture = culture;
  18. }
  19. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  20. // TODO: A new test that calls SetRelativeLayout directly is needed.
  21. [Fact]
  22. [AutoInitShutdown]
  23. public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
  24. {
  25. // Override CM
  26. Button.DefaultShadow = ShadowStyle.None;
  27. // Testing with the Button because it properly handles the Dim class.
  28. Toplevel t = new ();
  29. var w = new Window { Width = 100, Height = 100 };
  30. var f1 = new FrameView
  31. {
  32. X = 0,
  33. Y = 0,
  34. Width = Percent (50),
  35. Height = 5,
  36. Title = "f1"
  37. };
  38. var f2 = new FrameView
  39. {
  40. X = Pos.Right (f1),
  41. Y = 0,
  42. Width = Fill (),
  43. Height = 5,
  44. Title = "f2"
  45. };
  46. var v1 = new Button
  47. {
  48. X = Pos.X (f1) + 2,
  49. Y = Pos.Bottom (f1) + 2,
  50. Width = Width (f1) - 2,
  51. Height = Fill () - 2,
  52. ValidatePosDim = true,
  53. Text = "v1"
  54. };
  55. var v2 = new Button
  56. {
  57. X = Pos.X (f2) + 2,
  58. Y = Pos.Bottom (f2) + 2,
  59. Width = Width (f2) - 2,
  60. Height = Fill () - 2,
  61. ValidatePosDim = true,
  62. Text = "v2"
  63. };
  64. var v3 = new Button
  65. {
  66. Width = Percent (10),
  67. Height = Percent (10),
  68. ValidatePosDim = true,
  69. Text = "v3"
  70. };
  71. var v4 = new Button
  72. {
  73. Width = Absolute (50),
  74. Height = Absolute (50),
  75. ValidatePosDim = true,
  76. Text = "v4"
  77. };
  78. var v5 = new Button
  79. {
  80. Width = Width (v1) - Width (v3),
  81. Height = Height (v1) - Height (v3),
  82. ValidatePosDim = true,
  83. Text = "v5"
  84. };
  85. var v6 = new Button
  86. {
  87. X = Pos.X (f2),
  88. Y = Pos.Bottom (f2) + 2,
  89. Width = Percent (20, DimPercentMode.Position),
  90. Height = Percent (20, DimPercentMode.Position),
  91. ValidatePosDim = true,
  92. Text = "v6"
  93. };
  94. w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
  95. t.Add (w);
  96. t.Ready += (s, e) =>
  97. {
  98. Assert.Equal ("Absolute(100)", w.Width.ToString ());
  99. Assert.Equal ("Absolute(100)", w.Height.ToString ());
  100. Assert.Equal (100, w.Frame.Width);
  101. Assert.Equal (100, w.Frame.Height);
  102. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  103. Assert.Equal (49, f1.Frame.Width); // 50-1=49
  104. Assert.Equal (5, f1.Frame.Height);
  105. Assert.Equal ("Fill(Absolute(0))", f2.Width.ToString ());
  106. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  107. Assert.Equal (49, f2.Frame.Width); // 50-1=49
  108. Assert.Equal (5, f2.Frame.Height);
  109. #if DEBUG
  110. Assert.Equal ($"Combine(View(Width,FrameView(f1){f1.Border.Frame})-Absolute(2))", v1.Width.ToString ());
  111. #else
  112. Assert.Equal ($"Combine(View(Width,FrameView(){f1.Border.Frame})-Absolute(2))", v1.Width.ToString ());
  113. #endif
  114. Assert.Equal ("Combine(Fill(Absolute(0))-Absolute(2))", v1.Height.ToString ());
  115. Assert.Equal (47, v1.Frame.Width); // 49-2=47
  116. Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
  117. #if DEBUG
  118. Assert.Equal (
  119. $"Combine(View(Width,FrameView(f2){f2.Frame})-Absolute(2))",
  120. v2.Width.ToString ()
  121. #else
  122. Assert.Equal (
  123. $"Combine(View(Width,FrameView(){f2.Frame})-Absolute(2))",
  124. v2.Width.ToString ()
  125. #endif
  126. );
  127. #if DEBUG
  128. Assert.Equal ("Combine(Fill(Absolute(0))-Absolute(2))", v2.Height.ToString ());
  129. #else
  130. Assert.Equal ("Combine(Fill(Absolute(0))-Absolute(2))", v2.Height.ToString ());
  131. #endif
  132. Assert.Equal (47, v2.Frame.Width); // 49-2=47
  133. Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
  134. Assert.Equal (9, v3.Frame.Width); // 98*10%=9
  135. Assert.Equal (9, v3.Frame.Height); // 98*10%=9
  136. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  137. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  138. Assert.Equal (50, v4.Frame.Width);
  139. Assert.Equal (50, v4.Frame.Height);
  140. #if DEBUG
  141. Assert.Equal ($"Combine(View(Width,Button(v1){v1.Frame})-View(Width,Button(v3){v3.Viewport}))", v5.Width.ToString ());
  142. #else
  143. Assert.Equal ($"Combine(View(Height,Button(){v1.Frame})-View(Height,Button(){v3.Viewport}))", v5.Height.ToString ( ));
  144. #endif
  145. Assert.Equal (38, v5.Frame.Width); // 47-9=38
  146. Assert.Equal (80, v5.Frame.Height); // 89-9=80
  147. Assert.Equal (9, v6.Frame.Width); // 47*20%=9
  148. Assert.Equal (18, v6.Frame.Height); // 89*20%=18
  149. w.Width = 200;
  150. Assert.True (t.NeedsLayout);
  151. w.Height = 200;
  152. t.LayoutSubViews ();
  153. Assert.Equal ("Absolute(200)", w.Width.ToString ());
  154. Assert.Equal ("Absolute(200)", w.Height.ToString ());
  155. Assert.Equal (200, w.Frame.Width);
  156. Assert.Equal (200, w.Frame.Height);
  157. f1.Text = "Frame1";
  158. Assert.Equal (99, f1.Frame.Width); // 100-1=99
  159. Assert.Equal (5, f1.Frame.Height);
  160. f2.Text = "Frame2";
  161. Assert.Equal ("Fill(Absolute(0))", f2.Width.ToString ());
  162. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  163. Assert.Equal (99, f2.Frame.Width); // 100-1=99
  164. Assert.Equal (5, f2.Frame.Height);
  165. v1.Text = "Button1";
  166. #if DEBUG
  167. Assert.Equal ($"Combine(View(Width,FrameView(f1){f1.Frame})-Absolute(2))", v1.Width.ToString ());
  168. #else
  169. Assert.Equal ($"Combine(View(Width,FrameView(){f1.Frame})-Absolute(2))", v1.Width.ToString ());
  170. #endif
  171. Assert.Equal ("Combine(Fill(Absolute(0))-Absolute(2))", v1.Height.ToString ());
  172. Assert.Equal (97, v1.Frame.Width); // 99-2=97
  173. Assert.Equal (189, v1.Frame.Height); // 198-2-7=189
  174. v2.Text = "Button2";
  175. #if DEBUG
  176. Assert.Equal ($"Combine(View(Width,FrameView(f2){f2.Frame})-Absolute(2))", v2.Width.ToString ());
  177. #else
  178. Assert.Equal ($"Combine(View(Width,FrameView(){f2.Frame})-Absolute(2))", v2.Width.ToString ());
  179. #endif
  180. Assert.Equal ("Combine(Fill(Absolute(0))-Absolute(2))", v2.Height.ToString ());
  181. Assert.Equal (97, v2.Frame.Width); // 99-2=97
  182. Assert.Equal (189, v2.Frame.Height); // 198-2-7=189
  183. v3.Text = "Button3";
  184. // 198*10%=19 * Percent is related to the super-view if it isn't null otherwise the view width
  185. Assert.Equal (19, v3.Frame.Width);
  186. // 199*10%=19
  187. Assert.Equal (19, v3.Frame.Height);
  188. v4.Text = "Button4";
  189. v4.Width = Auto (DimAutoStyle.Text);
  190. v4.Height = Auto (DimAutoStyle.Text);
  191. v4.Layout ();
  192. Assert.Equal (Auto (DimAutoStyle.Text), v4.Width);
  193. Assert.Equal (Auto (DimAutoStyle.Text), v4.Height);
  194. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is DimAbsolute
  195. Assert.Equal (1, v4.Frame.Height); // 1 because is DimAbsolute
  196. v5.Text = "Button5";
  197. #if DEBUG
  198. Assert.Equal ($"Combine(View(Width,Button(v1){v1.Frame})-View(Width,Button(v3){v3.Frame}))", v5.Width.ToString ());
  199. Assert.Equal ($"Combine(View(Height,Button(v1){v1.Frame})-View(Height,Button(v3){v3.Frame}))", v5.Height.ToString ());
  200. #else
  201. Assert.Equal ($"Combine(View(Width,Button(){v1.Frame})-View(Width,Button(){v3.Frame}))", v5.Width.ToString ());
  202. Assert.Equal ($"Combine(View(Height,Button(){v1.Frame})-View(Height,Button(){v3.Frame}))", v5.Height.ToString ());
  203. #endif
  204. Assert.Equal (78, v5.Frame.Width); // 97-9=78
  205. Assert.Equal (170, v5.Frame.Height); // 189-19=170
  206. v6.Text = "Button6";
  207. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  208. Assert.Equal (38, v6.Frame.Height); // 198-7*20=18
  209. };
  210. Application.Iteration += (s, a) => Application.RequestStop ();
  211. Application.Run (t);
  212. t.Dispose ();
  213. }
  214. }