ContextMenuTests.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using System;
  2. using Avalonia.Input;
  3. using Avalonia.Markup.Xaml;
  4. using Avalonia.Markup.Xaml.MarkupExtensions;
  5. using Avalonia.Platform;
  6. using Avalonia.UnitTests;
  7. using Castle.DynamicProxy.Generators;
  8. using Moq;
  9. using Xunit;
  10. namespace Avalonia.Controls.UnitTests
  11. {
  12. public class ContextMenuTests
  13. {
  14. private Mock<IPopupImpl> popupImpl;
  15. private MouseTestHelper _mouse = new MouseTestHelper();
  16. [Fact]
  17. public void Opening_Raises_Single_Opened_Event()
  18. {
  19. using (Application())
  20. {
  21. var sut = new ContextMenu();
  22. var target = new Panel
  23. {
  24. ContextMenu = sut
  25. };
  26. var window = new Window { Content = target };
  27. window.ApplyTemplate();
  28. window.Presenter.ApplyTemplate();
  29. int openedCount = 0;
  30. sut.MenuOpened += (sender, args) =>
  31. {
  32. openedCount++;
  33. };
  34. sut.Open(target);
  35. Assert.Equal(1, openedCount);
  36. }
  37. }
  38. [Fact]
  39. public void Closing_Raises_Single_Closed_Event()
  40. {
  41. using (Application())
  42. {
  43. var sut = new ContextMenu();
  44. var target = new Panel
  45. {
  46. ContextMenu = sut
  47. };
  48. var window = new Window { Content = target };
  49. window.ApplyTemplate();
  50. window.Presenter.ApplyTemplate();
  51. sut.Open(target);
  52. int closedCount = 0;
  53. sut.MenuClosed += (sender, args) =>
  54. {
  55. closedCount++;
  56. };
  57. sut.Close();
  58. Assert.Equal(1, closedCount);
  59. }
  60. }
  61. [Fact]
  62. public void Clicking_On_Control_Toggles_ContextMenu()
  63. {
  64. using (Application())
  65. {
  66. popupImpl.Setup(x => x.Show()).Verifiable();
  67. popupImpl.Setup(x => x.Hide()).Verifiable();
  68. var sut = new ContextMenu();
  69. var target = new Panel
  70. {
  71. ContextMenu = sut
  72. };
  73. var window = new Window {Content = target};
  74. window.ApplyTemplate();
  75. window.Presenter.ApplyTemplate();
  76. _mouse.Click(target, MouseButton.Right);
  77. Assert.True(sut.IsOpen);
  78. _mouse.Click(target);
  79. Assert.False(sut.IsOpen);
  80. popupImpl.Verify(x => x.Show(), Times.Once);
  81. popupImpl.Verify(x => x.Hide(), Times.Once);
  82. }
  83. }
  84. [Fact]
  85. public void Right_Clicking_On_Control_Twice_Re_Opens_ContextMenu()
  86. {
  87. using (Application())
  88. {
  89. popupImpl.Setup(x => x.Show()).Verifiable();
  90. popupImpl.Setup(x => x.Hide()).Verifiable();
  91. var sut = new ContextMenu();
  92. var target = new Panel
  93. {
  94. ContextMenu = sut
  95. };
  96. var window = new Window {Content = target};
  97. window.ApplyTemplate();
  98. window.Presenter.ApplyTemplate();
  99. _mouse.Click(target, MouseButton.Right);
  100. Assert.True(sut.IsOpen);
  101. _mouse.Click(target, MouseButton.Right);
  102. Assert.True(sut.IsOpen);
  103. popupImpl.Verify(x => x.Hide(), Times.Once);
  104. popupImpl.Verify(x => x.Show(), Times.Exactly(2));
  105. }
  106. }
  107. [Fact]
  108. public void Cancelling_Opening_Does_Not_Show_ContextMenu()
  109. {
  110. using (Application())
  111. {
  112. popupImpl.Setup(x => x.Show()).Verifiable();
  113. bool eventCalled = false;
  114. var sut = new ContextMenu();
  115. var target = new Panel
  116. {
  117. ContextMenu = sut
  118. };
  119. new Window { Content = target };
  120. sut.ContextMenuOpening += (c, e) => { eventCalled = true; e.Cancel = true; };
  121. _mouse.Click(target, MouseButton.Right);
  122. Assert.True(eventCalled);
  123. Assert.False(sut.IsOpen);
  124. popupImpl.Verify(x => x.Show(), Times.Never);
  125. }
  126. }
  127. [Fact]
  128. public void Can_Set_Clear_ContextMenu_Property()
  129. {
  130. using (Application())
  131. {
  132. var target = new ContextMenu();
  133. var control = new Panel();
  134. control.ContextMenu = target;
  135. control.ContextMenu = null;
  136. }
  137. }
  138. [Fact]
  139. public void Context_Menu_In_Resources_Can_Be_Shared()
  140. {
  141. using (Application())
  142. {
  143. var xaml = @"
  144. <Window xmlns='https://github.com/avaloniaui'
  145. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  146. <Window.Resources>
  147. <ContextMenu x:Key='contextMenu'>
  148. <MenuItem>Foo</MenuItem>
  149. </ContextMenu>
  150. </Window.Resources>
  151. <StackPanel>
  152. <TextBlock Name='target1' ContextMenu='{StaticResource contextMenu}'/>
  153. <TextBlock Name='target2' ContextMenu='{StaticResource contextMenu}'/>
  154. </StackPanel>
  155. </Window>";
  156. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  157. var target1 = window.Find<TextBlock>("target1");
  158. var target2 = window.Find<TextBlock>("target2");
  159. var mouse = new MouseTestHelper();
  160. Assert.NotNull(target1.ContextMenu);
  161. Assert.NotNull(target2.ContextMenu);
  162. Assert.Same(target1.ContextMenu, target2.ContextMenu);
  163. window.Show();
  164. var menu = target1.ContextMenu;
  165. mouse.Click(target1, MouseButton.Right);
  166. Assert.True(menu.IsOpen);
  167. mouse.Click(target2, MouseButton.Right);
  168. Assert.True(menu.IsOpen);
  169. }
  170. }
  171. [Fact]
  172. public void Context_Menu_Can_Be_Set_In_Style()
  173. {
  174. using (Application())
  175. {
  176. var xaml = @"
  177. <Window xmlns='https://github.com/avaloniaui'
  178. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  179. <Window.Styles>
  180. <Style Selector='TextBlock'>
  181. <Setter Property='ContextMenu'>
  182. <ContextMenu>
  183. <MenuItem>Foo</MenuItem>
  184. </ContextMenu>
  185. </Setter>
  186. </Style>
  187. </Window.Styles>
  188. <StackPanel>
  189. <TextBlock Name='target1'/>
  190. <TextBlock Name='target2'/>
  191. </StackPanel>
  192. </Window>";
  193. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  194. var target1 = window.Find<TextBlock>("target1");
  195. var target2 = window.Find<TextBlock>("target2");
  196. var mouse = new MouseTestHelper();
  197. Assert.NotNull(target1.ContextMenu);
  198. Assert.NotNull(target2.ContextMenu);
  199. Assert.Same(target1.ContextMenu, target2.ContextMenu);
  200. window.Show();
  201. var menu = target1.ContextMenu;
  202. mouse.Click(target1, MouseButton.Right);
  203. Assert.True(menu.IsOpen);
  204. mouse.Click(target2, MouseButton.Right);
  205. Assert.True(menu.IsOpen);
  206. }
  207. }
  208. [Fact(Skip = "The only reason this test was 'passing' before was that the author forgot to call Window.ApplyTemplate()")]
  209. public void Cancelling_Closing_Leaves_ContextMenuOpen()
  210. {
  211. using (Application())
  212. {
  213. popupImpl.Setup(x => x.Show()).Verifiable();
  214. popupImpl.Setup(x => x.Hide()).Verifiable();
  215. bool eventCalled = false;
  216. var sut = new ContextMenu();
  217. var target = new Panel
  218. {
  219. ContextMenu = sut
  220. };
  221. var window = new Window {Content = target};
  222. window.ApplyTemplate();
  223. sut.ContextMenuClosing += (c, e) => { eventCalled = true; e.Cancel = true; };
  224. _mouse.Click(target, MouseButton.Right);
  225. Assert.True(sut.IsOpen);
  226. _mouse.Click(target, MouseButton.Right);
  227. Assert.True(eventCalled);
  228. Assert.True(sut.IsOpen);
  229. popupImpl.Verify(x => x.Show(), Times.Once());
  230. popupImpl.Verify(x => x.Hide(), Times.Never);
  231. }
  232. }
  233. private IDisposable Application()
  234. {
  235. var screen = new PixelRect(new PixelPoint(), new PixelSize(100, 100));
  236. var screenImpl = new Mock<IScreenImpl>();
  237. screenImpl.Setup(x => x.ScreenCount).Returns(1);
  238. screenImpl.Setup(X => X.AllScreens).Returns( new[] { new Screen(1, screen, screen, true) });
  239. var windowImpl = MockWindowingPlatform.CreateWindowMock();
  240. popupImpl = MockWindowingPlatform.CreatePopupMock(windowImpl.Object);
  241. popupImpl.SetupGet(x => x.RenderScaling).Returns(1);
  242. windowImpl.Setup(x => x.CreatePopup()).Returns(popupImpl.Object);
  243. windowImpl.Setup(x => x.Screen).Returns(screenImpl.Object);
  244. var services = TestServices.StyledWindow.With(
  245. inputManager: new InputManager(),
  246. windowImpl: windowImpl.Object,
  247. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object, x => popupImpl.Object));
  248. return UnitTestApplication.Start(services);
  249. }
  250. }
  251. }