AllViewsTests.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 GetAllViewClasses ())
  48. {
  49. output.WriteLine ($"Testing {type.Name}");
  50. Application.Init (new FakeDriver ());
  51. Toplevel top = Application.Top;
  52. View vType = CreateViewFromType (type, type.GetConstructor (Array.Empty<Type> ()));
  53. if (vType == null)
  54. {
  55. output.WriteLine ($"Ignoring {type} - It's a Generic");
  56. Application.Shutdown ();
  57. continue;
  58. }
  59. vType.AutoSize = false;
  60. vType.X = 0;
  61. vType.Y = 0;
  62. vType.Width = 10;
  63. vType.Height = 1;
  64. var view = new View
  65. {
  66. X = 0,
  67. Y = 1,
  68. Width = 10,
  69. Height = 1,
  70. CanFocus = true
  71. };
  72. var vTypeEnter = 0;
  73. var vTypeLeave = 0;
  74. var viewEnter = 0;
  75. var viewLeave = 0;
  76. vType.Enter += (s, e) => vTypeEnter++;
  77. vType.Leave += (s, e) => vTypeLeave++;
  78. view.Enter += (s, e) => viewEnter++;
  79. view.Leave += (s, e) => viewLeave++;
  80. top.Add (vType, view);
  81. Application.Begin (top);
  82. if (!vType.CanFocus || (vType is Toplevel && ((Toplevel)vType).Modal))
  83. {
  84. Application.Shutdown ();
  85. continue;
  86. }
  87. if (vType is TextView)
  88. {
  89. top.NewKeyDownEvent (Key.Tab.WithCtrl);
  90. }
  91. else if (vType is DatePicker)
  92. {
  93. for (var i = 0; i < 4; i++)
  94. {
  95. top.NewKeyDownEvent (Key.Tab.WithCtrl);
  96. }
  97. }
  98. else
  99. {
  100. top.NewKeyDownEvent (Key.Tab);
  101. }
  102. top.NewKeyDownEvent (Key.Tab);
  103. Assert.Equal (2, vTypeEnter);
  104. Assert.Equal (1, vTypeLeave);
  105. Assert.Equal (1, viewEnter);
  106. Assert.Equal (1, viewLeave);
  107. Application.Shutdown ();
  108. }
  109. }
  110. [Fact]
  111. public void AllViews_Tests_All_Constructors ()
  112. {
  113. Application.Init (new FakeDriver ());
  114. foreach (Type type in GetAllViewClasses ())
  115. {
  116. Assert.True (Test_All_Constructors_Of_Type (type));
  117. }
  118. Application.Shutdown ();
  119. }
  120. public static List<Type> GetAllViewClasses ()
  121. {
  122. return typeof (View).Assembly.GetTypes ()
  123. .Where (
  124. myType => myType.IsClass
  125. && !myType.IsAbstract
  126. && myType.IsPublic
  127. && myType.IsSubclassOf (typeof (View))
  128. )
  129. .ToList ();
  130. }
  131. //[Fact]
  132. //public void AllViews_HotKey_Works ()
  133. //{
  134. // foreach (var type in GetAllViewClasses ()) {
  135. // _output.WriteLine ($"Testing {type.Name}");
  136. // var view = GetTypeInitializer (type, type.GetConstructor (Array.Empty<Type> ()));
  137. // view.HotKeySpecifier = (Rune)'^';
  138. // view.Text = "^text";
  139. // Assert.Equal(Key.T, view.HotKey);
  140. // }
  141. //}
  142. public bool Test_All_Constructors_Of_Type (Type type)
  143. {
  144. foreach (ConstructorInfo ctor in type.GetConstructors ())
  145. {
  146. View view = CreateViewFromType (type, ctor);
  147. if (view != null)
  148. {
  149. Assert.True (type.FullName == view.GetType ().FullName);
  150. }
  151. }
  152. return true;
  153. }
  154. // BUGBUG: This is a hack. We should figure out how to dynamically
  155. // create the right type of argument for the constructor.
  156. private static void AddArguments (Type paramType, List<object> pTypes)
  157. {
  158. if (paramType == typeof (Rectangle))
  159. {
  160. pTypes.Add (Rectangle.Empty);
  161. }
  162. else if (paramType == typeof (string))
  163. {
  164. pTypes.Add (string.Empty);
  165. }
  166. else if (paramType == typeof (int))
  167. {
  168. pTypes.Add (0);
  169. }
  170. else if (paramType == typeof (bool))
  171. {
  172. pTypes.Add (true);
  173. }
  174. else if (paramType.Name == "IList")
  175. {
  176. pTypes.Add (new List<object> ());
  177. }
  178. else if (paramType.Name == "View")
  179. {
  180. var top = new Toplevel ();
  181. var view = new View ();
  182. top.Add (view);
  183. pTypes.Add (view);
  184. }
  185. else if (paramType.Name == "View[]")
  186. {
  187. pTypes.Add (new View [] { });
  188. }
  189. else if (paramType.Name == "Stream")
  190. {
  191. pTypes.Add (new MemoryStream ());
  192. }
  193. else if (paramType.Name == "String")
  194. {
  195. pTypes.Add (string.Empty);
  196. }
  197. else if (paramType.Name == "TreeView`1[T]")
  198. {
  199. pTypes.Add (string.Empty);
  200. }
  201. else
  202. {
  203. pTypes.Add (null);
  204. }
  205. }
  206. private static View CreateViewFromType (Type type, ConstructorInfo ctor)
  207. {
  208. View viewType = null;
  209. if (type.IsGenericType && type.IsTypeDefinition)
  210. {
  211. List<Type> gTypes = new ();
  212. foreach (Type args in type.GetGenericArguments ())
  213. {
  214. gTypes.Add (typeof (object));
  215. }
  216. type = type.MakeGenericType (gTypes.ToArray ());
  217. Assert.IsType (type, (View)Activator.CreateInstance (type));
  218. }
  219. else
  220. {
  221. ParameterInfo [] paramsInfo = ctor.GetParameters ();
  222. Type paramType;
  223. List<object> pTypes = new ();
  224. if (type.IsGenericType)
  225. {
  226. foreach (Type args in type.GetGenericArguments ())
  227. {
  228. paramType = args.GetType ();
  229. if (args.Name == "T")
  230. {
  231. pTypes.Add (typeof (object));
  232. }
  233. else
  234. {
  235. AddArguments (paramType, pTypes);
  236. }
  237. }
  238. }
  239. foreach (ParameterInfo p in paramsInfo)
  240. {
  241. paramType = p.ParameterType;
  242. if (p.HasDefaultValue)
  243. {
  244. pTypes.Add (p.DefaultValue);
  245. }
  246. else
  247. {
  248. AddArguments (paramType, pTypes);
  249. }
  250. }
  251. if (type.IsGenericType && !type.IsTypeDefinition)
  252. {
  253. viewType = (View)Activator.CreateInstance (type);
  254. Assert.IsType (type, viewType);
  255. }
  256. else
  257. {
  258. viewType = (View)ctor.Invoke (pTypes.ToArray ());
  259. Assert.IsType (type, viewType);
  260. }
  261. }
  262. return viewType;
  263. }
  264. }