RunStateTests.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. Assert.True (rs.WasDisposed);
  38. var top = new Toplevel ();
  39. rs = new Application.RunState (top);
  40. Assert.NotNull (rs);
  41. // Should throw because Toplevel was not cleaned up
  42. Assert.Throws<InvalidOperationException> (() => rs.Dispose ());
  43. rs.Toplevel.Dispose ();
  44. rs.Toplevel = null;
  45. rs.Dispose ();
  46. Assert.True (rs.WasDisposed);
  47. Assert.True (top.WasDisposed);
  48. }
  49. void Init ()
  50. {
  51. Application.Init (new FakeDriver ());
  52. Assert.NotNull (Application.Driver);
  53. Assert.NotNull (Application.MainLoop);
  54. Assert.NotNull (SynchronizationContext.Current);
  55. }
  56. void Shutdown ()
  57. {
  58. Application.Shutdown ();
  59. // Validate there are no outstanding RunState-based instances left
  60. foreach (var inst in Application.RunState.Instances) {
  61. Assert.True (inst.WasDisposed);
  62. }
  63. }
  64. [Fact]
  65. public void Begin_End_Cleans_Up_RunState ()
  66. {
  67. // Setup Mock driver
  68. Init ();
  69. // Test null Toplevel
  70. Assert.Throws<ArgumentNullException> (() => Application.Begin (null));
  71. var top = new Toplevel ();
  72. var rs = Application.Begin (top);
  73. Assert.NotNull (rs);
  74. Assert.Equal (top, Application.Current);
  75. Application.End (rs);
  76. Assert.Null (Application.Current);
  77. Assert.NotNull (Application.Top);
  78. Assert.NotNull (Application.MainLoop);
  79. Assert.NotNull (Application.Driver);
  80. Shutdown ();
  81. Assert.True (rs.WasDisposed);
  82. Assert.Null (Application.Top);
  83. Assert.Null (Application.MainLoop);
  84. Assert.Null (Application.Driver);
  85. }
  86. }
  87. }