TreeTableSourceTests.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using Xunit;
  7. using Xunit.Abstractions;
  8. namespace Terminal.Gui.ViewsTests;
  9. public class TreeTableSourceTests: IDisposable {
  10. readonly ITestOutputHelper _output;
  11. private readonly Rune _origChecked;
  12. private readonly Rune _origUnchecked;
  13. public TreeTableSourceTests (ITestOutputHelper output)
  14. {
  15. _output = output;
  16. _origChecked = ConfigurationManager.Glyphs.Checked;
  17. _origUnchecked = ConfigurationManager.Glyphs.UnChecked;
  18. ConfigurationManager.Glyphs.Checked = new Rune ('☑');
  19. ConfigurationManager.Glyphs.UnChecked = new Rune ('☐');
  20. }
  21. [Fact, AutoInitShutdown]
  22. public void TestTreeTableSource_BasicExpanding_WithKeyboard ()
  23. {
  24. var tv = GetTreeTable (out _);
  25. tv.Style.GetOrCreateColumnStyle (1).MinAcceptableWidth = 1;
  26. tv.Draw ();
  27. string expected =
  28. @"
  29. │Name │Description │
  30. ├──────────────┼───────────────────────┤
  31. │├+Lost Highway│Exciting night road │
  32. │└+Route 66 │Great race course │";
  33. TestHelpers.AssertDriverContentsAre (expected, _output);
  34. Assert.Equal(2, tv.Table.Rows);
  35. // top left is selected cell
  36. Assert.Equal (0, tv.SelectedRow);
  37. Assert.Equal(0, tv.SelectedColumn);
  38. // when pressing right we should expand the top route
  39. Application.Top.ProcessHotKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
  40. tv.Draw ();
  41. expected =
  42. @"
  43. │Name │Description │
  44. ├─────────────────┼────────────────────┤
  45. │├-Lost Highway │Exciting night road │
  46. ││ ├─Ford Trans-Am│Talking thunderbird │
  47. ││ └─DeLorean │Time travelling car │
  48. │└+Route 66 │Great race course │
  49. ";
  50. TestHelpers.AssertDriverContentsAre (expected, _output);
  51. // when pressing left we should collapse the top route again
  52. Application.Top.ProcessHotKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ()));
  53. tv.Draw ();
  54. expected =
  55. @"
  56. │Name │Description │
  57. ├──────────────┼───────────────────────┤
  58. │├+Lost Highway│Exciting night road │
  59. │└+Route 66 │Great race course │
  60. ";
  61. TestHelpers.AssertDriverContentsAre (expected, _output);
  62. }
  63. [Fact, AutoInitShutdown]
  64. public void TestTreeTableSource_BasicExpanding_WithMouse ()
  65. {
  66. var tv = GetTreeTable (out _);
  67. tv.Style.GetOrCreateColumnStyle (1).MinAcceptableWidth = 1;
  68. tv.Draw ();
  69. string expected =
  70. @"
  71. │Name │Description │
  72. ├──────────────┼───────────────────────┤
  73. │├+Lost Highway│Exciting night road │
  74. │└+Route 66 │Great race course │";
  75. TestHelpers.AssertDriverContentsAre (expected, _output);
  76. Assert.Equal (2, tv.Table.Rows);
  77. // top left is selected cell
  78. Assert.Equal (0, tv.SelectedRow);
  79. Assert.Equal (0, tv.SelectedColumn);
  80. Assert.True(tv.OnMouseEvent (new MouseEvent () { X = 2,Y=2,Flags = MouseFlags.Button1Clicked}));
  81. tv.Draw ();
  82. expected =
  83. @"
  84. │Name │Description │
  85. ├─────────────────┼────────────────────┤
  86. │├-Lost Highway │Exciting night road │
  87. ││ ├─Ford Trans-Am│Talking thunderbird │
  88. ││ └─DeLorean │Time travelling car │
  89. │└+Route 66 │Great race course │
  90. ";
  91. TestHelpers.AssertDriverContentsAre (expected, _output);
  92. // Clicking to the right/left of the expand/collapse does nothing
  93. tv.OnMouseEvent (new MouseEvent () { X = 3, Y = 2, Flags = MouseFlags.Button1Clicked });
  94. tv.Draw ();
  95. TestHelpers.AssertDriverContentsAre (expected, _output);
  96. tv.OnMouseEvent (new MouseEvent () { X = 1, Y = 2, Flags = MouseFlags.Button1Clicked });
  97. tv.Draw ();
  98. TestHelpers.AssertDriverContentsAre (expected, _output);
  99. // Clicking on the + again should collapse
  100. tv.OnMouseEvent (new MouseEvent () { X = 2, Y = 2, Flags = MouseFlags.Button1Clicked });
  101. tv.Draw ();
  102. expected =
  103. @"
  104. │Name │Description │
  105. ├──────────────┼───────────────────────┤
  106. │├+Lost Highway│Exciting night road │
  107. │└+Route 66 │Great race course │";
  108. TestHelpers.AssertDriverContentsAre (expected, _output);
  109. }
  110. [Fact, AutoInitShutdown]
  111. public void TestTreeTableSource_CombinedWithCheckboxes ()
  112. {
  113. var tv = GetTreeTable (out var treeSource);
  114. CheckBoxTableSourceWrapperByIndex checkSource;
  115. tv.Table = checkSource = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
  116. tv.Style.GetOrCreateColumnStyle (2).MinAcceptableWidth = 1;
  117. tv.Draw ();
  118. string expected =
  119. @"
  120. │ │Name │Description │
  121. ├─┼──────────────┼─────────────────────┤
  122. │☐│├+Lost Highway│Exciting night road │
  123. │☐│└+Route 66 │Great race course │
  124. ";
  125. TestHelpers.AssertDriverContentsAre (expected, _output);
  126. Assert.Equal (2, tv.Table.Rows);
  127. // top left is selected cell
  128. Assert.Equal (0, tv.SelectedRow);
  129. Assert.Equal (0, tv.SelectedColumn);
  130. // when pressing right we move to tree column
  131. tv.ProcessKey(new KeyEvent (Key.CursorRight, new KeyModifiers ()));
  132. // now we are in tree column
  133. Assert.Equal (0, tv.SelectedRow);
  134. Assert.Equal (1, tv.SelectedColumn);
  135. Application.Top.ProcessHotKey (new KeyEvent (Key.CursorRight, new KeyModifiers ()));
  136. tv.Draw ();
  137. expected =
  138. @"
  139. │ │Name │Description │
  140. ├─┼─────────────────┼──────────────────┤
  141. │☐│├-Lost Highway │Exciting night roa│
  142. │☐││ ├─Ford Trans-Am│Talking thunderbir│
  143. │☐││ └─DeLorean │Time travelling ca│
  144. │☐│└+Route 66 │Great race course │
  145. ";
  146. TestHelpers.AssertDriverContentsAre (expected, _output);
  147. tv.ProcessKey(new KeyEvent(Key.CursorDown,new KeyModifiers ()));
  148. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  149. tv.Draw ();
  150. expected =
  151. @"
  152. │ │Name │Description │
  153. ├─┼─────────────────┼──────────────────┤
  154. │☐│├-Lost Highway │Exciting night roa│
  155. │☑││ ├─Ford Trans-Am│Talking thunderbir│
  156. │☐││ └─DeLorean │Time travelling ca│
  157. │☐│└+Route 66 │Great race course │
  158. ";
  159. TestHelpers.AssertDriverContentsAre (expected, _output);
  160. var selectedObjects = checkSource.CheckedRows.Select (treeSource.GetObjectOnRow).ToArray();
  161. var selected = Assert.Single(selectedObjects);
  162. Assert.Equal ("Ford Trans-Am",selected.Name);
  163. Assert.Equal ("Talking thunderbird car", selected.Description);
  164. }
  165. interface IDescribedThing {
  166. string Name { get; }
  167. string Description { get; }
  168. }
  169. class Road : IDescribedThing {
  170. public string Name { get; set; }
  171. public string Description { get; set; }
  172. public List<Car> Traffic { get; set; }
  173. }
  174. class Car : IDescribedThing {
  175. public string Name { get; set; }
  176. public string Description { get; set; }
  177. }
  178. private TableView GetTreeTable (out TreeView<IDescribedThing> tree)
  179. {
  180. var tableView = new TableView ();
  181. tableView.ColorScheme = Colors.TopLevel;
  182. tableView.ColorScheme = Colors.TopLevel;
  183. tableView.Bounds = new Rect (0, 0, 40, 6);
  184. tableView.Style.ShowHorizontalHeaderUnderline = true;
  185. tableView.Style.ShowHorizontalHeaderOverline = false;
  186. tableView.Style.AlwaysShowHeaders = true;
  187. tableView.Style.SmoothHorizontalScrolling = true;
  188. tree = new TreeView<IDescribedThing> ();
  189. tree.AspectGetter = (d) => d.Name;
  190. tree.TreeBuilder = new DelegateTreeBuilder<IDescribedThing> (
  191. (d) => d is Road r ? r.Traffic : Enumerable.Empty<IDescribedThing> ()
  192. );
  193. tree.AddObject (new Road {
  194. Name = "Lost Highway",
  195. Description = "Exciting night road",
  196. Traffic = new List<Car> {
  197. new Car { Name = "Ford Trans-Am", Description = "Talking thunderbird car"},
  198. new Car { Name = "DeLorean", Description = "Time travelling car"}
  199. }
  200. });
  201. tree.AddObject (new Road {
  202. Name = "Route 66",
  203. Description = "Great race course",
  204. Traffic = new List<Car> {
  205. new Car { Name = "Pink Compact", Description = "Penelope Pitstop's car"},
  206. new Car { Name = "Mean Machine", Description = "Dick Dastardly's car"}
  207. }
  208. });
  209. tableView.Table = new TreeTableSource<IDescribedThing> (tableView,"Name",tree,
  210. new () {
  211. {"Description",(d)=>d.Description }
  212. });
  213. tableView.BeginInit ();
  214. tableView.EndInit ();
  215. tableView.LayoutSubviews ();
  216. Application.Top.Add (tableView);
  217. Application.Top.EnsureFocus ();
  218. Assert.Equal (tableView, Application.Top.MostFocused);
  219. return tableView;
  220. }
  221. public void Dispose ()
  222. {
  223. ConfigurationManager.Glyphs.Checked = _origChecked;
  224. ConfigurationManager.Glyphs.UnChecked = _origUnchecked;
  225. }
  226. }