AnchorEndTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewTests;
  3. public class AnchorEndTests (ITestOutputHelper output)
  4. {
  5. [Fact]
  6. public void AnchorEnd_Equal ()
  7. {
  8. var n1 = 0;
  9. var n2 = 0;
  10. Pos pos1 = Pos.AnchorEnd (n1);
  11. Pos pos2 = Pos.AnchorEnd (n2);
  12. Assert.Equal (pos1, pos2);
  13. // Test inequality
  14. n2 = 5;
  15. pos2 = Pos.AnchorEnd (n2);
  16. Assert.NotEqual (pos1, pos2);
  17. }
  18. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  19. // TODO: A new test that calls SetRelativeLayout directly is needed.
  20. [Fact]
  21. [AutoInitShutdown]
  22. public void AnchorEnd_Equal_Inside_Window ()
  23. {
  24. var viewWidth = 10;
  25. var viewHeight = 1;
  26. var tv = new TextView
  27. {
  28. X = Pos.AnchorEnd (viewWidth), Y = Pos.AnchorEnd (viewHeight), Width = viewWidth, Height = viewHeight
  29. };
  30. var win = new Window ();
  31. win.Add (tv);
  32. Toplevel top = new ();
  33. top.Add (win);
  34. RunState rs = Application.Begin (top);
  35. Assert.Equal (new (0, 0, 80, 25), top.Frame);
  36. Assert.Equal (new (0, 0, 80, 25), win.Frame);
  37. Assert.Equal (new (68, 22, 10, 1), tv.Frame);
  38. Application.End (rs);
  39. }
  40. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  41. // TODO: A new test that calls SetRelativeLayout directly is needed.
  42. [Fact]
  43. [AutoInitShutdown]
  44. public void AnchorEnd_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
  45. {
  46. var viewWidth = 10;
  47. var viewHeight = 1;
  48. var tv = new TextView
  49. {
  50. X = Pos.AnchorEnd (viewWidth), Y = Pos.AnchorEnd (viewHeight), Width = viewWidth, Height = viewHeight
  51. };
  52. var win = new Window ();
  53. win.Add (tv);
  54. var menu = new MenuBar ();
  55. var status = new StatusBar ();
  56. Toplevel top = new ();
  57. top.Add (win, menu, status);
  58. RunState rs = Application.Begin (top);
  59. Assert.Equal (new (0, 0, 80, 25), top.Frame);
  60. Assert.Equal (new (0, 0, 80, 1), menu.Frame);
  61. Assert.Equal (new (0, 24, 80, 1), status.Frame);
  62. Assert.Equal (new (0, 1, 80, 23), win.Frame);
  63. Assert.Equal (new (68, 20, 10, 1), tv.Frame);
  64. Application.End (rs);
  65. }
  66. [Fact]
  67. public void AnchorEnd_Negative_Throws ()
  68. {
  69. Pos pos;
  70. int n = -1;
  71. Assert.Throws<ArgumentException> (() => pos = Pos.AnchorEnd (n));
  72. }
  73. [Fact]
  74. public void AnchorEnd_SetsValue ()
  75. {
  76. var n = 0;
  77. Pos pos = Pos.AnchorEnd (0);
  78. Assert.Equal ($"AnchorEnd({n})", pos.ToString ());
  79. n = 5;
  80. pos = Pos.AnchorEnd (n);
  81. Assert.Equal ($"AnchorEnd({n})", pos.ToString ());
  82. }
  83. // This test used to be Dialog_In_Window_With_TextField_And_Button_AnchorEnd in DialogTests.
  84. [Fact]
  85. [SetupFakeDriver]
  86. public void AnchorEnd_View_And_Button ()
  87. {
  88. ((FakeDriver)Application.Driver).SetBufferSize (20, 5);
  89. var b = $"{CM.Glyphs.LeftBracket} Ok {CM.Glyphs.RightBracket}";
  90. var frame = new FrameView { Width = 18, Height = 3 };
  91. Assert.Equal (16, frame.Viewport.Width);
  92. Button btn = null;
  93. int Btn_Width () { return btn?.Viewport.Width ?? 0; }
  94. btn = new() { Text = "Ok", X = Pos.AnchorEnd (0) - Pos.Function (Btn_Width) };
  95. var view = new View
  96. {
  97. Text = "0123456789abcdefghij",
  98. // Dim.Fill (1) fills remaining space minus 1 (16 - 1 = 15)
  99. // Dim.Function (Btn_Width) is 6
  100. // Width should be 15 - 6 = 9
  101. Width = Dim.Fill (1) - Dim.Function (Btn_Width),
  102. Height = 1
  103. };
  104. frame.Add (btn, view);
  105. frame.BeginInit ();
  106. frame.EndInit ();
  107. frame.Draw ();
  108. Assert.Equal (6, btn.Viewport.Width);
  109. Assert.Equal (10, btn.Frame.X); // frame.Viewport.Width (16) - btn.Frame.Width (6) = 10
  110. Assert.Equal (0, btn.Frame.Y);
  111. Assert.Equal (6, btn.Frame.Width);
  112. Assert.Equal (1, btn.Frame.Height);
  113. Assert.Equal (9, view.Viewport.Width); // frame.Viewport.Width (16) - Dim.Fill (1) - Dim.Function (6) = 9
  114. Assert.Equal (0, view.Frame.X);
  115. Assert.Equal (0, view.Frame.Y);
  116. Assert.Equal (9, view.Frame.Width);
  117. Assert.Equal (1, view.Frame.Height);
  118. var expected = $@"
  119. ┌────────────────┐
  120. │012345678 {
  121. b
  122. }│
  123. └────────────────┘
  124. ";
  125. _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  126. }
  127. }