AllViewsTests.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System.Reflection;
  2. using Microsoft.VisualStudio.TestPlatform.Utilities;
  3. using UnitTests;
  4. using Xunit.Abstractions;
  5. namespace UnitTests_Parallelizable.ViewsTests;
  6. [Collection ("Global Test Setup")]
  7. public class AllViewsTests (ITestOutputHelper output) : TestsAllViews
  8. {
  9. [Theory]
  10. [MemberData (nameof (AllViewTypes))]
  11. public void AllViews_Tests_All_Constructors (Type viewType)
  12. {
  13. Assert.True (TestAllConstructorsOfType (viewType));
  14. return;
  15. bool TestAllConstructorsOfType (Type type)
  16. {
  17. foreach (ConstructorInfo ctor in type.GetConstructors ())
  18. {
  19. View view = CreateViewFromType (type, ctor);
  20. if (view != null)
  21. {
  22. Assert.True (type.FullName == view.GetType ().FullName);
  23. }
  24. view?.Dispose ();
  25. }
  26. return true;
  27. }
  28. }
  29. //[Fact]
  30. //public void AllViews_HotKey_Works ()
  31. //{
  32. // foreach (var type in GetAllViewClasses ()) {
  33. // _output.WriteLine ($"Testing {type.Name}");
  34. // var view = GetTypeInitializer (type, type.GetConstructor (Array.Empty<Type> ()));
  35. // view.HotKeySpecifier = (Rune)'^';
  36. // view.Text = "^text";
  37. // Assert.Equal(Key.T, view.HotKey);
  38. // }
  39. //}
  40. [Theory]
  41. [MemberData (nameof (AllViewTypes))]
  42. public void AllViews_Command_Select_Raises_Selecting (Type viewType)
  43. {
  44. var view = CreateInstanceIfNotGeneric (viewType);
  45. if (view == null)
  46. {
  47. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  48. return;
  49. }
  50. if (view is IDesignable designable)
  51. {
  52. designable.EnableForDesign ();
  53. }
  54. var selectingCount = 0;
  55. view.Selecting += (s, e) => selectingCount++;
  56. var acceptedCount = 0;
  57. view.Accepting += (s, e) => { acceptedCount++; };
  58. if (view.InvokeCommand (Command.Select) == true)
  59. {
  60. Assert.Equal (1, selectingCount);
  61. Assert.Equal (0, acceptedCount);
  62. }
  63. view?.Dispose ();
  64. }
  65. [Theory]
  66. [MemberData (nameof (AllViewTypes))]
  67. public void AllViews_Command_Accept_Raises_Accepting (Type viewType)
  68. {
  69. var view = CreateInstanceIfNotGeneric (viewType);
  70. if (view == null)
  71. {
  72. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  73. return;
  74. }
  75. if (view is IDesignable designable)
  76. {
  77. designable.EnableForDesign ();
  78. }
  79. var selectingCount = 0;
  80. view.Selecting += (s, e) => selectingCount++;
  81. var acceptingCount = 0;
  82. view.Accepting += (s, e) => { acceptingCount++; };
  83. if (view.InvokeCommand (Command.Accept) == true)
  84. {
  85. Assert.Equal (0, selectingCount);
  86. Assert.Equal (1, acceptingCount);
  87. }
  88. view?.Dispose ();
  89. }
  90. [Theory]
  91. [MemberData (nameof (AllViewTypes))]
  92. public void AllViews_Command_HotKey_Raises_HandlingHotKey (Type viewType)
  93. {
  94. var view = CreateInstanceIfNotGeneric (viewType);
  95. if (view == null)
  96. {
  97. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  98. return;
  99. }
  100. if (view is IDesignable designable)
  101. {
  102. designable.EnableForDesign ();
  103. }
  104. else
  105. {
  106. view.HotKey = Key.T;
  107. }
  108. var acceptedCount = 0;
  109. view.Accepting += (s, e) => { acceptedCount++; };
  110. var handlingHotKeyCount = 0;
  111. view.HandlingHotKey += (s, e) => { handlingHotKeyCount++; };
  112. if (view.InvokeCommand (Command.HotKey) == true)
  113. {
  114. Assert.Equal (1, handlingHotKeyCount);
  115. Assert.Equal (0, acceptedCount);
  116. }
  117. view?.Dispose ();
  118. }
  119. [Theory]
  120. [MemberData (nameof (AllViewTypes))]
  121. public void AllViews_Disabled_Draws_Disabled_Or_Faint (Type viewType)
  122. {
  123. var view = CreateInstanceIfNotGeneric (viewType);
  124. if (view == null)
  125. {
  126. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  127. return;
  128. }
  129. if (view is IDesignable designable)
  130. {
  131. designable.EnableForDesign ();
  132. }
  133. var mockDriver = new FakeDriver ();
  134. mockDriver.Init ();
  135. // TODO: Add AttributeSet event to FakeDriver if needed for attribute tracking tests
  136. // mockDriver.AttributeSet += (_, args) =>
  137. // {
  138. // if (args != view.GetAttributeForRole (VisualRole.Disabled) && args.Style != TextStyle.Faint)
  139. // {
  140. // Assert.Fail($"{viewType} with `Enabled == false` tried to SetAttribute to {args}");
  141. // }
  142. // };
  143. view.Driver = mockDriver;
  144. view.Enabled = false;
  145. view.SetNeedsDraw ();
  146. view.Draw ();
  147. view?.Dispose ();
  148. }
  149. }