TableViewTests.cs 64 KB

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