AllViewsTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Xunit;
  6. using System.IO;
  7. using System.Text;
  8. using Microsoft.VisualStudio.TestPlatform.Utilities;
  9. using Xunit.Abstractions;
  10. namespace Terminal.Gui.ViewsTests;
  11. public class AllViewsTests {
  12. readonly ITestOutputHelper _output;
  13. public AllViewsTests (ITestOutputHelper output)
  14. {
  15. this._output = output;
  16. }
  17. [Fact]
  18. public void AllViews_Tests_All_Constructors ()
  19. {
  20. Application.Init (new FakeDriver ());
  21. foreach (var type in GetAllViewClasses ()) {
  22. Assert.True (Constructors_FullTest (type));
  23. }
  24. Application.Shutdown ();
  25. }
  26. [Fact]
  27. public void AllViews_Enter_Leave_Events ()
  28. {
  29. foreach (var type in GetAllViewClasses ()) {
  30. _output.WriteLine ($"Testing {type.Name}");
  31. Application.Init (new FakeDriver ());
  32. var top = Application.Top;
  33. var vType = GetTypeInitializer (type, type.GetConstructor (Array.Empty<Type> ()));
  34. if (vType == null) {
  35. Application.Shutdown ();
  36. continue;
  37. }
  38. vType.X = 0;
  39. vType.Y = 0;
  40. vType.Width = 10;
  41. vType.Height = 1;
  42. var view = new View () {
  43. X = 0,
  44. Y = 1,
  45. Width = 10,
  46. Height = 1,
  47. CanFocus = true
  48. };
  49. var vTypeEnter = 0;
  50. var vTypeLeave = 0;
  51. var viewEnter = 0;
  52. var viewLeave = 0;
  53. vType.Enter += (s, e) => vTypeEnter++;
  54. vType.Leave += (s, e) => vTypeLeave++;
  55. view.Enter += (s, e) => viewEnter++;
  56. view.Leave += (s, e) => viewLeave++;
  57. top.Add (vType, view);
  58. Application.Begin (top);
  59. if (!vType.CanFocus || (vType is Toplevel && ((Toplevel)vType).Modal)) {
  60. Application.Shutdown ();
  61. continue;
  62. }
  63. if (vType is TextView) {
  64. top.NewKeyDownEvent (new (KeyCode.Tab | KeyCode.CtrlMask));
  65. } else {
  66. top.NewKeyDownEvent (new (KeyCode.Tab));
  67. }
  68. top.NewKeyDownEvent (new (KeyCode.Tab));
  69. Assert.Equal (2, vTypeEnter);
  70. Assert.Equal (1, vTypeLeave);
  71. Assert.Equal (1, viewEnter);
  72. Assert.Equal (1, viewLeave);
  73. Application.Shutdown ();
  74. }
  75. }
  76. //[Fact]
  77. //public void AllViews_HotKey_Works ()
  78. //{
  79. // foreach (var type in GetAllViewClasses ()) {
  80. // _output.WriteLine ($"Testing {type.Name}");
  81. // var view = GetTypeInitializer (type, type.GetConstructor (Array.Empty<Type> ()));
  82. // view.HotKeySpecifier = (Rune)'^';
  83. // view.Text = "^text";
  84. // Assert.Equal(Key.T, view.HotKey);
  85. // }
  86. //}
  87. public bool Constructors_FullTest (Type type)
  88. {
  89. foreach (var ctor in type.GetConstructors ()) {
  90. var view = GetTypeInitializer (type, ctor);
  91. if (view != null) {
  92. Assert.True (type.FullName == view.GetType ().FullName);
  93. }
  94. }
  95. return true;
  96. }
  97. private static View GetTypeInitializer (Type type, ConstructorInfo ctor)
  98. {
  99. View viewType = null;
  100. if (type.IsGenericType && type.IsTypeDefinition) {
  101. List<Type> gTypes = new List<Type> ();
  102. foreach (var args in type.GetGenericArguments ()) {
  103. gTypes.Add (typeof (object));
  104. }
  105. type = type.MakeGenericType (gTypes.ToArray ());
  106. Assert.IsType (type, (View)Activator.CreateInstance (type));
  107. } else {
  108. ParameterInfo [] paramsInfo = ctor.GetParameters ();
  109. Type paramType;
  110. List<object> pTypes = new List<object> ();
  111. if (type.IsGenericType) {
  112. foreach (var args in type.GetGenericArguments ()) {
  113. paramType = args.GetType ();
  114. if (args.Name == "T") {
  115. pTypes.Add (typeof (object));
  116. } else {
  117. AddArguments (paramType, pTypes);
  118. }
  119. }
  120. }
  121. foreach (var p in paramsInfo) {
  122. paramType = p.ParameterType;
  123. if (p.HasDefaultValue) {
  124. pTypes.Add (p.DefaultValue);
  125. } else {
  126. AddArguments (paramType, pTypes);
  127. }
  128. }
  129. if (type.IsGenericType && !type.IsTypeDefinition) {
  130. viewType = (View)Activator.CreateInstance (type);
  131. Assert.IsType (type, viewType);
  132. } else {
  133. viewType = (View)ctor.Invoke (pTypes.ToArray ());
  134. Assert.IsType (type, viewType);
  135. }
  136. }
  137. return viewType;
  138. }
  139. private static void AddArguments (Type paramType, List<object> pTypes)
  140. {
  141. if (paramType == typeof (Rect)) {
  142. pTypes.Add (Rect.Empty);
  143. } else if (paramType == typeof (string)) {
  144. pTypes.Add (string.Empty);
  145. } else if (paramType == typeof (int)) {
  146. pTypes.Add (0);
  147. } else if (paramType == typeof (bool)) {
  148. pTypes.Add (true);
  149. } else if (paramType.Name == "IList") {
  150. pTypes.Add (new List<object> ());
  151. } else if (paramType.Name == "View") {
  152. var top = new Toplevel ();
  153. var view = new View ();
  154. top.Add (view);
  155. pTypes.Add (view);
  156. } else if (paramType.Name == "View[]") {
  157. pTypes.Add (new View [] { });
  158. } else if (paramType.Name == "Stream") {
  159. pTypes.Add (new MemoryStream ());
  160. } else if (paramType.Name == "String") {
  161. pTypes.Add (string.Empty);
  162. } else if (paramType.Name == "TreeView`1[T]") {
  163. pTypes.Add (string.Empty);
  164. } else {
  165. pTypes.Add (null);
  166. }
  167. }
  168. public static List<Type> GetAllViewClasses ()
  169. {
  170. return typeof (View).Assembly.GetTypes ()
  171. .Where (myType => myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View)))
  172. .ToList ();
  173. }
  174. }