RunStateTests.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Alias Console to MockConsole so we don't accidentally use Console
  2. namespace Terminal.Gui.ApplicationTests;
  3. /// <summary>These tests focus on Application.RunState and the various ways it can be changed.</summary>
  4. public class RunStateTests
  5. {
  6. public RunStateTests ()
  7. {
  8. #if DEBUG_IDISPOSABLE
  9. Responder.Instances.Clear ();
  10. RunState.Instances.Clear ();
  11. #endif
  12. }
  13. [Fact]
  14. public void Begin_End_Cleans_Up_RunState ()
  15. {
  16. // Setup Mock driver
  17. Init ();
  18. // Test null Toplevel
  19. Assert.Throws<ArgumentNullException> (() => Application.Begin (null));
  20. var top = new Toplevel ();
  21. RunState rs = Application.Begin (top);
  22. Assert.NotNull (rs);
  23. Assert.Equal (top, Application.Current);
  24. Application.End (rs);
  25. Assert.Null (Application.Current);
  26. Assert.NotNull (Application.Top);
  27. Assert.NotNull (Application.MainLoop);
  28. Assert.NotNull (Application.Driver);
  29. top.Dispose ();
  30. Shutdown ();
  31. #if DEBUG_IDISPOSABLE
  32. Assert.True (rs.WasDisposed);
  33. #endif
  34. Assert.Null (Application.Top);
  35. Assert.Null (Application.MainLoop);
  36. Assert.Null (Application.Driver);
  37. }
  38. [Fact]
  39. public void Dispose_Cleans_Up_RunState ()
  40. {
  41. var rs = new RunState (null);
  42. Assert.NotNull (rs);
  43. // Should not throw because Toplevel was null
  44. rs.Dispose ();
  45. #if DEBUG_IDISPOSABLE
  46. Assert.True (rs.WasDisposed);
  47. #endif
  48. var top = new Toplevel ();
  49. rs = new RunState (top);
  50. Assert.NotNull (rs);
  51. // Should throw because Toplevel was not cleaned up
  52. Assert.Throws<InvalidOperationException> (() => rs.Dispose ());
  53. rs.Toplevel.Dispose ();
  54. rs.Toplevel = null;
  55. rs.Dispose ();
  56. #if DEBUG_IDISPOSABLE
  57. Assert.True (rs.WasDisposed);
  58. Assert.True (top.WasDisposed);
  59. #endif
  60. }
  61. [Fact]
  62. public void New_Creates_RunState ()
  63. {
  64. var rs = new RunState (null);
  65. Assert.Null (rs.Toplevel);
  66. var top = new Toplevel ();
  67. rs = new RunState (top);
  68. Assert.Equal (top, rs.Toplevel);
  69. }
  70. private void Init ()
  71. {
  72. Application.Init (new FakeDriver ());
  73. Assert.NotNull (Application.Driver);
  74. Assert.NotNull (Application.MainLoop);
  75. Assert.NotNull (SynchronizationContext.Current);
  76. }
  77. private void Shutdown ()
  78. {
  79. Application.Shutdown ();
  80. #if DEBUG_IDISPOSABLE
  81. // Validate there are no outstanding RunState-based instances left
  82. foreach (RunState inst in RunState.Instances)
  83. {
  84. Assert.True (inst.WasDisposed);
  85. }
  86. #endif
  87. }
  88. }