ContextMenuTests.cs 6.6 KB

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