TableViewTests.cs 111 KB

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