AppendAutocompleteTests.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 TestAutocomplete_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. private TextField GetTextFieldsInView ()
  39. {
  40. var tf = new TextField{
  41. Width = 10
  42. };
  43. var tf2 = new TextField{
  44. Y = 1,
  45. Width = 10
  46. };
  47. var top = Application.Top;
  48. top.Add (tf);
  49. top.Add (tf2);
  50. Application.Begin (top);
  51. Assert.Same(tf,top.Focused);
  52. return tf;
  53. }
  54. }
  55. }