TableViewTests.cs 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  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 += (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 += (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 += (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.Top.FocusFirst ();
  208. Assert.True (tableView.HasFocus);
  209. Assert.Equal (0, tableView.RowOffset);
  210. tableView.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ()));
  211. // window height is 5 rows 2 are header so page down should give 3 new rows
  212. Assert.Equal (3, tableView.SelectedRow);
  213. Assert.Equal (1, tableView.RowOffset);
  214. // header is no longer visible so page down should give 5 new rows
  215. tableView.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ()));
  216. Assert.Equal (8, tableView.SelectedRow);
  217. Assert.Equal (4, tableView.RowOffset);
  218. }
  219. [Fact]
  220. public void DeleteRow_SelectAll_AdjustsSelectionToPreventOverrun ()
  221. {
  222. // create a 4 by 4 table
  223. var tableView = new TableView () {
  224. Table = BuildTable (4, 4),
  225. MultiSelect = true,
  226. Bounds = new Rect (0, 0, 10, 5)
  227. };
  228. tableView.SelectAll ();
  229. Assert.Equal (16, tableView.GetAllSelectedCells ().Count ());
  230. // delete one of the columns
  231. tableView.Table.Columns.RemoveAt (2);
  232. // table should now be 3x4
  233. Assert.Equal (12, tableView.GetAllSelectedCells ().Count ());
  234. // remove a row
  235. tableView.Table.Rows.RemoveAt (1);
  236. // table should now be 3x3
  237. Assert.Equal (9, tableView.GetAllSelectedCells ().Count ());
  238. }
  239. [Fact]
  240. public void DeleteRow_SelectLastRow_AdjustsSelectionToPreventOverrun ()
  241. {
  242. // create a 4 by 4 table
  243. var tableView = new TableView () {
  244. Table = BuildTable (4, 4),
  245. MultiSelect = true,
  246. Bounds = new Rect (0, 0, 10, 5)
  247. };
  248. // select the last row
  249. tableView.MultiSelectedRegions.Clear ();
  250. tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (0, 3), new Rect (0, 3, 4, 1)));
  251. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  252. // remove a row
  253. tableView.Table.Rows.RemoveAt (0);
  254. tableView.EnsureValidSelection ();
  255. // since the selection no longer exists it should be removed
  256. Assert.Empty (tableView.MultiSelectedRegions);
  257. }
  258. [Theory]
  259. [InlineData (true)]
  260. [InlineData (false)]
  261. public void GetAllSelectedCells_SingleCellSelected_ReturnsOne (bool multiSelect)
  262. {
  263. var tableView = new TableView () {
  264. Table = BuildTable (3, 3),
  265. MultiSelect = multiSelect,
  266. Bounds = new Rect (0, 0, 10, 5)
  267. };
  268. tableView.SetSelection (1, 1, false);
  269. Assert.Single (tableView.GetAllSelectedCells ());
  270. Assert.Equal (new Point (1, 1), tableView.GetAllSelectedCells ().Single ());
  271. }
  272. [Fact]
  273. public void GetAllSelectedCells_SquareSelection_ReturnsFour ()
  274. {
  275. var tableView = new TableView () {
  276. Table = BuildTable (3, 3),
  277. MultiSelect = true,
  278. Bounds = new Rect (0, 0, 10, 5)
  279. };
  280. // move cursor to 1,1
  281. tableView.SetSelection (1, 1, false);
  282. // spread selection across to 2,2 (e.g. shift+right then shift+down)
  283. tableView.SetSelection (2, 2, true);
  284. var selected = tableView.GetAllSelectedCells ().ToArray ();
  285. Assert.Equal (4, selected.Length);
  286. Assert.Equal (new Point (1, 1), selected [0]);
  287. Assert.Equal (new Point (2, 1), selected [1]);
  288. Assert.Equal (new Point (1, 2), selected [2]);
  289. Assert.Equal (new Point (2, 2), selected [3]);
  290. }
  291. [Fact]
  292. public void GetAllSelectedCells_SquareSelection_FullRowSelect ()
  293. {
  294. var tableView = new TableView () {
  295. Table = BuildTable (3, 3),
  296. MultiSelect = true,
  297. FullRowSelect = true,
  298. Bounds = new Rect (0, 0, 10, 5)
  299. };
  300. // move cursor to 1,1
  301. tableView.SetSelection (1, 1, false);
  302. // spread selection across to 2,2 (e.g. shift+right then shift+down)
  303. tableView.SetSelection (2, 2, true);
  304. var selected = tableView.GetAllSelectedCells ().ToArray ();
  305. Assert.Equal (6, selected.Length);
  306. Assert.Equal (new Point (0, 1), selected [0]);
  307. Assert.Equal (new Point (1, 1), selected [1]);
  308. Assert.Equal (new Point (2, 1), selected [2]);
  309. Assert.Equal (new Point (0, 2), selected [3]);
  310. Assert.Equal (new Point (1, 2), selected [4]);
  311. Assert.Equal (new Point (2, 2), selected [5]);
  312. }
  313. [Fact]
  314. public void GetAllSelectedCells_TwoIsolatedSelections_ReturnsSix ()
  315. {
  316. var tableView = new TableView () {
  317. Table = BuildTable (20, 20),
  318. MultiSelect = true,
  319. Bounds = new Rect (0, 0, 10, 5)
  320. };
  321. /*
  322. Sets up disconnected selections like:
  323. 00000000000
  324. 01100000000
  325. 01100000000
  326. 00000001100
  327. 00000000000
  328. */
  329. tableView.MultiSelectedRegions.Clear ();
  330. tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (1, 1), new Rect (1, 1, 2, 2)));
  331. tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (7, 3), new Rect (7, 3, 2, 1)));
  332. tableView.SelectedColumn = 8;
  333. tableView.SelectedRow = 3;
  334. var selected = tableView.GetAllSelectedCells ().ToArray ();
  335. Assert.Equal (6, selected.Length);
  336. Assert.Equal (new Point (1, 1), selected [0]);
  337. Assert.Equal (new Point (2, 1), selected [1]);
  338. Assert.Equal (new Point (1, 2), selected [2]);
  339. Assert.Equal (new Point (2, 2), selected [3]);
  340. Assert.Equal (new Point (7, 3), selected [4]);
  341. Assert.Equal (new Point (8, 3), selected [5]);
  342. }
  343. [Fact]
  344. public void TableView_ExpandLastColumn_True ()
  345. {
  346. var tv = SetUpMiniTable ();
  347. // the thing we are testing
  348. tv.Style.ExpandLastColumn = true;
  349. tv.Redraw (tv.Bounds);
  350. string expected = @"
  351. ┌─┬──────┐
  352. │A│B │
  353. ├─┼──────┤
  354. │1│2 │
  355. ";
  356. TestHelpers.AssertDriverContentsAre (expected, output);
  357. // Shutdown must be called to safely clean up Application if Init has been called
  358. Application.Shutdown ();
  359. }
  360. [Fact]
  361. public void TableView_ExpandLastColumn_False ()
  362. {
  363. var tv = SetUpMiniTable ();
  364. // the thing we are testing
  365. tv.Style.ExpandLastColumn = false;
  366. tv.Redraw (tv.Bounds);
  367. string expected = @"
  368. ┌─┬─┬────┐
  369. │A│B│ │
  370. ├─┼─┼────┤
  371. │1│2│ │
  372. ";
  373. TestHelpers.AssertDriverContentsAre (expected, output);
  374. // Shutdown must be called to safely clean up Application if Init has been called
  375. Application.Shutdown ();
  376. }
  377. [Fact]
  378. public void TableView_ExpandLastColumn_False_ExactBounds ()
  379. {
  380. var tv = SetUpMiniTable ();
  381. // the thing we are testing
  382. tv.Style.ExpandLastColumn = false;
  383. // width exactly matches the max col widths
  384. tv.Bounds = new Rect (0, 0, 5, 4);
  385. tv.Redraw (tv.Bounds);
  386. string expected = @"
  387. ┌─┬─┐
  388. │A│B│
  389. ├─┼─┤
  390. │1│2│
  391. ";
  392. TestHelpers.AssertDriverContentsAre (expected, output);
  393. // Shutdown must be called to safely clean up Application if Init has been called
  394. Application.Shutdown ();
  395. }
  396. [Fact]
  397. [AutoInitShutdown]
  398. public void TableView_Activate()
  399. {
  400. string activatedValue = null;
  401. var tv = new TableView (BuildTable(1,1));
  402. tv.CellActivated += (c) => activatedValue = c.Table.Rows[c.Row][c.Col].ToString();
  403. Application.Top.Add (tv);
  404. Application.Begin (Application.Top);
  405. // pressing enter should activate the first cell (selected cell)
  406. tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  407. Assert.Equal ("R0C0",activatedValue);
  408. // reset the test
  409. activatedValue = null;
  410. // clear keybindings and ensure that Enter does not trigger the event anymore
  411. tv.ClearKeybindings ();
  412. tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  413. Assert.Null(activatedValue);
  414. // New method for changing the activation key
  415. tv.AddKeyBinding (Key.z, Command.Accept);
  416. tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ()));
  417. Assert.Equal ("R0C0", activatedValue);
  418. // reset the test
  419. activatedValue = null;
  420. tv.ClearKeybindings ();
  421. // Old method for changing the activation key
  422. tv.CellActivationKey = Key.z;
  423. tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ()));
  424. Assert.Equal ("R0C0", activatedValue);
  425. }
  426. [Fact]
  427. public void TableViewMultiSelect_CannotFallOffLeft()
  428. {
  429. var tv = SetUpMiniTable ();
  430. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  431. tv.MultiSelect = true;
  432. tv.SelectedColumn = 1;
  433. tv.SelectedRow = 1;
  434. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  435. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single().Rect);
  436. // this next shift left should be ignored because we are already at the bounds
  437. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  438. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  439. Assert.Equal (0, tv.SelectedColumn);
  440. Assert.Equal (1, tv.SelectedRow);
  441. Application.Shutdown ();
  442. }
  443. [Fact]
  444. public void TableViewMultiSelect_CannotFallOffRight()
  445. {
  446. var tv = SetUpMiniTable ();
  447. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  448. tv.MultiSelect = true;
  449. tv.SelectedColumn = 0;
  450. tv.SelectedRow = 1;
  451. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  452. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  453. // this next shift right should be ignored because we are already at the right bounds
  454. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  455. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  456. Assert.Equal (1, tv.SelectedColumn);
  457. Assert.Equal (1, tv.SelectedRow);
  458. Application.Shutdown ();
  459. }
  460. [Fact]
  461. public void TableViewMultiSelect_CannotFallOffBottom ()
  462. {
  463. var tv = SetUpMiniTable ();
  464. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  465. tv.MultiSelect = true;
  466. tv.SelectedColumn = 0;
  467. tv.SelectedRow = 0;
  468. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  469. tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
  470. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  471. // this next moves should be ignored because we already selected the whole table
  472. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  473. tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
  474. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  475. Assert.Equal (1, tv.SelectedColumn);
  476. Assert.Equal (1, tv.SelectedRow);
  477. Application.Shutdown ();
  478. }
  479. [Fact]
  480. public void TableViewMultiSelect_CannotFallOffTop()
  481. {
  482. var tv = SetUpMiniTable ();
  483. tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  484. tv.MultiSelect = true;
  485. tv.SelectedColumn = 1;
  486. tv.SelectedRow = 1;
  487. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  488. tv.ProcessKey (new KeyEvent (Key.CursorUp | Key.ShiftMask, new KeyModifiers { Shift = true }));
  489. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  490. // this next moves should be ignored because we already selected the whole table
  491. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  492. tv.ProcessKey (new KeyEvent (Key.CursorUp | Key.ShiftMask, new KeyModifiers { Shift = true }));
  493. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  494. Assert.Equal (0, tv.SelectedColumn);
  495. Assert.Equal (0, tv.SelectedRow);
  496. Application.Shutdown ();
  497. }
  498. [Fact, AutoInitShutdown]
  499. public void TestShiftClick_MultiSelect_TwoRowTable_FullRowSelect()
  500. {
  501. var tv = GetTwoRowSixColumnTable ();
  502. tv.MultiSelect = true;
  503. // Clicking in bottom row
  504. tv.MouseEvent (new MouseEvent {
  505. X = 1,
  506. Y = 3,
  507. Flags = MouseFlags.Button1Clicked
  508. });
  509. // should select that row
  510. Assert.Equal (1, tv.SelectedRow);
  511. // shift clicking top row
  512. tv.MouseEvent (new MouseEvent {
  513. X = 1,
  514. Y = 2,
  515. Flags = MouseFlags.Button1Clicked | MouseFlags.ButtonShift
  516. });
  517. // should extend the selection
  518. Assert.Equal (0, tv.SelectedRow);
  519. var selected = tv.GetAllSelectedCells ().ToArray();
  520. Assert.Contains (new Point(0,0), selected);
  521. Assert.Contains (new Point (0, 1), selected);
  522. }
  523. [Fact, AutoInitShutdown]
  524. public void TestControlClick_MultiSelect_ThreeRowTable_FullRowSelect ()
  525. {
  526. var tv = GetTwoRowSixColumnTable ();
  527. tv.Table.Rows.Add (1, 2, 3, 4, 5, 6);
  528. tv.MultiSelect = true;
  529. // Clicking in bottom row
  530. tv.MouseEvent (new MouseEvent {
  531. X = 1,
  532. Y = 4,
  533. Flags = MouseFlags.Button1Clicked
  534. });
  535. // should select that row
  536. Assert.Equal (2, tv.SelectedRow);
  537. // shift clicking top row
  538. tv.MouseEvent (new MouseEvent {
  539. X = 1,
  540. Y = 2,
  541. Flags = MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl
  542. });
  543. // should extend the selection
  544. // to include bottom and top row but not middle
  545. Assert.Equal (0, tv.SelectedRow);
  546. var selected = tv.GetAllSelectedCells ().ToArray ();
  547. Assert.Contains (new Point (0, 0), selected);
  548. Assert.DoesNotContain (new Point (0, 1), selected);
  549. Assert.Contains (new Point (0, 2), selected);
  550. }
  551. [Theory]
  552. [InlineData (false)]
  553. [InlineData (true)]
  554. public void TableView_ColorTests_FocusedOrNot (bool focused)
  555. {
  556. var tv = SetUpMiniTable ();
  557. // width exactly matches the max col widths
  558. tv.Bounds = new Rect (0, 0, 5, 4);
  559. // private method for forcing the view to be focused/not focused
  560. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  561. // when the view is/isn't focused
  562. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  563. tv.Redraw (tv.Bounds);
  564. string expected = @"
  565. ┌─┬─┐
  566. │A│B│
  567. ├─┼─┤
  568. │1│2│
  569. ";
  570. TestHelpers.AssertDriverContentsAre (expected, output);
  571. string expectedColors = @"
  572. 00000
  573. 00000
  574. 00000
  575. 01000
  576. ";
  577. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  578. // 0
  579. tv.ColorScheme.Normal,
  580. // 1
  581. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal});
  582. Application.Shutdown();
  583. }
  584. [Theory]
  585. [InlineData (false)]
  586. [InlineData (true)]
  587. public void TableView_ColorTests_InvertSelectedCellFirstCharacter (bool focused)
  588. {
  589. var tv = SetUpMiniTable ();
  590. tv.Style.InvertSelectedCellFirstCharacter = true;
  591. // width exactly matches the max col widths
  592. tv.Bounds = new Rect (0, 0, 5, 4);
  593. // private method for forcing the view to be focused/not focused
  594. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  595. // when the view is/isn't focused
  596. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  597. tv.Redraw (tv.Bounds);
  598. string expected = @"
  599. ┌─┬─┐
  600. │A│B│
  601. ├─┼─┤
  602. │1│2│
  603. ";
  604. TestHelpers.AssertDriverContentsAre (expected, output);
  605. string expectedColors = @"
  606. 00000
  607. 00000
  608. 00000
  609. 01000
  610. ";
  611. var invertHotFocus = new Attribute(tv.ColorScheme.HotFocus.Background,tv.ColorScheme.HotFocus.Foreground);
  612. var invertHotNormal = new Attribute(tv.ColorScheme.HotNormal.Background,tv.ColorScheme.HotNormal.Foreground);
  613. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  614. // 0
  615. tv.ColorScheme.Normal,
  616. // 1
  617. focused ? invertHotFocus : invertHotNormal});
  618. Application.Shutdown();
  619. }
  620. [Theory]
  621. [InlineData (false)]
  622. [InlineData (true)]
  623. public void TableView_ColorsTest_RowColorGetter (bool focused)
  624. {
  625. var tv = SetUpMiniTable ();
  626. // width exactly matches the max col widths
  627. tv.Bounds = new Rect (0, 0, 5, 4);
  628. var rowHighlight = new ColorScheme () {
  629. Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
  630. HotNormal = Attribute.Make (Color.Green, Color.Blue),
  631. HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
  632. Focus = Attribute.Make (Color.Cyan, Color.Magenta),
  633. };
  634. // when B is 2 use the custom highlight colour for the row
  635. tv.Style.RowColorGetter += (e)=>Convert.ToInt32(e.Table.Rows[e.RowIndex][1]) == 2 ? rowHighlight : null;
  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. 21222
  653. ";
  654. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  655. // 0
  656. tv.ColorScheme.Normal,
  657. // 1
  658. focused ? rowHighlight.HotFocus : rowHighlight.HotNormal,
  659. // 2
  660. rowHighlight.Normal});
  661. // change the value in the table so that
  662. // it no longer matches the RowColorGetter
  663. // delegate conditional ( which checks for
  664. // the value 2)
  665. tv.Table.Rows[0][1] = 5;
  666. tv.Redraw (tv.Bounds);
  667. expected = @"
  668. ┌─┬─┐
  669. │A│B│
  670. ├─┼─┤
  671. │1│5│
  672. ";
  673. TestHelpers.AssertDriverContentsAre (expected, output);
  674. expectedColors = @"
  675. 00000
  676. 00000
  677. 00000
  678. 01000
  679. ";
  680. // now we only see 2 colors used (the selected cell color and Normal
  681. // rowHighlight should no longer be used because the delegate returned null
  682. // (now that the cell value is 5 - which does not match the conditional)
  683. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  684. // 0
  685. tv.ColorScheme.Normal,
  686. // 1
  687. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
  688. // Shutdown must be called to safely clean up Application if Init has been called
  689. Application.Shutdown ();
  690. }
  691. [Theory]
  692. [InlineData (false)]
  693. [InlineData (true)]
  694. public void TableView_ColorsTest_ColorGetter (bool focused)
  695. {
  696. var tv = SetUpMiniTable ();
  697. // width exactly matches the max col widths
  698. tv.Bounds = new Rect (0, 0, 5, 4);
  699. // Create a style for column B
  700. var bStyle = tv.Style.GetOrCreateColumnStyle (tv.Table.Columns ["B"]);
  701. // when B is 2 use the custom highlight colour
  702. var cellHighlight = new ColorScheme () {
  703. Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
  704. HotNormal = Attribute.Make (Color.Green, Color.Blue),
  705. HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
  706. Focus = Attribute.Make (Color.Cyan, Color.Magenta),
  707. };
  708. bStyle.ColorGetter = (a) => Convert.ToInt32(a.CellValue) == 2 ? cellHighlight : null;
  709. // private method for forcing the view to be focused/not focused
  710. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  711. // when the view is/isn't focused
  712. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  713. tv.Redraw (tv.Bounds);
  714. string expected = @"
  715. ┌─┬─┐
  716. │A│B│
  717. ├─┼─┤
  718. │1│2│
  719. ";
  720. TestHelpers.AssertDriverContentsAre (expected, output);
  721. string expectedColors = @"
  722. 00000
  723. 00000
  724. 00000
  725. 01020
  726. ";
  727. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  728. // 0
  729. tv.ColorScheme.Normal,
  730. // 1
  731. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal,
  732. // 2
  733. cellHighlight.Normal});
  734. // change the value in the table so that
  735. // it no longer matches the ColorGetter
  736. // delegate conditional ( which checks for
  737. // the value 2)
  738. tv.Table.Rows[0][1] = 5;
  739. tv.Redraw (tv.Bounds);
  740. expected = @"
  741. ┌─┬─┐
  742. │A│B│
  743. ├─┼─┤
  744. │1│5│
  745. ";
  746. TestHelpers.AssertDriverContentsAre (expected, output);
  747. expectedColors = @"
  748. 00000
  749. 00000
  750. 00000
  751. 01000
  752. ";
  753. // now we only see 2 colors used (the selected cell color and Normal
  754. // cellHighlight should no longer be used because the delegate returned null
  755. // (now that the cell value is 5 - which does not match the conditional)
  756. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  757. // 0
  758. tv.ColorScheme.Normal,
  759. // 1
  760. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
  761. // Shutdown must be called to safely clean up Application if Init has been called
  762. Application.Shutdown ();
  763. }
  764. private TableView SetUpMiniTable ()
  765. {
  766. var tv = new TableView ();
  767. tv.Bounds = new Rect (0, 0, 10, 4);
  768. var dt = new DataTable ();
  769. var colA = dt.Columns.Add ("A");
  770. var colB = dt.Columns.Add ("B");
  771. dt.Rows.Add (1, 2);
  772. tv.Table = dt;
  773. tv.Style.GetOrCreateColumnStyle (colA).MinWidth = 1;
  774. tv.Style.GetOrCreateColumnStyle (colA).MinWidth = 1;
  775. tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
  776. tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
  777. GraphViewTests.InitFakeDriver ();
  778. tv.ColorScheme = Colors.Base;
  779. return tv;
  780. }
  781. [Fact]
  782. [AutoInitShutdown]
  783. public void ScrollDown_OneLineAtATime ()
  784. {
  785. var tableView = new TableView ();
  786. // Set big table
  787. tableView.Table = BuildTable (25, 50);
  788. // 1 header + 4 rows visible
  789. tableView.Bounds = new Rect (0, 0, 25, 5);
  790. tableView.Style.ShowHorizontalHeaderUnderline = false;
  791. tableView.Style.ShowHorizontalHeaderOverline = false;
  792. tableView.Style.AlwaysShowHeaders = true;
  793. // select last row
  794. tableView.SelectedRow = 3; // row is 0 indexed so this is the 4th visible row
  795. // Scroll down
  796. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorDown });
  797. // Scrolled off the page by 1 row so it should only have moved down 1 line of RowOffset
  798. Assert.Equal(4,tableView.SelectedRow);
  799. Assert.Equal (1, tableView.RowOffset);
  800. }
  801. [Fact]
  802. public void ScrollRight_SmoothScrolling ()
  803. {
  804. GraphViewTests.InitFakeDriver ();
  805. var tableView = new TableView ();
  806. tableView.ColorScheme = Colors.TopLevel;
  807. // 3 columns are visibile
  808. tableView.Bounds = new Rect (0, 0, 7, 5);
  809. tableView.Style.ShowHorizontalHeaderUnderline = false;
  810. tableView.Style.ShowHorizontalHeaderOverline = false;
  811. tableView.Style.AlwaysShowHeaders = true;
  812. tableView.Style.SmoothHorizontalScrolling = true;
  813. var dt = new DataTable ();
  814. dt.Columns.Add ("A");
  815. dt.Columns.Add ("B");
  816. dt.Columns.Add ("C");
  817. dt.Columns.Add ("D");
  818. dt.Columns.Add ("E");
  819. dt.Columns.Add ("F");
  820. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  821. tableView.Table = dt;
  822. // select last visible column
  823. tableView.SelectedColumn = 2; // column C
  824. tableView.Redraw (tableView.Bounds);
  825. string expected =
  826. @"
  827. │A│B│C│
  828. │1│2│3│";
  829. TestHelpers.AssertDriverContentsAre (expected, output);
  830. // Scroll right
  831. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  832. tableView.Redraw (tableView.Bounds);
  833. // Note that with SmoothHorizontalScrolling only a single new column
  834. // is exposed when scrolling right. This is not always the case though
  835. // sometimes if the leftmost column is long (i.e. A is a long column)
  836. // then when A is pushed off the screen multiple new columns could be exposed
  837. // (not just D but also E and F). This is because TableView never shows
  838. // 'half cells' or scrolls by console unit (scrolling is done by table row/column increments).
  839. expected =
  840. @"
  841. │B│C│D│
  842. │2│3│4│";
  843. TestHelpers.AssertDriverContentsAre (expected, output);
  844. // Shutdown must be called to safely clean up Application if Init has been called
  845. Application.Shutdown ();
  846. }
  847. [Fact]
  848. public void ScrollRight_WithoutSmoothScrolling ()
  849. {
  850. GraphViewTests.InitFakeDriver ();
  851. var tableView = new TableView ();
  852. tableView.ColorScheme = Colors.TopLevel;
  853. // 3 columns are visibile
  854. tableView.Bounds = new Rect (0, 0, 7, 5);
  855. tableView.Style.ShowHorizontalHeaderUnderline = false;
  856. tableView.Style.ShowHorizontalHeaderOverline = false;
  857. tableView.Style.AlwaysShowHeaders = true;
  858. tableView.Style.SmoothHorizontalScrolling = false;
  859. var dt = new DataTable ();
  860. dt.Columns.Add ("A");
  861. dt.Columns.Add ("B");
  862. dt.Columns.Add ("C");
  863. dt.Columns.Add ("D");
  864. dt.Columns.Add ("E");
  865. dt.Columns.Add ("F");
  866. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  867. tableView.Table = dt;
  868. // select last visible column
  869. tableView.SelectedColumn = 2; // column C
  870. tableView.Redraw (tableView.Bounds);
  871. string expected =
  872. @"
  873. │A│B│C│
  874. │1│2│3│";
  875. TestHelpers.AssertDriverContentsAre (expected, output);
  876. // Scroll right
  877. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  878. tableView.Redraw (tableView.Bounds);
  879. // notice that without smooth scrolling we just update the first column
  880. // rendered in the table to the newly exposed column (D). This is fast
  881. // since we don't have to worry about repeatedly measuring the content
  882. // area as we scroll until the new column (D) is exposed. But it makes
  883. // the view 'jump' to expose all new columns
  884. expected =
  885. @"
  886. │D│E│F│
  887. │4│5│6│";
  888. TestHelpers.AssertDriverContentsAre (expected, output);
  889. // Shutdown must be called to safely clean up Application if Init has been called
  890. Application.Shutdown ();
  891. }
  892. private TableView GetABCDEFTableView (out DataTable dt)
  893. {
  894. var tableView = new TableView ();
  895. tableView.ColorScheme = Colors.TopLevel;
  896. // 3 columns are visible
  897. tableView.Bounds = new Rect (0, 0, 7, 5);
  898. tableView.Style.ShowHorizontalHeaderUnderline = false;
  899. tableView.Style.ShowHorizontalHeaderOverline = false;
  900. tableView.Style.AlwaysShowHeaders = true;
  901. tableView.Style.SmoothHorizontalScrolling = false;
  902. dt = new DataTable ();
  903. dt.Columns.Add ("A");
  904. dt.Columns.Add ("B");
  905. dt.Columns.Add ("C");
  906. dt.Columns.Add ("D");
  907. dt.Columns.Add ("E");
  908. dt.Columns.Add ("F");
  909. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  910. tableView.Table = dt;
  911. return tableView;
  912. }
  913. [Fact, AutoInitShutdown]
  914. public void TestColumnStyle_VisibleFalse_IsNotRendered()
  915. {
  916. var tableView = GetABCDEFTableView (out DataTable dt);
  917. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  918. tableView.Redraw (tableView.Bounds);
  919. string expected =
  920. @"
  921. │A│C│D│
  922. │1│3│4│";
  923. TestHelpers.AssertDriverContentsAre (expected, output);
  924. }
  925. [Fact, AutoInitShutdown]
  926. public void TestColumnStyle_FirstColumnVisibleFalse_IsNotRendered ()
  927. {
  928. var tableView = GetABCDEFTableView (out DataTable dt);
  929. tableView.Style.ShowHorizontalScrollIndicators = true;
  930. tableView.Style.ShowHorizontalHeaderUnderline = true;
  931. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  932. tableView.Redraw (tableView.Bounds);
  933. string expected =
  934. @"
  935. │B│C│D│
  936. ├─┼─┼─►
  937. │2│3│4│";
  938. TestHelpers.AssertDriverContentsAre (expected, output);
  939. }
  940. [Fact, AutoInitShutdown]
  941. public void TestColumnStyle_AllColumnsVisibleFalse_BehavesAsTableNull ()
  942. {
  943. var tableView = GetABCDEFTableView (out DataTable dt);
  944. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  945. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  946. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["C"]).Visible = false;
  947. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  948. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  949. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  950. // expect nothing to be rendered when all columns are invisible
  951. string expected =
  952. @"
  953. ";
  954. tableView.Redraw (tableView.Bounds);
  955. TestHelpers.AssertDriverContentsAre (expected, output);
  956. // expect behavior to match when Table is null
  957. tableView.Table = null;
  958. tableView.Redraw (tableView.Bounds);
  959. TestHelpers.AssertDriverContentsAre (expected, output);
  960. }
  961. [Fact, AutoInitShutdown]
  962. public void TestColumnStyle_RemainingColumnsInvisible_NoScrollIndicator ()
  963. {
  964. var tableView = GetABCDEFTableView (out DataTable dt);
  965. tableView.Style.ShowHorizontalScrollIndicators = true;
  966. tableView.Style.ShowHorizontalHeaderUnderline = true;
  967. tableView.Redraw (tableView.Bounds);
  968. // normally we should have scroll indicators because DEF are of screen
  969. string expected =
  970. @"
  971. │A│B│C│
  972. ├─┼─┼─►
  973. │1│2│3│";
  974. TestHelpers.AssertDriverContentsAre (expected, output);
  975. // but if DEF are invisible we shouldn't be showing the indicator
  976. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  977. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  978. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  979. expected =
  980. @"
  981. │A│B│C│
  982. ├─┼─┼─┤
  983. │1│2│3│";
  984. tableView.Redraw (tableView.Bounds);
  985. TestHelpers.AssertDriverContentsAre (expected, output);
  986. }
  987. [Fact, AutoInitShutdown]
  988. public void TestColumnStyle_PreceedingColumnsInvisible_NoScrollIndicator ()
  989. {
  990. var tableView = GetABCDEFTableView (out DataTable dt);
  991. tableView.Style.ShowHorizontalScrollIndicators = true;
  992. tableView.Style.ShowHorizontalHeaderUnderline = true;
  993. tableView.ColumnOffset = 1;
  994. tableView.Redraw (tableView.Bounds);
  995. // normally we should have scroll indicators because A,E and F are of screen
  996. string expected =
  997. @"
  998. │B│C│D│
  999. ◄─┼─┼─►
  1000. │2│3│4│";
  1001. TestHelpers.AssertDriverContentsAre (expected, output);
  1002. // but if E and F are invisible so we shouldn't show right
  1003. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  1004. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  1005. expected =
  1006. @"
  1007. │B│C│D│
  1008. ◄─┼─┼─┤
  1009. │2│3│4│";
  1010. tableView.Redraw (tableView.Bounds);
  1011. TestHelpers.AssertDriverContentsAre (expected, output);
  1012. // now also A is invisible so we cannot scroll in either direction
  1013. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  1014. expected =
  1015. @"
  1016. │B│C│D│
  1017. ├─┼─┼─┤
  1018. │2│3│4│";
  1019. tableView.Redraw (tableView.Bounds);
  1020. TestHelpers.AssertDriverContentsAre (expected, output);
  1021. }
  1022. [Fact, AutoInitShutdown]
  1023. public void TestColumnStyle_VisibleFalse_CursorStepsOverInvisibleColumns ()
  1024. {
  1025. var tableView = GetABCDEFTableView (out var dt);
  1026. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  1027. tableView.SelectedColumn = 0;
  1028. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1029. // Expect the cursor navigation to skip over the invisible column(s)
  1030. Assert.Equal(2,tableView.SelectedColumn);
  1031. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1032. // Expect the cursor navigation backwards to skip over invisible column too
  1033. Assert.Equal (0, tableView.SelectedColumn);
  1034. }
  1035. [InlineData(true)]
  1036. [InlineData (false)]
  1037. [Theory, AutoInitShutdown]
  1038. public void TestColumnStyle_FirstColumnVisibleFalse_CursorStaysAt1(bool useHome)
  1039. {
  1040. var tableView = GetABCDEFTableView (out var dt);
  1041. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
  1042. tableView.SelectedColumn = 0;
  1043. Assert.Equal (0, tableView.SelectedColumn);
  1044. // column 0 is invisible so this method should move to 1
  1045. tableView.EnsureValidSelection();
  1046. Assert.Equal (1, tableView.SelectedColumn);
  1047. tableView.ProcessKey (new KeyEvent
  1048. {
  1049. Key = useHome ? Key.Home : Key.CursorLeft
  1050. });
  1051. // Expect the cursor to stay at 1
  1052. Assert.Equal (1, tableView.SelectedColumn);
  1053. }
  1054. [InlineData(true)]
  1055. [InlineData (false)]
  1056. [Theory, AutoInitShutdown]
  1057. public void TestMoveStartEnd_WithFullRowSelect(bool withFullRowSelect)
  1058. {
  1059. var tableView = GetTwoRowSixColumnTable ();
  1060. tableView.FullRowSelect = withFullRowSelect;
  1061. tableView.SelectedRow = 1;
  1062. tableView.SelectedColumn = 1;
  1063. tableView.ProcessKey (new KeyEvent
  1064. {
  1065. Key = Key.Home | Key.CtrlMask
  1066. });
  1067. if(withFullRowSelect)
  1068. {
  1069. // Should not be any horizontal movement when
  1070. // using navigate to Start/End and FullRowSelect
  1071. Assert.Equal (1, tableView.SelectedColumn);
  1072. Assert.Equal (0, tableView.SelectedRow);
  1073. }
  1074. else
  1075. {
  1076. Assert.Equal (0, tableView.SelectedColumn);
  1077. Assert.Equal (0, tableView.SelectedRow);
  1078. }
  1079. tableView.ProcessKey (new KeyEvent
  1080. {
  1081. Key = Key.End | Key.CtrlMask
  1082. });
  1083. if(withFullRowSelect)
  1084. {
  1085. Assert.Equal (1, tableView.SelectedColumn);
  1086. Assert.Equal (1, tableView.SelectedRow);
  1087. }
  1088. else
  1089. {
  1090. Assert.Equal (5, tableView.SelectedColumn);
  1091. Assert.Equal (1, tableView.SelectedRow);
  1092. }
  1093. }
  1094. [InlineData (true)]
  1095. [InlineData (false)]
  1096. [Theory, AutoInitShutdown]
  1097. public void TestColumnStyle_LastColumnVisibleFalse_CursorStaysAt2 (bool useEnd)
  1098. {
  1099. var tableView = GetABCDEFTableView (out var dt);
  1100. // select D
  1101. tableView.SelectedColumn = 3;
  1102. Assert.Equal (3, tableView.SelectedColumn);
  1103. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  1104. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
  1105. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
  1106. // column D is invisible so this method should move to 2 (C)
  1107. tableView.EnsureValidSelection ();
  1108. Assert.Equal (2, tableView.SelectedColumn);
  1109. tableView.ProcessKey (new KeyEvent {
  1110. Key = useEnd ? Key.End : Key.CursorRight
  1111. });
  1112. // Expect the cursor to stay at 2
  1113. Assert.Equal (2, tableView.SelectedColumn);
  1114. }
  1115. [Fact, AutoInitShutdown]
  1116. public void TestColumnStyle_VisibleFalse_MultiSelected ()
  1117. {
  1118. var tableView = GetABCDEFTableView (out var dt);
  1119. // user has rectangular selection
  1120. tableView.MultiSelectedRegions.Push (
  1121. new TableView.TableSelection(
  1122. new Point(0,0),
  1123. new Rect(0, 0, 3, 1))
  1124. );
  1125. Assert.Equal (3, tableView.GetAllSelectedCells ().Count());
  1126. Assert.True (tableView.IsSelected (0, 0));
  1127. Assert.True (tableView.IsSelected (1, 0));
  1128. Assert.True (tableView.IsSelected (2, 0));
  1129. Assert.False (tableView.IsSelected (3, 0));
  1130. // if middle column is invisible
  1131. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  1132. // it should not be included in the selection
  1133. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1134. Assert.True (tableView.IsSelected (0, 0));
  1135. Assert.False (tableView.IsSelected (1, 0));
  1136. Assert.True (tableView.IsSelected (2, 0));
  1137. Assert.False (tableView.IsSelected (3, 0));
  1138. Assert.DoesNotContain(new Point(1,0),tableView.GetAllSelectedCells ());
  1139. }
  1140. [Fact, AutoInitShutdown]
  1141. public void TestColumnStyle_VisibleFalse_MultiSelectingStepsOverInvisibleColumns ()
  1142. {
  1143. var tableView = GetABCDEFTableView (out var dt);
  1144. // if middle column is invisible
  1145. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
  1146. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight | Key.ShiftMask });
  1147. // Selection should extend from A to C but skip B
  1148. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1149. Assert.True (tableView.IsSelected (0, 0));
  1150. Assert.False (tableView.IsSelected (1, 0));
  1151. Assert.True (tableView.IsSelected (2, 0));
  1152. Assert.False (tableView.IsSelected (3, 0));
  1153. Assert.DoesNotContain (new Point (1, 0), tableView.GetAllSelectedCells ());
  1154. }
  1155. [Theory, AutoInitShutdown]
  1156. [InlineData(new object[] { true,true })]
  1157. [InlineData (new object[] { false,true })]
  1158. [InlineData (new object [] { true, false})]
  1159. [InlineData (new object [] { false, false})]
  1160. public void TestColumnStyle_VisibleFalse_DoesNotEffect_EnsureSelectedCellIsVisible (bool smooth, bool invisibleCol)
  1161. {
  1162. var tableView = GetABCDEFTableView (out var dt);
  1163. tableView.Style.SmoothHorizontalScrolling = smooth;
  1164. if(invisibleCol) {
  1165. tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
  1166. }
  1167. // New TableView should have first cell selected
  1168. Assert.Equal (0,tableView.SelectedColumn);
  1169. // With no scrolling
  1170. Assert.Equal (0, tableView.ColumnOffset);
  1171. // A,B and C are visible on screen at the moment so these should have no effect
  1172. tableView.SelectedColumn = 1;
  1173. tableView.EnsureSelectedCellIsVisible ();
  1174. Assert.Equal (0, tableView.ColumnOffset);
  1175. tableView.SelectedColumn = 2;
  1176. tableView.EnsureSelectedCellIsVisible ();
  1177. Assert.Equal (0, tableView.ColumnOffset);
  1178. // Selecting D should move the visible table area to fit D onto the screen
  1179. tableView.SelectedColumn = 3;
  1180. tableView.EnsureSelectedCellIsVisible ();
  1181. Assert.Equal (smooth ? 1 : 3, tableView.ColumnOffset);
  1182. }
  1183. [Fact]
  1184. public void LongColumnTest ()
  1185. {
  1186. GraphViewTests.InitFakeDriver ();
  1187. var tableView = new TableView ();
  1188. tableView.ColorScheme = Colors.TopLevel;
  1189. // 25 characters can be printed into table
  1190. tableView.Bounds = new Rect (0, 0, 25, 5);
  1191. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1192. tableView.Style.ShowHorizontalHeaderOverline = false;
  1193. tableView.Style.AlwaysShowHeaders = true;
  1194. tableView.Style.SmoothHorizontalScrolling = true;
  1195. var dt = new DataTable ();
  1196. dt.Columns.Add ("A");
  1197. dt.Columns.Add ("B");
  1198. dt.Columns.Add ("Very Long Column");
  1199. dt.Rows.Add (1, 2, new string('a',500));
  1200. dt.Rows.Add (1, 2, "aaa");
  1201. tableView.Table = dt;
  1202. tableView.Redraw (tableView.Bounds);
  1203. // default behaviour of TableView is not to render
  1204. // columns unless there is sufficient space
  1205. string expected =
  1206. @"
  1207. │A│B │
  1208. ├─┼─────────────────────►
  1209. │1│2 │
  1210. │1│2 │
  1211. ";
  1212. TestHelpers.AssertDriverContentsAre (expected, output);
  1213. // get a style for the long column
  1214. var style = tableView.Style.GetOrCreateColumnStyle(dt.Columns[2]);
  1215. // one way the API user can fix this for long columns
  1216. // is to specify a max width for the column
  1217. style.MaxWidth = 10;
  1218. tableView.Redraw (tableView.Bounds);
  1219. expected =
  1220. @"
  1221. │A│B│Very Long │
  1222. ├─┼─┼───────────────────┤
  1223. │1│2│aaaaaaaaaa │
  1224. │1│2│aaa │
  1225. ";
  1226. TestHelpers.AssertDriverContentsAre (expected, output);
  1227. // revert the style change
  1228. style.MaxWidth = TableView.DefaultMaxCellWidth;
  1229. // another way API user can fix problem is to implement
  1230. // RepresentationGetter and apply max length there
  1231. style.RepresentationGetter = (s)=>{
  1232. return s.ToString().Length < 15 ? s.ToString() : s.ToString().Substring(0,13)+"...";
  1233. };
  1234. tableView.Redraw (tableView.Bounds);
  1235. expected =
  1236. @"
  1237. │A│B│Very Long Column │
  1238. ├─┼─┼───────────────────┤
  1239. │1│2│aaaaaaaaaaaaa... │
  1240. │1│2│aaa │
  1241. ";
  1242. TestHelpers.AssertDriverContentsAre (expected, output);
  1243. // revert style change
  1244. style.RepresentationGetter = null;
  1245. // Both of the above methods rely on having a fixed
  1246. // size limit for the column. These are awkward if a
  1247. // table is resizeable e.g. Dim.Fill(). Ideally we want
  1248. // to render in any space available and truncate the content
  1249. // of the column dynamically so it fills the free space at
  1250. // the end of the table.
  1251. // We can now specify that the column can be any length
  1252. // (Up to MaxWidth) but the renderer can accept using
  1253. // less space down to this limit
  1254. style.MinAcceptableWidth = 5;
  1255. tableView.Redraw (tableView.Bounds);
  1256. expected =
  1257. @"
  1258. │A│B│Very Long Column │
  1259. ├─┼─┼───────────────────┤
  1260. │1│2│aaaaaaaaaaaaaaaaaaa│
  1261. │1│2│aaa │
  1262. ";
  1263. TestHelpers.AssertDriverContentsAre (expected, output);
  1264. // Now test making the width too small for the MinAcceptableWidth
  1265. // the Column won't fit so should not be rendered
  1266. Application.Shutdown ();
  1267. GraphViewTests.InitFakeDriver ();
  1268. tableView.Bounds = new Rect(0,0,9,5);
  1269. tableView.Redraw (tableView.Bounds);
  1270. expected =
  1271. @"
  1272. │A│B │
  1273. ├─┼─────►
  1274. │1│2 │
  1275. │1│2 │
  1276. ";
  1277. TestHelpers.AssertDriverContentsAre (expected, output);
  1278. // setting width to 10 leaves just enough space for the column to
  1279. // meet MinAcceptableWidth of 5. Column width includes terminator line
  1280. // symbol (e.g. ┤ or │)
  1281. tableView.Bounds = new Rect (0, 0, 10, 5);
  1282. tableView.Redraw (tableView.Bounds);
  1283. expected =
  1284. @"
  1285. │A│B│Very│
  1286. ├─┼─┼────┤
  1287. │1│2│aaaa│
  1288. │1│2│aaa │
  1289. ";
  1290. TestHelpers.AssertDriverContentsAre (expected, output);
  1291. Application.Shutdown ();
  1292. }
  1293. [Fact]
  1294. public void ScrollIndicators ()
  1295. {
  1296. GraphViewTests.InitFakeDriver ();
  1297. var tableView = new TableView ();
  1298. tableView.ColorScheme = Colors.TopLevel;
  1299. // 3 columns are visibile
  1300. tableView.Bounds = new Rect (0, 0, 7, 5);
  1301. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1302. tableView.Style.ShowHorizontalHeaderOverline = false;
  1303. tableView.Style.AlwaysShowHeaders = true;
  1304. tableView.Style.SmoothHorizontalScrolling = true;
  1305. var dt = new DataTable ();
  1306. dt.Columns.Add ("A");
  1307. dt.Columns.Add ("B");
  1308. dt.Columns.Add ("C");
  1309. dt.Columns.Add ("D");
  1310. dt.Columns.Add ("E");
  1311. dt.Columns.Add ("F");
  1312. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1313. tableView.Table = dt;
  1314. // select last visible column
  1315. tableView.SelectedColumn = 2; // column C
  1316. tableView.Redraw (tableView.Bounds);
  1317. // user can only scroll right so sees right indicator
  1318. // Because first column in table is A
  1319. string expected =
  1320. @"
  1321. │A│B│C│
  1322. ├─┼─┼─►
  1323. │1│2│3│";
  1324. TestHelpers.AssertDriverContentsAre (expected, output);
  1325. // Scroll right
  1326. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1327. // since A is now pushed off screen we get indicator showing
  1328. // that user can scroll left to see first column
  1329. tableView.Redraw (tableView.Bounds);
  1330. expected =
  1331. @"
  1332. │B│C│D│
  1333. ◄─┼─┼─►
  1334. │2│3│4│";
  1335. TestHelpers.AssertDriverContentsAre (expected, output);
  1336. // Scroll right twice more (to end of columns)
  1337. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1338. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1339. tableView.Redraw (tableView.Bounds);
  1340. expected =
  1341. @"
  1342. │D│E│F│
  1343. ◄─┼─┼─┤
  1344. │4│5│6│";
  1345. TestHelpers.AssertDriverContentsAre (expected, output);
  1346. // Shutdown must be called to safely clean up Application if Init has been called
  1347. Application.Shutdown ();
  1348. }
  1349. /// <summary>
  1350. /// Builds a simple table of string columns with the requested number of columns and rows
  1351. /// </summary>
  1352. /// <param name="cols"></param>
  1353. /// <param name="rows"></param>
  1354. /// <returns></returns>
  1355. public static DataTable BuildTable (int cols, int rows)
  1356. {
  1357. var dt = new DataTable ();
  1358. for (int c = 0; c < cols; c++) {
  1359. dt.Columns.Add ("Col" + c);
  1360. }
  1361. for (int r = 0; r < rows; r++) {
  1362. var newRow = dt.NewRow ();
  1363. for (int c = 0; c < cols; c++) {
  1364. newRow [c] = $"R{r}C{c}";
  1365. }
  1366. dt.Rows.Add (newRow);
  1367. }
  1368. return dt;
  1369. }
  1370. [Fact, AutoInitShutdown]
  1371. public void Test_ScreenToCell ()
  1372. {
  1373. var tableView = GetTwoRowSixColumnTable ();
  1374. tableView.Redraw (tableView.Bounds);
  1375. // user can only scroll right so sees right indicator
  1376. // Because first column in table is A
  1377. string expected =
  1378. @"
  1379. │A│B│C│
  1380. ├─┼─┼─►
  1381. │1│2│3│
  1382. │1│2│3│";
  1383. TestHelpers.AssertDriverContentsAre (expected, output);
  1384. // ---------------- X=0 -----------------------
  1385. // click is before first cell
  1386. Assert.Null (tableView.ScreenToCell (0, 0));
  1387. Assert.Null (tableView.ScreenToCell (0, 1));
  1388. Assert.Null (tableView.ScreenToCell (0, 2));
  1389. Assert.Null (tableView.ScreenToCell (0, 3));
  1390. Assert.Null (tableView.ScreenToCell (0, 4));
  1391. // ---------------- X=1 -----------------------
  1392. // click in header
  1393. Assert.Null (tableView.ScreenToCell (1, 0));
  1394. // click in header row line
  1395. Assert.Null (tableView.ScreenToCell (1, 1));
  1396. // click in cell 0,0
  1397. Assert.Equal (new Point(0,0),tableView.ScreenToCell (1, 2));
  1398. // click in cell 0,1
  1399. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3));
  1400. // after last row
  1401. Assert.Null (tableView.ScreenToCell (1, 4));
  1402. // ---------------- X=2 -----------------------
  1403. // ( even though there is a horizontal dividing line here we treat it as a hit on the cell before)
  1404. // click in header
  1405. Assert.Null (tableView.ScreenToCell (2, 0));
  1406. // click in header row line
  1407. Assert.Null (tableView.ScreenToCell (2, 1));
  1408. // click in cell 0,0
  1409. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (2, 2));
  1410. // click in cell 0,1
  1411. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3));
  1412. // after last row
  1413. Assert.Null (tableView.ScreenToCell (2, 4));
  1414. // ---------------- X=3 -----------------------
  1415. // click in header
  1416. Assert.Null (tableView.ScreenToCell (3, 0));
  1417. // click in header row line
  1418. Assert.Null (tableView.ScreenToCell (3, 1));
  1419. // click in cell 1,0
  1420. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2));
  1421. // click in cell 1,1
  1422. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3));
  1423. // after last row
  1424. Assert.Null (tableView.ScreenToCell (3, 4));
  1425. }
  1426. [Fact, AutoInitShutdown]
  1427. public void Test_ScreenToCell_DataColumnOverload ()
  1428. {
  1429. var tableView = GetTwoRowSixColumnTable ();
  1430. tableView.Redraw (tableView.Bounds);
  1431. // user can only scroll right so sees right indicator
  1432. // Because first column in table is A
  1433. string expected =
  1434. @"
  1435. │A│B│C│
  1436. ├─┼─┼─►
  1437. │1│2│3│
  1438. │1│2│3│";
  1439. TestHelpers.AssertDriverContentsAre (expected, output);
  1440. DataColumn col;
  1441. // ---------------- X=0 -----------------------
  1442. // click is before first cell
  1443. Assert.Null (tableView.ScreenToCell (0, 0,out col));
  1444. Assert.Null (col);
  1445. Assert.Null (tableView.ScreenToCell (0, 1,out col));
  1446. Assert.Null (col);
  1447. Assert.Null (tableView.ScreenToCell (0, 2,out col));
  1448. Assert.Null (col);
  1449. Assert.Null (tableView.ScreenToCell (0, 3,out col));
  1450. Assert.Null (col);
  1451. Assert.Null (tableView.ScreenToCell (0, 4,out col));
  1452. Assert.Null (col);
  1453. // ---------------- X=1 -----------------------
  1454. // click in header
  1455. Assert.Null (tableView.ScreenToCell (1, 0, out col));
  1456. Assert.Equal ("A", col.ColumnName);
  1457. // click in header row line (click in the horizontal line below header counts as click in header above - consistent with the column hit box)
  1458. Assert.Null (tableView.ScreenToCell (1, 1, out col));
  1459. Assert.Equal ("A", col.ColumnName);
  1460. // click in cell 0,0
  1461. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (1, 2, out col));
  1462. Assert.Null (col);
  1463. // click in cell 0,1
  1464. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3, out col));
  1465. Assert.Null (col);
  1466. // after last row
  1467. Assert.Null (tableView.ScreenToCell (1, 4, out col));
  1468. Assert.Null (col);
  1469. // ---------------- X=2 -----------------------
  1470. // click in header
  1471. Assert.Null (tableView.ScreenToCell (2, 0, out col));
  1472. Assert.Equal ("A", col.ColumnName);
  1473. // click in header row line
  1474. Assert.Null (tableView.ScreenToCell (2, 1, out col));
  1475. Assert.Equal ("A", col.ColumnName);
  1476. // click in cell 0,0
  1477. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (2, 2, out col));
  1478. Assert.Null (col);
  1479. // click in cell 0,1
  1480. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3, out col));
  1481. Assert.Null (col);
  1482. // after last row
  1483. Assert.Null (tableView.ScreenToCell (2, 4, out col));
  1484. Assert.Null (col);
  1485. // ---------------- X=3 -----------------------
  1486. // click in header
  1487. Assert.Null (tableView.ScreenToCell (3, 0, out col));
  1488. Assert.Equal ("B", col.ColumnName);
  1489. // click in header row line
  1490. Assert.Null (tableView.ScreenToCell (3, 1, out col));
  1491. Assert.Equal ("B", col.ColumnName);
  1492. // click in cell 1,0
  1493. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2, out col));
  1494. Assert.Null (col);
  1495. // click in cell 1,1
  1496. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3, out col));
  1497. Assert.Null (col);
  1498. // after last row
  1499. Assert.Null (tableView.ScreenToCell (3, 4, out col));
  1500. Assert.Null (col);
  1501. }
  1502. private TableView GetTwoRowSixColumnTable ()
  1503. {
  1504. var tableView = new TableView ();
  1505. tableView.ColorScheme = Colors.TopLevel;
  1506. // 3 columns are visible
  1507. tableView.Bounds = new Rect (0, 0, 7, 5);
  1508. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1509. tableView.Style.ShowHorizontalHeaderOverline = false;
  1510. tableView.Style.AlwaysShowHeaders = true;
  1511. tableView.Style.SmoothHorizontalScrolling = true;
  1512. var dt = new DataTable ();
  1513. dt.Columns.Add ("A");
  1514. dt.Columns.Add ("B");
  1515. dt.Columns.Add ("C");
  1516. dt.Columns.Add ("D");
  1517. dt.Columns.Add ("E");
  1518. dt.Columns.Add ("F");
  1519. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1520. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1521. tableView.Table = dt;
  1522. return tableView;
  1523. }
  1524. }
  1525. }