From a01fb157e400befa2e477549867d22a0cabec3e3 Mon Sep 17 00:00:00 2001 From: Andrey Kunchev Date: Mon, 21 Dec 2020 12:53:31 +0200 Subject: [PATCH 1/2] add some tests for contextmenu open without params --- .../ContextMenuTests.cs | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs b/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs index 39a3250686..f3a1316c7d 100644 --- a/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs @@ -44,6 +44,55 @@ namespace Avalonia.Controls.UnitTests } } + [Fact] + public void Open_Should_Use_Default_Control() + { + using (Application()) + { + var sut = new ContextMenu(); + var target = new Panel + { + ContextMenu = sut + }; + + var window = new Window { Content = target }; + window.ApplyTemplate(); + window.Presenter.ApplyTemplate(); + + bool opened = false; + + sut.MenuOpened += (sender, args) => + { + opened = true; + }; + + sut.Open(); + + Assert.True(opened); + } + } + + [Fact] + public void Open_Should_Raise_Exception_If_AlreadyDetached() + { + using (Application()) + { + var sut = new ContextMenu(); + var target = new Panel + { + ContextMenu = sut + }; + + var window = new Window { Content = target }; + window.ApplyTemplate(); + window.Presenter.ApplyTemplate(); + + target.ContextMenu = null; + + Assert.ThrowsAny(()=> sut.Open()); + } + } + [Fact] public void Closing_Raises_Single_Closed_Event() { From a045e963ac2a6def331ace62b977b52bc89636b6 Mon Sep 17 00:00:00 2001 From: Andrey Kunchev Date: Mon, 21 Dec 2020 12:54:23 +0200 Subject: [PATCH 2/2] ContextMenu.Open should call Open(null) so it doesn't break working scenarios --- src/Avalonia.Controls/ContextMenu.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/ContextMenu.cs b/src/Avalonia.Controls/ContextMenu.cs index 5e17182f3e..fb8080f0d4 100644 --- a/src/Avalonia.Controls/ContextMenu.cs +++ b/src/Avalonia.Controls/ContextMenu.cs @@ -246,7 +246,7 @@ namespace Avalonia.Controls /// /// Opens the menu. /// - public override void Open() => throw new NotSupportedException(); + public override void Open() => Open(null); /// /// Opens a context menu on the specified control.