AllViewsTests.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System.Collections;
  2. using System.Reflection;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewsTests;
  5. public class AllViewsTests (ITestOutputHelper output) : TestsAllViews
  6. {
  7. // TODO: Update all these tests to use AllViews like AllViews_Center_Properly does
  8. [Theory]
  9. [MemberData (nameof (AllViewTypes))]
  10. public void AllViews_Center_Properly (Type viewType)
  11. {
  12. var view = (View)CreateInstanceIfNotGeneric (viewType);
  13. // See https://github.com/gui-cs/Terminal.Gui/issues/3156
  14. if (view == null)
  15. {
  16. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  17. Application.Shutdown ();
  18. return;
  19. }
  20. view.X = Pos.Center ();
  21. view.Y = Pos.Center ();
  22. // Ensure the view has positive dimensions
  23. view.Width = 10;
  24. view.Height = 10;
  25. var frame = new View { X = 0, Y = 0, Width = 50, Height = 50 };
  26. frame.Add (view);
  27. frame.BeginInit ();
  28. frame.EndInit ();
  29. frame.LayoutSubviews ();
  30. // What's the natural width/height?
  31. int expectedX = (frame.Frame.Width - view.Frame.Width) / 2;
  32. int expectedY = (frame.Frame.Height - view.Frame.Height) / 2;
  33. Assert.True (
  34. view.Frame.Left == expectedX,
  35. $"{view} did not center horizontally. Expected: {expectedX}. Actual: {view.Frame.Left}"
  36. );
  37. Assert.True (
  38. view.Frame.Top == expectedY,
  39. $"{view} did not center vertically. Expected: {expectedY}. Actual: {view.Frame.Top}"
  40. );
  41. Application.Shutdown ();
  42. }
  43. [Theory]
  44. [MemberData (nameof (AllViewTypes))]
  45. public void AllViews_Enter_Leave_Events (Type viewType)
  46. {
  47. var vType = (View)CreateInstanceIfNotGeneric (viewType);
  48. if (vType == null)
  49. {
  50. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  51. return;
  52. }
  53. Application.Init (new FakeDriver ());
  54. Toplevel top = new ();
  55. vType.X = 0;
  56. vType.Y = 0;
  57. vType.Width = 10;
  58. vType.Height = 1;
  59. var view = new View
  60. {
  61. X = 0,
  62. Y = 1,
  63. Width = 10,
  64. Height = 1,
  65. CanFocus = true
  66. };
  67. var vTypeEnter = 0;
  68. var vTypeLeave = 0;
  69. var viewEnter = 0;
  70. var viewLeave = 0;
  71. vType.Enter += (s, e) => vTypeEnter++;
  72. vType.Leave += (s, e) => vTypeLeave++;
  73. view.Enter += (s, e) => viewEnter++;
  74. view.Leave += (s, e) => viewLeave++;
  75. top.Add (vType, view);
  76. Application.Begin (top);
  77. if (!vType.CanFocus || (vType is Toplevel && ((Toplevel)vType).Modal))
  78. {
  79. top.Dispose ();
  80. Application.Shutdown ();
  81. return;
  82. }
  83. if (vType is TextView)
  84. {
  85. top.NewKeyDownEvent (Key.Tab.WithCtrl);
  86. }
  87. else if (vType is DatePicker)
  88. {
  89. for (var i = 0; i < 4; i++)
  90. {
  91. top.NewKeyDownEvent (Key.Tab.WithCtrl);
  92. }
  93. }
  94. else
  95. {
  96. top.NewKeyDownEvent (Key.Tab);
  97. }
  98. top.NewKeyDownEvent (Key.Tab);
  99. Assert.Equal (2, vTypeEnter);
  100. Assert.Equal (1, vTypeLeave);
  101. Assert.Equal (1, viewEnter);
  102. Assert.Equal (1, viewLeave);
  103. top.Dispose ();
  104. Application.Shutdown ();
  105. }
  106. [Theory]
  107. [MemberData (nameof (AllViewTypes))]
  108. public void AllViews_Tests_All_Constructors (Type viewType)
  109. {
  110. Assert.True (Test_All_Constructors_Of_Type (viewType));
  111. }
  112. //[Fact]
  113. //public void AllViews_HotKey_Works ()
  114. //{
  115. // foreach (var type in GetAllViewClasses ()) {
  116. // _output.WriteLine ($"Testing {type.Name}");
  117. // var view = GetTypeInitializer (type, type.GetConstructor (Array.Empty<Type> ()));
  118. // view.HotKeySpecifier = (Rune)'^';
  119. // view.Text = "^text";
  120. // Assert.Equal(Key.T, view.HotKey);
  121. // }
  122. //}
  123. public bool Test_All_Constructors_Of_Type (Type type)
  124. {
  125. foreach (ConstructorInfo ctor in type.GetConstructors ())
  126. {
  127. View view = TestHelpers.CreateViewFromType (type, ctor);
  128. if (view != null)
  129. {
  130. Assert.True (type.FullName == view.GetType ().FullName);
  131. }
  132. }
  133. return true;
  134. }
  135. }