Pos.CombineTests.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #nullable disable
  2. using Xunit.Abstractions;
  3. using static Terminal.Gui.ViewBase.Dim;
  4. using static Terminal.Gui.ViewBase.Pos;
  5. namespace ViewBaseTests.Layout;
  6. public class PosCombineTests (ITestOutputHelper output)
  7. {
  8. private readonly ITestOutputHelper _output = output;
  9. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  10. // TODO: A new test that calls SetRelativeLayout directly is needed.
  11. [Fact]
  12. public void PosCombine_Referencing_Same_View ()
  13. {
  14. var super = new View { Width = 10, Height = 10, Text = "super" };
  15. var view1 = new View { Width = 2, Height = 2, Text = "view1" };
  16. var view2 = new View { Width = 2, Height = 2, Text = "view2" };
  17. view2.X = AnchorEnd (0) - (Right (view2) - Left (view2));
  18. super.Add (view1, view2);
  19. super.BeginInit ();
  20. super.EndInit ();
  21. Exception exception = Record.Exception (super.LayoutSubViews);
  22. Assert.Null (exception);
  23. Assert.Equal (new (0, 0, 10, 10), super.Frame);
  24. Assert.Equal (new (0, 0, 2, 2), view1.Frame);
  25. Assert.Equal (new (8, 0, 2, 2), view2.Frame);
  26. super.Dispose ();
  27. }
  28. [Fact]
  29. public void PosCombine_DimCombine_View_With_SubViews ()
  30. {
  31. IApplication app = Application.Create ();
  32. Runnable<bool> runnable = new () { Width = 80, Height = 25 };
  33. app.Begin (runnable);
  34. var win1 = new Window { Id = "win1", Width = 20, Height = 10 };
  35. var view1 = new View
  36. {
  37. Text = "view1",
  38. Width = Auto (DimAutoStyle.Text),
  39. Height = Auto (DimAutoStyle.Text)
  40. };
  41. var win2 = new Window { Id = "win2", Y = Bottom (view1) + 1, Width = 10, Height = 3 };
  42. var view2 = new View { Id = "view2", Width = Fill (), Height = 1, CanFocus = true };
  43. var view3 = new View { Id = "view3", Width = Fill (1), Height = 1, CanFocus = true };
  44. view2.Add (view3);
  45. win2.Add (view2);
  46. win1.Add (view1, win2);
  47. runnable.Add (win1);
  48. Assert.Equal (new (0, 0, 80, 25), runnable.Frame);
  49. Assert.Equal (new (0, 0, 5, 1), view1.Frame);
  50. Assert.Equal (new (0, 0, 20, 10), win1.Frame);
  51. Assert.Equal (new (0, 2, 10, 3), win2.Frame);
  52. Assert.Equal (new (0, 0, 8, 1), view2.Frame);
  53. Assert.Equal (new (0, 0, 7, 1), view3.Frame);
  54. View foundView = runnable.GetViewsUnderLocation (new (9, 4), ViewportSettingsFlags.None).LastOrDefault ();
  55. Assert.Equal (foundView, view2);
  56. runnable.Dispose ();
  57. }
  58. [Fact]
  59. public void PosCombine_Will_Throws ()
  60. {
  61. IApplication app = Application.Create ();
  62. app.Init ("fake");
  63. var t = new Runnable ();
  64. var w = new Window { X = Left (t) + 2, Y = Top (t) + 2 };
  65. var f = new FrameView ();
  66. var v1 = new View { X = Left (w) + 2, Y = Top (w) + 2 };
  67. var v2 = new View { X = Left (v1) + 2, Y = Top (v1) + 2 };
  68. f.Add (v1); // v2 not added
  69. w.Add (f);
  70. t.Add (w);
  71. f.X = X (v2) - X (v1);
  72. f.Y = Y (v2) - Y (v1);
  73. app.StopAfterFirstIteration = true;
  74. Assert.Throws<LayoutException> (() => app.Run (t));
  75. t.Dispose ();
  76. v2.Dispose ();
  77. app.Dispose ();
  78. }
  79. [Fact]
  80. public void PosCombine_Refs_SuperView_Throws ()
  81. {
  82. IApplication app = Application.Create ();
  83. app.Init ("fake");
  84. var top = new Runnable ();
  85. var w = new Window { X = Left (top) + 2, Y = Top (top) + 2 };
  86. var f = new FrameView ();
  87. var v1 = new View { X = Left (w) + 2, Y = Top (w) + 2 };
  88. var v2 = new View { X = Left (v1) + 2, Y = Top (v1) + 2 };
  89. f.Add (v1, v2);
  90. w.Add (f);
  91. top.Add (w);
  92. SessionToken token = app.Begin (top);
  93. f.X = X (app.TopRunnableView) + X (v2) - X (v1);
  94. f.Y = Y (app.TopRunnableView) + Y (v2) - Y (v1);
  95. app.TopRunnableView!.SubViewsLaidOut += (s, e) =>
  96. {
  97. Assert.Equal (0, app.TopRunnableView.Frame.X);
  98. Assert.Equal (0, app.TopRunnableView.Frame.Y);
  99. Assert.Equal (2, w.Frame.X);
  100. Assert.Equal (2, w.Frame.Y);
  101. Assert.Equal (2, f.Frame.X);
  102. Assert.Equal (2, f.Frame.Y);
  103. Assert.Equal (4, v1.Frame.X);
  104. Assert.Equal (4, v1.Frame.Y);
  105. Assert.Equal (6, v2.Frame.X);
  106. Assert.Equal (6, v2.Frame.Y);
  107. };
  108. app.StopAfterFirstIteration = true;
  109. Assert.Throws<LayoutException> (() => app.Run (top));
  110. app.TopRunnableView?.Dispose ();
  111. top.Dispose ();
  112. app.Dispose ();
  113. }
  114. }