ListViewTests.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. [Fact]
  28. public void ListViewSelectThenDown ()
  29. {
  30. var lv = new ListView (new List<string> () { "One", "Two", "Three" });
  31. lv.AllowsMarking = true;
  32. Assert.NotNull (lv.Source);
  33. // first item should be selected by default
  34. Assert.Equal (0, lv.SelectedItem);
  35. // nothing is ticked
  36. Assert.False (lv.Source.IsMarked (0));
  37. Assert.False (lv.Source.IsMarked (1));
  38. Assert.False (lv.Source.IsMarked (2));
  39. lv.AddKeyBinding (Key.Space | Key.ShiftMask, Command.ToggleChecked, Command.LineDown);
  40. var ev = new KeyEvent (Key.Space | Key.ShiftMask, new KeyModifiers () { Shift = true });
  41. // view should indicate that it has accepted and consumed the event
  42. Assert.True (lv.ProcessKey (ev));
  43. // second item should now be selected
  44. Assert.Equal (1, lv.SelectedItem);
  45. // first item only should be ticked
  46. Assert.True (lv.Source.IsMarked (0));
  47. Assert.False (lv.Source.IsMarked (1));
  48. Assert.False (lv.Source.IsMarked (2));
  49. // Press key combo again
  50. Assert.True (lv.ProcessKey (ev));
  51. Assert.Equal (2, lv.SelectedItem);
  52. Assert.True (lv.Source.IsMarked (0));
  53. Assert.True (lv.Source.IsMarked (1));
  54. Assert.False (lv.Source.IsMarked (2));
  55. // Press key combo again
  56. Assert.True (lv.ProcessKey (ev));
  57. Assert.Equal (2, lv.SelectedItem); // cannot move down any further
  58. Assert.True (lv.Source.IsMarked (0));
  59. Assert.True (lv.Source.IsMarked (1));
  60. Assert.True (lv.Source.IsMarked (2)); // but can toggle marked
  61. // Press key combo again
  62. Assert.True (lv.ProcessKey (ev));
  63. Assert.Equal (2, lv.SelectedItem); // cannot move down any further
  64. Assert.True (lv.Source.IsMarked (0));
  65. Assert.True (lv.Source.IsMarked (1));
  66. Assert.False (lv.Source.IsMarked (2)); // untoggle toggle marked
  67. }
  68. [Fact]
  69. public void SettingEmptyKeybindingThrows ()
  70. {
  71. var lv = new ListView (new List<string> () { "One", "Two", "Three" });
  72. Assert.Throws<ArgumentException> (() => lv.AddKeyBinding (Key.Space));
  73. }
  74. /// <summary>
  75. /// Tests that when none of the Commands in a chained keybinding are possible
  76. /// the <see cref="View.ProcessKey(KeyEvent)"/> returns the appropriate result
  77. /// </summary>
  78. [Fact]
  79. public void ListViewProcessKeyReturnValue_WithMultipleCommands ()
  80. {
  81. var lv = new ListView (new List<string> () { "One", "Two", "Three", "Four" });
  82. Assert.NotNull (lv.Source);
  83. // first item should be selected by default
  84. Assert.Equal (0, lv.SelectedItem);
  85. // bind shift down to move down twice in control
  86. lv.AddKeyBinding (Key.CursorDown | Key.ShiftMask, Command.LineDown, Command.LineDown);
  87. var ev = new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers () { Shift = true });
  88. Assert.True (lv.ProcessKey (ev), "The first time we move down 2 it should be possible");
  89. // After moving down twice from One we should be at 'Three'
  90. Assert.Equal (2, lv.SelectedItem);
  91. // clear the items
  92. lv.SetSource (null);
  93. // Press key combo again - return should be false this time as none of the Commands are allowable
  94. Assert.False (lv.ProcessKey (ev), "We cannot move down so will not respond to this");
  95. }
  96. private class NewListDataSource : IListDataSource {
  97. public int Count => throw new NotImplementedException ();
  98. public int Length => throw new NotImplementedException ();
  99. public bool IsMarked (int item)
  100. {
  101. throw new NotImplementedException ();
  102. }
  103. public void Render (ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width, int start = 0)
  104. {
  105. throw new NotImplementedException ();
  106. }
  107. public void SetMark (int item, bool value)
  108. {
  109. throw new NotImplementedException ();
  110. }
  111. public IList ToList ()
  112. {
  113. throw new NotImplementedException ();
  114. }
  115. }
  116. [Fact]
  117. public void KeyBindings_Command ()
  118. {
  119. List<string> source = new List<string> () { "One", "Two", "Three" };
  120. ListView lv = new ListView (source) { Height = 2, AllowsMarking = true };
  121. Assert.Equal (0, lv.SelectedItem);
  122. Assert.True (lv.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  123. Assert.Equal (1, lv.SelectedItem);
  124. Assert.True (lv.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ())));
  125. Assert.Equal (0, lv.SelectedItem);
  126. Assert.True (lv.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ())));
  127. Assert.Equal (2, lv.SelectedItem);
  128. Assert.Equal (2, lv.TopItem);
  129. Assert.True (lv.ProcessKey (new KeyEvent (Key.PageUp, new KeyModifiers ())));
  130. Assert.Equal (0, lv.SelectedItem);
  131. Assert.Equal (0, lv.TopItem);
  132. Assert.False (lv.Source.IsMarked (lv.SelectedItem));
  133. Assert.True (lv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ())));
  134. Assert.True (lv.Source.IsMarked (lv.SelectedItem));
  135. var opened = false;
  136. lv.OpenSelectedItem += (_) => opened = true;
  137. Assert.True (lv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  138. Assert.True (opened);
  139. Assert.True (lv.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
  140. Assert.Equal (2, lv.SelectedItem);
  141. Assert.True (lv.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
  142. Assert.Equal (0, lv.SelectedItem);
  143. }
  144. [Fact]
  145. [AutoInitShutdown]
  146. public void RowRender_Event ()
  147. {
  148. var rendered = false;
  149. var source = new List<string> () { "one", "two", "three" };
  150. var lv = new ListView () { Width = Dim.Fill (), Height = Dim.Fill () };
  151. lv.RowRender += _ => rendered = true;
  152. Application.Top.Add (lv);
  153. Application.Begin (Application.Top);
  154. Assert.False (rendered);
  155. lv.SetSource (source);
  156. lv.Redraw (lv.Bounds);
  157. Assert.True (rendered);
  158. }
  159. [Fact]
  160. [AutoInitShutdown]
  161. public void EnsuresVisibilitySelectedItem_Top ()
  162. {
  163. var source = new List<string> () { "First", "Second" };
  164. ListView lv = new ListView (source) { Width = Dim.Fill (), Height = 1 };
  165. lv.SelectedItem = 1;
  166. Application.Top.Add (lv);
  167. Application.Begin (Application.Top);
  168. Assert.Equal ("Second ", GetContents (0));
  169. Assert.Equal (new (' ', 7), GetContents (1));
  170. lv.MoveUp ();
  171. lv.Redraw (lv.Bounds);
  172. Assert.Equal ("First ", GetContents (0));
  173. Assert.Equal (new (' ', 7), GetContents (1));
  174. string GetContents (int line)
  175. {
  176. var item = "";
  177. for (int i = 0; i < 7; i++) {
  178. item += (char)Application.Driver.Contents [line, i, 0];
  179. }
  180. return item;
  181. }
  182. }
  183. }
  184. }