2
0

AutoInitShutdownAttribute.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System.Diagnostics;
  2. using System.Globalization;
  3. using System.Reflection;
  4. using Xunit.Sdk;
  5. namespace UnitTests;
  6. /// <summary>
  7. /// Enables test functions annotated with the [AutoInitShutdown] attribute to
  8. /// automatically call Application.Init at start of the test and Application.Shutdown after the
  9. /// test exits.
  10. /// This is necessary because a) Application is a singleton and Init/Shutdown must be called
  11. /// as a pair, and b) all unit test functions should be atomic..
  12. /// </summary>
  13. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
  14. public class AutoInitShutdownAttribute : BeforeAfterTestAttribute
  15. {
  16. /// <summary>
  17. /// Initializes a [AutoInitShutdown] attribute, which determines if/how Application.Init and Application.Shutdown
  18. /// are automatically called Before/After a test runs.
  19. /// </summary>
  20. /// <param name="autoInit">If true, Application.Init will be called Before the test runs.</param>
  21. /// <param name="consoleDriverType">
  22. /// Determines which IConsoleDriver (FakeDriver, WindowsDriver, CursesDriver, NetDriver)
  23. /// will be used when Application.Init is called. If null FakeDriver will be used. Only valid if
  24. /// <paramref name="autoInit"/> is true.
  25. /// </param>
  26. /// <param name="useFakeClipboard">
  27. /// If true, will force the use of <see cref="FakeDriver.FakeClipboard"/>. Only valid if
  28. /// <see cref="IConsoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.
  29. /// </param>
  30. /// <param name="fakeClipboardAlwaysThrowsNotSupportedException">
  31. /// Only valid if <paramref name="autoInit"/> is true. Only
  32. /// valid if <see cref="IConsoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.
  33. /// </param>
  34. /// <param name="fakeClipboardIsSupportedAlwaysTrue">
  35. /// Only valid if <paramref name="autoInit"/> is true. Only valid if
  36. /// <see cref="IConsoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.
  37. /// </param>
  38. /// <param name="verifyShutdown">If true and <see cref="Application.Initialized"/> is true, the test will fail.</param>
  39. public AutoInitShutdownAttribute (
  40. bool autoInit = true,
  41. Type consoleDriverType = null,
  42. bool useFakeClipboard = true,
  43. bool fakeClipboardAlwaysThrowsNotSupportedException = false,
  44. bool fakeClipboardIsSupportedAlwaysTrue = false,
  45. bool verifyShutdown = false
  46. )
  47. {
  48. AutoInit = autoInit;
  49. CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
  50. _driverType = consoleDriverType;
  51. FakeDriver.FakeBehaviors.UseFakeClipboard = useFakeClipboard;
  52. FakeDriver.FakeBehaviors.FakeClipboardAlwaysThrowsNotSupportedException =
  53. fakeClipboardAlwaysThrowsNotSupportedException;
  54. FakeDriver.FakeBehaviors.FakeClipboardIsSupportedAlwaysFalse = fakeClipboardIsSupportedAlwaysTrue;
  55. _verifyShutdown = verifyShutdown;
  56. }
  57. private readonly bool _verifyShutdown;
  58. private readonly Type _driverType;
  59. private IDisposable _v2Cleanup;
  60. public override void After (MethodInfo methodUnderTest)
  61. {
  62. Debug.WriteLine ($"After: {methodUnderTest?.Name ?? "Unknown Test"}");
  63. // Turn off diagnostic flags in case some test left them on
  64. View.Diagnostics = ViewDiagnosticFlags.Off;
  65. _v2Cleanup?.Dispose ();
  66. if (AutoInit)
  67. {
  68. // try
  69. {
  70. if (!_verifyShutdown)
  71. {
  72. Application.ResetState (ignoreDisposed: true);
  73. }
  74. Application.Shutdown ();
  75. #if DEBUG_IDISPOSABLE
  76. if (View.Instances.Count == 0)
  77. {
  78. Assert.Empty (View.Instances);
  79. }
  80. else
  81. {
  82. View.Instances.Clear ();
  83. }
  84. #endif
  85. }
  86. //catch (Exception e)
  87. //{
  88. // Assert.Fail ($"Application.Shutdown threw an exception after the test exited: {e}");
  89. //}
  90. //finally
  91. {
  92. #if DEBUG_IDISPOSABLE
  93. View.Instances.Clear ();
  94. Application.ResetState (true);
  95. #endif
  96. }
  97. }
  98. Debug.Assert (!CM.IsEnabled, "This test left ConfigurationManager enabled!");
  99. // Force the ConfigurationManager to reset to its hardcoded defaults
  100. CM.Disable(true);
  101. }
  102. public override void Before (MethodInfo methodUnderTest)
  103. {
  104. Debug.WriteLine ($"Before: {methodUnderTest?.Name ?? "Unknown Test"}");
  105. // Disable & force the ConfigurationManager to reset to its hardcoded defaults
  106. CM.Disable (true);
  107. //Debug.Assert(!CM.IsEnabled, "Some other test left ConfigurationManager enabled.");
  108. if (AutoInit)
  109. {
  110. #if DEBUG_IDISPOSABLE
  111. View.EnableDebugIDisposableAsserts = true;
  112. // Clear out any lingering Responder instances from previous tests
  113. if (View.Instances.Count == 0)
  114. {
  115. Assert.Empty (View.Instances);
  116. }
  117. else
  118. {
  119. View.Instances.Clear ();
  120. }
  121. #endif
  122. if (_driverType == null)
  123. {
  124. Application.Top = null;
  125. Application.TopLevels.Clear ();
  126. var fa = new FakeApplicationFactory ();
  127. _v2Cleanup = fa.SetupFakeApplication ();
  128. AutoInitShutdownAttribute.FakeResize (new Size (80,25));
  129. }
  130. else
  131. {
  132. Application.Init ((IConsoleDriver)Activator.CreateInstance (_driverType));
  133. }
  134. }
  135. }
  136. private bool AutoInit { get; }
  137. /// <summary>
  138. /// 'Resizes' the application and forces layout. Only works if your test uses <see cref="AutoInitShutdownAttribute"/>
  139. /// </summary>
  140. /// <param name="size"></param>
  141. public static void FakeResize (Size size)
  142. {
  143. var d = (IConsoleDriverFacade)Application.Driver!;
  144. d.OutputBuffer.SetWindowSize (size.Width, size.Height);
  145. ((FakeSizeMonitor)d.WindowSizeMonitor).RaiseSizeChanging (size);
  146. Application.LayoutAndDrawImpl ();
  147. }
  148. /// <summary>
  149. /// Runs a single iteration of the main loop (layout, draw, run timed events etc.)
  150. /// </summary>
  151. public static void RunIteration ()
  152. {
  153. var a = (ApplicationV2)ApplicationImpl.Instance;
  154. a.Coordinator?.RunIteration ();
  155. }
  156. }