HotKeyManagerTests.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Moq;
  4. using Avalonia.Controls.Presenters;
  5. using Avalonia.Controls.Templates;
  6. using Avalonia.Input;
  7. using Avalonia.Platform;
  8. using Avalonia.Styling;
  9. using Xunit;
  10. namespace Avalonia.Controls.UnitTests.Utils
  11. {
  12. public class HotKeyManagerTests
  13. {
  14. [Fact]
  15. public void HotKeyManager_Should_Register_And_Unregister_Key_Binding()
  16. {
  17. using (AvaloniaLocator.EnterScope())
  18. {
  19. var styler = new Mock<Styler>();
  20. AvaloniaLocator.CurrentMutable
  21. .Bind<IWindowingPlatform>().ToConstant(new WindowingPlatformMock())
  22. .Bind<IStyler>().ToConstant(styler.Object);
  23. var gesture1 = new KeyGesture {Key = Key.A, Modifiers = InputModifiers.Control};
  24. var gesture2 = new KeyGesture {Key = Key.B, Modifiers = InputModifiers.Control};
  25. var tl = new Window();
  26. var button = new Button();
  27. tl.Content = button;
  28. tl.Template = CreateWindowTemplate();
  29. tl.ApplyTemplate();
  30. tl.Presenter.ApplyTemplate();
  31. HotKeyManager.SetHotKey(button, gesture1);
  32. Assert.Equal(gesture1, tl.KeyBindings[0].Gesture);
  33. HotKeyManager.SetHotKey(button, gesture2);
  34. Assert.Equal(gesture2, tl.KeyBindings[0].Gesture);
  35. tl.Content = null;
  36. tl.Presenter.ApplyTemplate();
  37. Assert.Empty(tl.KeyBindings);
  38. tl.Content = button;
  39. tl.Presenter.ApplyTemplate();
  40. Assert.Equal(gesture2, tl.KeyBindings[0].Gesture);
  41. HotKeyManager.SetHotKey(button, null);
  42. Assert.Empty(tl.KeyBindings);
  43. }
  44. }
  45. private FuncControlTemplate CreateWindowTemplate()
  46. {
  47. return new FuncControlTemplate<Window>(parent =>
  48. {
  49. return new ContentPresenter
  50. {
  51. Name = "PART_ContentPresenter",
  52. [~ContentPresenter.ContentProperty] = parent[~ContentControl.ContentProperty],
  53. };
  54. });
  55. }
  56. }
  57. }