Dim.Tests.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. Assert.Equal ($"Combine(View(Width,FrameView(){f1.Border.Frame})-Absolute(2))", v1.Width.ToString ());
  110. Assert.Equal ("Combine(Fill(Absolute(0))-Absolute(2))", v1.Height.ToString ());
  111. Assert.Equal (47, v1.Frame.Width); // 49-2=47
  112. Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
  113. Assert.Equal (
  114. $"Combine(View(Width,FrameView(){f2.Frame})-Absolute(2))",
  115. v2.Width.ToString ()
  116. );
  117. Assert.Equal ("Combine(Fill(Absolute(0))-Absolute(2))", v2.Height.ToString ());
  118. Assert.Equal (47, v2.Frame.Width); // 49-2=47
  119. Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
  120. Assert.Equal (9, v3.Frame.Width); // 98*10%=9
  121. Assert.Equal (9, v3.Frame.Height); // 98*10%=9
  122. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  123. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  124. Assert.Equal (50, v4.Frame.Width);
  125. Assert.Equal (50, v4.Frame.Height);
  126. Assert.Equal ($"Combine(View(Height,Button(){v1.Frame})-View(Height,Button(){v3.Viewport}))", v5.Height.ToString ( ));
  127. Assert.Equal (38, v5.Frame.Width); // 47-9=38
  128. Assert.Equal (80, v5.Frame.Height); // 89-9=80
  129. Assert.Equal (9, v6.Frame.Width); // 47*20%=9
  130. Assert.Equal (18, v6.Frame.Height); // 89*20%=18
  131. w.Width = 200;
  132. Assert.True (t.NeedsLayout);
  133. w.Height = 200;
  134. t.LayoutSubViews ();
  135. Assert.Equal ("Absolute(200)", w.Width.ToString ());
  136. Assert.Equal ("Absolute(200)", w.Height.ToString ());
  137. Assert.Equal (200, w.Frame.Width);
  138. Assert.Equal (200, w.Frame.Height);
  139. f1.Text = "Frame1";
  140. Assert.Equal (99, f1.Frame.Width); // 100-1=99
  141. Assert.Equal (5, f1.Frame.Height);
  142. f2.Text = "Frame2";
  143. Assert.Equal ("Fill(Absolute(0))", f2.Width.ToString ());
  144. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  145. Assert.Equal (99, f2.Frame.Width); // 100-1=99
  146. Assert.Equal (5, f2.Frame.Height);
  147. v1.Text = "Button1";
  148. Assert.Equal ($"Combine(View(Width,FrameView(){f1.Frame})-Absolute(2))", v1.Width.ToString ());
  149. Assert.Equal ("Combine(Fill(Absolute(0))-Absolute(2))", v1.Height.ToString ());
  150. Assert.Equal (97, v1.Frame.Width); // 99-2=97
  151. Assert.Equal (189, v1.Frame.Height); // 198-2-7=189
  152. v2.Text = "Button2";
  153. Assert.Equal ($"Combine(View(Width,FrameView(){f2.Frame})-Absolute(2))", v2.Width.ToString ());
  154. Assert.Equal ("Combine(Fill(Absolute(0))-Absolute(2))", v2.Height.ToString ());
  155. Assert.Equal (97, v2.Frame.Width); // 99-2=97
  156. Assert.Equal (189, v2.Frame.Height); // 198-2-7=189
  157. v3.Text = "Button3";
  158. // 198*10%=19 * Percent is related to the super-view if it isn't null otherwise the view width
  159. Assert.Equal (19, v3.Frame.Width);
  160. // 199*10%=19
  161. Assert.Equal (19, v3.Frame.Height);
  162. v4.Text = "Button4";
  163. v4.Width = Auto (DimAutoStyle.Text);
  164. v4.Height = Auto (DimAutoStyle.Text);
  165. v4.Layout ();
  166. Assert.Equal (Auto (DimAutoStyle.Text), v4.Width);
  167. Assert.Equal (Auto (DimAutoStyle.Text), v4.Height);
  168. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is DimAbsolute
  169. Assert.Equal (1, v4.Frame.Height); // 1 because is DimAbsolute
  170. v5.Text = "Button5";
  171. Assert.Equal ($"Combine(View(Width,Button(){v1.Frame})-View(Width,Button(){v3.Frame}))", v5.Width.ToString ());
  172. Assert.Equal ($"Combine(View(Height,Button(){v1.Frame})-View(Height,Button(){v3.Frame}))", v5.Height.ToString ());
  173. Assert.Equal (78, v5.Frame.Width); // 97-9=78
  174. Assert.Equal (170, v5.Frame.Height); // 189-19=170
  175. v6.Text = "Button6";
  176. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  177. Assert.Equal (38, v6.Frame.Height); // 198-7*20=18
  178. };
  179. Application.Iteration += (s, a) => Application.RequestStop ();
  180. Application.Run (t);
  181. t.Dispose ();
  182. }
  183. }