RunStateTests.cs 2.4 KB

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