TableViewTests.cs 109 KB

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