AllViewsTests.cs 6.5 KB

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