123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- using Avalonia.Controls;
- using Avalonia.Input;
- using Avalonia.Input.Raw;
- using Avalonia.UnitTests;
- using Moq;
- using Xunit;
- namespace Avalonia.Base.UnitTests.Input
- {
- 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,
- PhysicalKey.A,
- "a"));
- 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 Control();
- var root = new TestRoot();
- var raised = 0;
- target.SetFocusedElement(
- focused,
- NavigationMethod.Unspecified,
- KeyModifiers.None);
- focused.KeyDown += (s, e) => ++raised;
- target.ProcessRawEvent(
- new RawKeyEventArgs(
- target,
- 0,
- root,
- RawKeyEventType.KeyDown,
- Key.A,
- RawInputModifiers.None,
- PhysicalKey.A,
- "a"));
- Assert.Equal(1, raised);
- }
- [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 Control();
- var root = new TestRoot();
- var raised = 0;
- target.SetFocusedElement(
- focused,
- NavigationMethod.Unspecified,
- KeyModifiers.None);
- focused.TextInput += (s, e) => ++raised;
- target.ProcessRawEvent(
- new RawTextInputEventArgs(
- target,
- 0,
- root,
- "Foo"));
- Assert.Equal(1, raised);
- }
- [Fact]
- public void Can_Change_KeyBindings_In_Keybinding_Event_Handler()
- {
- var target = new KeyboardDevice();
- var button = new Button();
- var root = new TestRoot(button);
- var raised = 0;
- button.KeyBindings.Add(new KeyBinding
- {
- Gesture = new KeyGesture(Key.O, KeyModifiers.Control),
- Command = new Utilities.DelegateCommand(() =>
- {
- button.KeyBindings.Clear();
- ++raised;
- }),
- });
- target.SetFocusedElement(button, NavigationMethod.Pointer, 0);
- target.ProcessRawEvent(
- new RawKeyEventArgs(
- target,
- 0,
- root,
- RawKeyEventType.KeyDown,
- Key.O,
- RawInputModifiers.Control,
- PhysicalKey.O,
- "o"));
- Assert.Equal(1, raised);
- }
- [Fact]
- public void Control_Focus_Should_Be_Set_Before_FocusedElement_Raises_PropertyChanged()
- {
- var target = new KeyboardDevice();
- var focused = new Control();
- var root = new TestRoot();
- var gotFocusRaised = 0;
- var propertyChangedRaised = 0;
- focused.GotFocus += (s, e) => ++gotFocusRaised;
- target.PropertyChanged += (s, e) =>
- {
- if (e.PropertyName == nameof(target.FocusedElement))
- {
- Assert.Equal(1, gotFocusRaised);
- ++propertyChangedRaised;
- }
- };
- target.SetFocusedElement(
- focused,
- NavigationMethod.Unspecified,
- KeyModifiers.None);
- Assert.Equal(1, propertyChangedRaised);
- }
- [Fact]
- public void Cancelled_Focus_Change_Should_Not_Send_Got_Focus_Event()
- {
- var target = new KeyboardDevice();
- var focused = new Control();
- var root = new TestRoot();
- bool focusCancelled = false;
- focused.GettingFocus += (s, e) =>
- {
- focusCancelled = e.TryCancel();
- };
- focused.GotFocus += (s, e) =>
- {
- focusCancelled = false;
- };
- target.SetFocusedElement(
- focused,
- NavigationMethod.Unspecified,
- KeyModifiers.None);
- Assert.True(focusCancelled);
- }
- [Fact]
- public void Redirected_Focus_Should_Change_Focused_Element()
- {
- var target = new KeyboardDevice();
- var first = new Control();
- var second = new Control();
- var stack = new StackPanel();
- stack.Children.AddRange(new[] { first, second });
- var root = new TestRoot(stack);
- first.GettingFocus += (s, e) =>
- {
- e.TrySetNewFocusedElement(second);
- };
- target.SetFocusedElement(
- first,
- NavigationMethod.Unspecified,
- KeyModifiers.None);
- Assert.True(second.IsFocused);
- }
- }
- }
|