TableViewTests.cs 68 KB

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