AllViewsTests.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #nullable enable
  2. using System.Reflection;
  3. using UnitTests;
  4. using Xunit.Abstractions;
  5. namespace ViewsTests;
  6. public class AllViewsTests (ITestOutputHelper output) : TestsAllViews
  7. {
  8. [Theory]
  9. [MemberData (nameof (AllViewTypes))]
  10. public void AllViews_Layout_Does_Not_Draw (Type viewType)
  11. {
  12. IDriver driver = CreateFakeDriver ();
  13. View? view = CreateInstanceIfNotGeneric (viewType);
  14. if (view is null)
  15. {
  16. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  17. return;
  18. }
  19. if (view is IDesignable designable)
  20. {
  21. designable.EnableForDesign ();
  22. }
  23. var drawContentCount = 0;
  24. view.DrawingContent += (s, e) => drawContentCount++;
  25. var layoutStartedCount = 0;
  26. view.SubViewLayout += (s, e) => layoutStartedCount++;
  27. var layoutCompleteCount = 0;
  28. view.SubViewsLaidOut += (s, e) => layoutCompleteCount++;
  29. view.SetNeedsLayout ();
  30. view.SetNeedsDraw ();
  31. view.Layout ();
  32. Assert.Equal (0, drawContentCount);
  33. Assert.Equal (1, layoutStartedCount);
  34. Assert.Equal (1, layoutCompleteCount);
  35. }
  36. [Theory]
  37. [MemberData (nameof (AllViewTypes))]
  38. public void AllViews_Center_Properly (Type viewType)
  39. {
  40. IDriver driver = CreateFakeDriver ();
  41. View? view = CreateInstanceIfNotGeneric (viewType);
  42. if (view is null)
  43. {
  44. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  45. return;
  46. }
  47. if (view is IDesignable designable)
  48. {
  49. designable.EnableForDesign ();
  50. }
  51. view.X = Pos.Center ();
  52. view.Y = Pos.Center ();
  53. // Ensure the view has positive dimensions
  54. view.Width = 10;
  55. view.Height = 10;
  56. var frame = new View { X = 0, Y = 0, Width = 50, Height = 50 };
  57. frame.Add (view);
  58. frame.LayoutSubViews ();
  59. frame.Dispose ();
  60. // What's the natural width/height?
  61. int expectedX = (frame.Frame.Width - view.Frame.Width) / 2;
  62. int expectedY = (frame.Frame.Height - view.Frame.Height) / 2;
  63. Assert.True (
  64. view.Frame.Left == expectedX,
  65. $"{view} did not center horizontally. Expected: {expectedX}. Actual: {view.Frame.Left}"
  66. );
  67. Assert.True (
  68. view.Frame.Top == expectedY,
  69. $"{view} did not center vertically. Expected: {expectedY}. Actual: {view.Frame.Top}"
  70. );
  71. }
  72. [Theory]
  73. [MemberData (nameof (AllViewTypes))]
  74. public void AllViews_Tests_All_Constructors (Type viewType)
  75. {
  76. Assert.True (TestAllConstructorsOfType (viewType));
  77. return;
  78. bool TestAllConstructorsOfType (Type type)
  79. {
  80. foreach (ConstructorInfo ctor in type.GetConstructors ())
  81. {
  82. View? view = CreateViewFromType (type, ctor);
  83. if (view != null)
  84. {
  85. Assert.True (type.FullName == view.GetType ().FullName);
  86. }
  87. view?.Dispose ();
  88. }
  89. return true;
  90. }
  91. }
  92. //[Fact]
  93. //public void AllViews_HotKey_Works ()
  94. //{
  95. // foreach (var type in GetAllViewClasses ()) {
  96. // _output.WriteLine ($"Testing {type.Name}");
  97. // var view = GetTypeInitializer (type, type.GetConstructor (Array.Empty<Type> ()));
  98. // view.HotKeySpecifier = (Rune)'^';
  99. // view.Text = "^text";
  100. // Assert.Equal(Key.T, view.HotKey);
  101. // }
  102. //}
  103. [Theory]
  104. [MemberData (nameof (AllViewTypes))]
  105. public void AllViews_Command_Select_Raises_Selecting (Type viewType)
  106. {
  107. var view = CreateInstanceIfNotGeneric (viewType);
  108. if (view == null)
  109. {
  110. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  111. return;
  112. }
  113. if (view is IDesignable designable)
  114. {
  115. designable.EnableForDesign ();
  116. }
  117. var activatingCount = 0;
  118. view.Activating += (s, e) => activatingCount++;
  119. var acceptedCount = 0;
  120. view.Accepting += (s, e) => { acceptedCount++; };
  121. if (view.InvokeCommand (Command.Activate) == true)
  122. {
  123. Assert.Equal (1, activatingCount);
  124. Assert.Equal (0, acceptedCount);
  125. }
  126. view?.Dispose ();
  127. }
  128. [Theory]
  129. [MemberData (nameof (AllViewTypes))]
  130. public void AllViews_Command_Accept_Raises_Accepting (Type viewType)
  131. {
  132. var view = CreateInstanceIfNotGeneric (viewType);
  133. if (view == null)
  134. {
  135. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  136. return;
  137. }
  138. if (view is IDesignable designable)
  139. {
  140. designable.EnableForDesign ();
  141. }
  142. var activatingCount = 0;
  143. view.Activating += (s, e) => activatingCount++;
  144. var acceptingCount = 0;
  145. view.Accepting += (s, e) => { acceptingCount++; };
  146. if (view.InvokeCommand (Command.Accept) == true)
  147. {
  148. Assert.Equal (0, activatingCount);
  149. Assert.Equal (1, acceptingCount);
  150. }
  151. view?.Dispose ();
  152. }
  153. [Theory]
  154. [MemberData (nameof (AllViewTypes))]
  155. public void AllViews_Command_HotKey_Raises_HandlingHotKey (Type viewType)
  156. {
  157. var view = CreateInstanceIfNotGeneric (viewType);
  158. if (view == null)
  159. {
  160. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  161. return;
  162. }
  163. if (view is IDesignable designable)
  164. {
  165. designable.EnableForDesign ();
  166. }
  167. else
  168. {
  169. view.HotKey = Key.T;
  170. }
  171. var acceptedCount = 0;
  172. view.Accepting += (s, e) => { acceptedCount++; };
  173. var handlingHotKeyCount = 0;
  174. view.HandlingHotKey += (s, e) => { handlingHotKeyCount++; };
  175. if (view.InvokeCommand (Command.HotKey) == true)
  176. {
  177. Assert.Equal (1, handlingHotKeyCount);
  178. Assert.Equal (0, acceptedCount);
  179. }
  180. view?.Dispose ();
  181. }
  182. //[Theory]
  183. //[MemberData (nameof (AllViewTypes))]
  184. //public void AllViews_Disabled_Draws_Disabled_Or_Faint (Type viewType)
  185. //{
  186. // var view = CreateInstanceIfNotGeneric (viewType);
  187. // if (view == null)
  188. // {
  189. // output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  190. // return;
  191. // }
  192. // if (view is IDesignable designable)
  193. // {
  194. // designable.EnableForDesign ();
  195. // }
  196. // var driver = CreateFakeDriver ();
  197. // driver.AttributeSet += (_, args) =>
  198. // {
  199. // if (args != view.GetAttributeForRole (VisualRole.Disabled) && args.Style != TextStyle.Faint)
  200. // {
  201. // Assert.Fail($"{viewType} with `Enabled == false` tried to SetAttribute to {args}");
  202. // }
  203. // };
  204. // view.Driver = driver;
  205. // view.Enabled = false;
  206. // view.SetNeedsDraw ();
  207. // view.Draw ();
  208. // view?.Dispose ();
  209. //}
  210. }