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.
 
 
 

74 lines
2.3 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using Perspex.Controls.Presenters;
using Perspex.Controls.Templates;
using Perspex.Controls.UnitTests.Primitives;
using Perspex.Input;
using Perspex.Platform;
using Perspex.Styling;
using Xunit;
namespace Perspex.Controls.UnitTests.Utils
{
public class HotKeyManagerTests
{
[Fact]
public void HotKeyManager_Should_Register_And_Unregister_Key_Binding()
{
using (PerspexLocator.EnterScope())
{
var styler = new Mock<Styler>();
PerspexLocator.CurrentMutable
.Bind<IWindowingPlatform>().ToConstant(new WindowingPlatformMock())
.Bind<IStyler>().ToConstant(styler.Object);
var gesture1 = new KeyGesture {Key = Key.A, Modifiers = InputModifiers.Control};
var gesture2 = new KeyGesture {Key = Key.B, Modifiers = InputModifiers.Control};
var tl = new Window();
var button = new Button();
tl.Content = button;
tl.Template = CreateWindowTemplate();
tl.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 =>
{
return new ContentPresenter
{
Name = "PART_ContentPresenter",
[~ContentPresenter.ContentProperty] = parent[~ContentControl.ContentProperty],
};
});
}
}
}