AllViewsTests.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 if (vType is DatePicker) {
  100. for (int i = 0; i < 4; i++) {
  101. top.NewKeyDownEvent (new (KeyCode.Tab | KeyCode.CtrlMask));
  102. }
  103. } else {
  104. top.NewKeyDownEvent (new (KeyCode.Tab));
  105. }
  106. top.NewKeyDownEvent (new (KeyCode.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_HotKey_Works ()
  116. //{
  117. // foreach (var type in GetAllViewClasses ()) {
  118. // _output.WriteLine ($"Testing {type.Name}");
  119. // var view = GetTypeInitializer (type, type.GetConstructor (Array.Empty<Type> ()));
  120. // view.HotKeySpecifier = (Rune)'^';
  121. // view.Text = "^text";
  122. // Assert.Equal(Key.T, view.HotKey);
  123. // }
  124. //}
  125. public bool Test_All_Constructors_Of_Type (Type type)
  126. {
  127. foreach (var ctor in type.GetConstructors ()) {
  128. var view = CreateViewFromType (type, ctor);
  129. if (view != null) {
  130. Assert.True (type.FullName == view.GetType ().FullName);
  131. }
  132. }
  133. return true;
  134. }
  135. private static View CreateViewFromType (Type type, ConstructorInfo ctor)
  136. {
  137. View viewType = null;
  138. if (type.IsGenericType && type.IsTypeDefinition) {
  139. List<Type> gTypes = new List<Type> ();
  140. foreach (var args in type.GetGenericArguments ()) {
  141. gTypes.Add (typeof (object));
  142. }
  143. type = type.MakeGenericType (gTypes.ToArray ());
  144. Assert.IsType (type, (View)Activator.CreateInstance (type));
  145. } else {
  146. ParameterInfo [] paramsInfo = ctor.GetParameters ();
  147. Type paramType;
  148. List<object> pTypes = new List<object> ();
  149. if (type.IsGenericType) {
  150. foreach (var args in type.GetGenericArguments ()) {
  151. paramType = args.GetType ();
  152. if (args.Name == "T") {
  153. pTypes.Add (typeof (object));
  154. } else {
  155. AddArguments (paramType, pTypes);
  156. }
  157. }
  158. }
  159. foreach (var p in paramsInfo) {
  160. paramType = p.ParameterType;
  161. if (p.HasDefaultValue) {
  162. pTypes.Add (p.DefaultValue);
  163. } else {
  164. AddArguments (paramType, pTypes);
  165. }
  166. }
  167. if (type.IsGenericType && !type.IsTypeDefinition) {
  168. viewType = (View)Activator.CreateInstance (type);
  169. Assert.IsType (type, viewType);
  170. } else {
  171. viewType = (View)ctor.Invoke (pTypes.ToArray ());
  172. Assert.IsType (type, viewType);
  173. }
  174. }
  175. return viewType;
  176. }
  177. // BUGBUG: This is a hack. We should figure out how to dynamically
  178. // create the right type of argument for the constructor.
  179. private static void AddArguments (Type paramType, List<object> pTypes)
  180. {
  181. if (paramType == typeof (Rect)) {
  182. pTypes.Add (Rect.Empty);
  183. } else if (paramType == typeof (string)) {
  184. pTypes.Add (string.Empty);
  185. } else if (paramType == typeof (int)) {
  186. pTypes.Add (0);
  187. } else if (paramType == typeof (bool)) {
  188. pTypes.Add (true);
  189. } else if (paramType.Name == "IList") {
  190. pTypes.Add (new List<object> ());
  191. } else if (paramType.Name == "View") {
  192. var top = new Toplevel ();
  193. var view = new View ();
  194. top.Add (view);
  195. pTypes.Add (view);
  196. } else if (paramType.Name == "View[]") {
  197. pTypes.Add (new View [] { });
  198. } else if (paramType.Name == "Stream") {
  199. pTypes.Add (new MemoryStream ());
  200. } else if (paramType.Name == "String") {
  201. pTypes.Add (string.Empty);
  202. } else if (paramType.Name == "TreeView`1[T]") {
  203. pTypes.Add (string.Empty);
  204. } else {
  205. pTypes.Add (null);
  206. }
  207. }
  208. public static List<Type> GetAllViewClasses ()
  209. {
  210. return typeof (View).Assembly.GetTypes ()
  211. .Where (myType => myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View)))
  212. .ToList ();
  213. }
  214. }