ContextMenuTests.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Windows.Input;
  3. using Avalonia.Controls.Primitives;
  4. using Avalonia.Data;
  5. using Avalonia.Input;
  6. using Avalonia.Markup.Data;
  7. using Avalonia.Platform;
  8. using Avalonia.UnitTests;
  9. using Moq;
  10. using Xunit;
  11. namespace Avalonia.Controls.UnitTests
  12. {
  13. public class ContextMenuTests
  14. {
  15. private Mock<IPopupImpl> popupImpl;
  16. private MouseTestHelper _mouse = new MouseTestHelper();
  17. [Fact]
  18. public void Clicking_On_Control_Toggles_ContextMenu()
  19. {
  20. using (Application())
  21. {
  22. popupImpl.Setup(x => x.Show()).Verifiable();
  23. popupImpl.Setup(x => x.Hide()).Verifiable();
  24. var sut = new ContextMenu();
  25. var target = new Panel
  26. {
  27. ContextMenu = sut
  28. };
  29. new Window { Content = target };
  30. _mouse.Click(target, MouseButton.Right);
  31. Assert.True(sut.IsOpen);
  32. _mouse.Click(target);
  33. Assert.False(sut.IsOpen);
  34. popupImpl.Verify(x => x.Show(), Times.Once);
  35. popupImpl.Verify(x => x.Hide(), Times.Once);
  36. }
  37. }
  38. [Fact]
  39. public void Right_Clicking_On_Control_Twice_Re_Opens_ContextMenu()
  40. {
  41. using (Application())
  42. {
  43. popupImpl.Setup(x => x.Show()).Verifiable();
  44. popupImpl.Setup(x => x.Hide()).Verifiable();
  45. var sut = new ContextMenu();
  46. var target = new Panel
  47. {
  48. ContextMenu = sut
  49. };
  50. var window = new Window { Content = target };
  51. _mouse.Click(target, MouseButton.Right);
  52. Assert.True(sut.IsOpen);
  53. _mouse.Click(target, MouseButton.Right);
  54. Assert.True(sut.IsOpen);
  55. popupImpl.Verify(x => x.Hide(), Times.Once);
  56. popupImpl.Verify(x => x.Show(), Times.Exactly(2));
  57. }
  58. }
  59. [Fact]
  60. public void Cancelling_Opening_Does_Not_Show_ContextMenu()
  61. {
  62. using (Application())
  63. {
  64. popupImpl.Setup(x => x.Show()).Verifiable();
  65. bool eventCalled = false;
  66. var sut = new ContextMenu();
  67. var target = new Panel
  68. {
  69. ContextMenu = sut
  70. };
  71. new Window { Content = target };
  72. sut.ContextMenuOpening += (c, e) => { eventCalled = true; e.Cancel = true; };
  73. _mouse.Click(target, MouseButton.Right);
  74. Assert.True(eventCalled);
  75. Assert.False(sut.IsOpen);
  76. popupImpl.Verify(x => x.Show(), Times.Never);
  77. }
  78. }
  79. [Fact]
  80. public void Cancelling_Closing_Leaves_ContextMenuOpen()
  81. {
  82. using (Application())
  83. {
  84. popupImpl.Setup(x => x.Show()).Verifiable();
  85. popupImpl.Setup(x => x.Hide()).Verifiable();
  86. bool eventCalled = false;
  87. var sut = new ContextMenu();
  88. var target = new Panel
  89. {
  90. ContextMenu = sut
  91. };
  92. new Window { Content = target };
  93. sut.ContextMenuClosing += (c, e) => { eventCalled = true; e.Cancel = true; };
  94. _mouse.Click(target, MouseButton.Right);
  95. Assert.True(sut.IsOpen);
  96. _mouse.Click(target, MouseButton.Right);
  97. Assert.True(eventCalled);
  98. Assert.True(sut.IsOpen);
  99. popupImpl.Verify(x => x.Show(), Times.Once());
  100. popupImpl.Verify(x => x.Hide(), Times.Never);
  101. }
  102. }
  103. private IDisposable Application()
  104. {
  105. var screen = new PixelRect(new PixelPoint(), new PixelSize(100, 100));
  106. var screenImpl = new Mock<IScreenImpl>();
  107. screenImpl.Setup(x => x.ScreenCount).Returns(1);
  108. screenImpl.Setup(X => X.AllScreens).Returns( new[] { new Screen(screen, screen, true) });
  109. var windowImpl = new Mock<IWindowImpl>();
  110. windowImpl.Setup(x => x.Screen).Returns(screenImpl.Object);
  111. popupImpl = new Mock<IPopupImpl>();
  112. popupImpl.SetupGet(x => x.Scaling).Returns(1);
  113. var services = TestServices.StyledWindow.With(
  114. inputManager: new InputManager(),
  115. windowImpl: windowImpl.Object,
  116. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object, () => popupImpl.Object));
  117. return UnitTestApplication.Start(services);
  118. }
  119. }
  120. }