From 85ec916af3258496b2edea5ca525981076a2dde3 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 17 Oct 2019 15:51:26 +0200 Subject: [PATCH] Add some tests for KeyboardDevice. Including failing tests for #3127. --- .../KeyboardDeviceTests.cs | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 tests/Avalonia.Input.UnitTests/KeyboardDeviceTests.cs diff --git a/tests/Avalonia.Input.UnitTests/KeyboardDeviceTests.cs b/tests/Avalonia.Input.UnitTests/KeyboardDeviceTests.cs new file mode 100644 index 0000000000..3c8e800fca --- /dev/null +++ b/tests/Avalonia.Input.UnitTests/KeyboardDeviceTests.cs @@ -0,0 +1,90 @@ +using Avalonia.Input.Raw; +using Avalonia.Interactivity; +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(); + + target.ProcessRawEvent( + new RawKeyEventArgs( + target, + 0, + root.Object, + RawKeyEventType.KeyDown, + Key.A, + RawInputModifiers.None)); + + root.Verify(x => x.RaiseEvent(It.IsAny())); + } + + [Fact] + public void Keypresses_Should_Be_Sent_To_Focused_Element() + { + var target = new KeyboardDevice(); + var focused = new Mock(); + var root = Mock.Of(); + + target.SetFocusedElement( + focused.Object, + NavigationMethod.Unspecified, + InputModifiers.None); + + target.ProcessRawEvent( + new RawKeyEventArgs( + target, + 0, + root, + RawKeyEventType.KeyDown, + Key.A, + RawInputModifiers.None)); + + focused.Verify(x => x.RaiseEvent(It.IsAny())); + } + + [Fact] + public void TextInput_Should_Be_Sent_To_Root_If_No_Focused_Element() + { + var target = new KeyboardDevice(); + var root = new Mock(); + + target.ProcessRawEvent( + new RawTextInputEventArgs( + target, + 0, + root.Object, + "Foo")); + + root.Verify(x => x.RaiseEvent(It.IsAny())); + } + + [Fact] + public void TextInput_Should_Be_Sent_To_Focused_Element() + { + var target = new KeyboardDevice(); + var focused = new Mock(); + var root = Mock.Of(); + + target.SetFocusedElement( + focused.Object, + NavigationMethod.Unspecified, + InputModifiers.None); + + target.ProcessRawEvent( + new RawTextInputEventArgs( + target, + 0, + root, + "Foo")); + + focused.Verify(x => x.RaiseEvent(It.IsAny())); + } + } +}