ApplicationPopoverTests.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. namespace Terminal.Gui.ApplicationTests;
  2. public class ApplicationPopoverTests
  3. {
  4. [Fact]
  5. public void Application_Init_Initializes_PopoverManager ()
  6. {
  7. // Arrange
  8. Assert.Null (Application.Popover);
  9. Application.Init (new FakeDriver ());
  10. // Act
  11. Assert.NotNull (Application.Popover);
  12. Application.ResetState (true);
  13. }
  14. [Fact]
  15. public void Application_Shutdown_Resets_PopoverManager ()
  16. {
  17. Application.ResetState (true);
  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_Reset_PopoverManager ()
  29. {
  30. Application.ResetState (true);
  31. // Arrange
  32. Assert.Null (Application.Popover);
  33. Application.Init (new FakeDriver ());
  34. Assert.NotNull (Application.Popover);
  35. Application.Iteration += (s, a) => Application.RequestStop ();
  36. var top = new Toplevel ();
  37. RunState rs = Application.Begin (top);
  38. // Act
  39. Application.End (rs);
  40. // Test
  41. Assert.NotNull (Application.Popover);
  42. top.Dispose ();
  43. Application.Shutdown ();
  44. }
  45. [Fact]
  46. public void Application_End_Hides_Active ()
  47. {
  48. Application.ResetState (true);
  49. // Arrange
  50. Assert.Null (Application.Popover);
  51. Application.Init (new FakeDriver ());
  52. Application.Iteration += (s, a) => Application.RequestStop ();
  53. var top = new Toplevel ();
  54. RunState rs = Application.Begin (top);
  55. PopoverTestClass popover = new ();
  56. Application.Popover?.Show (popover);
  57. Assert.True (popover.Visible);
  58. // Act
  59. Application.End (rs);
  60. top.Dispose ();
  61. // Test
  62. Assert.False (popover.Visible);
  63. Assert.NotNull (Application.Popover);
  64. Application.Shutdown ();
  65. Assert.Equal (1, popover.DisposedCount);
  66. }
  67. [Fact]
  68. public void Application_Shutdown_Disposes_Registered_Popovers ()
  69. {
  70. Application.ResetState (true);
  71. // Arrange
  72. Assert.Null (Application.Popover);
  73. Application.Init (new FakeDriver ());
  74. PopoverTestClass popover = new ();
  75. // Act
  76. Application.Popover?.Register (popover);
  77. Application.Shutdown ();
  78. // Test
  79. Assert.Equal (1, popover.DisposedCount);
  80. }
  81. [Fact]
  82. public void Application_Shutdown_Does_Not_Dispose_DeRegistered_Popovers ()
  83. {
  84. Application.ResetState (true);
  85. // Arrange
  86. Assert.Null (Application.Popover);
  87. Application.Init (new FakeDriver ());
  88. PopoverTestClass popover = new ();
  89. Application.Popover?.Register (popover);
  90. // Act
  91. Application.Popover?.DeRegister (popover);
  92. Application.Shutdown ();
  93. // Test
  94. Assert.Equal (0, popover.DisposedCount);
  95. popover.Dispose ();
  96. Assert.Equal (1, popover.DisposedCount);
  97. }
  98. [Fact]
  99. public void Application_Shutdown_Does_Not_Dispose_ActiveNotRegistered_Popover ()
  100. {
  101. Application.ResetState (true);
  102. // Arrange
  103. Assert.Null (Application.Popover);
  104. Application.Init (new FakeDriver ());
  105. PopoverTestClass popover = new ();
  106. Application.Popover?.Show (popover);
  107. Application.Popover?.DeRegister (popover);
  108. // Act
  109. Application.Shutdown ();
  110. // Test
  111. Assert.Equal (0, popover.DisposedCount);
  112. popover.Dispose ();
  113. Assert.Equal (1, popover.DisposedCount);
  114. }
  115. [Fact]
  116. public void Register_SetsTopLevel ()
  117. {
  118. Application.ResetState (true);
  119. // Arrange
  120. Assert.Null (Application.Popover);
  121. Application.Init (new FakeDriver ());
  122. Application.Top = new Toplevel ();
  123. PopoverTestClass popover = new ();
  124. // Act
  125. Application.Popover?.Register (popover);
  126. // Assert
  127. Assert.Equal (Application.Top, popover.Toplevel);
  128. Application.ResetState (true);
  129. }
  130. [Fact]
  131. public void Keyboard_Events_Go_Only_To_Popover_Associated_With_Toplevel ()
  132. {
  133. Application.ResetState (true);
  134. // Arrange
  135. Assert.Null (Application.Popover);
  136. Application.Init (new FakeDriver ());
  137. Application.Top = new Toplevel () { Id = "initialTop" };
  138. PopoverTestClass popover = new ();
  139. int keyDownEvents = 0;
  140. popover.KeyDown += (s, e) =>
  141. {
  142. keyDownEvents++;
  143. e.Handled = true;
  144. }; // Ensure it handles the key
  145. Application.Popover?.Register (popover);
  146. // Act
  147. Application.RaiseKeyDownEvent (Key.A); // Goes to initialTop
  148. Application.Top = new Toplevel () { Id = "secondaryTop" };
  149. Application.RaiseKeyDownEvent (Key.A); // Goes to secondaryTop
  150. // Test
  151. Assert.Equal (1, keyDownEvents);
  152. popover.Dispose ();
  153. Assert.Equal (1, popover.DisposedCount);
  154. Application.ResetState (true);
  155. }
  156. public class PopoverTestClass : PopoverBaseImpl
  157. {
  158. public List<Key> HandledKeys { get; } = [];
  159. public int NewCommandInvokeCount { get; private set; }
  160. // NOTE: Hides the base DisposedCount property
  161. public new int DisposedCount { get; private set; }
  162. public PopoverTestClass ()
  163. {
  164. CanFocus = true;
  165. AddCommand (Command.New, NewCommandHandler);
  166. HotKeyBindings.Add (Key.N.WithCtrl, Command.New);
  167. return;
  168. bool? NewCommandHandler (ICommandContext ctx)
  169. {
  170. NewCommandInvokeCount++;
  171. return false;
  172. }
  173. }
  174. protected override bool OnKeyDown (Key key)
  175. {
  176. HandledKeys.Add (key);
  177. return false;
  178. }
  179. /// <inheritdoc/>
  180. protected override void Dispose (bool disposing)
  181. {
  182. base.Dispose (disposing);
  183. DisposedCount++;
  184. }
  185. }
  186. }