| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using System;
- using System.Windows.Input;
- using Avalonia.Controls;
- using Avalonia.Input.Raw;
- using Avalonia.Interactivity;
- using Avalonia.UnitTests;
- using Moq;
- using Xunit;
- namespace Avalonia.Input.UnitTests
- {
- public class KeyboardDeviceTests
- {
- [Fact]
- public void Keypresses_Should_Be_Sent_To_Root_If_No_Focused_Element()
- {
- var target = new KeyboardDevice();
- var root = new Mock<IInputRoot>();
- target.ProcessRawEvent(
- new RawKeyEventArgs(
- target,
- 0,
- root.Object,
- RawKeyEventType.KeyDown,
- Key.A,
- RawInputModifiers.None));
- root.Verify(x => x.RaiseEvent(It.IsAny<KeyEventArgs>()));
- }
- [Fact]
- public void Keypresses_Should_Be_Sent_To_Focused_Element()
- {
- var target = new KeyboardDevice();
- var focused = new Mock<IInputElement>();
- var root = Mock.Of<IInputRoot>();
- target.SetFocusedElement(
- focused.Object,
- NavigationMethod.Unspecified,
- KeyModifiers.None);
- target.ProcessRawEvent(
- new RawKeyEventArgs(
- target,
- 0,
- root,
- RawKeyEventType.KeyDown,
- Key.A,
- RawInputModifiers.None));
- focused.Verify(x => x.RaiseEvent(It.IsAny<KeyEventArgs>()));
- }
- [Fact]
- public void TextInput_Should_Be_Sent_To_Root_If_No_Focused_Element()
- {
- var target = new KeyboardDevice();
- var root = new Mock<IInputRoot>();
- target.ProcessRawEvent(
- new RawTextInputEventArgs(
- target,
- 0,
- root.Object,
- "Foo"));
- root.Verify(x => x.RaiseEvent(It.IsAny<TextInputEventArgs>()));
- }
- [Fact]
- public void TextInput_Should_Be_Sent_To_Focused_Element()
- {
- var target = new KeyboardDevice();
- var focused = new Mock<IInputElement>();
- var root = Mock.Of<IInputRoot>();
- target.SetFocusedElement(
- focused.Object,
- NavigationMethod.Unspecified,
- KeyModifiers.None);
- target.ProcessRawEvent(
- new RawTextInputEventArgs(
- target,
- 0,
- root,
- "Foo"));
- focused.Verify(x => x.RaiseEvent(It.IsAny<TextInputEventArgs>()));
- }
- [Fact]
- public void Can_Change_KeyBindings_In_Keybinding_Event_Handler()
- {
- var target = new KeyboardDevice();
- var button = new Button();
- var root = new TestRoot(button);
- button.KeyBindings.Add(new KeyBinding
- {
- Gesture = new KeyGesture(Key.O, KeyModifiers.Control),
- Command = new DelegateCommand(() => button.KeyBindings.Clear()),
- });
- target.SetFocusedElement(button, NavigationMethod.Pointer, 0);
- target.ProcessRawEvent(
- new RawKeyEventArgs(
- target,
- 0,
- root,
- RawKeyEventType.KeyDown,
- Key.O,
- RawInputModifiers.Control));
- }
- private class DelegateCommand : ICommand
- {
- private readonly Action _action;
- public DelegateCommand(Action action) => _action = action;
- public event EventHandler CanExecuteChanged;
- public bool CanExecute(object parameter) => true;
- public void Execute(object parameter) => _action();
- }
- }
- }
|