LayoutTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.LayoutTests;
  3. public class LayoutTests (ITestOutputHelper output)
  4. {
  5. private readonly ITestOutputHelper _output = output;
  6. [Fact]
  7. public void LayoutSubviews_No_SuperView ()
  8. {
  9. var root = new View ();
  10. var first = new View
  11. {
  12. Id = "first",
  13. X = 1,
  14. Y = 2,
  15. Height = 3,
  16. Width = 4
  17. };
  18. root.Add (first);
  19. var second = new View { Id = "second" };
  20. root.Add (second);
  21. second.X = Pos.Right (first) + 1;
  22. root.LayoutSubviews ();
  23. Assert.Equal (6, second.Frame.X);
  24. root.Dispose ();
  25. first.Dispose ();
  26. second.Dispose ();
  27. }
  28. [Fact]
  29. public void LayoutSubviews_RootHas_SuperView ()
  30. {
  31. var top = new View ();
  32. var root = new View ();
  33. top.Add (root);
  34. var first = new View
  35. {
  36. Id = "first",
  37. X = 1,
  38. Y = 2,
  39. Height = 3,
  40. Width = 4
  41. };
  42. root.Add (first);
  43. var second = new View { Id = "second" };
  44. root.Add (second);
  45. second.X = Pos.Right (first) + 1;
  46. root.LayoutSubviews ();
  47. Assert.Equal (6, second.Frame.X);
  48. root.Dispose ();
  49. top.Dispose ();
  50. first.Dispose ();
  51. second.Dispose ();
  52. }
  53. [Fact]
  54. public void LayoutSubviews_ViewThatRefsSubView_Throws ()
  55. {
  56. var root = new View ();
  57. var super = new View ();
  58. root.Add (super);
  59. var sub = new View ();
  60. super.Add (sub);
  61. super.Width = Dim.Width (sub);
  62. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  63. root.Dispose ();
  64. super.Dispose ();
  65. }
  66. [Fact]
  67. public void TopologicalSort_Missing_Add ()
  68. {
  69. var root = new View ();
  70. var sub1 = new View ();
  71. root.Add (sub1);
  72. var sub2 = new View ();
  73. sub1.Width = Dim.Width (sub2);
  74. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  75. sub2.Width = Dim.Width (sub1);
  76. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  77. root.Dispose ();
  78. sub1.Dispose ();
  79. sub2.Dispose ();
  80. }
  81. [Fact]
  82. public void TopologicalSort_Recursive_Ref ()
  83. {
  84. var root = new View ();
  85. var sub1 = new View ();
  86. root.Add (sub1);
  87. var sub2 = new View ();
  88. root.Add (sub2);
  89. sub2.Width = Dim.Width (sub2);
  90. Exception exception = Record.Exception (root.LayoutSubviews);
  91. Assert.Null (exception);
  92. root.Dispose ();
  93. sub1.Dispose ();
  94. sub2.Dispose ();
  95. }
  96. //[Fact]
  97. //[AutoInitShutdown]
  98. //public void TrySetHeight_ForceValidatePosDim ()
  99. //{
  100. // var top = new View { X = 0, Y = 0, Height = 20 };
  101. // var v = new View { Height = Dim.Fill (), ValidatePosDim = true };
  102. // top.Add (v);
  103. // Assert.False (v.TrySetHeight (10, out int rHeight));
  104. // Assert.Equal (10, rHeight);
  105. // v.Height = Dim.Fill (1);
  106. // Assert.False (v.TrySetHeight (10, out rHeight));
  107. // Assert.Equal (9, rHeight);
  108. // v.Height = 0;
  109. // Assert.True (v.TrySetHeight (10, out rHeight));
  110. // Assert.Equal (10, rHeight);
  111. // Assert.False (v.IsInitialized);
  112. // var toplevel = new Toplevel ();
  113. // toplevel.Add (top);
  114. // Application.Begin (toplevel);
  115. // Assert.True (v.IsInitialized);
  116. // v.Height = 15;
  117. // Assert.True (v.TrySetHeight (5, out rHeight));
  118. // Assert.Equal (5, rHeight);
  119. //}
  120. //[Fact]
  121. //[AutoInitShutdown]
  122. //public void TrySetWidth_ForceValidatePosDim ()
  123. //{
  124. // var top = new View { X = 0, Y = 0, Width = 80 };
  125. // var v = new View { Width = Dim.Fill (), ValidatePosDim = true };
  126. // top.Add (v);
  127. // Assert.False (v.TrySetWidth (70, out int rWidth));
  128. // Assert.Equal (70, rWidth);
  129. // v.Width = Dim.Fill (1);
  130. // Assert.False (v.TrySetWidth (70, out rWidth));
  131. // Assert.Equal (69, rWidth);
  132. // v.Width = 0;
  133. // Assert.True (v.TrySetWidth (70, out rWidth));
  134. // Assert.Equal (70, rWidth);
  135. // Assert.False (v.IsInitialized);
  136. // var toplevel = new Toplevel ();
  137. // toplevel.Add (top);
  138. // Application.Begin (toplevel);
  139. // Assert.True (v.IsInitialized);
  140. // v.Width = 75;
  141. // Assert.True (v.TrySetWidth (60, out rWidth));
  142. // Assert.Equal (60, rWidth);
  143. //}
  144. }