TableViewTests.cs 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239
  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, AutoInitShutdown]
  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, AutoInitShutdown]
  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, AutoInitShutdown]
  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, AutoInitShutdown]
  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, AutoInitShutdown]
  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, AutoInitShutdown]
  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, AutoInitShutdown]
  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, AutoInitShutdown]
  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, AutoInitShutdown]
  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, AutoInitShutdown]
  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, AutoInitShutdown]
  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. tv.ColorScheme = Colors.Base;
  794. return tv;
  795. }
  796. [Fact]
  797. [AutoInitShutdown]
  798. public void ScrollDown_OneLineAtATime ()
  799. {
  800. var tableView = new TableView ();
  801. tableView.BeginInit (); tableView.EndInit ();
  802. // Set big table
  803. tableView.Table = BuildTable (25, 50);
  804. // 1 header + 4 rows visible
  805. tableView.Bounds = new Rect (0, 0, 25, 5);
  806. tableView.Style.ShowHorizontalHeaderUnderline = false;
  807. tableView.Style.ShowHorizontalHeaderOverline = false;
  808. tableView.Style.AlwaysShowHeaders = true;
  809. // select last row
  810. tableView.SelectedRow = 3; // row is 0 indexed so this is the 4th visible row
  811. // Scroll down
  812. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorDown });
  813. // Scrolled off the page by 1 row so it should only have moved down 1 line of RowOffset
  814. Assert.Equal (4, tableView.SelectedRow);
  815. Assert.Equal (1, tableView.RowOffset);
  816. }
  817. [Fact, AutoInitShutdown]
  818. public void ScrollRight_SmoothScrolling ()
  819. {
  820. var tableView = new TableView ();
  821. tableView.BeginInit (); tableView.EndInit ();
  822. tableView.ColorScheme = Colors.TopLevel;
  823. tableView.LayoutSubviews ();
  824. // 3 columns are visibile
  825. tableView.Bounds = new Rect (0, 0, 7, 5);
  826. tableView.Style.ShowHorizontalHeaderUnderline = false;
  827. tableView.Style.ShowHorizontalHeaderOverline = false;
  828. tableView.Style.AlwaysShowHeaders = true;
  829. tableView.Style.SmoothHorizontalScrolling = true;
  830. var dt = new DataTable ();
  831. dt.Columns.Add ("A");
  832. dt.Columns.Add ("B");
  833. dt.Columns.Add ("C");
  834. dt.Columns.Add ("D");
  835. dt.Columns.Add ("E");
  836. dt.Columns.Add ("F");
  837. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  838. tableView.Table = dt;
  839. // select last visible column
  840. tableView.SelectedColumn = 2; // column C
  841. tableView.Redraw (tableView.Bounds);
  842. string expected =
  843. @"
  844. โ”‚Aโ”‚Bโ”‚Cโ”‚
  845. โ”‚1โ”‚2โ”‚3โ”‚";
  846. TestHelpers.AssertDriverContentsAre (expected, output);
  847. // Scroll right
  848. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  849. tableView.Redraw (tableView.Bounds);
  850. // Note that with SmoothHorizontalScrolling only a single new column
  851. // is exposed when scrolling right. This is not always the case though
  852. // sometimes if the leftmost column is long (i.e. A is a long column)
  853. // then when A is pushed off the screen multiple new columns could be exposed
  854. // (not just D but also E and F). This is because TableView never shows
  855. // 'half cells' or scrolls by console unit (scrolling is done by table row/column increments).
  856. expected =
  857. @"
  858. โ”‚Bโ”‚Cโ”‚Dโ”‚
  859. โ”‚2โ”‚3โ”‚4โ”‚";
  860. TestHelpers.AssertDriverContentsAre (expected, output);
  861. // Shutdown must be called to safely clean up Application if Init has been called
  862. Application.Shutdown ();
  863. }
  864. [Fact, AutoInitShutdown]
  865. public void ScrollRight_WithoutSmoothScrolling ()
  866. {
  867. var tableView = new TableView ();
  868. tableView.BeginInit (); tableView.EndInit ();
  869. tableView.ColorScheme = Colors.TopLevel;
  870. // 3 columns are visibile
  871. tableView.Bounds = new Rect (0, 0, 7, 5);
  872. tableView.Style.ShowHorizontalHeaderUnderline = false;
  873. tableView.Style.ShowHorizontalHeaderOverline = false;
  874. tableView.Style.AlwaysShowHeaders = true;
  875. tableView.Style.SmoothHorizontalScrolling = false;
  876. var dt = new DataTable ();
  877. dt.Columns.Add ("A");
  878. dt.Columns.Add ("B");
  879. dt.Columns.Add ("C");
  880. dt.Columns.Add ("D");
  881. dt.Columns.Add ("E");
  882. dt.Columns.Add ("F");
  883. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  884. tableView.Table = dt;
  885. // select last visible column
  886. tableView.SelectedColumn = 2; // column C
  887. tableView.Redraw (tableView.Bounds);
  888. string expected =
  889. @"
  890. โ”‚Aโ”‚Bโ”‚Cโ”‚
  891. โ”‚1โ”‚2โ”‚3โ”‚";
  892. TestHelpers.AssertDriverContentsAre (expected, output);
  893. // Scroll right
  894. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  895. tableView.Redraw (tableView.Bounds);
  896. // notice that without smooth scrolling we just update the first column
  897. // rendered in the table to the newly exposed column (D). This is fast
  898. // since we don't have to worry about repeatedly measuring the content
  899. // area as we scroll until the new column (D) is exposed. But it makes
  900. // the view 'jump' to expose all new columns
  901. expected =
  902. @"
  903. โ”‚Dโ”‚Eโ”‚Fโ”‚
  904. โ”‚4โ”‚5โ”‚6โ”‚";
  905. TestHelpers.AssertDriverContentsAre (expected, output);
  906. // Shutdown must be called to safely clean up Application if Init has been called
  907. Application.Shutdown ();
  908. }
  909. private TableView GetABCDEFTableView (out DataTable dt)
  910. {
  911. var tableView = new TableView ();
  912. tableView.BeginInit (); tableView.EndInit ();
  913. tableView.ColorScheme = Colors.TopLevel;
  914. // 3 columns are visible
  915. tableView.Bounds = new Rect (0, 0, 7, 5);
  916. tableView.Style.ShowHorizontalHeaderUnderline = false;
  917. tableView.Style.ShowHorizontalHeaderOverline = false;
  918. tableView.Style.AlwaysShowHeaders = true;
  919. tableView.Style.SmoothHorizontalScrolling = false;
  920. dt = new DataTable ();
  921. dt.Columns.Add ("A");
  922. dt.Columns.Add ("B");
  923. dt.Columns.Add ("C");
  924. dt.Columns.Add ("D");
  925. dt.Columns.Add ("E");
  926. dt.Columns.Add ("F");
  927. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  928. tableView.Table = dt;
  929. return tableView;
  930. }
  931. [Fact, AutoInitShutdown]
  932. public void TestColumnStyle_VisibleFalse_IsNotRendered ()
  933. {
  934. var tableView = GetABCDEFTableView (out DataTable dt);
  935. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  936. tableView.LayoutSubviews ();
  937. tableView.Redraw (tableView.Bounds);
  938. string expected =
  939. @"
  940. โ”‚Aโ”‚Cโ”‚Dโ”‚
  941. โ”‚1โ”‚3โ”‚4โ”‚";
  942. TestHelpers.AssertDriverContentsAre (expected, output);
  943. }
  944. [Fact, AutoInitShutdown]
  945. public void TestColumnStyle_FirstColumnVisibleFalse_IsNotRendered ()
  946. {
  947. var tableView = GetABCDEFTableView (out DataTable dt);
  948. tableView.Style.ShowHorizontalScrollIndicators = true;
  949. tableView.Style.ShowHorizontalHeaderUnderline = true;
  950. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  951. tableView.LayoutSubviews ();
  952. tableView.Redraw (tableView.Bounds);
  953. string expected =
  954. @"
  955. โ”‚Bโ”‚Cโ”‚Dโ”‚
  956. โ”œโ”€โ”ผโ”€โ”ผโ”€โ–บ
  957. โ”‚2โ”‚3โ”‚4โ”‚";
  958. TestHelpers.AssertDriverContentsAre (expected, output);
  959. }
  960. [Fact, AutoInitShutdown]
  961. public void TestColumnStyle_AllColumnsVisibleFalse_BehavesAsTableNull ()
  962. {
  963. var tableView = GetABCDEFTableView (out DataTable dt);
  964. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  965. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  966. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["C"]).Visible = false;
  967. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  968. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  969. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  970. tableView.LayoutSubviews ();
  971. // expect nothing to be rendered when all columns are invisible
  972. string expected =
  973. @"
  974. ";
  975. tableView.Redraw (tableView.Bounds);
  976. TestHelpers.AssertDriverContentsAre (expected, output);
  977. // expect behavior to match when Table is null
  978. tableView.Table = null;
  979. tableView.Redraw (tableView.Bounds);
  980. TestHelpers.AssertDriverContentsAre (expected, output);
  981. }
  982. [Fact, AutoInitShutdown]
  983. public void TestColumnStyle_RemainingColumnsInvisible_NoScrollIndicator ()
  984. {
  985. var tableView = GetABCDEFTableView (out DataTable dt);
  986. tableView.Style.ShowHorizontalScrollIndicators = true;
  987. tableView.Style.ShowHorizontalHeaderUnderline = true;
  988. tableView.LayoutSubviews ();
  989. tableView.Redraw (tableView.Bounds);
  990. // normally we should have scroll indicators because DEF are of screen
  991. string expected =
  992. @"
  993. โ”‚Aโ”‚Bโ”‚Cโ”‚
  994. โ”œโ”€โ”ผโ”€โ”ผโ”€โ–บ
  995. โ”‚1โ”‚2โ”‚3โ”‚";
  996. TestHelpers.AssertDriverContentsAre (expected, output);
  997. // but if DEF are invisible we shouldn't be showing the indicator
  998. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  999. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  1000. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  1001. expected =
  1002. @"
  1003. โ”‚Aโ”‚Bโ”‚Cโ”‚
  1004. โ”œโ”€โ”ผโ”€โ”ผโ”€โ”ค
  1005. โ”‚1โ”‚2โ”‚3โ”‚";
  1006. tableView.Redraw (tableView.Bounds);
  1007. TestHelpers.AssertDriverContentsAre (expected, output);
  1008. }
  1009. [Fact, AutoInitShutdown]
  1010. public void TestColumnStyle_PreceedingColumnsInvisible_NoScrollIndicator ()
  1011. {
  1012. var tableView = GetABCDEFTableView (out DataTable dt);
  1013. tableView.Style.ShowHorizontalScrollIndicators = true;
  1014. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1015. tableView.ColumnOffset = 1;
  1016. tableView.LayoutSubviews ();
  1017. tableView.Redraw (tableView.Bounds);
  1018. // normally we should have scroll indicators because A,E and F are of screen
  1019. string expected =
  1020. @"
  1021. โ”‚Bโ”‚Cโ”‚Dโ”‚
  1022. โ—„โ”€โ”ผโ”€โ”ผโ”€โ–บ
  1023. โ”‚2โ”‚3โ”‚4โ”‚";
  1024. TestHelpers.AssertDriverContentsAre (expected, output);
  1025. // but if E and F are invisible so we shouldn't show right
  1026. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  1027. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  1028. expected =
  1029. @"
  1030. โ”‚Bโ”‚Cโ”‚Dโ”‚
  1031. โ—„โ”€โ”ผโ”€โ”ผโ”€โ”ค
  1032. โ”‚2โ”‚3โ”‚4โ”‚";
  1033. tableView.Redraw (tableView.Bounds);
  1034. TestHelpers.AssertDriverContentsAre (expected, output);
  1035. // now also A is invisible so we cannot scroll in either direction
  1036. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  1037. expected =
  1038. @"
  1039. โ”‚Bโ”‚Cโ”‚Dโ”‚
  1040. โ”œโ”€โ”ผโ”€โ”ผโ”€โ”ค
  1041. โ”‚2โ”‚3โ”‚4โ”‚";
  1042. tableView.Redraw (tableView.Bounds);
  1043. TestHelpers.AssertDriverContentsAre (expected, output);
  1044. }
  1045. [Fact, AutoInitShutdown]
  1046. public void TestColumnStyle_VisibleFalse_CursorStepsOverInvisibleColumns ()
  1047. {
  1048. var tableView = GetABCDEFTableView (out var dt);
  1049. tableView.LayoutSubviews ();
  1050. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  1051. tableView.SelectedColumn = 0;
  1052. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1053. // Expect the cursor navigation to skip over the invisible column(s)
  1054. Assert.Equal (2, tableView.SelectedColumn);
  1055. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1056. // Expect the cursor navigation backwards to skip over invisible column too
  1057. Assert.Equal (0, tableView.SelectedColumn);
  1058. }
  1059. [InlineData (true)]
  1060. [InlineData (false)]
  1061. [Theory, AutoInitShutdown]
  1062. public void TestColumnStyle_FirstColumnVisibleFalse_CursorStaysAt1 (bool useHome)
  1063. {
  1064. var tableView = GetABCDEFTableView (out var dt);
  1065. tableView.LayoutSubviews ();
  1066. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  1067. tableView.SelectedColumn = 0;
  1068. Assert.Equal (0, tableView.SelectedColumn);
  1069. // column 0 is invisible so this method should move to 1
  1070. tableView.EnsureValidSelection ();
  1071. Assert.Equal (1, tableView.SelectedColumn);
  1072. tableView.ProcessKey (new KeyEvent {
  1073. Key = useHome ? Key.Home : Key.CursorLeft
  1074. });
  1075. // Expect the cursor to stay at 1
  1076. Assert.Equal (1, tableView.SelectedColumn);
  1077. }
  1078. [InlineData (true)]
  1079. [InlineData (false)]
  1080. [Theory, AutoInitShutdown]
  1081. public void TestMoveStartEnd_WithFullRowSelect (bool withFullRowSelect)
  1082. {
  1083. var tableView = GetTwoRowSixColumnTable ();
  1084. tableView.LayoutSubviews ();
  1085. tableView.FullRowSelect = withFullRowSelect;
  1086. tableView.SelectedRow = 1;
  1087. tableView.SelectedColumn = 1;
  1088. tableView.ProcessKey (new KeyEvent {
  1089. Key = Key.Home | Key.CtrlMask
  1090. });
  1091. if (withFullRowSelect) {
  1092. // Should not be any horizontal movement when
  1093. // using navigate to Start/End and FullRowSelect
  1094. Assert.Equal (1, tableView.SelectedColumn);
  1095. Assert.Equal (0, tableView.SelectedRow);
  1096. } else {
  1097. Assert.Equal (0, tableView.SelectedColumn);
  1098. Assert.Equal (0, tableView.SelectedRow);
  1099. }
  1100. tableView.ProcessKey (new KeyEvent {
  1101. Key = Key.End | Key.CtrlMask
  1102. });
  1103. if (withFullRowSelect) {
  1104. Assert.Equal (1, tableView.SelectedColumn);
  1105. Assert.Equal (1, tableView.SelectedRow);
  1106. } else {
  1107. Assert.Equal (5, tableView.SelectedColumn);
  1108. Assert.Equal (1, tableView.SelectedRow);
  1109. }
  1110. }
  1111. [InlineData (true)]
  1112. [InlineData (false)]
  1113. [Theory, AutoInitShutdown]
  1114. public void TestColumnStyle_LastColumnVisibleFalse_CursorStaysAt2 (bool useEnd)
  1115. {
  1116. var tableView = GetABCDEFTableView (out var dt);
  1117. tableView.LayoutSubviews ();
  1118. // select D
  1119. tableView.SelectedColumn = 3;
  1120. Assert.Equal (3, tableView.SelectedColumn);
  1121. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  1122. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  1123. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  1124. // column D is invisible so this method should move to 2 (C)
  1125. tableView.EnsureValidSelection ();
  1126. Assert.Equal (2, tableView.SelectedColumn);
  1127. tableView.ProcessKey (new KeyEvent {
  1128. Key = useEnd ? Key.End : Key.CursorRight
  1129. });
  1130. // Expect the cursor to stay at 2
  1131. Assert.Equal (2, tableView.SelectedColumn);
  1132. }
  1133. [Fact, AutoInitShutdown]
  1134. public void TestColumnStyle_VisibleFalse_MultiSelected ()
  1135. {
  1136. var tableView = GetABCDEFTableView (out var dt);
  1137. tableView.LayoutSubviews ();
  1138. // user has rectangular selection
  1139. tableView.MultiSelectedRegions.Push (
  1140. new TableView.TableSelection (
  1141. new Point (0, 0),
  1142. new Rect (0, 0, 3, 1))
  1143. );
  1144. Assert.Equal (3, tableView.GetAllSelectedCells ().Count ());
  1145. Assert.True (tableView.IsSelected (0, 0));
  1146. Assert.True (tableView.IsSelected (1, 0));
  1147. Assert.True (tableView.IsSelected (2, 0));
  1148. Assert.False (tableView.IsSelected (3, 0));
  1149. // if middle column is invisible
  1150. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  1151. // it should not be included in the selection
  1152. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1153. Assert.True (tableView.IsSelected (0, 0));
  1154. Assert.False (tableView.IsSelected (1, 0));
  1155. Assert.True (tableView.IsSelected (2, 0));
  1156. Assert.False (tableView.IsSelected (3, 0));
  1157. Assert.DoesNotContain (new Point (1, 0), tableView.GetAllSelectedCells ());
  1158. }
  1159. [Fact, AutoInitShutdown]
  1160. public void TestColumnStyle_VisibleFalse_MultiSelectingStepsOverInvisibleColumns ()
  1161. {
  1162. var tableView = GetABCDEFTableView (out var dt);
  1163. tableView.LayoutSubviews ();
  1164. // if middle column is invisible
  1165. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  1166. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight | Key.ShiftMask });
  1167. // Selection should extend from A to C but skip B
  1168. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1169. Assert.True (tableView.IsSelected (0, 0));
  1170. Assert.False (tableView.IsSelected (1, 0));
  1171. Assert.True (tableView.IsSelected (2, 0));
  1172. Assert.False (tableView.IsSelected (3, 0));
  1173. Assert.DoesNotContain (new Point (1, 0), tableView.GetAllSelectedCells ());
  1174. }
  1175. [Fact, AutoInitShutdown]
  1176. public void TestToggleCells_MultiSelectOn ()
  1177. {
  1178. // 2 row table
  1179. var tableView = GetABCDEFTableView (out var dt);
  1180. tableView.LayoutSubviews ();
  1181. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1182. tableView.MultiSelect = true;
  1183. tableView.AddKeyBinding (Key.Space, Command.ToggleChecked);
  1184. var selectedCell = tableView.GetAllSelectedCells ().Single ();
  1185. Assert.Equal (0, selectedCell.X);
  1186. Assert.Equal (0, selectedCell.Y);
  1187. // Go Right
  1188. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1189. selectedCell = tableView.GetAllSelectedCells ().Single ();
  1190. Assert.Equal (1, selectedCell.X);
  1191. Assert.Equal (0, selectedCell.Y);
  1192. // Toggle Select
  1193. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1194. var m = tableView.MultiSelectedRegions.Single ();
  1195. Assert.True (m.IsToggled);
  1196. Assert.Equal (1, m.Origin.X);
  1197. Assert.Equal (0, m.Origin.Y);
  1198. selectedCell = tableView.GetAllSelectedCells ().Single ();
  1199. Assert.Equal (1, selectedCell.X);
  1200. Assert.Equal (0, selectedCell.Y);
  1201. // Go Left
  1202. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1203. // Both Toggled and Moved to should be selected
  1204. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1205. var s1 = tableView.GetAllSelectedCells ().ElementAt (0);
  1206. var s2 = tableView.GetAllSelectedCells ().ElementAt (1);
  1207. Assert.Equal (1, s1.X);
  1208. Assert.Equal (0, s1.Y);
  1209. Assert.Equal (0, s2.X);
  1210. Assert.Equal (0, s2.Y);
  1211. // Go Down
  1212. tableView.ProcessKey (new KeyEvent { Key = Key.CursorDown });
  1213. // Both Toggled and Moved to should be selected but not 0,0
  1214. // which we moved down from
  1215. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1216. s1 = tableView.GetAllSelectedCells ().ElementAt (0);
  1217. s2 = tableView.GetAllSelectedCells ().ElementAt (1);
  1218. Assert.Equal (1, s1.X);
  1219. Assert.Equal (0, s1.Y);
  1220. Assert.Equal (0, s2.X);
  1221. Assert.Equal (1, s2.Y);
  1222. // Go back to the toggled cell
  1223. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1224. tableView.ProcessKey (new KeyEvent { Key = Key.CursorUp });
  1225. // Toggle off
  1226. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1227. // Go Left
  1228. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1229. selectedCell = tableView.GetAllSelectedCells ().Single ();
  1230. Assert.Equal (0, selectedCell.X);
  1231. Assert.Equal (0, selectedCell.Y);
  1232. }
  1233. [Fact, AutoInitShutdown]
  1234. public void TestToggleCells_MultiSelectOn_FullRowSelect ()
  1235. {
  1236. // 2 row table
  1237. var tableView = GetABCDEFTableView (out var dt);
  1238. tableView.LayoutSubviews ();
  1239. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1240. tableView.FullRowSelect = true;
  1241. tableView.MultiSelect = true;
  1242. tableView.AddKeyBinding (Key.Space, Command.ToggleChecked);
  1243. // Toggle Select Cell 0,0
  1244. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1245. // Go Down
  1246. tableView.ProcessKey (new KeyEvent { Key = Key.CursorDown });
  1247. var m = tableView.MultiSelectedRegions.Single ();
  1248. Assert.True (m.IsToggled);
  1249. Assert.Equal (0, m.Origin.X);
  1250. Assert.Equal (0, m.Origin.Y);
  1251. //First row toggled and Second row active = 12 selected cells
  1252. Assert.Equal (12, tableView.GetAllSelectedCells ().Count ());
  1253. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1254. tableView.ProcessKey (new KeyEvent { Key = Key.CursorUp });
  1255. Assert.Single (tableView.MultiSelectedRegions.Where (r => r.IsToggled));
  1256. // Can untoggle at 1,0 even though 0,0 was initial toggle because FullRowSelect is on
  1257. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1258. Assert.Empty (tableView.MultiSelectedRegions.Where (r => r.IsToggled));
  1259. }
  1260. [Fact, AutoInitShutdown]
  1261. public void TestToggleCells_MultiSelectOn_SquareSelectToggled ()
  1262. {
  1263. // 3 row table
  1264. var tableView = GetABCDEFTableView (out var dt);
  1265. tableView.LayoutSubviews ();
  1266. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1267. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1268. tableView.MultiSelect = true;
  1269. tableView.AddKeyBinding (Key.Space, Command.ToggleChecked);
  1270. // Make a square selection
  1271. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorDown });
  1272. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorRight });
  1273. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  1274. // Toggle the square selected region on
  1275. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1276. // Go Right
  1277. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1278. //Toggled on square + the active cell (x=2,y=1)
  1279. Assert.Equal (5, tableView.GetAllSelectedCells ().Count ());
  1280. Assert.Equal (2, tableView.SelectedColumn);
  1281. Assert.Equal (1, tableView.SelectedRow);
  1282. // Untoggle the rectangular region by hitting toggle in
  1283. // any cell in that rect
  1284. tableView.ProcessKey (new KeyEvent { Key = Key.CursorUp });
  1285. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1286. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  1287. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1288. Assert.Single (tableView.GetAllSelectedCells ());
  1289. }
  1290. [Fact, AutoInitShutdown]
  1291. public void TestToggleCells_MultiSelectOn_Two_SquareSelects_BothToggled ()
  1292. {
  1293. // 6 row table
  1294. var tableView = GetABCDEFTableView (out var dt);
  1295. tableView.LayoutSubviews ();
  1296. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1297. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1298. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1299. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1300. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1301. tableView.MultiSelect = true;
  1302. tableView.AddKeyBinding (Key.Space, Command.ToggleChecked);
  1303. // Make first square selection (0,0 to 1,1)
  1304. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorDown });
  1305. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorRight });
  1306. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1307. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  1308. // Make second square selection leaving 1 unselected line between them
  1309. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1310. tableView.ProcessKey (new KeyEvent { Key = Key.CursorDown });
  1311. tableView.ProcessKey (new KeyEvent { Key = Key.CursorDown });
  1312. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorDown });
  1313. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorRight });
  1314. // 2 square selections
  1315. Assert.Equal (8, tableView.GetAllSelectedCells ().Count ());
  1316. }
  1317. [Theory, AutoInitShutdown]
  1318. [InlineData (new object [] { true, true })]
  1319. [InlineData (new object [] { false, true })]
  1320. [InlineData (new object [] { true, false })]
  1321. [InlineData (new object [] { false, false })]
  1322. public void TestColumnStyle_VisibleFalse_DoesNotEffect_EnsureSelectedCellIsVisible (bool smooth, bool invisibleCol)
  1323. {
  1324. var tableView = GetABCDEFTableView (out var dt);
  1325. tableView.LayoutSubviews ();
  1326. tableView.Style.SmoothHorizontalScrolling = smooth;
  1327. if (invisibleCol) {
  1328. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  1329. }
  1330. // New TableView should have first cell selected
  1331. Assert.Equal (0, tableView.SelectedColumn);
  1332. // With no scrolling
  1333. Assert.Equal (0, tableView.ColumnOffset);
  1334. // A,B and C are visible on screen at the moment so these should have no effect
  1335. tableView.SelectedColumn = 1;
  1336. tableView.EnsureSelectedCellIsVisible ();
  1337. Assert.Equal (0, tableView.ColumnOffset);
  1338. tableView.SelectedColumn = 2;
  1339. tableView.EnsureSelectedCellIsVisible ();
  1340. Assert.Equal (0, tableView.ColumnOffset);
  1341. // Selecting D should move the visible table area to fit D onto the screen
  1342. tableView.SelectedColumn = 3;
  1343. tableView.EnsureSelectedCellIsVisible ();
  1344. Assert.Equal (smooth ? 1 : 3, tableView.ColumnOffset);
  1345. }
  1346. [Fact, AutoInitShutdown]
  1347. public void LongColumnTest ()
  1348. {
  1349. var tableView = new TableView ();
  1350. tableView.BeginInit (); tableView.EndInit ();
  1351. tableView.ColorScheme = Colors.TopLevel;
  1352. // 25 characters can be printed into table
  1353. tableView.Bounds = new Rect (0, 0, 25, 5);
  1354. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1355. tableView.Style.ShowHorizontalHeaderOverline = false;
  1356. tableView.Style.AlwaysShowHeaders = true;
  1357. tableView.Style.SmoothHorizontalScrolling = true;
  1358. var dt = new DataTable ();
  1359. dt.Columns.Add ("A");
  1360. dt.Columns.Add ("B");
  1361. dt.Columns.Add ("Very Long Column");
  1362. dt.Rows.Add (1, 2, new string ('a', 500));
  1363. dt.Rows.Add (1, 2, "aaa");
  1364. tableView.Table = dt;
  1365. tableView.LayoutSubviews ();
  1366. tableView.Redraw (tableView.Bounds);
  1367. // default behaviour of TableView is not to render
  1368. // columns unless there is sufficient space
  1369. string expected =
  1370. @"
  1371. โ”‚Aโ”‚B โ”‚
  1372. โ”œโ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ
  1373. โ”‚1โ”‚2 โ”‚
  1374. โ”‚1โ”‚2 โ”‚
  1375. ";
  1376. TestHelpers.AssertDriverContentsAre (expected, output);
  1377. // get a style for the long column
  1378. var style = tableView.Style.GetOrCreateColumnStyle (dt.Columns [2]);
  1379. // one way the API user can fix this for long columns
  1380. // is to specify a max width for the column
  1381. style.MaxWidth = 10;
  1382. tableView.LayoutSubviews ();
  1383. tableView.Redraw (tableView.Bounds);
  1384. expected =
  1385. @"
  1386. โ”‚Aโ”‚Bโ”‚Very Long Column โ”‚
  1387. โ”œโ”€โ”ผโ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  1388. โ”‚1โ”‚2โ”‚aaaaaaaaaaaaaaaaaaaโ”‚
  1389. โ”‚1โ”‚2โ”‚aaa โ”‚
  1390. ";
  1391. TestHelpers.AssertDriverContentsAre (expected, output);
  1392. // revert the style change
  1393. style.MaxWidth = TableView.DefaultMaxCellWidth;
  1394. // another way API user can fix problem is to implement
  1395. // RepresentationGetter and apply max length there
  1396. style.RepresentationGetter = (s) => {
  1397. return s.ToString ().Length < 15 ? s.ToString () : s.ToString ().Substring (0, 13) + "...";
  1398. };
  1399. tableView.LayoutSubviews ();
  1400. tableView.Redraw (tableView.Bounds);
  1401. expected =
  1402. @"
  1403. โ”‚Aโ”‚Bโ”‚Very Long Column โ”‚
  1404. โ”œโ”€โ”ผโ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  1405. โ”‚1โ”‚2โ”‚aaaaaaaaaaaaa... โ”‚
  1406. โ”‚1โ”‚2โ”‚aaa โ”‚
  1407. ";
  1408. TestHelpers.AssertDriverContentsAre (expected, output);
  1409. // revert style change
  1410. style.RepresentationGetter = null;
  1411. // Both of the above methods rely on having a fixed
  1412. // size limit for the column. These are awkward if a
  1413. // table is resizeable e.g. Dim.Fill(). Ideally we want
  1414. // to render in any space available and truncate the content
  1415. // of the column dynamically so it fills the free space at
  1416. // the end of the table.
  1417. // We can now specify that the column can be any length
  1418. // (Up to MaxWidth) but the renderer can accept using
  1419. // less space down to this limit
  1420. style.MinAcceptableWidth = 5;
  1421. tableView.LayoutSubviews ();
  1422. tableView.Redraw (tableView.Bounds);
  1423. expected =
  1424. @"
  1425. โ”‚Aโ”‚Bโ”‚Very Long Column โ”‚
  1426. โ”œโ”€โ”ผโ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  1427. โ”‚1โ”‚2โ”‚aaaaaaaaaaaaaaaaaaaโ”‚
  1428. โ”‚1โ”‚2โ”‚aaa โ”‚
  1429. ";
  1430. TestHelpers.AssertDriverContentsAre (expected, output);
  1431. // Now test making the width too small for the MinAcceptableWidth
  1432. // the Column won't fit so should not be rendered
  1433. Application.Shutdown ();
  1434. tableView.Bounds = new Rect (0, 0, 9, 5);
  1435. tableView.LayoutSubviews ();
  1436. tableView.Redraw (tableView.Bounds);
  1437. expected =
  1438. @"
  1439. โ”‚Aโ”‚B โ”‚
  1440. โ”œโ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ–บ
  1441. โ”‚1โ”‚2 โ”‚
  1442. โ”‚1โ”‚2 โ”‚
  1443. ";
  1444. TestHelpers.AssertDriverContentsAre (expected, output);
  1445. // setting width to 10 leaves just enough space for the column to
  1446. // meet MinAcceptableWidth of 5. Column width includes terminator line
  1447. // symbol (e.g. โ”ค or โ”‚)
  1448. tableView.Bounds = new Rect (0, 0, 10, 5);
  1449. tableView.LayoutSubviews ();
  1450. tableView.Redraw (tableView.Bounds);
  1451. expected =
  1452. @"
  1453. โ”‚Aโ”‚Bโ”‚Veryโ”‚
  1454. โ”œโ”€โ”ผโ”€โ”ผโ”€โ”€โ”€โ”€โ”ค
  1455. โ”‚1โ”‚2โ”‚aaaaโ”‚
  1456. โ”‚1โ”‚2โ”‚aaa โ”‚
  1457. ";
  1458. TestHelpers.AssertDriverContentsAre (expected, output);
  1459. Application.Shutdown ();
  1460. }
  1461. [Fact, AutoInitShutdown]
  1462. public void ScrollIndicators ()
  1463. {
  1464. var tableView = new TableView ();
  1465. tableView.BeginInit (); tableView.EndInit ();
  1466. tableView.ColorScheme = Colors.TopLevel;
  1467. // 3 columns are visibile
  1468. tableView.Bounds = new Rect (0, 0, 7, 5);
  1469. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1470. tableView.Style.ShowHorizontalHeaderOverline = false;
  1471. tableView.Style.AlwaysShowHeaders = true;
  1472. tableView.Style.SmoothHorizontalScrolling = true;
  1473. var dt = new DataTable ();
  1474. dt.Columns.Add ("A");
  1475. dt.Columns.Add ("B");
  1476. dt.Columns.Add ("C");
  1477. dt.Columns.Add ("D");
  1478. dt.Columns.Add ("E");
  1479. dt.Columns.Add ("F");
  1480. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1481. tableView.Table = dt;
  1482. // select last visible column
  1483. tableView.SelectedColumn = 2; // column C
  1484. tableView.Redraw (tableView.Bounds);
  1485. // user can only scroll right so sees right indicator
  1486. // Because first column in table is A
  1487. string expected =
  1488. @"
  1489. โ”‚Aโ”‚Bโ”‚Cโ”‚
  1490. โ”œโ”€โ”ผโ”€โ”ผโ”€โ–บ
  1491. โ”‚1โ”‚2โ”‚3โ”‚";
  1492. TestHelpers.AssertDriverContentsAre (expected, output);
  1493. // Scroll right
  1494. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1495. // since A is now pushed off screen we get indicator showing
  1496. // that user can scroll left to see first column
  1497. tableView.Redraw (tableView.Bounds);
  1498. expected =
  1499. @"
  1500. โ”‚Bโ”‚Cโ”‚Dโ”‚
  1501. โ—„โ”€โ”ผโ”€โ”ผโ”€โ–บ
  1502. โ”‚2โ”‚3โ”‚4โ”‚";
  1503. TestHelpers.AssertDriverContentsAre (expected, output);
  1504. // Scroll right twice more (to end of columns)
  1505. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1506. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1507. tableView.Redraw (tableView.Bounds);
  1508. expected =
  1509. @"
  1510. โ”‚Dโ”‚Eโ”‚Fโ”‚
  1511. โ—„โ”€โ”ผโ”€โ”ผโ”€โ”ค
  1512. โ”‚4โ”‚5โ”‚6โ”‚";
  1513. TestHelpers.AssertDriverContentsAre (expected, output);
  1514. // Shutdown must be called to safely clean up Application if Init has been called
  1515. Application.Shutdown ();
  1516. }
  1517. [Fact, AutoInitShutdown]
  1518. public void CellEventsBackgroundFill ()
  1519. {
  1520. var tv = new TableView () {
  1521. Width = 20,
  1522. Height = 4
  1523. };
  1524. var dt = new DataTable ();
  1525. dt.Columns.Add ("C1");
  1526. dt.Columns.Add ("C2");
  1527. dt.Columns.Add ("C3");
  1528. dt.Rows.Add ("Hello", DBNull.Value, "f");
  1529. tv.Table = dt;
  1530. tv.NullSymbol = string.Empty;
  1531. Application.Top.Add (tv);
  1532. Application.Begin (Application.Top);
  1533. tv.Redraw (tv.Bounds);
  1534. var expected =
  1535. @"
  1536. โ”Œโ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  1537. โ”‚C1 โ”‚C2โ”‚C3 โ”‚
  1538. โ”œโ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  1539. โ”‚Helloโ”‚ โ”‚f โ”‚
  1540. ";
  1541. TestHelpers.AssertDriverContentsAre (expected, output);
  1542. var color = new Attribute (Color.Magenta, Color.BrightBlue);
  1543. var scheme = new ColorScheme {
  1544. Normal = color,
  1545. HotFocus = color,
  1546. Focus = color,
  1547. Disabled = color,
  1548. HotNormal = color,
  1549. };
  1550. // Now the thing we really want to test is the styles!
  1551. // All cells in the column have a column style that says
  1552. // the cell is pink!
  1553. foreach (DataColumn col in dt.Columns) {
  1554. var style = tv.Style.GetOrCreateColumnStyle (col);
  1555. style.ColorGetter = (e) => {
  1556. return scheme;
  1557. };
  1558. }
  1559. tv.Redraw (tv.Bounds);
  1560. expected =
  1561. @"
  1562. 00000000000000000000
  1563. 00000000000000000000
  1564. 00000000000000000000
  1565. 01111101101111111110
  1566. ";
  1567. TestHelpers.AssertDriverColorsAre (expected, new Attribute [] { tv.ColorScheme.Normal, color });
  1568. }
  1569. /// <summary>
  1570. /// Builds a simple table of string columns with the requested number of columns and rows
  1571. /// </summary>
  1572. /// <param name="cols"></param>
  1573. /// <param name="rows"></param>
  1574. /// <returns></returns>
  1575. public static DataTable BuildTable (int cols, int rows)
  1576. {
  1577. var dt = new DataTable ();
  1578. for (int c = 0; c < cols; c++) {
  1579. dt.Columns.Add ("Col" + c);
  1580. }
  1581. for (int r = 0; r < rows; r++) {
  1582. var newRow = dt.NewRow ();
  1583. for (int c = 0; c < cols; c++) {
  1584. newRow [c] = $"R{r}C{c}";
  1585. }
  1586. dt.Rows.Add (newRow);
  1587. }
  1588. return dt;
  1589. }
  1590. [Fact, AutoInitShutdown]
  1591. public void Test_ScreenToCell ()
  1592. {
  1593. var tableView = GetTwoRowSixColumnTable ();
  1594. tableView.BeginInit (); tableView.EndInit ();
  1595. tableView.LayoutSubviews ();
  1596. tableView.Redraw (tableView.Bounds);
  1597. // user can only scroll right so sees right indicator
  1598. // Because first column in table is A
  1599. string expected =
  1600. @"
  1601. โ”‚Aโ”‚Bโ”‚Cโ”‚
  1602. โ”œโ”€โ”ผโ”€โ”ผโ”€โ–บ
  1603. โ”‚1โ”‚2โ”‚3โ”‚
  1604. โ”‚1โ”‚2โ”‚3โ”‚";
  1605. TestHelpers.AssertDriverContentsAre (expected, output);
  1606. // ---------------- X=0 -----------------------
  1607. // click is before first cell
  1608. Assert.Null (tableView.ScreenToCell (0, 0));
  1609. Assert.Null (tableView.ScreenToCell (0, 1));
  1610. Assert.Null (tableView.ScreenToCell (0, 2));
  1611. Assert.Null (tableView.ScreenToCell (0, 3));
  1612. Assert.Null (tableView.ScreenToCell (0, 4));
  1613. // ---------------- X=1 -----------------------
  1614. // click in header
  1615. Assert.Null (tableView.ScreenToCell (1, 0));
  1616. // click in header row line
  1617. Assert.Null (tableView.ScreenToCell (1, 1));
  1618. // click in cell 0,0
  1619. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (1, 2));
  1620. // click in cell 0,1
  1621. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3));
  1622. // after last row
  1623. Assert.Null (tableView.ScreenToCell (1, 4));
  1624. // ---------------- X=2 -----------------------
  1625. // ( even though there is a horizontal dividing line here we treat it as a hit on the cell before)
  1626. // click in header
  1627. Assert.Null (tableView.ScreenToCell (2, 0));
  1628. // click in header row line
  1629. Assert.Null (tableView.ScreenToCell (2, 1));
  1630. // click in cell 0,0
  1631. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (2, 2));
  1632. // click in cell 0,1
  1633. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3));
  1634. // after last row
  1635. Assert.Null (tableView.ScreenToCell (2, 4));
  1636. // ---------------- X=3 -----------------------
  1637. // click in header
  1638. Assert.Null (tableView.ScreenToCell (3, 0));
  1639. // click in header row line
  1640. Assert.Null (tableView.ScreenToCell (3, 1));
  1641. // click in cell 1,0
  1642. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2));
  1643. // click in cell 1,1
  1644. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3));
  1645. // after last row
  1646. Assert.Null (tableView.ScreenToCell (3, 4));
  1647. }
  1648. [Fact, AutoInitShutdown]
  1649. public void Test_ScreenToCell_DataColumnOverload ()
  1650. {
  1651. var tableView = GetTwoRowSixColumnTable ();
  1652. tableView.LayoutSubviews ();
  1653. tableView.Redraw (tableView.Bounds);
  1654. // user can only scroll right so sees right indicator
  1655. // Because first column in table is A
  1656. string expected =
  1657. @"
  1658. โ”‚Aโ”‚Bโ”‚Cโ”‚
  1659. โ”œโ”€โ”ผโ”€โ”ผโ”€โ–บ
  1660. โ”‚1โ”‚2โ”‚3โ”‚
  1661. โ”‚1โ”‚2โ”‚3โ”‚";
  1662. TestHelpers.AssertDriverContentsAre (expected, output);
  1663. DataColumn col;
  1664. // ---------------- X=0 -----------------------
  1665. // click is before first cell
  1666. Assert.Null (tableView.ScreenToCell (0, 0, out col));
  1667. Assert.Null (col);
  1668. Assert.Null (tableView.ScreenToCell (0, 1, out col));
  1669. Assert.Null (col);
  1670. Assert.Null (tableView.ScreenToCell (0, 2, out col));
  1671. Assert.Null (col);
  1672. Assert.Null (tableView.ScreenToCell (0, 3, out col));
  1673. Assert.Null (col);
  1674. Assert.Null (tableView.ScreenToCell (0, 4, out col));
  1675. Assert.Null (col);
  1676. // ---------------- X=1 -----------------------
  1677. // click in header
  1678. Assert.Null (tableView.ScreenToCell (1, 0, out col));
  1679. Assert.Equal ("A", col.ColumnName);
  1680. // click in header row line (click in the horizontal line below header counts as click in header above - consistent with the column hit box)
  1681. Assert.Null (tableView.ScreenToCell (1, 1, out col));
  1682. Assert.Equal ("A", col.ColumnName);
  1683. // click in cell 0,0
  1684. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (1, 2, out col));
  1685. Assert.Null (col);
  1686. // click in cell 0,1
  1687. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3, out col));
  1688. Assert.Null (col);
  1689. // after last row
  1690. Assert.Null (tableView.ScreenToCell (1, 4, out col));
  1691. Assert.Null (col);
  1692. // ---------------- X=2 -----------------------
  1693. // click in header
  1694. Assert.Null (tableView.ScreenToCell (2, 0, out col));
  1695. Assert.Equal ("A", col.ColumnName);
  1696. // click in header row line
  1697. Assert.Null (tableView.ScreenToCell (2, 1, out col));
  1698. Assert.Equal ("A", col.ColumnName);
  1699. // click in cell 0,0
  1700. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (2, 2, out col));
  1701. Assert.Null (col);
  1702. // click in cell 0,1
  1703. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3, out col));
  1704. Assert.Null (col);
  1705. // after last row
  1706. Assert.Null (tableView.ScreenToCell (2, 4, out col));
  1707. Assert.Null (col);
  1708. // ---------------- X=3 -----------------------
  1709. // click in header
  1710. Assert.Null (tableView.ScreenToCell (3, 0, out col));
  1711. Assert.Equal ("B", col.ColumnName);
  1712. // click in header row line
  1713. Assert.Null (tableView.ScreenToCell (3, 1, out col));
  1714. Assert.Equal ("B", col.ColumnName);
  1715. // click in cell 1,0
  1716. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2, out col));
  1717. Assert.Null (col);
  1718. // click in cell 1,1
  1719. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3, out col));
  1720. Assert.Null (col);
  1721. // after last row
  1722. Assert.Null (tableView.ScreenToCell (3, 4, out col));
  1723. Assert.Null (col);
  1724. }
  1725. private TableView GetTwoRowSixColumnTable ()
  1726. {
  1727. var tableView = new TableView ();
  1728. tableView.ColorScheme = Colors.TopLevel;
  1729. // 3 columns are visible
  1730. tableView.Bounds = new Rect (0, 0, 7, 5);
  1731. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1732. tableView.Style.ShowHorizontalHeaderOverline = false;
  1733. tableView.Style.AlwaysShowHeaders = true;
  1734. tableView.Style.SmoothHorizontalScrolling = true;
  1735. var dt = new DataTable ();
  1736. dt.Columns.Add ("A");
  1737. dt.Columns.Add ("B");
  1738. dt.Columns.Add ("C");
  1739. dt.Columns.Add ("D");
  1740. dt.Columns.Add ("E");
  1741. dt.Columns.Add ("F");
  1742. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1743. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1744. tableView.Table = dt;
  1745. return tableView;
  1746. }
  1747. }
  1748. }