ContextMenuTests.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. using System;
  2. using Avalonia.Controls.Primitives;
  3. using Avalonia.Input;
  4. using Avalonia.Markup.Xaml;
  5. using Avalonia.Platform;
  6. using Avalonia.Rendering;
  7. using Avalonia.UnitTests;
  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 ContextRequested_Opens_ContextMenu()
  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. target.RaiseEvent(new ContextRequestedEventArgs());
  35. Assert.True(sut.IsOpen);
  36. Assert.Equal(1, openedCount);
  37. }
  38. }
  39. [Fact]
  40. public void ContextMenu_Is_Opened_When_ContextFlyout_Is_Also_Set()
  41. {
  42. // We have this test for backwards compatability with the code that already sets custom ContextMenu.
  43. using (Application())
  44. {
  45. var sut = new ContextMenu();
  46. var flyout = new Flyout();
  47. var target = new Panel
  48. {
  49. ContextMenu = sut,
  50. ContextFlyout = flyout
  51. };
  52. var window = new Window { Content = target };
  53. window.ApplyTemplate();
  54. window.Presenter.ApplyTemplate();
  55. target.RaiseEvent(new ContextRequestedEventArgs());
  56. Assert.True(sut.IsOpen);
  57. Assert.False(flyout.IsOpen);
  58. }
  59. }
  60. [Fact]
  61. public void KeyUp_Raised_On_Target_Opens_ContextFlyout()
  62. {
  63. using (Application())
  64. {
  65. var sut = new ContextMenu();
  66. var target = new Panel
  67. {
  68. ContextMenu = sut
  69. };
  70. var contextRequestedCount = 0;
  71. target.AddHandler(Control.ContextRequestedEvent, (s, a) => contextRequestedCount++, Interactivity.RoutingStrategies.Tunnel);
  72. var window = PreparedWindow(target);
  73. window.Show();
  74. target.RaiseEvent(new KeyEventArgs { RoutedEvent = InputElement.KeyUpEvent, Key = Key.Apps, Source = window });
  75. Assert.True(sut.IsOpen);
  76. Assert.Equal(1, contextRequestedCount);
  77. }
  78. }
  79. [Fact]
  80. public void KeyUp_Raised_On_Flyout_Closes_Opened_ContextMenu()
  81. {
  82. using (Application())
  83. {
  84. var sut = new ContextMenu();
  85. var target = new Panel
  86. {
  87. ContextMenu = sut
  88. };
  89. var window = PreparedWindow(target);
  90. window.Show();
  91. target.RaiseEvent(new ContextRequestedEventArgs());
  92. Assert.True(sut.IsOpen);
  93. sut.RaiseEvent(new KeyEventArgs { RoutedEvent = InputElement.KeyUpEvent, Key = Key.Apps, Source = window });
  94. Assert.False(sut.IsOpen);
  95. }
  96. }
  97. [Fact]
  98. public void Opening_Raises_Single_Opened_Event()
  99. {
  100. using (Application())
  101. {
  102. var sut = new ContextMenu();
  103. var target = new Panel
  104. {
  105. ContextMenu = sut
  106. };
  107. var window = new Window { Content = target };
  108. window.ApplyTemplate();
  109. window.Presenter.ApplyTemplate();
  110. int openedCount = 0;
  111. sut.MenuOpened += (sender, args) =>
  112. {
  113. openedCount++;
  114. };
  115. sut.Open(target);
  116. Assert.Equal(1, openedCount);
  117. }
  118. }
  119. [Fact]
  120. public void Open_Should_Use_Default_Control()
  121. {
  122. using (Application())
  123. {
  124. var sut = new ContextMenu();
  125. var target = new Panel
  126. {
  127. ContextMenu = sut
  128. };
  129. var window = new Window { Content = target };
  130. window.ApplyTemplate();
  131. window.Presenter.ApplyTemplate();
  132. bool opened = false;
  133. sut.MenuOpened += (sender, args) =>
  134. {
  135. opened = true;
  136. };
  137. sut.Open();
  138. Assert.True(opened);
  139. }
  140. }
  141. [Fact]
  142. public void Open_Should_Raise_Exception_If_AlreadyDetached()
  143. {
  144. using (Application())
  145. {
  146. var sut = new ContextMenu();
  147. var target = new Panel
  148. {
  149. ContextMenu = sut
  150. };
  151. var window = new Window { Content = target };
  152. window.ApplyTemplate();
  153. window.Presenter.ApplyTemplate();
  154. target.ContextMenu = null;
  155. Assert.ThrowsAny<Exception>(()=> sut.Open());
  156. }
  157. }
  158. [Fact]
  159. public void Closing_Raises_Single_Closed_Event()
  160. {
  161. using (Application())
  162. {
  163. var sut = new ContextMenu();
  164. var target = new Panel
  165. {
  166. ContextMenu = sut
  167. };
  168. var window = new Window { Content = target };
  169. window.ApplyTemplate();
  170. window.Presenter.ApplyTemplate();
  171. sut.Open(target);
  172. int closedCount = 0;
  173. sut.MenuClosed += (sender, args) =>
  174. {
  175. closedCount++;
  176. };
  177. sut.Close();
  178. Assert.Equal(1, closedCount);
  179. }
  180. }
  181. [Fact]
  182. public void Cancel_Light_Dismiss_Closing_Keeps_Flyout_Open()
  183. {
  184. using (Application())
  185. {
  186. popupImpl.Setup(x => x.Show(true, false)).Verifiable();
  187. popupImpl.Setup(x => x.Hide()).Verifiable();
  188. var window = PreparedWindow();
  189. window.Width = 100;
  190. window.Height = 100;
  191. var button = new Button
  192. {
  193. Height = 10,
  194. Width = 10,
  195. HorizontalAlignment = Layout.HorizontalAlignment.Left,
  196. VerticalAlignment = Layout.VerticalAlignment.Top
  197. };
  198. window.Content = button;
  199. window.ApplyTemplate();
  200. window.Show();
  201. var tracker = 0;
  202. var c = new ContextMenu();
  203. c.ContextMenuClosing += (s, e) =>
  204. {
  205. tracker++;
  206. e.Cancel = true;
  207. };
  208. button.ContextMenu = c;
  209. c.Open(button);
  210. var overlay = LightDismissOverlayLayer.GetLightDismissOverlayLayer(window);
  211. _mouse.Down(overlay, MouseButton.Left, new Point(90, 90));
  212. _mouse.Up(button, MouseButton.Left, new Point(90, 90));
  213. Assert.Equal(1, tracker);
  214. Assert.True(c.IsOpen);
  215. popupImpl.Verify(x => x.Hide(), Times.Never);
  216. popupImpl.Verify(x => x.Show(true, false), Times.Exactly(1));
  217. }
  218. }
  219. [Fact]
  220. public void Light_Dismiss_Closes_Flyout()
  221. {
  222. using (Application())
  223. {
  224. popupImpl.Setup(x => x.Show(true, false)).Verifiable();
  225. popupImpl.Setup(x => x.Hide()).Verifiable();
  226. var window = PreparedWindow();
  227. window.Width = 100;
  228. window.Height = 100;
  229. var button = new Button
  230. {
  231. Height = 10,
  232. Width = 10,
  233. HorizontalAlignment = Layout.HorizontalAlignment.Left,
  234. VerticalAlignment = Layout.VerticalAlignment.Top
  235. };
  236. window.Content = button;
  237. window.ApplyTemplate();
  238. window.Show();
  239. var c = new ContextMenu();
  240. c.PlacementMode = PlacementMode.Bottom;
  241. c.Open(button);
  242. var overlay = LightDismissOverlayLayer.GetLightDismissOverlayLayer(window);
  243. _mouse.Down(overlay, MouseButton.Left, new Point(90, 90));
  244. _mouse.Up(button, MouseButton.Left, new Point(90, 90));
  245. Assert.False(c.IsOpen);
  246. popupImpl.Verify(x => x.Hide(), Times.Exactly(1));
  247. popupImpl.Verify(x => x.Show(true, false), Times.Exactly(1));
  248. }
  249. }
  250. [Fact]
  251. public void Clicking_On_Control_Toggles_ContextMenu()
  252. {
  253. using (Application())
  254. {
  255. popupImpl.Setup(x => x.Show(true, false)).Verifiable();
  256. popupImpl.Setup(x => x.Hide()).Verifiable();
  257. var sut = new ContextMenu();
  258. var target = new Panel
  259. {
  260. ContextMenu = sut
  261. };
  262. var window = PreparedWindow(target);
  263. window.Show();
  264. var overlay = LightDismissOverlayLayer.GetLightDismissOverlayLayer(window);
  265. _mouse.Click(target, MouseButton.Right);
  266. Assert.True(sut.IsOpen);
  267. _mouse.Down(overlay);
  268. _mouse.Up(target);
  269. Assert.False(sut.IsOpen);
  270. popupImpl.Verify(x => x.Show(true, false), Times.Once);
  271. popupImpl.Verify(x => x.Hide(), Times.Once);
  272. }
  273. }
  274. [Fact]
  275. public void Right_Clicking_On_Control_Twice_Re_Opens_ContextMenu()
  276. {
  277. using (Application())
  278. {
  279. popupImpl.Setup(x => x.Show(true, false)).Verifiable();
  280. popupImpl.Setup(x => x.Hide()).Verifiable();
  281. var sut = new ContextMenu();
  282. var target = new Panel
  283. {
  284. ContextMenu = sut
  285. };
  286. var window = PreparedWindow(target);
  287. window.Show();
  288. var overlay = LightDismissOverlayLayer.GetLightDismissOverlayLayer(window);
  289. _mouse.Click(target, MouseButton.Right);
  290. Assert.True(sut.IsOpen);
  291. _mouse.Down(overlay, MouseButton.Right);
  292. _mouse.Up(target, MouseButton.Right);
  293. Assert.True(sut.IsOpen);
  294. popupImpl.Verify(x => x.Hide(), Times.Once);
  295. popupImpl.Verify(x => x.Show(true, false), Times.Exactly(2));
  296. }
  297. }
  298. [Fact]
  299. public void Context_Menu_Can_Be_Shared_Between_Controls_Even_After_A_Control_Is_Removed_From_Visual_Tree()
  300. {
  301. using (Application())
  302. {
  303. var sut = new ContextMenu();
  304. var target1 = new Panel
  305. {
  306. ContextMenu = sut
  307. };
  308. var target2 = new Panel
  309. {
  310. ContextMenu = sut
  311. };
  312. var sp = new StackPanel { Children = { target1, target2 } };
  313. var window = new Window { Content = sp };
  314. window.ApplyTemplate();
  315. window.Presenter.ApplyTemplate();
  316. _mouse.Click(target1, MouseButton.Right);
  317. Assert.True(sut.IsOpen);
  318. sp.Children.Remove(target1);
  319. Assert.False(sut.IsOpen);
  320. _mouse.Click(target2, MouseButton.Right);
  321. Assert.True(sut.IsOpen);
  322. }
  323. }
  324. [Fact]
  325. public void Cancelling_Opening_Does_Not_Show_ContextMenu()
  326. {
  327. using (Application())
  328. {
  329. popupImpl.Setup(x => x.Show(true, false)).Verifiable();
  330. bool eventCalled = false;
  331. var sut = new ContextMenu();
  332. var target = new Panel
  333. {
  334. ContextMenu = sut
  335. };
  336. new Window { Content = target };
  337. sut.ContextMenuOpening += (c, e) => { eventCalled = true; e.Cancel = true; };
  338. _mouse.Click(target, MouseButton.Right);
  339. Assert.True(eventCalled);
  340. Assert.False(sut.IsOpen);
  341. popupImpl.Verify(x => x.Show(true, false), Times.Never);
  342. }
  343. }
  344. [Fact]
  345. public void Can_Set_Clear_ContextMenu_Property()
  346. {
  347. using (Application())
  348. {
  349. var target = new ContextMenu();
  350. var control = new Panel();
  351. control.ContextMenu = target;
  352. control.ContextMenu = null;
  353. }
  354. }
  355. [Fact]
  356. public void Should_Reset_Popup_Parent_On_Target_Detached()
  357. {
  358. using (Application())
  359. {
  360. var userControl = new UserControl();
  361. var window = PreparedWindow(userControl);
  362. window.Show();
  363. var menu = new ContextMenu();
  364. userControl.ContextMenu = menu;
  365. menu.Open();
  366. var popup = Assert.IsType<Popup>(menu.Parent);
  367. Assert.NotNull(popup.Parent);
  368. window.Content = null;
  369. Assert.Null(popup.Parent);
  370. }
  371. }
  372. [Fact]
  373. public void Context_Menu_In_Resources_Can_Be_Shared()
  374. {
  375. using (Application())
  376. {
  377. var xaml = @"
  378. <Window xmlns='https://github.com/avaloniaui'
  379. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  380. <Window.Resources>
  381. <ContextMenu x:Key='contextMenu'>
  382. <MenuItem>Foo</MenuItem>
  383. </ContextMenu>
  384. </Window.Resources>
  385. <StackPanel>
  386. <TextBlock Name='target1' ContextMenu='{StaticResource contextMenu}'/>
  387. <TextBlock Name='target2' ContextMenu='{StaticResource contextMenu}'/>
  388. </StackPanel>
  389. </Window>";
  390. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  391. var target1 = window.Find<TextBlock>("target1");
  392. var target2 = window.Find<TextBlock>("target2");
  393. var mouse = new MouseTestHelper();
  394. Assert.NotNull(target1.ContextMenu);
  395. Assert.NotNull(target2.ContextMenu);
  396. Assert.Same(target1.ContextMenu, target2.ContextMenu);
  397. window.Show();
  398. var menu = target1.ContextMenu;
  399. mouse.Click(target1, MouseButton.Right);
  400. Assert.True(menu.IsOpen);
  401. mouse.Click(target2, MouseButton.Right);
  402. Assert.True(menu.IsOpen);
  403. }
  404. }
  405. [Fact]
  406. public void Context_Menu_Can_Be_Set_In_Style()
  407. {
  408. using (Application())
  409. {
  410. var xaml = @"
  411. <Window xmlns='https://github.com/avaloniaui'
  412. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  413. <Window.Styles>
  414. <Style Selector='TextBlock'>
  415. <Setter Property='ContextMenu'>
  416. <ContextMenu>
  417. <MenuItem>Foo</MenuItem>
  418. </ContextMenu>
  419. </Setter>
  420. </Style>
  421. </Window.Styles>
  422. <StackPanel>
  423. <TextBlock Name='target1'/>
  424. <TextBlock Name='target2'/>
  425. </StackPanel>
  426. </Window>";
  427. var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
  428. var target1 = window.Find<TextBlock>("target1");
  429. var target2 = window.Find<TextBlock>("target2");
  430. var mouse = new MouseTestHelper();
  431. Assert.NotNull(target1.ContextMenu);
  432. Assert.NotNull(target2.ContextMenu);
  433. Assert.Same(target1.ContextMenu, target2.ContextMenu);
  434. window.Show();
  435. var menu = target1.ContextMenu;
  436. mouse.Click(target1, MouseButton.Right);
  437. Assert.True(menu.IsOpen);
  438. mouse.Click(target2, MouseButton.Right);
  439. Assert.True(menu.IsOpen);
  440. }
  441. }
  442. [Fact]
  443. public void Cancelling_Closing_Leaves_ContextMenuOpen()
  444. {
  445. using (Application())
  446. {
  447. popupImpl.Setup(x => x.Show(true, false)).Verifiable();
  448. popupImpl.Setup(x => x.Hide()).Verifiable();
  449. bool eventCalled = false;
  450. var sut = new ContextMenu();
  451. var target = new Panel
  452. {
  453. ContextMenu = sut
  454. };
  455. var window = PreparedWindow(target);
  456. var overlay = LightDismissOverlayLayer.GetLightDismissOverlayLayer(window);
  457. sut.ContextMenuClosing += (c, e) => { eventCalled = true; e.Cancel = true; };
  458. window.Show();
  459. _mouse.Click(target, MouseButton.Right);
  460. Assert.True(sut.IsOpen);
  461. _mouse.Down(overlay, MouseButton.Right);
  462. _mouse.Up(target, MouseButton.Right);
  463. Assert.True(eventCalled);
  464. Assert.True(sut.IsOpen);
  465. popupImpl.Verify(x => x.Show(true, false), Times.Once());
  466. popupImpl.Verify(x => x.Hide(), Times.Never);
  467. }
  468. }
  469. private static Window PreparedWindow(object content = null)
  470. {
  471. var renderer = new Mock<IRenderer>();
  472. var platform = AvaloniaLocator.Current.GetService<IWindowingPlatform>();
  473. var windowImpl = Mock.Get(platform.CreateWindow());
  474. windowImpl.Setup(x => x.CreateRenderer(It.IsAny<IRenderRoot>())).Returns(renderer.Object);
  475. var w = new Window(windowImpl.Object) { Content = content };
  476. w.ApplyTemplate();
  477. w.Presenter.ApplyTemplate();
  478. return w;
  479. }
  480. private IDisposable Application()
  481. {
  482. var screen = new PixelRect(new PixelPoint(), new PixelSize(100, 100));
  483. var screenImpl = new Mock<IScreenImpl>();
  484. screenImpl.Setup(x => x.ScreenCount).Returns(1);
  485. screenImpl.Setup(X => X.AllScreens).Returns( new[] { new Screen(1, screen, screen, true) });
  486. var windowImpl = MockWindowingPlatform.CreateWindowMock();
  487. popupImpl = MockWindowingPlatform.CreatePopupMock(windowImpl.Object);
  488. popupImpl.SetupGet(x => x.RenderScaling).Returns(1);
  489. windowImpl.Setup(x => x.CreatePopup()).Returns(popupImpl.Object);
  490. windowImpl.Setup(x => x.Screen).Returns(screenImpl.Object);
  491. var services = TestServices.StyledWindow.With(
  492. inputManager: new InputManager(),
  493. windowImpl: windowImpl.Object,
  494. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object, x => popupImpl.Object));
  495. return UnitTestApplication.Start(services);
  496. }
  497. }
  498. }