RunStateTests.cs 2.5 KB

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