TestSetup.cs 3.9 KB

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