ApplicationPopoverTests.cs 4.4 KB

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