ContextMenuTests.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 Opening_Raises_Single_Opened_Event()
  19. {
  20. using (Application())
  21. {
  22. var sut = new ContextMenu();
  23. var target = new Panel
  24. {
  25. ContextMenu = sut
  26. };
  27. new Window { Content = target };
  28. int openedCount = 0;
  29. sut.MenuOpened += (sender, args) =>
  30. {
  31. openedCount++;
  32. };
  33. sut.Open(null);
  34. Assert.Equal(1, openedCount);
  35. }
  36. }
  37. [Fact]
  38. public void Closing_Raises_Single_Closed_Event()
  39. {
  40. using (Application())
  41. {
  42. var sut = new ContextMenu();
  43. var target = new Panel
  44. {
  45. ContextMenu = sut
  46. };
  47. new Window { Content = target };
  48. sut.Open(null);
  49. int closedCount = 0;
  50. sut.MenuClosed += (sender, args) =>
  51. {
  52. closedCount++;
  53. };
  54. sut.Close();
  55. Assert.Equal(1, closedCount);
  56. }
  57. }
  58. [Fact]
  59. public void Clicking_On_Control_Toggles_ContextMenu()
  60. {
  61. using (Application())
  62. {
  63. popupImpl.Setup(x => x.Show()).Verifiable();
  64. popupImpl.Setup(x => x.Hide()).Verifiable();
  65. var sut = new ContextMenu();
  66. var target = new Panel
  67. {
  68. ContextMenu = sut
  69. };
  70. new Window { Content = target };
  71. _mouse.Click(target, MouseButton.Right);
  72. Assert.True(sut.IsOpen);
  73. _mouse.Click(target);
  74. Assert.False(sut.IsOpen);
  75. popupImpl.Verify(x => x.Show(), Times.Once);
  76. popupImpl.Verify(x => x.Hide(), Times.Once);
  77. }
  78. }
  79. [Fact]
  80. public void Right_Clicking_On_Control_Twice_Re_Opens_ContextMenu()
  81. {
  82. using (Application())
  83. {
  84. popupImpl.Setup(x => x.Show()).Verifiable();
  85. popupImpl.Setup(x => x.Hide()).Verifiable();
  86. var sut = new ContextMenu();
  87. var target = new Panel
  88. {
  89. ContextMenu = sut
  90. };
  91. var window = new Window { Content = target };
  92. _mouse.Click(target, MouseButton.Right);
  93. Assert.True(sut.IsOpen);
  94. _mouse.Click(target, MouseButton.Right);
  95. Assert.True(sut.IsOpen);
  96. popupImpl.Verify(x => x.Hide(), Times.Once);
  97. popupImpl.Verify(x => x.Show(), Times.Exactly(2));
  98. }
  99. }
  100. [Fact]
  101. public void Cancelling_Opening_Does_Not_Show_ContextMenu()
  102. {
  103. using (Application())
  104. {
  105. popupImpl.Setup(x => x.Show()).Verifiable();
  106. bool eventCalled = false;
  107. var sut = new ContextMenu();
  108. var target = new Panel
  109. {
  110. ContextMenu = sut
  111. };
  112. new Window { Content = target };
  113. sut.ContextMenuOpening += (c, e) => { eventCalled = true; e.Cancel = true; };
  114. _mouse.Click(target, MouseButton.Right);
  115. Assert.True(eventCalled);
  116. Assert.False(sut.IsOpen);
  117. popupImpl.Verify(x => x.Show(), Times.Never);
  118. }
  119. }
  120. [Fact]
  121. public void Cancelling_Closing_Leaves_ContextMenuOpen()
  122. {
  123. using (Application())
  124. {
  125. popupImpl.Setup(x => x.Show()).Verifiable();
  126. popupImpl.Setup(x => x.Hide()).Verifiable();
  127. bool eventCalled = false;
  128. var sut = new ContextMenu();
  129. var target = new Panel
  130. {
  131. ContextMenu = sut
  132. };
  133. new Window { Content = target };
  134. sut.ContextMenuClosing += (c, e) => { eventCalled = true; e.Cancel = true; };
  135. _mouse.Click(target, MouseButton.Right);
  136. Assert.True(sut.IsOpen);
  137. _mouse.Click(target, MouseButton.Right);
  138. Assert.True(eventCalled);
  139. Assert.True(sut.IsOpen);
  140. popupImpl.Verify(x => x.Show(), Times.Once());
  141. popupImpl.Verify(x => x.Hide(), Times.Never);
  142. }
  143. }
  144. private IDisposable Application()
  145. {
  146. var screen = new PixelRect(new PixelPoint(), new PixelSize(100, 100));
  147. var screenImpl = new Mock<IScreenImpl>();
  148. screenImpl.Setup(x => x.ScreenCount).Returns(1);
  149. screenImpl.Setup(X => X.AllScreens).Returns( new[] { new Screen(screen, screen, true) });
  150. var windowImpl = new Mock<IWindowImpl>();
  151. windowImpl.Setup(x => x.Screen).Returns(screenImpl.Object);
  152. popupImpl = new Mock<IPopupImpl>();
  153. popupImpl.SetupGet(x => x.Scaling).Returns(1);
  154. var services = TestServices.StyledWindow.With(
  155. inputManager: new InputManager(),
  156. windowImpl: windowImpl.Object,
  157. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object, () => popupImpl.Object));
  158. return UnitTestApplication.Start(services);
  159. }
  160. }
  161. }