TableViewTests.cs 108 KB

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