ApplicationPopoverTests.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #nullable enable
  2. using Moq;
  3. using Terminal.Gui.App;
  4. namespace UnitTests_Parallelizable.ApplicationTests;
  5. public class ApplicationPopoverTests
  6. {
  7. [Fact]
  8. public void Register_AddsPopover ()
  9. {
  10. // Arrange
  11. var popover = new Mock<IPopover> ().Object;
  12. var popoverManager = new ApplicationPopover ();
  13. // Act
  14. popoverManager.Register (popover);
  15. // Assert
  16. Assert.Contains (popover, popoverManager.Popovers);
  17. }
  18. [Fact]
  19. public void DeRegister_RemovesPopover ()
  20. {
  21. // Arrange
  22. var popover = new Mock<IPopover> ().Object;
  23. var popoverManager = new ApplicationPopover ();
  24. popoverManager.Register (popover);
  25. // Act
  26. var result = popoverManager.DeRegister (popover);
  27. // Assert
  28. Assert.True (result);
  29. Assert.DoesNotContain (popover, popoverManager.Popovers);
  30. }
  31. [Fact]
  32. public void Show_SetsActivePopover ()
  33. {
  34. // Arrange
  35. var popover = new Mock<PopoverTestClass> ().Object;
  36. var popoverManager = new ApplicationPopover ();
  37. popoverManager.Register (popover);
  38. // Act
  39. popoverManager.Show (popover);
  40. // Assert
  41. Assert.Equal (popover, popoverManager.GetActivePopover ());
  42. }
  43. [Fact]
  44. public void Hide_ClearsActivePopover ()
  45. {
  46. // Arrange
  47. var popover = new Mock<IPopover> ().Object;
  48. var popoverManager = new ApplicationPopover ();
  49. popoverManager.Register (popover);
  50. popoverManager.Show (popover);
  51. // Act
  52. popoverManager.Hide (popover);
  53. // Assert
  54. Assert.Null (popoverManager.GetActivePopover ());
  55. }
  56. [Fact]
  57. public void DispatchKeyDown_ActivePopoverGetsKey ()
  58. {
  59. // Arrange
  60. var popover = new PopoverTestClass ();
  61. var popoverManager = new ApplicationPopover ();
  62. popoverManager.Register (popover);
  63. popoverManager.Show (popover);
  64. // Act
  65. popoverManager.DispatchKeyDown (Key.A);
  66. // Assert
  67. Assert.Contains (KeyCode.A, popover.HandledKeys);
  68. }
  69. [Fact]
  70. public void DispatchKeyDown_ActivePopoverGetsHotKey ()
  71. {
  72. // Arrange
  73. var popover = new PopoverTestClass ();
  74. var popoverManager = new ApplicationPopover ();
  75. popoverManager.Register (popover);
  76. popoverManager.Show (popover);
  77. // Act
  78. popoverManager.DispatchKeyDown (Key.N.WithCtrl);
  79. // Assert
  80. Assert.Equal (1, popover.NewCommandInvokeCount);
  81. Assert.Contains (Key.N.WithCtrl, popover.HandledKeys);
  82. }
  83. [Fact]
  84. public void DispatchKeyDown_InactivePopoverGetsHotKey ()
  85. {
  86. // Arrange
  87. var activePopover = new PopoverTestClass () { Id = "activePopover" };
  88. var inactivePopover = new PopoverTestClass () { Id = "inactivePopover" }; ;
  89. var popoverManager = new ApplicationPopover ();
  90. popoverManager.Register (activePopover);
  91. popoverManager.Show (activePopover);
  92. popoverManager.Register (inactivePopover);
  93. // Act
  94. popoverManager.DispatchKeyDown (Key.N.WithCtrl);
  95. // Assert
  96. Assert.Equal (1, activePopover.NewCommandInvokeCount);
  97. Assert.Equal (1, inactivePopover.NewCommandInvokeCount);
  98. Assert.Contains (Key.N.WithCtrl, activePopover.HandledKeys);
  99. Assert.NotEmpty (inactivePopover.HandledKeys);
  100. }
  101. [Fact]
  102. public void DispatchKeyDown_InactivePopoverDoesGetKey ()
  103. {
  104. // Arrange
  105. var activePopover = new PopoverTestClass ();
  106. var inactivePopover = new PopoverTestClass ();
  107. var popoverManager = new ApplicationPopover ();
  108. popoverManager.Register (activePopover);
  109. popoverManager.Show (activePopover);
  110. popoverManager.Register (inactivePopover);
  111. // Act
  112. popoverManager.DispatchKeyDown (Key.A);
  113. // Assert
  114. Assert.Contains (Key.A, activePopover.HandledKeys);
  115. Assert.NotEmpty (inactivePopover.HandledKeys);
  116. }
  117. public class PopoverTestClass : View, IPopover
  118. {
  119. public List<Key> HandledKeys { get; } = new List<Key> ();
  120. public int NewCommandInvokeCount { get; private set; }
  121. public PopoverTestClass ()
  122. {
  123. ViewportSettings = ViewportSettingsFlags.Transparent | ViewportSettingsFlags.TransparentMouse;
  124. CanFocus = true;
  125. AddCommand (Command.New, NewCommandHandler!);
  126. HotKeyBindings.Add (Key.N.WithCtrl, Command.New);
  127. AddCommand (Command.Quit, Quit);
  128. KeyBindings.Add (Application.QuitKey, Command.Quit);
  129. return;
  130. bool? Quit (ICommandContext? ctx)
  131. {
  132. if (!Visible)
  133. {
  134. return false;
  135. }
  136. Visible = false;
  137. return true;
  138. }
  139. bool? NewCommandHandler (ICommandContext ctx)
  140. {
  141. NewCommandInvokeCount++;
  142. return false;
  143. }
  144. }
  145. protected override bool OnKeyDown (Key key)
  146. {
  147. HandledKeys.Add (key);
  148. return false;
  149. }
  150. /// <inheritdoc />
  151. public Toplevel? Current { get; set; }
  152. }
  153. }