TableViewTests.cs 106 KB

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