Pos.AnchorEndTests.cs 8.9 KB

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