AllViewsTests.cs 4.4 KB

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