From f8cf268f0e86e32c6c7bb700400a24141ce3beb5 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 1 May 2019 12:02:12 +0200 Subject: [PATCH] Added unit test for toplevel menu close. Added unit test for previous commit, and also tweaked code a little bit to use `IsTopLevel` instead of trying to cast the parent menu item. --- .../Platform/DefaultMenuInteractionHandler.cs | 2 +- .../DefaultMenuInteractionHandlerTests.cs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs b/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs index 0badd918cc..73854b9f60 100644 --- a/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs +++ b/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs @@ -338,7 +338,7 @@ namespace Avalonia.Controls.Platform { if (item.IsSubMenuOpen) { - if (item.Parent is Menu) + if (item.IsTopLevel) { CloseMenu(item); } diff --git a/tests/Avalonia.Controls.UnitTests/Platform/DefaultMenuInteractionHandlerTests.cs b/tests/Avalonia.Controls.UnitTests/Platform/DefaultMenuInteractionHandlerTests.cs index 87b235dce7..df1846c617 100644 --- a/tests/Avalonia.Controls.UnitTests/Platform/DefaultMenuInteractionHandlerTests.cs +++ b/tests/Avalonia.Controls.UnitTests/Platform/DefaultMenuInteractionHandlerTests.cs @@ -1,6 +1,7 @@ using System; using Avalonia.Controls.Platform; using Avalonia.Input; +using Avalonia.Interactivity; using Moq; using Xunit; @@ -110,6 +111,22 @@ namespace Avalonia.Controls.UnitTests.Platform Assert.True(e.Handled); } + [Fact] + public void Click_On_Open_TopLevel_Menu_Closes_Menu() + { + var target = new DefaultMenuInteractionHandler(false); + var menu = Mock.Of(); + var item = Mock.Of(x => + x.IsSubMenuOpen == true && + x.IsTopLevel == true && + x.HasSubMenu == true && + x.Parent == menu); + var e = new PointerPressedEventArgs { MouseButton = MouseButton.Left, Source = item }; + + target.PointerPressed(item, e); + Mock.Get(menu).Verify(x => x.Close()); + } + [Fact] public void PointerEnter_Opens_Item_When_Old_Item_Is_Open() {