TableViewTests.cs 62 KB

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