TableViewTests.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  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. using System.Reflection;
  11. namespace Terminal.Gui.Views {
  12. public class TableViewTests {
  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. [AutoInitShutdown]
  60. public void Redraw_EmptyTable ()
  61. {
  62. var tableView = new TableView ();
  63. tableView.ColorScheme = new ColorScheme();
  64. tableView.Bounds = new Rect (0, 0, 25, 10);
  65. // Set a table with 1 column
  66. tableView.Table = BuildTable (1, 50);
  67. tableView.Redraw(tableView.Bounds);
  68. tableView.Table.Columns.Remove(tableView.Table.Columns[0]);
  69. tableView.Redraw(tableView.Bounds);
  70. }
  71. [Fact]
  72. public void SelectedCellChanged_NotFiredForSameValue ()
  73. {
  74. var tableView = new TableView () {
  75. Table = BuildTable (25, 50)
  76. };
  77. bool called = false;
  78. tableView.SelectedCellChanged += (e) => { called = true; };
  79. Assert.Equal (0, tableView.SelectedColumn);
  80. Assert.False (called);
  81. // Changing value to same as it already was should not raise an event
  82. tableView.SelectedColumn = 0;
  83. Assert.False (called);
  84. tableView.SelectedColumn = 10;
  85. Assert.True (called);
  86. }
  87. [Fact]
  88. public void SelectedCellChanged_SelectedColumnIndexesCorrect ()
  89. {
  90. var tableView = new TableView () {
  91. Table = BuildTable (25, 50)
  92. };
  93. bool called = false;
  94. tableView.SelectedCellChanged += (e) => {
  95. called = true;
  96. Assert.Equal (0, e.OldCol);
  97. Assert.Equal (10, e.NewCol);
  98. };
  99. tableView.SelectedColumn = 10;
  100. Assert.True (called);
  101. }
  102. [Fact]
  103. public void SelectedCellChanged_SelectedRowIndexesCorrect ()
  104. {
  105. var tableView = new TableView () {
  106. Table = BuildTable (25, 50)
  107. };
  108. bool called = false;
  109. tableView.SelectedCellChanged += (e) => {
  110. called = true;
  111. Assert.Equal (0, e.OldRow);
  112. Assert.Equal (10, e.NewRow);
  113. };
  114. tableView.SelectedRow = 10;
  115. Assert.True (called);
  116. }
  117. [Fact]
  118. public void Test_SumColumnWidth_UnicodeLength ()
  119. {
  120. Assert.Equal (11, "hello there".Sum (c => Rune.ColumnWidth (c)));
  121. // Creates a string with the peculiar (french?) r symbol
  122. String surrogate = "Les Mise" + Char.ConvertFromUtf32 (Int32.Parse ("0301", NumberStyles.HexNumber)) + "rables";
  123. // The unicode width of this string is shorter than the string length!
  124. Assert.Equal (14, surrogate.Sum (c => Rune.ColumnWidth (c)));
  125. Assert.Equal (15, surrogate.Length);
  126. }
  127. [Fact]
  128. public void IsSelected_MultiSelectionOn_Vertical ()
  129. {
  130. var tableView = new TableView () {
  131. Table = BuildTable (25, 50),
  132. MultiSelect = true
  133. };
  134. // 3 cell vertical selection
  135. tableView.SetSelection (1, 1, false);
  136. tableView.SetSelection (1, 3, true);
  137. Assert.False (tableView.IsSelected (0, 0));
  138. Assert.False (tableView.IsSelected (1, 0));
  139. Assert.False (tableView.IsSelected (2, 0));
  140. Assert.False (tableView.IsSelected (0, 1));
  141. Assert.True (tableView.IsSelected (1, 1));
  142. Assert.False (tableView.IsSelected (2, 1));
  143. Assert.False (tableView.IsSelected (0, 2));
  144. Assert.True (tableView.IsSelected (1, 2));
  145. Assert.False (tableView.IsSelected (2, 2));
  146. Assert.False (tableView.IsSelected (0, 3));
  147. Assert.True (tableView.IsSelected (1, 3));
  148. Assert.False (tableView.IsSelected (2, 3));
  149. Assert.False (tableView.IsSelected (0, 4));
  150. Assert.False (tableView.IsSelected (1, 4));
  151. Assert.False (tableView.IsSelected (2, 4));
  152. }
  153. [Fact]
  154. public void IsSelected_MultiSelectionOn_Horizontal ()
  155. {
  156. var tableView = new TableView () {
  157. Table = BuildTable (25, 50),
  158. MultiSelect = true
  159. };
  160. // 2 cell horizontal selection
  161. tableView.SetSelection (1, 0, false);
  162. tableView.SetSelection (2, 0, true);
  163. Assert.False (tableView.IsSelected (0, 0));
  164. Assert.True (tableView.IsSelected (1, 0));
  165. Assert.True (tableView.IsSelected (2, 0));
  166. Assert.False (tableView.IsSelected (3, 0));
  167. Assert.False (tableView.IsSelected (0, 1));
  168. Assert.False (tableView.IsSelected (1, 1));
  169. Assert.False (tableView.IsSelected (2, 1));
  170. Assert.False (tableView.IsSelected (3, 1));
  171. }
  172. [Fact]
  173. public void IsSelected_MultiSelectionOn_BoxSelection ()
  174. {
  175. var tableView = new TableView () {
  176. Table = BuildTable (25, 50),
  177. MultiSelect = true
  178. };
  179. // 4 cell horizontal in box 2x2
  180. tableView.SetSelection (0, 0, false);
  181. tableView.SetSelection (1, 1, true);
  182. Assert.True (tableView.IsSelected (0, 0));
  183. Assert.True (tableView.IsSelected (1, 0));
  184. Assert.False (tableView.IsSelected (2, 0));
  185. Assert.True (tableView.IsSelected (0, 1));
  186. Assert.True (tableView.IsSelected (1, 1));
  187. Assert.False (tableView.IsSelected (2, 1));
  188. Assert.False (tableView.IsSelected (0, 2));
  189. Assert.False (tableView.IsSelected (1, 2));
  190. Assert.False (tableView.IsSelected (2, 2));
  191. }
  192. [AutoInitShutdown]
  193. [Fact]
  194. public void PageDown_ExcludesHeaders ()
  195. {
  196. var tableView = new TableView () {
  197. Table = BuildTable (25, 50),
  198. MultiSelect = true,
  199. Bounds = new Rect (0, 0, 10, 5)
  200. };
  201. // Header should take up 2 lines
  202. tableView.Style.ShowHorizontalHeaderOverline = false;
  203. tableView.Style.ShowHorizontalHeaderUnderline = true;
  204. tableView.Style.AlwaysShowHeaders = false;
  205. // ensure that TableView has the input focus
  206. Application.Top.Add (tableView);
  207. Application.Top.FocusFirst ();
  208. Assert.True (tableView.HasFocus);
  209. Assert.Equal (0, tableView.RowOffset);
  210. tableView.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ()));
  211. // window height is 5 rows 2 are header so page down should give 3 new rows
  212. Assert.Equal (3, tableView.SelectedRow);
  213. Assert.Equal (1, tableView.RowOffset);
  214. // header is no longer visible so page down should give 5 new rows
  215. tableView.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ()));
  216. Assert.Equal (8, tableView.SelectedRow);
  217. Assert.Equal (4, tableView.RowOffset);
  218. }
  219. [Fact]
  220. public void DeleteRow_SelectAll_AdjustsSelectionToPreventOverrun ()
  221. {
  222. // create a 4 by 4 table
  223. var tableView = new TableView () {
  224. Table = BuildTable (4, 4),
  225. MultiSelect = true,
  226. Bounds = new Rect (0, 0, 10, 5)
  227. };
  228. tableView.SelectAll ();
  229. Assert.Equal (16, tableView.GetAllSelectedCells ().Count ());
  230. // delete one of the columns
  231. tableView.Table.Columns.RemoveAt (2);
  232. // table should now be 3x4
  233. Assert.Equal (12, tableView.GetAllSelectedCells ().Count ());
  234. // remove a row
  235. tableView.Table.Rows.RemoveAt (1);
  236. // table should now be 3x3
  237. Assert.Equal (9, tableView.GetAllSelectedCells ().Count ());
  238. }
  239. [Fact]
  240. public void DeleteRow_SelectLastRow_AdjustsSelectionToPreventOverrun ()
  241. {
  242. // create a 4 by 4 table
  243. var tableView = new TableView () {
  244. Table = BuildTable (4, 4),
  245. MultiSelect = true,
  246. Bounds = new Rect (0, 0, 10, 5)
  247. };
  248. // select the last row
  249. tableView.MultiSelectedRegions.Clear ();
  250. tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (0, 3), new Rect (0, 3, 4, 1)));
  251. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  252. // remove a row
  253. tableView.Table.Rows.RemoveAt (0);
  254. tableView.EnsureValidSelection ();
  255. // since the selection no longer exists it should be removed
  256. Assert.Empty (tableView.MultiSelectedRegions);
  257. }
  258. [Theory]
  259. [InlineData (true)]
  260. [InlineData (false)]
  261. public void GetAllSelectedCells_SingleCellSelected_ReturnsOne (bool multiSelect)
  262. {
  263. var tableView = new TableView () {
  264. Table = BuildTable (3, 3),
  265. MultiSelect = multiSelect,
  266. Bounds = new Rect (0, 0, 10, 5)
  267. };
  268. tableView.SetSelection (1, 1, false);
  269. Assert.Single (tableView.GetAllSelectedCells ());
  270. Assert.Equal (new Point (1, 1), tableView.GetAllSelectedCells ().Single ());
  271. }
  272. [Fact]
  273. public void GetAllSelectedCells_SquareSelection_ReturnsFour ()
  274. {
  275. var tableView = new TableView () {
  276. Table = BuildTable (3, 3),
  277. MultiSelect = true,
  278. Bounds = new Rect (0, 0, 10, 5)
  279. };
  280. // move cursor to 1,1
  281. tableView.SetSelection (1, 1, false);
  282. // spread selection across to 2,2 (e.g. shift+right then shift+down)
  283. tableView.SetSelection (2, 2, true);
  284. var selected = tableView.GetAllSelectedCells ().ToArray ();
  285. Assert.Equal (4, selected.Length);
  286. Assert.Equal (new Point (1, 1), selected [0]);
  287. Assert.Equal (new Point (2, 1), selected [1]);
  288. Assert.Equal (new Point (1, 2), selected [2]);
  289. Assert.Equal (new Point (2, 2), selected [3]);
  290. }
  291. [Fact]
  292. public void GetAllSelectedCells_SquareSelection_FullRowSelect ()
  293. {
  294. var tableView = new TableView () {
  295. Table = BuildTable (3, 3),
  296. MultiSelect = true,
  297. FullRowSelect = true,
  298. Bounds = new Rect (0, 0, 10, 5)
  299. };
  300. // move cursor to 1,1
  301. tableView.SetSelection (1, 1, false);
  302. // spread selection across to 2,2 (e.g. shift+right then shift+down)
  303. tableView.SetSelection (2, 2, true);
  304. var selected = tableView.GetAllSelectedCells ().ToArray ();
  305. Assert.Equal (6, selected.Length);
  306. Assert.Equal (new Point (0, 1), selected [0]);
  307. Assert.Equal (new Point (1, 1), selected [1]);
  308. Assert.Equal (new Point (2, 1), selected [2]);
  309. Assert.Equal (new Point (0, 2), selected [3]);
  310. Assert.Equal (new Point (1, 2), selected [4]);
  311. Assert.Equal (new Point (2, 2), selected [5]);
  312. }
  313. [Fact]
  314. public void GetAllSelectedCells_TwoIsolatedSelections_ReturnsSix ()
  315. {
  316. var tableView = new TableView () {
  317. Table = BuildTable (20, 20),
  318. MultiSelect = true,
  319. Bounds = new Rect (0, 0, 10, 5)
  320. };
  321. /*
  322. Sets up disconnected selections like:
  323. 00000000000
  324. 01100000000
  325. 01100000000
  326. 00000001100
  327. 00000000000
  328. */
  329. tableView.MultiSelectedRegions.Clear ();
  330. tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (1, 1), new Rect (1, 1, 2, 2)));
  331. tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (7, 3), new Rect (7, 3, 2, 1)));
  332. tableView.SelectedColumn = 8;
  333. tableView.SelectedRow = 3;
  334. var selected = tableView.GetAllSelectedCells ().ToArray ();
  335. Assert.Equal (6, selected.Length);
  336. Assert.Equal (new Point (1, 1), selected [0]);
  337. Assert.Equal (new Point (2, 1), selected [1]);
  338. Assert.Equal (new Point (1, 2), selected [2]);
  339. Assert.Equal (new Point (2, 2), selected [3]);
  340. Assert.Equal (new Point (7, 3), selected [4]);
  341. Assert.Equal (new Point (8, 3), selected [5]);
  342. }
  343. [Fact]
  344. public void TableView_ExpandLastColumn_True ()
  345. {
  346. var tv = SetUpMiniTable ();
  347. // the thing we are testing
  348. tv.Style.ExpandLastColumn = true;
  349. tv.Redraw (tv.Bounds);
  350. string expected = @"
  351. ┌─┬──────┐
  352. │A│B │
  353. ├─┼──────┤
  354. │1│2 │
  355. ";
  356. TestHelpers.AssertDriverContentsAre (expected, output);
  357. // Shutdown must be called to safely clean up Application if Init has been called
  358. Application.Shutdown ();
  359. }
  360. [Fact]
  361. public void TableView_ExpandLastColumn_False ()
  362. {
  363. var tv = SetUpMiniTable ();
  364. // the thing we are testing
  365. tv.Style.ExpandLastColumn = false;
  366. tv.Redraw (tv.Bounds);
  367. string expected = @"
  368. ┌─┬─┬────┐
  369. │A│B│ │
  370. ├─┼─┼────┤
  371. │1│2│ │
  372. ";
  373. TestHelpers.AssertDriverContentsAre (expected, output);
  374. // Shutdown must be called to safely clean up Application if Init has been called
  375. Application.Shutdown ();
  376. }
  377. [Fact]
  378. public void TableView_ExpandLastColumn_False_ExactBounds ()
  379. {
  380. var tv = SetUpMiniTable ();
  381. // the thing we are testing
  382. tv.Style.ExpandLastColumn = false;
  383. // width exactly matches the max col widths
  384. tv.Bounds = new Rect (0, 0, 5, 4);
  385. tv.Redraw (tv.Bounds);
  386. string expected = @"
  387. ┌─┬─┐
  388. │A│B│
  389. ├─┼─┤
  390. │1│2│
  391. ";
  392. TestHelpers.AssertDriverContentsAre (expected, output);
  393. // Shutdown must be called to safely clean up Application if Init has been called
  394. Application.Shutdown ();
  395. }
  396. [Fact]
  397. [AutoInitShutdown]
  398. public void TableView_Activate()
  399. {
  400. string activatedValue = null;
  401. var tv = new TableView (BuildTable(1,1));
  402. tv.CellActivated += (c) => activatedValue = c.Table.Rows[c.Row][c.Col].ToString();
  403. Application.Top.Add (tv);
  404. Application.Begin (Application.Top);
  405. // pressing enter should activate the first cell (selected cell)
  406. tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  407. Assert.Equal ("R0C0",activatedValue);
  408. // reset the test
  409. activatedValue = null;
  410. // clear keybindings and ensure that Enter does not trigger the event anymore
  411. tv.ClearKeybindings ();
  412. tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  413. Assert.Null(activatedValue);
  414. // New method for changing the activation key
  415. tv.AddKeyBinding (Key.z, Command.Accept);
  416. tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ()));
  417. Assert.Equal ("R0C0", activatedValue);
  418. // reset the test
  419. activatedValue = null;
  420. tv.ClearKeybindings ();
  421. // Old method for changing the activation key
  422. tv.CellActivationKey = Key.z;
  423. tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ()));
  424. Assert.Equal ("R0C0", activatedValue);
  425. }
  426. [Fact]
  427. public void TableViewMultiSelect_CannotFallOffLeft()
  428. {
  429. var tv = SetUpMiniTable ();
  430. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  431. tv.MultiSelect = true;
  432. tv.SelectedColumn = 1;
  433. tv.SelectedRow = 1;
  434. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  435. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single().Rect);
  436. // this next shift left should be ignored because we are already at the bounds
  437. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  438. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  439. Assert.Equal (0, tv.SelectedColumn);
  440. Assert.Equal (1, tv.SelectedRow);
  441. Application.Shutdown ();
  442. }
  443. [Fact]
  444. public void TableViewMultiSelect_CannotFallOffRight()
  445. {
  446. var tv = SetUpMiniTable ();
  447. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  448. tv.MultiSelect = true;
  449. tv.SelectedColumn = 0;
  450. tv.SelectedRow = 1;
  451. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  452. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  453. // this next shift right should be ignored because we are already at the right bounds
  454. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  455. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  456. Assert.Equal (1, tv.SelectedColumn);
  457. Assert.Equal (1, tv.SelectedRow);
  458. Application.Shutdown ();
  459. }
  460. [Fact]
  461. public void TableViewMultiSelect_CannotFallOffBottom ()
  462. {
  463. var tv = SetUpMiniTable ();
  464. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  465. tv.MultiSelect = true;
  466. tv.SelectedColumn = 0;
  467. tv.SelectedRow = 0;
  468. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  469. tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
  470. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  471. // this next moves should be ignored because we already selected the whole table
  472. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  473. tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
  474. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  475. Assert.Equal (1, tv.SelectedColumn);
  476. Assert.Equal (1, tv.SelectedRow);
  477. Application.Shutdown ();
  478. }
  479. [Fact]
  480. public void TableViewMultiSelect_CannotFallOffTop()
  481. {
  482. var tv = SetUpMiniTable ();
  483. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  484. tv.MultiSelect = true;
  485. tv.SelectedColumn = 1;
  486. tv.SelectedRow = 1;
  487. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  488. tv.ProcessKey (new KeyEvent (Key.CursorUp | Key.ShiftMask, new KeyModifiers { Shift = true }));
  489. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  490. // this next moves should be ignored because we already selected the whole table
  491. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  492. tv.ProcessKey (new KeyEvent (Key.CursorUp | Key.ShiftMask, new KeyModifiers { Shift = true }));
  493. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  494. Assert.Equal (0, tv.SelectedColumn);
  495. Assert.Equal (0, tv.SelectedRow);
  496. Application.Shutdown ();
  497. }
  498. [Theory]
  499. [InlineData (false)]
  500. [InlineData (true)]
  501. public void TableView_ColorTests_FocusedOrNot (bool focused)
  502. {
  503. var tv = SetUpMiniTable ();
  504. // width exactly matches the max col widths
  505. tv.Bounds = new Rect (0, 0, 5, 4);
  506. // private method for forcing the view to be focused/not focused
  507. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  508. // when the view is/isn't focused
  509. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  510. tv.Redraw (tv.Bounds);
  511. string expected = @"
  512. ┌─┬─┐
  513. │A│B│
  514. ├─┼─┤
  515. │1│2│
  516. ";
  517. TestHelpers.AssertDriverContentsAre (expected, output);
  518. string expectedColors = @"
  519. 00000
  520. 00000
  521. 00000
  522. 01000
  523. ";
  524. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  525. // 0
  526. tv.ColorScheme.Normal,
  527. // 1
  528. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal});
  529. Application.Shutdown();
  530. }
  531. [Theory]
  532. [InlineData (false)]
  533. [InlineData (true)]
  534. public void TableView_ColorTests_InvertSelectedCellFirstCharacter (bool focused)
  535. {
  536. var tv = SetUpMiniTable ();
  537. tv.Style.InvertSelectedCellFirstCharacter = true;
  538. // width exactly matches the max col widths
  539. tv.Bounds = new Rect (0, 0, 5, 4);
  540. // private method for forcing the view to be focused/not focused
  541. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  542. // when the view is/isn't focused
  543. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  544. tv.Redraw (tv.Bounds);
  545. string expected = @"
  546. ┌─┬─┐
  547. │A│B│
  548. ├─┼─┤
  549. │1│2│
  550. ";
  551. TestHelpers.AssertDriverContentsAre (expected, output);
  552. string expectedColors = @"
  553. 00000
  554. 00000
  555. 00000
  556. 01000
  557. ";
  558. var invertHotFocus = new Attribute(tv.ColorScheme.HotFocus.Background,tv.ColorScheme.HotFocus.Foreground);
  559. var invertHotNormal = new Attribute(tv.ColorScheme.HotNormal.Background,tv.ColorScheme.HotNormal.Foreground);
  560. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  561. // 0
  562. tv.ColorScheme.Normal,
  563. // 1
  564. focused ? invertHotFocus : invertHotNormal});
  565. Application.Shutdown();
  566. }
  567. [Theory]
  568. [InlineData (false)]
  569. [InlineData (true)]
  570. public void TableView_ColorsTest_RowColorGetter (bool focused)
  571. {
  572. var tv = SetUpMiniTable ();
  573. // width exactly matches the max col widths
  574. tv.Bounds = new Rect (0, 0, 5, 4);
  575. var rowHighlight = new ColorScheme () {
  576. Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
  577. HotNormal = Attribute.Make (Color.Green, Color.Blue),
  578. HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
  579. Focus = Attribute.Make (Color.Cyan, Color.Magenta),
  580. };
  581. // when B is 2 use the custom highlight colour for the row
  582. tv.Style.RowColorGetter += (e)=>Convert.ToInt32(e.Table.Rows[e.RowIndex][1]) == 2 ? rowHighlight : null;
  583. // private method for forcing the view to be focused/not focused
  584. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  585. // when the view is/isn't focused
  586. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  587. tv.Redraw (tv.Bounds);
  588. string expected = @"
  589. ┌─┬─┐
  590. │A│B│
  591. ├─┼─┤
  592. │1│2│
  593. ";
  594. TestHelpers.AssertDriverContentsAre (expected, output);
  595. string expectedColors = @"
  596. 00000
  597. 00000
  598. 00000
  599. 21222
  600. ";
  601. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  602. // 0
  603. tv.ColorScheme.Normal,
  604. // 1
  605. focused ? rowHighlight.HotFocus : rowHighlight.HotNormal,
  606. // 2
  607. rowHighlight.Normal});
  608. // change the value in the table so that
  609. // it no longer matches the RowColorGetter
  610. // delegate conditional ( which checks for
  611. // the value 2)
  612. tv.Table.Rows[0][1] = 5;
  613. tv.Redraw (tv.Bounds);
  614. expected = @"
  615. ┌─┬─┐
  616. │A│B│
  617. ├─┼─┤
  618. │1│5│
  619. ";
  620. TestHelpers.AssertDriverContentsAre (expected, output);
  621. expectedColors = @"
  622. 00000
  623. 00000
  624. 00000
  625. 01000
  626. ";
  627. // now we only see 2 colors used (the selected cell color and Normal
  628. // rowHighlight should no longer be used because the delegate returned null
  629. // (now that the cell value is 5 - which does not match the conditional)
  630. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  631. // 0
  632. tv.ColorScheme.Normal,
  633. // 1
  634. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
  635. // Shutdown must be called to safely clean up Application if Init has been called
  636. Application.Shutdown ();
  637. }
  638. [Theory]
  639. [InlineData (false)]
  640. [InlineData (true)]
  641. public void TableView_ColorsTest_ColorGetter (bool focused)
  642. {
  643. var tv = SetUpMiniTable ();
  644. // width exactly matches the max col widths
  645. tv.Bounds = new Rect (0, 0, 5, 4);
  646. // Create a style for column B
  647. var bStyle = tv.Style.GetOrCreateColumnStyle (tv.Table.Columns ["B"]);
  648. // when B is 2 use the custom highlight colour
  649. var cellHighlight = new ColorScheme () {
  650. Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
  651. HotNormal = Attribute.Make (Color.Green, Color.Blue),
  652. HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
  653. Focus = Attribute.Make (Color.Cyan, Color.Magenta),
  654. };
  655. bStyle.ColorGetter = (a) => Convert.ToInt32(a.CellValue) == 2 ? cellHighlight : null;
  656. // private method for forcing the view to be focused/not focused
  657. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  658. // when the view is/isn't focused
  659. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  660. tv.Redraw (tv.Bounds);
  661. string expected = @"
  662. ┌─┬─┐
  663. │A│B│
  664. ├─┼─┤
  665. │1│2│
  666. ";
  667. TestHelpers.AssertDriverContentsAre (expected, output);
  668. string expectedColors = @"
  669. 00000
  670. 00000
  671. 00000
  672. 01020
  673. ";
  674. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  675. // 0
  676. tv.ColorScheme.Normal,
  677. // 1
  678. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal,
  679. // 2
  680. cellHighlight.Normal});
  681. // change the value in the table so that
  682. // it no longer matches the ColorGetter
  683. // delegate conditional ( which checks for
  684. // the value 2)
  685. tv.Table.Rows[0][1] = 5;
  686. tv.Redraw (tv.Bounds);
  687. expected = @"
  688. ┌─┬─┐
  689. │A│B│
  690. ├─┼─┤
  691. │1│5│
  692. ";
  693. TestHelpers.AssertDriverContentsAre (expected, output);
  694. expectedColors = @"
  695. 00000
  696. 00000
  697. 00000
  698. 01000
  699. ";
  700. // now we only see 2 colors used (the selected cell color and Normal
  701. // cellHighlight should no longer be used because the delegate returned null
  702. // (now that the cell value is 5 - which does not match the conditional)
  703. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  704. // 0
  705. tv.ColorScheme.Normal,
  706. // 1
  707. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
  708. // Shutdown must be called to safely clean up Application if Init has been called
  709. Application.Shutdown ();
  710. }
  711. private TableView SetUpMiniTable ()
  712. {
  713. var tv = new TableView ();
  714. tv.Bounds = new Rect (0, 0, 10, 4);
  715. var dt = new DataTable ();
  716. var colA = dt.Columns.Add ("A");
  717. var colB = dt.Columns.Add ("B");
  718. dt.Rows.Add (1, 2);
  719. tv.Table = dt;
  720. tv.Style.GetOrCreateColumnStyle (colA).MinWidth = 1;
  721. tv.Style.GetOrCreateColumnStyle (colA).MinWidth = 1;
  722. tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
  723. tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
  724. GraphViewTests.InitFakeDriver ();
  725. tv.ColorScheme = Colors.Base;
  726. return tv;
  727. }
  728. [Fact]
  729. [AutoInitShutdown]
  730. public void ScrollDown_OneLineAtATime ()
  731. {
  732. var tableView = new TableView ();
  733. // Set big table
  734. tableView.Table = BuildTable (25, 50);
  735. // 1 header + 4 rows visible
  736. tableView.Bounds = new Rect (0, 0, 25, 5);
  737. tableView.Style.ShowHorizontalHeaderUnderline = false;
  738. tableView.Style.ShowHorizontalHeaderOverline = false;
  739. tableView.Style.AlwaysShowHeaders = true;
  740. // select last row
  741. tableView.SelectedRow = 3; // row is 0 indexed so this is the 4th visible row
  742. // Scroll down
  743. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorDown });
  744. // Scrolled off the page by 1 row so it should only have moved down 1 line of RowOffset
  745. Assert.Equal(4,tableView.SelectedRow);
  746. Assert.Equal (1, tableView.RowOffset);
  747. }
  748. [Fact]
  749. public void ScrollRight_SmoothScrolling ()
  750. {
  751. GraphViewTests.InitFakeDriver ();
  752. var tableView = new TableView ();
  753. tableView.ColorScheme = Colors.TopLevel;
  754. // 3 columns are visibile
  755. tableView.Bounds = new Rect (0, 0, 7, 5);
  756. tableView.Style.ShowHorizontalHeaderUnderline = false;
  757. tableView.Style.ShowHorizontalHeaderOverline = false;
  758. tableView.Style.AlwaysShowHeaders = true;
  759. tableView.Style.SmoothHorizontalScrolling = true;
  760. var dt = new DataTable ();
  761. dt.Columns.Add ("A");
  762. dt.Columns.Add ("B");
  763. dt.Columns.Add ("C");
  764. dt.Columns.Add ("D");
  765. dt.Columns.Add ("E");
  766. dt.Columns.Add ("F");
  767. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  768. tableView.Table = dt;
  769. // select last visible column
  770. tableView.SelectedColumn = 2; // column C
  771. tableView.Redraw (tableView.Bounds);
  772. string expected =
  773. @"
  774. │A│B│C│
  775. │1│2│3│";
  776. TestHelpers.AssertDriverContentsAre (expected, output);
  777. // Scroll right
  778. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  779. tableView.Redraw (tableView.Bounds);
  780. // Note that with SmoothHorizontalScrolling only a single new column
  781. // is exposed when scrolling right. This is not always the case though
  782. // sometimes if the leftmost column is long (i.e. A is a long column)
  783. // then when A is pushed off the screen multiple new columns could be exposed
  784. // (not just D but also E and F). This is because TableView never shows
  785. // 'half cells' or scrolls by console unit (scrolling is done by table row/column increments).
  786. expected =
  787. @"
  788. │B│C│D│
  789. │2│3│4│";
  790. TestHelpers.AssertDriverContentsAre (expected, output);
  791. // Shutdown must be called to safely clean up Application if Init has been called
  792. Application.Shutdown ();
  793. }
  794. [Fact]
  795. public void ScrollRight_WithoutSmoothScrolling ()
  796. {
  797. GraphViewTests.InitFakeDriver ();
  798. var tableView = new TableView ();
  799. tableView.ColorScheme = Colors.TopLevel;
  800. // 3 columns are visibile
  801. tableView.Bounds = new Rect (0, 0, 7, 5);
  802. tableView.Style.ShowHorizontalHeaderUnderline = false;
  803. tableView.Style.ShowHorizontalHeaderOverline = false;
  804. tableView.Style.AlwaysShowHeaders = true;
  805. tableView.Style.SmoothHorizontalScrolling = false;
  806. var dt = new DataTable ();
  807. dt.Columns.Add ("A");
  808. dt.Columns.Add ("B");
  809. dt.Columns.Add ("C");
  810. dt.Columns.Add ("D");
  811. dt.Columns.Add ("E");
  812. dt.Columns.Add ("F");
  813. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  814. tableView.Table = dt;
  815. // select last visible column
  816. tableView.SelectedColumn = 2; // column C
  817. tableView.Redraw (tableView.Bounds);
  818. string expected =
  819. @"
  820. │A│B│C│
  821. │1│2│3│";
  822. TestHelpers.AssertDriverContentsAre (expected, output);
  823. // Scroll right
  824. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  825. tableView.Redraw (tableView.Bounds);
  826. // notice that without smooth scrolling we just update the first column
  827. // rendered in the table to the newly exposed column (D). This is fast
  828. // since we don't have to worry about repeatedly measuring the content
  829. // area as we scroll until the new column (D) is exposed. But it makes
  830. // the view 'jump' to expose all new columns
  831. expected =
  832. @"
  833. │D│E│F│
  834. │4│5│6│";
  835. TestHelpers.AssertDriverContentsAre (expected, output);
  836. // Shutdown must be called to safely clean up Application if Init has been called
  837. Application.Shutdown ();
  838. }
  839. private TableView GetABCDEFTableView (out DataTable dt)
  840. {
  841. var tableView = new TableView ();
  842. tableView.ColorScheme = Colors.TopLevel;
  843. // 3 columns are visible
  844. tableView.Bounds = new Rect (0, 0, 7, 5);
  845. tableView.Style.ShowHorizontalHeaderUnderline = false;
  846. tableView.Style.ShowHorizontalHeaderOverline = false;
  847. tableView.Style.AlwaysShowHeaders = true;
  848. tableView.Style.SmoothHorizontalScrolling = false;
  849. dt = new DataTable ();
  850. dt.Columns.Add ("A");
  851. dt.Columns.Add ("B");
  852. dt.Columns.Add ("C");
  853. dt.Columns.Add ("D");
  854. dt.Columns.Add ("E");
  855. dt.Columns.Add ("F");
  856. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  857. tableView.Table = dt;
  858. return tableView;
  859. }
  860. [Fact, AutoInitShutdown]
  861. public void TestColumnStyle_VisibleFalse_IsNotRendered()
  862. {
  863. var tableView = GetABCDEFTableView (out DataTable dt);
  864. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  865. tableView.Redraw (tableView.Bounds);
  866. string expected =
  867. @"
  868. │A│C│D│
  869. │1│3│4│";
  870. TestHelpers.AssertDriverContentsAre (expected, output);
  871. }
  872. [Fact, AutoInitShutdown]
  873. public void TestColumnStyle_FirstColumnVisibleFalse_IsNotRendered ()
  874. {
  875. var tableView = GetABCDEFTableView (out DataTable dt);
  876. tableView.Style.ShowHorizontalScrollIndicators = true;
  877. tableView.Style.ShowHorizontalHeaderUnderline = true;
  878. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  879. tableView.Redraw (tableView.Bounds);
  880. string expected =
  881. @"
  882. │B│C│D│
  883. ├─┼─┼─►
  884. │2│3│4│";
  885. TestHelpers.AssertDriverContentsAre (expected, output);
  886. }
  887. [Fact, AutoInitShutdown]
  888. public void TestColumnStyle_AllColumnsVisibleFalse_BehavesAsTableNull ()
  889. {
  890. var tableView = GetABCDEFTableView (out DataTable dt);
  891. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  892. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  893. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["C"]).Visible = false;
  894. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  895. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  896. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  897. // expect nothing to be rendered when all columns are invisible
  898. string expected =
  899. @"
  900. ";
  901. tableView.Redraw (tableView.Bounds);
  902. TestHelpers.AssertDriverContentsAre (expected, output);
  903. // expect behavior to match when Table is null
  904. tableView.Table = null;
  905. tableView.Redraw (tableView.Bounds);
  906. TestHelpers.AssertDriverContentsAre (expected, output);
  907. }
  908. [Fact, AutoInitShutdown]
  909. public void TestColumnStyle_RemainingColumnsInvisible_NoScrollIndicator ()
  910. {
  911. var tableView = GetABCDEFTableView (out DataTable dt);
  912. tableView.Style.ShowHorizontalScrollIndicators = true;
  913. tableView.Style.ShowHorizontalHeaderUnderline = true;
  914. tableView.Redraw (tableView.Bounds);
  915. // normally we should have scroll indicators because DEF are of screen
  916. string expected =
  917. @"
  918. │A│B│C│
  919. ├─┼─┼─►
  920. │1│2│3│";
  921. TestHelpers.AssertDriverContentsAre (expected, output);
  922. // but if DEF are invisible we shouldn't be showing the indicator
  923. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  924. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  925. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  926. expected =
  927. @"
  928. │A│B│C│
  929. ├─┼─┼─┤
  930. │1│2│3│";
  931. tableView.Redraw (tableView.Bounds);
  932. TestHelpers.AssertDriverContentsAre (expected, output);
  933. }
  934. [Fact, AutoInitShutdown]
  935. public void TestColumnStyle_PreceedingColumnsInvisible_NoScrollIndicator ()
  936. {
  937. var tableView = GetABCDEFTableView (out DataTable dt);
  938. tableView.Style.ShowHorizontalScrollIndicators = true;
  939. tableView.Style.ShowHorizontalHeaderUnderline = true;
  940. tableView.ColumnOffset = 1;
  941. tableView.Redraw (tableView.Bounds);
  942. // normally we should have scroll indicators because A,E and F are of screen
  943. string expected =
  944. @"
  945. │B│C│D│
  946. ◄─┼─┼─►
  947. │2│3│4│";
  948. TestHelpers.AssertDriverContentsAre (expected, output);
  949. // but if E and F are invisible so we shouldn't show right
  950. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  951. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  952. expected =
  953. @"
  954. │B│C│D│
  955. ◄─┼─┼─┤
  956. │2│3│4│";
  957. tableView.Redraw (tableView.Bounds);
  958. TestHelpers.AssertDriverContentsAre (expected, output);
  959. // now also A is invisible so we cannot scroll in either direction
  960. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  961. expected =
  962. @"
  963. │B│C│D│
  964. ├─┼─┼─┤
  965. │2│3│4│";
  966. tableView.Redraw (tableView.Bounds);
  967. TestHelpers.AssertDriverContentsAre (expected, output);
  968. }
  969. [Fact, AutoInitShutdown]
  970. public void TestColumnStyle_VisibleFalse_CursorStepsOverInvisibleColumns ()
  971. {
  972. var tableView = GetABCDEFTableView (out var dt);
  973. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  974. tableView.SelectedColumn = 0;
  975. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  976. // Expect the cursor navigation to skip over the invisible column(s)
  977. Assert.Equal(2,tableView.SelectedColumn);
  978. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  979. // Expect the cursor navigation backwards to skip over invisible column too
  980. Assert.Equal (0, tableView.SelectedColumn);
  981. }
  982. [InlineData(true)]
  983. [InlineData (false)]
  984. [Theory, AutoInitShutdown]
  985. public void TestColumnStyle_FirstColumnVisibleFalse_CursorStaysAt1(bool useHome)
  986. {
  987. var tableView = GetABCDEFTableView (out var dt);
  988. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  989. tableView.SelectedColumn = 0;
  990. Assert.Equal (0, tableView.SelectedColumn);
  991. // column 0 is invisible so this method should move to 1
  992. tableView.EnsureValidSelection();
  993. Assert.Equal (1, tableView.SelectedColumn);
  994. tableView.ProcessKey (new KeyEvent
  995. {
  996. Key = useHome ? Key.Home : Key.CursorLeft
  997. });
  998. // Expect the cursor to stay at 1
  999. Assert.Equal (1, tableView.SelectedColumn);
  1000. }
  1001. [InlineData (true)]
  1002. [InlineData (false)]
  1003. [Theory, AutoInitShutdown]
  1004. public void TestColumnStyle_LastColumnVisibleFalse_CursorStaysAt2 (bool useEnd)
  1005. {
  1006. var tableView = GetABCDEFTableView (out var dt);
  1007. // select D
  1008. tableView.SelectedColumn = 3;
  1009. Assert.Equal (3, tableView.SelectedColumn);
  1010. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  1011. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  1012. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  1013. // column D is invisible so this method should move to 2 (C)
  1014. tableView.EnsureValidSelection ();
  1015. Assert.Equal (2, tableView.SelectedColumn);
  1016. tableView.ProcessKey (new KeyEvent {
  1017. Key = useEnd ? Key.End : Key.CursorRight
  1018. });
  1019. // Expect the cursor to stay at 2
  1020. Assert.Equal (2, tableView.SelectedColumn);
  1021. }
  1022. [Fact, AutoInitShutdown]
  1023. public void TestColumnStyle_VisibleFalse_MultiSelected ()
  1024. {
  1025. var tableView = GetABCDEFTableView (out var dt);
  1026. // user has rectangular selection
  1027. tableView.MultiSelectedRegions.Push (
  1028. new TableView.TableSelection(
  1029. new Point(0,0),
  1030. new Rect(0, 0, 3, 1))
  1031. );
  1032. Assert.Equal (3, tableView.GetAllSelectedCells ().Count());
  1033. Assert.True (tableView.IsSelected (0, 0));
  1034. Assert.True (tableView.IsSelected (1, 0));
  1035. Assert.True (tableView.IsSelected (2, 0));
  1036. Assert.False (tableView.IsSelected (3, 0));
  1037. // if middle column is invisible
  1038. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  1039. // it should not be included in the selection
  1040. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1041. Assert.True (tableView.IsSelected (0, 0));
  1042. Assert.False (tableView.IsSelected (1, 0));
  1043. Assert.True (tableView.IsSelected (2, 0));
  1044. Assert.False (tableView.IsSelected (3, 0));
  1045. Assert.DoesNotContain(new Point(1,0),tableView.GetAllSelectedCells ());
  1046. }
  1047. [Fact, AutoInitShutdown]
  1048. public void TestColumnStyle_VisibleFalse_MultiSelectingStepsOverInvisibleColumns ()
  1049. {
  1050. var tableView = GetABCDEFTableView (out var dt);
  1051. // if middle column is invisible
  1052. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  1053. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight | Key.ShiftMask });
  1054. // Selection should extend from A to C but skip B
  1055. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1056. Assert.True (tableView.IsSelected (0, 0));
  1057. Assert.False (tableView.IsSelected (1, 0));
  1058. Assert.True (tableView.IsSelected (2, 0));
  1059. Assert.False (tableView.IsSelected (3, 0));
  1060. Assert.DoesNotContain (new Point (1, 0), tableView.GetAllSelectedCells ());
  1061. }
  1062. [Theory, AutoInitShutdown]
  1063. [InlineData(new object[] { true,true })]
  1064. [InlineData (new object[] { false,true })]
  1065. [InlineData (new object [] { true, false})]
  1066. [InlineData (new object [] { false, false})]
  1067. public void TestColumnStyle_VisibleFalse_DoesNotEffect_EnsureSelectedCellIsVisible (bool smooth, bool invisibleCol)
  1068. {
  1069. var tableView = GetABCDEFTableView (out var dt);
  1070. tableView.Style.SmoothHorizontalScrolling = smooth;
  1071. if(invisibleCol) {
  1072. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  1073. }
  1074. // New TableView should have first cell selected
  1075. Assert.Equal (0,tableView.SelectedColumn);
  1076. // With no scrolling
  1077. Assert.Equal (0, tableView.ColumnOffset);
  1078. // A,B and C are visible on screen at the moment so these should have no effect
  1079. tableView.SelectedColumn = 1;
  1080. tableView.EnsureSelectedCellIsVisible ();
  1081. Assert.Equal (0, tableView.ColumnOffset);
  1082. tableView.SelectedColumn = 2;
  1083. tableView.EnsureSelectedCellIsVisible ();
  1084. Assert.Equal (0, tableView.ColumnOffset);
  1085. // Selecting D should move the visible table area to fit D onto the screen
  1086. tableView.SelectedColumn = 3;
  1087. tableView.EnsureSelectedCellIsVisible ();
  1088. Assert.Equal (smooth ? 1 : 3, tableView.ColumnOffset);
  1089. }
  1090. [Fact]
  1091. public void LongColumnTest ()
  1092. {
  1093. GraphViewTests.InitFakeDriver ();
  1094. var tableView = new TableView ();
  1095. tableView.ColorScheme = Colors.TopLevel;
  1096. // 25 characters can be printed into table
  1097. tableView.Bounds = new Rect (0, 0, 25, 5);
  1098. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1099. tableView.Style.ShowHorizontalHeaderOverline = false;
  1100. tableView.Style.AlwaysShowHeaders = true;
  1101. tableView.Style.SmoothHorizontalScrolling = true;
  1102. var dt = new DataTable ();
  1103. dt.Columns.Add ("A");
  1104. dt.Columns.Add ("B");
  1105. dt.Columns.Add ("Very Long Column");
  1106. dt.Rows.Add (1, 2, new string('a',500));
  1107. dt.Rows.Add (1, 2, "aaa");
  1108. tableView.Table = dt;
  1109. tableView.Redraw (tableView.Bounds);
  1110. // default behaviour of TableView is not to render
  1111. // columns unless there is sufficient space
  1112. string expected =
  1113. @"
  1114. │A│B │
  1115. ├─┼─────────────────────►
  1116. │1│2 │
  1117. │1│2 │
  1118. ";
  1119. TestHelpers.AssertDriverContentsAre (expected, output);
  1120. // get a style for the long column
  1121. var style = tableView.Style.GetOrCreateColumnStyle(dt.Columns[2]);
  1122. // one way the API user can fix this for long columns
  1123. // is to specify a max width for the column
  1124. style.MaxWidth = 10;
  1125. tableView.Redraw (tableView.Bounds);
  1126. expected =
  1127. @"
  1128. │A│B│Very Long │
  1129. ├─┼─┼───────────────────┤
  1130. │1│2│aaaaaaaaaa │
  1131. │1│2│aaa │
  1132. ";
  1133. TestHelpers.AssertDriverContentsAre (expected, output);
  1134. // revert the style change
  1135. style.MaxWidth = TableView.DefaultMaxCellWidth;
  1136. // another way API user can fix problem is to implement
  1137. // RepresentationGetter and apply max length there
  1138. style.RepresentationGetter = (s)=>{
  1139. return s.ToString().Length < 15 ? s.ToString() : s.ToString().Substring(0,13)+"...";
  1140. };
  1141. tableView.Redraw (tableView.Bounds);
  1142. expected =
  1143. @"
  1144. │A│B│Very Long Column │
  1145. ├─┼─┼───────────────────┤
  1146. │1│2│aaaaaaaaaaaaa... │
  1147. │1│2│aaa │
  1148. ";
  1149. TestHelpers.AssertDriverContentsAre (expected, output);
  1150. // revert style change
  1151. style.RepresentationGetter = null;
  1152. // Both of the above methods rely on having a fixed
  1153. // size limit for the column. These are awkward if a
  1154. // table is resizeable e.g. Dim.Fill(). Ideally we want
  1155. // to render in any space available and truncate the content
  1156. // of the column dynamically so it fills the free space at
  1157. // the end of the table.
  1158. // We can now specify that the column can be any length
  1159. // (Up to MaxWidth) but the renderer can accept using
  1160. // less space down to this limit
  1161. style.MinAcceptableWidth = 5;
  1162. tableView.Redraw (tableView.Bounds);
  1163. expected =
  1164. @"
  1165. │A│B│Very Long Column │
  1166. ├─┼─┼───────────────────┤
  1167. │1│2│aaaaaaaaaaaaaaaaaaa│
  1168. │1│2│aaa │
  1169. ";
  1170. TestHelpers.AssertDriverContentsAre (expected, output);
  1171. // Now test making the width too small for the MinAcceptableWidth
  1172. // the Column won't fit so should not be rendered
  1173. Application.Shutdown ();
  1174. GraphViewTests.InitFakeDriver ();
  1175. tableView.Bounds = new Rect(0,0,9,5);
  1176. tableView.Redraw (tableView.Bounds);
  1177. expected =
  1178. @"
  1179. │A│B │
  1180. ├─┼─────►
  1181. │1│2 │
  1182. │1│2 │
  1183. ";
  1184. TestHelpers.AssertDriverContentsAre (expected, output);
  1185. // setting width to 10 leaves just enough space for the column to
  1186. // meet MinAcceptableWidth of 5. Column width includes terminator line
  1187. // symbol (e.g. ┤ or │)
  1188. tableView.Bounds = new Rect (0, 0, 10, 5);
  1189. tableView.Redraw (tableView.Bounds);
  1190. expected =
  1191. @"
  1192. │A│B│Very│
  1193. ├─┼─┼────┤
  1194. │1│2│aaaa│
  1195. │1│2│aaa │
  1196. ";
  1197. TestHelpers.AssertDriverContentsAre (expected, output);
  1198. Application.Shutdown ();
  1199. }
  1200. [Fact]
  1201. public void ScrollIndicators ()
  1202. {
  1203. GraphViewTests.InitFakeDriver ();
  1204. var tableView = new TableView ();
  1205. tableView.ColorScheme = Colors.TopLevel;
  1206. // 3 columns are visibile
  1207. tableView.Bounds = new Rect (0, 0, 7, 5);
  1208. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1209. tableView.Style.ShowHorizontalHeaderOverline = false;
  1210. tableView.Style.AlwaysShowHeaders = true;
  1211. tableView.Style.SmoothHorizontalScrolling = true;
  1212. var dt = new DataTable ();
  1213. dt.Columns.Add ("A");
  1214. dt.Columns.Add ("B");
  1215. dt.Columns.Add ("C");
  1216. dt.Columns.Add ("D");
  1217. dt.Columns.Add ("E");
  1218. dt.Columns.Add ("F");
  1219. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1220. tableView.Table = dt;
  1221. // select last visible column
  1222. tableView.SelectedColumn = 2; // column C
  1223. tableView.Redraw (tableView.Bounds);
  1224. // user can only scroll right so sees right indicator
  1225. // Because first column in table is A
  1226. string expected =
  1227. @"
  1228. │A│B│C│
  1229. ├─┼─┼─►
  1230. │1│2│3│";
  1231. TestHelpers.AssertDriverContentsAre (expected, output);
  1232. // Scroll right
  1233. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1234. // since A is now pushed off screen we get indicator showing
  1235. // that user can scroll left to see first column
  1236. tableView.Redraw (tableView.Bounds);
  1237. expected =
  1238. @"
  1239. │B│C│D│
  1240. ◄─┼─┼─►
  1241. │2│3│4│";
  1242. TestHelpers.AssertDriverContentsAre (expected, output);
  1243. // Scroll right twice more (to end of columns)
  1244. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1245. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1246. tableView.Redraw (tableView.Bounds);
  1247. expected =
  1248. @"
  1249. │D│E│F│
  1250. ◄─┼─┼─┤
  1251. │4│5│6│";
  1252. TestHelpers.AssertDriverContentsAre (expected, output);
  1253. // Shutdown must be called to safely clean up Application if Init has been called
  1254. Application.Shutdown ();
  1255. }
  1256. /// <summary>
  1257. /// Builds a simple table of string columns with the requested number of columns and rows
  1258. /// </summary>
  1259. /// <param name="cols"></param>
  1260. /// <param name="rows"></param>
  1261. /// <returns></returns>
  1262. public static DataTable BuildTable (int cols, int rows)
  1263. {
  1264. var dt = new DataTable ();
  1265. for (int c = 0; c < cols; c++) {
  1266. dt.Columns.Add ("Col" + c);
  1267. }
  1268. for (int r = 0; r < rows; r++) {
  1269. var newRow = dt.NewRow ();
  1270. for (int c = 0; c < cols; c++) {
  1271. newRow [c] = $"R{r}C{c}";
  1272. }
  1273. dt.Rows.Add (newRow);
  1274. }
  1275. return dt;
  1276. }
  1277. [Fact, AutoInitShutdown]
  1278. public void Test_ScreenToCell ()
  1279. {
  1280. var tableView = GetTwoRowSixColumnTable ();
  1281. tableView.Redraw (tableView.Bounds);
  1282. // user can only scroll right so sees right indicator
  1283. // Because first column in table is A
  1284. string expected =
  1285. @"
  1286. │A│B│C│
  1287. ├─┼─┼─►
  1288. │1│2│3│
  1289. │1│2│3│";
  1290. TestHelpers.AssertDriverContentsAre (expected, output);
  1291. // ---------------- X=0 -----------------------
  1292. // click is before first cell
  1293. Assert.Null (tableView.ScreenToCell (0, 0));
  1294. Assert.Null (tableView.ScreenToCell (0, 1));
  1295. Assert.Null (tableView.ScreenToCell (0, 2));
  1296. Assert.Null (tableView.ScreenToCell (0, 3));
  1297. Assert.Null (tableView.ScreenToCell (0, 4));
  1298. // ---------------- X=1 -----------------------
  1299. // click in header
  1300. Assert.Null (tableView.ScreenToCell (1, 0));
  1301. // click in header row line
  1302. Assert.Null (tableView.ScreenToCell (1, 1));
  1303. // click in cell 0,0
  1304. Assert.Equal (new Point(0,0),tableView.ScreenToCell (1, 2));
  1305. // click in cell 0,1
  1306. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3));
  1307. // after last row
  1308. Assert.Null (tableView.ScreenToCell (1, 4));
  1309. // ---------------- X=2 -----------------------
  1310. // ( even though there is a horizontal dividing line here we treat it as a hit on the cell before)
  1311. // click in header
  1312. Assert.Null (tableView.ScreenToCell (2, 0));
  1313. // click in header row line
  1314. Assert.Null (tableView.ScreenToCell (2, 1));
  1315. // click in cell 0,0
  1316. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (2, 2));
  1317. // click in cell 0,1
  1318. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3));
  1319. // after last row
  1320. Assert.Null (tableView.ScreenToCell (2, 4));
  1321. // ---------------- X=3 -----------------------
  1322. // click in header
  1323. Assert.Null (tableView.ScreenToCell (3, 0));
  1324. // click in header row line
  1325. Assert.Null (tableView.ScreenToCell (3, 1));
  1326. // click in cell 1,0
  1327. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2));
  1328. // click in cell 1,1
  1329. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3));
  1330. // after last row
  1331. Assert.Null (tableView.ScreenToCell (3, 4));
  1332. }
  1333. [Fact, AutoInitShutdown]
  1334. public void Test_ScreenToCell_DataColumnOverload ()
  1335. {
  1336. var tableView = GetTwoRowSixColumnTable ();
  1337. tableView.Redraw (tableView.Bounds);
  1338. // user can only scroll right so sees right indicator
  1339. // Because first column in table is A
  1340. string expected =
  1341. @"
  1342. │A│B│C│
  1343. ├─┼─┼─►
  1344. │1│2│3│
  1345. │1│2│3│";
  1346. TestHelpers.AssertDriverContentsAre (expected, output);
  1347. DataColumn col;
  1348. // ---------------- X=0 -----------------------
  1349. // click is before first cell
  1350. Assert.Null (tableView.ScreenToCell (0, 0,out col));
  1351. Assert.Null (col);
  1352. Assert.Null (tableView.ScreenToCell (0, 1,out col));
  1353. Assert.Null (col);
  1354. Assert.Null (tableView.ScreenToCell (0, 2,out col));
  1355. Assert.Null (col);
  1356. Assert.Null (tableView.ScreenToCell (0, 3,out col));
  1357. Assert.Null (col);
  1358. Assert.Null (tableView.ScreenToCell (0, 4,out col));
  1359. Assert.Null (col);
  1360. // ---------------- X=1 -----------------------
  1361. // click in header
  1362. Assert.Null (tableView.ScreenToCell (1, 0, out col));
  1363. Assert.Equal ("A", col.ColumnName);
  1364. // click in header row line (click in the horizontal line below header counts as click in header above - consistent with the column hit box)
  1365. Assert.Null (tableView.ScreenToCell (1, 1, out col));
  1366. Assert.Equal ("A", col.ColumnName);
  1367. // click in cell 0,0
  1368. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (1, 2, out col));
  1369. Assert.Null (col);
  1370. // click in cell 0,1
  1371. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3, out col));
  1372. Assert.Null (col);
  1373. // after last row
  1374. Assert.Null (tableView.ScreenToCell (1, 4, out col));
  1375. Assert.Null (col);
  1376. // ---------------- X=2 -----------------------
  1377. // click in header
  1378. Assert.Null (tableView.ScreenToCell (2, 0, out col));
  1379. Assert.Equal ("A", col.ColumnName);
  1380. // click in header row line
  1381. Assert.Null (tableView.ScreenToCell (2, 1, out col));
  1382. Assert.Equal ("A", col.ColumnName);
  1383. // click in cell 0,0
  1384. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (2, 2, out col));
  1385. Assert.Null (col);
  1386. // click in cell 0,1
  1387. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3, out col));
  1388. Assert.Null (col);
  1389. // after last row
  1390. Assert.Null (tableView.ScreenToCell (2, 4, out col));
  1391. Assert.Null (col);
  1392. // ---------------- X=3 -----------------------
  1393. // click in header
  1394. Assert.Null (tableView.ScreenToCell (3, 0, out col));
  1395. Assert.Equal ("B", col.ColumnName);
  1396. // click in header row line
  1397. Assert.Null (tableView.ScreenToCell (3, 1, out col));
  1398. Assert.Equal ("B", col.ColumnName);
  1399. // click in cell 1,0
  1400. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2, out col));
  1401. Assert.Null (col);
  1402. // click in cell 1,1
  1403. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3, out col));
  1404. Assert.Null (col);
  1405. // after last row
  1406. Assert.Null (tableView.ScreenToCell (3, 4, out col));
  1407. Assert.Null (col);
  1408. }
  1409. private TableView GetTwoRowSixColumnTable ()
  1410. {
  1411. var tableView = new TableView ();
  1412. tableView.ColorScheme = Colors.TopLevel;
  1413. // 3 columns are visible
  1414. tableView.Bounds = new Rect (0, 0, 7, 5);
  1415. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1416. tableView.Style.ShowHorizontalHeaderOverline = false;
  1417. tableView.Style.AlwaysShowHeaders = true;
  1418. tableView.Style.SmoothHorizontalScrolling = true;
  1419. var dt = new DataTable ();
  1420. dt.Columns.Add ("A");
  1421. dt.Columns.Add ("B");
  1422. dt.Columns.Add ("C");
  1423. dt.Columns.Add ("D");
  1424. dt.Columns.Add ("E");
  1425. dt.Columns.Add ("F");
  1426. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1427. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1428. tableView.Table = dt;
  1429. return tableView;
  1430. }
  1431. }
  1432. }