AllViewsTests.cs 6.2 KB

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