ApplicationPopoverTests.cs 4.9 KB

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