Pos.AnchorEndTests.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. var b = $"{CM.Glyphs.LeftBracket} Ok {CM.Glyphs.RightBracket}";
  156. var frame = new FrameView { Width = 18, Height = 3 };
  157. Assert.Equal (16, frame.Viewport.Width);
  158. Button btn = null;
  159. int Btn_Width () { return btn?.Viewport.Width ?? 0; }
  160. btn = new () { Text = "Ok", X = Pos.AnchorEnd (0) - Pos.Func (Btn_Width) };
  161. var view = new View
  162. {
  163. Text = "0123456789abcdefghij",
  164. // Dim.Fill (1) fills remaining space minus 1 (16 - 1 = 15)
  165. // Dim.Function (Btn_Width) is 6
  166. // Width should be 15 - 6 = 9
  167. Width = Dim.Fill (1) - Dim.Func (Btn_Width),
  168. Height = 1
  169. };
  170. frame.Add (btn, view);
  171. frame.BeginInit ();
  172. frame.EndInit ();
  173. frame.Draw ();
  174. Assert.Equal (6, btn.Viewport.Width);
  175. Assert.Equal (10, btn.Frame.X); // frame.Viewport.Width (16) - btn.Frame.Width (6) = 10
  176. Assert.Equal (0, btn.Frame.Y);
  177. Assert.Equal (6, btn.Frame.Width);
  178. Assert.Equal (1, btn.Frame.Height);
  179. Assert.Equal (9, view.Viewport.Width); // frame.Viewport.Width (16) - Dim.Fill (1) - Dim.Function (6) = 9
  180. Assert.Equal (0, view.Frame.X);
  181. Assert.Equal (0, view.Frame.Y);
  182. Assert.Equal (9, view.Frame.Width);
  183. Assert.Equal (1, view.Frame.Height);
  184. var expected = $@"
  185. ┌────────────────┐
  186. │012345678 {b}│
  187. └────────────────┘
  188. ";
  189. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  190. }
  191. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  192. // TODO: A new test that calls SetRelativeLayout directly is needed.
  193. [Fact]
  194. [AutoInitShutdown]
  195. public void PosAnchorEnd_Equal_Inside_Window ()
  196. {
  197. var viewWidth = 10;
  198. var viewHeight = 1;
  199. var tv = new TextView
  200. {
  201. X = Pos.AnchorEnd (viewWidth), Y = Pos.AnchorEnd (viewHeight), Width = viewWidth, Height = viewHeight
  202. };
  203. var win = new Window ();
  204. win.Add (tv);
  205. Toplevel top = new ();
  206. top.Add (win);
  207. RunState rs = Application.Begin (top);
  208. Assert.Equal (new (0, 0, 80, 25), top.Frame);
  209. Assert.Equal (new (0, 0, 80, 25), win.Frame);
  210. Assert.Equal (new (68, 22, 10, 1), tv.Frame);
  211. Application.End (rs);
  212. top.Dispose ();
  213. }
  214. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  215. // TODO: A new test that calls SetRelativeLayout directly is needed.
  216. [Fact]
  217. [AutoInitShutdown]
  218. public void PosAnchorEnd_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  219. {
  220. var viewWidth = 10;
  221. var viewHeight = 1;
  222. var tv = new TextView
  223. {
  224. X = Pos.AnchorEnd (viewWidth), Y = Pos.AnchorEnd (viewHeight), Width = viewWidth, Height = viewHeight
  225. };
  226. var win = new Window ();
  227. win.Add (tv);
  228. var menu = new MenuBar ();
  229. var status = new StatusBar ();
  230. Toplevel top = new ();
  231. top.Add (win, menu, status);
  232. RunState rs = Application.Begin (top);
  233. Assert.Equal (new (0, 0, 80, 25), top.Frame);
  234. Assert.Equal (new (0, 0, 80, 1), menu.Frame);
  235. Assert.Equal (new (0, 24, 80, 1), status.Frame);
  236. Assert.Equal (new (0, 1, 80, 23), win.Frame);
  237. Assert.Equal (new (68, 20, 10, 1), tv.Frame);
  238. Application.End (rs);
  239. top.Dispose ();
  240. }
  241. [Fact]
  242. public void PosAnchorEnd_Calculate_ReturnsExpectedValue ()
  243. {
  244. var posAnchorEnd = new PosAnchorEnd (5);
  245. var result = posAnchorEnd.Calculate (10, new DimAbsolute (2), null, Dimension.None);
  246. Assert.Equal (5, result);
  247. }
  248. [Fact]
  249. public void PosAnchorEnd_MinusOne_Combine_Works ()
  250. {
  251. var pos = AnchorEnd () - 1;
  252. var result = pos.Calculate (10, new DimAbsolute (2), null, Dimension.None);
  253. Assert.Equal (7, result);
  254. }
  255. }