TableViewTests.cs 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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.Views {
  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. [Theory]
  499. [InlineData (false)]
  500. [InlineData (true)]
  501. public void TableView_ColorTests_FocusedOrNot (bool focused)
  502. {
  503. var tv = SetUpMiniTable ();
  504. // width exactly matches the max col widths
  505. tv.Bounds = new Rect (0, 0, 5, 4);
  506. // private method for forcing the view to be focused/not focused
  507. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  508. // when the view is/isn't focused
  509. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  510. tv.Redraw (tv.Bounds);
  511. string expected = @"
  512. ┌─┬─┐
  513. │A│B│
  514. ├─┼─┤
  515. │1│2│
  516. ";
  517. TestHelpers.AssertDriverContentsAre (expected, output);
  518. string expectedColors = @"
  519. 00000
  520. 00000
  521. 00000
  522. 01000
  523. ";
  524. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  525. // 0
  526. tv.ColorScheme.Normal,
  527. // 1
  528. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal});
  529. Application.Shutdown();
  530. }
  531. [Theory]
  532. [InlineData (false)]
  533. [InlineData (true)]
  534. public void TableView_ColorTests_InvertSelectedCellFirstCharacter (bool focused)
  535. {
  536. var tv = SetUpMiniTable ();
  537. tv.Style.InvertSelectedCellFirstCharacter = true;
  538. // width exactly matches the max col widths
  539. tv.Bounds = new Rect (0, 0, 5, 4);
  540. // private method for forcing the view to be focused/not focused
  541. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  542. // when the view is/isn't focused
  543. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  544. tv.Redraw (tv.Bounds);
  545. string expected = @"
  546. ┌─┬─┐
  547. │A│B│
  548. ├─┼─┤
  549. │1│2│
  550. ";
  551. TestHelpers.AssertDriverContentsAre (expected, output);
  552. string expectedColors = @"
  553. 00000
  554. 00000
  555. 00000
  556. 01000
  557. ";
  558. var invertHotFocus = new Attribute(tv.ColorScheme.HotFocus.Background,tv.ColorScheme.HotFocus.Foreground);
  559. var invertHotNormal = new Attribute(tv.ColorScheme.HotNormal.Background,tv.ColorScheme.HotNormal.Foreground);
  560. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  561. // 0
  562. tv.ColorScheme.Normal,
  563. // 1
  564. focused ? invertHotFocus : invertHotNormal});
  565. Application.Shutdown();
  566. }
  567. [Theory]
  568. [InlineData (false)]
  569. [InlineData (true)]
  570. public void TableView_ColorsTest_RowColorGetter (bool focused)
  571. {
  572. var tv = SetUpMiniTable ();
  573. // width exactly matches the max col widths
  574. tv.Bounds = new Rect (0, 0, 5, 4);
  575. var rowHighlight = new ColorScheme () {
  576. Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
  577. HotNormal = Attribute.Make (Color.Green, Color.Blue),
  578. HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
  579. Focus = Attribute.Make (Color.Cyan, Color.Magenta),
  580. };
  581. // when B is 2 use the custom highlight colour for the row
  582. tv.Style.RowColorGetter += (e)=>Convert.ToInt32(e.Table.Rows[e.RowIndex][1]) == 2 ? rowHighlight : null;
  583. // private method for forcing the view to be focused/not focused
  584. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  585. // when the view is/isn't focused
  586. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  587. tv.Redraw (tv.Bounds);
  588. string expected = @"
  589. ┌─┬─┐
  590. │A│B│
  591. ├─┼─┤
  592. │1│2│
  593. ";
  594. TestHelpers.AssertDriverContentsAre (expected, output);
  595. string expectedColors = @"
  596. 00000
  597. 00000
  598. 00000
  599. 21222
  600. ";
  601. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  602. // 0
  603. tv.ColorScheme.Normal,
  604. // 1
  605. focused ? rowHighlight.HotFocus : rowHighlight.HotNormal,
  606. // 2
  607. rowHighlight.Normal});
  608. // change the value in the table so that
  609. // it no longer matches the RowColorGetter
  610. // delegate conditional ( which checks for
  611. // the value 2)
  612. tv.Table.Rows[0][1] = 5;
  613. tv.Redraw (tv.Bounds);
  614. expected = @"
  615. ┌─┬─┐
  616. │A│B│
  617. ├─┼─┤
  618. │1│5│
  619. ";
  620. TestHelpers.AssertDriverContentsAre (expected, output);
  621. expectedColors = @"
  622. 00000
  623. 00000
  624. 00000
  625. 01000
  626. ";
  627. // now we only see 2 colors used (the selected cell color and Normal
  628. // rowHighlight should no longer be used because the delegate returned null
  629. // (now that the cell value is 5 - which does not match the conditional)
  630. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  631. // 0
  632. tv.ColorScheme.Normal,
  633. // 1
  634. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
  635. // Shutdown must be called to safely clean up Application if Init has been called
  636. Application.Shutdown ();
  637. }
  638. [Theory]
  639. [InlineData (false)]
  640. [InlineData (true)]
  641. public void TableView_ColorsTest_ColorGetter (bool focused)
  642. {
  643. var tv = SetUpMiniTable ();
  644. // width exactly matches the max col widths
  645. tv.Bounds = new Rect (0, 0, 5, 4);
  646. // Create a style for column B
  647. var bStyle = tv.Style.GetOrCreateColumnStyle (tv.Table.Columns ["B"]);
  648. // when B is 2 use the custom highlight colour
  649. var cellHighlight = new ColorScheme () {
  650. Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
  651. HotNormal = Attribute.Make (Color.Green, Color.Blue),
  652. HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
  653. Focus = Attribute.Make (Color.Cyan, Color.Magenta),
  654. };
  655. bStyle.ColorGetter = (a) => Convert.ToInt32(a.CellValue) == 2 ? cellHighlight : null;
  656. // private method for forcing the view to be focused/not focused
  657. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  658. // when the view is/isn't focused
  659. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  660. tv.Redraw (tv.Bounds);
  661. string expected = @"
  662. ┌─┬─┐
  663. │A│B│
  664. ├─┼─┤
  665. │1│2│
  666. ";
  667. TestHelpers.AssertDriverContentsAre (expected, output);
  668. string expectedColors = @"
  669. 00000
  670. 00000
  671. 00000
  672. 01020
  673. ";
  674. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  675. // 0
  676. tv.ColorScheme.Normal,
  677. // 1
  678. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal,
  679. // 2
  680. cellHighlight.Normal});
  681. // change the value in the table so that
  682. // it no longer matches the ColorGetter
  683. // delegate conditional ( which checks for
  684. // the value 2)
  685. tv.Table.Rows[0][1] = 5;
  686. tv.Redraw (tv.Bounds);
  687. expected = @"
  688. ┌─┬─┐
  689. │A│B│
  690. ├─┼─┤
  691. │1│5│
  692. ";
  693. TestHelpers.AssertDriverContentsAre (expected, output);
  694. expectedColors = @"
  695. 00000
  696. 00000
  697. 00000
  698. 01000
  699. ";
  700. // now we only see 2 colors used (the selected cell color and Normal
  701. // cellHighlight should no longer be used because the delegate returned null
  702. // (now that the cell value is 5 - which does not match the conditional)
  703. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  704. // 0
  705. tv.ColorScheme.Normal,
  706. // 1
  707. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
  708. // Shutdown must be called to safely clean up Application if Init has been called
  709. Application.Shutdown ();
  710. }
  711. private TableView SetUpMiniTable ()
  712. {
  713. var tv = new TableView ();
  714. tv.Bounds = new Rect (0, 0, 10, 4);
  715. var dt = new DataTable ();
  716. var colA = dt.Columns.Add ("A");
  717. var colB = dt.Columns.Add ("B");
  718. dt.Rows.Add (1, 2);
  719. tv.Table = dt;
  720. tv.Style.GetOrCreateColumnStyle (colA).MinWidth = 1;
  721. tv.Style.GetOrCreateColumnStyle (colA).MinWidth = 1;
  722. tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
  723. tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
  724. GraphViewTests.InitFakeDriver ();
  725. tv.ColorScheme = Colors.Base;
  726. return tv;
  727. }
  728. [Fact]
  729. [AutoInitShutdown]
  730. public void ScrollDown_OneLineAtATime ()
  731. {
  732. var tableView = new TableView ();
  733. // Set big table
  734. tableView.Table = BuildTable (25, 50);
  735. // 1 header + 4 rows visible
  736. tableView.Bounds = new Rect (0, 0, 25, 5);
  737. tableView.Style.ShowHorizontalHeaderUnderline = false;
  738. tableView.Style.ShowHorizontalHeaderOverline = false;
  739. tableView.Style.AlwaysShowHeaders = true;
  740. // select last row
  741. tableView.SelectedRow = 3; // row is 0 indexed so this is the 4th visible row
  742. // Scroll down
  743. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorDown });
  744. // Scrolled off the page by 1 row so it should only have moved down 1 line of RowOffset
  745. Assert.Equal(4,tableView.SelectedRow);
  746. Assert.Equal (1, tableView.RowOffset);
  747. }
  748. [Fact]
  749. public void ScrollRight_SmoothScrolling ()
  750. {
  751. GraphViewTests.InitFakeDriver ();
  752. var tableView = new TableView ();
  753. tableView.ColorScheme = Colors.TopLevel;
  754. // 3 columns are visibile
  755. tableView.Bounds = new Rect (0, 0, 7, 5);
  756. tableView.Style.ShowHorizontalHeaderUnderline = false;
  757. tableView.Style.ShowHorizontalHeaderOverline = false;
  758. tableView.Style.AlwaysShowHeaders = true;
  759. tableView.Style.SmoothHorizontalScrolling = true;
  760. var dt = new DataTable ();
  761. dt.Columns.Add ("A");
  762. dt.Columns.Add ("B");
  763. dt.Columns.Add ("C");
  764. dt.Columns.Add ("D");
  765. dt.Columns.Add ("E");
  766. dt.Columns.Add ("F");
  767. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  768. tableView.Table = dt;
  769. // select last visible column
  770. tableView.SelectedColumn = 2; // column C
  771. tableView.Redraw (tableView.Bounds);
  772. string expected =
  773. @"
  774. │A│B│C│
  775. │1│2│3│";
  776. TestHelpers.AssertDriverContentsAre (expected, output);
  777. // Scroll right
  778. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  779. tableView.Redraw (tableView.Bounds);
  780. // Note that with SmoothHorizontalScrolling only a single new column
  781. // is exposed when scrolling right. This is not always the case though
  782. // sometimes if the leftmost column is long (i.e. A is a long column)
  783. // then when A is pushed off the screen multiple new columns could be exposed
  784. // (not just D but also E and F). This is because TableView never shows
  785. // 'half cells' or scrolls by console unit (scrolling is done by table row/column increments).
  786. expected =
  787. @"
  788. │B│C│D│
  789. │2│3│4│";
  790. TestHelpers.AssertDriverContentsAre (expected, output);
  791. // Shutdown must be called to safely clean up Application if Init has been called
  792. Application.Shutdown ();
  793. }
  794. [Fact]
  795. public void ScrollRight_WithoutSmoothScrolling ()
  796. {
  797. GraphViewTests.InitFakeDriver ();
  798. var tableView = new TableView ();
  799. tableView.ColorScheme = Colors.TopLevel;
  800. // 3 columns are visibile
  801. tableView.Bounds = new Rect (0, 0, 7, 5);
  802. tableView.Style.ShowHorizontalHeaderUnderline = false;
  803. tableView.Style.ShowHorizontalHeaderOverline = false;
  804. tableView.Style.AlwaysShowHeaders = true;
  805. tableView.Style.SmoothHorizontalScrolling = false;
  806. var dt = new DataTable ();
  807. dt.Columns.Add ("A");
  808. dt.Columns.Add ("B");
  809. dt.Columns.Add ("C");
  810. dt.Columns.Add ("D");
  811. dt.Columns.Add ("E");
  812. dt.Columns.Add ("F");
  813. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  814. tableView.Table = dt;
  815. // select last visible column
  816. tableView.SelectedColumn = 2; // column C
  817. tableView.Redraw (tableView.Bounds);
  818. string expected =
  819. @"
  820. │A│B│C│
  821. │1│2│3│";
  822. TestHelpers.AssertDriverContentsAre (expected, output);
  823. // Scroll right
  824. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  825. tableView.Redraw (tableView.Bounds);
  826. // notice that without smooth scrolling we just update the first column
  827. // rendered in the table to the newly exposed column (D). This is fast
  828. // since we don't have to worry about repeatedly measuring the content
  829. // area as we scroll until the new column (D) is exposed. But it makes
  830. // the view 'jump' to expose all new columns
  831. expected =
  832. @"
  833. │D│E│F│
  834. │4│5│6│";
  835. TestHelpers.AssertDriverContentsAre (expected, output);
  836. // Shutdown must be called to safely clean up Application if Init has been called
  837. Application.Shutdown ();
  838. }
  839. [Fact]
  840. public void LongColumnTest ()
  841. {
  842. GraphViewTests.InitFakeDriver ();
  843. var tableView = new TableView ();
  844. tableView.ColorScheme = Colors.TopLevel;
  845. // 25 characters can be printed into table
  846. tableView.Bounds = new Rect (0, 0, 25, 5);
  847. tableView.Style.ShowHorizontalHeaderUnderline = true;
  848. tableView.Style.ShowHorizontalHeaderOverline = false;
  849. tableView.Style.AlwaysShowHeaders = true;
  850. tableView.Style.SmoothHorizontalScrolling = true;
  851. var dt = new DataTable ();
  852. dt.Columns.Add ("A");
  853. dt.Columns.Add ("B");
  854. dt.Columns.Add ("Very Long Column");
  855. dt.Rows.Add (1, 2, new string('a',500));
  856. dt.Rows.Add (1, 2, "aaa");
  857. tableView.Table = dt;
  858. tableView.Redraw (tableView.Bounds);
  859. // default behaviour of TableView is not to render
  860. // columns unless there is sufficient space
  861. string expected =
  862. @"
  863. │A│B │
  864. ├─┼─────────────────────►
  865. │1│2 │
  866. │1│2 │
  867. ";
  868. TestHelpers.AssertDriverContentsAre (expected, output);
  869. // get a style for the long column
  870. var style = tableView.Style.GetOrCreateColumnStyle(dt.Columns[2]);
  871. // one way the API user can fix this for long columns
  872. // is to specify a max width for the column
  873. style.MaxWidth = 10;
  874. tableView.Redraw (tableView.Bounds);
  875. expected =
  876. @"
  877. │A│B│Very Long │
  878. ├─┼─┼───────────────────┤
  879. │1│2│aaaaaaaaaa │
  880. │1│2│aaa │
  881. ";
  882. TestHelpers.AssertDriverContentsAre (expected, output);
  883. // revert the style change
  884. style.MaxWidth = TableView.DefaultMaxCellWidth;
  885. // another way API user can fix problem is to implement
  886. // RepresentationGetter and apply max length there
  887. style.RepresentationGetter = (s)=>{
  888. return s.ToString().Length < 15 ? s.ToString() : s.ToString().Substring(0,13)+"...";
  889. };
  890. tableView.Redraw (tableView.Bounds);
  891. expected =
  892. @"
  893. │A│B│Very Long Column │
  894. ├─┼─┼───────────────────┤
  895. │1│2│aaaaaaaaaaaaa... │
  896. │1│2│aaa │
  897. ";
  898. TestHelpers.AssertDriverContentsAre (expected, output);
  899. // revert style change
  900. style.RepresentationGetter = null;
  901. // Both of the above methods rely on having a fixed
  902. // size limit for the column. These are awkward if a
  903. // table is resizeable e.g. Dim.Fill(). Ideally we want
  904. // to render in any space available and truncate the content
  905. // of the column dynamically so it fills the free space at
  906. // the end of the table.
  907. // We can now specify that the column can be any length
  908. // (Up to MaxWidth) but the renderer can accept using
  909. // less space down to this limit
  910. style.MinAcceptableWidth = 5;
  911. tableView.Redraw (tableView.Bounds);
  912. expected =
  913. @"
  914. │A│B│Very Long Column │
  915. ├─┼─┼───────────────────┤
  916. │1│2│aaaaaaaaaaaaaaaaaaa│
  917. │1│2│aaa │
  918. ";
  919. TestHelpers.AssertDriverContentsAre (expected, output);
  920. // Now test making the width too small for the MinAcceptableWidth
  921. // the Column won't fit so should not be rendered
  922. Application.Shutdown ();
  923. GraphViewTests.InitFakeDriver ();
  924. tableView.Bounds = new Rect(0,0,9,5);
  925. tableView.Redraw (tableView.Bounds);
  926. expected =
  927. @"
  928. │A│B │
  929. ├─┼─────►
  930. │1│2 │
  931. │1│2 │
  932. ";
  933. TestHelpers.AssertDriverContentsAre (expected, output);
  934. // setting width to 10 leaves just enough space for the column to
  935. // meet MinAcceptableWidth of 5. Column width includes terminator line
  936. // symbol (e.g. ┤ or │)
  937. tableView.Bounds = new Rect (0, 0, 10, 5);
  938. tableView.Redraw (tableView.Bounds);
  939. expected =
  940. @"
  941. │A│B│Very│
  942. ├─┼─┼────┤
  943. │1│2│aaaa│
  944. │1│2│aaa │
  945. ";
  946. TestHelpers.AssertDriverContentsAre (expected, output);
  947. Application.Shutdown ();
  948. }
  949. [Fact]
  950. public void ScrollIndicators ()
  951. {
  952. GraphViewTests.InitFakeDriver ();
  953. var tableView = new TableView ();
  954. tableView.ColorScheme = Colors.TopLevel;
  955. // 3 columns are visibile
  956. tableView.Bounds = new Rect (0, 0, 7, 5);
  957. tableView.Style.ShowHorizontalHeaderUnderline = true;
  958. tableView.Style.ShowHorizontalHeaderOverline = false;
  959. tableView.Style.AlwaysShowHeaders = true;
  960. tableView.Style.SmoothHorizontalScrolling = true;
  961. var dt = new DataTable ();
  962. dt.Columns.Add ("A");
  963. dt.Columns.Add ("B");
  964. dt.Columns.Add ("C");
  965. dt.Columns.Add ("D");
  966. dt.Columns.Add ("E");
  967. dt.Columns.Add ("F");
  968. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  969. tableView.Table = dt;
  970. // select last visible column
  971. tableView.SelectedColumn = 2; // column C
  972. tableView.Redraw (tableView.Bounds);
  973. // user can only scroll right so sees right indicator
  974. // Because first column in table is A
  975. string expected =
  976. @"
  977. │A│B│C│
  978. ├─┼─┼─►
  979. │1│2│3│";
  980. TestHelpers.AssertDriverContentsAre (expected, output);
  981. // Scroll right
  982. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  983. // since A is now pushed off screen we get indicator showing
  984. // that user can scroll left to see first column
  985. tableView.Redraw (tableView.Bounds);
  986. expected =
  987. @"
  988. │B│C│D│
  989. ◄─┼─┼─►
  990. │2│3│4│";
  991. TestHelpers.AssertDriverContentsAre (expected, output);
  992. // Scroll right twice more (to end of columns)
  993. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  994. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  995. tableView.Redraw (tableView.Bounds);
  996. expected =
  997. @"
  998. │D│E│F│
  999. ◄─┼─┼─┤
  1000. │4│5│6│";
  1001. TestHelpers.AssertDriverContentsAre (expected, output);
  1002. // Shutdown must be called to safely clean up Application if Init has been called
  1003. Application.Shutdown ();
  1004. }
  1005. /// <summary>
  1006. /// Builds a simple table of string columns with the requested number of columns and rows
  1007. /// </summary>
  1008. /// <param name="cols"></param>
  1009. /// <param name="rows"></param>
  1010. /// <returns></returns>
  1011. public static DataTable BuildTable (int cols, int rows)
  1012. {
  1013. var dt = new DataTable ();
  1014. for (int c = 0; c < cols; c++) {
  1015. dt.Columns.Add ("Col" + c);
  1016. }
  1017. for (int r = 0; r < rows; r++) {
  1018. var newRow = dt.NewRow ();
  1019. for (int c = 0; c < cols; c++) {
  1020. newRow [c] = $"R{r}C{c}";
  1021. }
  1022. dt.Rows.Add (newRow);
  1023. }
  1024. return dt;
  1025. }
  1026. [Fact, AutoInitShutdown]
  1027. public void Test_ScreenToCell ()
  1028. {
  1029. var tableView = GetTwoRowSixColumnTable ();
  1030. tableView.Redraw (tableView.Bounds);
  1031. // user can only scroll right so sees right indicator
  1032. // Because first column in table is A
  1033. string expected =
  1034. @"
  1035. │A│B│C│
  1036. ├─┼─┼─►
  1037. │1│2│3│
  1038. │1│2│3│";
  1039. TestHelpers.AssertDriverContentsAre (expected, output);
  1040. // ---------------- X=0 -----------------------
  1041. // click is before first cell
  1042. Assert.Null (tableView.ScreenToCell (0, 0));
  1043. Assert.Null (tableView.ScreenToCell (0, 1));
  1044. Assert.Null (tableView.ScreenToCell (0, 2));
  1045. Assert.Null (tableView.ScreenToCell (0, 3));
  1046. Assert.Null (tableView.ScreenToCell (0, 4));
  1047. // ---------------- X=1 -----------------------
  1048. // click in header
  1049. Assert.Null (tableView.ScreenToCell (1, 0));
  1050. // click in header row line
  1051. Assert.Null (tableView.ScreenToCell (1, 1));
  1052. // click in cell 0,0
  1053. Assert.Equal (new Point(0,0),tableView.ScreenToCell (1, 2));
  1054. // click in cell 0,1
  1055. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3));
  1056. // after last row
  1057. Assert.Null (tableView.ScreenToCell (1, 4));
  1058. // ---------------- X=2 -----------------------
  1059. // ( even though there is a horizontal dividing line here we treat it as a hit on the cell before)
  1060. // click in header
  1061. Assert.Null (tableView.ScreenToCell (2, 0));
  1062. // click in header row line
  1063. Assert.Null (tableView.ScreenToCell (2, 1));
  1064. // click in cell 0,0
  1065. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (2, 2));
  1066. // click in cell 0,1
  1067. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3));
  1068. // after last row
  1069. Assert.Null (tableView.ScreenToCell (2, 4));
  1070. // ---------------- X=3 -----------------------
  1071. // click in header
  1072. Assert.Null (tableView.ScreenToCell (3, 0));
  1073. // click in header row line
  1074. Assert.Null (tableView.ScreenToCell (3, 1));
  1075. // click in cell 1,0
  1076. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2));
  1077. // click in cell 1,1
  1078. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3));
  1079. // after last row
  1080. Assert.Null (tableView.ScreenToCell (3, 4));
  1081. }
  1082. [Fact, AutoInitShutdown]
  1083. public void Test_ScreenToCell_DataColumnOverload ()
  1084. {
  1085. var tableView = GetTwoRowSixColumnTable ();
  1086. tableView.Redraw (tableView.Bounds);
  1087. // user can only scroll right so sees right indicator
  1088. // Because first column in table is A
  1089. string expected =
  1090. @"
  1091. │A│B│C│
  1092. ├─┼─┼─►
  1093. │1│2│3│
  1094. │1│2│3│";
  1095. TestHelpers.AssertDriverContentsAre (expected, output);
  1096. DataColumn col;
  1097. // ---------------- X=0 -----------------------
  1098. // click is before first cell
  1099. Assert.Null (tableView.ScreenToCell (0, 0,out col));
  1100. Assert.Null (col);
  1101. Assert.Null (tableView.ScreenToCell (0, 1,out col));
  1102. Assert.Null (col);
  1103. Assert.Null (tableView.ScreenToCell (0, 2,out col));
  1104. Assert.Null (col);
  1105. Assert.Null (tableView.ScreenToCell (0, 3,out col));
  1106. Assert.Null (col);
  1107. Assert.Null (tableView.ScreenToCell (0, 4,out col));
  1108. Assert.Null (col);
  1109. // ---------------- X=1 -----------------------
  1110. // click in header
  1111. Assert.Null (tableView.ScreenToCell (1, 0, out col));
  1112. Assert.Equal ("A", col.ColumnName);
  1113. // click in header row line (click in the horizontal line below header counts as click in header above - consistent with the column hit box)
  1114. Assert.Null (tableView.ScreenToCell (1, 1, out col));
  1115. Assert.Equal ("A", col.ColumnName);
  1116. // click in cell 0,0
  1117. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (1, 2, out col));
  1118. Assert.Null (col);
  1119. // click in cell 0,1
  1120. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3, out col));
  1121. Assert.Null (col);
  1122. // after last row
  1123. Assert.Null (tableView.ScreenToCell (1, 4, out col));
  1124. Assert.Null (col);
  1125. // ---------------- X=2 -----------------------
  1126. // click in header
  1127. Assert.Null (tableView.ScreenToCell (2, 0, out col));
  1128. Assert.Equal ("A", col.ColumnName);
  1129. // click in header row line
  1130. Assert.Null (tableView.ScreenToCell (2, 1, out col));
  1131. Assert.Equal ("A", col.ColumnName);
  1132. // click in cell 0,0
  1133. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (2, 2, out col));
  1134. Assert.Null (col);
  1135. // click in cell 0,1
  1136. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3, out col));
  1137. Assert.Null (col);
  1138. // after last row
  1139. Assert.Null (tableView.ScreenToCell (2, 4, out col));
  1140. Assert.Null (col);
  1141. // ---------------- X=3 -----------------------
  1142. // click in header
  1143. Assert.Null (tableView.ScreenToCell (3, 0, out col));
  1144. Assert.Equal ("B", col.ColumnName);
  1145. // click in header row line
  1146. Assert.Null (tableView.ScreenToCell (3, 1, out col));
  1147. Assert.Equal ("B", col.ColumnName);
  1148. // click in cell 1,0
  1149. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2, out col));
  1150. Assert.Null (col);
  1151. // click in cell 1,1
  1152. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3, out col));
  1153. Assert.Null (col);
  1154. // after last row
  1155. Assert.Null (tableView.ScreenToCell (3, 4, out col));
  1156. Assert.Null (col);
  1157. }
  1158. private TableView GetTwoRowSixColumnTable ()
  1159. {
  1160. var tableView = new TableView ();
  1161. tableView.ColorScheme = Colors.TopLevel;
  1162. // 3 columns are visible
  1163. tableView.Bounds = new Rect (0, 0, 7, 5);
  1164. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1165. tableView.Style.ShowHorizontalHeaderOverline = false;
  1166. tableView.Style.AlwaysShowHeaders = true;
  1167. tableView.Style.SmoothHorizontalScrolling = true;
  1168. var dt = new DataTable ();
  1169. dt.Columns.Add ("A");
  1170. dt.Columns.Add ("B");
  1171. dt.Columns.Add ("C");
  1172. dt.Columns.Add ("D");
  1173. dt.Columns.Add ("E");
  1174. dt.Columns.Add ("F");
  1175. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1176. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1177. tableView.Table = dt;
  1178. return tableView;
  1179. }
  1180. }
  1181. }