Pos.CombineTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Microsoft.VisualStudio.TestPlatform.Utilities;
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. using static Terminal.Gui.ViewBase.Dim;
  5. using static Terminal.Gui.ViewBase.Pos;
  6. namespace UnitTests.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. [SetupFakeApplication]
  14. public void PosCombine_Will_Throws ()
  15. {
  16. Toplevel t = new ();
  17. var w = new Window { X = Pos.Left (t) + 2, Y = Pos.Top (t) + 2 };
  18. var f = new FrameView ();
  19. var v1 = new View { X = Pos.Left (w) + 2, Y = Pos.Top (w) + 2 };
  20. var v2 = new View { X = Pos.Left (v1) + 2, Y = Pos.Top (v1) + 2 };
  21. f.Add (v1); // v2 not added
  22. w.Add (f);
  23. t.Add (w);
  24. f.X = Pos.X (v2) - Pos.X (v1);
  25. f.Y = Pos.Y (v2) - Pos.Y (v1);
  26. Assert.Throws<LayoutException> (() => Application.Run (t));
  27. t.Dispose ();
  28. v2.Dispose ();
  29. }
  30. [Fact]
  31. [SetupFakeApplication]
  32. public void PosCombine_DimCombine_View_With_SubViews ()
  33. {
  34. Application.TopRunnable = new Toplevel () { Width = 80, Height = 25 };
  35. var win1 = new Window { Id = "win1", Width = 20, Height = 10 };
  36. var view1 = new View
  37. {
  38. Text = "view1",
  39. Width = Auto (DimAutoStyle.Text),
  40. Height = Auto (DimAutoStyle.Text)
  41. };
  42. var win2 = new Window { Id = "win2", Y = Pos.Bottom (view1) + 1, Width = 10, Height = 3 };
  43. var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = 1, CanFocus = true };
  44. //var clicked = false;
  45. //view2.MouseClick += (sender, e) => clicked = true;
  46. var view3 = new View { Id = "view3", Width = Dim.Fill (1), Height = 1, CanFocus = true };
  47. view2.Add (view3);
  48. win2.Add (view2);
  49. win1.Add (view1, win2);
  50. Application.TopRunnable.Add (win1);
  51. Application.TopRunnable.Layout ();
  52. Assert.Equal (new Rectangle (0, 0, 80, 25), Application.TopRunnable.Frame);
  53. Assert.Equal (new Rectangle (0, 0, 5, 1), view1.Frame);
  54. Assert.Equal (new Rectangle (0, 0, 20, 10), win1.Frame);
  55. Assert.Equal (new Rectangle (0, 2, 10, 3), win2.Frame);
  56. Assert.Equal (new Rectangle (0, 0, 8, 1), view2.Frame);
  57. Assert.Equal (new Rectangle (0, 0, 7, 1), view3.Frame);
  58. var foundView = Application.TopRunnable.GetViewsUnderLocation (new Point(9, 4), ViewportSettingsFlags.None).LastOrDefault ();
  59. Assert.Equal (foundView, view2);
  60. Application.TopRunnable.Dispose ();
  61. }
  62. [Fact]
  63. public void PosCombine_Refs_SuperView_Throws ()
  64. {
  65. Application.Init ("fake");
  66. var top = new Toplevel ();
  67. var w = new Window { X = Pos.Left (top) + 2, Y = Pos.Top (top) + 2 };
  68. var f = new FrameView ();
  69. var v1 = new View { X = Pos.Left (w) + 2, Y = Pos.Top (w) + 2 };
  70. var v2 = new View { X = Pos.Left (v1) + 2, Y = Pos.Top (v1) + 2 };
  71. f.Add (v1, v2);
  72. w.Add (f);
  73. top.Add (w);
  74. Application.Begin (top);
  75. f.X = Pos.X (Application.TopRunnable) + Pos.X (v2) - Pos.X (v1);
  76. f.Y = Pos.Y (Application.TopRunnable) + Pos.Y (v2) - Pos.Y (v1);
  77. Application.TopRunnable.SubViewsLaidOut += (s, e) =>
  78. {
  79. Assert.Equal (0, Application.TopRunnable.Frame.X);
  80. Assert.Equal (0, Application.TopRunnable.Frame.Y);
  81. Assert.Equal (2, w.Frame.X);
  82. Assert.Equal (2, w.Frame.Y);
  83. Assert.Equal (2, f.Frame.X);
  84. Assert.Equal (2, f.Frame.Y);
  85. Assert.Equal (4, v1.Frame.X);
  86. Assert.Equal (4, v1.Frame.Y);
  87. Assert.Equal (6, v2.Frame.X);
  88. Assert.Equal (6, v2.Frame.Y);
  89. };
  90. Application.StopAfterFirstIteration = true;
  91. Assert.Throws<LayoutException> (() => Application.Run ());
  92. Application.TopRunnable.Dispose ();
  93. top.Dispose ();
  94. Application.Shutdown ();
  95. }
  96. }