AllViewsTests.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System.Reflection;
  2. using Microsoft.VisualStudio.TestPlatform.Utilities;
  3. using UnitTests;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.ViewsTests;
  6. public class AllViewsTests (ITestOutputHelper output) : TestsAllViews
  7. {
  8. [Theory]
  9. [MemberData (nameof (AllViewTypes))]
  10. public void AllViews_Tests_All_Constructors (Type viewType)
  11. {
  12. Assert.True (TestAllConstructorsOfType (viewType));
  13. return;
  14. bool TestAllConstructorsOfType (Type type)
  15. {
  16. foreach (ConstructorInfo ctor in type.GetConstructors ())
  17. {
  18. View view = CreateViewFromType (type, ctor);
  19. if (view != null)
  20. {
  21. Assert.True (type.FullName == view.GetType ().FullName);
  22. }
  23. }
  24. return true;
  25. }
  26. }
  27. //[Fact]
  28. //public void AllViews_HotKey_Works ()
  29. //{
  30. // foreach (var type in GetAllViewClasses ()) {
  31. // _output.WriteLine ($"Testing {type.Name}");
  32. // var view = GetTypeInitializer (type, type.GetConstructor (Array.Empty<Type> ()));
  33. // view.HotKeySpecifier = (Rune)'^';
  34. // view.Text = "^text";
  35. // Assert.Equal(Key.T, view.HotKey);
  36. // }
  37. //}
  38. [Theory]
  39. [MemberData (nameof (AllViewTypes))]
  40. public void AllViews_Command_Select_Raises_Selecting (Type viewType)
  41. {
  42. var view = CreateInstanceIfNotGeneric (viewType);
  43. if (view == null)
  44. {
  45. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  46. return;
  47. }
  48. if (view is IDesignable designable)
  49. {
  50. designable.EnableForDesign ();
  51. }
  52. var selectingCount = 0;
  53. view.Selecting += (s, e) => selectingCount++;
  54. var acceptedCount = 0;
  55. view.Accepting += (s, e) => { acceptedCount++; };
  56. if (view.InvokeCommand (Command.Select) == true)
  57. {
  58. Assert.Equal (1, selectingCount);
  59. Assert.Equal (0, acceptedCount);
  60. }
  61. }
  62. [Theory]
  63. [MemberData (nameof (AllViewTypes))]
  64. public void AllViews_Command_Accept_Raises_Accepted (Type viewType)
  65. {
  66. var view = CreateInstanceIfNotGeneric (viewType);
  67. if (view == null)
  68. {
  69. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  70. return;
  71. }
  72. if (view is IDesignable designable)
  73. {
  74. designable.EnableForDesign ();
  75. }
  76. var selectingCount = 0;
  77. view.Selecting += (s, e) => selectingCount++;
  78. var acceptedCount = 0;
  79. view.Accepting += (s, e) => { acceptedCount++; };
  80. if (view.InvokeCommand (Command.Accept) == true)
  81. {
  82. Assert.Equal (0, selectingCount);
  83. Assert.Equal (1, acceptedCount);
  84. }
  85. }
  86. [Theory]
  87. [MemberData (nameof (AllViewTypes))]
  88. public void AllViews_Command_HotKey_Raises_HandlingHotKey (Type viewType)
  89. {
  90. var view = CreateInstanceIfNotGeneric (viewType);
  91. if (view == null)
  92. {
  93. output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  94. return;
  95. }
  96. if (view is IDesignable designable)
  97. {
  98. designable.EnableForDesign ();
  99. }
  100. else
  101. {
  102. view.HotKey = Key.T;
  103. }
  104. var acceptedCount = 0;
  105. view.Accepting += (s, e) => { acceptedCount++; };
  106. var handlingHotKeyCount = 0;
  107. view.HandlingHotKey += (s, e) => { handlingHotKeyCount++; };
  108. if (view.InvokeCommand (Command.HotKey) == true)
  109. {
  110. Assert.Equal (1, handlingHotKeyCount);
  111. Assert.Equal (0, acceptedCount);
  112. }
  113. }
  114. }