ListViewTests.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Xunit;
  8. namespace Terminal.Gui.Views {
  9. public class ListViewTests {
  10. [Fact]
  11. public void Constructors_Defaults ()
  12. {
  13. var lv = new ListView ();
  14. Assert.Null (lv.Source);
  15. Assert.True (lv.CanFocus);
  16. lv = new ListView (new List<string> () { "One", "Two", "Three" });
  17. Assert.NotNull (lv.Source);
  18. lv = new ListView (new NewListDataSource ());
  19. Assert.NotNull (lv.Source);
  20. lv = new ListView (new Rect (0, 1, 10, 20), new List<string> () { "One", "Two", "Three" });
  21. Assert.NotNull (lv.Source);
  22. Assert.Equal (new Rect (0, 1, 10, 20), lv.Frame);
  23. lv = new ListView (new Rect (0, 1, 10, 20), new NewListDataSource ());
  24. Assert.NotNull (lv.Source);
  25. Assert.Equal (new Rect (0, 1, 10, 20), lv.Frame);
  26. }
  27. private class NewListDataSource : IListDataSource {
  28. public int Count => throw new NotImplementedException ();
  29. public int Length => throw new NotImplementedException ();
  30. public bool IsMarked (int item)
  31. {
  32. throw new NotImplementedException ();
  33. }
  34. public void Render (ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width, int start = 0)
  35. {
  36. throw new NotImplementedException ();
  37. }
  38. public void SetMark (int item, bool value)
  39. {
  40. throw new NotImplementedException ();
  41. }
  42. public IList ToList ()
  43. {
  44. throw new NotImplementedException ();
  45. }
  46. }
  47. [Fact]
  48. public void KeyBindings_Command ()
  49. {
  50. List<string> source = new List<string> () { "One", "Two", "Three" };
  51. ListView lv = new ListView (source) { Height = 2, AllowsMarking = true };
  52. Assert.Equal (0, lv.SelectedItem);
  53. Assert.True (lv.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  54. Assert.Equal (1, lv.SelectedItem);
  55. Assert.True (lv.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  56. Assert.Equal (0, lv.SelectedItem);
  57. Assert.True (lv.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ())));
  58. Assert.Equal (2, lv.SelectedItem);
  59. Assert.Equal (2, lv.TopItem);
  60. Assert.True (lv.ProcessKey (new KeyEvent (Key.PageUp, new KeyModifiers ())));
  61. Assert.Equal (0, lv.SelectedItem);
  62. Assert.Equal (0, lv.TopItem);
  63. Assert.False (lv.Source.IsMarked (lv.SelectedItem));
  64. Assert.True (lv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ())));
  65. Assert.True (lv.Source.IsMarked (lv.SelectedItem));
  66. var opened = false;
  67. lv.OpenSelectedItem += (_) => opened = true;
  68. Assert.True (lv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  69. Assert.True (opened);
  70. Assert.True (lv.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
  71. Assert.Equal (2, lv.SelectedItem);
  72. Assert.True (lv.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
  73. Assert.Equal (0, lv.SelectedItem);
  74. }
  75. [Fact]
  76. [AutoInitShutdown]
  77. public void RowRender_Event ()
  78. {
  79. var rendered = false;
  80. var source = new List<string> () { "one", "two", "three" };
  81. var lv = new ListView () { Width = Dim.Fill (), Height = Dim.Fill () };
  82. lv.RowRender += _ => rendered = true;
  83. Application.Top.Add (lv);
  84. Application.Begin (Application.Top);
  85. Assert.False (rendered);
  86. lv.SetSource (source);
  87. lv.Redraw (lv.Bounds);
  88. Assert.True (rendered);
  89. }
  90. [Fact]
  91. [AutoInitShutdown]
  92. public void EnsuresVisibilitySelectedItem_Top ()
  93. {
  94. var source = new List<string> () { "First", "Second" };
  95. ListView lv = new ListView (source) { Width = Dim.Fill (), Height = 1 };
  96. lv.SelectedItem = 1;
  97. Application.Top.Add (lv);
  98. Application.Begin (Application.Top);
  99. Assert.Equal ("Second ", GetContents (0));
  100. Assert.Equal (new (' ', 7), GetContents (1));
  101. lv.MoveUp ();
  102. lv.Redraw (lv.Bounds);
  103. Assert.Equal ("First ", GetContents (0));
  104. Assert.Equal (new (' ', 7), GetContents (1));
  105. string GetContents (int line)
  106. {
  107. var item = "";
  108. for (int i = 0; i < 7; i++) {
  109. item += (char)Application.Driver.Contents [line, i, 0];
  110. }
  111. return item;
  112. }
  113. }
  114. }
  115. }