MenuItemTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Input;
  5. using Avalonia.Collections;
  6. using Avalonia.Controls.Presenters;
  7. using Avalonia.Controls.Primitives;
  8. using Avalonia.Controls.Templates;
  9. using Avalonia.Data;
  10. using Avalonia.Input;
  11. using Avalonia.Platform;
  12. using Avalonia.UnitTests;
  13. using Avalonia.VisualTree;
  14. using Moq;
  15. using Xunit;
  16. namespace Avalonia.Controls.UnitTests
  17. {
  18. public class MenuItemTests
  19. {
  20. private Mock<IPopupImpl> popupImpl;
  21. [Fact]
  22. public void Header_Of_Minus_Should_Apply_Separator_Pseudoclass()
  23. {
  24. var target = new MenuItem { Header = "-" };
  25. Assert.True(target.Classes.Contains(":separator"));
  26. }
  27. [Fact]
  28. public void Separator_Item_Should_Set_Focusable_False()
  29. {
  30. var target = new MenuItem { Header = "-" };
  31. Assert.False(target.Focusable);
  32. }
  33. [Fact]
  34. public void MenuItem_Is_Disabled_When_Command_Is_Enabled_But_IsEnabled_Is_False()
  35. {
  36. var command = new TestCommand(true);
  37. var target = new MenuItem
  38. {
  39. IsEnabled = false,
  40. Command = command,
  41. };
  42. var root = new TestRoot { Child = target };
  43. Assert.False(((IInputElement)target).IsEffectivelyEnabled);
  44. }
  45. [Fact]
  46. public void MenuItem_Is_Disabled_When_Bound_Command_Doesnt_Exist()
  47. {
  48. var target = new MenuItem
  49. {
  50. [!MenuItem.CommandProperty] = new Binding("Command"),
  51. };
  52. Assert.True(target.IsEnabled);
  53. Assert.False(target.IsEffectivelyEnabled);
  54. }
  55. [Fact]
  56. public void MenuItem_Is_Disabled_When_Bound_Command_Is_Removed()
  57. {
  58. var viewModel = new
  59. {
  60. Command = new TestCommand(true),
  61. };
  62. var target = new MenuItem
  63. {
  64. DataContext = viewModel,
  65. [!MenuItem.CommandProperty] = new Binding("Command"),
  66. };
  67. Assert.True(target.IsEnabled);
  68. Assert.True(target.IsEffectivelyEnabled);
  69. target.DataContext = null;
  70. Assert.True(target.IsEnabled);
  71. Assert.False(target.IsEffectivelyEnabled);
  72. }
  73. [Fact]
  74. public void MenuItem_Is_Enabled_When_Added_To_Logical_Tree_And_Bound_Command_Is_Added()
  75. {
  76. var viewModel = new
  77. {
  78. Command = new TestCommand(true),
  79. };
  80. var target = new MenuItem
  81. {
  82. DataContext = new object(),
  83. [!MenuItem.CommandProperty] = new Binding("Command"),
  84. };
  85. var root = new TestRoot { Child = target };
  86. Assert.True(target.IsEnabled);
  87. Assert.False(target.IsEffectivelyEnabled);
  88. target.DataContext = viewModel;
  89. Assert.True(target.IsEnabled);
  90. Assert.True(target.IsEffectivelyEnabled);
  91. }
  92. [Fact]
  93. public void MenuItem_Is_Disabled_When_Disabled_Bound_Command_Is_Added()
  94. {
  95. var viewModel = new
  96. {
  97. Command = new TestCommand(false),
  98. };
  99. var target = new MenuItem
  100. {
  101. DataContext = new object(),
  102. [!MenuItem.CommandProperty] = new Binding("Command"),
  103. };
  104. Assert.True(target.IsEnabled);
  105. Assert.False(target.IsEffectivelyEnabled);
  106. target.DataContext = viewModel;
  107. Assert.True(target.IsEnabled);
  108. Assert.False(target.IsEffectivelyEnabled);
  109. }
  110. [Fact]
  111. public void MenuItem_Does_Not_Subscribe_To_Command_CanExecuteChanged_Until_Added_To_Logical_Tree()
  112. {
  113. var command = new TestCommand();
  114. var target = new MenuItem
  115. {
  116. Command = command,
  117. };
  118. Assert.Equal(0, command.SubscriptionCount);
  119. }
  120. [Fact]
  121. public void MenuItem_Subscribes_To_Command_CanExecuteChanged_When_Added_To_Logical_Tree()
  122. {
  123. var command = new TestCommand();
  124. var target = new MenuItem { Command = command };
  125. var root = new TestRoot { Child = target };
  126. Assert.Equal(1, command.SubscriptionCount);
  127. }
  128. [Fact]
  129. public void MenuItem_Unsubscribes_From_Command_CanExecuteChanged_When_Removed_From_Logical_Tree()
  130. {
  131. var command = new TestCommand();
  132. var target = new MenuItem { Command = command };
  133. var root = new TestRoot { Child = target };
  134. root.Child = null;
  135. Assert.Equal(0, command.SubscriptionCount);
  136. }
  137. [Fact]
  138. public void MenuItem_Invokes_CanExecute_When_Added_To_Logical_Tree_And_CommandParameter_Changed()
  139. {
  140. var command = new TestCommand(p => p is bool value && value);
  141. var target = new MenuItem { Command = command };
  142. var root = new TestRoot { Child = target };
  143. target.CommandParameter = true;
  144. Assert.True(target.IsEffectivelyEnabled);
  145. target.CommandParameter = false;
  146. Assert.False(target.IsEffectivelyEnabled);
  147. }
  148. [Fact]
  149. public void MenuItem_Does_Not_Invoke_CanExecute_When_ContextMenu_Closed()
  150. {
  151. using (Application())
  152. {
  153. var canExecuteCallCount = 0;
  154. var command = new TestCommand(_ =>
  155. {
  156. canExecuteCallCount++;
  157. return true;
  158. });
  159. var target = new MenuItem();
  160. var contextMenu = new ContextMenu { Items = { target } };
  161. var window = new Window { Content = new Panel { ContextMenu = contextMenu } };
  162. window.ApplyStyling();
  163. window.ApplyTemplate();
  164. window.Presenter.ApplyTemplate();
  165. Assert.True(target.IsEffectivelyEnabled);
  166. target.Command = command;
  167. Assert.Equal(0, canExecuteCallCount);
  168. target.CommandParameter = false;
  169. Assert.Equal(0, canExecuteCallCount);
  170. command.RaiseCanExecuteChanged();
  171. Assert.Equal(0, canExecuteCallCount);
  172. contextMenu.Open();
  173. Assert.Equal(2, canExecuteCallCount);//2 because popup is changing logical child
  174. command.RaiseCanExecuteChanged();
  175. Assert.Equal(3, canExecuteCallCount);
  176. target.CommandParameter = true;
  177. Assert.Equal(4, canExecuteCallCount);
  178. }
  179. }
  180. [Fact]
  181. public void MenuItem_Does_Not_Invoke_CanExecute_When_MenuFlyout_Closed()
  182. {
  183. using (Application())
  184. {
  185. var canExecuteCallCount = 0;
  186. var command = new TestCommand(_ =>
  187. {
  188. canExecuteCallCount++;
  189. return true;
  190. });
  191. var target = new MenuItem();
  192. var flyout = new MenuFlyout { Items = new AvaloniaList<MenuItem> { target } };
  193. var button = new Button { Flyout = flyout };
  194. var window = new Window { Content = button };
  195. window.ApplyStyling();
  196. window.ApplyTemplate();
  197. window.Presenter.ApplyTemplate();
  198. Assert.True(target.IsEffectivelyEnabled);
  199. target.Command = command;
  200. Assert.Equal(0, canExecuteCallCount);
  201. target.CommandParameter = false;
  202. Assert.Equal(0, canExecuteCallCount);
  203. command.RaiseCanExecuteChanged();
  204. Assert.Equal(0, canExecuteCallCount);
  205. flyout.ShowAt(button);
  206. Assert.Equal(2, canExecuteCallCount);
  207. command.RaiseCanExecuteChanged();
  208. Assert.Equal(3, canExecuteCallCount);
  209. target.CommandParameter = true;
  210. Assert.Equal(4, canExecuteCallCount);
  211. }
  212. }
  213. [Fact]
  214. public void MenuItem_Does_Not_Invoke_CanExecute_When_Parent_MenuItem_Closed()
  215. {
  216. using (Application())
  217. {
  218. var canExecuteCallCount = 0;
  219. var command = new TestCommand(_ =>
  220. {
  221. canExecuteCallCount++;
  222. return true;
  223. });
  224. var target = new MenuItem();
  225. var parentMenuItem = new MenuItem { Items = { target } };
  226. var contextMenu = new ContextMenu { Items = { parentMenuItem } };
  227. var window = new Window { Content = new Panel { ContextMenu = contextMenu } };
  228. window.ApplyStyling();
  229. window.ApplyTemplate();
  230. window.Presenter.ApplyTemplate();
  231. contextMenu.Open();
  232. Assert.True(target.IsEffectivelyEnabled);
  233. target.Command = command;
  234. Assert.Equal(0, canExecuteCallCount);
  235. target.CommandParameter = false;
  236. Assert.Equal(0, canExecuteCallCount);
  237. command.RaiseCanExecuteChanged();
  238. Assert.Equal(0, canExecuteCallCount);
  239. try
  240. {
  241. parentMenuItem.IsSubMenuOpen = true;
  242. }
  243. catch (InvalidOperationException)
  244. {
  245. //popup host creation failed exception
  246. }
  247. Assert.Equal(1, canExecuteCallCount);
  248. command.RaiseCanExecuteChanged();
  249. Assert.Equal(2, canExecuteCallCount);
  250. target.CommandParameter = true;
  251. Assert.Equal(3, canExecuteCallCount);
  252. }
  253. }
  254. [Fact]
  255. public void TemplatedParent_Should_Not_Be_Applied_To_Submenus()
  256. {
  257. using (Application())
  258. {
  259. MenuItem topLevelMenu;
  260. MenuItem childMenu1;
  261. MenuItem childMenu2;
  262. var menu = new Menu
  263. {
  264. Items =
  265. {
  266. (topLevelMenu = new MenuItem
  267. {
  268. Header = "Foo",
  269. Items =
  270. {
  271. (childMenu1 = new MenuItem { Header = "Bar" }),
  272. (childMenu2 = new MenuItem { Header = "Baz" }),
  273. }
  274. }),
  275. }
  276. };
  277. var window = new Window { Content = menu };
  278. window.LayoutManager.ExecuteInitialLayoutPass();
  279. topLevelMenu.IsSubMenuOpen = true;
  280. Assert.True(childMenu1.IsAttachedToVisualTree);
  281. Assert.Null(childMenu1.TemplatedParent);
  282. Assert.Null(childMenu2.TemplatedParent);
  283. topLevelMenu.IsSubMenuOpen = false;
  284. topLevelMenu.IsSubMenuOpen = true;
  285. Assert.Null(childMenu1.TemplatedParent);
  286. Assert.Null(childMenu2.TemplatedParent);
  287. }
  288. }
  289. [Fact]
  290. public void Menu_ItemTemplate_Should_Be_Applied_To_TopLevel_MenuItem_Header()
  291. {
  292. using var app = Application();
  293. var items = new[]
  294. {
  295. new MenuViewModel("Foo"),
  296. new MenuViewModel("Bar"),
  297. };
  298. var itemTemplate = new FuncDataTemplate<MenuViewModel>((x, _) =>
  299. new TextBlock { Text = x.Header });
  300. var menu = new Menu
  301. {
  302. ItemTemplate = itemTemplate,
  303. ItemsSource = items,
  304. };
  305. var window = new Window { Content = menu };
  306. window.LayoutManager.ExecuteInitialLayoutPass();
  307. var panel = Assert.IsType<StackPanel>(menu.Presenter.Panel);
  308. Assert.Equal(2, panel.Children.Count);
  309. for (var i = 0; i < panel.Children.Count; i++)
  310. {
  311. var menuItem = Assert.IsType<MenuItem>(panel.Children[i]);
  312. Assert.Equal(items[i], menuItem.Header);
  313. Assert.Same(itemTemplate, menuItem.HeaderTemplate);
  314. var headerPresenter = Assert.IsType<ContentPresenter>(menuItem.HeaderPresenter);
  315. Assert.Same(itemTemplate, headerPresenter.ContentTemplate);
  316. var headerControl = Assert.IsType<TextBlock>(headerPresenter.Child);
  317. Assert.Equal(items[i].Header, headerControl.Text);
  318. }
  319. }
  320. private IDisposable Application()
  321. {
  322. var screen = new PixelRect(new PixelPoint(), new PixelSize(100, 100));
  323. var screenImpl = new Mock<IScreenImpl>();
  324. screenImpl.Setup(x => x.ScreenCount).Returns(1);
  325. screenImpl.Setup(X => X.AllScreens).Returns( new[] { new Screen(1, screen, screen, true) });
  326. var windowImpl = MockWindowingPlatform.CreateWindowMock();
  327. popupImpl = MockWindowingPlatform.CreatePopupMock(windowImpl.Object);
  328. popupImpl.SetupGet(x => x.RenderScaling).Returns(1);
  329. windowImpl.Setup(x => x.CreatePopup()).Returns(popupImpl.Object);
  330. windowImpl.Setup(x => x.Screen).Returns(screenImpl.Object);
  331. var services = TestServices.StyledWindow.With(
  332. inputManager: new InputManager(),
  333. windowImpl: windowImpl.Object,
  334. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object, x => popupImpl.Object));
  335. return UnitTestApplication.Start(services);
  336. }
  337. private class TestCommand : ICommand
  338. {
  339. private readonly Func<object, bool> _canExecute;
  340. private readonly Action<object> _execute;
  341. private EventHandler _canExecuteChanged;
  342. public TestCommand(bool enabled = true)
  343. : this(_ => enabled, _ => { })
  344. {
  345. }
  346. public TestCommand(Func<object, bool> canExecute, Action<object> execute = null)
  347. {
  348. _canExecute = canExecute;
  349. _execute = execute ?? (_ => { });
  350. }
  351. public int SubscriptionCount { get; private set; }
  352. public event EventHandler CanExecuteChanged
  353. {
  354. add { _canExecuteChanged += value; ++SubscriptionCount; }
  355. remove { _canExecuteChanged -= value; --SubscriptionCount; }
  356. }
  357. public bool CanExecute(object parameter) => _canExecute(parameter);
  358. public void Execute(object parameter) => _execute(parameter);
  359. public void RaiseCanExecuteChanged() => _canExecuteChanged?.Invoke(this, EventArgs.Empty);
  360. }
  361. private record MenuViewModel(string Header);
  362. }
  363. }