CollectionNavigatorTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.TextTests;
  3. public class CollectionNavigatorTests
  4. {
  5. private static readonly string [] simpleStrings =
  6. {
  7. "appricot", // 0
  8. "arm", // 1
  9. "bat", // 2
  10. "batman", // 3
  11. "candle" // 4
  12. };
  13. private readonly ITestOutputHelper _output;
  14. public CollectionNavigatorTests (ITestOutputHelper output) { _output = output; }
  15. [Fact]
  16. public void AtSymbol ()
  17. {
  18. var strings = new [] { "appricot", "arm", "ta", "@bob", "@bb", "text", "egg", "candle" };
  19. var n = new CollectionNavigator (strings);
  20. Assert.Equal (3, n.GetNextMatchingItem (0, '@'));
  21. Assert.Equal (3, n.GetNextMatchingItem (3, 'b'));
  22. Assert.Equal (4, n.GetNextMatchingItem (3, 'b'));
  23. }
  24. [Fact]
  25. public void Cycling ()
  26. {
  27. // cycling with 'b'
  28. var n = new CollectionNavigator (simpleStrings);
  29. Assert.Equal (2, n.GetNextMatchingItem (0, 'b'));
  30. Assert.Equal (3, n.GetNextMatchingItem (2, 'b'));
  31. // if 4 (candle) is selected it should loop back to bat
  32. Assert.Equal (2, n.GetNextMatchingItem (4, 'b'));
  33. // cycling with 'a'
  34. n = new CollectionNavigator (simpleStrings);
  35. Assert.Equal (0, n.GetNextMatchingItem (-1, 'a'));
  36. Assert.Equal (1, n.GetNextMatchingItem (0, 'a'));
  37. // if 4 (candle) is selected it should loop back to appricot
  38. Assert.Equal (0, n.GetNextMatchingItem (4, 'a'));
  39. }
  40. [Fact]
  41. public void Delay ()
  42. {
  43. var strings = new [] { "$$", "$100.00", "$101.00", "$101.10", "$200.00", "appricot" };
  44. var current = 0;
  45. var n = new CollectionNavigator (strings);
  46. // No delay
  47. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, 'a'));
  48. Assert.Equal ("a", n.SearchString);
  49. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  50. Assert.Equal ("$", n.SearchString);
  51. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  52. Assert.Equal ("$$", n.SearchString);
  53. // Delay
  54. Thread.Sleep (n.TypingDelay + 10);
  55. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, 'a'));
  56. Assert.Equal ("a", n.SearchString);
  57. Thread.Sleep (n.TypingDelay + 10);
  58. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  59. Assert.Equal ("$", n.SearchString);
  60. Thread.Sleep (n.TypingDelay + 10);
  61. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, '$'));
  62. Assert.Equal ("$", n.SearchString);
  63. Thread.Sleep (n.TypingDelay + 10);
  64. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, '$'));
  65. Assert.Equal ("$", n.SearchString);
  66. Thread.Sleep (n.TypingDelay + 10);
  67. Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, '$'));
  68. Assert.Equal ("$", n.SearchString);
  69. Thread.Sleep (n.TypingDelay + 10);
  70. Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, '2')); // Shouldn't move
  71. Assert.Equal ("2", n.SearchString);
  72. }
  73. [Fact]
  74. public void FullText ()
  75. {
  76. var strings = new [] { "appricot", "arm", "ta", "target", "text", "egg", "candle" };
  77. var n = new CollectionNavigator (strings);
  78. var current = 0;
  79. Assert.Equal (strings.IndexOf ("ta"), current = n.GetNextMatchingItem (current, 't'));
  80. // should match "te" in "text"
  81. Assert.Equal (strings.IndexOf ("text"), current = n.GetNextMatchingItem (current, 'e'));
  82. // still matches text
  83. Assert.Equal (strings.IndexOf ("text"), current = n.GetNextMatchingItem (current, 'x'));
  84. // nothing starts texa so it should NOT jump to appricot
  85. Assert.Equal (strings.IndexOf ("text"), current = n.GetNextMatchingItem (current, 'a'));
  86. Thread.Sleep (n.TypingDelay + 100);
  87. // nothing starts "texa". Since were past timedelay we DO jump to appricot
  88. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, 'a'));
  89. }
  90. [Theory]
  91. [InlineData (KeyCode.A, true)]
  92. [InlineData (KeyCode.Z, true)]
  93. [InlineData (KeyCode.D0, true)]
  94. [InlineData (KeyCode.A | KeyCode.ShiftMask, true)]
  95. [InlineData (KeyCode.Z | KeyCode.ShiftMask, true)]
  96. [InlineData (KeyCode.Space, true)]
  97. [InlineData (KeyCode.Z | KeyCode.CtrlMask, false)]
  98. [InlineData (KeyCode.Z | KeyCode.AltMask, false)]
  99. [InlineData (KeyCode.F1, false)]
  100. [InlineData (KeyCode.Delete, false)]
  101. [InlineData (KeyCode.Esc, false)]
  102. [InlineData (KeyCode.ShiftMask, false)]
  103. public void IsCompatibleKey_Does_Not_Allow_Alt_And_Ctrl_Keys (KeyCode keyCode, bool compatible)
  104. {
  105. Assert.Equal (compatible, CollectionNavigatorBase.IsCompatibleKey (keyCode));
  106. }
  107. [Fact]
  108. public void MinimizeMovement_False_ShouldMoveIfMultipleMatches ()
  109. {
  110. var strings = new [] { "$$", "$100.00", "$101.00", "$101.10", "$200.00", "appricot", "c", "car", "cart" };
  111. var current = 0;
  112. var n = new CollectionNavigator (strings);
  113. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$"));
  114. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$"));
  115. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$")); // back to top
  116. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$"));
  117. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$"));
  118. Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, "$"));
  119. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$"));
  120. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$")); // back to top
  121. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, "a"));
  122. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$")); // back to top
  123. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$100.00"));
  124. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$"));
  125. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$101.00"));
  126. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$2"));
  127. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$200.00"));
  128. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$101.00"));
  129. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$2"));
  130. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, "$101.00"));
  131. Assert.Equal (strings.IndexOf ("$200.00"), current = n.GetNextMatchingItem (current, "$2"));
  132. Assert.Equal (strings.IndexOf ("car"), current = n.GetNextMatchingItem (current, "car"));
  133. Assert.Equal (strings.IndexOf ("cart"), current = n.GetNextMatchingItem (current, "car"));
  134. Assert.Equal (-1, current = n.GetNextMatchingItem (current, "x"));
  135. }
  136. [Fact]
  137. public void MinimizeMovement_True_ShouldStayOnCurrentIfMultipleMatches ()
  138. {
  139. var strings = new [] { "$$", "$100.00", "$101.00", "$101.10", "$200.00", "appricot", "c", "car", "cart" };
  140. var current = 0;
  141. var n = new CollectionNavigator (strings);
  142. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$", true));
  143. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$", true));
  144. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, "$$", true)); // back to top
  145. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$1", true));
  146. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$", true));
  147. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, "$", true));
  148. Assert.Equal (strings.IndexOf ("car"), current = n.GetNextMatchingItem (current, "car", true));
  149. Assert.Equal (strings.IndexOf ("car"), current = n.GetNextMatchingItem (current, "car", true));
  150. Assert.Equal (-1, current = n.GetNextMatchingItem (current, "x", true));
  151. }
  152. [Fact]
  153. public void MutliKeySearchPlusWrongKeyStays ()
  154. {
  155. var strings = new [] { "a", "c", "can", "candle", "candy", "yellow", "zebra" };
  156. var current = 0;
  157. var n = new CollectionNavigator (strings);
  158. // https://github.com/gui-cs/Terminal.Gui/pull/2132#issuecomment-1298425573
  159. // One thing that it currently does that is different from Explorer is that as soon as you hit a wrong key then it jumps to that index.
  160. // So if you type cand then z it jumps you to something beginning with z. In the same situation Windows Explorer beeps (not the best!)
  161. // but remains on candle.
  162. // We might be able to update the behaviour so that a 'wrong' keypress (z) within 500ms of a 'right' keypress ("can" + 'd') is
  163. // simply ignored (possibly ending the search process though). That would give a short delay for user to realise the thing
  164. // they typed doesn't exist and then start a new search (which would be possible 500ms after the last 'good' keypress).
  165. // This would only apply for 2+ character searches where theres been a successful 2+ character match right before.
  166. Assert.Equal (strings.IndexOf ("a"), current = n.GetNextMatchingItem (current, 'a'));
  167. Assert.Equal ("a", n.SearchString);
  168. Assert.Equal (strings.IndexOf ("c"), current = n.GetNextMatchingItem (current, 'c'));
  169. Assert.Equal ("c", n.SearchString);
  170. Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'a'));
  171. Assert.Equal ("ca", n.SearchString);
  172. Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'n'));
  173. Assert.Equal ("can", n.SearchString);
  174. Assert.Equal (strings.IndexOf ("candle"), current = n.GetNextMatchingItem (current, 'd'));
  175. Assert.Equal ("cand", n.SearchString);
  176. // Same as above, but with a 'wrong' key (z)
  177. Thread.Sleep (n.TypingDelay + 10);
  178. Assert.Equal (strings.IndexOf ("a"), current = n.GetNextMatchingItem (current, 'a'));
  179. Assert.Equal ("a", n.SearchString);
  180. Assert.Equal (strings.IndexOf ("c"), current = n.GetNextMatchingItem (current, 'c'));
  181. Assert.Equal ("c", n.SearchString);
  182. Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'a'));
  183. Assert.Equal ("ca", n.SearchString);
  184. Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'n'));
  185. Assert.Equal ("can", n.SearchString);
  186. Assert.Equal (strings.IndexOf ("can"), current = n.GetNextMatchingItem (current, 'z')); // Shouldn't move
  187. Assert.Equal ("can", n.SearchString); // Shouldn't change
  188. }
  189. [Fact]
  190. public void OutOfBoundsShouldBeIgnored ()
  191. {
  192. var n = new CollectionNavigator (simpleStrings);
  193. // Expect saying that index 500 is the current selection should not cause
  194. // error and just be ignored (treated as no selection)
  195. Assert.Equal (2, n.GetNextMatchingItem (500, 'b'));
  196. }
  197. [Fact]
  198. public void ShouldAcceptNegativeOne ()
  199. {
  200. var n = new CollectionNavigator (simpleStrings);
  201. // Expect that index of -1 (i.e. no selection) should work correctly
  202. // and select the first entry of the letter 'b'
  203. Assert.Equal (2, n.GetNextMatchingItem (-1, 'b'));
  204. }
  205. [Fact]
  206. public void Symbols ()
  207. {
  208. var strings = new [] { "$$", "$100.00", "$101.00", "$101.10", "$200.00", "appricot" };
  209. var current = 0;
  210. var n = new CollectionNavigator (strings);
  211. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, 'a'));
  212. Assert.Equal ("a", n.SearchString);
  213. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  214. Assert.Equal ("$", n.SearchString);
  215. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, '1'));
  216. Assert.Equal ("$1", n.SearchString);
  217. Assert.Equal (strings.IndexOf ("$100.00"), current = n.GetNextMatchingItem (current, '0'));
  218. Assert.Equal ("$10", n.SearchString);
  219. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, '1'));
  220. Assert.Equal ("$101", n.SearchString);
  221. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, '.'));
  222. Assert.Equal ("$101.", n.SearchString);
  223. // stay on the same item becuase still in timedelay
  224. Assert.Equal (strings.IndexOf ("$101.00"), current = n.GetNextMatchingItem (current, 'a'));
  225. Assert.Equal ("$101.", n.SearchString);
  226. Thread.Sleep (n.TypingDelay + 100);
  227. // another '$' means searching for "$" again
  228. Assert.Equal (strings.IndexOf ("$101.10"), current = n.GetNextMatchingItem (current, '$'));
  229. Assert.Equal ("$", n.SearchString);
  230. Assert.Equal (strings.IndexOf ("$$"), current = n.GetNextMatchingItem (current, '$'));
  231. Assert.Equal ("$$", n.SearchString);
  232. }
  233. [Fact]
  234. public void Unicode ()
  235. {
  236. var strings = new [] { "appricot", "arm", "ta", "丗丙业丞", "丗丙丛", "text", "egg", "candle" };
  237. var n = new CollectionNavigator (strings);
  238. var current = 0;
  239. Assert.Equal (strings.IndexOf ("丗丙业丞"), current = n.GetNextMatchingItem (current, '丗'));
  240. // 丗丙业丞 is as good a match as 丗丙丛
  241. // so when doing multi character searches we should
  242. // prefer to stay on the same index unless we invalidate
  243. // our typed text
  244. Assert.Equal (strings.IndexOf ("丗丙业丞"), current = n.GetNextMatchingItem (current, '丙'));
  245. // No longer matches 丗丙业丞 and now only matches 丗丙丛
  246. // so we should move to the new match
  247. Assert.Equal (strings.IndexOf ("丗丙丛"), current = n.GetNextMatchingItem (current, '丛'));
  248. // nothing starts "丗丙丛a". Since were still in the timedelay we do not jump to appricot
  249. Assert.Equal (strings.IndexOf ("丗丙丛"), current = n.GetNextMatchingItem (current, 'a'));
  250. Thread.Sleep (n.TypingDelay + 100);
  251. // nothing starts "丗丙丛a". Since were past timedelay we DO jump to appricot
  252. Assert.Equal (strings.IndexOf ("appricot"), current = n.GetNextMatchingItem (current, 'a'));
  253. }
  254. [Fact]
  255. public void Word ()
  256. {
  257. var strings = new [] { "appricot", "arm", "bat", "batman", "bates hotel", "candle" };
  258. var current = 0;
  259. var n = new CollectionNavigator (strings);
  260. Assert.Equal (strings.IndexOf ("bat"), current = n.GetNextMatchingItem (current, 'b')); // match bat
  261. Assert.Equal (strings.IndexOf ("bat"), current = n.GetNextMatchingItem (current, 'a')); // match bat
  262. Assert.Equal (strings.IndexOf ("bat"), current = n.GetNextMatchingItem (current, 't')); // match bat
  263. Assert.Equal (
  264. strings.IndexOf ("bates hotel"),
  265. current = n.GetNextMatchingItem (current, 'e')
  266. ); // match bates hotel
  267. Assert.Equal (
  268. strings.IndexOf ("bates hotel"),
  269. current = n.GetNextMatchingItem (current, 's')
  270. ); // match bates hotel
  271. Assert.Equal (
  272. strings.IndexOf ("bates hotel"),
  273. current = n.GetNextMatchingItem (current, ' ')
  274. ); // match bates hotel
  275. }
  276. }