// Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. 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(); AvaloniaLocator.CurrentMutable .Bind().ToConstant(new WindowingPlatformMock()) .Bind().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((parent, scope) => { return new ContentPresenter { Name = "PART_ContentPresenter", [~ContentPresenter.ContentProperty] = parent[~ContentControl.ContentProperty], }.RegisterInNameScope(scope); }); } } }