RunStateTests.cs 2.6 KB

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