TestSetup.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. namespace UnitTests.Parallelizable;
  2. /// <summary>
  3. /// Ensures that tests can run in parallel without interference
  4. /// by setting various Terminal.Gui static properties to their default values. E.g. View.EnableDebugIDisposableAsserts.
  5. /// Annotate all test classes with [Collection("Global Test Setup")] or have it inherit from this class.
  6. /// </summary>
  7. public class GlobalTestSetup : IDisposable
  8. {
  9. public GlobalTestSetup ()
  10. {
  11. #if DEBUG_IDISPOSABLE
  12. // Ensure EnableDebugIDisposableAsserts is false before tests run
  13. View.EnableDebugIDisposableAsserts = false;
  14. #endif
  15. CheckDefaultState ();
  16. }
  17. public void Dispose ()
  18. {
  19. // Optionally reset EnableDebugIDisposableAsserts after tests. Don't do this.
  20. // View.EnableDebugIDisposableAsserts = true;
  21. // Reset application state just in case a test changed something.
  22. // TODO: Add an Assert to ensure none of the state of Application changed.
  23. // TODO: Add an Assert to ensure none of the state of ConfigurationManager changed.
  24. CheckDefaultState ();
  25. Application.ResetState (true);
  26. }
  27. // IMPORTANT: Ensure this matches the code in Init_ResetState_Resets_Properties
  28. // here: .\Tests\UnitTests\Application\ApplicationTests.cs
  29. private void CheckDefaultState ()
  30. {
  31. #if DEBUG_IDISPOSABLE
  32. Assert.False (View.EnableDebugIDisposableAsserts, "View.EnableDebugIDisposableAsserts should be false for Parallelizable tests.");
  33. #endif
  34. // Check that all Application fields and properties are set to their default values
  35. // Public Properties
  36. Assert.Null (Application.Top);
  37. Assert.Null (Application.MouseGrabView);
  38. Assert.Null (Application.WantContinuousButtonPressedView);
  39. // Don't check Application.ForceDriver
  40. // Assert.Empty (Application.ForceDriver);
  41. // Don't check Application.Force16Colors
  42. //Assert.False (Application.Force16Colors);
  43. Assert.Null (Application.Driver);
  44. Assert.Null (Application.MainLoop);
  45. Assert.False (Application.EndAfterFirstIteration);
  46. Assert.Equal (Key.Tab.WithShift, Application.PrevTabKey);
  47. Assert.Equal (Key.Tab, Application.NextTabKey);
  48. Assert.Equal (Key.F6.WithShift, Application.PrevTabGroupKey);
  49. Assert.Equal (Key.F6, Application.NextTabGroupKey);
  50. Assert.Equal (Key.Esc, Application.QuitKey);
  51. // Internal properties
  52. Assert.False (Application.Initialized);
  53. Assert.Equal (Application.GetSupportedCultures (), Application.SupportedCultures);
  54. Assert.Equal (Application.GetAvailableCulturesFromEmbeddedResources (), Application.SupportedCultures);
  55. Assert.False (Application._forceFakeConsole);
  56. Assert.Equal (-1, Application.MainThreadId);
  57. Assert.Empty (Application.TopLevels);
  58. Assert.Empty (Application._cachedViewsUnderMouse);
  59. // Mouse
  60. // Do not reset _lastMousePosition
  61. //Assert.Null (Application._lastMousePosition);
  62. // Navigation
  63. Assert.Null (Application.Navigation);
  64. // Popover
  65. Assert.Null (Application.Popover);
  66. // Events - Can't check
  67. //Assert.Null (Application.NotifyNewRunState);
  68. //Assert.Null (Application.NotifyNewRunState);
  69. //Assert.Null (Application.Iteration);
  70. //Assert.Null (Application.SizeChanging);
  71. //Assert.Null (Application.GrabbedMouse);
  72. //Assert.Null (Application.UnGrabbingMouse);
  73. //Assert.Null (Application.GrabbedMouse);
  74. //Assert.Null (Application.UnGrabbedMouse);
  75. //Assert.Null (Application.MouseEvent);
  76. //Assert.Null (Application.KeyDown);
  77. //Assert.Null (Application.KeyUp);
  78. }
  79. }
  80. // Define a collection for the global setup
  81. [CollectionDefinition ("Global Test Setup")]
  82. public class GlobalTestSetupCollection : ICollectionFixture<GlobalTestSetup>
  83. {
  84. // This class has no code and is never instantiated.
  85. // Its purpose is to apply the [CollectionDefinition] attribute
  86. // and associate the GlobalTestSetup with the test collection.
  87. }