KeyGestureTests.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Collections.Generic;
  2. using Avalonia.Input;
  3. using Xunit;
  4. namespace Avalonia.Base.UnitTests.Input
  5. {
  6. public class KeyGestureTests
  7. {
  8. public static readonly IEnumerable<object[]> ParseData = new object[][]
  9. {
  10. new object[]{"Ctrl+A", new KeyGesture(Key.A, KeyModifiers.Control)},
  11. new object[]{" \tShift\t+Alt +B", new KeyGesture(Key.B, KeyModifiers.Shift | KeyModifiers.Alt) },
  12. new object[]{"Control++", new KeyGesture(Key.OemPlus, KeyModifiers.Control) },
  13. new object[]{ "Shift+⌘+A", new KeyGesture(Key.A, KeyModifiers.Meta | KeyModifiers.Shift) },
  14. new object[]{ "Shift+Cmd+A", new KeyGesture(Key.A, KeyModifiers.Meta | KeyModifiers.Shift) },
  15. };
  16. public static readonly IEnumerable<object[]> ToStringData = new object[][]
  17. {
  18. new object[]{new KeyGesture(Key.A), "A"},
  19. new object[]{new KeyGesture(Key.A, KeyModifiers.Control), "Ctrl+A"},
  20. new object[]{new KeyGesture(Key.A, KeyModifiers.Control | KeyModifiers.Shift), "Ctrl+Shift+A"},
  21. new object[]{new KeyGesture(Key.A, KeyModifiers.Alt | KeyModifiers.Shift), "Shift+Alt+A"},
  22. new object[]{new KeyGesture(Key.A, KeyModifiers.Control | KeyModifiers.Alt | KeyModifiers.Shift), "Ctrl+Shift+Alt+A"},
  23. new object[]{new KeyGesture(Key.A, KeyModifiers.Meta | KeyModifiers.Shift), "Shift+Cmd+A"},
  24. };
  25. [Theory]
  26. [MemberData(nameof(ParseData))]
  27. public void Key_Gesture_Is_Able_To_Parse_Sample_Data(string text, KeyGesture gesture)
  28. {
  29. Assert.Equal(gesture, KeyGesture.Parse(text));
  30. }
  31. [Theory]
  32. [InlineData(Key.OemMinus, Key.Subtract)]
  33. [InlineData(Key.OemPlus, Key.Add)]
  34. [InlineData(Key.OemPeriod, Key.Decimal)]
  35. public void Key_Gesture_Matches_NumPad_To_Regular_Digit(Key gestureKey, Key pressedKey)
  36. {
  37. var keyGesture = new KeyGesture(gestureKey);
  38. Assert.True(keyGesture.Matches(new KeyEventArgs
  39. {
  40. Key = pressedKey
  41. }));
  42. }
  43. [Theory]
  44. [MemberData(nameof(ToStringData))]
  45. public void ToString_Produces_Correct_Results(KeyGesture gesture, string expected)
  46. {
  47. Assert.Equal(expected, gesture.ToString());
  48. }
  49. }
  50. }