Pos.CombineTests.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Microsoft.VisualStudio.TestPlatform.Utilities;
  2. using Xunit.Abstractions;
  3. using static Terminal.Gui.Dim;
  4. using static Terminal.Gui.Pos;
  5. namespace Terminal.Gui.LayoutTests;
  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 = Pos.AnchorEnd (0) - (Pos.Right (view2) - Pos.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. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  29. // TODO: A new test that calls SetRelativeLayout directly is needed.
  30. [Fact]
  31. [TestRespondersDisposed]
  32. public void PosCombine_Will_Throws ()
  33. {
  34. Application.Init (new FakeDriver ());
  35. Toplevel t = new ();
  36. var w = new Window { X = Pos.Left (t) + 2, Y = Pos.Top (t) + 2 };
  37. var f = new FrameView ();
  38. var v1 = new View { X = Pos.Left (w) + 2, Y = Pos.Top (w) + 2 };
  39. var v2 = new View { X = Pos.Left (v1) + 2, Y = Pos.Top (v1) + 2 };
  40. f.Add (v1); // v2 not added
  41. w.Add (f);
  42. t.Add (w);
  43. f.X = Pos.X (v2) - Pos.X (v1);
  44. f.Y = Pos.Y (v2) - Pos.Y (v1);
  45. Assert.Throws<InvalidOperationException> (() => Application.Run (t));
  46. t.Dispose ();
  47. Application.Shutdown ();
  48. v2.Dispose ();
  49. }
  50. [Fact]
  51. [SetupFakeDriver]
  52. public void PosCombine_DimCombine_View_With_SubViews ()
  53. {
  54. Application.Top = new Toplevel () { Width = 80, Height = 25 };
  55. var win1 = new Window { Id = "win1", Width = 20, Height = 10 };
  56. var view1 = new View
  57. {
  58. Text = "view1",
  59. Width = Auto (DimAutoStyle.Text),
  60. Height = Auto (DimAutoStyle.Text)
  61. };
  62. var win2 = new Window { Id = "win2", Y = Pos.Bottom (view1) + 1, Width = 10, Height = 3 };
  63. var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = 1, CanFocus = true };
  64. //var clicked = false;
  65. //view2.MouseClick += (sender, e) => clicked = true;
  66. var view3 = new View { Id = "view3", Width = Dim.Fill (1), Height = 1, CanFocus = true };
  67. view2.Add (view3);
  68. win2.Add (view2);
  69. win1.Add (view1, win2);
  70. Application.Top.Add (win1);
  71. Application.Top.Layout ();
  72. Assert.Equal (new Rectangle (0, 0, 80, 25), Application.Top.Frame);
  73. Assert.Equal (new Rectangle (0, 0, 5, 1), view1.Frame);
  74. Assert.Equal (new Rectangle (0, 0, 20, 10), win1.Frame);
  75. Assert.Equal (new Rectangle (0, 2, 10, 3), win2.Frame);
  76. Assert.Equal (new Rectangle (0, 0, 8, 1), view2.Frame);
  77. Assert.Equal (new Rectangle (0, 0, 7, 1), view3.Frame);
  78. var foundView = View.GetViewsUnderMouse (new Point(9, 4)).LastOrDefault ();
  79. Assert.Equal (foundView, view2);
  80. Application.Top.Dispose ();
  81. Application.ResetState (ignoreDisposed: true);
  82. }
  83. [Fact]
  84. public void PosCombine_Refs_SuperView_Throws ()
  85. {
  86. Application.Init (new FakeDriver ());
  87. var top = new Toplevel ();
  88. var w = new Window { X = Pos.Left (top) + 2, Y = Pos.Top (top) + 2 };
  89. var f = new FrameView ();
  90. var v1 = new View { X = Pos.Left (w) + 2, Y = Pos.Top (w) + 2 };
  91. var v2 = new View { X = Pos.Left (v1) + 2, Y = Pos.Top (v1) + 2 };
  92. f.Add (v1, v2);
  93. w.Add (f);
  94. top.Add (w);
  95. Application.Begin (top);
  96. f.X = Pos.X (Application.Top) + Pos.X (v2) - Pos.X (v1);
  97. f.Y = Pos.Y (Application.Top) + Pos.Y (v2) - Pos.Y (v1);
  98. Application.Top.LayoutComplete += (s, e) =>
  99. {
  100. Assert.Equal (0, Application.Top.Frame.X);
  101. Assert.Equal (0, Application.Top.Frame.Y);
  102. Assert.Equal (2, w.Frame.X);
  103. Assert.Equal (2, w.Frame.Y);
  104. Assert.Equal (2, f.Frame.X);
  105. Assert.Equal (2, f.Frame.Y);
  106. Assert.Equal (4, v1.Frame.X);
  107. Assert.Equal (4, v1.Frame.Y);
  108. Assert.Equal (6, v2.Frame.X);
  109. Assert.Equal (6, v2.Frame.Y);
  110. };
  111. Application.Iteration += (s, a) => Application.RequestStop ();
  112. Assert.Throws<InvalidOperationException> (() => Application.Run ());
  113. top.Dispose ();
  114. Application.ResetState (ignoreDisposed: true);
  115. }
  116. }