TableViewTests.cs 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280
  1. using System.Collections;
  2. using System.Data;
  3. using System.Globalization;
  4. using System.Reflection;
  5. using Xunit.Abstractions;
  6. namespace Terminal.Gui.ViewsTests;
  7. public class TableViewTests (ITestOutputHelper output)
  8. {
  9. /// <summary>Builds a simple list with the requested number of string items</summary>
  10. /// <param name="items"></param>
  11. /// <returns></returns>
  12. public static IList BuildList (int items)
  13. {
  14. List<string> list = new ();
  15. for (var i = 0; i < items; i++)
  16. {
  17. list.Add ("Item " + i);
  18. }
  19. return list.ToArray ();
  20. }
  21. public static DataTableSource BuildTable (int cols, int rows) { return BuildTable (cols, rows, out _); }
  22. /// <summary>Builds a simple table of string columns with the requested number of columns and rows</summary>
  23. /// <param name="cols"></param>
  24. /// <param name="rows"></param>
  25. /// <returns></returns>
  26. public static DataTableSource BuildTable (int cols, int rows, out DataTable dt)
  27. {
  28. dt = new ();
  29. for (var c = 0; c < cols; c++)
  30. {
  31. dt.Columns.Add ("Col" + c);
  32. }
  33. for (var r = 0; r < rows; r++)
  34. {
  35. DataRow newRow = dt.NewRow ();
  36. for (var c = 0; c < cols; c++)
  37. {
  38. newRow [c] = $"R{r}C{c}";
  39. }
  40. dt.Rows.Add (newRow);
  41. }
  42. return new (dt);
  43. }
  44. [Fact]
  45. [AutoInitShutdown]
  46. public void CellEventsBackgroundFill ()
  47. {
  48. var tv = new TableView { Width = 20, Height = 4 };
  49. var dt = new DataTable ();
  50. dt.Columns.Add ("C1");
  51. dt.Columns.Add ("C2");
  52. dt.Columns.Add ("C3");
  53. dt.Rows.Add ("Hello", DBNull.Value, "f");
  54. tv.Table = new DataTableSource (dt);
  55. tv.NullSymbol = string.Empty;
  56. var top = new Toplevel ();
  57. top.Add (tv);
  58. Application.Begin (top);
  59. tv.Draw ();
  60. var expected =
  61. @"
  62. ┌─────┬──┬─────────┐
  63. │C1 │C2│C3 │
  64. ├─────┼──┼─────────┤
  65. │Hello│ │f │
  66. ";
  67. TestHelpers.AssertDriverContentsAre (expected, output);
  68. var color = new Attribute (Color.Magenta, Color.BrightBlue);
  69. var scheme = new ColorScheme
  70. {
  71. Normal = color,
  72. HotFocus = color,
  73. Focus = color,
  74. Disabled = color,
  75. HotNormal = color
  76. };
  77. // Now the thing we really want to test is the styles!
  78. // All cells in the column have a column style that says
  79. // the cell is pink!
  80. for (var i = 0; i < dt.Columns.Count; i++)
  81. {
  82. ColumnStyle style = tv.Style.GetOrCreateColumnStyle (i);
  83. style.ColorGetter = e => { return scheme; };
  84. }
  85. tv.Draw ();
  86. expected =
  87. @"
  88. 00000000000000000000
  89. 00000000000000000000
  90. 00000000000000000000
  91. 01111101101111111110
  92. ";
  93. TestHelpers.AssertDriverAttributesAre (expected, Application.Driver, tv.ColorScheme.Normal, color);
  94. top.Dispose ();
  95. }
  96. [Fact]
  97. public void DeleteRow_SelectAll_AdjustsSelectionToPreventOverrun ()
  98. {
  99. // create a 4 by 4 table
  100. var tableView = new TableView
  101. {
  102. Table = BuildTable (4, 4, out DataTable dt), MultiSelect = true, Viewport = new (0, 0, 10, 5)
  103. };
  104. tableView.BeginInit ();
  105. tableView.EndInit ();
  106. tableView.SelectAll ();
  107. Assert.Equal (16, tableView.GetAllSelectedCells ().Count ());
  108. // delete one of the columns
  109. dt.Columns.RemoveAt (2);
  110. // table should now be 3x4
  111. Assert.Equal (12, tableView.GetAllSelectedCells ().Count ());
  112. // remove a row
  113. dt.Rows.RemoveAt (1);
  114. // table should now be 3x3
  115. Assert.Equal (9, tableView.GetAllSelectedCells ().Count ());
  116. }
  117. [Fact]
  118. public void DeleteRow_SelectLastRow_AdjustsSelectionToPreventOverrun ()
  119. {
  120. // create a 4 by 4 table
  121. var tableView = new TableView
  122. {
  123. Table = BuildTable (4, 4, out DataTable dt), MultiSelect = true, Viewport = new (0, 0, 10, 5)
  124. };
  125. tableView.BeginInit ();
  126. tableView.EndInit ();
  127. tableView.ChangeSelectionToEndOfTable (false);
  128. // select the last row
  129. tableView.MultiSelectedRegions.Clear ();
  130. tableView.MultiSelectedRegions.Push (new (new (0, 3), new (0, 3, 4, 1)));
  131. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  132. // remove a row
  133. dt.Rows.RemoveAt (0);
  134. tableView.EnsureValidSelection ();
  135. // since the selection no longer exists it should be removed
  136. Assert.Empty (tableView.MultiSelectedRegions);
  137. }
  138. [Fact]
  139. public void EnsureValidScrollOffsets_LoadSmallerTable ()
  140. {
  141. var tableView = new TableView ();
  142. tableView.BeginInit ();
  143. tableView.EndInit ();
  144. tableView.Viewport = new (0, 0, 25, 10);
  145. Assert.Equal (0, tableView.RowOffset);
  146. Assert.Equal (0, tableView.ColumnOffset);
  147. // Set big table
  148. tableView.Table = BuildTable (25, 50);
  149. // Scroll down and along
  150. tableView.RowOffset = 20;
  151. tableView.ColumnOffset = 10;
  152. tableView.EnsureValidScrollOffsets ();
  153. // The scroll should be valid at the moment
  154. Assert.Equal (20, tableView.RowOffset);
  155. Assert.Equal (10, tableView.ColumnOffset);
  156. // Set small table
  157. tableView.Table = BuildTable (2, 2);
  158. // Setting a small table should automatically trigger fixing the scroll offsets to ensure valid cells
  159. Assert.Equal (0, tableView.RowOffset);
  160. Assert.Equal (0, tableView.ColumnOffset);
  161. // Trying to set invalid indexes should not be possible
  162. tableView.RowOffset = 20;
  163. tableView.ColumnOffset = 10;
  164. Assert.Equal (1, tableView.RowOffset);
  165. Assert.Equal (1, tableView.ColumnOffset);
  166. }
  167. [Fact]
  168. public void EnsureValidScrollOffsets_WithNoCells ()
  169. {
  170. var tableView = new TableView ();
  171. Assert.Equal (0, tableView.RowOffset);
  172. Assert.Equal (0, tableView.ColumnOffset);
  173. // Set empty table
  174. tableView.Table = new DataTableSource (new ());
  175. // Since table has no rows or columns scroll offset should default to 0
  176. tableView.EnsureValidScrollOffsets ();
  177. Assert.Equal (0, tableView.RowOffset);
  178. Assert.Equal (0, tableView.ColumnOffset);
  179. }
  180. [Theory]
  181. [InlineData (true)]
  182. [InlineData (false)]
  183. public void GetAllSelectedCells_SingleCellSelected_ReturnsOne (bool multiSelect)
  184. {
  185. var tableView = new TableView
  186. {
  187. Table = BuildTable (3, 3), MultiSelect = multiSelect, Viewport = new (0, 0, 10, 5)
  188. };
  189. tableView.BeginInit ();
  190. tableView.EndInit ();
  191. tableView.SetSelection (1, 1, false);
  192. Assert.Single (tableView.GetAllSelectedCells ());
  193. Assert.Equal (new (1, 1), tableView.GetAllSelectedCells ().Single ());
  194. }
  195. [Fact]
  196. public void GetAllSelectedCells_SquareSelection_FullRowSelect ()
  197. {
  198. var tableView = new TableView
  199. {
  200. Table = BuildTable (3, 3), MultiSelect = true, FullRowSelect = true, Viewport = new (0, 0, 10, 5)
  201. };
  202. tableView.BeginInit ();
  203. tableView.EndInit ();
  204. // move cursor to 1,1
  205. tableView.SetSelection (1, 1, false);
  206. // spread selection across to 2,2 (e.g. shift+right then shift+down)
  207. tableView.SetSelection (2, 2, true);
  208. Point [] selected = tableView.GetAllSelectedCells ().ToArray ();
  209. Assert.Equal (6, selected.Length);
  210. Assert.Equal (new (0, 1), selected [0]);
  211. Assert.Equal (new (1, 1), selected [1]);
  212. Assert.Equal (new (2, 1), selected [2]);
  213. Assert.Equal (new (0, 2), selected [3]);
  214. Assert.Equal (new (1, 2), selected [4]);
  215. Assert.Equal (new (2, 2), selected [5]);
  216. }
  217. [Fact]
  218. public void GetAllSelectedCells_SquareSelection_ReturnsFour ()
  219. {
  220. var tableView = new TableView
  221. {
  222. Table = BuildTable (3, 3), MultiSelect = true, Viewport = new (0, 0, 10, 5)
  223. };
  224. tableView.BeginInit ();
  225. tableView.EndInit ();
  226. // move cursor to 1,1
  227. tableView.SetSelection (1, 1, false);
  228. // spread selection across to 2,2 (e.g. shift+right then shift+down)
  229. tableView.SetSelection (2, 2, true);
  230. Point [] selected = tableView.GetAllSelectedCells ().ToArray ();
  231. Assert.Equal (4, selected.Length);
  232. Assert.Equal (new (1, 1), selected [0]);
  233. Assert.Equal (new (2, 1), selected [1]);
  234. Assert.Equal (new (1, 2), selected [2]);
  235. Assert.Equal (new (2, 2), selected [3]);
  236. }
  237. [Fact]
  238. public void GetAllSelectedCells_TwoIsolatedSelections_ReturnsSix ()
  239. {
  240. var tableView = new TableView
  241. {
  242. Table = BuildTable (20, 20), MultiSelect = true, Viewport = new (0, 0, 10, 5)
  243. };
  244. tableView.BeginInit ();
  245. tableView.EndInit ();
  246. /*
  247. Sets up disconnected selections like:
  248. 00000000000
  249. 01100000000
  250. 01100000000
  251. 00000001100
  252. 00000000000
  253. */
  254. tableView.MultiSelectedRegions.Clear ();
  255. tableView.MultiSelectedRegions.Push (new (new (1, 1), new (1, 1, 2, 2)));
  256. tableView.MultiSelectedRegions.Push (new (new (7, 3), new (7, 3, 2, 1)));
  257. tableView.SelectedColumn = 8;
  258. tableView.SelectedRow = 3;
  259. Point [] selected = tableView.GetAllSelectedCells ().ToArray ();
  260. Assert.Equal (6, selected.Length);
  261. Assert.Equal (new (1, 1), selected [0]);
  262. Assert.Equal (new (2, 1), selected [1]);
  263. Assert.Equal (new (1, 2), selected [2]);
  264. Assert.Equal (new (2, 2), selected [3]);
  265. Assert.Equal (new (7, 3), selected [4]);
  266. Assert.Equal (new (8, 3), selected [5]);
  267. }
  268. [Fact]
  269. public void IsSelected_MultiSelectionOn_BoxSelection ()
  270. {
  271. var tableView = new TableView { Table = BuildTable (25, 50), MultiSelect = true };
  272. // 4 cell horizontal in box 2x2
  273. tableView.SetSelection (0, 0, false);
  274. tableView.SetSelection (1, 1, true);
  275. Assert.True (tableView.IsSelected (0, 0));
  276. Assert.True (tableView.IsSelected (1, 0));
  277. Assert.False (tableView.IsSelected (2, 0));
  278. Assert.True (tableView.IsSelected (0, 1));
  279. Assert.True (tableView.IsSelected (1, 1));
  280. Assert.False (tableView.IsSelected (2, 1));
  281. Assert.False (tableView.IsSelected (0, 2));
  282. Assert.False (tableView.IsSelected (1, 2));
  283. Assert.False (tableView.IsSelected (2, 2));
  284. }
  285. [Fact]
  286. public void IsSelected_MultiSelectionOn_Horizontal ()
  287. {
  288. var tableView = new TableView { Table = BuildTable (25, 50), MultiSelect = true };
  289. // 2 cell horizontal selection
  290. tableView.SetSelection (1, 0, false);
  291. tableView.SetSelection (2, 0, true);
  292. Assert.False (tableView.IsSelected (0, 0));
  293. Assert.True (tableView.IsSelected (1, 0));
  294. Assert.True (tableView.IsSelected (2, 0));
  295. Assert.False (tableView.IsSelected (3, 0));
  296. Assert.False (tableView.IsSelected (0, 1));
  297. Assert.False (tableView.IsSelected (1, 1));
  298. Assert.False (tableView.IsSelected (2, 1));
  299. Assert.False (tableView.IsSelected (3, 1));
  300. }
  301. [Fact]
  302. public void IsSelected_MultiSelectionOn_Vertical ()
  303. {
  304. var tableView = new TableView { Table = BuildTable (25, 50), MultiSelect = true };
  305. // 3 cell vertical selection
  306. tableView.SetSelection (1, 1, false);
  307. tableView.SetSelection (1, 3, true);
  308. Assert.False (tableView.IsSelected (0, 0));
  309. Assert.False (tableView.IsSelected (1, 0));
  310. Assert.False (tableView.IsSelected (2, 0));
  311. Assert.False (tableView.IsSelected (0, 1));
  312. Assert.True (tableView.IsSelected (1, 1));
  313. Assert.False (tableView.IsSelected (2, 1));
  314. Assert.False (tableView.IsSelected (0, 2));
  315. Assert.True (tableView.IsSelected (1, 2));
  316. Assert.False (tableView.IsSelected (2, 2));
  317. Assert.False (tableView.IsSelected (0, 3));
  318. Assert.True (tableView.IsSelected (1, 3));
  319. Assert.False (tableView.IsSelected (2, 3));
  320. Assert.False (tableView.IsSelected (0, 4));
  321. Assert.False (tableView.IsSelected (1, 4));
  322. Assert.False (tableView.IsSelected (2, 4));
  323. }
  324. [Fact]
  325. [AutoInitShutdown]
  326. public void LongColumnTest ()
  327. {
  328. var tableView = new TableView ();
  329. var top = new Toplevel ();
  330. top.Add (tableView);
  331. Application.Begin (top);
  332. tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  333. // 25 characters can be printed into table
  334. tableView.Viewport = new (0, 0, 25, 5);
  335. tableView.Style.ShowHorizontalHeaderUnderline = true;
  336. tableView.Style.ShowHorizontalHeaderOverline = false;
  337. tableView.Style.AlwaysShowHeaders = true;
  338. tableView.Style.SmoothHorizontalScrolling = true;
  339. var dt = new DataTable ();
  340. dt.Columns.Add ("A");
  341. dt.Columns.Add ("B");
  342. dt.Columns.Add ("Very Long Column");
  343. dt.Rows.Add (1, 2, new string ('a', 500));
  344. dt.Rows.Add (1, 2, "aaa");
  345. tableView.Table = new DataTableSource (dt);
  346. tableView.LayoutSubviews ();
  347. tableView.Draw ();
  348. // default behaviour of TableView is not to render
  349. // columns unless there is sufficient space
  350. var expected =
  351. @"
  352. │A│B │
  353. ├─┼─────────────────────►
  354. │1│2 │
  355. │1│2 │
  356. ";
  357. TestHelpers.AssertDriverContentsAre (expected, output);
  358. // get a style for the long column
  359. ColumnStyle style = tableView.Style.GetOrCreateColumnStyle (2);
  360. // one way the API user can fix this for long columns
  361. // is to specify a MinAcceptableWidth for the column
  362. style.MaxWidth = 10;
  363. tableView.LayoutSubviews ();
  364. tableView.Draw ();
  365. expected =
  366. @"
  367. │A│B│Very Long Column │
  368. ├─┼─┼───────────────────┤
  369. │1│2│aaaaaaaaaaaaaaaaaaa│
  370. │1│2│aaa │
  371. ";
  372. TestHelpers.AssertDriverContentsAre (expected, output);
  373. // revert the style change
  374. style.MaxWidth = TableView.DefaultMaxCellWidth;
  375. // another way API user can fix problem is to implement
  376. // RepresentationGetter and apply max length there
  377. style.RepresentationGetter = s => { return s.ToString ().Length < 15 ? s.ToString () : s.ToString ().Substring (0, 13) + "..."; };
  378. tableView.LayoutSubviews ();
  379. tableView.Draw ();
  380. expected =
  381. @"
  382. │A│B│Very Long Column │
  383. ├─┼─┼───────────────────┤
  384. │1│2│aaaaaaaaaaaaa... │
  385. │1│2│aaa │
  386. ";
  387. TestHelpers.AssertDriverContentsAre (expected, output);
  388. // revert style change
  389. style.RepresentationGetter = null;
  390. // Both of the above methods rely on having a fixed
  391. // size limit for the column. These are awkward if a
  392. // table is resizeable e.g. Dim.Fill(). Ideally we want
  393. // to render in any space available and truncate the content
  394. // of the column dynamically so it fills the free space at
  395. // the end of the table.
  396. // We can now specify that the column can be any length
  397. // (Up to MaxWidth) but the renderer can accept using
  398. // less space down to this limit
  399. style.MinAcceptableWidth = 5;
  400. tableView.LayoutSubviews ();
  401. tableView.Draw ();
  402. expected =
  403. @"
  404. │A│B│Very Long Column │
  405. ├─┼─┼───────────────────┤
  406. │1│2│aaaaaaaaaaaaaaaaaaa│
  407. │1│2│aaa │
  408. ";
  409. TestHelpers.AssertDriverContentsAre (expected, output);
  410. // Now test making the width too small for the MinAcceptableWidth
  411. // the Column won't fit so should not be rendered
  412. var driver = (FakeDriver)Application.Driver;
  413. driver.ClearContents ();
  414. tableView.Viewport = new (0, 0, 9, 5);
  415. tableView.LayoutSubviews ();
  416. tableView.Draw ();
  417. expected =
  418. @"
  419. │A│B │
  420. ├─┼─────►
  421. │1│2 │
  422. │1│2 │
  423. ";
  424. TestHelpers.AssertDriverContentsAre (expected, output);
  425. // setting width to 10 leaves just enough space for the column to
  426. // meet MinAcceptableWidth of 5. Column width includes terminator line
  427. // symbol (e.g. ┤ or │)
  428. tableView.Viewport = new (0, 0, 10, 5);
  429. tableView.LayoutSubviews ();
  430. tableView.Draw ();
  431. expected =
  432. @"
  433. │A│B│Very│
  434. ├─┼─┼────┤
  435. │1│2│aaaa│
  436. │1│2│aaa │
  437. ";
  438. TestHelpers.AssertDriverContentsAre (expected, output);
  439. tableView.Viewport = new (0, 0, 25, 5);
  440. // revert style change
  441. style.MinAcceptableWidth = TableView.DefaultMinAcceptableWidth;
  442. // Now let's test the global MaxCellWidth and MinCellWidth
  443. tableView.Style.ExpandLastColumn = false;
  444. tableView.MaxCellWidth = 10;
  445. tableView.MinCellWidth = 3;
  446. tableView.LayoutSubviews ();
  447. tableView.Draw ();
  448. expected =
  449. @"
  450. │A │B │Very Long │ │
  451. ├───┼───┼──────────┼────┤
  452. │1 │2 │aaaaaaaaaa│ │
  453. │1 │2 │aaa │ │
  454. ";
  455. TestHelpers.AssertDriverContentsAre (expected, output);
  456. // MaxCellWidth limits MinCellWidth
  457. tableView.MaxCellWidth = 5;
  458. tableView.MinCellWidth = 10;
  459. tableView.LayoutSubviews ();
  460. tableView.Draw ();
  461. expected =
  462. @"
  463. │A │B │Very │ │
  464. ├─────┼─────┼─────┼─────┤
  465. │1 │2 │aaaaa│ │
  466. │1 │2 │aaa │ │
  467. ";
  468. TestHelpers.AssertDriverContentsAre (expected, output);
  469. top.Dispose ();
  470. Application.Shutdown ();
  471. top.Dispose ();
  472. }
  473. [AutoInitShutdown]
  474. [Fact]
  475. public void PageDown_ExcludesHeaders ()
  476. {
  477. var tableView = new TableView
  478. {
  479. Table = BuildTable (25, 50), MultiSelect = true, Viewport = new (0, 0, 10, 5)
  480. };
  481. // Header should take up 2 lines
  482. tableView.Style.ShowHorizontalHeaderOverline = false;
  483. tableView.Style.ShowHorizontalHeaderUnderline = true;
  484. tableView.Style.AlwaysShowHeaders = false;
  485. // ensure that TableView has the input focus
  486. var top = new Toplevel ();
  487. top.Add (tableView);
  488. Application.Begin (top);
  489. top.FocusDeepest (NavigationDirection.Forward, null);
  490. Assert.True (tableView.HasFocus);
  491. Assert.Equal (0, tableView.RowOffset);
  492. tableView.NewKeyDownEvent (Key.PageDown);
  493. // window height is 5 rows 2 are header so page down should give 3 new rows
  494. Assert.Equal (3, tableView.SelectedRow);
  495. Assert.Equal (1, tableView.RowOffset);
  496. // header is no longer visible so page down should give 5 new rows
  497. tableView.NewKeyDownEvent (Key.PageDown);
  498. Assert.Equal (8, tableView.SelectedRow);
  499. Assert.Equal (4, tableView.RowOffset);
  500. top.Dispose ();
  501. }
  502. [Fact]
  503. [SetupFakeDriver]
  504. public void Redraw_EmptyTable ()
  505. {
  506. var tableView = new TableView ();
  507. tableView.ColorScheme = new ();
  508. tableView.Viewport = new (0, 0, 25, 10);
  509. // Set a table with 1 column
  510. tableView.Table = BuildTable (1, 50, out DataTable dt);
  511. tableView.Draw ();
  512. dt.Columns.Remove (dt.Columns [0]);
  513. tableView.Draw ();
  514. }
  515. [Fact]
  516. public void ScrollDown_OneLineAtATime ()
  517. {
  518. var tableView = new TableView ();
  519. tableView.BeginInit ();
  520. tableView.EndInit ();
  521. // Set big table
  522. tableView.Table = BuildTable (25, 50);
  523. // 1 header + 4 rows visible
  524. tableView.Viewport = new (0, 0, 25, 5);
  525. tableView.Style.ShowHorizontalHeaderUnderline = false;
  526. tableView.Style.ShowHorizontalHeaderOverline = false;
  527. tableView.Style.AlwaysShowHeaders = true;
  528. // select last row
  529. tableView.SelectedRow = 3; // row is 0 indexed so this is the 4th visible row
  530. // Scroll down
  531. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorDown });
  532. // Scrolled off the page by 1 row so it should only have moved down 1 line of RowOffset
  533. Assert.Equal (4, tableView.SelectedRow);
  534. Assert.Equal (1, tableView.RowOffset);
  535. }
  536. [Fact]
  537. [SetupFakeDriver]
  538. public void ScrollIndicators ()
  539. {
  540. var tableView = new TableView ();
  541. tableView.BeginInit ();
  542. tableView.EndInit ();
  543. tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  544. // 3 columns are visibile
  545. tableView.Viewport = new (0, 0, 7, 5);
  546. tableView.Style.ShowHorizontalHeaderUnderline = true;
  547. tableView.Style.ShowHorizontalHeaderOverline = false;
  548. tableView.Style.AlwaysShowHeaders = true;
  549. tableView.Style.SmoothHorizontalScrolling = true;
  550. var dt = new DataTable ();
  551. dt.Columns.Add ("A");
  552. dt.Columns.Add ("B");
  553. dt.Columns.Add ("C");
  554. dt.Columns.Add ("D");
  555. dt.Columns.Add ("E");
  556. dt.Columns.Add ("F");
  557. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  558. tableView.Table = new DataTableSource (dt);
  559. // select last visible column
  560. tableView.SelectedColumn = 2; // column C
  561. tableView.Draw ();
  562. // user can only scroll right so sees right indicator
  563. // Because first column in table is A
  564. var expected =
  565. @"
  566. │A│B│C│
  567. ├─┼─┼─►
  568. │1│2│3│";
  569. TestHelpers.AssertDriverContentsAre (expected, output);
  570. // Scroll right
  571. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  572. // since A is now pushed off screen we get indicator showing
  573. // that user can scroll left to see first column
  574. tableView.Draw ();
  575. expected =
  576. @"
  577. │B│C│D│
  578. ◄─┼─┼─►
  579. │2│3│4│";
  580. TestHelpers.AssertDriverContentsAre (expected, output);
  581. // Scroll right twice more (to end of columns)
  582. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  583. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  584. tableView.Draw ();
  585. expected =
  586. @"
  587. │D│E│F│
  588. ◄─┼─┼─┤
  589. │4│5│6│";
  590. TestHelpers.AssertDriverContentsAre (expected, output);
  591. // Shutdown must be called to safely clean up Application if Init has been called
  592. Application.Shutdown ();
  593. }
  594. [Fact]
  595. [SetupFakeDriver]
  596. public void ScrollRight_SmoothScrolling ()
  597. {
  598. var tableView = new TableView ();
  599. tableView.BeginInit ();
  600. tableView.EndInit ();
  601. tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  602. tableView.LayoutSubviews ();
  603. // 3 columns are visibile
  604. tableView.Viewport = new (0, 0, 7, 5);
  605. tableView.Style.ShowHorizontalHeaderUnderline = false;
  606. tableView.Style.ShowHorizontalHeaderOverline = false;
  607. tableView.Style.AlwaysShowHeaders = true;
  608. tableView.Style.SmoothHorizontalScrolling = true;
  609. var dt = new DataTable ();
  610. dt.Columns.Add ("A");
  611. dt.Columns.Add ("B");
  612. dt.Columns.Add ("C");
  613. dt.Columns.Add ("D");
  614. dt.Columns.Add ("E");
  615. dt.Columns.Add ("F");
  616. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  617. tableView.Table = new DataTableSource (dt);
  618. // select last visible column
  619. tableView.SelectedColumn = 2; // column C
  620. tableView.Draw ();
  621. var expected =
  622. @"
  623. │A│B│C│
  624. │1│2│3│";
  625. TestHelpers.AssertDriverContentsAre (expected, output);
  626. // Scroll right
  627. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  628. tableView.Draw ();
  629. // Note that with SmoothHorizontalScrolling only a single new column
  630. // is exposed when scrolling right. This is not always the case though
  631. // sometimes if the leftmost column is long (i.e. A is a long column)
  632. // then when A is pushed off the screen multiple new columns could be exposed
  633. // (not just D but also E and F). This is because TableView never shows
  634. // 'half cells' or scrolls by console unit (scrolling is done by table row/column increments).
  635. expected =
  636. @"
  637. │B│C│D│
  638. │2│3│4│";
  639. TestHelpers.AssertDriverContentsAre (expected, output);
  640. }
  641. [Fact]
  642. [SetupFakeDriver]
  643. public void ScrollRight_WithoutSmoothScrolling ()
  644. {
  645. var tableView = new TableView ();
  646. tableView.BeginInit ();
  647. tableView.EndInit ();
  648. tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  649. // 3 columns are visibile
  650. tableView.Viewport = new (0, 0, 7, 5);
  651. tableView.Style.ShowHorizontalHeaderUnderline = false;
  652. tableView.Style.ShowHorizontalHeaderOverline = false;
  653. tableView.Style.AlwaysShowHeaders = true;
  654. tableView.Style.SmoothHorizontalScrolling = false;
  655. var dt = new DataTable ();
  656. dt.Columns.Add ("A");
  657. dt.Columns.Add ("B");
  658. dt.Columns.Add ("C");
  659. dt.Columns.Add ("D");
  660. dt.Columns.Add ("E");
  661. dt.Columns.Add ("F");
  662. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  663. tableView.Table = new DataTableSource (dt);
  664. // select last visible column
  665. tableView.SelectedColumn = 2; // column C
  666. tableView.Draw ();
  667. var expected =
  668. @"
  669. │A│B│C│
  670. │1│2│3│";
  671. TestHelpers.AssertDriverContentsAre (expected, output);
  672. // Scroll right
  673. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  674. tableView.Draw ();
  675. // notice that without smooth scrolling we just update the first column
  676. // rendered in the table to the newly exposed column (D). This is fast
  677. // since we don't have to worry about repeatedly measuring the content
  678. // area as we scroll until the new column (D) is exposed. But it makes
  679. // the view 'jump' to expose all new columns
  680. expected =
  681. @"
  682. │D│E│F│
  683. │4│5│6│";
  684. TestHelpers.AssertDriverContentsAre (expected, output);
  685. }
  686. [Fact]
  687. public void SelectedCellChanged_NotFiredForSameValue ()
  688. {
  689. var tableView = new TableView { Table = BuildTable (25, 50) };
  690. var called = false;
  691. tableView.SelectedCellChanged += (s, e) => { called = true; };
  692. Assert.Equal (0, tableView.SelectedColumn);
  693. Assert.False (called);
  694. // Changing value to same as it already was should not raise an event
  695. tableView.SelectedColumn = 0;
  696. Assert.False (called);
  697. tableView.SelectedColumn = 10;
  698. Assert.True (called);
  699. }
  700. [Fact]
  701. public void SelectedCellChanged_SelectedColumnIndexesCorrect ()
  702. {
  703. var tableView = new TableView { Table = BuildTable (25, 50) };
  704. var called = false;
  705. tableView.SelectedCellChanged += (s, e) =>
  706. {
  707. called = true;
  708. Assert.Equal (0, e.OldCol);
  709. Assert.Equal (10, e.NewCol);
  710. };
  711. tableView.SelectedColumn = 10;
  712. Assert.True (called);
  713. }
  714. [Fact]
  715. public void SelectedCellChanged_SelectedRowIndexesCorrect ()
  716. {
  717. var tableView = new TableView { Table = BuildTable (25, 50) };
  718. var called = false;
  719. tableView.SelectedCellChanged += (s, e) =>
  720. {
  721. called = true;
  722. Assert.Equal (0, e.OldRow);
  723. Assert.Equal (10, e.NewRow);
  724. };
  725. tableView.SelectedRow = 10;
  726. Assert.True (called);
  727. }
  728. [Fact]
  729. [SetupFakeDriver]
  730. public void ShowHorizontalBottomLine_NoCellLines ()
  731. {
  732. TableView tableView = GetABCDEFTableView (out _);
  733. tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  734. // 3 columns are visibile
  735. tableView.Viewport = new (0, 0, 7, 5);
  736. tableView.Style.ShowHorizontalHeaderUnderline = true;
  737. tableView.Style.ShowHorizontalHeaderOverline = false;
  738. tableView.Style.AlwaysShowHeaders = true;
  739. tableView.Style.SmoothHorizontalScrolling = true;
  740. tableView.Style.ShowHorizontalBottomline = true;
  741. tableView.Style.ShowVerticalCellLines = false;
  742. tableView.Draw ();
  743. // user can only scroll right so sees right indicator
  744. // Because first column in table is A
  745. var expected =
  746. @"
  747. │A│B│C│
  748. └─┴─┴─►
  749. 1 2 3
  750. ───────";
  751. TestHelpers.AssertDriverContentsAre (expected, output);
  752. }
  753. [Fact]
  754. [SetupFakeDriver]
  755. public void ShowHorizontalBottomLine_WithVerticalCellLines ()
  756. {
  757. TableView tableView = GetABCDEFTableView (out _);
  758. tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  759. // 3 columns are visibile
  760. tableView.Viewport = new (0, 0, 7, 5);
  761. tableView.Style.ShowHorizontalHeaderUnderline = true;
  762. tableView.Style.ShowHorizontalHeaderOverline = false;
  763. tableView.Style.AlwaysShowHeaders = true;
  764. tableView.Style.SmoothHorizontalScrolling = true;
  765. tableView.Style.ShowHorizontalBottomline = true;
  766. tableView.Draw ();
  767. // user can only scroll right so sees right indicator
  768. // Because first column in table is A
  769. var expected =
  770. @"
  771. │A│B│C│
  772. ├─┼─┼─►
  773. │1│2│3│
  774. └─┴─┴─┘";
  775. TestHelpers.AssertDriverContentsAre (expected, output);
  776. }
  777. [Fact]
  778. [AutoInitShutdown]
  779. public void TableView_Activate ()
  780. {
  781. string activatedValue = null;
  782. var tv = new TableView (BuildTable (1, 1));
  783. tv.CellActivated += (s, c) => activatedValue = c.Table [c.Row, c.Col].ToString ();
  784. var top = new Toplevel ();
  785. top.Add (tv);
  786. Application.Begin (top);
  787. // pressing enter should activate the first cell (selected cell)
  788. tv.NewKeyDownEvent (Key.Enter);
  789. Assert.Equal ("R0C0", activatedValue);
  790. // reset the test
  791. activatedValue = null;
  792. // clear keybindings and ensure that Enter does not trigger the event anymore
  793. tv.KeyBindings.Clear ();
  794. tv.NewKeyDownEvent (Key.Enter);
  795. Assert.Null (activatedValue);
  796. // New method for changing the activation key
  797. tv.KeyBindings.Add (Key.Z, Command.Accept);
  798. tv.NewKeyDownEvent (Key.Z);
  799. Assert.Equal ("R0C0", activatedValue);
  800. // reset the test
  801. activatedValue = null;
  802. tv.KeyBindings.Clear ();
  803. // Old method for changing the activation key
  804. tv.CellActivationKey = KeyCode.Z;
  805. tv.NewKeyDownEvent (Key.Z);
  806. Assert.Equal ("R0C0", activatedValue);
  807. top.Dispose ();
  808. }
  809. [Theory]
  810. [SetupFakeDriver]
  811. [InlineData (false)]
  812. [InlineData (true)]
  813. public void TableView_ColorsTest_ColorGetter (bool focused)
  814. {
  815. TableView tv = SetUpMiniTable (out DataTable dt);
  816. tv.LayoutSubviews ();
  817. // width exactly matches the max col widths
  818. tv.Viewport = new (0, 0, 5, 4);
  819. // Create a style for column B
  820. ColumnStyle bStyle = tv.Style.GetOrCreateColumnStyle (1);
  821. // when B is 2 use the custom highlight color
  822. var cellHighlight = new ColorScheme
  823. {
  824. Normal = new (Color.BrightCyan, Color.DarkGray),
  825. HotNormal = new (Color.Green, Color.Blue),
  826. Focus = new (Color.Cyan, Color.Magenta),
  827. // Not used by TableView
  828. HotFocus = new (Color.BrightYellow, Color.White)
  829. };
  830. bStyle.ColorGetter = a => Convert.ToInt32 (a.CellValue) == 2 ? cellHighlight : null;
  831. tv.HasFocus = focused;
  832. Assert.Equal(focused, tv.HasFocus);
  833. tv.Draw ();
  834. var expected = @"
  835. ┌─┬─┐
  836. │A│B│
  837. ├─┼─┤
  838. │1│2│
  839. ";
  840. TestHelpers.AssertDriverContentsAre (expected, output);
  841. var expectedColors = @"
  842. 00000
  843. 00000
  844. 00000
  845. 01020
  846. ";
  847. TestHelpers.AssertDriverAttributesAre (
  848. expectedColors,
  849. Application.Driver,
  850. tv.ColorScheme.Normal,
  851. focused ? tv.ColorScheme.Focus : tv.ColorScheme.HotNormal,
  852. cellHighlight.Normal
  853. );
  854. // change the value in the table so that
  855. // it no longer matches the ColorGetter
  856. // delegate conditional ( which checks for
  857. // the value 2)
  858. dt.Rows [0] [1] = 5;
  859. tv.Draw ();
  860. expected = @"
  861. ┌─┬─┐
  862. │A│B│
  863. ├─┼─┤
  864. │1│5│
  865. ";
  866. TestHelpers.AssertDriverContentsAre (expected, output);
  867. expectedColors = @"
  868. 00000
  869. 00000
  870. 00000
  871. 01000
  872. ";
  873. // now we only see 2 colors used (the selected cell color and Normal
  874. // cellHighlight should no longer be used because the delegate returned null
  875. // (now that the cell value is 5 - which does not match the conditional)
  876. TestHelpers.AssertDriverAttributesAre (
  877. expectedColors,
  878. Application.Driver,
  879. tv.ColorScheme.Normal,
  880. focused ? tv.ColorScheme.Focus : tv.ColorScheme.HotNormal
  881. );
  882. }
  883. [Theory]
  884. [SetupFakeDriver]
  885. [InlineData (false)]
  886. [InlineData (true)]
  887. public void TableView_ColorsTest_RowColorGetter (bool focused)
  888. {
  889. TableView tv = SetUpMiniTable (out DataTable dt);
  890. tv.LayoutSubviews ();
  891. // width exactly matches the max col widths
  892. tv.Viewport = new (0, 0, 5, 4);
  893. var rowHighlight = new ColorScheme
  894. {
  895. Normal = new (Color.BrightCyan, Color.DarkGray),
  896. HotNormal = new (Color.Green, Color.Blue),
  897. Focus = new (Color.BrightYellow, Color.White),
  898. // Not used by TableView
  899. HotFocus = new (Color.Cyan, Color.Magenta)
  900. };
  901. // when B is 2 use the custom highlight color for the row
  902. tv.Style.RowColorGetter += e => Convert.ToInt32 (e.Table [e.RowIndex, 1]) == 2 ? rowHighlight : null;
  903. tv.HasFocus = focused;
  904. Assert.Equal (focused, tv.HasFocus);
  905. tv.Draw ();
  906. var expected = @"
  907. ┌─┬─┐
  908. │A│B│
  909. ├─┼─┤
  910. │1│2│
  911. ";
  912. TestHelpers.AssertDriverContentsAre (expected, output);
  913. var expectedColors = @"
  914. 00000
  915. 00000
  916. 00000
  917. 21222
  918. ";
  919. TestHelpers.AssertDriverAttributesAre (
  920. expectedColors,
  921. Application.Driver,
  922. tv.ColorScheme.Normal,
  923. focused ? rowHighlight.Focus : rowHighlight.HotNormal,
  924. rowHighlight.Normal
  925. );
  926. // change the value in the table so that
  927. // it no longer matches the RowColorGetter
  928. // delegate conditional ( which checks for
  929. // the value 2)
  930. dt.Rows [0] [1] = 5;
  931. tv.Draw ();
  932. expected = @"
  933. ┌─┬─┐
  934. │A│B│
  935. ├─┼─┤
  936. │1│5│
  937. ";
  938. TestHelpers.AssertDriverContentsAre (expected, output);
  939. expectedColors = @"
  940. 00000
  941. 00000
  942. 00000
  943. 01000
  944. ";
  945. // now we only see 2 colors used (the selected cell color and Normal
  946. // rowHighlight should no longer be used because the delegate returned null
  947. // (now that the cell value is 5 - which does not match the conditional)
  948. TestHelpers.AssertDriverAttributesAre (
  949. expectedColors,
  950. Application.Driver,
  951. tv.ColorScheme.Normal,
  952. focused ? tv.ColorScheme.Focus : tv.ColorScheme.HotNormal
  953. );
  954. }
  955. [Theory]
  956. [SetupFakeDriver]
  957. [InlineData (false)]
  958. [InlineData (true)]
  959. public void TableView_ColorTests_FocusedOrNot (bool focused)
  960. {
  961. TableView tv = SetUpMiniTable ();
  962. tv.LayoutSubviews ();
  963. // width exactly matches the max col widths
  964. tv.Viewport = new (0, 0, 5, 4);
  965. tv.HasFocus = focused;
  966. Assert.Equal (focused, tv.HasFocus);
  967. tv.Draw ();
  968. var expected = @"
  969. ┌─┬─┐
  970. │A│B│
  971. ├─┼─┤
  972. │1│2│
  973. ";
  974. TestHelpers.AssertDriverContentsAre (expected, output);
  975. var expectedColors = @"
  976. 00000
  977. 00000
  978. 00000
  979. 01000
  980. ";
  981. TestHelpers.AssertDriverAttributesAre (
  982. expectedColors,
  983. Application.Driver,
  984. tv.ColorScheme.Normal,
  985. focused ? tv.ColorScheme.Focus : tv.ColorScheme.HotNormal
  986. );
  987. }
  988. [Theory]
  989. [SetupFakeDriver]
  990. [InlineData (false)]
  991. [InlineData (true)]
  992. public void TableView_ColorTests_InvertSelectedCellFirstCharacter (bool focused)
  993. {
  994. TableView tv = SetUpMiniTable ();
  995. tv.Style.InvertSelectedCellFirstCharacter = true;
  996. tv.LayoutSubviews ();
  997. // width exactly matches the max col widths
  998. tv.Viewport = new (0, 0, 5, 4);
  999. tv.HasFocus = focused;
  1000. Assert.Equal (focused, tv.HasFocus);
  1001. tv.Draw ();
  1002. var expected = @"
  1003. ┌─┬─┐
  1004. │A│B│
  1005. ├─┼─┤
  1006. │1│2│
  1007. ";
  1008. TestHelpers.AssertDriverContentsAre (expected, output);
  1009. var expectedColors = @"
  1010. 00000
  1011. 00000
  1012. 00000
  1013. 01000
  1014. ";
  1015. var invertFocus = new Attribute (tv.ColorScheme.Focus.Background, tv.ColorScheme.Focus.Foreground);
  1016. var invertHotNormal = new Attribute (tv.ColorScheme.HotNormal.Background, tv.ColorScheme.HotNormal.Foreground);
  1017. TestHelpers.AssertDriverAttributesAre (
  1018. expectedColors,
  1019. Application.Driver,
  1020. tv.ColorScheme.Normal,
  1021. focused ? invertFocus : invertHotNormal
  1022. );
  1023. }
  1024. [Fact]
  1025. [SetupFakeDriver]
  1026. public void TableView_ExpandLastColumn_False ()
  1027. {
  1028. TableView tv = SetUpMiniTable ();
  1029. // the thing we are testing
  1030. tv.Style.ExpandLastColumn = false;
  1031. tv.Draw ();
  1032. var expected = @"
  1033. ┌─┬─┬────┐
  1034. │A│B│ │
  1035. ├─┼─┼────┤
  1036. │1│2│ │
  1037. ";
  1038. TestHelpers.AssertDriverContentsAre (expected, output);
  1039. }
  1040. [Fact]
  1041. [SetupFakeDriver]
  1042. public void TableView_ExpandLastColumn_False_ExactBounds ()
  1043. {
  1044. TableView tv = SetUpMiniTable ();
  1045. // the thing we are testing
  1046. tv.Style.ExpandLastColumn = false;
  1047. // width exactly matches the max col widths
  1048. tv.Viewport = new (0, 0, 5, 4);
  1049. tv.Draw ();
  1050. var expected = @"
  1051. ┌─┬─┐
  1052. │A│B│
  1053. ├─┼─┤
  1054. │1│2│
  1055. ";
  1056. TestHelpers.AssertDriverContentsAre (expected, output);
  1057. }
  1058. [Fact]
  1059. [SetupFakeDriver]
  1060. public void TableView_ExpandLastColumn_True ()
  1061. {
  1062. TableView tv = SetUpMiniTable ();
  1063. // the thing we are testing
  1064. tv.Style.ExpandLastColumn = true;
  1065. tv.Draw ();
  1066. var expected = @"
  1067. ┌─┬──────┐
  1068. │A│B │
  1069. ├─┼──────┤
  1070. │1│2 │
  1071. ";
  1072. TestHelpers.AssertDriverContentsAre (expected, output);
  1073. }
  1074. [Fact]
  1075. [SetupFakeDriver]
  1076. public void TableView_ShowHeadersFalse_AllLines ()
  1077. {
  1078. TableView tv = GetABCDEFTableView (out _);
  1079. tv.Viewport = new (0, 0, 5, 5);
  1080. tv.Style.ShowHeaders = false;
  1081. tv.Style.ShowHorizontalHeaderOverline = true;
  1082. tv.Style.ShowHorizontalHeaderUnderline = true;
  1083. // Horizontal scrolling option is part of the underline
  1084. tv.Style.ShowHorizontalScrollIndicators = true;
  1085. tv.Draw ();
  1086. var expected = @"
  1087. ┌─┬─┐
  1088. ├─┼─►
  1089. │1│2│
  1090. ";
  1091. TestHelpers.AssertDriverContentsAre (expected, output);
  1092. }
  1093. [Fact]
  1094. [SetupFakeDriver]
  1095. public void TableView_ShowHeadersFalse_AndNoHeaderLines ()
  1096. {
  1097. TableView tv = GetABCDEFTableView (out _);
  1098. tv.Viewport = new (0, 0, 5, 5);
  1099. tv.Style.ShowHeaders = false;
  1100. tv.Style.ShowHorizontalHeaderOverline = false;
  1101. tv.Style.ShowHorizontalHeaderUnderline = false;
  1102. tv.Draw ();
  1103. var expected = @"
  1104. │1│2│
  1105. ";
  1106. TestHelpers.AssertDriverContentsAre (expected, output);
  1107. }
  1108. [Fact]
  1109. [SetupFakeDriver]
  1110. public void TableView_ShowHeadersFalse_OverlineTrue ()
  1111. {
  1112. TableView tv = GetABCDEFTableView (out _);
  1113. tv.Viewport = new (0, 0, 5, 5);
  1114. tv.Style.ShowHeaders = false;
  1115. tv.Style.ShowHorizontalHeaderOverline = true;
  1116. tv.Style.ShowHorizontalHeaderUnderline = false;
  1117. tv.Draw ();
  1118. var expected = @"
  1119. ┌─┬─┐
  1120. │1│2│
  1121. ";
  1122. TestHelpers.AssertDriverContentsAre (expected, output);
  1123. }
  1124. [Fact]
  1125. [SetupFakeDriver]
  1126. public void TableView_ShowHeadersFalse_UnderlineTrue ()
  1127. {
  1128. TableView tv = GetABCDEFTableView (out _);
  1129. tv.Viewport = new (0, 0, 5, 5);
  1130. tv.Style.ShowHeaders = false;
  1131. tv.Style.ShowHorizontalHeaderOverline = false;
  1132. tv.Style.ShowHorizontalHeaderUnderline = true;
  1133. // Horizontal scrolling option is part of the underline
  1134. tv.Style.ShowHorizontalScrollIndicators = true;
  1135. tv.Draw ();
  1136. var expected = @"
  1137. ├─┼─►
  1138. │1│2│
  1139. ";
  1140. TestHelpers.AssertDriverContentsAre (expected, output);
  1141. }
  1142. [Fact]
  1143. [SetupFakeDriver]
  1144. public void TableViewMultiSelect_CannotFallOffBottom ()
  1145. {
  1146. TableView tv = SetUpMiniTable (out DataTable dt);
  1147. dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  1148. tv.MultiSelect = true;
  1149. tv.SelectedColumn = 0;
  1150. tv.SelectedRow = 0;
  1151. tv.NewKeyDownEvent (Key.CursorRight.WithShift);
  1152. tv.NewKeyDownEvent (Key.CursorDown.WithShift);
  1153. Assert.Equal (new (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rectangle);
  1154. // this next moves should be ignored because we already selected the whole table
  1155. tv.NewKeyDownEvent (Key.CursorRight.WithShift);
  1156. tv.NewKeyDownEvent (Key.CursorDown.WithShift);
  1157. Assert.Equal (new (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rectangle);
  1158. Assert.Equal (1, tv.SelectedColumn);
  1159. Assert.Equal (1, tv.SelectedRow);
  1160. }
  1161. [Fact]
  1162. [SetupFakeDriver]
  1163. public void TableViewMultiSelect_CannotFallOffLeft ()
  1164. {
  1165. TableView tv = SetUpMiniTable (out DataTable dt);
  1166. dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  1167. tv.MultiSelect = true;
  1168. tv.SelectedColumn = 1;
  1169. tv.SelectedRow = 1;
  1170. tv.NewKeyDownEvent (Key.CursorLeft.WithShift);
  1171. Assert.Equal (new (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rectangle);
  1172. // this next shift left should be ignored because we are already at the bounds
  1173. tv.NewKeyDownEvent (Key.CursorLeft.WithShift);
  1174. Assert.Equal (new (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rectangle);
  1175. Assert.Equal (0, tv.SelectedColumn);
  1176. Assert.Equal (1, tv.SelectedRow);
  1177. }
  1178. [Fact]
  1179. [SetupFakeDriver]
  1180. public void TableViewMultiSelect_CannotFallOffRight ()
  1181. {
  1182. TableView tv = SetUpMiniTable (out DataTable dt);
  1183. dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  1184. tv.MultiSelect = true;
  1185. tv.SelectedColumn = 0;
  1186. tv.SelectedRow = 1;
  1187. tv.NewKeyDownEvent (Key.CursorRight.WithShift);
  1188. Assert.Equal (new (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rectangle);
  1189. // this next shift right should be ignored because we are already at the right bounds
  1190. tv.NewKeyDownEvent (Key.CursorRight.WithShift);
  1191. Assert.Equal (new (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rectangle);
  1192. Assert.Equal (1, tv.SelectedColumn);
  1193. Assert.Equal (1, tv.SelectedRow);
  1194. }
  1195. [Fact]
  1196. [SetupFakeDriver]
  1197. public void TableViewMultiSelect_CannotFallOffTop ()
  1198. {
  1199. TableView tv = SetUpMiniTable (out DataTable dt);
  1200. dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  1201. tv.LayoutSubviews ();
  1202. tv.MultiSelect = true;
  1203. tv.SelectedColumn = 1;
  1204. tv.SelectedRow = 1;
  1205. tv.NewKeyDownEvent (Key.CursorLeft.WithShift);
  1206. tv.NewKeyDownEvent (Key.CursorUp.WithShift);
  1207. Assert.Equal (new (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rectangle);
  1208. // this next moves should be ignored because we already selected the whole table
  1209. tv.NewKeyDownEvent (Key.CursorLeft.WithShift);
  1210. tv.NewKeyDownEvent (Key.CursorUp.WithShift);
  1211. Assert.Equal (new (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rectangle);
  1212. Assert.Equal (0, tv.SelectedColumn);
  1213. Assert.Equal (0, tv.SelectedRow);
  1214. }
  1215. [Fact]
  1216. [AutoInitShutdown]
  1217. public void Test_CollectionNavigator ()
  1218. {
  1219. var tv = new TableView ();
  1220. tv.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  1221. tv.Viewport = new (0, 0, 50, 7);
  1222. tv.Table = new EnumerableTableSource<string> (
  1223. new [] { "fish", "troll", "trap", "zoo" },
  1224. new () { { "Name", t => t }, { "EndsWith", t => t.Last () } }
  1225. );
  1226. tv.LayoutSubviews ();
  1227. tv.Draw ();
  1228. var expected =
  1229. @"
  1230. ┌─────┬──────────────────────────────────────────┐
  1231. │Name │EndsWith │
  1232. ├─────┼──────────────────────────────────────────┤
  1233. │fish │h │
  1234. │troll│l │
  1235. │trap │p │
  1236. │zoo │o │";
  1237. TestHelpers.AssertDriverContentsAre (expected, output);
  1238. Assert.Equal (0, tv.SelectedRow);
  1239. // this test assumes no focus
  1240. Assert.False (tv.HasFocus);
  1241. // already on fish
  1242. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.F });
  1243. Assert.Equal (0, tv.SelectedRow);
  1244. // not focused
  1245. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.Z });
  1246. Assert.Equal (0, tv.SelectedRow);
  1247. // ensure that TableView has the input focus
  1248. var top = new Toplevel ();
  1249. top.Add (tv);
  1250. Application.Begin (top);
  1251. top.FocusDeepest (NavigationDirection.Forward, null);
  1252. Assert.True (tv.HasFocus);
  1253. // already on fish
  1254. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.F });
  1255. Assert.Equal (0, tv.SelectedRow);
  1256. // move to zoo
  1257. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.Z });
  1258. Assert.Equal (3, tv.SelectedRow);
  1259. // move to troll
  1260. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.T });
  1261. Assert.Equal (1, tv.SelectedRow);
  1262. // move to trap
  1263. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.T });
  1264. Assert.Equal (2, tv.SelectedRow);
  1265. // change columns to navigate by column 2
  1266. Assert.Equal (0, tv.SelectedColumn);
  1267. Assert.Equal (2, tv.SelectedRow);
  1268. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  1269. Assert.Equal (1, tv.SelectedColumn);
  1270. Assert.Equal (2, tv.SelectedRow);
  1271. // nothing ends with t so stay where you are
  1272. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.T });
  1273. Assert.Equal (2, tv.SelectedRow);
  1274. //jump to fish which ends in h
  1275. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.H });
  1276. Assert.Equal (0, tv.SelectedRow);
  1277. // jump to zoo which ends in o
  1278. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.O });
  1279. Assert.Equal (3, tv.SelectedRow);
  1280. top.Dispose ();
  1281. }
  1282. [Fact]
  1283. [SetupFakeDriver]
  1284. public void Test_ScreenToCell ()
  1285. {
  1286. TableView tableView = GetTwoRowSixColumnTable ();
  1287. tableView.BeginInit ();
  1288. tableView.EndInit ();
  1289. tableView.LayoutSubviews ();
  1290. tableView.Draw ();
  1291. // user can only scroll right so sees right indicator
  1292. // Because first column in table is A
  1293. var expected =
  1294. @"
  1295. │A│B│C│
  1296. ├─┼─┼─►
  1297. │1│2│3│
  1298. │1│2│3│";
  1299. TestHelpers.AssertDriverContentsAre (expected, output);
  1300. // ---------------- X=0 -----------------------
  1301. // click is before first cell
  1302. Assert.Null (tableView.ScreenToCell (0, 0));
  1303. Assert.Null (tableView.ScreenToCell (0, 1));
  1304. Assert.Null (tableView.ScreenToCell (0, 2));
  1305. Assert.Null (tableView.ScreenToCell (0, 3));
  1306. Assert.Null (tableView.ScreenToCell (0, 4));
  1307. // ---------------- X=1 -----------------------
  1308. // click in header
  1309. Assert.Null (tableView.ScreenToCell (1, 0));
  1310. // click in header row line
  1311. Assert.Null (tableView.ScreenToCell (1, 1));
  1312. // click in cell 0,0
  1313. Assert.Equal (Point.Empty, tableView.ScreenToCell (1, 2));
  1314. // click in cell 0,1
  1315. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3));
  1316. // after last row
  1317. Assert.Null (tableView.ScreenToCell (1, 4));
  1318. // ---------------- X=2 -----------------------
  1319. // ( even though there is a horizontal dividing line here we treat it as a hit on the cell before)
  1320. // click in header
  1321. Assert.Null (tableView.ScreenToCell (2, 0));
  1322. // click in header row line
  1323. Assert.Null (tableView.ScreenToCell (2, 1));
  1324. // click in cell 0,0
  1325. Assert.Equal (Point.Empty, tableView.ScreenToCell (2, 2));
  1326. // click in cell 0,1
  1327. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3));
  1328. // after last row
  1329. Assert.Null (tableView.ScreenToCell (2, 4));
  1330. // ---------------- X=3 -----------------------
  1331. // click in header
  1332. Assert.Null (tableView.ScreenToCell (3, 0));
  1333. // click in header row line
  1334. Assert.Null (tableView.ScreenToCell (3, 1));
  1335. // click in cell 1,0
  1336. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2));
  1337. // click in cell 1,1
  1338. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3));
  1339. // after last row
  1340. Assert.Null (tableView.ScreenToCell (3, 4));
  1341. }
  1342. [Fact]
  1343. [SetupFakeDriver]
  1344. public void Test_ScreenToCell_DataColumnOverload ()
  1345. {
  1346. TableView tableView = GetTwoRowSixColumnTable ();
  1347. tableView.LayoutSubviews ();
  1348. tableView.Draw ();
  1349. // user can only scroll right so sees right indicator
  1350. // Because first column in table is A
  1351. var expected =
  1352. @"
  1353. │A│B│C│
  1354. ├─┼─┼─►
  1355. │1│2│3│
  1356. │1│2│3│";
  1357. TestHelpers.AssertDriverContentsAre (expected, output);
  1358. int? col;
  1359. // ---------------- X=0 -----------------------
  1360. // click is before first cell
  1361. Assert.Null (tableView.ScreenToCell (0, 0, out col));
  1362. Assert.Null (col);
  1363. Assert.Null (tableView.ScreenToCell (0, 1, out col));
  1364. Assert.Null (col);
  1365. Assert.Null (tableView.ScreenToCell (0, 2, out col));
  1366. Assert.Null (col);
  1367. Assert.Null (tableView.ScreenToCell (0, 3, out col));
  1368. Assert.Null (col);
  1369. Assert.Null (tableView.ScreenToCell (0, 4, out col));
  1370. Assert.Null (col);
  1371. // ---------------- X=1 -----------------------
  1372. // click in header
  1373. Assert.Null (tableView.ScreenToCell (1, 0, out col));
  1374. Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
  1375. // click in header row line (click in the horizontal line below header counts as click in header above - consistent with the column hit box)
  1376. Assert.Null (tableView.ScreenToCell (1, 1, out col));
  1377. Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
  1378. // click in cell 0,0
  1379. Assert.Equal (Point.Empty, tableView.ScreenToCell (1, 2, out col));
  1380. Assert.Null (col);
  1381. // click in cell 0,1
  1382. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3, out col));
  1383. Assert.Null (col);
  1384. // after last row
  1385. Assert.Null (tableView.ScreenToCell (1, 4, out col));
  1386. Assert.Null (col);
  1387. // ---------------- X=2 -----------------------
  1388. // click in header
  1389. Assert.Null (tableView.ScreenToCell (2, 0, out col));
  1390. Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
  1391. // click in header row line
  1392. Assert.Null (tableView.ScreenToCell (2, 1, out col));
  1393. Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
  1394. // click in cell 0,0
  1395. Assert.Equal (Point.Empty, tableView.ScreenToCell (2, 2, out col));
  1396. Assert.Null (col);
  1397. // click in cell 0,1
  1398. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3, out col));
  1399. Assert.Null (col);
  1400. // after last row
  1401. Assert.Null (tableView.ScreenToCell (2, 4, out col));
  1402. Assert.Null (col);
  1403. // ---------------- X=3 -----------------------
  1404. // click in header
  1405. Assert.Null (tableView.ScreenToCell (3, 0, out col));
  1406. Assert.Equal ("B", tableView.Table.ColumnNames [col.Value]);
  1407. // click in header row line
  1408. Assert.Null (tableView.ScreenToCell (3, 1, out col));
  1409. Assert.Equal ("B", tableView.Table.ColumnNames [col.Value]);
  1410. // click in cell 1,0
  1411. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2, out col));
  1412. Assert.Null (col);
  1413. // click in cell 1,1
  1414. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3, out col));
  1415. Assert.Null (col);
  1416. // after last row
  1417. Assert.Null (tableView.ScreenToCell (3, 4, out col));
  1418. Assert.Null (col);
  1419. }
  1420. [Fact]
  1421. public void Test_SumColumnWidth_UnicodeLength ()
  1422. {
  1423. Assert.Equal (11, "hello there".EnumerateRunes ().Sum (c => c.GetColumns ()));
  1424. // Creates a string with the peculiar (french?) r symbol
  1425. string surrogate = "Les Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
  1426. // The unicode width of this string is shorter than the string length!
  1427. Assert.Equal (14, surrogate.EnumerateRunes ().Sum (c => c.GetColumns ()));
  1428. Assert.Equal (15, surrogate.Length);
  1429. }
  1430. [Fact]
  1431. [SetupFakeDriver]
  1432. public void TestColumnStyle_AllColumnsVisibleFalse_BehavesAsTableNull ()
  1433. {
  1434. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1435. for (var i = 0; i < 6; i++)
  1436. {
  1437. tableView.Style.GetOrCreateColumnStyle (i).Visible = false;
  1438. }
  1439. tableView.LayoutSubviews ();
  1440. // expect nothing to be rendered when all columns are invisible
  1441. var expected =
  1442. @"
  1443. ";
  1444. tableView.Draw ();
  1445. TestHelpers.AssertDriverContentsAre (expected, output);
  1446. // expect behavior to match when Table is null
  1447. tableView.Table = null;
  1448. tableView.Draw ();
  1449. TestHelpers.AssertDriverContentsAre (expected, output);
  1450. }
  1451. [InlineData (true)]
  1452. [InlineData (false)]
  1453. [Theory]
  1454. [SetupFakeDriver]
  1455. public void TestColumnStyle_FirstColumnVisibleFalse_CursorStaysAt1 (bool useHome)
  1456. {
  1457. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1458. tableView.LayoutSubviews ();
  1459. tableView.Style.GetOrCreateColumnStyle (0).Visible = false;
  1460. tableView.SelectedColumn = 0;
  1461. Assert.Equal (0, tableView.SelectedColumn);
  1462. // column 0 is invisible so this method should move to 1
  1463. tableView.EnsureValidSelection ();
  1464. Assert.Equal (1, tableView.SelectedColumn);
  1465. tableView.NewKeyDownEvent (
  1466. new () { KeyCode = useHome ? KeyCode.Home : KeyCode.CursorLeft }
  1467. );
  1468. // Expect the cursor to stay at 1
  1469. Assert.Equal (1, tableView.SelectedColumn);
  1470. }
  1471. [Fact]
  1472. [SetupFakeDriver]
  1473. public void TestColumnStyle_FirstColumnVisibleFalse_IsNotRendered ()
  1474. {
  1475. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1476. tableView.Style.ShowHorizontalScrollIndicators = true;
  1477. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1478. tableView.Style.GetOrCreateColumnStyle (0).Visible = false;
  1479. tableView.LayoutSubviews ();
  1480. tableView.Draw ();
  1481. var expected =
  1482. @"
  1483. │B│C│D│
  1484. ├─┼─┼─►
  1485. │2│3│4│";
  1486. TestHelpers.AssertDriverContentsAre (expected, output);
  1487. }
  1488. [InlineData (true)]
  1489. [InlineData (false)]
  1490. [Theory]
  1491. [SetupFakeDriver]
  1492. public void TestColumnStyle_LastColumnVisibleFalse_CursorStaysAt2 (bool useEnd)
  1493. {
  1494. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1495. tableView.LayoutSubviews ();
  1496. // select D
  1497. tableView.SelectedColumn = 3;
  1498. Assert.Equal (3, tableView.SelectedColumn);
  1499. tableView.Style.GetOrCreateColumnStyle (3).Visible = false;
  1500. tableView.Style.GetOrCreateColumnStyle (4).Visible = false;
  1501. tableView.Style.GetOrCreateColumnStyle (5).Visible = false;
  1502. // column D is invisible so this method should move to 2 (C)
  1503. tableView.EnsureValidSelection ();
  1504. Assert.Equal (2, tableView.SelectedColumn);
  1505. tableView.NewKeyDownEvent (
  1506. new () { KeyCode = useEnd ? KeyCode.End : KeyCode.CursorRight }
  1507. );
  1508. // Expect the cursor to stay at 2
  1509. Assert.Equal (2, tableView.SelectedColumn);
  1510. }
  1511. [Fact]
  1512. [SetupFakeDriver]
  1513. public void TestColumnStyle_PreceedingColumnsInvisible_NoScrollIndicator ()
  1514. {
  1515. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1516. tableView.Style.ShowHorizontalScrollIndicators = true;
  1517. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1518. tableView.ColumnOffset = 1;
  1519. tableView.LayoutSubviews ();
  1520. tableView.Draw ();
  1521. // normally we should have scroll indicators because A,E and F are of screen
  1522. var expected =
  1523. @"
  1524. │B│C│D│
  1525. ◄─┼─┼─►
  1526. │2│3│4│";
  1527. TestHelpers.AssertDriverContentsAre (expected, output);
  1528. // but if E and F are invisible so we shouldn't show right
  1529. tableView.Style.GetOrCreateColumnStyle (4).Visible = false;
  1530. tableView.Style.GetOrCreateColumnStyle (5).Visible = false;
  1531. expected =
  1532. @"
  1533. │B│C│D│
  1534. ◄─┼─┼─┤
  1535. │2│3│4│";
  1536. tableView.Draw ();
  1537. TestHelpers.AssertDriverContentsAre (expected, output);
  1538. // now also A is invisible so we cannot scroll in either direction
  1539. tableView.Style.GetOrCreateColumnStyle (0).Visible = false;
  1540. expected =
  1541. @"
  1542. │B│C│D│
  1543. ├─┼─┼─┤
  1544. │2│3│4│";
  1545. tableView.Draw ();
  1546. TestHelpers.AssertDriverContentsAre (expected, output);
  1547. }
  1548. [Fact]
  1549. [SetupFakeDriver]
  1550. public void TestColumnStyle_RemainingColumnsInvisible_NoScrollIndicator ()
  1551. {
  1552. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1553. tableView.Style.ShowHorizontalScrollIndicators = true;
  1554. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1555. tableView.LayoutSubviews ();
  1556. tableView.Draw ();
  1557. // normally we should have scroll indicators because DEF are of screen
  1558. var expected =
  1559. @"
  1560. │A│B│C│
  1561. ├─┼─┼─►
  1562. │1│2│3│";
  1563. TestHelpers.AssertDriverContentsAre (expected, output);
  1564. // but if DEF are invisible we shouldn't be showing the indicator
  1565. tableView.Style.GetOrCreateColumnStyle (3).Visible = false;
  1566. tableView.Style.GetOrCreateColumnStyle (4).Visible = false;
  1567. tableView.Style.GetOrCreateColumnStyle (5).Visible = false;
  1568. expected =
  1569. @"
  1570. │A│B│C│
  1571. ├─┼─┼─┤
  1572. │1│2│3│";
  1573. tableView.Draw ();
  1574. TestHelpers.AssertDriverContentsAre (expected, output);
  1575. }
  1576. [Fact]
  1577. [SetupFakeDriver]
  1578. public void TestColumnStyle_VisibleFalse_CursorStepsOverInvisibleColumns ()
  1579. {
  1580. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1581. tableView.LayoutSubviews ();
  1582. tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
  1583. tableView.SelectedColumn = 0;
  1584. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  1585. // Expect the cursor navigation to skip over the invisible column(s)
  1586. Assert.Equal (2, tableView.SelectedColumn);
  1587. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorLeft });
  1588. // Expect the cursor navigation backwards to skip over invisible column too
  1589. Assert.Equal (0, tableView.SelectedColumn);
  1590. }
  1591. [Theory]
  1592. [SetupFakeDriver]
  1593. [InlineData (true, true)]
  1594. [InlineData (false, true)]
  1595. [InlineData (true, false)]
  1596. [InlineData (false, false)]
  1597. public void TestColumnStyle_VisibleFalse_DoesNotEffect_EnsureSelectedCellIsVisible (
  1598. bool smooth,
  1599. bool invisibleCol
  1600. )
  1601. {
  1602. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1603. tableView.LayoutSubviews ();
  1604. tableView.Style.SmoothHorizontalScrolling = smooth;
  1605. if (invisibleCol)
  1606. {
  1607. tableView.Style.GetOrCreateColumnStyle (3).Visible = false;
  1608. }
  1609. // New TableView should have first cell selected
  1610. Assert.Equal (0, tableView.SelectedColumn);
  1611. // With no scrolling
  1612. Assert.Equal (0, tableView.ColumnOffset);
  1613. // A,B and C are visible on screen at the moment so these should have no effect
  1614. tableView.SelectedColumn = 1;
  1615. tableView.EnsureSelectedCellIsVisible ();
  1616. Assert.Equal (0, tableView.ColumnOffset);
  1617. tableView.SelectedColumn = 2;
  1618. tableView.EnsureSelectedCellIsVisible ();
  1619. Assert.Equal (0, tableView.ColumnOffset);
  1620. // Selecting D should move the visible table area to fit D onto the screen
  1621. tableView.SelectedColumn = 3;
  1622. tableView.EnsureSelectedCellIsVisible ();
  1623. Assert.Equal (smooth ? 1 : 3, tableView.ColumnOffset);
  1624. }
  1625. [Fact]
  1626. [SetupFakeDriver]
  1627. public void TestColumnStyle_VisibleFalse_IsNotRendered ()
  1628. {
  1629. TableView tableView = GetABCDEFTableView (out _);
  1630. tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
  1631. tableView.LayoutSubviews ();
  1632. tableView.Draw ();
  1633. var expected =
  1634. @"
  1635. │A│C│D│
  1636. │1│3│4│";
  1637. TestHelpers.AssertDriverContentsAre (expected, output);
  1638. }
  1639. [Fact]
  1640. [SetupFakeDriver]
  1641. public void TestColumnStyle_VisibleFalse_MultiSelected ()
  1642. {
  1643. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1644. tableView.LayoutSubviews ();
  1645. // user has rectangular selection
  1646. tableView.MultiSelectedRegions.Push (
  1647. new (
  1648. Point.Empty,
  1649. new (0, 0, 3, 1)
  1650. )
  1651. );
  1652. Assert.Equal (3, tableView.GetAllSelectedCells ().Count ());
  1653. Assert.True (tableView.IsSelected (0, 0));
  1654. Assert.True (tableView.IsSelected (1, 0));
  1655. Assert.True (tableView.IsSelected (2, 0));
  1656. Assert.False (tableView.IsSelected (3, 0));
  1657. // if middle column is invisible
  1658. tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
  1659. // it should not be included in the selection
  1660. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1661. Assert.True (tableView.IsSelected (0, 0));
  1662. Assert.False (tableView.IsSelected (1, 0));
  1663. Assert.True (tableView.IsSelected (2, 0));
  1664. Assert.False (tableView.IsSelected (3, 0));
  1665. Assert.DoesNotContain (new (1, 0), tableView.GetAllSelectedCells ());
  1666. }
  1667. [Fact]
  1668. [SetupFakeDriver]
  1669. public void TestColumnStyle_VisibleFalse_MultiSelectingStepsOverInvisibleColumns ()
  1670. {
  1671. TableView tableView = GetABCDEFTableView (out _);
  1672. tableView.LayoutSubviews ();
  1673. // if middle column is invisible
  1674. tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
  1675. tableView.NewKeyDownEvent (Key.CursorRight.WithShift);
  1676. // Selection should extend from A to C but skip B
  1677. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1678. Assert.True (tableView.IsSelected (0, 0));
  1679. Assert.False (tableView.IsSelected (1, 0));
  1680. Assert.True (tableView.IsSelected (2, 0));
  1681. Assert.False (tableView.IsSelected (3, 0));
  1682. Assert.DoesNotContain (new (1, 0), tableView.GetAllSelectedCells ());
  1683. }
  1684. [Fact]
  1685. [SetupFakeDriver]
  1686. public void TestControlClick_MultiSelect_ThreeRowTable_FullRowSelect ()
  1687. {
  1688. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  1689. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1690. tv.LayoutSubviews ();
  1691. tv.MultiSelect = true;
  1692. // Clicking in bottom row
  1693. tv.NewMouseEvent (
  1694. new () { Position = new (1, 4), Flags = MouseFlags.Button1Clicked }
  1695. );
  1696. // should select that row
  1697. Assert.Equal (2, tv.SelectedRow);
  1698. // shift clicking top row
  1699. tv.NewMouseEvent (
  1700. new () { Position = new (1, 2), Flags = MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl }
  1701. );
  1702. // should extend the selection
  1703. // to include bottom and top row but not middle
  1704. Assert.Equal (0, tv.SelectedRow);
  1705. Point [] selected = tv.GetAllSelectedCells ().ToArray ();
  1706. Assert.Contains (Point.Empty, selected);
  1707. Assert.DoesNotContain (new (0, 1), selected);
  1708. Assert.Contains (new (0, 2), selected);
  1709. }
  1710. [Fact]
  1711. [SetupFakeDriver]
  1712. public void TestEnumerableDataSource_BasicTypes ()
  1713. {
  1714. ((FakeDriver)Application.Driver!).SetBufferSize (100, 100);
  1715. var tv = new TableView ();
  1716. tv.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  1717. tv.Viewport = new (0, 0, 50, 6);
  1718. tv.Table = new EnumerableTableSource<Type> (
  1719. new [] { typeof (string), typeof (int), typeof (float) },
  1720. new ()
  1721. {
  1722. { "Name", t => t.Name }, { "Namespace", t => t.Namespace },
  1723. { "BaseType", t => t.BaseType }
  1724. }
  1725. );
  1726. tv.LayoutSubviews ();
  1727. tv.Draw ();
  1728. var expected =
  1729. @"
  1730. ┌──────┬─────────┬───────────────────────────────┐
  1731. │Name │Namespace│BaseType │
  1732. ├──────┼─────────┼───────────────────────────────┤
  1733. │String│System │System.Object │
  1734. │Int32 │System │System.ValueType │
  1735. │Single│System │System.ValueType │";
  1736. TestHelpers.AssertDriverContentsAre (expected, output);
  1737. }
  1738. [Fact]
  1739. [SetupFakeDriver]
  1740. public void TestFullRowSelect_AlwaysUseNormalColorForVerticalCellLines ()
  1741. {
  1742. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  1743. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1744. tv.Viewport = new (0, 0, 7, 6);
  1745. tv.Frame = new (0, 0, 7, 6);
  1746. tv.LayoutSubviews ();
  1747. tv.FullRowSelect = true;
  1748. tv.Style.ShowHorizontalBottomline = true;
  1749. tv.Style.AlwaysUseNormalColorForVerticalCellLines = true;
  1750. // Clicking in bottom row
  1751. tv.NewMouseEvent (
  1752. new () { Position = new (1, 4), Flags = MouseFlags.Button1Clicked }
  1753. );
  1754. // should select that row
  1755. Assert.Equal (2, tv.SelectedRow);
  1756. tv.OnDrawContent (tv.Viewport);
  1757. var expected =
  1758. @"
  1759. │A│B│C│
  1760. ├─┼─┼─►
  1761. │1│2│3│
  1762. │1│2│3│
  1763. │1│2│3│
  1764. └─┴─┴─┘";
  1765. TestHelpers.AssertDriverContentsAre (expected, output);
  1766. Attribute normal = tv.ColorScheme.Normal;
  1767. tv.ColorScheme = new (tv.ColorScheme) { Focus = new (Color.Magenta, Color.White) };
  1768. Attribute focus = tv.ColorScheme.Focus;
  1769. tv.Draw ();
  1770. // Focus color (1) should be used for cells only because
  1771. // AlwaysUseNormalColorForVerticalCellLines is true
  1772. expected =
  1773. @"
  1774. 0000000
  1775. 0000000
  1776. 0000000
  1777. 0000000
  1778. 0101010
  1779. 0000000";
  1780. TestHelpers.AssertDriverAttributesAre (expected, Application.Driver, normal, focus);
  1781. }
  1782. [Fact]
  1783. [SetupFakeDriver]
  1784. public void TestFullRowSelect_SelectionColorDoesNotStop_WhenShowVerticalCellLinesIsFalse ()
  1785. {
  1786. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  1787. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1788. tv.LayoutSubviews ();
  1789. tv.Viewport = new (0, 0, 7, 6);
  1790. tv.FullRowSelect = true;
  1791. tv.Style.ShowVerticalCellLines = false;
  1792. tv.Style.ShowVerticalHeaderLines = false;
  1793. // Clicking in bottom row
  1794. tv.NewMouseEvent (
  1795. new () { Position = new (1, 4), Flags = MouseFlags.Button1Clicked }
  1796. );
  1797. // should select that row
  1798. Assert.Equal (2, tv.SelectedRow);
  1799. tv.Draw ();
  1800. var expected =
  1801. @"
  1802. A B C
  1803. ───────
  1804. 1 2 3
  1805. 1 2 3
  1806. 1 2 3";
  1807. TestHelpers.AssertDriverContentsAre (expected, output);
  1808. Attribute normal = tv.ColorScheme.Normal;
  1809. tv.ColorScheme = new (tv.ColorScheme) { Focus = new (Color.Magenta, Color.White) };
  1810. Attribute focus = tv.ColorScheme.Focus;
  1811. tv.Draw ();
  1812. // Focus color (1) should be used for rendering the selected line
  1813. // Note that because there are no vertical cell lines we use the focus
  1814. // color for the whole row
  1815. expected =
  1816. @"
  1817. 000000
  1818. 000000
  1819. 000000
  1820. 000000
  1821. 111111";
  1822. TestHelpers.AssertDriverAttributesAre (expected, Application.Driver, normal, focus);
  1823. }
  1824. [Fact]
  1825. [SetupFakeDriver]
  1826. public void TestFullRowSelect_SelectionColorStopsAtTableEdge_WithCellLines ()
  1827. {
  1828. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  1829. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1830. tv.Viewport = new (0, 0, 7, 6);
  1831. tv.Frame = new (0, 0, 7, 6);
  1832. tv.LayoutSubviews ();
  1833. tv.FullRowSelect = true;
  1834. tv.Style.ShowHorizontalBottomline = true;
  1835. // Clicking in bottom row
  1836. tv.NewMouseEvent (
  1837. new () { Position = new (1, 4), Flags = MouseFlags.Button1Clicked }
  1838. );
  1839. // should select that row
  1840. Assert.Equal (2, tv.SelectedRow);
  1841. tv.OnDrawContent (tv.Viewport);
  1842. var expected =
  1843. @"
  1844. │A│B│C│
  1845. ├─┼─┼─►
  1846. │1│2│3│
  1847. │1│2│3│
  1848. │1│2│3│
  1849. └─┴─┴─┘";
  1850. TestHelpers.AssertDriverContentsAre (expected, output);
  1851. Attribute normal = tv.ColorScheme.Normal;
  1852. tv.ColorScheme = new (tv.ColorScheme) { Focus = new (Color.Magenta, Color.White) };
  1853. Attribute focus = tv.ColorScheme.Focus;
  1854. tv.Draw ();
  1855. // Focus color (1) should be used for rendering the selected line
  1856. // But should not spill into the borders. Normal color (0) should be
  1857. // used for the rest.
  1858. expected =
  1859. @"
  1860. 0000000
  1861. 0000000
  1862. 0000000
  1863. 0000000
  1864. 0111110
  1865. 0000000";
  1866. TestHelpers.AssertDriverAttributesAre (expected, Application.Driver, normal, focus);
  1867. }
  1868. [Theory]
  1869. [SetupFakeDriver]
  1870. [InlineData (Orientation.Horizontal, false)]
  1871. [InlineData (Orientation.Vertical, false)]
  1872. [InlineData (Orientation.Horizontal, true)]
  1873. [InlineData (Orientation.Vertical, true)]
  1874. public void TestListTableSource (Orientation orient, bool parallel)
  1875. {
  1876. IList list = BuildList (16);
  1877. var tv = new TableView ();
  1878. //tv.BeginInit (); tv.EndInit ();
  1879. tv.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  1880. tv.Viewport = new (0, 0, 25, 4);
  1881. tv.Style = new ()
  1882. {
  1883. ShowHeaders = false, ShowHorizontalHeaderOverline = false, ShowHorizontalHeaderUnderline = false
  1884. };
  1885. var listStyle = new ListColumnStyle { Orientation = orient, ScrollParallel = parallel };
  1886. tv.Table = new ListTableSource (list, tv, listStyle);
  1887. tv.LayoutSubviews ();
  1888. tv.Draw ();
  1889. var horizPerpExpected =
  1890. @"
  1891. │Item 0│Item 1 │
  1892. │Item 2│Item 3 │
  1893. │Item 4│Item 5 │
  1894. │Item 6│Item 7 │";
  1895. var horizParaExpected =
  1896. @"
  1897. │Item 0 │Item 1 │Item 2 │
  1898. │Item 4 │Item 5 │Item 6 │
  1899. │Item 8 │Item 9 │Item 10│
  1900. │Item 12│Item 13│Item 14│";
  1901. var vertPerpExpected =
  1902. @"
  1903. │Item 0│Item 4│Item 8 │
  1904. │Item 1│Item 5│Item 9 │
  1905. │Item 2│Item 6│Item 10 │
  1906. │Item 3│Item 7│Item 11 │";
  1907. var vertParaExpected =
  1908. @"
  1909. │Item 0│Item 8 │
  1910. │Item 1│Item 9 │
  1911. │Item 2│Item 10 │
  1912. │Item 3│Item 11 │";
  1913. string expected;
  1914. if (orient == Orientation.Vertical)
  1915. {
  1916. if (parallel)
  1917. {
  1918. expected = vertParaExpected;
  1919. }
  1920. else
  1921. {
  1922. expected = vertPerpExpected;
  1923. }
  1924. }
  1925. else
  1926. {
  1927. if (parallel)
  1928. {
  1929. expected = horizParaExpected;
  1930. }
  1931. else
  1932. {
  1933. expected = horizPerpExpected;
  1934. }
  1935. }
  1936. TestHelpers.AssertDriverContentsAre (expected, output);
  1937. }
  1938. [InlineData (true)]
  1939. [InlineData (false)]
  1940. [Theory]
  1941. [SetupFakeDriver]
  1942. public void TestMoveStartEnd_WithFullRowSelect (bool withFullRowSelect)
  1943. {
  1944. TableView tableView = GetTwoRowSixColumnTable ();
  1945. tableView.LayoutSubviews ();
  1946. tableView.FullRowSelect = withFullRowSelect;
  1947. tableView.SelectedRow = 1;
  1948. tableView.SelectedColumn = 1;
  1949. tableView.NewKeyDownEvent (Key.Home.WithCtrl);
  1950. if (withFullRowSelect)
  1951. {
  1952. // Should not be any horizontal movement when
  1953. // using navigate to Start/End and FullRowSelect
  1954. Assert.Equal (1, tableView.SelectedColumn);
  1955. Assert.Equal (0, tableView.SelectedRow);
  1956. }
  1957. else
  1958. {
  1959. Assert.Equal (0, tableView.SelectedColumn);
  1960. Assert.Equal (0, tableView.SelectedRow);
  1961. }
  1962. tableView.NewKeyDownEvent (
  1963. new (
  1964. KeyCode.End | KeyCode.CtrlMask
  1965. )
  1966. );
  1967. if (withFullRowSelect)
  1968. {
  1969. Assert.Equal (1, tableView.SelectedColumn);
  1970. Assert.Equal (1, tableView.SelectedRow);
  1971. }
  1972. else
  1973. {
  1974. Assert.Equal (5, tableView.SelectedColumn);
  1975. Assert.Equal (1, tableView.SelectedRow);
  1976. }
  1977. }
  1978. [Fact]
  1979. [SetupFakeDriver]
  1980. public void TestShiftClick_MultiSelect_TwoRowTable_FullRowSelect ()
  1981. {
  1982. TableView tv = GetTwoRowSixColumnTable ();
  1983. tv.LayoutSubviews ();
  1984. tv.MultiSelect = true;
  1985. // Clicking in bottom row
  1986. tv.NewMouseEvent (
  1987. new () { Position = new (1, 3), Flags = MouseFlags.Button1Clicked }
  1988. );
  1989. // should select that row
  1990. Assert.Equal (1, tv.SelectedRow);
  1991. // shift clicking top row
  1992. tv.NewMouseEvent (
  1993. new () { Position = new (1, 2), Flags = MouseFlags.Button1Clicked | MouseFlags.ButtonShift }
  1994. );
  1995. // should extend the selection
  1996. Assert.Equal (0, tv.SelectedRow);
  1997. Point [] selected = tv.GetAllSelectedCells ().ToArray ();
  1998. Assert.Contains (Point.Empty, selected);
  1999. Assert.Contains (new (0, 1), selected);
  2000. }
  2001. [Fact]
  2002. [SetupFakeDriver]
  2003. public void TestTableViewCheckboxes_ByObject ()
  2004. {
  2005. TableView tv = GetPetTable (out EnumerableTableSource<PickablePet> source);
  2006. tv.LayoutSubviews ();
  2007. IReadOnlyCollection<PickablePet> pets = source.Data;
  2008. CheckBoxTableSourceWrapperByObject<PickablePet> wrapper = new (
  2009. tv,
  2010. source,
  2011. p => p.IsPicked,
  2012. (p, b) => p.IsPicked = b
  2013. );
  2014. tv.Table = wrapper;
  2015. tv.Draw ();
  2016. var expected =
  2017. @"
  2018. ┌─┬───────┬─────────────┐
  2019. │ │Name │Kind │
  2020. ├─┼───────┼─────────────┤
  2021. │☐│Tammy │Cat │
  2022. │☐│Tibbles│Cat │
  2023. │☐│Ripper │Dog │";
  2024. TestHelpers.AssertDriverContentsAre (expected, output);
  2025. #pragma warning disable xUnit2029
  2026. Assert.Empty (pets.Where (p => p.IsPicked));
  2027. #pragma warning restore xUnit2029
  2028. tv.NewKeyDownEvent (Key.Space);
  2029. Assert.True (pets.First ().IsPicked);
  2030. tv.Draw ();
  2031. expected =
  2032. @"
  2033. ┌─┬───────┬─────────────┐
  2034. │ │Name │Kind │
  2035. ├─┼───────┼─────────────┤
  2036. │☑│Tammy │Cat │
  2037. │☐│Tibbles│Cat │
  2038. │☐│Ripper │Dog │";
  2039. TestHelpers.AssertDriverContentsAre (expected, output);
  2040. tv.NewKeyDownEvent (Key.CursorDown);
  2041. tv.NewKeyDownEvent (Key.Space);
  2042. Assert.True (pets.ElementAt (0).IsPicked);
  2043. Assert.True (pets.ElementAt (1).IsPicked);
  2044. Assert.False (pets.ElementAt (2).IsPicked);
  2045. tv.Draw ();
  2046. expected =
  2047. @"
  2048. ┌─┬───────┬─────────────┐
  2049. │ │Name │Kind │
  2050. ├─┼───────┼─────────────┤
  2051. │☑│Tammy │Cat │
  2052. │☑│Tibbles│Cat │
  2053. │☐│Ripper │Dog │";
  2054. TestHelpers.AssertDriverContentsAre (expected, output);
  2055. tv.NewKeyDownEvent (Key.CursorUp);
  2056. tv.NewKeyDownEvent (Key.Space);
  2057. Assert.False (pets.ElementAt (0).IsPicked);
  2058. Assert.True (pets.ElementAt (1).IsPicked);
  2059. Assert.False (pets.ElementAt (2).IsPicked);
  2060. tv.Draw ();
  2061. expected =
  2062. @"
  2063. ┌─┬───────┬─────────────┐
  2064. │ │Name │Kind │
  2065. ├─┼───────┼─────────────┤
  2066. │☐│Tammy │Cat │
  2067. │☑│Tibbles│Cat │
  2068. │☐│Ripper │Dog │";
  2069. TestHelpers.AssertDriverContentsAre (expected, output);
  2070. }
  2071. [Fact]
  2072. [SetupFakeDriver]
  2073. public void TestTableViewCheckboxes_MultiSelectIsUnion_WhenToggling ()
  2074. {
  2075. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  2076. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2077. tv.LayoutSubviews ();
  2078. var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
  2079. tv.Table = wrapper;
  2080. wrapper.CheckedRows.Add (0);
  2081. wrapper.CheckedRows.Add (2);
  2082. tv.Draw ();
  2083. var expected =
  2084. @"
  2085. │ │A│B│
  2086. ├─┼─┼─►
  2087. │☑│1│2│
  2088. │☐│1│2│
  2089. │☑│1│2│";
  2090. //toggle top two at once
  2091. tv.NewKeyDownEvent (Key.CursorDown.WithShift);
  2092. Assert.True (tv.IsSelected (0, 0));
  2093. Assert.True (tv.IsSelected (0, 1));
  2094. tv.NewKeyDownEvent (Key.Space);
  2095. // Because at least 1 of the rows is not yet ticked we toggle them all to ticked
  2096. TestHelpers.AssertDriverContentsAre (expected, output);
  2097. Assert.Contains (0, wrapper.CheckedRows);
  2098. Assert.Contains (1, wrapper.CheckedRows);
  2099. Assert.Contains (2, wrapper.CheckedRows);
  2100. Assert.Equal (3, wrapper.CheckedRows.Count);
  2101. tv.Draw ();
  2102. expected =
  2103. @"
  2104. │ │A│B│
  2105. ├─┼─┼─►
  2106. │☑│1│2│
  2107. │☑│1│2│
  2108. │☑│1│2│";
  2109. TestHelpers.AssertDriverContentsAre (expected, output);
  2110. // Untoggle the top 2
  2111. tv.NewKeyDownEvent (Key.Space);
  2112. tv.Draw ();
  2113. expected =
  2114. @"
  2115. │ │A│B│
  2116. ├─┼─┼─►
  2117. │☐│1│2│
  2118. │☐│1│2│
  2119. │☑│1│2│";
  2120. TestHelpers.AssertDriverContentsAre (expected, output);
  2121. Assert.Single (wrapper.CheckedRows, 2);
  2122. }
  2123. [Fact]
  2124. [SetupFakeDriver]
  2125. public void TestTableViewCheckboxes_SelectAllToggle ()
  2126. {
  2127. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  2128. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2129. tv.LayoutSubviews ();
  2130. var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
  2131. tv.Table = wrapper;
  2132. //toggle all cells
  2133. tv.NewKeyDownEvent (Key.A.WithCtrl);
  2134. tv.NewKeyDownEvent (Key.Space);
  2135. tv.Draw ();
  2136. var expected =
  2137. @"
  2138. │ │A│B│
  2139. ├─┼─┼─►
  2140. │☑│1│2│
  2141. │☑│1│2│
  2142. │☑│1│2│";
  2143. TestHelpers.AssertDriverContentsAre (expected, output);
  2144. Assert.Contains (0, wrapper.CheckedRows);
  2145. Assert.Contains (1, wrapper.CheckedRows);
  2146. Assert.Contains (2, wrapper.CheckedRows);
  2147. Assert.Equal (3, wrapper.CheckedRows.Count);
  2148. // Untoggle all again
  2149. tv.NewKeyDownEvent (Key.Space);
  2150. tv.Draw ();
  2151. expected =
  2152. @"
  2153. │ │A│B│
  2154. ├─┼─┼─►
  2155. │☐│1│2│
  2156. │☐│1│2│
  2157. │☐│1│2│";
  2158. TestHelpers.AssertDriverContentsAre (expected, output);
  2159. Assert.Empty (wrapper.CheckedRows);
  2160. }
  2161. [Fact]
  2162. [SetupFakeDriver]
  2163. public void TestTableViewCheckboxes_SelectAllToggle_ByObject ()
  2164. {
  2165. TableView tv = GetPetTable (out EnumerableTableSource<PickablePet> source);
  2166. tv.LayoutSubviews ();
  2167. IReadOnlyCollection<PickablePet> pets = source.Data;
  2168. CheckBoxTableSourceWrapperByObject<PickablePet> wrapper = new (
  2169. tv,
  2170. source,
  2171. p => p.IsPicked,
  2172. (p, b) => p.IsPicked = b
  2173. );
  2174. tv.Table = wrapper;
  2175. Assert.DoesNotContain (pets, p => p.IsPicked);
  2176. //toggle all cells
  2177. tv.NewKeyDownEvent (Key.A.WithCtrl);
  2178. tv.NewKeyDownEvent (Key.Space);
  2179. Assert.True (pets.All (p => p.IsPicked));
  2180. tv.Draw ();
  2181. var expected =
  2182. @"
  2183. ┌─┬───────┬─────────────┐
  2184. │ │Name │Kind │
  2185. ├─┼───────┼─────────────┤
  2186. │☑│Tammy │Cat │
  2187. │☑│Tibbles│Cat │
  2188. │☑│Ripper │Dog │";
  2189. TestHelpers.AssertDriverContentsAre (expected, output);
  2190. tv.NewKeyDownEvent (Key.Space);
  2191. #pragma warning disable xUnit2029
  2192. Assert.Empty (pets.Where (p => p.IsPicked));
  2193. #pragma warning restore xUnit2029
  2194. tv.Draw ();
  2195. expected =
  2196. @"
  2197. ┌─┬───────┬─────────────┐
  2198. │ │Name │Kind │
  2199. ├─┼───────┼─────────────┤
  2200. │☐│Tammy │Cat │
  2201. │☐│Tibbles│Cat │
  2202. │☐│Ripper │Dog │
  2203. ";
  2204. TestHelpers.AssertDriverContentsAre (expected, output);
  2205. }
  2206. [Fact]
  2207. [SetupFakeDriver]
  2208. public void TestTableViewCheckboxes_Simple ()
  2209. {
  2210. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  2211. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2212. tv.LayoutSubviews ();
  2213. var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
  2214. tv.Table = wrapper;
  2215. tv.Draw ();
  2216. var expected =
  2217. @"
  2218. │ │A│B│
  2219. ├─┼─┼─►
  2220. │☐│1│2│
  2221. │☐│1│2│
  2222. │☐│1│2│";
  2223. TestHelpers.AssertDriverContentsAre (expected, output);
  2224. Assert.Empty (wrapper.CheckedRows);
  2225. //toggle the top cell
  2226. tv.NewKeyDownEvent (Key.Space);
  2227. Assert.Single (wrapper.CheckedRows, 0);
  2228. tv.Draw ();
  2229. expected =
  2230. @"
  2231. │ │A│B│
  2232. ├─┼─┼─►
  2233. │☑│1│2│
  2234. │☐│1│2│
  2235. │☐│1│2│";
  2236. TestHelpers.AssertDriverContentsAre (expected, output);
  2237. tv.NewKeyDownEvent (Key.CursorDown);
  2238. tv.NewKeyDownEvent (Key.Space);
  2239. Assert.Contains (0, wrapper.CheckedRows);
  2240. Assert.Contains (1, wrapper.CheckedRows);
  2241. Assert.Equal (2, wrapper.CheckedRows.Count);
  2242. tv.Draw ();
  2243. expected =
  2244. @"
  2245. │ │A│B│
  2246. ├─┼─┼─►
  2247. │☑│1│2│
  2248. │☑│1│2│
  2249. │☐│1│2│";
  2250. TestHelpers.AssertDriverContentsAre (expected, output);
  2251. // untoggle top one
  2252. tv.NewKeyDownEvent (Key.CursorUp);
  2253. tv.NewKeyDownEvent (Key.Space);
  2254. Assert.Single (wrapper.CheckedRows, 1);
  2255. tv.Draw ();
  2256. expected =
  2257. @"
  2258. │ │A│B│
  2259. ├─┼─┼─►
  2260. │☐│1│2│
  2261. │☑│1│2│
  2262. │☐│1│2│";
  2263. TestHelpers.AssertDriverContentsAre (expected, output);
  2264. }
  2265. [Fact]
  2266. [SetupFakeDriver]
  2267. public void TestTableViewRadioBoxes_Simple_ByObject ()
  2268. {
  2269. TableView tv = GetPetTable (out EnumerableTableSource<PickablePet> source);
  2270. tv.LayoutSubviews ();
  2271. IReadOnlyCollection<PickablePet> pets = source.Data;
  2272. CheckBoxTableSourceWrapperByObject<PickablePet> wrapper = new (
  2273. tv,
  2274. source,
  2275. p => p.IsPicked,
  2276. (p, b) => p.IsPicked = b
  2277. );
  2278. wrapper.UseRadioButtons = true;
  2279. tv.Table = wrapper;
  2280. tv.Draw ();
  2281. var expected =
  2282. @"
  2283. ┌─┬───────┬─────────────┐
  2284. │ │Name │Kind │
  2285. ├─┼───────┼─────────────┤
  2286. │○│Tammy │Cat │
  2287. │○│Tibbles│Cat │
  2288. │○│Ripper │Dog │
  2289. ";
  2290. TestHelpers.AssertDriverContentsAre (expected, output);
  2291. #pragma warning disable xUnit2029
  2292. Assert.Empty (pets.Where (p => p.IsPicked));
  2293. #pragma warning restore xUnit2029
  2294. tv.NewKeyDownEvent (Key.Space);
  2295. Assert.True (pets.First ().IsPicked);
  2296. tv.Draw ();
  2297. expected =
  2298. @"
  2299. ┌─┬───────┬─────────────┐
  2300. │ │Name │Kind │
  2301. ├─┼───────┼─────────────┤
  2302. │◉│Tammy │Cat │
  2303. │○│Tibbles│Cat │
  2304. │○│Ripper │Dog │";
  2305. TestHelpers.AssertDriverContentsAre (expected, output);
  2306. tv.NewKeyDownEvent (Key.CursorDown);
  2307. tv.NewKeyDownEvent (Key.Space);
  2308. Assert.False (pets.ElementAt (0).IsPicked);
  2309. Assert.True (pets.ElementAt (1).IsPicked);
  2310. Assert.False (pets.ElementAt (2).IsPicked);
  2311. tv.Draw ();
  2312. expected =
  2313. @"
  2314. ┌─┬───────┬─────────────┐
  2315. │ │Name │Kind │
  2316. ├─┼───────┼─────────────┤
  2317. │○│Tammy │Cat │
  2318. │◉│Tibbles│Cat │
  2319. │○│Ripper │Dog │";
  2320. TestHelpers.AssertDriverContentsAre (expected, output);
  2321. tv.NewKeyDownEvent (Key.CursorUp);
  2322. tv.NewKeyDownEvent (Key.Space);
  2323. Assert.True (pets.ElementAt (0).IsPicked);
  2324. Assert.False (pets.ElementAt (1).IsPicked);
  2325. Assert.False (pets.ElementAt (2).IsPicked);
  2326. tv.Draw ();
  2327. expected =
  2328. @"
  2329. ┌─┬───────┬─────────────┐
  2330. │ │Name │Kind │
  2331. ├─┼───────┼─────────────┤
  2332. │◉│Tammy │Cat │
  2333. │○│Tibbles│Cat │
  2334. │○│Ripper │Dog │";
  2335. TestHelpers.AssertDriverContentsAre (expected, output);
  2336. }
  2337. [Fact]
  2338. public void TestToggleCells_MultiSelectOn ()
  2339. {
  2340. // 2 row table
  2341. TableView tableView = GetABCDEFTableView (out DataTable dt);
  2342. tableView.LayoutSubviews ();
  2343. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2344. tableView.MultiSelect = true;
  2345. tableView.KeyBindings.ReplaceCommands (Key.Space, Command.Select);
  2346. Point selectedCell = tableView.GetAllSelectedCells ().Single ();
  2347. Assert.Equal (0, selectedCell.X);
  2348. Assert.Equal (0, selectedCell.Y);
  2349. // Go Right
  2350. tableView.NewKeyDownEvent (Key.CursorRight);
  2351. selectedCell = tableView.GetAllSelectedCells ().Single ();
  2352. Assert.Equal (1, selectedCell.X);
  2353. Assert.Equal (0, selectedCell.Y);
  2354. // Toggle Select
  2355. tableView.NewKeyDownEvent (Key.Space);
  2356. TableSelection m = tableView.MultiSelectedRegions.Single ();
  2357. Assert.True (m.IsToggled);
  2358. Assert.Equal (1, m.Origin.X);
  2359. Assert.Equal (0, m.Origin.Y);
  2360. selectedCell = tableView.GetAllSelectedCells ().Single ();
  2361. Assert.Equal (1, selectedCell.X);
  2362. Assert.Equal (0, selectedCell.Y);
  2363. // Go Left
  2364. tableView.NewKeyDownEvent (Key.CursorLeft);
  2365. // Both Toggled and Moved to should be selected
  2366. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  2367. Point s1 = tableView.GetAllSelectedCells ().ElementAt (0);
  2368. Point s2 = tableView.GetAllSelectedCells ().ElementAt (1);
  2369. Assert.Equal (1, s1.X);
  2370. Assert.Equal (0, s1.Y);
  2371. Assert.Equal (0, s2.X);
  2372. Assert.Equal (0, s2.Y);
  2373. // Go Down
  2374. tableView.NewKeyDownEvent (Key.CursorDown);
  2375. // Both Toggled and Moved to should be selected but not 0,0
  2376. // which we moved down from
  2377. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  2378. s1 = tableView.GetAllSelectedCells ().ElementAt (0);
  2379. s2 = tableView.GetAllSelectedCells ().ElementAt (1);
  2380. Assert.Equal (1, s1.X);
  2381. Assert.Equal (0, s1.Y);
  2382. Assert.Equal (0, s2.X);
  2383. Assert.Equal (1, s2.Y);
  2384. // Go back to the toggled cell
  2385. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  2386. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorUp });
  2387. // Toggle off
  2388. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.Space });
  2389. // Go Left
  2390. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorLeft });
  2391. selectedCell = tableView.GetAllSelectedCells ().Single ();
  2392. Assert.Equal (0, selectedCell.X);
  2393. Assert.Equal (0, selectedCell.Y);
  2394. }
  2395. [Fact]
  2396. public void TestToggleCells_MultiSelectOn_FullRowSelect ()
  2397. {
  2398. // 2 row table
  2399. TableView tableView = GetABCDEFTableView (out DataTable dt);
  2400. tableView.LayoutSubviews ();
  2401. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2402. tableView.FullRowSelect = true;
  2403. tableView.MultiSelect = true;
  2404. tableView.KeyBindings.ReplaceCommands (Key.Space, Command.Select);
  2405. // Toggle Select Cell 0,0
  2406. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.Space });
  2407. // Go Down
  2408. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorDown });
  2409. TableSelection m = tableView.MultiSelectedRegions.Single ();
  2410. Assert.True (m.IsToggled);
  2411. Assert.Equal (0, m.Origin.X);
  2412. Assert.Equal (0, m.Origin.Y);
  2413. //First row toggled and Second row active = 12 selected cells
  2414. Assert.Equal (12, tableView.GetAllSelectedCells ().Count ());
  2415. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  2416. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorUp });
  2417. Assert.Single (tableView.MultiSelectedRegions.Where (r => r.IsToggled));
  2418. // Can untoggle at 1,0 even though 0,0 was initial toggle because FullRowSelect is on
  2419. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.Space });
  2420. #pragma warning disable xUnit2029
  2421. Assert.Empty (tableView.MultiSelectedRegions.Where (r => r.IsToggled));
  2422. #pragma warning restore xUnit2029
  2423. }
  2424. [Fact]
  2425. public void TestToggleCells_MultiSelectOn_SquareSelectToggled ()
  2426. {
  2427. // 3 row table
  2428. TableView tableView = GetABCDEFTableView (out DataTable dt);
  2429. tableView.LayoutSubviews ();
  2430. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2431. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2432. tableView.MultiSelect = true;
  2433. tableView.KeyBindings.ReplaceCommands (Key.Space, Command.Select);
  2434. // Make a square selection
  2435. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorDown });
  2436. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorRight });
  2437. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  2438. // Toggle the square selected region on
  2439. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.Space });
  2440. // Go Right
  2441. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  2442. //Toggled on square + the active cell (x=2,y=1)
  2443. Assert.Equal (5, tableView.GetAllSelectedCells ().Count ());
  2444. Assert.Equal (2, tableView.SelectedColumn);
  2445. Assert.Equal (1, tableView.SelectedRow);
  2446. // Untoggle the rectangular region by hitting toggle in
  2447. // any cell in that rect
  2448. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorUp });
  2449. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorLeft });
  2450. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  2451. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.Space });
  2452. Assert.Single (tableView.GetAllSelectedCells ());
  2453. }
  2454. [Fact]
  2455. public void TestToggleCells_MultiSelectOn_Two_SquareSelects_BothToggled ()
  2456. {
  2457. // 6 row table
  2458. TableView tableView = GetABCDEFTableView (out DataTable dt);
  2459. tableView.LayoutSubviews ();
  2460. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2461. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2462. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2463. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2464. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2465. tableView.MultiSelect = true;
  2466. tableView.KeyBindings.ReplaceCommands (Key.Space, Command.Select);
  2467. // Make first square selection (0,0 to 1,1)
  2468. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorDown });
  2469. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorRight });
  2470. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.Space });
  2471. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  2472. // Make second square selection leaving 1 unselected line between them
  2473. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorLeft });
  2474. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorDown });
  2475. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorDown });
  2476. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorDown });
  2477. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorRight });
  2478. // 2 square selections
  2479. Assert.Equal (8, tableView.GetAllSelectedCells ().Count ());
  2480. }
  2481. private TableView GetABCDEFTableView (out DataTable dt)
  2482. {
  2483. var tableView = new TableView ();
  2484. tableView.BeginInit ();
  2485. tableView.EndInit ();
  2486. tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  2487. // 3 columns are visible
  2488. tableView.Viewport = new (0, 0, 7, 5);
  2489. tableView.Style.ShowHorizontalHeaderUnderline = false;
  2490. tableView.Style.ShowHorizontalHeaderOverline = false;
  2491. tableView.Style.AlwaysShowHeaders = true;
  2492. tableView.Style.SmoothHorizontalScrolling = false;
  2493. dt = new ();
  2494. dt.Columns.Add ("A");
  2495. dt.Columns.Add ("B");
  2496. dt.Columns.Add ("C");
  2497. dt.Columns.Add ("D");
  2498. dt.Columns.Add ("E");
  2499. dt.Columns.Add ("F");
  2500. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2501. tableView.Table = new DataTableSource (dt);
  2502. return tableView;
  2503. }
  2504. private TableView GetPetTable (out EnumerableTableSource<PickablePet> source)
  2505. {
  2506. var tv = new TableView ();
  2507. tv.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  2508. tv.Viewport = new (0, 0, 25, 6);
  2509. List<PickablePet> pets = new ()
  2510. {
  2511. new (false, "Tammy", "Cat"),
  2512. new (false, "Tibbles", "Cat"),
  2513. new (false, "Ripper", "Dog")
  2514. };
  2515. tv.Table = source = new (
  2516. pets,
  2517. new ()
  2518. {
  2519. { "Name", p => p.Name }, { "Kind", p => p.Kind }
  2520. }
  2521. );
  2522. tv.LayoutSubviews ();
  2523. return tv;
  2524. }
  2525. private TableView GetTwoRowSixColumnTable () { return GetTwoRowSixColumnTable (out _); }
  2526. private TableView GetTwoRowSixColumnTable (out DataTable dt)
  2527. {
  2528. var tableView = new TableView ();
  2529. tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  2530. // 3 columns are visible
  2531. tableView.Viewport = new (0, 0, 7, 5);
  2532. tableView.Style.ShowHorizontalHeaderUnderline = true;
  2533. tableView.Style.ShowHorizontalHeaderOverline = false;
  2534. tableView.Style.AlwaysShowHeaders = true;
  2535. tableView.Style.SmoothHorizontalScrolling = true;
  2536. dt = new ();
  2537. dt.Columns.Add ("A");
  2538. dt.Columns.Add ("B");
  2539. dt.Columns.Add ("C");
  2540. dt.Columns.Add ("D");
  2541. dt.Columns.Add ("E");
  2542. dt.Columns.Add ("F");
  2543. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2544. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2545. tableView.Table = new DataTableSource (dt);
  2546. return tableView;
  2547. }
  2548. private TableView SetUpMiniTable () { return SetUpMiniTable (out _); }
  2549. private TableView SetUpMiniTable (out DataTable dt)
  2550. {
  2551. var tv = new TableView ();
  2552. tv.BeginInit ();
  2553. tv.EndInit ();
  2554. tv.Viewport = new (0, 0, 10, 4);
  2555. dt = new ();
  2556. dt.Columns.Add ("A");
  2557. dt.Columns.Add ("B");
  2558. dt.Rows.Add (1, 2);
  2559. tv.Table = new DataTableSource (dt);
  2560. tv.Style.GetOrCreateColumnStyle (0).MinWidth = 1;
  2561. tv.Style.GetOrCreateColumnStyle (0).MinWidth = 1;
  2562. tv.Style.GetOrCreateColumnStyle (1).MaxWidth = 1;
  2563. tv.Style.GetOrCreateColumnStyle (1).MaxWidth = 1;
  2564. tv.ColorScheme = Colors.ColorSchemes ["Base"];
  2565. return tv;
  2566. }
  2567. private class PickablePet
  2568. {
  2569. public PickablePet (bool isPicked, string name, string kind)
  2570. {
  2571. IsPicked = isPicked;
  2572. Name = name;
  2573. Kind = kind;
  2574. }
  2575. public bool IsPicked { get; set; }
  2576. public string Kind { get; }
  2577. public string Name { get; }
  2578. }
  2579. }