AllViewsTests.cs 8.8 KB

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