AppendAutocompleteTests.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 = GetTextFieldsInViewSuggestingFish();
  42. // f is typed and suggestion is "fish"
  43. tf.Redraw(tf.Bounds);
  44. TestHelpers.AssertDriverContentsAre("fish",output);
  45. Assert.Equal("f",tf.Text.ToString());
  46. // When cancelling autocomplete
  47. Application.Driver.SendKeys('e',ConsoleKey.Escape,false,false,false);
  48. // Suggestion should disapear
  49. tf.Redraw(tf.Bounds);
  50. TestHelpers.AssertDriverContentsAre("f",output);
  51. Assert.Equal("f",tf.Text.ToString());
  52. // Still has focus though
  53. Assert.Same(tf,Application.Top.Focused);
  54. // But can tab away
  55. Application.Driver.SendKeys('\t',ConsoleKey.Tab,false,false,false);
  56. Assert.NotSame(tf,Application.Top.Focused);
  57. }
  58. private TextField GetTextFieldsInViewSuggestingFish ()
  59. {
  60. var tf = GetTextFieldsInView();
  61. tf.Autocomplete = new AppendAutocomplete(tf);
  62. var generator = (SingleWordSuggestionGenerator)tf.Autocomplete.SuggestionGenerator;
  63. generator.AllSuggestions = new List<string>{"fish"};
  64. tf.Redraw(tf.Bounds);
  65. TestHelpers.AssertDriverContentsAre("",output);
  66. tf.ProcessKey(new KeyEvent(Key.f,new KeyModifiers()));
  67. tf.Redraw(tf.Bounds);
  68. TestHelpers.AssertDriverContentsAre("fish",output);
  69. Assert.Equal("f",tf.Text.ToString());
  70. return tf;
  71. }
  72. private TextField GetTextFieldsInView ()
  73. {
  74. var tf = new TextField{
  75. Width = 10
  76. };
  77. var tf2 = new TextField{
  78. Y = 1,
  79. Width = 10
  80. };
  81. var top = Application.Top;
  82. top.Add (tf);
  83. top.Add (tf2);
  84. Application.Begin (top);
  85. Assert.Same(tf,top.Focused);
  86. return tf;
  87. }
  88. }
  89. }