Pos.AnchorEndTests.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using Xunit.Abstractions;
  2. using static Terminal.Gui.Dim;
  3. using static Terminal.Gui.Pos;
  4. namespace Terminal.Gui.LayoutTests;
  5. public class PosAnchorEndTests (ITestOutputHelper output)
  6. {
  7. [Fact]
  8. public void PosAnchorEnd_Constructor ()
  9. {
  10. var posAnchorEnd = new PosAnchorEnd (10);
  11. Assert.NotNull (posAnchorEnd);
  12. }
  13. [Theory]
  14. [InlineData (0, 0, true)]
  15. [InlineData (10, 10, true)]
  16. [InlineData (0, 10, false)]
  17. [InlineData (10, 1, false)]
  18. public void PosAnchorEnd_Equals (int offset1, int offset2, bool expectedEquals)
  19. {
  20. var posAnchorEnd1 = new PosAnchorEnd (offset1);
  21. var posAnchorEnd2 = new PosAnchorEnd (offset2);
  22. Assert.Equal (expectedEquals, posAnchorEnd1.Equals (posAnchorEnd2));
  23. Assert.Equal (expectedEquals, posAnchorEnd2.Equals (posAnchorEnd1));
  24. }
  25. [Fact]
  26. public void PosAnchorEnd_ToString ()
  27. {
  28. var posAnchorEnd = new PosAnchorEnd (10);
  29. var expectedString = "AnchorEnd(10)";
  30. Assert.Equal (expectedString, posAnchorEnd.ToString ());
  31. }
  32. [Fact]
  33. public void PosAnchorEnd_GetAnchor ()
  34. {
  35. var posAnchorEnd = new PosAnchorEnd (10);
  36. var width = 50;
  37. var expectedAnchor = width - 10;
  38. Assert.Equal (expectedAnchor, posAnchorEnd.GetAnchor (width));
  39. }
  40. [Fact]
  41. public void PosAnchorEnd_CreatesCorrectInstance ()
  42. {
  43. var pos = Pos.AnchorEnd (10);
  44. Assert.IsType<PosAnchorEnd> (pos);
  45. }
  46. [Fact]
  47. public void PosAnchorEnd_Negative_Throws ()
  48. {
  49. Pos pos;
  50. int n = -1;
  51. Assert.Throws<ArgumentOutOfRangeException> (() => pos = Pos.AnchorEnd (n));
  52. }
  53. [Theory]
  54. [InlineData (0)]
  55. [InlineData (1)]
  56. public void PosAnchorEnd_SetsValue_GetAnchor_Is_Negative (int offset)
  57. {
  58. Pos pos = Pos.AnchorEnd (offset);
  59. Assert.Equal (offset, -pos.GetAnchor (0));
  60. }
  61. [Theory]
  62. [InlineData (0, 0, 25)]
  63. [InlineData (0, 10, 25)]
  64. [InlineData (1, 10, 24)]
  65. [InlineData (10, 10, 15)]
  66. [InlineData (20, 10, 5)]
  67. [InlineData (25, 10, 0)]
  68. [InlineData (26, 10, -1)]
  69. public void PosAnchorEnd_With_Offset_PositionsViewOffsetFromRight (int offset, int width, int expectedXPosition)
  70. {
  71. // Arrange
  72. var superView = new View { Width = 25, Height = 25 };
  73. var view = new View
  74. {
  75. X = Pos.AnchorEnd (offset),
  76. Width = width,
  77. Height = 1
  78. };
  79. superView.Add (view);
  80. superView.BeginInit ();
  81. superView.EndInit ();
  82. // Act
  83. superView.LayoutSubviews ();
  84. // Assert
  85. Assert.Equal (expectedXPosition, view.Frame.X);
  86. }
  87. // UseDimForOffset tests
  88. [Fact]
  89. public void PosAnchorEnd_UseDimForOffset_CreatesCorrectInstance ()
  90. {
  91. var pos = Pos.AnchorEnd ();
  92. Assert.IsType<PosAnchorEnd> (pos);
  93. Assert.True (((PosAnchorEnd)pos).UseDimForOffset);
  94. }
  95. [Fact]
  96. public void PosAnchorEnd_UseDimForOffset_SetsValue_GetAnchor_Is_Negative ()
  97. {
  98. Pos pos = Pos.AnchorEnd ();
  99. Assert.Equal (-10, -pos.GetAnchor (10));
  100. }
  101. [Theory]
  102. [InlineData (0, 25)]
  103. [InlineData (10, 15)]
  104. [InlineData (9, 16)]
  105. [InlineData (11, 14)]
  106. [InlineData (25, 0)]
  107. [InlineData (26, -1)]
  108. public void PosAnchorEnd_UseDimForOffset_PositionsViewOffsetByDim (int dim, int expectedXPosition)
  109. {
  110. // Arrange
  111. var superView = new View { Width = 25, Height = 25 };
  112. var view = new View
  113. {
  114. X = Pos.AnchorEnd (),
  115. Width = dim,
  116. Height = 1
  117. };
  118. superView.Add (view);
  119. superView.BeginInit ();
  120. superView.EndInit ();
  121. // Act
  122. superView.LayoutSubviews ();
  123. // Assert
  124. Assert.Equal (expectedXPosition, view.Frame.X);
  125. }
  126. [Theory]
  127. [InlineData (0, 25)]
  128. [InlineData (10, 23)]
  129. [InlineData (50, 13)]
  130. [InlineData (100, 0)]
  131. public void PosAnchorEnd_UseDimForOffset_DimPercent_PositionsViewOffsetByDim (int percent, int expectedXPosition)
  132. {
  133. // Arrange
  134. var superView = new View { Width = 25, Height = 25 };
  135. var view = new View
  136. {
  137. X = Pos.AnchorEnd (),
  138. Width = Dim.Percent (percent),
  139. Height = 1
  140. };
  141. superView.Add (view);
  142. superView.BeginInit ();
  143. superView.EndInit ();
  144. // Act
  145. superView.LayoutSubviews ();
  146. // Assert
  147. Assert.Equal (expectedXPosition, view.Frame.X);
  148. }
  149. // This test used to be Dialog_In_Window_With_TextField_And_Button_AnchorEnd in DialogTests.
  150. [Fact]
  151. [SetupFakeDriver]
  152. public void PosAnchorEnd_View_And_Button ()
  153. {
  154. ((FakeDriver)Application.Driver!).SetBufferSize (20, 5);
  155. // Override CM
  156. Button.DefaultShadow = ShadowStyle.None;
  157. var b = $"{CM.Glyphs.LeftBracket} Ok {CM.Glyphs.RightBracket}";
  158. var frame = new FrameView { Width = 18, Height = 3 };
  159. Assert.Equal (16, frame.Viewport.Width);
  160. Button btn = null;
  161. int Btn_Width () { return btn?.Viewport.Width ?? 0; }
  162. btn = new () { Text = "Ok", X = Pos.AnchorEnd (0) - Pos.Func (Btn_Width) };
  163. var view = new View
  164. {
  165. Text = "0123456789abcdefghij",
  166. // Dim.Fill (1) fills remaining space minus 1 (16 - 1 = 15)
  167. // Dim.Function (Btn_Width) is 6
  168. // Width should be 15 - 6 = 9
  169. Width = Dim.Fill (1) - Dim.Func (Btn_Width),
  170. Height = 1
  171. };
  172. frame.Add (btn, view);
  173. frame.BeginInit(); // Needed to enable Border
  174. frame.EndInit();
  175. frame.Layout ();
  176. Assert.Equal (6, btn.Viewport.Width);
  177. Assert.Equal (10, btn.Frame.X); // frame.Viewport.Width (16) - btn.Frame.Width (6) = 10
  178. Assert.Equal (0, btn.Frame.Y);
  179. Assert.Equal (6, btn.Frame.Width);
  180. Assert.Equal (1, btn.Frame.Height);
  181. Assert.Equal (9, view.Viewport.Width); // frame.Viewport.Width (16) - Dim.Fill (1) - Dim.Function (6) = 9
  182. Assert.Equal (0, view.Frame.X);
  183. Assert.Equal (0, view.Frame.Y);
  184. Assert.Equal (9, view.Frame.Width);
  185. Assert.Equal (1, view.Frame.Height);
  186. frame.Draw ();
  187. var expected = $@"
  188. ┌────────────────┐
  189. │012345678 {b}│
  190. └────────────────┘
  191. ";
  192. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  193. }
  194. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  195. // TODO: A new test that calls SetRelativeLayout directly is needed.
  196. [Fact]
  197. [AutoInitShutdown]
  198. public void PosAnchorEnd_Equal_Inside_Window ()
  199. {
  200. var viewWidth = 10;
  201. var viewHeight = 1;
  202. var tv = new TextView
  203. {
  204. X = Pos.AnchorEnd (viewWidth), Y = Pos.AnchorEnd (viewHeight), Width = viewWidth, Height = viewHeight
  205. };
  206. var win = new Window ();
  207. win.Add (tv);
  208. Toplevel top = new ();
  209. top.Add (win);
  210. RunState rs = Application.Begin (top);
  211. Assert.Equal (new (0, 0, 80, 25), top.Frame);
  212. Assert.Equal (new (0, 0, 80, 25), win.Frame);
  213. Assert.Equal (new (68, 22, 10, 1), tv.Frame);
  214. Application.End (rs);
  215. top.Dispose ();
  216. }
  217. //// TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  218. //// TODO: A new test that calls SetRelativeLayout directly is needed.
  219. //[Fact]
  220. //[AutoInitShutdown]
  221. //public void PosAnchorEnd_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  222. //{
  223. // var viewWidth = 10;
  224. // var viewHeight = 1;
  225. // var tv = new TextView
  226. // {
  227. // X = Pos.AnchorEnd (viewWidth), Y = Pos.AnchorEnd (viewHeight), Width = viewWidth, Height = viewHeight
  228. // };
  229. // var win = new Window ();
  230. // win.Add (tv);
  231. // var menu = new MenuBar ();
  232. // var status = new StatusBar ();
  233. // Toplevel top = new ();
  234. // top.Add (win, menu, status);
  235. // RunState rs = Application.Begin (top);
  236. // Assert.Equal (new (0, 0, 80, 25), top.Frame);
  237. // Assert.Equal (new (0, 0, 80, 1), menu.Frame);
  238. // Assert.Equal (new (0, 24, 80, 1), status.Frame);
  239. // Assert.Equal (new (0, 1, 80, 23), win.Frame);
  240. // Assert.Equal (new (68, 20, 10, 1), tv.Frame);
  241. // Application.End (rs);
  242. // top.Dispose ();
  243. //}
  244. [Fact]
  245. public void PosAnchorEnd_Calculate_ReturnsExpectedValue ()
  246. {
  247. var posAnchorEnd = new PosAnchorEnd (5);
  248. var result = posAnchorEnd.Calculate (10, new DimAbsolute (2), null, Dimension.None);
  249. Assert.Equal (5, result);
  250. }
  251. [Fact]
  252. public void PosAnchorEnd_MinusOne_Combine_Works ()
  253. {
  254. var pos = AnchorEnd () - 1;
  255. var result = pos.Calculate (10, new DimAbsolute (2), null, Dimension.None);
  256. Assert.Equal (7, result);
  257. }
  258. }