Преглед на файлове

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.
Steven Kirk преди 6 години
родител
ревизия
f8cf268f0e

+ 1 - 1
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);
                     }

+ 17 - 0
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<IMenu>();
+                var item = Mock.Of<IMenuItem>(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()
             {