TableViewTests.cs 64 KB

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