ContextMenuTests.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 }.ApplyTemplate();
  28. int openedCount = 0;
  29. sut.MenuOpened += (sender, args) =>
  30. {
  31. openedCount++;
  32. };
  33. sut.Open(target);
  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 }.ApplyTemplate();
  48. sut.Open(target);
  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. var window = new Window {Content = target};
  71. window.ApplyTemplate();
  72. _mouse.Click(target, MouseButton.Right);
  73. Assert.True(sut.IsOpen);
  74. _mouse.Click(target);
  75. Assert.False(sut.IsOpen);
  76. popupImpl.Verify(x => x.Show(), Times.Once);
  77. popupImpl.Verify(x => x.Hide(), Times.Once);
  78. }
  79. }
  80. [Fact]
  81. public void Right_Clicking_On_Control_Twice_Re_Opens_ContextMenu()
  82. {
  83. using (Application())
  84. {
  85. popupImpl.Setup(x => x.Show()).Verifiable();
  86. popupImpl.Setup(x => x.Hide()).Verifiable();
  87. var sut = new ContextMenu();
  88. var target = new Panel
  89. {
  90. ContextMenu = sut
  91. };
  92. var window = new Window {Content = target};
  93. window.ApplyTemplate();
  94. _mouse.Click(target, MouseButton.Right);
  95. Assert.True(sut.IsOpen);
  96. _mouse.Click(target, MouseButton.Right);
  97. Assert.True(sut.IsOpen);
  98. popupImpl.Verify(x => x.Hide(), Times.Once);
  99. popupImpl.Verify(x => x.Show(), Times.Exactly(2));
  100. }
  101. }
  102. [Fact]
  103. public void Cancelling_Opening_Does_Not_Show_ContextMenu()
  104. {
  105. using (Application())
  106. {
  107. popupImpl.Setup(x => x.Show()).Verifiable();
  108. bool eventCalled = false;
  109. var sut = new ContextMenu();
  110. var target = new Panel
  111. {
  112. ContextMenu = sut
  113. };
  114. new Window { Content = target };
  115. sut.ContextMenuOpening += (c, e) => { eventCalled = true; e.Cancel = true; };
  116. _mouse.Click(target, MouseButton.Right);
  117. Assert.True(eventCalled);
  118. Assert.False(sut.IsOpen);
  119. popupImpl.Verify(x => x.Show(), Times.Never);
  120. }
  121. }
  122. [Fact(Skip = "The only reason this test was 'passing' before was that the author forgot to call Window.ApplyTemplate()")]
  123. public void Cancelling_Closing_Leaves_ContextMenuOpen()
  124. {
  125. using (Application())
  126. {
  127. popupImpl.Setup(x => x.Show()).Verifiable();
  128. popupImpl.Setup(x => x.Hide()).Verifiable();
  129. bool eventCalled = false;
  130. var sut = new ContextMenu();
  131. var target = new Panel
  132. {
  133. ContextMenu = sut
  134. };
  135. var window = new Window {Content = target};
  136. window.ApplyTemplate();
  137. sut.ContextMenuClosing += (c, e) => { eventCalled = true; e.Cancel = true; };
  138. _mouse.Click(target, MouseButton.Right);
  139. Assert.True(sut.IsOpen);
  140. _mouse.Click(target, MouseButton.Right);
  141. Assert.True(eventCalled);
  142. Assert.True(sut.IsOpen);
  143. popupImpl.Verify(x => x.Show(), Times.Once());
  144. popupImpl.Verify(x => x.Hide(), Times.Never);
  145. }
  146. }
  147. private IDisposable Application()
  148. {
  149. var screen = new PixelRect(new PixelPoint(), new PixelSize(100, 100));
  150. var screenImpl = new Mock<IScreenImpl>();
  151. screenImpl.Setup(x => x.ScreenCount).Returns(1);
  152. screenImpl.Setup(X => X.AllScreens).Returns( new[] { new Screen(screen, screen, true) });
  153. popupImpl = MockWindowingPlatform.CreatePopupMock();
  154. popupImpl.SetupGet(x => x.Scaling).Returns(1);
  155. var windowImpl = MockWindowingPlatform.CreateWindowMock(() => popupImpl.Object);
  156. windowImpl.Setup(x => x.Screen).Returns(screenImpl.Object);
  157. var services = TestServices.StyledWindow.With(
  158. inputManager: new InputManager(),
  159. windowImpl: windowImpl.Object,
  160. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object, () => popupImpl.Object));
  161. return UnitTestApplication.Start(services);
  162. }
  163. }
  164. }