TableViewTests.cs 102 KB

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