ListViewTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Collections.ObjectModel;
  2. using Moq;
  3. namespace UnitTests_Parallelizable.ViewsTests;
  4. public class ListViewTests
  5. {
  6. [Fact]
  7. public void ListViewCollectionNavigatorMatcher_DefaultBehaviour ()
  8. {
  9. ObservableCollection<string> source = new () { "apricot", "arm", "bat", "batman", "bates hotel", "candle" };
  10. ListView lv = new ListView { Source = new ListWrapper<string> (source) };
  11. // Keys are consumed during navigation
  12. Assert.True (lv.NewKeyDownEvent (Key.B));
  13. Assert.True (lv.NewKeyDownEvent (Key.A));
  14. Assert.True (lv.NewKeyDownEvent (Key.T));
  15. Assert.Equal ("bat", (string)lv.Source.ToList () [lv.SelectedItem]);
  16. }
  17. [Fact]
  18. public void ListViewCollectionNavigatorMatcher_IgnoreKeys ()
  19. {
  20. ObservableCollection<string> source = new () { "apricot", "arm", "bat", "batman", "bates hotel", "candle" };
  21. ListView lv = new ListView { Source = new ListWrapper<string> (source) };
  22. var matchNone = new Mock<ICollectionNavigatorMatcher> ();
  23. matchNone.Setup (m => m.IsCompatibleKey (It.IsAny<Key> ()))
  24. .Returns (false);
  25. lv.KeystrokeNavigator.Matcher = matchNone.Object;
  26. // Keys are ignored because IsCompatibleKey returned false i.e. don't use these keys for navigation
  27. Assert.False (lv.NewKeyDownEvent (Key.B));
  28. Assert.False (lv.NewKeyDownEvent (Key.A));
  29. Assert.False (lv.NewKeyDownEvent (Key.T));
  30. // assert IsMatch never called
  31. matchNone.Verify (m => m.IsMatch (It.IsAny<string> (), It.IsAny<object> ()), Times.Never ());
  32. }
  33. [Fact]
  34. public void ListViewCollectionNavigatorMatcher_OverrideMatching ()
  35. {
  36. ObservableCollection<string> source = new () { "apricot", "arm", "bat", "batman", "bates hotel", "candle" };
  37. ListView lv = new ListView { Source = new ListWrapper<string> (source) };
  38. var matchNone = new Mock<ICollectionNavigatorMatcher> ();
  39. matchNone.Setup (m => m.IsCompatibleKey (It.IsAny<Key> ()))
  40. .Returns (true);
  41. // Match any string starting with b to "candle" (psych!)
  42. matchNone.Setup (m => m.IsMatch (It.IsAny<string> (), It.IsAny<object> ()))
  43. .Returns ((string s, object key) => s.StartsWith ('B') && key?.ToString () == "candle");
  44. lv.KeystrokeNavigator.Matcher = matchNone.Object;
  45. // Keys are consumed during navigation
  46. Assert.True (lv.NewKeyDownEvent (Key.B));
  47. Assert.Equal (5, lv.SelectedItem);
  48. Assert.True (lv.NewKeyDownEvent (Key.A));
  49. Assert.Equal (5, lv.SelectedItem);
  50. Assert.True (lv.NewKeyDownEvent (Key.T));
  51. Assert.Equal (5, lv.SelectedItem);
  52. Assert.Equal ("candle", (string)lv.Source.ToList () [lv.SelectedItem]);
  53. }
  54. [Fact]
  55. public void ListView_CollectionNavigatorMatcher_KeybindingsOverrideNavigator ()
  56. {
  57. ObservableCollection<string> source = new () { "apricot", "arm", "bat", "batman", "bates hotel", "candle" };
  58. ListView lv = new ListView { Source = new ListWrapper<string> (source) };
  59. lv.SetFocus ();
  60. lv.KeyBindings.Add (Key.B, Command.Down);
  61. Assert.Equal (-1, lv.SelectedItem);
  62. // Keys should be consumed to move down the navigation i.e. to apricot
  63. Assert.True (lv.NewKeyDownEvent (Key.B));
  64. Assert.Equal (0, lv.SelectedItem);
  65. Assert.True (lv.NewKeyDownEvent (Key.B));
  66. Assert.Equal (1, lv.SelectedItem);
  67. // There is no keybinding for Key.C so it hits collection navigator i.e. we jump to candle
  68. Assert.True (lv.NewKeyDownEvent (Key.C));
  69. Assert.Equal (5, lv.SelectedItem);
  70. }
  71. [Fact]
  72. public void CollectionNavigatorMatcher_KeybindingsOverrideNavigator ()
  73. {
  74. ObservableCollection<string> source = new () { "apricot", "arm", "bat", "batman", "bates hotel", "candle" };
  75. ListView lv = new ListView { Source = new ListWrapper<string> (source) };
  76. lv.SetFocus ();
  77. lv.KeyBindings.Add (Key.B, Command.Down);
  78. Assert.Equal (-1, lv.SelectedItem);
  79. // Keys should be consumed to move down the navigation i.e. to apricot
  80. Assert.True (lv.NewKeyDownEvent (Key.B));
  81. Assert.Equal (0, lv.SelectedItem);
  82. Assert.True (lv.NewKeyDownEvent (Key.B));
  83. Assert.Equal (1, lv.SelectedItem);
  84. // There is no keybinding for Key.C so it hits collection navigator i.e. we jump to candle
  85. Assert.True (lv.NewKeyDownEvent (Key.C));
  86. Assert.Equal (5, lv.SelectedItem);
  87. }
  88. }