AnchorEndTests.cs 8.3 KB

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