HotKeyManagerTests.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Moq;
  7. using Perspex.Controls.Presenters;
  8. using Perspex.Controls.Templates;
  9. using Perspex.Input;
  10. using Perspex.Platform;
  11. using Perspex.Styling;
  12. using Xunit;
  13. namespace Perspex.Controls.UnitTests.Utils
  14. {
  15. public class HotKeyManagerTests
  16. {
  17. [Fact]
  18. public void HotKeyManager_Should_Register_And_Unregister_Key_Binding()
  19. {
  20. using (PerspexLocator.EnterScope())
  21. {
  22. var windowImpl = new Mock<IWindowImpl>();
  23. var styler = new Mock<Styler>();
  24. PerspexLocator.CurrentMutable
  25. .Bind<IWindowImpl>().ToConstant(windowImpl.Object)
  26. .Bind<IStyler>().ToConstant(styler.Object);
  27. var gesture1 = new KeyGesture {Key = Key.A, Modifiers = InputModifiers.Control};
  28. var gesture2 = new KeyGesture {Key = Key.B, Modifiers = InputModifiers.Control};
  29. var tl = new Window();
  30. var button = new Button();
  31. tl.Content = button;
  32. tl.Template = CreateWindowTemplate();
  33. tl.ApplyTemplate();
  34. HotKeyManager.SetHotKey(button, gesture1);
  35. Assert.Equal(gesture1, tl.KeyBindings[0].Gesture);
  36. HotKeyManager.SetHotKey(button, gesture2);
  37. Assert.Equal(gesture2, tl.KeyBindings[0].Gesture);
  38. tl.Content = null;
  39. tl.Presenter.ApplyTemplate();
  40. Assert.Empty(tl.KeyBindings);
  41. tl.Content = button;
  42. tl.Presenter.ApplyTemplate();
  43. Assert.Equal(gesture2, tl.KeyBindings[0].Gesture);
  44. HotKeyManager.SetHotKey(button, null);
  45. Assert.Empty(tl.KeyBindings);
  46. }
  47. }
  48. private FuncControlTemplate CreateWindowTemplate()
  49. {
  50. return new FuncControlTemplate<Window>(parent =>
  51. {
  52. return new ContentPresenter
  53. {
  54. Name = "contentPresenter",
  55. [~ContentPresenter.ContentProperty] = parent[~ContentControl.ContentProperty],
  56. };
  57. });
  58. }
  59. }
  60. }