2
0

Dim.CombineTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using Xunit.Abstractions;
  2. using static Terminal.Gui.Dim;
  3. namespace Terminal.Gui.LayoutTests;
  4. public class DimCombineTests (ITestOutputHelper output)
  5. {
  6. private readonly ITestOutputHelper _output = output;
  7. [Fact]
  8. public void DimCombine_Calculate_ReturnsCorrectValue ()
  9. {
  10. var dim1 = new DimAbsolute (10);
  11. var dim2 = new DimAbsolute (20);
  12. var dim = dim1 + dim2;
  13. var result = dim.Calculate (0, 100, null, Dimension.None);
  14. Assert.Equal (30, result);
  15. }
  16. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  17. // TODO: A new test that calls SetRelativeLayout directly is needed.
  18. [Fact]
  19. [TestRespondersDisposed]
  20. public void DimCombine_ObtuseScenario_Does_Not_Throw_If_Two_SubViews_Refs_The_Same_SuperView ()
  21. {
  22. var t = new View { Width = 80, Height = 25, Text = "top" };
  23. var w = new Window
  24. {
  25. Width = Dim.Width (t) - 2, // 78
  26. Height = Dim.Height (t) - 2 // 23
  27. };
  28. var f = new FrameView ();
  29. var v1 = new View
  30. {
  31. Width = Dim.Width (w) - 2, // 76
  32. Height = Dim.Height (w) - 2 // 21
  33. };
  34. var v2 = new View
  35. {
  36. Width = Dim.Width (v1) - 2, // 74
  37. Height = Dim.Height (v1) - 2 // 19
  38. };
  39. f.Add (v1, v2);
  40. w.Add (f);
  41. t.Add (w);
  42. t.BeginInit ();
  43. t.EndInit ();
  44. f.Width = Dim.Width (t) - Dim.Width (w) + 4; // 80 - 74 = 6
  45. f.Height = Dim.Height (t) - Dim.Height (w) + 4; // 25 - 19 = 6
  46. // BUGBUG: v2 - f references t and w here; t is f's super-superview and w is f's superview. This is supported!
  47. Exception exception = Record.Exception (t.LayoutSubviews);
  48. Assert.Null (exception);
  49. Assert.Equal (80, t.Frame.Width);
  50. Assert.Equal (25, t.Frame.Height);
  51. Assert.Equal (78, w.Frame.Width);
  52. Assert.Equal (23, w.Frame.Height);
  53. Assert.Equal (6, f.Frame.Width);
  54. Assert.Equal (6, f.Frame.Height);
  55. Assert.Equal (76, v1.Frame.Width);
  56. Assert.Equal (21, v1.Frame.Height);
  57. Assert.Equal (74, v2.Frame.Width);
  58. Assert.Equal (19, v2.Frame.Height);
  59. t.Dispose ();
  60. }
  61. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  62. // TODO: A new test that calls SetRelativeLayout directly is needed.
  63. /// <summary>This is an intentionally obtuse test. See https://github.com/gui-cs/Terminal.Gui/issues/2461</summary>
  64. [Fact]
  65. [TestRespondersDisposed]
  66. public void DimCombine_ObtuseScenario_Throw_If_SuperView_Refs_SubView ()
  67. {
  68. var t = new View { Width = 80, Height = 25 };
  69. var w = new Window
  70. {
  71. Width = Dim.Width (t) - 2, // 78
  72. Height = Dim.Height (t) - 2 // 23
  73. };
  74. var f = new FrameView ();
  75. var v1 = new View
  76. {
  77. Width = Dim.Width (w) - 2, // 76
  78. Height = Dim.Height (w) - 2 // 21
  79. };
  80. var v2 = new View
  81. {
  82. Width = Dim.Width (v1) - 2, // 74
  83. Height = Dim.Height (v1) - 2 // 19
  84. };
  85. f.Add (v1, v2);
  86. w.Add (f);
  87. t.Add (w);
  88. t.BeginInit ();
  89. t.EndInit ();
  90. f.Width = Dim.Width (t) - Dim.Width (v2); // 80 - 74 = 6
  91. f.Height = Dim.Height (t) - Dim.Height (v2); // 25 - 19 = 6
  92. Assert.Throws<InvalidOperationException> (t.LayoutSubviews);
  93. Assert.Equal (80, t.Frame.Width);
  94. Assert.Equal (25, t.Frame.Height);
  95. Assert.Equal (78, w.Frame.Width);
  96. Assert.Equal (23, w.Frame.Height);
  97. Assert.Equal (6, f.Frame.Width);
  98. Assert.Equal (6, f.Frame.Height);
  99. Assert.Equal (76, v1.Frame.Width);
  100. Assert.Equal (21, v1.Frame.Height);
  101. Assert.Equal (74, v2.Frame.Width);
  102. Assert.Equal (19, v2.Frame.Height);
  103. t.Dispose ();
  104. }
  105. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  106. // TODO: A new test that calls SetRelativeLayout directly is needed.
  107. [Fact]
  108. [TestRespondersDisposed]
  109. public void DimCombine_View_Not_Added_Throws ()
  110. {
  111. var t = new View { Width = 80, Height = 50 };
  112. var super = new View { Width = Dim.Width (t) - 2, Height = Dim.Height (t) - 2 };
  113. t.Add (super);
  114. var sub = new View ();
  115. super.Add (sub);
  116. var v1 = new View { Width = Dim.Width (super) - 2, Height = Dim.Height (super) - 2 };
  117. var v2 = new View { Width = Dim.Width (v1) - 2, Height = Dim.Height (v1) - 2 };
  118. sub.Add (v1);
  119. // v2 not added to sub; should cause exception on Layout since it's referenced by sub.
  120. sub.Width = Dim.Fill () - Dim.Width (v2);
  121. sub.Height = Dim.Fill () - Dim.Height (v2);
  122. t.BeginInit ();
  123. t.EndInit ();
  124. Assert.Throws<InvalidOperationException> (() => t.LayoutSubviews ());
  125. t.Dispose ();
  126. v2.Dispose ();
  127. }
  128. }