HotKeyedControlsTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Windows.Input;
  3. using Avalonia.Input;
  4. using Avalonia.Input.Raw;
  5. using Avalonia.LogicalTree;
  6. using Avalonia.Platform;
  7. using Avalonia.UnitTests;
  8. using Moq;
  9. using Xunit;
  10. namespace Avalonia.Controls.UnitTests
  11. {
  12. internal class HotKeyedTextBox : TextBox, ICommandSource
  13. {
  14. private class DelegateCommand : ICommand
  15. {
  16. private readonly Action _action;
  17. public DelegateCommand(Action action) => _action = action;
  18. public event EventHandler CanExecuteChanged { add { } remove { } }
  19. public bool CanExecute(object parameter) => true;
  20. public void Execute(object parameter) => _action();
  21. }
  22. public static readonly StyledProperty<KeyGesture> HotKeyProperty =
  23. HotKeyManager.HotKeyProperty.AddOwner<HotKeyedTextBox>();
  24. private KeyGesture _hotkey;
  25. public KeyGesture HotKey
  26. {
  27. get => GetValue(HotKeyProperty);
  28. set => SetValue(HotKeyProperty, value);
  29. }
  30. protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
  31. {
  32. if (_hotkey != null)
  33. {
  34. this.SetValue(HotKeyProperty, _hotkey);
  35. }
  36. base.OnAttachedToLogicalTree(e);
  37. }
  38. protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
  39. {
  40. if (this.HotKey != null)
  41. {
  42. _hotkey = this.HotKey;
  43. this.SetValue(HotKeyProperty, null);
  44. }
  45. base.OnDetachedFromLogicalTree(e);
  46. }
  47. public void CanExecuteChanged(object sender, EventArgs e)
  48. {
  49. }
  50. protected override Type StyleKeyOverride => typeof(TextBox);
  51. public ICommand Command => _command;
  52. public object CommandParameter => null;
  53. private readonly DelegateCommand _command;
  54. public HotKeyedTextBox()
  55. {
  56. _command = new DelegateCommand(() => Focus());
  57. }
  58. }
  59. public class HotKeyedControlsTests : ScopedTestBase
  60. {
  61. private static Window PreparedWindow(object content = null)
  62. {
  63. var platform = AvaloniaLocator.Current.GetRequiredService<IWindowingPlatform>();
  64. var windowImpl = Mock.Get(platform.CreateWindow());
  65. windowImpl.Setup(x => x.Compositor).Returns(RendererMocks.CreateDummyCompositor());
  66. var w = new Window(windowImpl.Object) { Content = content };
  67. w.ApplyTemplate();
  68. return w;
  69. }
  70. private static IDisposable CreateServicesWithFocus()
  71. {
  72. return UnitTestApplication.Start(
  73. TestServices.StyledWindow.With(
  74. windowingPlatform: new MockWindowingPlatform(
  75. null,
  76. window => MockWindowingPlatform.CreatePopupMock(window).Object),
  77. keyboardDevice: () => new KeyboardDevice()));
  78. }
  79. [Fact]
  80. public void HotKeyedTextBox_Focus_Performed_On_Hotkey()
  81. {
  82. using var _ = CreateServicesWithFocus();
  83. var keyboardDevice = new KeyboardDevice();
  84. var hotKeyedTextBox = new HotKeyedTextBox { HotKey = new KeyGesture(Key.F, KeyModifiers.Control) };
  85. var root = PreparedWindow();
  86. root.Content = hotKeyedTextBox;
  87. root.Show();
  88. Assert.False(hotKeyedTextBox.IsFocused);
  89. keyboardDevice.ProcessRawEvent(
  90. new RawKeyEventArgs(
  91. keyboardDevice,
  92. 0,
  93. root,
  94. RawKeyEventType.KeyDown,
  95. Key.F,
  96. RawInputModifiers.Control,
  97. PhysicalKey.F,
  98. "f"));
  99. Assert.True(hotKeyedTextBox.IsFocused);
  100. }
  101. }
  102. }