MenuItemTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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.ApplyStyling();
  161. window.ApplyTemplate();
  162. window.Presenter.ApplyTemplate();
  163. Assert.True(target.IsEffectivelyEnabled);
  164. target.Command = command;
  165. Assert.Equal(0, canExecuteCallCount);
  166. target.CommandParameter = false;
  167. Assert.Equal(0, canExecuteCallCount);
  168. command.RaiseCanExecuteChanged();
  169. Assert.Equal(0, canExecuteCallCount);
  170. contextMenu.Open();
  171. Assert.Equal(2, canExecuteCallCount);//2 because popup is changing logical child
  172. command.RaiseCanExecuteChanged();
  173. Assert.Equal(3, canExecuteCallCount);
  174. target.CommandParameter = true;
  175. Assert.Equal(4, canExecuteCallCount);
  176. }
  177. }
  178. [Fact]
  179. public void MenuItem_Does_Not_Invoke_CanExecute_When_MenuFlyout_Closed()
  180. {
  181. using (Application())
  182. {
  183. var canExecuteCallCount = 0;
  184. var command = new TestCommand(_ =>
  185. {
  186. canExecuteCallCount++;
  187. return true;
  188. });
  189. var target = new MenuItem();
  190. var flyout = new MenuFlyout { Items = new AvaloniaList<MenuItem> { target } };
  191. var button = new Button { Flyout = flyout };
  192. var window = new Window { Content = button };
  193. window.ApplyStyling();
  194. window.ApplyTemplate();
  195. window.Presenter.ApplyTemplate();
  196. Assert.True(target.IsEffectivelyEnabled);
  197. target.Command = command;
  198. Assert.Equal(0, canExecuteCallCount);
  199. target.CommandParameter = false;
  200. Assert.Equal(0, canExecuteCallCount);
  201. command.RaiseCanExecuteChanged();
  202. Assert.Equal(0, canExecuteCallCount);
  203. flyout.ShowAt(button);
  204. Assert.Equal(2, canExecuteCallCount);
  205. command.RaiseCanExecuteChanged();
  206. Assert.Equal(3, canExecuteCallCount);
  207. target.CommandParameter = true;
  208. Assert.Equal(4, canExecuteCallCount);
  209. }
  210. }
  211. [Fact]
  212. public void MenuItem_Does_Not_Invoke_CanExecute_When_Parent_MenuItem_Closed()
  213. {
  214. using (Application())
  215. {
  216. var canExecuteCallCount = 0;
  217. var command = new TestCommand(_ =>
  218. {
  219. canExecuteCallCount++;
  220. return true;
  221. });
  222. var target = new MenuItem();
  223. var parentMenuItem = new MenuItem { Items = new AvaloniaList<MenuItem> { target } };
  224. var contextMenu = new ContextMenu { Items = new AvaloniaList<MenuItem> { parentMenuItem } };
  225. var window = new Window { Content = new Panel { ContextMenu = contextMenu } };
  226. window.ApplyStyling();
  227. window.ApplyTemplate();
  228. window.Presenter.ApplyTemplate();
  229. contextMenu.Open();
  230. Assert.True(target.IsEffectivelyEnabled);
  231. target.Command = command;
  232. Assert.Equal(0, canExecuteCallCount);
  233. target.CommandParameter = false;
  234. Assert.Equal(0, canExecuteCallCount);
  235. command.RaiseCanExecuteChanged();
  236. Assert.Equal(0, canExecuteCallCount);
  237. try
  238. {
  239. parentMenuItem.IsSubMenuOpen = true;
  240. }
  241. catch (InvalidOperationException)
  242. {
  243. //popup host creation failed exception
  244. }
  245. Assert.Equal(1, canExecuteCallCount);
  246. command.RaiseCanExecuteChanged();
  247. Assert.Equal(2, canExecuteCallCount);
  248. target.CommandParameter = true;
  249. Assert.Equal(3, canExecuteCallCount);
  250. }
  251. }
  252. [Fact]
  253. public void TemplatedParent_Should_Not_Be_Applied_To_Submenus()
  254. {
  255. using (Application())
  256. {
  257. MenuItem topLevelMenu;
  258. MenuItem childMenu1;
  259. MenuItem childMenu2;
  260. var menu = new Menu
  261. {
  262. Items = new[]
  263. {
  264. (topLevelMenu = new MenuItem
  265. {
  266. Header = "Foo",
  267. Items = new[]
  268. {
  269. (childMenu1 = new MenuItem { Header = "Bar" }),
  270. (childMenu2 = new MenuItem { Header = "Baz" }),
  271. }
  272. }),
  273. }
  274. };
  275. var window = new Window { Content = menu };
  276. window.LayoutManager.ExecuteInitialLayoutPass();
  277. topLevelMenu.IsSubMenuOpen = true;
  278. Assert.True(((IVisual)childMenu1).IsAttachedToVisualTree);
  279. Assert.Null(childMenu1.TemplatedParent);
  280. Assert.Null(childMenu2.TemplatedParent);
  281. topLevelMenu.IsSubMenuOpen = false;
  282. topLevelMenu.IsSubMenuOpen = true;
  283. Assert.Null(childMenu1.TemplatedParent);
  284. Assert.Null(childMenu2.TemplatedParent);
  285. }
  286. }
  287. private IDisposable Application()
  288. {
  289. var screen = new PixelRect(new PixelPoint(), new PixelSize(100, 100));
  290. var screenImpl = new Mock<IScreenImpl>();
  291. screenImpl.Setup(x => x.ScreenCount).Returns(1);
  292. screenImpl.Setup(X => X.AllScreens).Returns( new[] { new Screen(1, screen, screen, true) });
  293. var windowImpl = MockWindowingPlatform.CreateWindowMock();
  294. popupImpl = MockWindowingPlatform.CreatePopupMock(windowImpl.Object);
  295. popupImpl.SetupGet(x => x.RenderScaling).Returns(1);
  296. windowImpl.Setup(x => x.CreatePopup()).Returns(popupImpl.Object);
  297. windowImpl.Setup(x => x.Screen).Returns(screenImpl.Object);
  298. var services = TestServices.StyledWindow.With(
  299. inputManager: new InputManager(),
  300. windowImpl: windowImpl.Object,
  301. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object, x => popupImpl.Object));
  302. return UnitTestApplication.Start(services);
  303. }
  304. private class TestCommand : ICommand
  305. {
  306. private readonly Func<object, bool> _canExecute;
  307. private readonly Action<object> _execute;
  308. private EventHandler _canExecuteChanged;
  309. public TestCommand(bool enabled = true)
  310. : this(_ => enabled, _ => { })
  311. {
  312. }
  313. public TestCommand(Func<object, bool> canExecute, Action<object> execute = null)
  314. {
  315. _canExecute = canExecute;
  316. _execute = execute ?? (_ => { });
  317. }
  318. public int SubscriptionCount { get; private set; }
  319. public event EventHandler CanExecuteChanged
  320. {
  321. add { _canExecuteChanged += value; ++SubscriptionCount; }
  322. remove { _canExecuteChanged -= value; --SubscriptionCount; }
  323. }
  324. public bool CanExecute(object parameter) => _canExecute(parameter);
  325. public void Execute(object parameter) => _execute(parameter);
  326. public void RaiseCanExecuteChanged() => _canExecuteChanged?.Invoke(this, EventArgs.Empty);
  327. }
  328. }
  329. }