TableViewTests.cs 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  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.ViewTests {
  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. tableView.ChangeSelectionToEndOfTable(false);
  249. // select the last row
  250. tableView.MultiSelectedRegions.Clear ();
  251. tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (0, 3), new Rect (0, 3, 4, 1)));
  252. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  253. // remove a row
  254. tableView.Table.Rows.RemoveAt (0);
  255. tableView.EnsureValidSelection ();
  256. // since the selection no longer exists it should be removed
  257. Assert.Empty (tableView.MultiSelectedRegions);
  258. }
  259. [Theory]
  260. [InlineData (true)]
  261. [InlineData (false)]
  262. public void GetAllSelectedCells_SingleCellSelected_ReturnsOne (bool multiSelect)
  263. {
  264. var tableView = new TableView () {
  265. Table = BuildTable (3, 3),
  266. MultiSelect = multiSelect,
  267. Bounds = new Rect (0, 0, 10, 5)
  268. };
  269. tableView.SetSelection (1, 1, false);
  270. Assert.Single (tableView.GetAllSelectedCells ());
  271. Assert.Equal (new Point (1, 1), tableView.GetAllSelectedCells ().Single ());
  272. }
  273. [Fact]
  274. public void GetAllSelectedCells_SquareSelection_ReturnsFour ()
  275. {
  276. var tableView = new TableView () {
  277. Table = BuildTable (3, 3),
  278. MultiSelect = true,
  279. Bounds = new Rect (0, 0, 10, 5)
  280. };
  281. // move cursor to 1,1
  282. tableView.SetSelection (1, 1, false);
  283. // spread selection across to 2,2 (e.g. shift+right then shift+down)
  284. tableView.SetSelection (2, 2, true);
  285. var selected = tableView.GetAllSelectedCells ().ToArray ();
  286. Assert.Equal (4, selected.Length);
  287. Assert.Equal (new Point (1, 1), selected [0]);
  288. Assert.Equal (new Point (2, 1), selected [1]);
  289. Assert.Equal (new Point (1, 2), selected [2]);
  290. Assert.Equal (new Point (2, 2), selected [3]);
  291. }
  292. [Fact]
  293. public void GetAllSelectedCells_SquareSelection_FullRowSelect ()
  294. {
  295. var tableView = new TableView () {
  296. Table = BuildTable (3, 3),
  297. MultiSelect = true,
  298. FullRowSelect = true,
  299. Bounds = new Rect (0, 0, 10, 5)
  300. };
  301. // move cursor to 1,1
  302. tableView.SetSelection (1, 1, false);
  303. // spread selection across to 2,2 (e.g. shift+right then shift+down)
  304. tableView.SetSelection (2, 2, true);
  305. var selected = tableView.GetAllSelectedCells ().ToArray ();
  306. Assert.Equal (6, selected.Length);
  307. Assert.Equal (new Point (0, 1), selected [0]);
  308. Assert.Equal (new Point (1, 1), selected [1]);
  309. Assert.Equal (new Point (2, 1), selected [2]);
  310. Assert.Equal (new Point (0, 2), selected [3]);
  311. Assert.Equal (new Point (1, 2), selected [4]);
  312. Assert.Equal (new Point (2, 2), selected [5]);
  313. }
  314. [Fact]
  315. public void GetAllSelectedCells_TwoIsolatedSelections_ReturnsSix ()
  316. {
  317. var tableView = new TableView () {
  318. Table = BuildTable (20, 20),
  319. MultiSelect = true,
  320. Bounds = new Rect (0, 0, 10, 5)
  321. };
  322. /*
  323. Sets up disconnected selections like:
  324. 00000000000
  325. 01100000000
  326. 01100000000
  327. 00000001100
  328. 00000000000
  329. */
  330. tableView.MultiSelectedRegions.Clear ();
  331. tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (1, 1), new Rect (1, 1, 2, 2)));
  332. tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (7, 3), new Rect (7, 3, 2, 1)));
  333. tableView.SelectedColumn = 8;
  334. tableView.SelectedRow = 3;
  335. var selected = tableView.GetAllSelectedCells ().ToArray ();
  336. Assert.Equal (6, selected.Length);
  337. Assert.Equal (new Point (1, 1), selected [0]);
  338. Assert.Equal (new Point (2, 1), selected [1]);
  339. Assert.Equal (new Point (1, 2), selected [2]);
  340. Assert.Equal (new Point (2, 2), selected [3]);
  341. Assert.Equal (new Point (7, 3), selected [4]);
  342. Assert.Equal (new Point (8, 3), selected [5]);
  343. }
  344. [Fact]
  345. public void TableView_ExpandLastColumn_True ()
  346. {
  347. var tv = SetUpMiniTable ();
  348. // the thing we are testing
  349. tv.Style.ExpandLastColumn = true;
  350. tv.Redraw (tv.Bounds);
  351. string expected = @"
  352. ┌─┬──────┐
  353. │A│B │
  354. ├─┼──────┤
  355. │1│2 │
  356. ";
  357. TestHelpers.AssertDriverContentsAre (expected, output);
  358. // Shutdown must be called to safely clean up Application if Init has been called
  359. Application.Shutdown ();
  360. }
  361. [Fact]
  362. public void TableView_ExpandLastColumn_False ()
  363. {
  364. var tv = SetUpMiniTable ();
  365. // the thing we are testing
  366. tv.Style.ExpandLastColumn = false;
  367. tv.Redraw (tv.Bounds);
  368. string expected = @"
  369. ┌─┬─┬────┐
  370. │A│B│ │
  371. ├─┼─┼────┤
  372. │1│2│ │
  373. ";
  374. TestHelpers.AssertDriverContentsAre (expected, output);
  375. // Shutdown must be called to safely clean up Application if Init has been called
  376. Application.Shutdown ();
  377. }
  378. [Fact]
  379. public void TableView_ExpandLastColumn_False_ExactBounds ()
  380. {
  381. var tv = SetUpMiniTable ();
  382. // the thing we are testing
  383. tv.Style.ExpandLastColumn = false;
  384. // width exactly matches the max col widths
  385. tv.Bounds = new Rect (0, 0, 5, 4);
  386. tv.Redraw (tv.Bounds);
  387. string expected = @"
  388. ┌─┬─┐
  389. │A│B│
  390. ├─┼─┤
  391. │1│2│
  392. ";
  393. TestHelpers.AssertDriverContentsAre (expected, output);
  394. // Shutdown must be called to safely clean up Application if Init has been called
  395. Application.Shutdown ();
  396. }
  397. [Fact]
  398. [AutoInitShutdown]
  399. public void TableView_Activate()
  400. {
  401. string activatedValue = null;
  402. var tv = new TableView (BuildTable(1,1));
  403. tv.CellActivated += (c) => activatedValue = c.Table.Rows[c.Row][c.Col].ToString();
  404. Application.Top.Add (tv);
  405. Application.Begin (Application.Top);
  406. // pressing enter should activate the first cell (selected cell)
  407. tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  408. Assert.Equal ("R0C0",activatedValue);
  409. // reset the test
  410. activatedValue = null;
  411. // clear keybindings and ensure that Enter does not trigger the event anymore
  412. tv.ClearKeybindings ();
  413. tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  414. Assert.Null(activatedValue);
  415. // New method for changing the activation key
  416. tv.AddKeyBinding (Key.z, Command.Accept);
  417. tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ()));
  418. Assert.Equal ("R0C0", activatedValue);
  419. // reset the test
  420. activatedValue = null;
  421. tv.ClearKeybindings ();
  422. // Old method for changing the activation key
  423. tv.CellActivationKey = Key.z;
  424. tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ()));
  425. Assert.Equal ("R0C0", activatedValue);
  426. }
  427. [Fact]
  428. public void TableViewMultiSelect_CannotFallOffLeft()
  429. {
  430. var tv = SetUpMiniTable ();
  431. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  432. tv.MultiSelect = true;
  433. tv.SelectedColumn = 1;
  434. tv.SelectedRow = 1;
  435. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  436. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single().Rect);
  437. // this next shift left should be ignored because we are already at the bounds
  438. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  439. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  440. Assert.Equal (0, tv.SelectedColumn);
  441. Assert.Equal (1, tv.SelectedRow);
  442. Application.Shutdown ();
  443. }
  444. [Fact]
  445. public void TableViewMultiSelect_CannotFallOffRight()
  446. {
  447. var tv = SetUpMiniTable ();
  448. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  449. tv.MultiSelect = true;
  450. tv.SelectedColumn = 0;
  451. tv.SelectedRow = 1;
  452. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  453. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  454. // this next shift right should be ignored because we are already at the right bounds
  455. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  456. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  457. Assert.Equal (1, tv.SelectedColumn);
  458. Assert.Equal (1, tv.SelectedRow);
  459. Application.Shutdown ();
  460. }
  461. [Fact]
  462. public void TableViewMultiSelect_CannotFallOffBottom ()
  463. {
  464. var tv = SetUpMiniTable ();
  465. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  466. tv.MultiSelect = true;
  467. tv.SelectedColumn = 0;
  468. tv.SelectedRow = 0;
  469. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  470. tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
  471. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  472. // this next moves should be ignored because we already selected the whole table
  473. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  474. tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
  475. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  476. Assert.Equal (1, tv.SelectedColumn);
  477. Assert.Equal (1, tv.SelectedRow);
  478. Application.Shutdown ();
  479. }
  480. [Fact]
  481. public void TableViewMultiSelect_CannotFallOffTop()
  482. {
  483. var tv = SetUpMiniTable ();
  484. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  485. tv.MultiSelect = true;
  486. tv.SelectedColumn = 1;
  487. tv.SelectedRow = 1;
  488. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  489. tv.ProcessKey (new KeyEvent (Key.CursorUp | Key.ShiftMask, new KeyModifiers { Shift = true }));
  490. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  491. // this next moves should be ignored because we already selected the whole table
  492. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  493. tv.ProcessKey (new KeyEvent (Key.CursorUp | Key.ShiftMask, new KeyModifiers { Shift = true }));
  494. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  495. Assert.Equal (0, tv.SelectedColumn);
  496. Assert.Equal (0, tv.SelectedRow);
  497. Application.Shutdown ();
  498. }
  499. [Fact, AutoInitShutdown]
  500. public void TestShiftClick_MultiSelect_TwoRowTable_FullRowSelect()
  501. {
  502. var tv = GetTwoRowSixColumnTable ();
  503. tv.MultiSelect = true;
  504. // Clicking in bottom row
  505. tv.MouseEvent (new MouseEvent {
  506. X = 1,
  507. Y = 3,
  508. Flags = MouseFlags.Button1Clicked
  509. });
  510. // should select that row
  511. Assert.Equal (1, tv.SelectedRow);
  512. // shift clicking top row
  513. tv.MouseEvent (new MouseEvent {
  514. X = 1,
  515. Y = 2,
  516. Flags = MouseFlags.Button1Clicked | MouseFlags.ButtonShift
  517. });
  518. // should extend the selection
  519. Assert.Equal (0, tv.SelectedRow);
  520. var selected = tv.GetAllSelectedCells ().ToArray();
  521. Assert.Contains (new Point(0,0), selected);
  522. Assert.Contains (new Point (0, 1), selected);
  523. }
  524. [Fact, AutoInitShutdown]
  525. public void TestControlClick_MultiSelect_ThreeRowTable_FullRowSelect ()
  526. {
  527. var tv = GetTwoRowSixColumnTable ();
  528. tv.Table.Rows.Add (1, 2, 3, 4, 5, 6);
  529. tv.MultiSelect = true;
  530. // Clicking in bottom row
  531. tv.MouseEvent (new MouseEvent {
  532. X = 1,
  533. Y = 4,
  534. Flags = MouseFlags.Button1Clicked
  535. });
  536. // should select that row
  537. Assert.Equal (2, tv.SelectedRow);
  538. // shift clicking top row
  539. tv.MouseEvent (new MouseEvent {
  540. X = 1,
  541. Y = 2,
  542. Flags = MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl
  543. });
  544. // should extend the selection
  545. // to include bottom and top row but not middle
  546. Assert.Equal (0, tv.SelectedRow);
  547. var selected = tv.GetAllSelectedCells ().ToArray ();
  548. Assert.Contains (new Point (0, 0), selected);
  549. Assert.DoesNotContain (new Point (0, 1), selected);
  550. Assert.Contains (new Point (0, 2), selected);
  551. }
  552. [Theory]
  553. [InlineData (false)]
  554. [InlineData (true)]
  555. public void TableView_ColorTests_FocusedOrNot (bool focused)
  556. {
  557. var tv = SetUpMiniTable ();
  558. // width exactly matches the max col widths
  559. tv.Bounds = new Rect (0, 0, 5, 4);
  560. // private method for forcing the view to be focused/not focused
  561. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  562. // when the view is/isn't focused
  563. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  564. tv.Redraw (tv.Bounds);
  565. string expected = @"
  566. ┌─┬─┐
  567. │A│B│
  568. ├─┼─┤
  569. │1│2│
  570. ";
  571. TestHelpers.AssertDriverContentsAre (expected, output);
  572. string expectedColors = @"
  573. 00000
  574. 00000
  575. 00000
  576. 01000
  577. ";
  578. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  579. // 0
  580. tv.ColorScheme.Normal,
  581. // 1
  582. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal});
  583. Application.Shutdown();
  584. }
  585. [Theory]
  586. [InlineData (false)]
  587. [InlineData (true)]
  588. public void TableView_ColorTests_InvertSelectedCellFirstCharacter (bool focused)
  589. {
  590. var tv = SetUpMiniTable ();
  591. tv.Style.InvertSelectedCellFirstCharacter = true;
  592. // width exactly matches the max col widths
  593. tv.Bounds = new Rect (0, 0, 5, 4);
  594. // private method for forcing the view to be focused/not focused
  595. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  596. // when the view is/isn't focused
  597. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  598. tv.Redraw (tv.Bounds);
  599. string expected = @"
  600. ┌─┬─┐
  601. │A│B│
  602. ├─┼─┤
  603. │1│2│
  604. ";
  605. TestHelpers.AssertDriverContentsAre (expected, output);
  606. string expectedColors = @"
  607. 00000
  608. 00000
  609. 00000
  610. 01000
  611. ";
  612. var invertHotFocus = new Attribute(tv.ColorScheme.HotFocus.Background,tv.ColorScheme.HotFocus.Foreground);
  613. var invertHotNormal = new Attribute(tv.ColorScheme.HotNormal.Background,tv.ColorScheme.HotNormal.Foreground);
  614. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  615. // 0
  616. tv.ColorScheme.Normal,
  617. // 1
  618. focused ? invertHotFocus : invertHotNormal});
  619. Application.Shutdown();
  620. }
  621. [Theory]
  622. [InlineData (false)]
  623. [InlineData (true)]
  624. public void TableView_ColorsTest_RowColorGetter (bool focused)
  625. {
  626. var tv = SetUpMiniTable ();
  627. // width exactly matches the max col widths
  628. tv.Bounds = new Rect (0, 0, 5, 4);
  629. var rowHighlight = new ColorScheme () {
  630. Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
  631. HotNormal = Attribute.Make (Color.Green, Color.Blue),
  632. HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
  633. Focus = Attribute.Make (Color.Cyan, Color.Magenta),
  634. };
  635. // when B is 2 use the custom highlight colour for the row
  636. tv.Style.RowColorGetter += (e)=>Convert.ToInt32(e.Table.Rows[e.RowIndex][1]) == 2 ? rowHighlight : null;
  637. // private method for forcing the view to be focused/not focused
  638. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  639. // when the view is/isn't focused
  640. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  641. tv.Redraw (tv.Bounds);
  642. string expected = @"
  643. ┌─┬─┐
  644. │A│B│
  645. ├─┼─┤
  646. │1│2│
  647. ";
  648. TestHelpers.AssertDriverContentsAre (expected, output);
  649. string expectedColors = @"
  650. 00000
  651. 00000
  652. 00000
  653. 21222
  654. ";
  655. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  656. // 0
  657. tv.ColorScheme.Normal,
  658. // 1
  659. focused ? rowHighlight.HotFocus : rowHighlight.HotNormal,
  660. // 2
  661. rowHighlight.Normal});
  662. // change the value in the table so that
  663. // it no longer matches the RowColorGetter
  664. // delegate conditional ( which checks for
  665. // the value 2)
  666. tv.Table.Rows[0][1] = 5;
  667. tv.Redraw (tv.Bounds);
  668. expected = @"
  669. ┌─┬─┐
  670. │A│B│
  671. ├─┼─┤
  672. │1│5│
  673. ";
  674. TestHelpers.AssertDriverContentsAre (expected, output);
  675. expectedColors = @"
  676. 00000
  677. 00000
  678. 00000
  679. 01000
  680. ";
  681. // now we only see 2 colors used (the selected cell color and Normal
  682. // rowHighlight should no longer be used because the delegate returned null
  683. // (now that the cell value is 5 - which does not match the conditional)
  684. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  685. // 0
  686. tv.ColorScheme.Normal,
  687. // 1
  688. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
  689. // Shutdown must be called to safely clean up Application if Init has been called
  690. Application.Shutdown ();
  691. }
  692. [Theory]
  693. [InlineData (false)]
  694. [InlineData (true)]
  695. public void TableView_ColorsTest_ColorGetter (bool focused)
  696. {
  697. var tv = SetUpMiniTable ();
  698. // width exactly matches the max col widths
  699. tv.Bounds = new Rect (0, 0, 5, 4);
  700. // Create a style for column B
  701. var bStyle = tv.Style.GetOrCreateColumnStyle (tv.Table.Columns ["B"]);
  702. // when B is 2 use the custom highlight colour
  703. var cellHighlight = new ColorScheme () {
  704. Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
  705. HotNormal = Attribute.Make (Color.Green, Color.Blue),
  706. HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
  707. Focus = Attribute.Make (Color.Cyan, Color.Magenta),
  708. };
  709. bStyle.ColorGetter = (a) => Convert.ToInt32(a.CellValue) == 2 ? cellHighlight : null;
  710. // private method for forcing the view to be focused/not focused
  711. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  712. // when the view is/isn't focused
  713. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  714. tv.Redraw (tv.Bounds);
  715. string expected = @"
  716. ┌─┬─┐
  717. │A│B│
  718. ├─┼─┤
  719. │1│2│
  720. ";
  721. TestHelpers.AssertDriverContentsAre (expected, output);
  722. string expectedColors = @"
  723. 00000
  724. 00000
  725. 00000
  726. 01020
  727. ";
  728. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  729. // 0
  730. tv.ColorScheme.Normal,
  731. // 1
  732. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal,
  733. // 2
  734. cellHighlight.Normal});
  735. // change the value in the table so that
  736. // it no longer matches the ColorGetter
  737. // delegate conditional ( which checks for
  738. // the value 2)
  739. tv.Table.Rows[0][1] = 5;
  740. tv.Redraw (tv.Bounds);
  741. expected = @"
  742. ┌─┬─┐
  743. │A│B│
  744. ├─┼─┤
  745. │1│5│
  746. ";
  747. TestHelpers.AssertDriverContentsAre (expected, output);
  748. expectedColors = @"
  749. 00000
  750. 00000
  751. 00000
  752. 01000
  753. ";
  754. // now we only see 2 colors used (the selected cell color and Normal
  755. // cellHighlight should no longer be used because the delegate returned null
  756. // (now that the cell value is 5 - which does not match the conditional)
  757. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  758. // 0
  759. tv.ColorScheme.Normal,
  760. // 1
  761. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
  762. // Shutdown must be called to safely clean up Application if Init has been called
  763. Application.Shutdown ();
  764. }
  765. private TableView SetUpMiniTable ()
  766. {
  767. var tv = new TableView ();
  768. tv.Bounds = new Rect (0, 0, 10, 4);
  769. var dt = new DataTable ();
  770. var colA = dt.Columns.Add ("A");
  771. var colB = dt.Columns.Add ("B");
  772. dt.Rows.Add (1, 2);
  773. tv.Table = dt;
  774. tv.Style.GetOrCreateColumnStyle (colA).MinWidth = 1;
  775. tv.Style.GetOrCreateColumnStyle (colA).MinWidth = 1;
  776. tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
  777. tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
  778. GraphViewTests.InitFakeDriver ();
  779. tv.ColorScheme = Colors.Base;
  780. return tv;
  781. }
  782. [Fact]
  783. [AutoInitShutdown]
  784. public void ScrollDown_OneLineAtATime ()
  785. {
  786. var tableView = new TableView ();
  787. // Set big table
  788. tableView.Table = BuildTable (25, 50);
  789. // 1 header + 4 rows visible
  790. tableView.Bounds = new Rect (0, 0, 25, 5);
  791. tableView.Style.ShowHorizontalHeaderUnderline = false;
  792. tableView.Style.ShowHorizontalHeaderOverline = false;
  793. tableView.Style.AlwaysShowHeaders = true;
  794. // select last row
  795. tableView.SelectedRow = 3; // row is 0 indexed so this is the 4th visible row
  796. // Scroll down
  797. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorDown });
  798. // Scrolled off the page by 1 row so it should only have moved down 1 line of RowOffset
  799. Assert.Equal(4,tableView.SelectedRow);
  800. Assert.Equal (1, tableView.RowOffset);
  801. }
  802. [Fact]
  803. public void ScrollRight_SmoothScrolling ()
  804. {
  805. GraphViewTests.InitFakeDriver ();
  806. var tableView = new TableView ();
  807. tableView.ColorScheme = Colors.TopLevel;
  808. // 3 columns are visibile
  809. tableView.Bounds = new Rect (0, 0, 7, 5);
  810. tableView.Style.ShowHorizontalHeaderUnderline = false;
  811. tableView.Style.ShowHorizontalHeaderOverline = false;
  812. tableView.Style.AlwaysShowHeaders = true;
  813. tableView.Style.SmoothHorizontalScrolling = true;
  814. var dt = new DataTable ();
  815. dt.Columns.Add ("A");
  816. dt.Columns.Add ("B");
  817. dt.Columns.Add ("C");
  818. dt.Columns.Add ("D");
  819. dt.Columns.Add ("E");
  820. dt.Columns.Add ("F");
  821. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  822. tableView.Table = dt;
  823. // select last visible column
  824. tableView.SelectedColumn = 2; // column C
  825. tableView.Redraw (tableView.Bounds);
  826. string expected =
  827. @"
  828. │A│B│C│
  829. │1│2│3│";
  830. TestHelpers.AssertDriverContentsAre (expected, output);
  831. // Scroll right
  832. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  833. tableView.Redraw (tableView.Bounds);
  834. // Note that with SmoothHorizontalScrolling only a single new column
  835. // is exposed when scrolling right. This is not always the case though
  836. // sometimes if the leftmost column is long (i.e. A is a long column)
  837. // then when A is pushed off the screen multiple new columns could be exposed
  838. // (not just D but also E and F). This is because TableView never shows
  839. // 'half cells' or scrolls by console unit (scrolling is done by table row/column increments).
  840. expected =
  841. @"
  842. │B│C│D│
  843. │2│3│4│";
  844. TestHelpers.AssertDriverContentsAre (expected, output);
  845. // Shutdown must be called to safely clean up Application if Init has been called
  846. Application.Shutdown ();
  847. }
  848. [Fact]
  849. public void ScrollRight_WithoutSmoothScrolling ()
  850. {
  851. GraphViewTests.InitFakeDriver ();
  852. var tableView = new TableView ();
  853. tableView.ColorScheme = Colors.TopLevel;
  854. // 3 columns are visibile
  855. tableView.Bounds = new Rect (0, 0, 7, 5);
  856. tableView.Style.ShowHorizontalHeaderUnderline = false;
  857. tableView.Style.ShowHorizontalHeaderOverline = false;
  858. tableView.Style.AlwaysShowHeaders = true;
  859. tableView.Style.SmoothHorizontalScrolling = false;
  860. var dt = new DataTable ();
  861. dt.Columns.Add ("A");
  862. dt.Columns.Add ("B");
  863. dt.Columns.Add ("C");
  864. dt.Columns.Add ("D");
  865. dt.Columns.Add ("E");
  866. dt.Columns.Add ("F");
  867. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  868. tableView.Table = dt;
  869. // select last visible column
  870. tableView.SelectedColumn = 2; // column C
  871. tableView.Redraw (tableView.Bounds);
  872. string expected =
  873. @"
  874. │A│B│C│
  875. │1│2│3│";
  876. TestHelpers.AssertDriverContentsAre (expected, output);
  877. // Scroll right
  878. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  879. tableView.Redraw (tableView.Bounds);
  880. // notice that without smooth scrolling we just update the first column
  881. // rendered in the table to the newly exposed column (D). This is fast
  882. // since we don't have to worry about repeatedly measuring the content
  883. // area as we scroll until the new column (D) is exposed. But it makes
  884. // the view 'jump' to expose all new columns
  885. expected =
  886. @"
  887. │D│E│F│
  888. │4│5│6│";
  889. TestHelpers.AssertDriverContentsAre (expected, output);
  890. // Shutdown must be called to safely clean up Application if Init has been called
  891. Application.Shutdown ();
  892. }
  893. private TableView GetABCDEFTableView (out DataTable dt)
  894. {
  895. var tableView = new TableView ();
  896. tableView.ColorScheme = Colors.TopLevel;
  897. // 3 columns are visible
  898. tableView.Bounds = new Rect (0, 0, 7, 5);
  899. tableView.Style.ShowHorizontalHeaderUnderline = false;
  900. tableView.Style.ShowHorizontalHeaderOverline = false;
  901. tableView.Style.AlwaysShowHeaders = true;
  902. tableView.Style.SmoothHorizontalScrolling = false;
  903. dt = new DataTable ();
  904. dt.Columns.Add ("A");
  905. dt.Columns.Add ("B");
  906. dt.Columns.Add ("C");
  907. dt.Columns.Add ("D");
  908. dt.Columns.Add ("E");
  909. dt.Columns.Add ("F");
  910. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  911. tableView.Table = dt;
  912. return tableView;
  913. }
  914. [Fact, AutoInitShutdown]
  915. public void TestColumnStyle_VisibleFalse_IsNotRendered()
  916. {
  917. var tableView = GetABCDEFTableView (out DataTable dt);
  918. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  919. tableView.Redraw (tableView.Bounds);
  920. string expected =
  921. @"
  922. │A│C│D│
  923. │1│3│4│";
  924. TestHelpers.AssertDriverContentsAre (expected, output);
  925. }
  926. [Fact, AutoInitShutdown]
  927. public void TestColumnStyle_FirstColumnVisibleFalse_IsNotRendered ()
  928. {
  929. var tableView = GetABCDEFTableView (out DataTable dt);
  930. tableView.Style.ShowHorizontalScrollIndicators = true;
  931. tableView.Style.ShowHorizontalHeaderUnderline = true;
  932. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  933. tableView.Redraw (tableView.Bounds);
  934. string expected =
  935. @"
  936. │B│C│D│
  937. ├─┼─┼─►
  938. │2│3│4│";
  939. TestHelpers.AssertDriverContentsAre (expected, output);
  940. }
  941. [Fact, AutoInitShutdown]
  942. public void TestColumnStyle_AllColumnsVisibleFalse_BehavesAsTableNull ()
  943. {
  944. var tableView = GetABCDEFTableView (out DataTable dt);
  945. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  946. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  947. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["C"]).Visible = false;
  948. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  949. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  950. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  951. // expect nothing to be rendered when all columns are invisible
  952. string expected =
  953. @"
  954. ";
  955. tableView.Redraw (tableView.Bounds);
  956. TestHelpers.AssertDriverContentsAre (expected, output);
  957. // expect behavior to match when Table is null
  958. tableView.Table = null;
  959. tableView.Redraw (tableView.Bounds);
  960. TestHelpers.AssertDriverContentsAre (expected, output);
  961. }
  962. [Fact, AutoInitShutdown]
  963. public void TestColumnStyle_RemainingColumnsInvisible_NoScrollIndicator ()
  964. {
  965. var tableView = GetABCDEFTableView (out DataTable dt);
  966. tableView.Style.ShowHorizontalScrollIndicators = true;
  967. tableView.Style.ShowHorizontalHeaderUnderline = true;
  968. tableView.Redraw (tableView.Bounds);
  969. // normally we should have scroll indicators because DEF are of screen
  970. string expected =
  971. @"
  972. │A│B│C│
  973. ├─┼─┼─►
  974. │1│2│3│";
  975. TestHelpers.AssertDriverContentsAre (expected, output);
  976. // but if DEF are invisible we shouldn't be showing the indicator
  977. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  978. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  979. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  980. expected =
  981. @"
  982. │A│B│C│
  983. ├─┼─┼─┤
  984. │1│2│3│";
  985. tableView.Redraw (tableView.Bounds);
  986. TestHelpers.AssertDriverContentsAre (expected, output);
  987. }
  988. [Fact, AutoInitShutdown]
  989. public void TestColumnStyle_PreceedingColumnsInvisible_NoScrollIndicator ()
  990. {
  991. var tableView = GetABCDEFTableView (out DataTable dt);
  992. tableView.Style.ShowHorizontalScrollIndicators = true;
  993. tableView.Style.ShowHorizontalHeaderUnderline = true;
  994. tableView.ColumnOffset = 1;
  995. tableView.Redraw (tableView.Bounds);
  996. // normally we should have scroll indicators because A,E and F are of screen
  997. string expected =
  998. @"
  999. │B│C│D│
  1000. ◄─┼─┼─►
  1001. │2│3│4│";
  1002. TestHelpers.AssertDriverContentsAre (expected, output);
  1003. // but if E and F are invisible so we shouldn't show right
  1004. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  1005. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  1006. expected =
  1007. @"
  1008. │B│C│D│
  1009. ◄─┼─┼─┤
  1010. │2│3│4│";
  1011. tableView.Redraw (tableView.Bounds);
  1012. TestHelpers.AssertDriverContentsAre (expected, output);
  1013. // now also A is invisible so we cannot scroll in either direction
  1014. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  1015. expected =
  1016. @"
  1017. │B│C│D│
  1018. ├─┼─┼─┤
  1019. │2│3│4│";
  1020. tableView.Redraw (tableView.Bounds);
  1021. TestHelpers.AssertDriverContentsAre (expected, output);
  1022. }
  1023. [Fact, AutoInitShutdown]
  1024. public void TestColumnStyle_VisibleFalse_CursorStepsOverInvisibleColumns ()
  1025. {
  1026. var tableView = GetABCDEFTableView (out var dt);
  1027. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  1028. tableView.SelectedColumn = 0;
  1029. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1030. // Expect the cursor navigation to skip over the invisible column(s)
  1031. Assert.Equal(2,tableView.SelectedColumn);
  1032. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1033. // Expect the cursor navigation backwards to skip over invisible column too
  1034. Assert.Equal (0, tableView.SelectedColumn);
  1035. }
  1036. [InlineData(true)]
  1037. [InlineData (false)]
  1038. [Theory, AutoInitShutdown]
  1039. public void TestColumnStyle_FirstColumnVisibleFalse_CursorStaysAt1(bool useHome)
  1040. {
  1041. var tableView = GetABCDEFTableView (out var dt);
  1042. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  1043. tableView.SelectedColumn = 0;
  1044. Assert.Equal (0, tableView.SelectedColumn);
  1045. // column 0 is invisible so this method should move to 1
  1046. tableView.EnsureValidSelection();
  1047. Assert.Equal (1, tableView.SelectedColumn);
  1048. tableView.ProcessKey (new KeyEvent
  1049. {
  1050. Key = useHome ? Key.Home : Key.CursorLeft
  1051. });
  1052. // Expect the cursor to stay at 1
  1053. Assert.Equal (1, tableView.SelectedColumn);
  1054. }
  1055. [InlineData(true)]
  1056. [InlineData (false)]
  1057. [Theory, AutoInitShutdown]
  1058. public void TestMoveStartEnd_WithFullRowSelect(bool withFullRowSelect)
  1059. {
  1060. var tableView = GetTwoRowSixColumnTable ();
  1061. tableView.FullRowSelect = withFullRowSelect;
  1062. tableView.SelectedRow = 1;
  1063. tableView.SelectedColumn = 1;
  1064. tableView.ProcessKey (new KeyEvent
  1065. {
  1066. Key = Key.Home | Key.CtrlMask
  1067. });
  1068. if(withFullRowSelect)
  1069. {
  1070. // Should not be any horizontal movement when
  1071. // using navigate to Start/End and FullRowSelect
  1072. Assert.Equal (1, tableView.SelectedColumn);
  1073. Assert.Equal (0, tableView.SelectedRow);
  1074. }
  1075. else
  1076. {
  1077. Assert.Equal (0, tableView.SelectedColumn);
  1078. Assert.Equal (0, tableView.SelectedRow);
  1079. }
  1080. tableView.ProcessKey (new KeyEvent
  1081. {
  1082. Key = Key.End | Key.CtrlMask
  1083. });
  1084. if(withFullRowSelect)
  1085. {
  1086. Assert.Equal (1, tableView.SelectedColumn);
  1087. Assert.Equal (1, tableView.SelectedRow);
  1088. }
  1089. else
  1090. {
  1091. Assert.Equal (5, tableView.SelectedColumn);
  1092. Assert.Equal (1, tableView.SelectedRow);
  1093. }
  1094. }
  1095. [InlineData (true)]
  1096. [InlineData (false)]
  1097. [Theory, AutoInitShutdown]
  1098. public void TestColumnStyle_LastColumnVisibleFalse_CursorStaysAt2 (bool useEnd)
  1099. {
  1100. var tableView = GetABCDEFTableView (out var dt);
  1101. // select D
  1102. tableView.SelectedColumn = 3;
  1103. Assert.Equal (3, tableView.SelectedColumn);
  1104. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  1105. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  1106. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  1107. // column D is invisible so this method should move to 2 (C)
  1108. tableView.EnsureValidSelection ();
  1109. Assert.Equal (2, tableView.SelectedColumn);
  1110. tableView.ProcessKey (new KeyEvent {
  1111. Key = useEnd ? Key.End : Key.CursorRight
  1112. });
  1113. // Expect the cursor to stay at 2
  1114. Assert.Equal (2, tableView.SelectedColumn);
  1115. }
  1116. [Fact, AutoInitShutdown]
  1117. public void TestColumnStyle_VisibleFalse_MultiSelected ()
  1118. {
  1119. var tableView = GetABCDEFTableView (out var dt);
  1120. // user has rectangular selection
  1121. tableView.MultiSelectedRegions.Push (
  1122. new TableView.TableSelection(
  1123. new Point(0,0),
  1124. new Rect(0, 0, 3, 1))
  1125. );
  1126. Assert.Equal (3, tableView.GetAllSelectedCells ().Count());
  1127. Assert.True (tableView.IsSelected (0, 0));
  1128. Assert.True (tableView.IsSelected (1, 0));
  1129. Assert.True (tableView.IsSelected (2, 0));
  1130. Assert.False (tableView.IsSelected (3, 0));
  1131. // if middle column is invisible
  1132. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  1133. // it should not be included in the selection
  1134. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1135. Assert.True (tableView.IsSelected (0, 0));
  1136. Assert.False (tableView.IsSelected (1, 0));
  1137. Assert.True (tableView.IsSelected (2, 0));
  1138. Assert.False (tableView.IsSelected (3, 0));
  1139. Assert.DoesNotContain(new Point(1,0),tableView.GetAllSelectedCells ());
  1140. }
  1141. [Fact, AutoInitShutdown]
  1142. public void TestColumnStyle_VisibleFalse_MultiSelectingStepsOverInvisibleColumns ()
  1143. {
  1144. var tableView = GetABCDEFTableView (out var dt);
  1145. // if middle column is invisible
  1146. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  1147. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight | Key.ShiftMask });
  1148. // Selection should extend from A to C but skip B
  1149. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1150. Assert.True (tableView.IsSelected (0, 0));
  1151. Assert.False (tableView.IsSelected (1, 0));
  1152. Assert.True (tableView.IsSelected (2, 0));
  1153. Assert.False (tableView.IsSelected (3, 0));
  1154. Assert.DoesNotContain (new Point (1, 0), tableView.GetAllSelectedCells ());
  1155. }
  1156. [Fact, AutoInitShutdown]
  1157. public void TestToggleCells_MultiSelectOn ()
  1158. {
  1159. // 2 row table
  1160. var tableView = GetABCDEFTableView (out var dt);
  1161. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1162. tableView.MultiSelect = true;
  1163. tableView.AddKeyBinding(Key.Space,Command.ToggleChecked);
  1164. var selectedCell = tableView.GetAllSelectedCells().Single();
  1165. Assert.Equal(0,selectedCell.X);
  1166. Assert.Equal(0,selectedCell.Y);
  1167. // Go Right
  1168. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1169. selectedCell = tableView.GetAllSelectedCells().Single();
  1170. Assert.Equal(1,selectedCell.X);
  1171. Assert.Equal(0,selectedCell.Y);
  1172. // Toggle Select
  1173. tableView.ProcessKey (new KeyEvent { Key = Key.Space});
  1174. var m = tableView.MultiSelectedRegions.Single();
  1175. Assert.True(m.IsToggled);
  1176. Assert.Equal(1,m.Origin.X);
  1177. Assert.Equal(0,m.Origin.Y);
  1178. selectedCell = tableView.GetAllSelectedCells().Single();
  1179. Assert.Equal(1,selectedCell.X);
  1180. Assert.Equal(0,selectedCell.Y);
  1181. // Go Left
  1182. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1183. // Both Toggled and Moved to should be selected
  1184. Assert.Equal(2,tableView.GetAllSelectedCells().Count());
  1185. var s1 = tableView.GetAllSelectedCells().ElementAt(0);
  1186. var s2 = tableView.GetAllSelectedCells().ElementAt(1);
  1187. Assert.Equal(1,s1.X);
  1188. Assert.Equal(0,s1.Y);
  1189. Assert.Equal(0,s2.X);
  1190. Assert.Equal(0,s2.Y);
  1191. // Go Down
  1192. tableView.ProcessKey (new KeyEvent { Key = Key.CursorDown });
  1193. // Both Toggled and Moved to should be selected but not 0,0
  1194. // which we moved down from
  1195. Assert.Equal(2,tableView.GetAllSelectedCells().Count());
  1196. s1 = tableView.GetAllSelectedCells().ElementAt(0);
  1197. s2 = tableView.GetAllSelectedCells().ElementAt(1);
  1198. Assert.Equal(1,s1.X);
  1199. Assert.Equal(0,s1.Y);
  1200. Assert.Equal(0,s2.X);
  1201. Assert.Equal(1,s2.Y);
  1202. // Go back to the toggled cell
  1203. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight});
  1204. tableView.ProcessKey (new KeyEvent { Key = Key.CursorUp});
  1205. // Toggle off
  1206. tableView.ProcessKey (new KeyEvent { Key = Key.Space});
  1207. // Go Left
  1208. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft});
  1209. selectedCell = tableView.GetAllSelectedCells().Single();
  1210. Assert.Equal(0,selectedCell.X);
  1211. Assert.Equal(0,selectedCell.Y);
  1212. }
  1213. [Fact, AutoInitShutdown]
  1214. public void TestToggleCells_MultiSelectOn_FullRowSelect ()
  1215. {
  1216. // 2 row table
  1217. var tableView = GetABCDEFTableView (out var dt);
  1218. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1219. tableView.FullRowSelect = true;
  1220. tableView.MultiSelect = true;
  1221. tableView.AddKeyBinding(Key.Space,Command.ToggleChecked);
  1222. // Toggle Select Cell 0,0
  1223. tableView.ProcessKey (new KeyEvent { Key = Key.Space});
  1224. // Go Down
  1225. tableView.ProcessKey (new KeyEvent { Key = Key.CursorDown });
  1226. var m = tableView.MultiSelectedRegions.Single();
  1227. Assert.True(m.IsToggled);
  1228. Assert.Equal(0,m.Origin.X);
  1229. Assert.Equal(0,m.Origin.Y);
  1230. //First row toggled and Second row active = 12 selected cells
  1231. Assert.Equal(12,tableView.GetAllSelectedCells().Count());
  1232. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1233. tableView.ProcessKey (new KeyEvent { Key = Key.CursorUp });
  1234. Assert.Single(tableView.MultiSelectedRegions.Where(r=>r.IsToggled));
  1235. // Can untoggle at 1,0 even though 0,0 was initial toggle because FullRowSelect is on
  1236. tableView.ProcessKey (new KeyEvent { Key = Key.Space});
  1237. Assert.Empty(tableView.MultiSelectedRegions.Where(r=>r.IsToggled));
  1238. }
  1239. [Fact, AutoInitShutdown]
  1240. public void TestToggleCells_MultiSelectOn_SquareSelectToggled ()
  1241. {
  1242. // 3 row table
  1243. var tableView = GetABCDEFTableView (out var dt);
  1244. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1245. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1246. tableView.MultiSelect = true;
  1247. tableView.AddKeyBinding(Key.Space,Command.ToggleChecked);
  1248. // Make a square selection
  1249. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorDown});
  1250. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorRight});
  1251. Assert.Equal(4,tableView.GetAllSelectedCells().Count());
  1252. // Toggle the square selected region on
  1253. tableView.ProcessKey (new KeyEvent { Key = Key.Space});
  1254. // Go Right
  1255. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1256. //Toggled on square + the active cell (x=2,y=1)
  1257. Assert.Equal(5,tableView.GetAllSelectedCells().Count());
  1258. Assert.Equal(2,tableView.SelectedColumn);
  1259. Assert.Equal(1,tableView.SelectedRow);
  1260. // Untoggle the rectangular region by hitting toggle in
  1261. // any cell in that rect
  1262. tableView.ProcessKey (new KeyEvent { Key = Key.CursorUp });
  1263. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1264. Assert.Equal(4,tableView.GetAllSelectedCells().Count());
  1265. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1266. Assert.Equal(1,tableView.GetAllSelectedCells().Count());
  1267. }
  1268. [Fact, AutoInitShutdown]
  1269. public void TestToggleCells_MultiSelectOn_Two_SquareSelects_BothToggled ()
  1270. {
  1271. // 6 row table
  1272. var tableView = GetABCDEFTableView (out var dt);
  1273. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1274. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1275. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1276. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1277. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1278. tableView.MultiSelect = true;
  1279. tableView.AddKeyBinding(Key.Space,Command.ToggleChecked);
  1280. // Make first square selection (0,0 to 1,1)
  1281. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorDown});
  1282. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorRight});
  1283. tableView.ProcessKey (new KeyEvent { Key = Key.Space});
  1284. Assert.Equal(4,tableView.GetAllSelectedCells().Count());
  1285. // Make second square selection leaving 1 unselected line between them
  1286. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1287. tableView.ProcessKey (new KeyEvent { Key = Key.CursorDown });
  1288. tableView.ProcessKey (new KeyEvent { Key = Key.CursorDown });
  1289. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorDown});
  1290. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorRight});
  1291. // 2 square selections
  1292. Assert.Equal(8,tableView.GetAllSelectedCells().Count());
  1293. }
  1294. [Theory, AutoInitShutdown]
  1295. [InlineData(new object[] { true,true })]
  1296. [InlineData (new object[] { false,true })]
  1297. [InlineData (new object [] { true, false})]
  1298. [InlineData (new object [] { false, false})]
  1299. public void TestColumnStyle_VisibleFalse_DoesNotEffect_EnsureSelectedCellIsVisible (bool smooth, bool invisibleCol)
  1300. {
  1301. var tableView = GetABCDEFTableView (out var dt);
  1302. tableView.Style.SmoothHorizontalScrolling = smooth;
  1303. if(invisibleCol) {
  1304. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  1305. }
  1306. // New TableView should have first cell selected
  1307. Assert.Equal (0,tableView.SelectedColumn);
  1308. // With no scrolling
  1309. Assert.Equal (0, tableView.ColumnOffset);
  1310. // A,B and C are visible on screen at the moment so these should have no effect
  1311. tableView.SelectedColumn = 1;
  1312. tableView.EnsureSelectedCellIsVisible ();
  1313. Assert.Equal (0, tableView.ColumnOffset);
  1314. tableView.SelectedColumn = 2;
  1315. tableView.EnsureSelectedCellIsVisible ();
  1316. Assert.Equal (0, tableView.ColumnOffset);
  1317. // Selecting D should move the visible table area to fit D onto the screen
  1318. tableView.SelectedColumn = 3;
  1319. tableView.EnsureSelectedCellIsVisible ();
  1320. Assert.Equal (smooth ? 1 : 3, tableView.ColumnOffset);
  1321. }
  1322. [Fact]
  1323. public void LongColumnTest ()
  1324. {
  1325. GraphViewTests.InitFakeDriver ();
  1326. var tableView = new TableView ();
  1327. tableView.ColorScheme = Colors.TopLevel;
  1328. // 25 characters can be printed into table
  1329. tableView.Bounds = new Rect (0, 0, 25, 5);
  1330. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1331. tableView.Style.ShowHorizontalHeaderOverline = false;
  1332. tableView.Style.AlwaysShowHeaders = true;
  1333. tableView.Style.SmoothHorizontalScrolling = true;
  1334. var dt = new DataTable ();
  1335. dt.Columns.Add ("A");
  1336. dt.Columns.Add ("B");
  1337. dt.Columns.Add ("Very Long Column");
  1338. dt.Rows.Add (1, 2, new string('a',500));
  1339. dt.Rows.Add (1, 2, "aaa");
  1340. tableView.Table = dt;
  1341. tableView.Redraw (tableView.Bounds);
  1342. // default behaviour of TableView is not to render
  1343. // columns unless there is sufficient space
  1344. string expected =
  1345. @"
  1346. │A│B │
  1347. ├─┼─────────────────────►
  1348. │1│2 │
  1349. │1│2 │
  1350. ";
  1351. TestHelpers.AssertDriverContentsAre (expected, output);
  1352. // get a style for the long column
  1353. var style = tableView.Style.GetOrCreateColumnStyle(dt.Columns[2]);
  1354. // one way the API user can fix this for long columns
  1355. // is to specify a max width for the column
  1356. style.MaxWidth = 10;
  1357. tableView.Redraw (tableView.Bounds);
  1358. expected =
  1359. @"
  1360. │A│B│Very Long │
  1361. ├─┼─┼───────────────────┤
  1362. │1│2│aaaaaaaaaa │
  1363. │1│2│aaa │
  1364. ";
  1365. TestHelpers.AssertDriverContentsAre (expected, output);
  1366. // revert the style change
  1367. style.MaxWidth = TableView.DefaultMaxCellWidth;
  1368. // another way API user can fix problem is to implement
  1369. // RepresentationGetter and apply max length there
  1370. style.RepresentationGetter = (s)=>{
  1371. return s.ToString().Length < 15 ? s.ToString() : s.ToString().Substring(0,13)+"...";
  1372. };
  1373. tableView.Redraw (tableView.Bounds);
  1374. expected =
  1375. @"
  1376. │A│B│Very Long Column │
  1377. ├─┼─┼───────────────────┤
  1378. │1│2│aaaaaaaaaaaaa... │
  1379. │1│2│aaa │
  1380. ";
  1381. TestHelpers.AssertDriverContentsAre (expected, output);
  1382. // revert style change
  1383. style.RepresentationGetter = null;
  1384. // Both of the above methods rely on having a fixed
  1385. // size limit for the column. These are awkward if a
  1386. // table is resizeable e.g. Dim.Fill(). Ideally we want
  1387. // to render in any space available and truncate the content
  1388. // of the column dynamically so it fills the free space at
  1389. // the end of the table.
  1390. // We can now specify that the column can be any length
  1391. // (Up to MaxWidth) but the renderer can accept using
  1392. // less space down to this limit
  1393. style.MinAcceptableWidth = 5;
  1394. tableView.Redraw (tableView.Bounds);
  1395. expected =
  1396. @"
  1397. │A│B│Very Long Column │
  1398. ├─┼─┼───────────────────┤
  1399. │1│2│aaaaaaaaaaaaaaaaaaa│
  1400. │1│2│aaa │
  1401. ";
  1402. TestHelpers.AssertDriverContentsAre (expected, output);
  1403. // Now test making the width too small for the MinAcceptableWidth
  1404. // the Column won't fit so should not be rendered
  1405. Application.Shutdown ();
  1406. GraphViewTests.InitFakeDriver ();
  1407. tableView.Bounds = new Rect(0,0,9,5);
  1408. tableView.Redraw (tableView.Bounds);
  1409. expected =
  1410. @"
  1411. │A│B │
  1412. ├─┼─────►
  1413. │1│2 │
  1414. │1│2 │
  1415. ";
  1416. TestHelpers.AssertDriverContentsAre (expected, output);
  1417. // setting width to 10 leaves just enough space for the column to
  1418. // meet MinAcceptableWidth of 5. Column width includes terminator line
  1419. // symbol (e.g. ┤ or │)
  1420. tableView.Bounds = new Rect (0, 0, 10, 5);
  1421. tableView.Redraw (tableView.Bounds);
  1422. expected =
  1423. @"
  1424. │A│B│Very│
  1425. ├─┼─┼────┤
  1426. │1│2│aaaa│
  1427. │1│2│aaa │
  1428. ";
  1429. TestHelpers.AssertDriverContentsAre (expected, output);
  1430. Application.Shutdown ();
  1431. }
  1432. [Fact]
  1433. public void ScrollIndicators ()
  1434. {
  1435. GraphViewTests.InitFakeDriver ();
  1436. var tableView = new TableView ();
  1437. tableView.ColorScheme = Colors.TopLevel;
  1438. // 3 columns are visibile
  1439. tableView.Bounds = new Rect (0, 0, 7, 5);
  1440. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1441. tableView.Style.ShowHorizontalHeaderOverline = false;
  1442. tableView.Style.AlwaysShowHeaders = true;
  1443. tableView.Style.SmoothHorizontalScrolling = true;
  1444. var dt = new DataTable ();
  1445. dt.Columns.Add ("A");
  1446. dt.Columns.Add ("B");
  1447. dt.Columns.Add ("C");
  1448. dt.Columns.Add ("D");
  1449. dt.Columns.Add ("E");
  1450. dt.Columns.Add ("F");
  1451. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1452. tableView.Table = dt;
  1453. // select last visible column
  1454. tableView.SelectedColumn = 2; // column C
  1455. tableView.Redraw (tableView.Bounds);
  1456. // user can only scroll right so sees right indicator
  1457. // Because first column in table is A
  1458. string expected =
  1459. @"
  1460. │A│B│C│
  1461. ├─┼─┼─►
  1462. │1│2│3│";
  1463. TestHelpers.AssertDriverContentsAre (expected, output);
  1464. // Scroll right
  1465. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1466. // since A is now pushed off screen we get indicator showing
  1467. // that user can scroll left to see first column
  1468. tableView.Redraw (tableView.Bounds);
  1469. expected =
  1470. @"
  1471. │B│C│D│
  1472. ◄─┼─┼─►
  1473. │2│3│4│";
  1474. TestHelpers.AssertDriverContentsAre (expected, output);
  1475. // Scroll right twice more (to end of columns)
  1476. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1477. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1478. tableView.Redraw (tableView.Bounds);
  1479. expected =
  1480. @"
  1481. │D│E│F│
  1482. ◄─┼─┼─┤
  1483. │4│5│6│";
  1484. TestHelpers.AssertDriverContentsAre (expected, output);
  1485. // Shutdown must be called to safely clean up Application if Init has been called
  1486. Application.Shutdown ();
  1487. }
  1488. /// <summary>
  1489. /// Builds a simple table of string columns with the requested number of columns and rows
  1490. /// </summary>
  1491. /// <param name="cols"></param>
  1492. /// <param name="rows"></param>
  1493. /// <returns></returns>
  1494. public static DataTable BuildTable (int cols, int rows)
  1495. {
  1496. var dt = new DataTable ();
  1497. for (int c = 0; c < cols; c++) {
  1498. dt.Columns.Add ("Col" + c);
  1499. }
  1500. for (int r = 0; r < rows; r++) {
  1501. var newRow = dt.NewRow ();
  1502. for (int c = 0; c < cols; c++) {
  1503. newRow [c] = $"R{r}C{c}";
  1504. }
  1505. dt.Rows.Add (newRow);
  1506. }
  1507. return dt;
  1508. }
  1509. [Fact, AutoInitShutdown]
  1510. public void Test_ScreenToCell ()
  1511. {
  1512. var tableView = GetTwoRowSixColumnTable ();
  1513. tableView.Redraw (tableView.Bounds);
  1514. // user can only scroll right so sees right indicator
  1515. // Because first column in table is A
  1516. string expected =
  1517. @"
  1518. │A│B│C│
  1519. ├─┼─┼─►
  1520. │1│2│3│
  1521. │1│2│3│";
  1522. TestHelpers.AssertDriverContentsAre (expected, output);
  1523. // ---------------- X=0 -----------------------
  1524. // click is before first cell
  1525. Assert.Null (tableView.ScreenToCell (0, 0));
  1526. Assert.Null (tableView.ScreenToCell (0, 1));
  1527. Assert.Null (tableView.ScreenToCell (0, 2));
  1528. Assert.Null (tableView.ScreenToCell (0, 3));
  1529. Assert.Null (tableView.ScreenToCell (0, 4));
  1530. // ---------------- X=1 -----------------------
  1531. // click in header
  1532. Assert.Null (tableView.ScreenToCell (1, 0));
  1533. // click in header row line
  1534. Assert.Null (tableView.ScreenToCell (1, 1));
  1535. // click in cell 0,0
  1536. Assert.Equal (new Point(0,0),tableView.ScreenToCell (1, 2));
  1537. // click in cell 0,1
  1538. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3));
  1539. // after last row
  1540. Assert.Null (tableView.ScreenToCell (1, 4));
  1541. // ---------------- X=2 -----------------------
  1542. // ( even though there is a horizontal dividing line here we treat it as a hit on the cell before)
  1543. // click in header
  1544. Assert.Null (tableView.ScreenToCell (2, 0));
  1545. // click in header row line
  1546. Assert.Null (tableView.ScreenToCell (2, 1));
  1547. // click in cell 0,0
  1548. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (2, 2));
  1549. // click in cell 0,1
  1550. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3));
  1551. // after last row
  1552. Assert.Null (tableView.ScreenToCell (2, 4));
  1553. // ---------------- X=3 -----------------------
  1554. // click in header
  1555. Assert.Null (tableView.ScreenToCell (3, 0));
  1556. // click in header row line
  1557. Assert.Null (tableView.ScreenToCell (3, 1));
  1558. // click in cell 1,0
  1559. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2));
  1560. // click in cell 1,1
  1561. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3));
  1562. // after last row
  1563. Assert.Null (tableView.ScreenToCell (3, 4));
  1564. }
  1565. [Fact, AutoInitShutdown]
  1566. public void Test_ScreenToCell_DataColumnOverload ()
  1567. {
  1568. var tableView = GetTwoRowSixColumnTable ();
  1569. tableView.Redraw (tableView.Bounds);
  1570. // user can only scroll right so sees right indicator
  1571. // Because first column in table is A
  1572. string expected =
  1573. @"
  1574. │A│B│C│
  1575. ├─┼─┼─►
  1576. │1│2│3│
  1577. │1│2│3│";
  1578. TestHelpers.AssertDriverContentsAre (expected, output);
  1579. DataColumn col;
  1580. // ---------------- X=0 -----------------------
  1581. // click is before first cell
  1582. Assert.Null (tableView.ScreenToCell (0, 0,out col));
  1583. Assert.Null (col);
  1584. Assert.Null (tableView.ScreenToCell (0, 1,out col));
  1585. Assert.Null (col);
  1586. Assert.Null (tableView.ScreenToCell (0, 2,out col));
  1587. Assert.Null (col);
  1588. Assert.Null (tableView.ScreenToCell (0, 3,out col));
  1589. Assert.Null (col);
  1590. Assert.Null (tableView.ScreenToCell (0, 4,out col));
  1591. Assert.Null (col);
  1592. // ---------------- X=1 -----------------------
  1593. // click in header
  1594. Assert.Null (tableView.ScreenToCell (1, 0, out col));
  1595. Assert.Equal ("A", col.ColumnName);
  1596. // click in header row line (click in the horizontal line below header counts as click in header above - consistent with the column hit box)
  1597. Assert.Null (tableView.ScreenToCell (1, 1, out col));
  1598. Assert.Equal ("A", col.ColumnName);
  1599. // click in cell 0,0
  1600. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (1, 2, out col));
  1601. Assert.Null (col);
  1602. // click in cell 0,1
  1603. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3, out col));
  1604. Assert.Null (col);
  1605. // after last row
  1606. Assert.Null (tableView.ScreenToCell (1, 4, out col));
  1607. Assert.Null (col);
  1608. // ---------------- X=2 -----------------------
  1609. // click in header
  1610. Assert.Null (tableView.ScreenToCell (2, 0, out col));
  1611. Assert.Equal ("A", col.ColumnName);
  1612. // click in header row line
  1613. Assert.Null (tableView.ScreenToCell (2, 1, out col));
  1614. Assert.Equal ("A", col.ColumnName);
  1615. // click in cell 0,0
  1616. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (2, 2, out col));
  1617. Assert.Null (col);
  1618. // click in cell 0,1
  1619. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3, out col));
  1620. Assert.Null (col);
  1621. // after last row
  1622. Assert.Null (tableView.ScreenToCell (2, 4, out col));
  1623. Assert.Null (col);
  1624. // ---------------- X=3 -----------------------
  1625. // click in header
  1626. Assert.Null (tableView.ScreenToCell (3, 0, out col));
  1627. Assert.Equal ("B", col.ColumnName);
  1628. // click in header row line
  1629. Assert.Null (tableView.ScreenToCell (3, 1, out col));
  1630. Assert.Equal ("B", col.ColumnName);
  1631. // click in cell 1,0
  1632. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2, out col));
  1633. Assert.Null (col);
  1634. // click in cell 1,1
  1635. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3, out col));
  1636. Assert.Null (col);
  1637. // after last row
  1638. Assert.Null (tableView.ScreenToCell (3, 4, out col));
  1639. Assert.Null (col);
  1640. }
  1641. private TableView GetTwoRowSixColumnTable ()
  1642. {
  1643. var tableView = new TableView ();
  1644. tableView.ColorScheme = Colors.TopLevel;
  1645. // 3 columns are visible
  1646. tableView.Bounds = new Rect (0, 0, 7, 5);
  1647. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1648. tableView.Style.ShowHorizontalHeaderOverline = false;
  1649. tableView.Style.AlwaysShowHeaders = true;
  1650. tableView.Style.SmoothHorizontalScrolling = true;
  1651. var dt = new DataTable ();
  1652. dt.Columns.Add ("A");
  1653. dt.Columns.Add ("B");
  1654. dt.Columns.Add ("C");
  1655. dt.Columns.Add ("D");
  1656. dt.Columns.Add ("E");
  1657. dt.Columns.Add ("F");
  1658. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1659. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1660. tableView.Table = dt;
  1661. return tableView;
  1662. }
  1663. }
  1664. }