AppendAutocompleteTests.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_AfterCloseKey_NoAutocomplete()
  40. {
  41. var tf = GetTextFieldsInViewSuggesting("fish");
  42. // f is typed and suggestion is "fish"
  43. Application.Driver.SendKeys('f',ConsoleKey.F,false,false,false);
  44. tf.Redraw(tf.Bounds);
  45. TestHelpers.AssertDriverContentsAre("fish",output);
  46. Assert.Equal("f",tf.Text.ToString());
  47. // When cancelling autocomplete
  48. Application.Driver.SendKeys('e',ConsoleKey.Escape,false,false,false);
  49. // Suggestion should disapear
  50. tf.Redraw(tf.Bounds);
  51. TestHelpers.AssertDriverContentsAre("f",output);
  52. Assert.Equal("f",tf.Text.ToString());
  53. // Still has focus though
  54. Assert.Same(tf,Application.Top.Focused);
  55. // But can tab away
  56. Application.Driver.SendKeys('\t',ConsoleKey.Tab,false,false,false);
  57. Assert.NotSame(tf,Application.Top.Focused);
  58. }
  59. [Fact, AutoInitShutdown]
  60. public void TestAutoAppend_AfterCloseKey_ReapearsOnLetter()
  61. {
  62. var tf = GetTextFieldsInViewSuggesting("fish");
  63. // f is typed and suggestion is "fish"
  64. Application.Driver.SendKeys('f',ConsoleKey.F,false,false,false);
  65. tf.Redraw(tf.Bounds);
  66. TestHelpers.AssertDriverContentsAre("fish",output);
  67. Assert.Equal("f",tf.Text.ToString());
  68. // When cancelling autocomplete
  69. Application.Driver.SendKeys('e',ConsoleKey.Escape,false,false,false);
  70. // Suggestion should disapear
  71. tf.Redraw(tf.Bounds);
  72. TestHelpers.AssertDriverContentsAre("f",output);
  73. Assert.Equal("f",tf.Text.ToString());
  74. // Should reapear when you press next letter
  75. Application.Driver.SendKeys('i',ConsoleKey.I,false,false,false);
  76. tf.Redraw(tf.Bounds);
  77. TestHelpers.AssertDriverContentsAre("fish",output);
  78. Assert.Equal("fi",tf.Text.ToString());
  79. }
  80. [Theory, AutoInitShutdown]
  81. [InlineData(ConsoleKey.UpArrow)]
  82. [InlineData(ConsoleKey.DownArrow)]
  83. public void TestAutoAppend_CycleSelections(ConsoleKey cycleKey)
  84. {
  85. var tf = GetTextFieldsInViewSuggesting("fish","friend");
  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 cycling autocomplete
  92. Application.Driver.SendKeys(' ',cycleKey,false,false,false);
  93. tf.Redraw(tf.Bounds);
  94. TestHelpers.AssertDriverContentsAre("friend",output);
  95. Assert.Equal("f",tf.Text.ToString());
  96. // Should be able to cycle in circles endlessly
  97. Application.Driver.SendKeys(' ',cycleKey,false,false,false);
  98. tf.Redraw(tf.Bounds);
  99. TestHelpers.AssertDriverContentsAre("fish",output);
  100. Assert.Equal("f",tf.Text.ToString());
  101. }
  102. private TextField GetTextFieldsInViewSuggesting (params string[] suggestions)
  103. {
  104. var tf = GetTextFieldsInView();
  105. tf.Autocomplete = new AppendAutocomplete(tf);
  106. var generator = (SingleWordSuggestionGenerator)tf.Autocomplete.SuggestionGenerator;
  107. generator.AllSuggestions = suggestions.ToList();
  108. tf.Redraw(tf.Bounds);
  109. TestHelpers.AssertDriverContentsAre("",output);
  110. return tf;
  111. }
  112. private TextField GetTextFieldsInView ()
  113. {
  114. var tf = new TextField{
  115. Width = 10
  116. };
  117. var tf2 = new TextField{
  118. Y = 1,
  119. Width = 10
  120. };
  121. var top = Application.Top;
  122. top.Add (tf);
  123. top.Add (tf2);
  124. Application.Begin (top);
  125. Assert.Same(tf,top.Focused);
  126. return tf;
  127. }
  128. }
  129. }