RunStateTests.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. View.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. Application.End (rs);
  24. Assert.NotNull (Application.Top);
  25. Assert.NotNull (Application.MainLoop);
  26. Assert.NotNull (Application.Driver);
  27. top.Dispose ();
  28. Shutdown ();
  29. #if DEBUG_IDISPOSABLE
  30. Assert.True (rs.WasDisposed);
  31. #endif
  32. Assert.Null (Application.Top);
  33. Assert.Null (Application.MainLoop);
  34. Assert.Null (Application.Driver);
  35. }
  36. [Fact]
  37. public void Dispose_Cleans_Up_RunState ()
  38. {
  39. var rs = new RunState (null);
  40. Assert.NotNull (rs);
  41. // Should not throw because Toplevel was null
  42. rs.Dispose ();
  43. #if DEBUG_IDISPOSABLE
  44. Assert.True (rs.WasDisposed);
  45. #endif
  46. var top = new Toplevel ();
  47. rs = new RunState (top);
  48. Assert.NotNull (rs);
  49. // Should throw because Toplevel was not cleaned up
  50. Assert.Throws<InvalidOperationException> (() => rs.Dispose ());
  51. rs.Toplevel.Dispose ();
  52. rs.Toplevel = null;
  53. rs.Dispose ();
  54. #if DEBUG_IDISPOSABLE
  55. Assert.True (rs.WasDisposed);
  56. Assert.True (top.WasDisposed);
  57. #endif
  58. }
  59. [Fact]
  60. public void New_Creates_RunState ()
  61. {
  62. var rs = new RunState (null);
  63. Assert.Null (rs.Toplevel);
  64. var top = new Toplevel ();
  65. rs = new RunState (top);
  66. Assert.Equal (top, rs.Toplevel);
  67. }
  68. private void Init ()
  69. {
  70. Application.Init (new FakeDriver ());
  71. Assert.NotNull (Application.Driver);
  72. Assert.NotNull (Application.MainLoop);
  73. Assert.NotNull (SynchronizationContext.Current);
  74. }
  75. private void Shutdown ()
  76. {
  77. Application.Shutdown ();
  78. #if DEBUG_IDISPOSABLE
  79. // Validate there are no outstanding RunState-based instances left
  80. foreach (RunState inst in RunState.Instances)
  81. {
  82. Assert.True (inst.WasDisposed);
  83. }
  84. #endif
  85. }
  86. }