TableViewTests.cs 90 KB

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