AppendAutocompleteTests.cs 8.0 KB

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