TableViewTests.cs 74 KB

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