TableViewTests.cs 101 KB

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