Pos.CombineTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using Microsoft.VisualStudio.TestPlatform.Utilities;
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. using static Terminal.Gui.Dim;
  5. using static Terminal.Gui.Pos;
  6. namespace Terminal.Gui.LayoutTests;
  7. public class PosCombineTests (ITestOutputHelper output)
  8. {
  9. private readonly ITestOutputHelper _output = output;
  10. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  11. // TODO: A new test that calls SetRelativeLayout directly is needed.
  12. [Fact]
  13. [TestRespondersDisposed]
  14. public void PosCombine_Will_Throws ()
  15. {
  16. Application.Init (new FakeDriver ());
  17. Toplevel t = new ();
  18. var w = new Window { X = Pos.Left (t) + 2, Y = Pos.Top (t) + 2 };
  19. var f = new FrameView ();
  20. var v1 = new View { X = Pos.Left (w) + 2, Y = Pos.Top (w) + 2 };
  21. var v2 = new View { X = Pos.Left (v1) + 2, Y = Pos.Top (v1) + 2 };
  22. f.Add (v1); // v2 not added
  23. w.Add (f);
  24. t.Add (w);
  25. f.X = Pos.X (v2) - Pos.X (v1);
  26. f.Y = Pos.Y (v2) - Pos.Y (v1);
  27. Assert.Throws<LayoutException> (() => Application.Run (t));
  28. t.Dispose ();
  29. Application.Shutdown ();
  30. v2.Dispose ();
  31. }
  32. [Fact]
  33. [SetupFakeDriver]
  34. public void PosCombine_DimCombine_View_With_SubViews ()
  35. {
  36. Application.Top = new Toplevel () { Width = 80, Height = 25 };
  37. var win1 = new Window { Id = "win1", Width = 20, Height = 10 };
  38. var view1 = new View
  39. {
  40. Text = "view1",
  41. Width = Auto (DimAutoStyle.Text),
  42. Height = Auto (DimAutoStyle.Text)
  43. };
  44. var win2 = new Window { Id = "win2", Y = Pos.Bottom (view1) + 1, Width = 10, Height = 3 };
  45. var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = 1, CanFocus = true };
  46. //var clicked = false;
  47. //view2.MouseClick += (sender, e) => clicked = true;
  48. var view3 = new View { Id = "view3", Width = Dim.Fill (1), Height = 1, CanFocus = true };
  49. view2.Add (view3);
  50. win2.Add (view2);
  51. win1.Add (view1, win2);
  52. Application.Top.Add (win1);
  53. Application.Top.Layout ();
  54. Assert.Equal (new Rectangle (0, 0, 80, 25), Application.Top.Frame);
  55. Assert.Equal (new Rectangle (0, 0, 5, 1), view1.Frame);
  56. Assert.Equal (new Rectangle (0, 0, 20, 10), win1.Frame);
  57. Assert.Equal (new Rectangle (0, 2, 10, 3), win2.Frame);
  58. Assert.Equal (new Rectangle (0, 0, 8, 1), view2.Frame);
  59. Assert.Equal (new Rectangle (0, 0, 7, 1), view3.Frame);
  60. var foundView = View.GetViewsUnderMouse (new Point(9, 4)).LastOrDefault ();
  61. Assert.Equal (foundView, view2);
  62. Application.Top.Dispose ();
  63. Application.ResetState (ignoreDisposed: true);
  64. }
  65. [Fact]
  66. public void PosCombine_Refs_SuperView_Throws ()
  67. {
  68. Application.Init (new FakeDriver ());
  69. var top = new Toplevel ();
  70. var w = new Window { X = Pos.Left (top) + 2, Y = Pos.Top (top) + 2 };
  71. var f = new FrameView ();
  72. var v1 = new View { X = Pos.Left (w) + 2, Y = Pos.Top (w) + 2 };
  73. var v2 = new View { X = Pos.Left (v1) + 2, Y = Pos.Top (v1) + 2 };
  74. f.Add (v1, v2);
  75. w.Add (f);
  76. top.Add (w);
  77. Application.Begin (top);
  78. f.X = Pos.X (Application.Top) + Pos.X (v2) - Pos.X (v1);
  79. f.Y = Pos.Y (Application.Top) + Pos.Y (v2) - Pos.Y (v1);
  80. Application.Top.SubViewsLaidOut += (s, e) =>
  81. {
  82. Assert.Equal (0, Application.Top.Frame.X);
  83. Assert.Equal (0, Application.Top.Frame.Y);
  84. Assert.Equal (2, w.Frame.X);
  85. Assert.Equal (2, w.Frame.Y);
  86. Assert.Equal (2, f.Frame.X);
  87. Assert.Equal (2, f.Frame.Y);
  88. Assert.Equal (4, v1.Frame.X);
  89. Assert.Equal (4, v1.Frame.Y);
  90. Assert.Equal (6, v2.Frame.X);
  91. Assert.Equal (6, v2.Frame.Y);
  92. };
  93. Application.Iteration += (s, a) => Application.RequestStop ();
  94. Assert.Throws<LayoutException> (() => Application.Run ());
  95. top.Dispose ();
  96. Application.ResetState (ignoreDisposed: true);
  97. }
  98. }