ApplicationPopoverTests.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. namespace UnitTests.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. // See: https://github.com/gui-cs/Terminal.Gui/issues/4122
  157. [Theory]
  158. [InlineData (0, 0, new [] { "top" })]
  159. [InlineData (10, 10, new string [] { })]
  160. [InlineData (1, 1, new [] { "top", "view" })]
  161. [InlineData (5, 5, new [] { "top" })]
  162. [InlineData (6, 6, new [] { "popoverSubView" })]
  163. [InlineData (7, 7, new [] { "top" })]
  164. [InlineData (3, 3, new [] { "top" })]
  165. public void GetViewsUnderMouse_Supports_ActivePopover (int mouseX, int mouseY, string [] viewIdStrings)
  166. {
  167. Application.ResetState (true);
  168. // Arrange
  169. Assert.Null (Application.Popover);
  170. Application.Init (new FakeDriver ());
  171. Application.Top = new ()
  172. {
  173. Frame = new (0, 0, 10, 10),
  174. Id = "top"
  175. };
  176. View view = new ()
  177. {
  178. Id = "view",
  179. X = 1,
  180. Y = 1,
  181. Width = 2,
  182. Height = 2,
  183. }; // at 1,1 to 3,2 (screen)
  184. Application.Top.Add (view);
  185. PopoverTestClass popover = new ()
  186. {
  187. Id = "popover",
  188. X = 5,
  189. Y = 5,
  190. Width = 3,
  191. Height = 3,
  192. }; // at 5,5 to 8,8 (screen)
  193. View popoverSubView = new ()
  194. {
  195. Id = "popoverSubView",
  196. X = 1,
  197. Y = 1,
  198. Width = 1,
  199. Height = 1,
  200. }; // at 6,6 to 7,7 (screen)
  201. popover.Add (popoverSubView);
  202. Application.Popover?.Show (popover);
  203. List<View?> found = View.GetViewsUnderLocation (new (mouseX, mouseY), ViewportSettingsFlags.TransparentMouse);
  204. string [] foundIds = found.Select (v => v!.Id).ToArray ();
  205. Assert.Equal (viewIdStrings, foundIds);
  206. popover.Dispose ();
  207. Application.Top.Dispose ();
  208. Application.ResetState (true);
  209. }
  210. public class PopoverTestClass : PopoverBaseImpl
  211. {
  212. public List<Key> HandledKeys { get; } = [];
  213. public int NewCommandInvokeCount { get; private set; }
  214. // NOTE: Hides the base DisposedCount property
  215. public new int DisposedCount { get; private set; }
  216. public PopoverTestClass ()
  217. {
  218. CanFocus = true;
  219. AddCommand (Command.New, NewCommandHandler);
  220. HotKeyBindings.Add (Key.N.WithCtrl, Command.New);
  221. return;
  222. bool? NewCommandHandler (ICommandContext ctx)
  223. {
  224. NewCommandInvokeCount++;
  225. return false;
  226. }
  227. }
  228. protected override bool OnKeyDown (Key key)
  229. {
  230. HandledKeys.Add (key);
  231. return false;
  232. }
  233. /// <inheritdoc/>
  234. protected override void Dispose (bool disposing)
  235. {
  236. base.Dispose (disposing);
  237. DisposedCount++;
  238. }
  239. }
  240. }