1
0

KeyGestureTests.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. new object[]{"None", new KeyGesture(Key.None)},
  16. new object[]{"Alt+Shift", new KeyGesture(Key.None, KeyModifiers.Alt | KeyModifiers.Shift)},
  17. };
  18. public static readonly IEnumerable<object[]> ToStringData = new object[][]
  19. {
  20. new object[]{new KeyGesture(Key.A), "A"},
  21. new object[]{new KeyGesture(Key.A, KeyModifiers.Control), "Ctrl+A"},
  22. new object[]{new KeyGesture(Key.A, KeyModifiers.Control | KeyModifiers.Shift), "Ctrl+Shift+A"},
  23. new object[]{new KeyGesture(Key.A, KeyModifiers.Alt | KeyModifiers.Shift), "Shift+Alt+A"},
  24. new object[]{new KeyGesture(Key.A, KeyModifiers.Control | KeyModifiers.Alt | KeyModifiers.Shift), "Ctrl+Shift+Alt+A"},
  25. new object[]{new KeyGesture(Key.A, KeyModifiers.Meta | KeyModifiers.Shift), "Shift+Cmd+A"},
  26. new object[]{new KeyGesture(Key.None), "None"},
  27. new object[]{new KeyGesture(Key.None, KeyModifiers.Alt | KeyModifiers.Shift), "Shift+Alt"},
  28. };
  29. [Theory]
  30. [MemberData(nameof(ParseData))]
  31. public void Key_Gesture_Is_Able_To_Parse_Sample_Data(string text, KeyGesture gesture)
  32. {
  33. Assert.Equal(gesture, KeyGesture.Parse(text));
  34. }
  35. [Theory]
  36. [InlineData(Key.OemMinus, Key.Subtract)]
  37. [InlineData(Key.OemPlus, Key.Add)]
  38. [InlineData(Key.OemPeriod, Key.Decimal)]
  39. public void Key_Gesture_Matches_NumPad_To_Regular_Digit(Key gestureKey, Key pressedKey)
  40. {
  41. var keyGesture = new KeyGesture(gestureKey);
  42. Assert.True(keyGesture.Matches(new KeyEventArgs
  43. {
  44. Key = pressedKey
  45. }));
  46. }
  47. [Theory]
  48. [MemberData(nameof(ToStringData))]
  49. public void ToString_Produces_Correct_Results(KeyGesture gesture, string expected)
  50. {
  51. Assert.Equal(expected, gesture.ToString());
  52. }
  53. }
  54. }