MenuItemTests.cs 13 KB

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