AllViewsTests.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System.Collections;
  2. using System.Reflection;
  3. using System.Text;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.ViewsTests;
  6. public class AllViewsTests (ITestOutputHelper output) : TestsAllViews
  7. {
  8. // TODO: Update all these tests to use AllViews like AllViews_Center_Properly does
  9. [Theory]
  10. [MemberData (nameof (AllViewTypes))]
  11. public void AllViews_Center_Properly (Type viewType)
  12. {
  13. var view = (View)CreateInstanceIfNotGeneric (viewType);
  14. // See https://github.com/gui-cs/Terminal.Gui/issues/3156
  15. if (view == null)
  16. {
  17. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  18. Application.Shutdown ();
  19. return;
  20. }
  21. if (view is IDesignable designable)
  22. {
  23. designable.EnableForDesign ();
  24. }
  25. view.X = Pos.Center ();
  26. view.Y = Pos.Center ();
  27. // Ensure the view has positive dimensions
  28. view.Width = 10;
  29. view.Height = 10;
  30. var frame = new View { X = 0, Y = 0, Width = 50, Height = 50 };
  31. frame.Add (view);
  32. frame.BeginInit ();
  33. frame.EndInit ();
  34. frame.LayoutSubviews ();
  35. // What's the natural width/height?
  36. int expectedX = (frame.Frame.Width - view.Frame.Width) / 2;
  37. int expectedY = (frame.Frame.Height - view.Frame.Height) / 2;
  38. Assert.True (
  39. view.Frame.Left == expectedX,
  40. $"{view} did not center horizontally. Expected: {expectedX}. Actual: {view.Frame.Left}"
  41. );
  42. Assert.True (
  43. view.Frame.Top == expectedY,
  44. $"{view} did not center vertically. Expected: {expectedY}. Actual: {view.Frame.Top}"
  45. );
  46. Application.Shutdown ();
  47. }
  48. [Theory]
  49. [MemberData (nameof (AllViewTypes))]
  50. public void AllViews_Tests_All_Constructors (Type viewType)
  51. {
  52. Assert.True (Test_All_Constructors_Of_Type (viewType));
  53. }
  54. public bool Test_All_Constructors_Of_Type (Type type)
  55. {
  56. foreach (ConstructorInfo ctor in type.GetConstructors ())
  57. {
  58. View view = TestHelpers.CreateViewFromType (type, ctor);
  59. if (view != null)
  60. {
  61. Assert.True (type.FullName == view.GetType ().FullName);
  62. }
  63. }
  64. return true;
  65. }
  66. //[Fact]
  67. //public void AllViews_HotKey_Works ()
  68. //{
  69. // foreach (var type in GetAllViewClasses ()) {
  70. // _output.WriteLine ($"Testing {type.Name}");
  71. // var view = GetTypeInitializer (type, type.GetConstructor (Array.Empty<Type> ()));
  72. // view.HotKeySpecifier = (Rune)'^';
  73. // view.Text = "^text";
  74. // Assert.Equal(Key.T, view.HotKey);
  75. // }
  76. //}
  77. [Theory]
  78. [MemberData (nameof (AllViewTypes))]
  79. public void AllViews_Command_Select_Raises_Selecting (Type viewType)
  80. {
  81. var view = (View)CreateInstanceIfNotGeneric (viewType);
  82. if (view == null)
  83. {
  84. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  85. return;
  86. }
  87. if (view is IDesignable designable)
  88. {
  89. designable.EnableForDesign ();
  90. }
  91. var selectingCount = 0;
  92. view.Selecting += (s, e) => selectingCount++;
  93. var acceptedCount = 0;
  94. view.Accepting += (s, e) =>
  95. {
  96. acceptedCount++;
  97. };
  98. if (view.InvokeCommand(Command.Select) == true)
  99. {
  100. Assert.Equal(1, selectingCount);
  101. Assert.Equal (0, acceptedCount);
  102. }
  103. }
  104. [Theory]
  105. [MemberData (nameof (AllViewTypes))]
  106. public void AllViews_Command_Accept_Raises_Accepted (Type viewType)
  107. {
  108. var view = (View)CreateInstanceIfNotGeneric (viewType);
  109. if (view == null)
  110. {
  111. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  112. return;
  113. }
  114. if (view is IDesignable designable)
  115. {
  116. designable.EnableForDesign ();
  117. }
  118. var selectingCount = 0;
  119. view.Selecting += (s, e) => selectingCount++;
  120. var acceptedCount = 0;
  121. view.Accepting += (s, e) =>
  122. {
  123. acceptedCount++;
  124. };
  125. if (view.InvokeCommand (Command.Accept) == true)
  126. {
  127. Assert.Equal (0, selectingCount);
  128. Assert.Equal (1, acceptedCount);
  129. }
  130. }
  131. [Theory]
  132. [MemberData (nameof (AllViewTypes))]
  133. public void AllViews_Command_HotKey_Raises_HandlingHotKey (Type viewType)
  134. {
  135. var view = (View)CreateInstanceIfNotGeneric (viewType);
  136. if (view == null)
  137. {
  138. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  139. return;
  140. }
  141. if (view is IDesignable designable)
  142. {
  143. designable.EnableForDesign ();
  144. }
  145. else
  146. {
  147. view.HotKey = Key.T;
  148. }
  149. var selectingCount = 0;
  150. view.Selecting += (s, e) => selectingCount++;
  151. var acceptedCount = 0;
  152. view.Accepting += (s, e) =>
  153. {
  154. acceptedCount++;
  155. };
  156. var hotkeyHandledCount = 0;
  157. view.HandlingHotKey += (s, e) =>
  158. {
  159. hotkeyHandledCount++;
  160. };
  161. if (view.InvokeCommand (Command.HotKey) == true)
  162. {
  163. Assert.Equal (1, hotkeyHandledCount);
  164. Assert.Equal (0, selectingCount);
  165. Assert.Equal (0, acceptedCount);
  166. }
  167. }
  168. }