TableViewTests.cs 62 KB

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