2
0

TableViewTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Terminal.Gui;
  7. using Xunit;
  8. using System.Globalization;
  9. using Xunit.Abstractions;
  10. namespace Terminal.Gui.Views {
  11. public class TableViewTests
  12. {
  13. readonly ITestOutputHelper output;
  14. public TableViewTests(ITestOutputHelper output)
  15. {
  16. this.output = output;
  17. }
  18. [Fact]
  19. public void EnsureValidScrollOffsets_WithNoCells()
  20. {
  21. var tableView = new TableView();
  22. Assert.Equal(0,tableView.RowOffset);
  23. Assert.Equal(0,tableView.ColumnOffset);
  24. // Set empty table
  25. tableView.Table = new DataTable();
  26. // Since table has no rows or columns scroll offset should default to 0
  27. tableView.EnsureValidScrollOffsets();
  28. Assert.Equal(0,tableView.RowOffset);
  29. Assert.Equal(0,tableView.ColumnOffset);
  30. }
  31. [Fact]
  32. public void EnsureValidScrollOffsets_LoadSmallerTable()
  33. {
  34. var tableView = new TableView();
  35. tableView.Bounds = new Rect(0,0,25,10);
  36. Assert.Equal(0,tableView.RowOffset);
  37. Assert.Equal(0,tableView.ColumnOffset);
  38. // Set big table
  39. tableView.Table = BuildTable(25,50);
  40. // Scroll down and along
  41. tableView.RowOffset = 20;
  42. tableView.ColumnOffset = 10;
  43. tableView.EnsureValidScrollOffsets();
  44. // The scroll should be valid at the moment
  45. Assert.Equal(20,tableView.RowOffset);
  46. Assert.Equal(10,tableView.ColumnOffset);
  47. // Set small table
  48. tableView.Table = BuildTable(2,2);
  49. // Setting a small table should automatically trigger fixing the scroll offsets to ensure valid cells
  50. Assert.Equal(0,tableView.RowOffset);
  51. Assert.Equal(0,tableView.ColumnOffset);
  52. // Trying to set invalid indexes should not be possible
  53. tableView.RowOffset = 20;
  54. tableView.ColumnOffset = 10;
  55. Assert.Equal(1,tableView.RowOffset);
  56. Assert.Equal(1,tableView.ColumnOffset);
  57. }
  58. [Fact]
  59. public void SelectedCellChanged_NotFiredForSameValue()
  60. {
  61. var tableView = new TableView(){
  62. Table = BuildTable(25,50)
  63. };
  64. bool called = false;
  65. tableView.SelectedCellChanged += (e)=>{called=true;};
  66. Assert.Equal(0,tableView.SelectedColumn);
  67. Assert.False(called);
  68. // Changing value to same as it already was should not raise an event
  69. tableView.SelectedColumn = 0;
  70. Assert.False(called);
  71. tableView.SelectedColumn = 10;
  72. Assert.True(called);
  73. }
  74. [Fact]
  75. public void SelectedCellChanged_SelectedColumnIndexesCorrect()
  76. {
  77. var tableView = new TableView(){
  78. Table = BuildTable(25,50)
  79. };
  80. bool called = false;
  81. tableView.SelectedCellChanged += (e)=>{
  82. called=true;
  83. Assert.Equal(0,e.OldCol);
  84. Assert.Equal(10,e.NewCol);
  85. };
  86. tableView.SelectedColumn = 10;
  87. Assert.True(called);
  88. }
  89. [Fact]
  90. public void SelectedCellChanged_SelectedRowIndexesCorrect()
  91. {
  92. var tableView = new TableView(){
  93. Table = BuildTable(25,50)
  94. };
  95. bool called = false;
  96. tableView.SelectedCellChanged += (e)=>{
  97. called=true;
  98. Assert.Equal(0,e.OldRow);
  99. Assert.Equal(10,e.NewRow);
  100. };
  101. tableView.SelectedRow = 10;
  102. Assert.True(called);
  103. }
  104. [Fact]
  105. public void Test_SumColumnWidth_UnicodeLength()
  106. {
  107. Assert.Equal(11,"hello there".Sum(c=>Rune.ColumnWidth(c)));
  108. // Creates a string with the peculiar (french?) r symbol
  109. String surrogate = "Les Mise" + Char.ConvertFromUtf32(Int32.Parse("0301", NumberStyles.HexNumber)) + "rables";
  110. // The unicode width of this string is shorter than the string length!
  111. Assert.Equal(14,surrogate.Sum(c=>Rune.ColumnWidth(c)));
  112. Assert.Equal(15,surrogate.Length);
  113. }
  114. [Fact]
  115. public void IsSelected_MultiSelectionOn_Vertical()
  116. {
  117. var tableView = new TableView(){
  118. Table = BuildTable(25,50),
  119. MultiSelect = true
  120. };
  121. // 3 cell vertical selection
  122. tableView.SetSelection(1,1,false);
  123. tableView.SetSelection(1,3,true);
  124. Assert.False(tableView.IsSelected(0,0));
  125. Assert.False(tableView.IsSelected(1,0));
  126. Assert.False(tableView.IsSelected(2,0));
  127. Assert.False(tableView.IsSelected(0,1));
  128. Assert.True(tableView.IsSelected(1,1));
  129. Assert.False(tableView.IsSelected(2,1));
  130. Assert.False(tableView.IsSelected(0,2));
  131. Assert.True(tableView.IsSelected(1,2));
  132. Assert.False(tableView.IsSelected(2,2));
  133. Assert.False(tableView.IsSelected(0,3));
  134. Assert.True(tableView.IsSelected(1,3));
  135. Assert.False(tableView.IsSelected(2,3));
  136. Assert.False(tableView.IsSelected(0,4));
  137. Assert.False(tableView.IsSelected(1,4));
  138. Assert.False(tableView.IsSelected(2,4));
  139. }
  140. [Fact]
  141. public void IsSelected_MultiSelectionOn_Horizontal()
  142. {
  143. var tableView = new TableView(){
  144. Table = BuildTable(25,50),
  145. MultiSelect = true
  146. };
  147. // 2 cell horizontal selection
  148. tableView.SetSelection(1,0,false);
  149. tableView.SetSelection(2,0,true);
  150. Assert.False(tableView.IsSelected(0,0));
  151. Assert.True(tableView.IsSelected(1,0));
  152. Assert.True(tableView.IsSelected(2,0));
  153. Assert.False(tableView.IsSelected(3,0));
  154. Assert.False(tableView.IsSelected(0,1));
  155. Assert.False(tableView.IsSelected(1,1));
  156. Assert.False(tableView.IsSelected(2,1));
  157. Assert.False(tableView.IsSelected(3,1));
  158. }
  159. [Fact]
  160. public void IsSelected_MultiSelectionOn_BoxSelection()
  161. {
  162. var tableView = new TableView(){
  163. Table = BuildTable(25,50),
  164. MultiSelect = true
  165. };
  166. // 4 cell horizontal in box 2x2
  167. tableView.SetSelection(0,0,false);
  168. tableView.SetSelection(1,1,true);
  169. Assert.True(tableView.IsSelected(0,0));
  170. Assert.True(tableView.IsSelected(1,0));
  171. Assert.False(tableView.IsSelected(2,0));
  172. Assert.True(tableView.IsSelected(0,1));
  173. Assert.True(tableView.IsSelected(1,1));
  174. Assert.False(tableView.IsSelected(2,1));
  175. Assert.False(tableView.IsSelected(0,2));
  176. Assert.False(tableView.IsSelected(1,2));
  177. Assert.False(tableView.IsSelected(2,2));
  178. }
  179. [Fact]
  180. public void PageDown_ExcludesHeaders()
  181. {
  182. var driver = new FakeDriver ();
  183. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  184. driver.Init (() => { });
  185. var tableView = new TableView(){
  186. Table = BuildTable(25,50),
  187. MultiSelect = true,
  188. Bounds = new Rect(0,0,10,5)
  189. };
  190. // Header should take up 2 lines
  191. tableView.Style.ShowHorizontalHeaderOverline = false;
  192. tableView.Style.ShowHorizontalHeaderUnderline = true;
  193. tableView.Style.AlwaysShowHeaders = false;
  194. Assert.Equal(0,tableView.RowOffset);
  195. tableView.ProcessKey(new KeyEvent(Key.PageDown,new KeyModifiers()));
  196. // window height is 5 rows 2 are header so page down should give 3 new rows
  197. Assert.Equal(3,tableView.RowOffset);
  198. // header is no longer visible so page down should give 5 new rows
  199. tableView.ProcessKey(new KeyEvent(Key.PageDown,new KeyModifiers()));
  200. Assert.Equal(8,tableView.RowOffset);
  201. }
  202. [Fact]
  203. public void DeleteRow_SelectAll_AdjustsSelectionToPreventOverrun()
  204. {
  205. // create a 4 by 4 table
  206. var tableView = new TableView(){
  207. Table = BuildTable(4,4),
  208. MultiSelect = true,
  209. Bounds = new Rect(0,0,10,5)
  210. };
  211. tableView.SelectAll();
  212. Assert.Equal(16,tableView.GetAllSelectedCells().Count());
  213. // delete one of the columns
  214. tableView.Table.Columns.RemoveAt(2);
  215. // table should now be 3x4
  216. Assert.Equal(12,tableView.GetAllSelectedCells().Count());
  217. // remove a row
  218. tableView.Table.Rows.RemoveAt(1);
  219. // table should now be 3x3
  220. Assert.Equal(9,tableView.GetAllSelectedCells().Count());
  221. }
  222. [Fact]
  223. public void DeleteRow_SelectLastRow_AdjustsSelectionToPreventOverrun()
  224. {
  225. // create a 4 by 4 table
  226. var tableView = new TableView(){
  227. Table = BuildTable(4,4),
  228. MultiSelect = true,
  229. Bounds = new Rect(0,0,10,5)
  230. };
  231. // select the last row
  232. tableView.MultiSelectedRegions.Clear();
  233. tableView.MultiSelectedRegions.Push(new TableView.TableSelection (new Point(0,3), new Rect(0,3,4,1)));
  234. Assert.Equal(4,tableView.GetAllSelectedCells().Count());
  235. // remove a row
  236. tableView.Table.Rows.RemoveAt(0);
  237. tableView.EnsureValidSelection();
  238. // since the selection no longer exists it should be removed
  239. Assert.Empty(tableView.MultiSelectedRegions);
  240. }
  241. [Theory]
  242. [InlineData(true)]
  243. [InlineData(false)]
  244. public void GetAllSelectedCells_SingleCellSelected_ReturnsOne(bool multiSelect)
  245. {
  246. var tableView = new TableView(){
  247. Table = BuildTable(3,3),
  248. MultiSelect = multiSelect,
  249. Bounds = new Rect(0,0,10,5)
  250. };
  251. tableView.SetSelection(1,1,false);
  252. Assert.Single(tableView.GetAllSelectedCells());
  253. Assert.Equal(new Point(1,1),tableView.GetAllSelectedCells().Single());
  254. }
  255. [Fact]
  256. public void GetAllSelectedCells_SquareSelection_ReturnsFour()
  257. {
  258. var tableView = new TableView(){
  259. Table = BuildTable(3,3),
  260. MultiSelect = true,
  261. Bounds = new Rect(0,0,10,5)
  262. };
  263. // move cursor to 1,1
  264. tableView.SetSelection(1,1,false);
  265. // spread selection across to 2,2 (e.g. shift+right then shift+down)
  266. tableView.SetSelection(2,2,true);
  267. var selected = tableView.GetAllSelectedCells().ToArray();
  268. Assert.Equal(4,selected.Length);
  269. Assert.Equal(new Point(1,1),selected[0]);
  270. Assert.Equal(new Point(2,1),selected[1]);
  271. Assert.Equal(new Point(1,2),selected[2]);
  272. Assert.Equal(new Point(2,2),selected[3]);
  273. }
  274. [Fact]
  275. public void GetAllSelectedCells_SquareSelection_FullRowSelect()
  276. {
  277. var tableView = new TableView(){
  278. Table = BuildTable(3,3),
  279. MultiSelect = true,
  280. FullRowSelect = true,
  281. Bounds = new Rect(0,0,10,5)
  282. };
  283. // move cursor to 1,1
  284. tableView.SetSelection(1,1,false);
  285. // spread selection across to 2,2 (e.g. shift+right then shift+down)
  286. tableView.SetSelection(2,2,true);
  287. var selected = tableView.GetAllSelectedCells().ToArray();
  288. Assert.Equal(6,selected.Length);
  289. Assert.Equal(new Point(0,1),selected[0]);
  290. Assert.Equal(new Point(1,1),selected[1]);
  291. Assert.Equal(new Point(2,1),selected[2]);
  292. Assert.Equal(new Point(0,2),selected[3]);
  293. Assert.Equal(new Point(1,2),selected[4]);
  294. Assert.Equal(new Point(2,2),selected[5]);
  295. }
  296. [Fact]
  297. public void GetAllSelectedCells_TwoIsolatedSelections_ReturnsSix()
  298. {
  299. var tableView = new TableView(){
  300. Table = BuildTable(20,20),
  301. MultiSelect = true,
  302. Bounds = new Rect(0,0,10,5)
  303. };
  304. /*
  305. Sets up disconnected selections like:
  306. 00000000000
  307. 01100000000
  308. 01100000000
  309. 00000001100
  310. 00000000000
  311. */
  312. tableView.MultiSelectedRegions.Clear();
  313. tableView.MultiSelectedRegions.Push(new TableView.TableSelection(new Point(1,1),new Rect(1,1,2,2)));
  314. tableView.MultiSelectedRegions.Push(new TableView.TableSelection (new Point(7,3),new Rect(7,3,2,1)));
  315. tableView.SelectedColumn = 8;
  316. tableView.SelectedRow = 3;
  317. var selected = tableView.GetAllSelectedCells().ToArray();
  318. Assert.Equal(6,selected.Length);
  319. Assert.Equal(new Point(1,1),selected[0]);
  320. Assert.Equal(new Point(2,1),selected[1]);
  321. Assert.Equal(new Point(1,2),selected[2]);
  322. Assert.Equal(new Point(2,2),selected[3]);
  323. Assert.Equal(new Point(7,3),selected[4]);
  324. Assert.Equal(new Point(8,3),selected[5]);
  325. }
  326. [Fact]
  327. public void TableView_ExpandLastColumn_True()
  328. {
  329. var tv = SetUpMiniTable();
  330. // the thing we are testing
  331. tv.Style.ExpandLastColumn = true;
  332. tv.Redraw(tv.Bounds);
  333. string expected = @"
  334. ┌─┬──────┐
  335. │A│B │
  336. ├─┼──────┤
  337. │1│2 │
  338. ";
  339. GraphViewTests.AssertDriverContentsAre(expected, output);
  340. }
  341. [Fact]
  342. public void TableView_ExpandLastColumn_False()
  343. {
  344. var tv = SetUpMiniTable();
  345. // the thing we are testing
  346. tv.Style.ExpandLastColumn = false;
  347. tv.Redraw(tv.Bounds);
  348. string expected = @"
  349. ┌─┬─┬────┐
  350. │A│B│ │
  351. ├─┼─┼────┤
  352. │1│2│ │
  353. ";
  354. GraphViewTests.AssertDriverContentsAre(expected, output);
  355. }
  356. [Fact]
  357. public void TableView_ExpandLastColumn_False_ExactBounds()
  358. {
  359. var tv = SetUpMiniTable();
  360. // the thing we are testing
  361. tv.Style.ExpandLastColumn = false;
  362. // width exactly matches the max col widths
  363. tv.Bounds = new Rect(0,0,5,4);
  364. tv.Redraw(tv.Bounds);
  365. string expected = @"
  366. ┌─┬─┐
  367. │A│B│
  368. ├─┼─┤
  369. │1│2│
  370. ";
  371. GraphViewTests.AssertDriverContentsAre(expected, output);
  372. }
  373. private TableView SetUpMiniTable ()
  374. {
  375. var tv = new TableView();
  376. tv.Bounds = new Rect(0,0,10,4);
  377. var dt = new DataTable();
  378. var colA = dt.Columns.Add("A");
  379. var colB = dt.Columns.Add("B");
  380. dt.Rows.Add(1,2);
  381. tv.Table = dt;
  382. tv.Style.GetOrCreateColumnStyle(colA).MinWidth=1;
  383. tv.Style.GetOrCreateColumnStyle(colA).MinWidth=1;
  384. tv.Style.GetOrCreateColumnStyle(colB).MaxWidth=1;
  385. tv.Style.GetOrCreateColumnStyle(colB).MaxWidth=1;
  386. GraphViewTests.InitFakeDriver();
  387. tv.ColorScheme = new ColorScheme(){
  388. Normal = Application.Driver.MakeAttribute(Color.White,Color.Black),
  389. HotFocus = Application.Driver.MakeAttribute(Color.White,Color.Black)
  390. };
  391. return tv;
  392. }
  393. /// <summary>
  394. /// Builds a simple table of string columns with the requested number of columns and rows
  395. /// </summary>
  396. /// <param name="cols"></param>
  397. /// <param name="rows"></param>
  398. /// <returns></returns>
  399. public static DataTable BuildTable(int cols, int rows)
  400. {
  401. var dt = new DataTable();
  402. for(int c = 0; c < cols; c++) {
  403. dt.Columns.Add("Col"+c);
  404. }
  405. for(int r = 0; r < rows; r++) {
  406. var newRow = dt.NewRow();
  407. for(int c = 0; c < cols; c++) {
  408. newRow[c] = $"R{r}C{c}";
  409. }
  410. dt.Rows.Add(newRow);
  411. }
  412. return dt;
  413. }
  414. }
  415. }