ApplicationPopoverTests.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using static System.Net.Mime.MediaTypeNames;
  2. namespace Terminal.Gui.ApplicationTests;
  3. public class ApplicationPopoverTests
  4. {
  5. [Fact]
  6. public void ApplicationInit_Initializes_PopoverManager ()
  7. {
  8. // Arrange
  9. Assert.Null (Application.Popover);
  10. Application.Init (new FakeDriver ());
  11. // Act
  12. Assert.NotNull (Application.Popover);
  13. Application.ResetState (true);
  14. }
  15. [Fact]
  16. public void Application_Shutdown_CleansUp_PopoverManager ()
  17. {
  18. // Arrange
  19. Assert.Null (Application.Popover);
  20. Application.Init (new FakeDriver ());
  21. // Act
  22. Assert.NotNull (Application.Popover);
  23. Application.Shutdown ();
  24. // Test
  25. Assert.Null (Application.Popover);
  26. }
  27. [Fact]
  28. public void Application_End_Does_Not_CleanedUp ()
  29. {
  30. // Arrange
  31. Assert.Null (Application.Popover);
  32. Application.Init (new FakeDriver ());
  33. Assert.NotNull (Application.Popover);
  34. Application.Iteration += (s, a) => Application.RequestStop ();
  35. var top = new Toplevel ();
  36. RunState rs = Application.Begin (top);
  37. // Act
  38. Application.End (rs);
  39. // Test
  40. Assert.NotNull (Application.Popover);
  41. top.Dispose ();
  42. Application.Shutdown ();
  43. }
  44. [Fact]
  45. public void Application_End_Hides_Active ()
  46. {
  47. // Arrange
  48. Assert.Null (Application.Popover);
  49. Application.Init (new FakeDriver ());
  50. Application.Iteration += (s, a) => Application.RequestStop ();
  51. var top = new Toplevel ();
  52. RunState rs = Application.Begin (top);
  53. PopoverTestClass popover = new ();
  54. Application.Popover?.Show (popover);
  55. Assert.True (popover.Visible);
  56. // Act
  57. Application.End (rs);
  58. top.Dispose ();
  59. // Test
  60. Assert.False (popover.Visible);
  61. Assert.NotNull (Application.Popover);
  62. popover.Dispose ();
  63. Application.Shutdown ();
  64. }
  65. [Fact]
  66. public void Application_Shutdown_Disposes_Registered_Popovers ()
  67. {
  68. // Arrange
  69. Assert.Null (Application.Popover);
  70. Application.Init (new FakeDriver ());
  71. PopoverTestClass popover = new ();
  72. // Act
  73. Application.Popover?.Register (popover);
  74. Application.Shutdown ();
  75. // Test
  76. Assert.Equal(1, popover.DisposedCount);
  77. }
  78. [Fact]
  79. public void Application_Shutdown_Does_Not_Dispose_DeRegistered_Popovers ()
  80. {
  81. // Arrange
  82. Assert.Null (Application.Popover);
  83. Application.Init (new FakeDriver ());
  84. PopoverTestClass popover = new ();
  85. Application.Popover?.Register (popover);
  86. // Act
  87. Application.Popover?.DeRegister (popover);
  88. Application.Shutdown ();
  89. // Test
  90. Assert.Equal (0, popover.DisposedCount);
  91. popover.Dispose ();
  92. }
  93. [Fact]
  94. public void Application_Shutdown_Does_Not_Dispose_ActiveNotRegistered_Popover ()
  95. {
  96. // Arrange
  97. Assert.Null (Application.Popover);
  98. Application.Init (new FakeDriver ());
  99. PopoverTestClass popover = new ();
  100. Application.Popover?.Show (popover);
  101. // Act
  102. Application.Shutdown ();
  103. // Test
  104. Assert.Equal (0, popover.DisposedCount);
  105. popover.Dispose ();
  106. }
  107. public class PopoverTestClass : View, IPopover
  108. {
  109. public List<Key> HandledKeys { get; } = [];
  110. public int NewCommandInvokeCount { get; private set; }
  111. // NOTE: Hides the base DisposedCount property
  112. public new int DisposedCount { get; private set; }
  113. public PopoverTestClass ()
  114. {
  115. CanFocus = true;
  116. AddCommand (Command.New, NewCommandHandler);
  117. HotKeyBindings.Add (Key.N.WithCtrl, Command.New);
  118. return;
  119. bool? NewCommandHandler (ICommandContext ctx)
  120. {
  121. NewCommandInvokeCount++;
  122. return false;
  123. }
  124. }
  125. protected override bool OnKeyDown (Key key)
  126. {
  127. HandledKeys.Add (key);
  128. return false;
  129. }
  130. /// <inheritdoc />
  131. protected override void Dispose (bool disposing)
  132. {
  133. base.Dispose (disposing);
  134. DisposedCount++;
  135. }
  136. }
  137. }