2
0

TestSetup.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.MouseGrabHandler.MouseGrabView);
  38. // Don't check Application.ForceDriver
  39. // Assert.Empty (Application.ForceDriver);
  40. // Don't check Application.Force16Colors
  41. //Assert.False (Application.Force16Colors);
  42. Assert.Null (Application.Driver);
  43. Assert.Null (Application.MainLoop);
  44. Assert.False (Application.EndAfterFirstIteration);
  45. Assert.Equal (Key.Tab.WithShift, Application.PrevTabKey);
  46. Assert.Equal (Key.Tab, Application.NextTabKey);
  47. Assert.Equal (Key.F6.WithShift, Application.PrevTabGroupKey);
  48. Assert.Equal (Key.F6, Application.NextTabGroupKey);
  49. Assert.Equal (Key.Esc, Application.QuitKey);
  50. // Internal properties
  51. Assert.False (Application.Initialized);
  52. Assert.Equal (Application.GetSupportedCultures (), Application.SupportedCultures);
  53. Assert.Equal (Application.GetAvailableCulturesFromEmbeddedResources (), Application.SupportedCultures);
  54. Assert.False (Application._forceFakeConsole);
  55. Assert.Equal (-1, Application.MainThreadId);
  56. Assert.Empty (Application.TopLevels);
  57. Assert.Empty (Application.CachedViewsUnderMouse);
  58. // Mouse
  59. // Do not reset _lastMousePosition
  60. //Assert.Null (Application._lastMousePosition);
  61. // Navigation
  62. Assert.Null (Application.Navigation);
  63. // Popover
  64. Assert.Null (Application.Popover);
  65. // Events - Can't check
  66. //Assert.Null (Application.NotifyNewRunState);
  67. //Assert.Null (Application.NotifyNewRunState);
  68. //Assert.Null (Application.Iteration);
  69. //Assert.Null (Application.SizeChanging);
  70. //Assert.Null (Application.GrabbedMouse);
  71. //Assert.Null (Application.UnGrabbingMouse);
  72. //Assert.Null (Application.GrabbedMouse);
  73. //Assert.Null (Application.UnGrabbedMouse);
  74. //Assert.Null (Application.MouseEvent);
  75. //Assert.Null (Application.KeyDown);
  76. //Assert.Null (Application.KeyUp);
  77. }
  78. }
  79. // Define a collection for the global setup
  80. [CollectionDefinition ("Global Test Setup")]
  81. public class GlobalTestSetupCollection : ICollectionFixture<GlobalTestSetup>
  82. {
  83. // This class has no code and is never instantiated.
  84. // Its purpose is to apply the [CollectionDefinition] attribute
  85. // and associate the GlobalTestSetup with the test collection.
  86. }