AutoInitShutdownAttribute.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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="forceDriver">
  22. /// Forces the specified driver ("windows", "dotnet", "unix", or "fake") to
  23. /// be used when Application.Init is called. If not specified FakeDriver will be used. Only valid if
  24. /// <paramref name="autoInit"/> is true.
  25. /// </param>
  26. public AutoInitShutdownAttribute (
  27. bool autoInit = true,
  28. string forceDriver = null
  29. )
  30. {
  31. _autoInit = autoInit;
  32. CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
  33. _forceDriver = forceDriver;
  34. }
  35. private readonly string _forceDriver;
  36. private IDisposable _v2Cleanup;
  37. public override void After (MethodInfo methodUnderTest)
  38. {
  39. Debug.WriteLine ($"After: {methodUnderTest?.Name ?? "Unknown Test"}");
  40. // Turn off diagnostic flags in case some test left them on
  41. View.Diagnostics = ViewDiagnosticFlags.Off;
  42. _v2Cleanup?.Dispose ();
  43. if (_autoInit)
  44. {
  45. try
  46. {
  47. Application.Shutdown ();
  48. #if DEBUG_IDISPOSABLE
  49. if (View.Instances.Count == 0)
  50. {
  51. Assert.Empty (View.Instances);
  52. }
  53. else
  54. {
  55. View.Instances.Clear ();
  56. }
  57. #endif
  58. }
  59. //catch (Exception e)
  60. //{
  61. // Debug.WriteLine ($"Application.Shutdown threw an exception after the test exited: {e}");
  62. //}
  63. finally
  64. {
  65. #if DEBUG_IDISPOSABLE
  66. View.Instances.Clear ();
  67. Application.ResetState (true);
  68. #endif
  69. ApplicationImpl.SetInstance (null);
  70. }
  71. }
  72. Debug.Assert (!CM.IsEnabled, "This test left ConfigurationManager enabled!");
  73. // Force the ConfigurationManager to reset to its hardcoded defaults
  74. CM.Disable (true);
  75. }
  76. public override void Before (MethodInfo methodUnderTest)
  77. {
  78. Debug.WriteLine ($"Before: {methodUnderTest?.Name ?? "Unknown Test"}");
  79. Debug.Assert (!CM.IsEnabled, "A previous test left ConfigurationManager enabled!");
  80. // Disable & force the ConfigurationManager to reset to its hardcoded defaults
  81. CM.Disable (true);
  82. //Debug.Assert(!CM.IsEnabled, "Some other test left ConfigurationManager enabled.");
  83. if (_autoInit)
  84. {
  85. #if DEBUG_IDISPOSABLE
  86. View.EnableDebugIDisposableAsserts = true;
  87. // Clear out any lingering Responder instances from previous tests
  88. if (View.Instances.Count == 0)
  89. {
  90. Assert.Empty (View.Instances);
  91. }
  92. else
  93. {
  94. View.Instances.Clear ();
  95. }
  96. #endif
  97. if (string.IsNullOrEmpty (_forceDriver) || _forceDriver.ToLowerInvariant () == "fake")
  98. {
  99. var fa = new FakeApplicationFactory ();
  100. _v2Cleanup = fa.SetupFakeApplication ();
  101. }
  102. else
  103. {
  104. Assert.Fail ("Specifying driver name not yet supported");
  105. //Application.Init ((IDriver)Activator.CreateInstance (_forceDriver));
  106. }
  107. }
  108. }
  109. private bool _autoInit { get; }
  110. /// <summary>
  111. /// Runs a single iteration of the main loop (layout, draw, run timed events etc.)
  112. /// </summary>
  113. public static void RunIteration ()
  114. {
  115. ApplicationImpl a = (ApplicationImpl)ApplicationImpl.Instance;
  116. a.Coordinator?.RunIteration ();
  117. }
  118. }