AppendAutocompleteTests.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.ViewTests {
  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. TestHelpers.AssertDriverContentsAre("fish",output);
  101. Assert.Equal("fi",tf.Text.ToString());
  102. }
  103. [Theory, AutoInitShutdown]
  104. [InlineData("ffffffffffffffffffffffffff","ffffffffff")]
  105. [InlineData("f234567890","f234567890")]
  106. [InlineData("fisérables","fisérables")]
  107. public void TestAutoAppendRendering_ShouldNotOverspill(string overspillUsing,string expectRender)
  108. {
  109. var tf = GetTextFieldsInViewSuggesting(overspillUsing);
  110. // f is typed we should only see 'f' up to size of View (10)
  111. Application.Driver.SendKeys('f',ConsoleKey.F,false,false,false);
  112. tf.Redraw(tf.Bounds);
  113. TestHelpers.AssertDriverContentsAre(expectRender,output);
  114. Assert.Equal("f",tf.Text.ToString());
  115. }
  116. [Theory, AutoInitShutdown]
  117. [InlineData(ConsoleKey.UpArrow)]
  118. [InlineData(ConsoleKey.DownArrow)]
  119. public void TestAutoAppend_CycleSelections(ConsoleKey cycleKey)
  120. {
  121. var tf = GetTextFieldsInViewSuggesting("fish","friend");
  122. // f is typed and suggestion is "fish"
  123. Application.Driver.SendKeys('f',ConsoleKey.F,false,false,false);
  124. tf.Redraw(tf.Bounds);
  125. TestHelpers.AssertDriverContentsAre("fish",output);
  126. Assert.Equal("f",tf.Text.ToString());
  127. // When cycling autocomplete
  128. Application.Driver.SendKeys(' ',cycleKey,false,false,false);
  129. tf.Redraw(tf.Bounds);
  130. TestHelpers.AssertDriverContentsAre("friend",output);
  131. Assert.Equal("f",tf.Text.ToString());
  132. // Should be able to cycle in circles endlessly
  133. Application.Driver.SendKeys(' ',cycleKey,false,false,false);
  134. tf.Redraw(tf.Bounds);
  135. TestHelpers.AssertDriverContentsAre("fish",output);
  136. Assert.Equal("f",tf.Text.ToString());
  137. }
  138. [Fact, AutoInitShutdown]
  139. public void TestAutoAppend_NoRender_WhenNoMatch()
  140. {
  141. var tf = GetTextFieldsInViewSuggesting("fish");
  142. // f is typed and suggestion is "fish"
  143. Application.Driver.SendKeys('f',ConsoleKey.F,false,false,false);
  144. tf.Redraw(tf.Bounds);
  145. TestHelpers.AssertDriverContentsAre("fish",output);
  146. Assert.Equal("f",tf.Text.ToString());
  147. // x is typed and suggestion should disapear
  148. Application.Driver.SendKeys('x',ConsoleKey.F,false,false,false);
  149. tf.Redraw(tf.Bounds);
  150. TestHelpers.AssertDriverContentsAre("fx",output);
  151. Assert.Equal("fx",tf.Text.ToString());
  152. }
  153. [Fact, AutoInitShutdown]
  154. public void TestAutoAppend_NoRender_WhenCursorNotAtEnd()
  155. {
  156. var tf = GetTextFieldsInViewSuggesting("fish");
  157. // f is typed and suggestion is "fish"
  158. Application.Driver.SendKeys('f',ConsoleKey.F,false,false,false);
  159. tf.Redraw(tf.Bounds);
  160. TestHelpers.AssertDriverContentsAre("fish",output);
  161. Assert.Equal("f",tf.Text.ToString());
  162. // add a space then go back 1
  163. Application.Driver.SendKeys(' ',ConsoleKey.Spacebar,false,false,false);
  164. Application.Driver.SendKeys('<',ConsoleKey.LeftArrow,false,false,false);
  165. tf.Redraw(tf.Bounds);
  166. TestHelpers.AssertDriverContentsAre("f",output);
  167. Assert.Equal("f ",tf.Text.ToString());
  168. }
  169. private TextField GetTextFieldsInViewSuggesting (params string[] suggestions)
  170. {
  171. var tf = GetTextFieldsInView();
  172. tf.Autocomplete = new AppendAutocomplete(tf);
  173. var generator = (SingleWordSuggestionGenerator)tf.Autocomplete.SuggestionGenerator;
  174. generator.AllSuggestions = suggestions.ToList();
  175. tf.Redraw(tf.Bounds);
  176. TestHelpers.AssertDriverContentsAre("",output);
  177. return tf;
  178. }
  179. private TextField GetTextFieldsInView ()
  180. {
  181. var tf = new TextField{
  182. Width = 10
  183. };
  184. var tf2 = new TextField{
  185. Y = 1,
  186. Width = 10
  187. };
  188. var top = Application.Top;
  189. top.Add (tf);
  190. top.Add (tf2);
  191. Application.Begin (top);
  192. Assert.Same(tf,top.Focused);
  193. return tf;
  194. }
  195. }
  196. }