TableViewTests.cs 90 KB

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