AllViewsTests.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System.Reflection;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewsTests;
  4. public class AllViewsTests (ITestOutputHelper output)
  5. {
  6. // TODO: Update all these tests to use AllViews like AllViews_Center_Properly does
  7. public static TheoryData<View, string> AllViews => TestHelpers.GetAllViewsTheoryData ();
  8. [Theory]
  9. [MemberData (nameof (AllViews))]
  10. public void AllViews_Center_Properly (View view, string viewName)
  11. {
  12. // See https://github.com/gui-cs/Terminal.Gui/issues/3156
  13. if (view == null)
  14. {
  15. output.WriteLine ($"Ignoring {viewName} - It's a Generic");
  16. Application.Shutdown ();
  17. return;
  18. }
  19. view.X = Pos.Center ();
  20. view.Y = Pos.Center ();
  21. // Turn off AutoSize
  22. view.AutoSize = false;
  23. // Ensure the view has positive dimensions
  24. view.Width = 10;
  25. view.Height = 10;
  26. var frame = new View { X = 0, Y = 0, Width = 50, Height = 50 };
  27. frame.Add (view);
  28. frame.BeginInit ();
  29. frame.EndInit ();
  30. frame.LayoutSubviews ();
  31. // What's the natural width/height?
  32. int expectedX = (frame.Frame.Width - view.Frame.Width) / 2;
  33. int expectedY = (frame.Frame.Height - view.Frame.Height) / 2;
  34. Assert.True (
  35. view.Frame.Left == expectedX,
  36. $"{view} did not center horizontally. Expected: {expectedX}. Actual: {view.Frame.Left}"
  37. );
  38. Assert.True (
  39. view.Frame.Top == expectedY,
  40. $"{view} did not center vertically. Expected: {expectedY}. Actual: {view.Frame.Top}"
  41. );
  42. Application.Shutdown ();
  43. }
  44. [Fact]
  45. public void AllViews_Enter_Leave_Events ()
  46. {
  47. foreach (Type type in TestHelpers.GetAllViewClasses ())
  48. {
  49. output.WriteLine ($"Testing {type.Name}");
  50. Application.Init (new FakeDriver ());
  51. Toplevel top = new ();
  52. View vType = TestHelpers.CreateViewFromType (type, type.GetConstructor (Array.Empty<Type> ()));
  53. if (vType == null)
  54. {
  55. output.WriteLine ($"Ignoring {type} - It's a Generic");
  56. top.Dispose ();
  57. Application.Shutdown ();
  58. continue;
  59. }
  60. vType.AutoSize = false;
  61. vType.X = 0;
  62. vType.Y = 0;
  63. vType.Width = 10;
  64. vType.Height = 1;
  65. var view = new View
  66. {
  67. X = 0,
  68. Y = 1,
  69. Width = 10,
  70. Height = 1,
  71. CanFocus = true
  72. };
  73. var vTypeEnter = 0;
  74. var vTypeLeave = 0;
  75. var viewEnter = 0;
  76. var viewLeave = 0;
  77. vType.Enter += (s, e) => vTypeEnter++;
  78. vType.Leave += (s, e) => vTypeLeave++;
  79. view.Enter += (s, e) => viewEnter++;
  80. view.Leave += (s, e) => viewLeave++;
  81. top.Add (vType, view);
  82. Application.Begin (top);
  83. if (!vType.CanFocus || (vType is Toplevel && ((Toplevel)vType).Modal))
  84. {
  85. top.Dispose ();
  86. Application.Shutdown ();
  87. continue;
  88. }
  89. if (vType is TextView)
  90. {
  91. top.NewKeyDownEvent (Key.Tab.WithCtrl);
  92. }
  93. else if (vType is DatePicker)
  94. {
  95. for (var i = 0; i < 4; i++)
  96. {
  97. top.NewKeyDownEvent (Key.Tab.WithCtrl);
  98. }
  99. }
  100. else
  101. {
  102. top.NewKeyDownEvent (Key.Tab);
  103. }
  104. top.NewKeyDownEvent (Key.Tab);
  105. Assert.Equal (2, vTypeEnter);
  106. Assert.Equal (1, vTypeLeave);
  107. Assert.Equal (1, viewEnter);
  108. Assert.Equal (1, viewLeave);
  109. top.Dispose ();
  110. Application.Shutdown ();
  111. }
  112. }
  113. [Fact]
  114. public void AllViews_Tests_All_Constructors ()
  115. {
  116. Application.Init (new FakeDriver ());
  117. foreach (Type type in TestHelpers.GetAllViewClasses ())
  118. {
  119. Assert.True (Test_All_Constructors_Of_Type (type));
  120. }
  121. Application.Shutdown ();
  122. }
  123. //[Fact]
  124. //public void AllViews_HotKey_Works ()
  125. //{
  126. // foreach (var type in GetAllViewClasses ()) {
  127. // _output.WriteLine ($"Testing {type.Name}");
  128. // var view = GetTypeInitializer (type, type.GetConstructor (Array.Empty<Type> ()));
  129. // view.HotKeySpecifier = (Rune)'^';
  130. // view.Text = "^text";
  131. // Assert.Equal(Key.T, view.HotKey);
  132. // }
  133. //}
  134. public bool Test_All_Constructors_Of_Type (Type type)
  135. {
  136. foreach (ConstructorInfo ctor in type.GetConstructors ())
  137. {
  138. View view = TestHelpers.CreateViewFromType (type, ctor);
  139. if (view != null)
  140. {
  141. Assert.True (type.FullName == view.GetType ().FullName);
  142. }
  143. }
  144. return true;
  145. }
  146. // BUGBUG: This is a hack. We should figure out how to dynamically
  147. // create the right type of argument for the constructor.
  148. private static void AddArguments (Type paramType, List<object> pTypes)
  149. {
  150. if (paramType == typeof (Rectangle))
  151. {
  152. pTypes.Add (Rectangle.Empty);
  153. }
  154. else if (paramType == typeof (string))
  155. {
  156. pTypes.Add (string.Empty);
  157. }
  158. else if (paramType == typeof (int))
  159. {
  160. pTypes.Add (0);
  161. }
  162. else if (paramType == typeof (bool))
  163. {
  164. pTypes.Add (true);
  165. }
  166. else if (paramType.Name == "IList")
  167. {
  168. pTypes.Add (new List<object> ());
  169. }
  170. else if (paramType.Name == "View")
  171. {
  172. var top = new Toplevel ();
  173. var view = new View ();
  174. top.Add (view);
  175. pTypes.Add (view);
  176. }
  177. else if (paramType.Name == "View[]")
  178. {
  179. pTypes.Add (new View [] { });
  180. }
  181. else if (paramType.Name == "Stream")
  182. {
  183. pTypes.Add (new MemoryStream ());
  184. }
  185. else if (paramType.Name == "String")
  186. {
  187. pTypes.Add (string.Empty);
  188. }
  189. else if (paramType.Name == "TreeView`1[T]")
  190. {
  191. pTypes.Add (string.Empty);
  192. }
  193. else
  194. {
  195. pTypes.Add (null);
  196. }
  197. }
  198. }