AllViewsTests.cs 4.5 KB

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