LayoutTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. public void LayoutSubviews_Uses_ContentSize ()
  98. {
  99. var superView = new View ()
  100. {
  101. Width = 5,
  102. Height = 5,
  103. };
  104. superView.SetContentSize (new (10, 10));
  105. var view = new View ()
  106. {
  107. X = Pos.Center ()
  108. };
  109. superView.Add (view);
  110. superView.LayoutSubviews ();
  111. Assert.Equal (5, view.Frame.X);
  112. superView.Dispose ();
  113. }
  114. // Test OnLayoutStarted/OnLayoutComplete - ensure that they are called at right times
  115. [Fact]
  116. public void LayoutSubviews_LayoutStarted_Complete ()
  117. {
  118. var superView = new View ();
  119. var view = new View ();
  120. superView.Add (view);
  121. superView.BeginInit ();
  122. superView.EndInit ();
  123. var layoutStarted = false;
  124. var layoutComplete = false;
  125. var borderLayoutStarted = false;
  126. var borderLayoutComplete = false;
  127. view.LayoutStarted += (sender, e) => layoutStarted = true;
  128. view.LayoutComplete += (sender, e) => layoutComplete = true;
  129. view.Border.LayoutStarted += (sender, e) =>
  130. {
  131. Assert.True (layoutStarted);
  132. borderLayoutStarted = true;
  133. };
  134. view.Border.LayoutComplete += (sender, e) =>
  135. {
  136. Assert.True (layoutStarted);
  137. Assert.False (layoutComplete);
  138. borderLayoutComplete = true;
  139. };
  140. superView.LayoutSubviews ();
  141. Assert.True (borderLayoutStarted);
  142. Assert.True (borderLayoutComplete);
  143. Assert.True (layoutStarted);
  144. Assert.True (layoutComplete);
  145. superView.Dispose ();
  146. }
  147. }