AppendAutocompleteTests.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xunit;
  7. using Xunit.Abstractions;
  8. namespace Terminal.Gui.TextTests {
  9. public class AppendAutocompleteTests {
  10. readonly ITestOutputHelper output;
  11. public AppendAutocompleteTests (ITestOutputHelper output)
  12. {
  13. this.output = output;
  14. }
  15. [Fact, AutoInitShutdown]
  16. public void TestAutoAppend_ShowThenAccept_MatchCase ()
  17. {
  18. var tf = GetTextFieldsInView ();
  19. tf.Autocomplete = new AppendAutocomplete (tf);
  20. var generator = (SingleWordSuggestionGenerator)tf.Autocomplete.SuggestionGenerator;
  21. generator.AllSuggestions = new List<string> { "fish" };
  22. tf.Draw ();
  23. tf.PositionCursor ();
  24. TestHelpers.AssertDriverContentsAre ("", output);
  25. tf.ProcessKey (new KeyEvent (Key.f, new KeyModifiers ()));
  26. tf.Draw ();
  27. tf.PositionCursor ();
  28. TestHelpers.AssertDriverContentsAre ("fish", output);
  29. Assert.Equal ("f", tf.Text);
  30. Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  31. tf.Draw ();
  32. TestHelpers.AssertDriverContentsAre ("fish", output);
  33. Assert.Equal ("fish", tf.Text);
  34. // Tab should autcomplete but not move focus
  35. Assert.Same (tf, Application.Top.Focused);
  36. // Second tab should move focus (nothing to autocomplete)
  37. Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  38. Assert.NotSame (tf, Application.Top.Focused);
  39. }
  40. [Fact, AutoInitShutdown]
  41. public void TestAutoAppend_ShowThenAccept_CasesDiffer ()
  42. {
  43. var tf = GetTextFieldsInView ();
  44. tf.Autocomplete = new AppendAutocomplete (tf);
  45. var generator = (SingleWordSuggestionGenerator)tf.Autocomplete.SuggestionGenerator;
  46. generator.AllSuggestions = new List<string> { "FISH" };
  47. tf.Draw ();
  48. tf.PositionCursor ();
  49. TestHelpers.AssertDriverContentsAre ("", output);
  50. tf.ProcessKey (new KeyEvent (Key.m, new KeyModifiers ()));
  51. tf.ProcessKey (new KeyEvent (Key.y, new KeyModifiers ()));
  52. tf.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  53. tf.ProcessKey (new KeyEvent (Key.f, new KeyModifiers ()));
  54. // Even though there is no match on case we should still get the suggestion
  55. tf.Draw ();
  56. tf.PositionCursor ();
  57. TestHelpers.AssertDriverContentsAre ("my fISH", output);
  58. Assert.Equal ("my f", tf.Text);
  59. // When tab completing the case of the whole suggestion should be applied
  60. Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  61. tf.Draw ();
  62. TestHelpers.AssertDriverContentsAre ("my FISH", output);
  63. Assert.Equal ("my FISH", tf.Text);
  64. }
  65. [Fact, AutoInitShutdown]
  66. public void TestAutoAppend_AfterCloseKey_NoAutocomplete ()
  67. {
  68. var tf = GetTextFieldsInViewSuggesting ("fish");
  69. // f is typed and suggestion is "fish"
  70. Application.Driver.SendKeys ('f', ConsoleKey.F, false, false, false);
  71. tf.Draw ();
  72. tf.PositionCursor ();
  73. TestHelpers.AssertDriverContentsAre ("fish", output);
  74. Assert.Equal ("f", tf.Text);
  75. // When cancelling autocomplete
  76. Application.Driver.SendKeys ('e', ConsoleKey.Escape, false, false, false);
  77. // Suggestion should disapear
  78. tf.Draw ();
  79. TestHelpers.AssertDriverContentsAre ("f", output);
  80. Assert.Equal ("f", tf.Text);
  81. // Still has focus though
  82. Assert.Same (tf, Application.Top.Focused);
  83. // But can tab away
  84. Application.Driver.SendKeys ('\t', ConsoleKey.Tab, false, false, false);
  85. Assert.NotSame (tf, Application.Top.Focused);
  86. }
  87. [Fact, AutoInitShutdown]
  88. public void TestAutoAppend_AfterCloseKey_ReapearsOnLetter ()
  89. {
  90. var tf = GetTextFieldsInViewSuggesting ("fish");
  91. // f is typed and suggestion is "fish"
  92. Application.Driver.SendKeys ('f', ConsoleKey.F, false, false, false);
  93. tf.Draw ();
  94. tf.PositionCursor ();
  95. TestHelpers.AssertDriverContentsAre ("fish", output);
  96. Assert.Equal ("f", tf.Text);
  97. // When cancelling autocomplete
  98. Application.Driver.SendKeys ('e', ConsoleKey.Escape, false, false, false);
  99. // Suggestion should disapear
  100. tf.Draw ();
  101. TestHelpers.AssertDriverContentsAre ("f", output);
  102. Assert.Equal ("f", tf.Text);
  103. // Should reapear when you press next letter
  104. Application.Driver.SendKeys ('i', ConsoleKey.I, false, false, false);
  105. tf.Draw ();
  106. // BUGBUG: v2 - I broke this test and don't have time to figure out why. @tznind - help!
  107. //TestHelpers.AssertDriverContentsAre ("fish", output);
  108. Assert.Equal ("fi", tf.Text);
  109. }
  110. [Theory, AutoInitShutdown]
  111. [InlineData ("ffffffffffffffffffffffffff", "ffffffffff")]
  112. [InlineData ("f234567890", "f234567890")]
  113. [InlineData ("fisérables", "fisérables")]
  114. public void TestAutoAppendRendering_ShouldNotOverspill (string overspillUsing, string expectRender)
  115. {
  116. var tf = GetTextFieldsInViewSuggesting (overspillUsing);
  117. // f is typed we should only see 'f' up to size of View (10)
  118. Application.Driver.SendKeys ('f', ConsoleKey.F, false, false, false);
  119. tf.Draw ();
  120. tf.PositionCursor ();
  121. TestHelpers.AssertDriverContentsAre (expectRender, output);
  122. Assert.Equal ("f", tf.Text);
  123. }
  124. [Theory, AutoInitShutdown]
  125. [InlineData (ConsoleKey.UpArrow)]
  126. [InlineData (ConsoleKey.DownArrow)]
  127. public void TestAutoAppend_CycleSelections (ConsoleKey cycleKey)
  128. {
  129. var tf = GetTextFieldsInViewSuggesting ("fish", "friend");
  130. // f is typed and suggestion is "fish"
  131. Application.Driver.SendKeys ('f', ConsoleKey.F, false, false, false);
  132. tf.Draw ();
  133. tf.PositionCursor ();
  134. TestHelpers.AssertDriverContentsAre ("fish", output);
  135. Assert.Equal ("f", tf.Text);
  136. // When cycling autocomplete
  137. Application.Driver.SendKeys (' ', cycleKey, false, false, false);
  138. tf.Draw ();
  139. tf.PositionCursor ();
  140. TestHelpers.AssertDriverContentsAre ("friend", output);
  141. Assert.Equal ("f", tf.Text);
  142. // Should be able to cycle in circles endlessly
  143. Application.Driver.SendKeys (' ', cycleKey, false, false, false);
  144. tf.Draw ();
  145. tf.PositionCursor ();
  146. TestHelpers.AssertDriverContentsAre ("fish", output);
  147. Assert.Equal ("f", tf.Text);
  148. }
  149. [Fact, AutoInitShutdown]
  150. public void TestAutoAppend_NoRender_WhenNoMatch ()
  151. {
  152. var tf = GetTextFieldsInViewSuggesting ("fish");
  153. // f is typed and suggestion is "fish"
  154. Application.Driver.SendKeys ('f', ConsoleKey.F, false, false, false);
  155. tf.Draw ();
  156. tf.PositionCursor ();
  157. TestHelpers.AssertDriverContentsAre ("fish", output);
  158. Assert.Equal ("f", tf.Text);
  159. // x is typed and suggestion should disapear
  160. Application.Driver.SendKeys ('x', ConsoleKey.F, false, false, false);
  161. tf.Draw ();
  162. TestHelpers.AssertDriverContentsAre ("fx", output);
  163. Assert.Equal ("fx", tf.Text);
  164. }
  165. [Fact, AutoInitShutdown]
  166. public void TestAutoAppend_NoRender_WhenCursorNotAtEnd ()
  167. {
  168. var tf = GetTextFieldsInViewSuggesting ("fish");
  169. // f is typed and suggestion is "fish"
  170. Application.Driver.SendKeys ('f', ConsoleKey.F, false, false, false);
  171. tf.Draw ();
  172. tf.PositionCursor ();
  173. TestHelpers.AssertDriverContentsAre ("fish", output);
  174. Assert.Equal ("f", tf.Text);
  175. // add a space then go back 1
  176. Application.Driver.SendKeys (' ', ConsoleKey.Spacebar, false, false, false);
  177. Application.Driver.SendKeys ('<', ConsoleKey.LeftArrow, false, false, false);
  178. tf.Draw ();
  179. TestHelpers.AssertDriverContentsAre ("f", output);
  180. Assert.Equal ("f ", tf.Text);
  181. }
  182. private TextField GetTextFieldsInViewSuggesting (params string [] suggestions)
  183. {
  184. var tf = GetTextFieldsInView ();
  185. tf.Autocomplete = new AppendAutocomplete (tf);
  186. var generator = (SingleWordSuggestionGenerator)tf.Autocomplete.SuggestionGenerator;
  187. generator.AllSuggestions = suggestions.ToList ();
  188. tf.Draw ();
  189. tf.PositionCursor ();
  190. TestHelpers.AssertDriverContentsAre ("", output);
  191. return tf;
  192. }
  193. private TextField GetTextFieldsInView ()
  194. {
  195. var tf = new TextField {
  196. Width = 10
  197. };
  198. var tf2 = new TextField {
  199. Y = 1,
  200. Width = 10
  201. };
  202. var top = Application.Top;
  203. top.Add (tf);
  204. top.Add (tf2);
  205. Application.Begin (top);
  206. Assert.Same (tf, top.Focused);
  207. return tf;
  208. }
  209. }
  210. }