A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

69 lines
2.1 KiB

using Moq;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Templates;
using Avalonia.Input;
using Avalonia.Platform;
using Avalonia.Styling;
using Xunit;
namespace Avalonia.Controls.UnitTests.Utils
{
public class HotKeyManagerTests
{
[Fact]
public void HotKeyManager_Should_Register_And_Unregister_Key_Binding()
{
using (AvaloniaLocator.EnterScope())
{
var styler = new Mock<Styler>();
AvaloniaLocator.CurrentMutable
.Bind<IWindowingPlatform>().ToConstant(new WindowingPlatformMock())
.Bind<IStyler>().ToConstant(styler.Object);
var gesture1 = new KeyGesture(Key.A, InputModifiers.Control);
var gesture2 = new KeyGesture(Key.B, InputModifiers.Control);
var tl = new Window();
var button = new Button();
tl.Content = button;
tl.Template = CreateWindowTemplate();
tl.ApplyTemplate();
tl.Presenter.ApplyTemplate();
HotKeyManager.SetHotKey(button, gesture1);
Assert.Equal(gesture1, tl.KeyBindings[0].Gesture);
HotKeyManager.SetHotKey(button, gesture2);
Assert.Equal(gesture2, tl.KeyBindings[0].Gesture);
tl.Content = null;
tl.Presenter.ApplyTemplate();
Assert.Empty(tl.KeyBindings);
tl.Content = button;
tl.Presenter.ApplyTemplate();
Assert.Equal(gesture2, tl.KeyBindings[0].Gesture);
HotKeyManager.SetHotKey(button, null);
Assert.Empty(tl.KeyBindings);
}
}
private FuncControlTemplate CreateWindowTemplate()
{
return new FuncControlTemplate<Window>((parent, scope) =>
{
return new ContentPresenter
{
Name = "PART_ContentPresenter",
[~ContentPresenter.ContentProperty] = parent[~ContentControl.ContentProperty],
}.RegisterInNameScope(scope);
});
}
}
}