AllViewsTests.cs 4.6 KB

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