TableViewTests.cs 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Terminal.Gui;
  7. using Xunit;
  8. using System.Globalization;
  9. using Xunit.Abstractions;
  10. using System.Reflection;
  11. using Terminal.Gui.ViewTests;
  12. using System.Collections;
  13. using static Terminal.Gui.SpinnerStyle;
  14. namespace Terminal.Gui.ViewsTests {
  15. public class TableViewTests {
  16. readonly ITestOutputHelper output;
  17. public TableViewTests (ITestOutputHelper output)
  18. {
  19. this.output = output;
  20. }
  21. [Fact]
  22. public void EnsureValidScrollOffsets_WithNoCells ()
  23. {
  24. var tableView = new TableView ();
  25. Assert.Equal (0, tableView.RowOffset);
  26. Assert.Equal (0, tableView.ColumnOffset);
  27. // Set empty table
  28. tableView.Table = new DataTableSource (new DataTable ());
  29. // Since table has no rows or columns scroll offset should default to 0
  30. tableView.EnsureValidScrollOffsets ();
  31. Assert.Equal (0, tableView.RowOffset);
  32. Assert.Equal (0, tableView.ColumnOffset);
  33. }
  34. [Fact]
  35. public void EnsureValidScrollOffsets_LoadSmallerTable ()
  36. {
  37. var tableView = new TableView ();
  38. tableView.BeginInit (); tableView.EndInit ();
  39. tableView.Bounds = new Rect (0, 0, 25, 10);
  40. Assert.Equal (0, tableView.RowOffset);
  41. Assert.Equal (0, tableView.ColumnOffset);
  42. // Set big table
  43. tableView.Table = BuildTable (25, 50);
  44. // Scroll down and along
  45. tableView.RowOffset = 20;
  46. tableView.ColumnOffset = 10;
  47. tableView.EnsureValidScrollOffsets ();
  48. // The scroll should be valid at the moment
  49. Assert.Equal (20, tableView.RowOffset);
  50. Assert.Equal (10, tableView.ColumnOffset);
  51. // Set small table
  52. tableView.Table = BuildTable (2, 2);
  53. // Setting a small table should automatically trigger fixing the scroll offsets to ensure valid cells
  54. Assert.Equal (0, tableView.RowOffset);
  55. Assert.Equal (0, tableView.ColumnOffset);
  56. // Trying to set invalid indexes should not be possible
  57. tableView.RowOffset = 20;
  58. tableView.ColumnOffset = 10;
  59. Assert.Equal (1, tableView.RowOffset);
  60. Assert.Equal (1, tableView.ColumnOffset);
  61. }
  62. [Fact]
  63. [AutoInitShutdown]
  64. public void Redraw_EmptyTable ()
  65. {
  66. var tableView = new TableView ();
  67. tableView.ColorScheme = new ColorScheme ();
  68. tableView.Bounds = new Rect (0, 0, 25, 10);
  69. // Set a table with 1 column
  70. tableView.Table = BuildTable (1, 50, out var dt);
  71. tableView.Draw ();
  72. dt.Columns.Remove (dt.Columns [0]);
  73. tableView.Draw ();
  74. }
  75. [Fact]
  76. public void SelectedCellChanged_NotFiredForSameValue ()
  77. {
  78. var tableView = new TableView () {
  79. Table = BuildTable (25, 50)
  80. };
  81. bool called = false;
  82. tableView.SelectedCellChanged += (s, e) => { called = true; };
  83. Assert.Equal (0, tableView.SelectedColumn);
  84. Assert.False (called);
  85. // Changing value to same as it already was should not raise an event
  86. tableView.SelectedColumn = 0;
  87. Assert.False (called);
  88. tableView.SelectedColumn = 10;
  89. Assert.True (called);
  90. }
  91. [Fact]
  92. public void SelectedCellChanged_SelectedColumnIndexesCorrect ()
  93. {
  94. var tableView = new TableView () {
  95. Table = BuildTable (25, 50)
  96. };
  97. bool called = false;
  98. tableView.SelectedCellChanged += (s, e) => {
  99. called = true;
  100. Assert.Equal (0, e.OldCol);
  101. Assert.Equal (10, e.NewCol);
  102. };
  103. tableView.SelectedColumn = 10;
  104. Assert.True (called);
  105. }
  106. [Fact]
  107. public void SelectedCellChanged_SelectedRowIndexesCorrect ()
  108. {
  109. var tableView = new TableView () {
  110. Table = BuildTable (25, 50)
  111. };
  112. bool called = false;
  113. tableView.SelectedCellChanged += (s, e) => {
  114. called = true;
  115. Assert.Equal (0, e.OldRow);
  116. Assert.Equal (10, e.NewRow);
  117. };
  118. tableView.SelectedRow = 10;
  119. Assert.True (called);
  120. }
  121. [Fact]
  122. public void Test_SumColumnWidth_UnicodeLength ()
  123. {
  124. Assert.Equal (11, "hello there".EnumerateRunes ().Sum (c => c.GetColumns ()));
  125. // Creates a string with the peculiar (french?) r symbol
  126. String surrogate = "Les Mise" + Char.ConvertFromUtf32 (Int32.Parse ("0301", NumberStyles.HexNumber)) + "rables";
  127. // The unicode width of this string is shorter than the string length!
  128. Assert.Equal (14, surrogate.EnumerateRunes ().Sum (c => c.GetColumns ()));
  129. Assert.Equal (15, surrogate.Length);
  130. }
  131. [Fact]
  132. public void IsSelected_MultiSelectionOn_Vertical ()
  133. {
  134. var tableView = new TableView () {
  135. Table = BuildTable (25, 50),
  136. MultiSelect = true
  137. };
  138. // 3 cell vertical selection
  139. tableView.SetSelection (1, 1, false);
  140. tableView.SetSelection (1, 3, true);
  141. Assert.False (tableView.IsSelected (0, 0));
  142. Assert.False (tableView.IsSelected (1, 0));
  143. Assert.False (tableView.IsSelected (2, 0));
  144. Assert.False (tableView.IsSelected (0, 1));
  145. Assert.True (tableView.IsSelected (1, 1));
  146. Assert.False (tableView.IsSelected (2, 1));
  147. Assert.False (tableView.IsSelected (0, 2));
  148. Assert.True (tableView.IsSelected (1, 2));
  149. Assert.False (tableView.IsSelected (2, 2));
  150. Assert.False (tableView.IsSelected (0, 3));
  151. Assert.True (tableView.IsSelected (1, 3));
  152. Assert.False (tableView.IsSelected (2, 3));
  153. Assert.False (tableView.IsSelected (0, 4));
  154. Assert.False (tableView.IsSelected (1, 4));
  155. Assert.False (tableView.IsSelected (2, 4));
  156. }
  157. [Fact]
  158. public void IsSelected_MultiSelectionOn_Horizontal ()
  159. {
  160. var tableView = new TableView () {
  161. Table = BuildTable (25, 50),
  162. MultiSelect = true
  163. };
  164. // 2 cell horizontal selection
  165. tableView.SetSelection (1, 0, false);
  166. tableView.SetSelection (2, 0, true);
  167. Assert.False (tableView.IsSelected (0, 0));
  168. Assert.True (tableView.IsSelected (1, 0));
  169. Assert.True (tableView.IsSelected (2, 0));
  170. Assert.False (tableView.IsSelected (3, 0));
  171. Assert.False (tableView.IsSelected (0, 1));
  172. Assert.False (tableView.IsSelected (1, 1));
  173. Assert.False (tableView.IsSelected (2, 1));
  174. Assert.False (tableView.IsSelected (3, 1));
  175. }
  176. [Fact]
  177. public void IsSelected_MultiSelectionOn_BoxSelection ()
  178. {
  179. var tableView = new TableView () {
  180. Table = BuildTable (25, 50),
  181. MultiSelect = true
  182. };
  183. // 4 cell horizontal in box 2x2
  184. tableView.SetSelection (0, 0, false);
  185. tableView.SetSelection (1, 1, true);
  186. Assert.True (tableView.IsSelected (0, 0));
  187. Assert.True (tableView.IsSelected (1, 0));
  188. Assert.False (tableView.IsSelected (2, 0));
  189. Assert.True (tableView.IsSelected (0, 1));
  190. Assert.True (tableView.IsSelected (1, 1));
  191. Assert.False (tableView.IsSelected (2, 1));
  192. Assert.False (tableView.IsSelected (0, 2));
  193. Assert.False (tableView.IsSelected (1, 2));
  194. Assert.False (tableView.IsSelected (2, 2));
  195. }
  196. [AutoInitShutdown]
  197. [Fact]
  198. public void PageDown_ExcludesHeaders ()
  199. {
  200. var tableView = new TableView () {
  201. Table = BuildTable (25, 50),
  202. MultiSelect = true,
  203. Bounds = new Rect (0, 0, 10, 5)
  204. };
  205. // Header should take up 2 lines
  206. tableView.Style.ShowHorizontalHeaderOverline = false;
  207. tableView.Style.ShowHorizontalHeaderUnderline = true;
  208. tableView.Style.AlwaysShowHeaders = false;
  209. // ensure that TableView has the input focus
  210. Application.Top.Add (tableView);
  211. Application.Begin (Application.Top);
  212. Application.Top.FocusFirst ();
  213. Assert.True (tableView.HasFocus);
  214. Assert.Equal (0, tableView.RowOffset);
  215. tableView.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ()));
  216. // window height is 5 rows 2 are header so page down should give 3 new rows
  217. Assert.Equal (3, tableView.SelectedRow);
  218. Assert.Equal (1, tableView.RowOffset);
  219. // header is no longer visible so page down should give 5 new rows
  220. tableView.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ()));
  221. Assert.Equal (8, tableView.SelectedRow);
  222. Assert.Equal (4, tableView.RowOffset);
  223. }
  224. [Fact]
  225. public void DeleteRow_SelectAll_AdjustsSelectionToPreventOverrun ()
  226. {
  227. // create a 4 by 4 table
  228. var tableView = new TableView () {
  229. Table = BuildTable (4, 4, out var dt),
  230. MultiSelect = true,
  231. Bounds = new Rect (0, 0, 10, 5)
  232. };
  233. tableView.BeginInit (); tableView.EndInit ();
  234. tableView.SelectAll ();
  235. Assert.Equal (16, tableView.GetAllSelectedCells ().Count ());
  236. // delete one of the columns
  237. dt.Columns.RemoveAt (2);
  238. // table should now be 3x4
  239. Assert.Equal (12, tableView.GetAllSelectedCells ().Count ());
  240. // remove a row
  241. dt.Rows.RemoveAt (1);
  242. // table should now be 3x3
  243. Assert.Equal (9, tableView.GetAllSelectedCells ().Count ());
  244. }
  245. [Fact]
  246. public void DeleteRow_SelectLastRow_AdjustsSelectionToPreventOverrun ()
  247. {
  248. // create a 4 by 4 table
  249. var tableView = new TableView () {
  250. Table = BuildTable (4, 4, out var dt),
  251. MultiSelect = true,
  252. Bounds = new Rect (0, 0, 10, 5)
  253. };
  254. tableView.BeginInit (); tableView.EndInit ();
  255. tableView.ChangeSelectionToEndOfTable (false);
  256. // select the last row
  257. tableView.MultiSelectedRegions.Clear ();
  258. tableView.MultiSelectedRegions.Push (new TableSelection (new Point (0, 3), new Rect (0, 3, 4, 1)));
  259. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  260. // remove a row
  261. dt.Rows.RemoveAt (0);
  262. tableView.EnsureValidSelection ();
  263. // since the selection no longer exists it should be removed
  264. Assert.Empty (tableView.MultiSelectedRegions);
  265. }
  266. [Theory]
  267. [InlineData (true)]
  268. [InlineData (false)]
  269. public void GetAllSelectedCells_SingleCellSelected_ReturnsOne (bool multiSelect)
  270. {
  271. var tableView = new TableView () {
  272. Table = BuildTable (3, 3),
  273. MultiSelect = multiSelect,
  274. Bounds = new Rect (0, 0, 10, 5)
  275. };
  276. tableView.BeginInit (); tableView.EndInit ();
  277. tableView.SetSelection (1, 1, false);
  278. Assert.Single (tableView.GetAllSelectedCells ());
  279. Assert.Equal (new Point (1, 1), tableView.GetAllSelectedCells ().Single ());
  280. }
  281. [Fact]
  282. public void GetAllSelectedCells_SquareSelection_ReturnsFour ()
  283. {
  284. var tableView = new TableView () {
  285. Table = BuildTable (3, 3),
  286. MultiSelect = true,
  287. Bounds = new Rect (0, 0, 10, 5)
  288. };
  289. tableView.BeginInit (); tableView.EndInit ();
  290. // move cursor to 1,1
  291. tableView.SetSelection (1, 1, false);
  292. // spread selection across to 2,2 (e.g. shift+right then shift+down)
  293. tableView.SetSelection (2, 2, true);
  294. var selected = tableView.GetAllSelectedCells ().ToArray ();
  295. Assert.Equal (4, selected.Length);
  296. Assert.Equal (new Point (1, 1), selected [0]);
  297. Assert.Equal (new Point (2, 1), selected [1]);
  298. Assert.Equal (new Point (1, 2), selected [2]);
  299. Assert.Equal (new Point (2, 2), selected [3]);
  300. }
  301. [Fact]
  302. public void GetAllSelectedCells_SquareSelection_FullRowSelect ()
  303. {
  304. var tableView = new TableView () {
  305. Table = BuildTable (3, 3),
  306. MultiSelect = true,
  307. FullRowSelect = true,
  308. Bounds = new Rect (0, 0, 10, 5)
  309. };
  310. tableView.BeginInit (); tableView.EndInit ();
  311. // move cursor to 1,1
  312. tableView.SetSelection (1, 1, false);
  313. // spread selection across to 2,2 (e.g. shift+right then shift+down)
  314. tableView.SetSelection (2, 2, true);
  315. var selected = tableView.GetAllSelectedCells ().ToArray ();
  316. Assert.Equal (6, selected.Length);
  317. Assert.Equal (new Point (0, 1), selected [0]);
  318. Assert.Equal (new Point (1, 1), selected [1]);
  319. Assert.Equal (new Point (2, 1), selected [2]);
  320. Assert.Equal (new Point (0, 2), selected [3]);
  321. Assert.Equal (new Point (1, 2), selected [4]);
  322. Assert.Equal (new Point (2, 2), selected [5]);
  323. }
  324. [Fact]
  325. public void GetAllSelectedCells_TwoIsolatedSelections_ReturnsSix ()
  326. {
  327. var tableView = new TableView () {
  328. Table = BuildTable (20, 20),
  329. MultiSelect = true,
  330. Bounds = new Rect (0, 0, 10, 5)
  331. };
  332. tableView.BeginInit (); tableView.EndInit ();
  333. /*
  334. Sets up disconnected selections like:
  335. 00000000000
  336. 01100000000
  337. 01100000000
  338. 00000001100
  339. 00000000000
  340. */
  341. tableView.MultiSelectedRegions.Clear ();
  342. tableView.MultiSelectedRegions.Push (new TableSelection (new Point (1, 1), new Rect (1, 1, 2, 2)));
  343. tableView.MultiSelectedRegions.Push (new TableSelection (new Point (7, 3), new Rect (7, 3, 2, 1)));
  344. tableView.SelectedColumn = 8;
  345. tableView.SelectedRow = 3;
  346. var selected = tableView.GetAllSelectedCells ().ToArray ();
  347. Assert.Equal (6, selected.Length);
  348. Assert.Equal (new Point (1, 1), selected [0]);
  349. Assert.Equal (new Point (2, 1), selected [1]);
  350. Assert.Equal (new Point (1, 2), selected [2]);
  351. Assert.Equal (new Point (2, 2), selected [3]);
  352. Assert.Equal (new Point (7, 3), selected [4]);
  353. Assert.Equal (new Point (8, 3), selected [5]);
  354. }
  355. [Fact, AutoInitShutdown]
  356. public void TableView_ShowHeadersFalse_AndNoHeaderLines ()
  357. {
  358. var tv = GetABCDEFTableView (out _);
  359. tv.Bounds = new Rect (0, 0, 5, 5);
  360. tv.Style.ShowHeaders = false;
  361. tv.Style.ShowHorizontalHeaderOverline = false;
  362. tv.Style.ShowHorizontalHeaderUnderline = false;
  363. tv.Draw ();
  364. string expected = @"
  365. │1│2│
  366. ";
  367. TestHelpers.AssertDriverContentsAre (expected, output);
  368. }
  369. [Fact, AutoInitShutdown]
  370. public void TableView_ShowHeadersFalse_OverlineTrue ()
  371. {
  372. var tv = GetABCDEFTableView (out _);
  373. tv.Bounds = new Rect (0, 0, 5, 5);
  374. tv.Style.ShowHeaders = false;
  375. tv.Style.ShowHorizontalHeaderOverline = true;
  376. tv.Style.ShowHorizontalHeaderUnderline = false;
  377. tv.Draw ();
  378. string expected = @"
  379. ┌─┬─┐
  380. │1│2│
  381. ";
  382. TestHelpers.AssertDriverContentsAre (expected, output);
  383. }
  384. [Fact, AutoInitShutdown]
  385. public void TableView_ShowHeadersFalse_UnderlineTrue ()
  386. {
  387. var tv = GetABCDEFTableView (out _);
  388. tv.Bounds = new Rect (0, 0, 5, 5);
  389. tv.Style.ShowHeaders = false;
  390. tv.Style.ShowHorizontalHeaderOverline = false;
  391. tv.Style.ShowHorizontalHeaderUnderline = true;
  392. // Horizontal scrolling option is part of the underline
  393. tv.Style.ShowHorizontalScrollIndicators = true;
  394. tv.Draw ();
  395. string expected = @"
  396. ├─┼─►
  397. │1│2│
  398. ";
  399. TestHelpers.AssertDriverContentsAre (expected, output);
  400. }
  401. [Fact, AutoInitShutdown]
  402. public void TableView_ShowHeadersFalse_AllLines ()
  403. {
  404. var tv = GetABCDEFTableView (out _);
  405. tv.Bounds = new Rect (0, 0, 5, 5);
  406. tv.Style.ShowHeaders = false;
  407. tv.Style.ShowHorizontalHeaderOverline = true;
  408. tv.Style.ShowHorizontalHeaderUnderline = true;
  409. // Horizontal scrolling option is part of the underline
  410. tv.Style.ShowHorizontalScrollIndicators = true;
  411. tv.Draw ();
  412. string expected = @"
  413. ┌─┬─┐
  414. ├─┼─►
  415. │1│2│
  416. ";
  417. TestHelpers.AssertDriverContentsAre (expected, output);
  418. }
  419. [Fact, AutoInitShutdown]
  420. public void TableView_ExpandLastColumn_True ()
  421. {
  422. var tv = SetUpMiniTable ();
  423. // the thing we are testing
  424. tv.Style.ExpandLastColumn = true;
  425. tv.Draw ();
  426. string expected = @"
  427. ┌─┬──────┐
  428. │A│B │
  429. ├─┼──────┤
  430. │1│2 │
  431. ";
  432. TestHelpers.AssertDriverContentsAre (expected, output);
  433. // Shutdown must be called to safely clean up Application if Init has been called
  434. Application.Shutdown ();
  435. }
  436. [Fact, AutoInitShutdown]
  437. public void TableView_ExpandLastColumn_False ()
  438. {
  439. var tv = SetUpMiniTable ();
  440. // the thing we are testing
  441. tv.Style.ExpandLastColumn = false;
  442. tv.Draw ();
  443. string expected = @"
  444. ┌─┬─┬────┐
  445. │A│B│ │
  446. ├─┼─┼────┤
  447. │1│2│ │
  448. ";
  449. TestHelpers.AssertDriverContentsAre (expected, output);
  450. // Shutdown must be called to safely clean up Application if Init has been called
  451. Application.Shutdown ();
  452. }
  453. [Fact, AutoInitShutdown]
  454. public void TableView_ExpandLastColumn_False_ExactBounds ()
  455. {
  456. var tv = SetUpMiniTable ();
  457. // the thing we are testing
  458. tv.Style.ExpandLastColumn = false;
  459. // width exactly matches the max col widths
  460. tv.Bounds = new Rect (0, 0, 5, 4);
  461. tv.Draw ();
  462. string expected = @"
  463. ┌─┬─┐
  464. │A│B│
  465. ├─┼─┤
  466. │1│2│
  467. ";
  468. TestHelpers.AssertDriverContentsAre (expected, output);
  469. // Shutdown must be called to safely clean up Application if Init has been called
  470. Application.Shutdown ();
  471. }
  472. [Fact]
  473. [AutoInitShutdown]
  474. public void TableView_Activate ()
  475. {
  476. string activatedValue = null;
  477. var tv = new TableView (BuildTable (1, 1));
  478. tv.CellActivated += (s, c) => activatedValue = c.Table [c.Row, c.Col].ToString ();
  479. Application.Top.Add (tv);
  480. Application.Begin (Application.Top);
  481. // pressing enter should activate the first cell (selected cell)
  482. tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  483. Assert.Equal ("R0C0", activatedValue);
  484. // reset the test
  485. activatedValue = null;
  486. // clear keybindings and ensure that Enter does not trigger the event anymore
  487. tv.ClearKeyBindings ();
  488. tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
  489. Assert.Null (activatedValue);
  490. // New method for changing the activation key
  491. tv.AddKeyBinding (Key.z, Command.Accept);
  492. tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ()));
  493. Assert.Equal ("R0C0", activatedValue);
  494. // reset the test
  495. activatedValue = null;
  496. tv.ClearKeyBindings ();
  497. // Old method for changing the activation key
  498. tv.CellActivationKey = Key.z;
  499. tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ()));
  500. Assert.Equal ("R0C0", activatedValue);
  501. }
  502. [Fact, AutoInitShutdown]
  503. public void TableViewMultiSelect_CannotFallOffLeft ()
  504. {
  505. var tv = SetUpMiniTable (out var dt);
  506. dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  507. tv.MultiSelect = true;
  508. tv.SelectedColumn = 1;
  509. tv.SelectedRow = 1;
  510. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  511. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  512. // this next shift left should be ignored because we are already at the bounds
  513. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  514. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  515. Assert.Equal (0, tv.SelectedColumn);
  516. Assert.Equal (1, tv.SelectedRow);
  517. Application.Shutdown ();
  518. }
  519. [Fact, AutoInitShutdown]
  520. public void TableViewMultiSelect_CannotFallOffRight ()
  521. {
  522. var tv = SetUpMiniTable (out var dt);
  523. dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  524. tv.MultiSelect = true;
  525. tv.SelectedColumn = 0;
  526. tv.SelectedRow = 1;
  527. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  528. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  529. // this next shift right should be ignored because we are already at the right bounds
  530. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  531. Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
  532. Assert.Equal (1, tv.SelectedColumn);
  533. Assert.Equal (1, tv.SelectedRow);
  534. Application.Shutdown ();
  535. }
  536. [Fact, AutoInitShutdown]
  537. public void TableViewMultiSelect_CannotFallOffBottom ()
  538. {
  539. var tv = SetUpMiniTable (out var dt);
  540. dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  541. tv.MultiSelect = true;
  542. tv.SelectedColumn = 0;
  543. tv.SelectedRow = 0;
  544. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  545. tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
  546. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  547. // this next moves should be ignored because we already selected the whole table
  548. tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
  549. tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
  550. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  551. Assert.Equal (1, tv.SelectedColumn);
  552. Assert.Equal (1, tv.SelectedRow);
  553. Application.Shutdown ();
  554. }
  555. [Fact, AutoInitShutdown]
  556. public void TableViewMultiSelect_CannotFallOffTop ()
  557. {
  558. var tv = SetUpMiniTable (out var dt);
  559. dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  560. tv.LayoutSubviews ();
  561. tv.MultiSelect = true;
  562. tv.SelectedColumn = 1;
  563. tv.SelectedRow = 1;
  564. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  565. tv.ProcessKey (new KeyEvent (Key.CursorUp | Key.ShiftMask, new KeyModifiers { Shift = true }));
  566. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  567. // this next moves should be ignored because we already selected the whole table
  568. tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
  569. tv.ProcessKey (new KeyEvent (Key.CursorUp | Key.ShiftMask, new KeyModifiers { Shift = true }));
  570. Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
  571. Assert.Equal (0, tv.SelectedColumn);
  572. Assert.Equal (0, tv.SelectedRow);
  573. Application.Shutdown ();
  574. }
  575. [Fact, AutoInitShutdown]
  576. public void TestShiftClick_MultiSelect_TwoRowTable_FullRowSelect ()
  577. {
  578. var tv = GetTwoRowSixColumnTable ();
  579. tv.LayoutSubviews ();
  580. tv.MultiSelect = true;
  581. // Clicking in bottom row
  582. tv.MouseEvent (new MouseEvent {
  583. X = 1,
  584. Y = 3,
  585. Flags = MouseFlags.Button1Clicked
  586. });
  587. // should select that row
  588. Assert.Equal (1, tv.SelectedRow);
  589. // shift clicking top row
  590. tv.MouseEvent (new MouseEvent {
  591. X = 1,
  592. Y = 2,
  593. Flags = MouseFlags.Button1Clicked | MouseFlags.ButtonShift
  594. });
  595. // should extend the selection
  596. Assert.Equal (0, tv.SelectedRow);
  597. var selected = tv.GetAllSelectedCells ().ToArray ();
  598. Assert.Contains (new Point (0, 0), selected);
  599. Assert.Contains (new Point (0, 1), selected);
  600. }
  601. [Fact, AutoInitShutdown]
  602. public void TestControlClick_MultiSelect_ThreeRowTable_FullRowSelect ()
  603. {
  604. var tv = GetTwoRowSixColumnTable (out var dt);
  605. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  606. tv.LayoutSubviews ();
  607. tv.MultiSelect = true;
  608. // Clicking in bottom row
  609. tv.MouseEvent (new MouseEvent {
  610. X = 1,
  611. Y = 4,
  612. Flags = MouseFlags.Button1Clicked
  613. });
  614. // should select that row
  615. Assert.Equal (2, tv.SelectedRow);
  616. // shift clicking top row
  617. tv.MouseEvent (new MouseEvent {
  618. X = 1,
  619. Y = 2,
  620. Flags = MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl
  621. });
  622. // should extend the selection
  623. // to include bottom and top row but not middle
  624. Assert.Equal (0, tv.SelectedRow);
  625. var selected = tv.GetAllSelectedCells ().ToArray ();
  626. Assert.Contains (new Point (0, 0), selected);
  627. Assert.DoesNotContain (new Point (0, 1), selected);
  628. Assert.Contains (new Point (0, 2), selected);
  629. }
  630. [Theory, AutoInitShutdown]
  631. [InlineData (false)]
  632. [InlineData (true)]
  633. public void TableView_ColorTests_FocusedOrNot (bool focused)
  634. {
  635. var tv = SetUpMiniTable ();
  636. tv.LayoutSubviews ();
  637. // width exactly matches the max col widths
  638. tv.Bounds = new Rect (0, 0, 5, 4);
  639. // private method for forcing the view to be focused/not focused
  640. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  641. // when the view is/isn't focused
  642. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  643. tv.Draw ();
  644. string expected = @"
  645. ┌─┬─┐
  646. │A│B│
  647. ├─┼─┤
  648. │1│2│
  649. ";
  650. TestHelpers.AssertDriverContentsAre (expected, output);
  651. string expectedColors = @"
  652. 00000
  653. 00000
  654. 00000
  655. 01000
  656. ";
  657. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  658. // 0
  659. tv.ColorScheme.Normal,
  660. // 1
  661. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal});
  662. Application.Shutdown ();
  663. }
  664. [Theory, AutoInitShutdown]
  665. [InlineData (false)]
  666. [InlineData (true)]
  667. public void TableView_ColorTests_InvertSelectedCellFirstCharacter (bool focused)
  668. {
  669. var tv = SetUpMiniTable ();
  670. tv.Style.InvertSelectedCellFirstCharacter = true;
  671. tv.LayoutSubviews ();
  672. // width exactly matches the max col widths
  673. tv.Bounds = new Rect (0, 0, 5, 4);
  674. // private method for forcing the view to be focused/not focused
  675. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  676. // when the view is/isn't focused
  677. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  678. tv.Draw ();
  679. string expected = @"
  680. ┌─┬─┐
  681. │A│B│
  682. ├─┼─┤
  683. │1│2│
  684. ";
  685. TestHelpers.AssertDriverContentsAre (expected, output);
  686. string expectedColors = @"
  687. 00000
  688. 00000
  689. 00000
  690. 01000
  691. ";
  692. var invertHotFocus = new Attribute (tv.ColorScheme.HotFocus.Background, tv.ColorScheme.HotFocus.Foreground);
  693. var invertHotNormal = new Attribute (tv.ColorScheme.HotNormal.Background, tv.ColorScheme.HotNormal.Foreground);
  694. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  695. // 0
  696. tv.ColorScheme.Normal,
  697. // 1
  698. focused ? invertHotFocus : invertHotNormal});
  699. Application.Shutdown ();
  700. }
  701. [Theory, AutoInitShutdown]
  702. [InlineData (false)]
  703. [InlineData (true)]
  704. public void TableView_ColorsTest_RowColorGetter (bool focused)
  705. {
  706. var tv = SetUpMiniTable (out DataTable dt);
  707. tv.LayoutSubviews ();
  708. // width exactly matches the max col widths
  709. tv.Bounds = new Rect (0, 0, 5, 4);
  710. var rowHighlight = new ColorScheme () {
  711. Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
  712. HotNormal = Attribute.Make (Color.Green, Color.Blue),
  713. HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
  714. Focus = Attribute.Make (Color.Cyan, Color.Magenta),
  715. };
  716. // when B is 2 use the custom highlight colour for the row
  717. tv.Style.RowColorGetter += (e) => Convert.ToInt32 (e.Table [e.RowIndex, 1]) == 2 ? rowHighlight : null;
  718. // private method for forcing the view to be focused/not focused
  719. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  720. // when the view is/isn't focused
  721. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  722. tv.Draw ();
  723. string expected = @"
  724. ┌─┬─┐
  725. │A│B│
  726. ├─┼─┤
  727. │1│2│
  728. ";
  729. TestHelpers.AssertDriverContentsAre (expected, output);
  730. string expectedColors = @"
  731. 00000
  732. 00000
  733. 00000
  734. 21222
  735. ";
  736. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  737. // 0
  738. tv.ColorScheme.Normal,
  739. // 1
  740. focused ? rowHighlight.HotFocus : rowHighlight.HotNormal,
  741. // 2
  742. rowHighlight.Normal});
  743. // change the value in the table so that
  744. // it no longer matches the RowColorGetter
  745. // delegate conditional ( which checks for
  746. // the value 2)
  747. dt.Rows [0] [1] = 5;
  748. tv.Draw ();
  749. expected = @"
  750. ┌─┬─┐
  751. │A│B│
  752. ├─┼─┤
  753. │1│5│
  754. ";
  755. TestHelpers.AssertDriverContentsAre (expected, output);
  756. expectedColors = @"
  757. 00000
  758. 00000
  759. 00000
  760. 01000
  761. ";
  762. // now we only see 2 colors used (the selected cell color and Normal
  763. // rowHighlight should no longer be used because the delegate returned null
  764. // (now that the cell value is 5 - which does not match the conditional)
  765. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  766. // 0
  767. tv.ColorScheme.Normal,
  768. // 1
  769. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
  770. // Shutdown must be called to safely clean up Application if Init has been called
  771. Application.Shutdown ();
  772. }
  773. [Theory, AutoInitShutdown]
  774. [InlineData (false)]
  775. [InlineData (true)]
  776. public void TableView_ColorsTest_ColorGetter (bool focused)
  777. {
  778. var tv = SetUpMiniTable (out var dt);
  779. tv.LayoutSubviews ();
  780. // width exactly matches the max col widths
  781. tv.Bounds = new Rect (0, 0, 5, 4);
  782. // Create a style for column B
  783. var bStyle = tv.Style.GetOrCreateColumnStyle (1);
  784. // when B is 2 use the custom highlight colour
  785. var cellHighlight = new ColorScheme () {
  786. Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
  787. HotNormal = Attribute.Make (Color.Green, Color.Blue),
  788. HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
  789. Focus = Attribute.Make (Color.Cyan, Color.Magenta),
  790. };
  791. bStyle.ColorGetter = (a) => Convert.ToInt32 (a.CellValue) == 2 ? cellHighlight : null;
  792. // private method for forcing the view to be focused/not focused
  793. var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
  794. // when the view is/isn't focused
  795. setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
  796. tv.Draw ();
  797. string expected = @"
  798. ┌─┬─┐
  799. │A│B│
  800. ├─┼─┤
  801. │1│2│
  802. ";
  803. TestHelpers.AssertDriverContentsAre (expected, output);
  804. string expectedColors = @"
  805. 00000
  806. 00000
  807. 00000
  808. 01020
  809. ";
  810. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  811. // 0
  812. tv.ColorScheme.Normal,
  813. // 1
  814. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal,
  815. // 2
  816. cellHighlight.Normal});
  817. // change the value in the table so that
  818. // it no longer matches the ColorGetter
  819. // delegate conditional ( which checks for
  820. // the value 2)
  821. dt.Rows [0] [1] = 5;
  822. tv.Draw ();
  823. expected = @"
  824. ┌─┬─┐
  825. │A│B│
  826. ├─┼─┤
  827. │1│5│
  828. ";
  829. TestHelpers.AssertDriverContentsAre (expected, output);
  830. expectedColors = @"
  831. 00000
  832. 00000
  833. 00000
  834. 01000
  835. ";
  836. // now we only see 2 colors used (the selected cell color and Normal
  837. // cellHighlight should no longer be used because the delegate returned null
  838. // (now that the cell value is 5 - which does not match the conditional)
  839. TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
  840. // 0
  841. tv.ColorScheme.Normal,
  842. // 1
  843. focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
  844. // Shutdown must be called to safely clean up Application if Init has been called
  845. Application.Shutdown ();
  846. }
  847. private TableView SetUpMiniTable ()
  848. {
  849. return SetUpMiniTable (out _);
  850. }
  851. private TableView SetUpMiniTable (out DataTable dt)
  852. {
  853. var tv = new TableView ();
  854. tv.BeginInit (); tv.EndInit ();
  855. tv.Bounds = new Rect (0, 0, 10, 4);
  856. dt = new DataTable ();
  857. dt.Columns.Add ("A");
  858. dt.Columns.Add ("B");
  859. dt.Rows.Add (1, 2);
  860. tv.Table = new DataTableSource (dt);
  861. tv.Style.GetOrCreateColumnStyle (0).MinWidth = 1;
  862. tv.Style.GetOrCreateColumnStyle (0).MinWidth = 1;
  863. tv.Style.GetOrCreateColumnStyle (1).MaxWidth = 1;
  864. tv.Style.GetOrCreateColumnStyle (1).MaxWidth = 1;
  865. tv.ColorScheme = Colors.Base;
  866. return tv;
  867. }
  868. [Fact]
  869. [AutoInitShutdown]
  870. public void ScrollDown_OneLineAtATime ()
  871. {
  872. var tableView = new TableView ();
  873. tableView.BeginInit (); tableView.EndInit ();
  874. // Set big table
  875. tableView.Table = BuildTable (25, 50);
  876. // 1 header + 4 rows visible
  877. tableView.Bounds = new Rect (0, 0, 25, 5);
  878. tableView.Style.ShowHorizontalHeaderUnderline = false;
  879. tableView.Style.ShowHorizontalHeaderOverline = false;
  880. tableView.Style.AlwaysShowHeaders = true;
  881. // select last row
  882. tableView.SelectedRow = 3; // row is 0 indexed so this is the 4th visible row
  883. // Scroll down
  884. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorDown });
  885. // Scrolled off the page by 1 row so it should only have moved down 1 line of RowOffset
  886. Assert.Equal (4, tableView.SelectedRow);
  887. Assert.Equal (1, tableView.RowOffset);
  888. }
  889. [Fact, AutoInitShutdown]
  890. public void ScrollRight_SmoothScrolling ()
  891. {
  892. var tableView = new TableView ();
  893. tableView.BeginInit (); tableView.EndInit ();
  894. tableView.ColorScheme = Colors.TopLevel;
  895. tableView.LayoutSubviews ();
  896. // 3 columns are visibile
  897. tableView.Bounds = new Rect (0, 0, 7, 5);
  898. tableView.Style.ShowHorizontalHeaderUnderline = false;
  899. tableView.Style.ShowHorizontalHeaderOverline = false;
  900. tableView.Style.AlwaysShowHeaders = true;
  901. tableView.Style.SmoothHorizontalScrolling = true;
  902. var dt = new DataTable ();
  903. dt.Columns.Add ("A");
  904. dt.Columns.Add ("B");
  905. dt.Columns.Add ("C");
  906. dt.Columns.Add ("D");
  907. dt.Columns.Add ("E");
  908. dt.Columns.Add ("F");
  909. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  910. tableView.Table = new DataTableSource (dt);
  911. // select last visible column
  912. tableView.SelectedColumn = 2; // column C
  913. tableView.Draw ();
  914. string expected =
  915. @"
  916. │A│B│C│
  917. │1│2│3│";
  918. TestHelpers.AssertDriverContentsAre (expected, output);
  919. // Scroll right
  920. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  921. tableView.Draw ();
  922. // Note that with SmoothHorizontalScrolling only a single new column
  923. // is exposed when scrolling right. This is not always the case though
  924. // sometimes if the leftmost column is long (i.e. A is a long column)
  925. // then when A is pushed off the screen multiple new columns could be exposed
  926. // (not just D but also E and F). This is because TableView never shows
  927. // 'half cells' or scrolls by console unit (scrolling is done by table row/column increments).
  928. expected =
  929. @"
  930. │B│C│D│
  931. │2│3│4│";
  932. TestHelpers.AssertDriverContentsAre (expected, output);
  933. // Shutdown must be called to safely clean up Application if Init has been called
  934. Application.Shutdown ();
  935. }
  936. [Fact, AutoInitShutdown]
  937. public void ScrollRight_WithoutSmoothScrolling ()
  938. {
  939. var tableView = new TableView ();
  940. tableView.BeginInit (); tableView.EndInit ();
  941. tableView.ColorScheme = Colors.TopLevel;
  942. // 3 columns are visibile
  943. tableView.Bounds = new Rect (0, 0, 7, 5);
  944. tableView.Style.ShowHorizontalHeaderUnderline = false;
  945. tableView.Style.ShowHorizontalHeaderOverline = false;
  946. tableView.Style.AlwaysShowHeaders = true;
  947. tableView.Style.SmoothHorizontalScrolling = false;
  948. var dt = new DataTable ();
  949. dt.Columns.Add ("A");
  950. dt.Columns.Add ("B");
  951. dt.Columns.Add ("C");
  952. dt.Columns.Add ("D");
  953. dt.Columns.Add ("E");
  954. dt.Columns.Add ("F");
  955. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  956. tableView.Table = new DataTableSource (dt);
  957. // select last visible column
  958. tableView.SelectedColumn = 2; // column C
  959. tableView.Draw ();
  960. string expected =
  961. @"
  962. │A│B│C│
  963. │1│2│3│";
  964. TestHelpers.AssertDriverContentsAre (expected, output);
  965. // Scroll right
  966. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  967. tableView.Draw ();
  968. // notice that without smooth scrolling we just update the first column
  969. // rendered in the table to the newly exposed column (D). This is fast
  970. // since we don't have to worry about repeatedly measuring the content
  971. // area as we scroll until the new column (D) is exposed. But it makes
  972. // the view 'jump' to expose all new columns
  973. expected =
  974. @"
  975. │D│E│F│
  976. │4│5│6│";
  977. TestHelpers.AssertDriverContentsAre (expected, output);
  978. // Shutdown must be called to safely clean up Application if Init has been called
  979. Application.Shutdown ();
  980. }
  981. private TableView GetABCDEFTableView (out DataTable dt)
  982. {
  983. var tableView = new TableView ();
  984. tableView.BeginInit (); tableView.EndInit ();
  985. tableView.ColorScheme = Colors.TopLevel;
  986. // 3 columns are visible
  987. tableView.Bounds = new Rect (0, 0, 7, 5);
  988. tableView.Style.ShowHorizontalHeaderUnderline = false;
  989. tableView.Style.ShowHorizontalHeaderOverline = false;
  990. tableView.Style.AlwaysShowHeaders = true;
  991. tableView.Style.SmoothHorizontalScrolling = false;
  992. dt = new DataTable ();
  993. dt.Columns.Add ("A");
  994. dt.Columns.Add ("B");
  995. dt.Columns.Add ("C");
  996. dt.Columns.Add ("D");
  997. dt.Columns.Add ("E");
  998. dt.Columns.Add ("F");
  999. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1000. tableView.Table = new DataTableSource (dt);
  1001. return tableView;
  1002. }
  1003. [Fact, AutoInitShutdown]
  1004. public void TestColumnStyle_VisibleFalse_IsNotRendered ()
  1005. {
  1006. var tableView = GetABCDEFTableView (out _);
  1007. tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
  1008. tableView.LayoutSubviews ();
  1009. tableView.Draw ();
  1010. string expected =
  1011. @"
  1012. │A│C│D│
  1013. │1│3│4│";
  1014. TestHelpers.AssertDriverContentsAre (expected, output);
  1015. }
  1016. [Fact, AutoInitShutdown]
  1017. public void TestColumnStyle_FirstColumnVisibleFalse_IsNotRendered ()
  1018. {
  1019. var tableView = GetABCDEFTableView (out DataTable dt);
  1020. tableView.Style.ShowHorizontalScrollIndicators = true;
  1021. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1022. tableView.Style.GetOrCreateColumnStyle (0).Visible = false;
  1023. tableView.LayoutSubviews ();
  1024. tableView.Draw ();
  1025. string expected =
  1026. @"
  1027. │B│C│D│
  1028. ├─┼─┼─►
  1029. │2│3│4│";
  1030. TestHelpers.AssertDriverContentsAre (expected, output);
  1031. }
  1032. [Fact, AutoInitShutdown]
  1033. public void TestColumnStyle_AllColumnsVisibleFalse_BehavesAsTableNull ()
  1034. {
  1035. var tableView = GetABCDEFTableView (out DataTable dt);
  1036. for (int i = 0; i < 6; i++) {
  1037. tableView.Style.GetOrCreateColumnStyle (i).Visible = false;
  1038. }
  1039. tableView.LayoutSubviews ();
  1040. // expect nothing to be rendered when all columns are invisible
  1041. string expected =
  1042. @"
  1043. ";
  1044. tableView.Draw ();
  1045. TestHelpers.AssertDriverContentsAre (expected, output);
  1046. // expect behavior to match when Table is null
  1047. tableView.Table = null;
  1048. tableView.Draw ();
  1049. TestHelpers.AssertDriverContentsAre (expected, output);
  1050. }
  1051. [Fact, AutoInitShutdown]
  1052. public void TestColumnStyle_RemainingColumnsInvisible_NoScrollIndicator ()
  1053. {
  1054. var tableView = GetABCDEFTableView (out DataTable dt);
  1055. tableView.Style.ShowHorizontalScrollIndicators = true;
  1056. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1057. tableView.LayoutSubviews ();
  1058. tableView.Draw ();
  1059. // normally we should have scroll indicators because DEF are of screen
  1060. string expected =
  1061. @"
  1062. │A│B│C│
  1063. ├─┼─┼─►
  1064. │1│2│3│";
  1065. TestHelpers.AssertDriverContentsAre (expected, output);
  1066. // but if DEF are invisible we shouldn't be showing the indicator
  1067. tableView.Style.GetOrCreateColumnStyle (3).Visible = false;
  1068. tableView.Style.GetOrCreateColumnStyle (4).Visible = false;
  1069. tableView.Style.GetOrCreateColumnStyle (5).Visible = false;
  1070. expected =
  1071. @"
  1072. │A│B│C│
  1073. ├─┼─┼─┤
  1074. │1│2│3│";
  1075. tableView.Draw ();
  1076. TestHelpers.AssertDriverContentsAre (expected, output);
  1077. }
  1078. [Fact, AutoInitShutdown]
  1079. public void TestColumnStyle_PreceedingColumnsInvisible_NoScrollIndicator ()
  1080. {
  1081. var tableView = GetABCDEFTableView (out DataTable dt);
  1082. tableView.Style.ShowHorizontalScrollIndicators = true;
  1083. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1084. tableView.ColumnOffset = 1;
  1085. tableView.LayoutSubviews ();
  1086. tableView.Draw ();
  1087. // normally we should have scroll indicators because A,E and F are of screen
  1088. string expected =
  1089. @"
  1090. │B│C│D│
  1091. ◄─┼─┼─►
  1092. │2│3│4│";
  1093. TestHelpers.AssertDriverContentsAre (expected, output);
  1094. // but if E and F are invisible so we shouldn't show right
  1095. tableView.Style.GetOrCreateColumnStyle (4).Visible = false;
  1096. tableView.Style.GetOrCreateColumnStyle (5).Visible = false;
  1097. expected =
  1098. @"
  1099. │B│C│D│
  1100. ◄─┼─┼─┤
  1101. │2│3│4│";
  1102. tableView.Draw ();
  1103. TestHelpers.AssertDriverContentsAre (expected, output);
  1104. // now also A is invisible so we cannot scroll in either direction
  1105. tableView.Style.GetOrCreateColumnStyle (0).Visible = false;
  1106. expected =
  1107. @"
  1108. │B│C│D│
  1109. ├─┼─┼─┤
  1110. │2│3│4│";
  1111. tableView.Draw ();
  1112. TestHelpers.AssertDriverContentsAre (expected, output);
  1113. }
  1114. [Fact, AutoInitShutdown]
  1115. public void TestColumnStyle_VisibleFalse_CursorStepsOverInvisibleColumns ()
  1116. {
  1117. var tableView = GetABCDEFTableView (out var dt);
  1118. tableView.LayoutSubviews ();
  1119. tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
  1120. tableView.SelectedColumn = 0;
  1121. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1122. // Expect the cursor navigation to skip over the invisible column(s)
  1123. Assert.Equal (2, tableView.SelectedColumn);
  1124. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1125. // Expect the cursor navigation backwards to skip over invisible column too
  1126. Assert.Equal (0, tableView.SelectedColumn);
  1127. }
  1128. [InlineData (true)]
  1129. [InlineData (false)]
  1130. [Theory, AutoInitShutdown]
  1131. public void TestColumnStyle_FirstColumnVisibleFalse_CursorStaysAt1 (bool useHome)
  1132. {
  1133. var tableView = GetABCDEFTableView (out var dt);
  1134. tableView.LayoutSubviews ();
  1135. tableView.Style.GetOrCreateColumnStyle (0).Visible = false;
  1136. tableView.SelectedColumn = 0;
  1137. Assert.Equal (0, tableView.SelectedColumn);
  1138. // column 0 is invisible so this method should move to 1
  1139. tableView.EnsureValidSelection ();
  1140. Assert.Equal (1, tableView.SelectedColumn);
  1141. tableView.ProcessKey (new KeyEvent {
  1142. Key = useHome ? Key.Home : Key.CursorLeft
  1143. });
  1144. // Expect the cursor to stay at 1
  1145. Assert.Equal (1, tableView.SelectedColumn);
  1146. }
  1147. [InlineData (true)]
  1148. [InlineData (false)]
  1149. [Theory, AutoInitShutdown]
  1150. public void TestMoveStartEnd_WithFullRowSelect (bool withFullRowSelect)
  1151. {
  1152. var tableView = GetTwoRowSixColumnTable ();
  1153. tableView.LayoutSubviews ();
  1154. tableView.FullRowSelect = withFullRowSelect;
  1155. tableView.SelectedRow = 1;
  1156. tableView.SelectedColumn = 1;
  1157. tableView.ProcessKey (new KeyEvent {
  1158. Key = Key.Home | Key.CtrlMask
  1159. });
  1160. if (withFullRowSelect) {
  1161. // Should not be any horizontal movement when
  1162. // using navigate to Start/End and FullRowSelect
  1163. Assert.Equal (1, tableView.SelectedColumn);
  1164. Assert.Equal (0, tableView.SelectedRow);
  1165. } else {
  1166. Assert.Equal (0, tableView.SelectedColumn);
  1167. Assert.Equal (0, tableView.SelectedRow);
  1168. }
  1169. tableView.ProcessKey (new KeyEvent {
  1170. Key = Key.End | Key.CtrlMask
  1171. });
  1172. if (withFullRowSelect) {
  1173. Assert.Equal (1, tableView.SelectedColumn);
  1174. Assert.Equal (1, tableView.SelectedRow);
  1175. } else {
  1176. Assert.Equal (5, tableView.SelectedColumn);
  1177. Assert.Equal (1, tableView.SelectedRow);
  1178. }
  1179. }
  1180. [InlineData (true)]
  1181. [InlineData (false)]
  1182. [Theory, AutoInitShutdown]
  1183. public void TestColumnStyle_LastColumnVisibleFalse_CursorStaysAt2 (bool useEnd)
  1184. {
  1185. var tableView = GetABCDEFTableView (out var dt);
  1186. tableView.LayoutSubviews ();
  1187. // select D
  1188. tableView.SelectedColumn = 3;
  1189. Assert.Equal (3, tableView.SelectedColumn);
  1190. tableView.Style.GetOrCreateColumnStyle (3).Visible = false;
  1191. tableView.Style.GetOrCreateColumnStyle (4).Visible = false;
  1192. tableView.Style.GetOrCreateColumnStyle (5).Visible = false;
  1193. // column D is invisible so this method should move to 2 (C)
  1194. tableView.EnsureValidSelection ();
  1195. Assert.Equal (2, tableView.SelectedColumn);
  1196. tableView.ProcessKey (new KeyEvent {
  1197. Key = useEnd ? Key.End : Key.CursorRight
  1198. });
  1199. // Expect the cursor to stay at 2
  1200. Assert.Equal (2, tableView.SelectedColumn);
  1201. }
  1202. [Fact, AutoInitShutdown]
  1203. public void TestColumnStyle_VisibleFalse_MultiSelected ()
  1204. {
  1205. var tableView = GetABCDEFTableView (out var dt);
  1206. tableView.LayoutSubviews ();
  1207. // user has rectangular selection
  1208. tableView.MultiSelectedRegions.Push (
  1209. new TableSelection (
  1210. new Point (0, 0),
  1211. new Rect (0, 0, 3, 1))
  1212. );
  1213. Assert.Equal (3, tableView.GetAllSelectedCells ().Count ());
  1214. Assert.True (tableView.IsSelected (0, 0));
  1215. Assert.True (tableView.IsSelected (1, 0));
  1216. Assert.True (tableView.IsSelected (2, 0));
  1217. Assert.False (tableView.IsSelected (3, 0));
  1218. // if middle column is invisible
  1219. tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
  1220. // it should not be included in the selection
  1221. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1222. Assert.True (tableView.IsSelected (0, 0));
  1223. Assert.False (tableView.IsSelected (1, 0));
  1224. Assert.True (tableView.IsSelected (2, 0));
  1225. Assert.False (tableView.IsSelected (3, 0));
  1226. Assert.DoesNotContain (new Point (1, 0), tableView.GetAllSelectedCells ());
  1227. }
  1228. [Fact, AutoInitShutdown]
  1229. public void TestColumnStyle_VisibleFalse_MultiSelectingStepsOverInvisibleColumns ()
  1230. {
  1231. var tableView = GetABCDEFTableView (out _);
  1232. tableView.LayoutSubviews ();
  1233. // if middle column is invisible
  1234. tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
  1235. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight | Key.ShiftMask });
  1236. // Selection should extend from A to C but skip B
  1237. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1238. Assert.True (tableView.IsSelected (0, 0));
  1239. Assert.False (tableView.IsSelected (1, 0));
  1240. Assert.True (tableView.IsSelected (2, 0));
  1241. Assert.False (tableView.IsSelected (3, 0));
  1242. Assert.DoesNotContain (new Point (1, 0), tableView.GetAllSelectedCells ());
  1243. }
  1244. [Fact, AutoInitShutdown]
  1245. public void TestToggleCells_MultiSelectOn ()
  1246. {
  1247. // 2 row table
  1248. var tableView = GetABCDEFTableView (out var dt);
  1249. tableView.LayoutSubviews ();
  1250. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1251. tableView.MultiSelect = true;
  1252. tableView.AddKeyBinding (Key.Space, Command.ToggleChecked);
  1253. var selectedCell = tableView.GetAllSelectedCells ().Single ();
  1254. Assert.Equal (0, selectedCell.X);
  1255. Assert.Equal (0, selectedCell.Y);
  1256. // Go Right
  1257. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1258. selectedCell = tableView.GetAllSelectedCells ().Single ();
  1259. Assert.Equal (1, selectedCell.X);
  1260. Assert.Equal (0, selectedCell.Y);
  1261. // Toggle Select
  1262. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1263. var m = tableView.MultiSelectedRegions.Single ();
  1264. Assert.True (m.IsToggled);
  1265. Assert.Equal (1, m.Origin.X);
  1266. Assert.Equal (0, m.Origin.Y);
  1267. selectedCell = tableView.GetAllSelectedCells ().Single ();
  1268. Assert.Equal (1, selectedCell.X);
  1269. Assert.Equal (0, selectedCell.Y);
  1270. // Go Left
  1271. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1272. // Both Toggled and Moved to should be selected
  1273. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1274. var s1 = tableView.GetAllSelectedCells ().ElementAt (0);
  1275. var s2 = tableView.GetAllSelectedCells ().ElementAt (1);
  1276. Assert.Equal (1, s1.X);
  1277. Assert.Equal (0, s1.Y);
  1278. Assert.Equal (0, s2.X);
  1279. Assert.Equal (0, s2.Y);
  1280. // Go Down
  1281. tableView.ProcessKey (new KeyEvent { Key = Key.CursorDown });
  1282. // Both Toggled and Moved to should be selected but not 0,0
  1283. // which we moved down from
  1284. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1285. s1 = tableView.GetAllSelectedCells ().ElementAt (0);
  1286. s2 = tableView.GetAllSelectedCells ().ElementAt (1);
  1287. Assert.Equal (1, s1.X);
  1288. Assert.Equal (0, s1.Y);
  1289. Assert.Equal (0, s2.X);
  1290. Assert.Equal (1, s2.Y);
  1291. // Go back to the toggled cell
  1292. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1293. tableView.ProcessKey (new KeyEvent { Key = Key.CursorUp });
  1294. // Toggle off
  1295. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1296. // Go Left
  1297. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1298. selectedCell = tableView.GetAllSelectedCells ().Single ();
  1299. Assert.Equal (0, selectedCell.X);
  1300. Assert.Equal (0, selectedCell.Y);
  1301. }
  1302. [Fact, AutoInitShutdown]
  1303. public void TestToggleCells_MultiSelectOn_FullRowSelect ()
  1304. {
  1305. // 2 row table
  1306. var tableView = GetABCDEFTableView (out var dt);
  1307. tableView.LayoutSubviews ();
  1308. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1309. tableView.FullRowSelect = true;
  1310. tableView.MultiSelect = true;
  1311. tableView.AddKeyBinding (Key.Space, Command.ToggleChecked);
  1312. // Toggle Select Cell 0,0
  1313. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1314. // Go Down
  1315. tableView.ProcessKey (new KeyEvent { Key = Key.CursorDown });
  1316. var m = tableView.MultiSelectedRegions.Single ();
  1317. Assert.True (m.IsToggled);
  1318. Assert.Equal (0, m.Origin.X);
  1319. Assert.Equal (0, m.Origin.Y);
  1320. //First row toggled and Second row active = 12 selected cells
  1321. Assert.Equal (12, tableView.GetAllSelectedCells ().Count ());
  1322. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1323. tableView.ProcessKey (new KeyEvent { Key = Key.CursorUp });
  1324. Assert.Single (tableView.MultiSelectedRegions.Where (r => r.IsToggled));
  1325. // Can untoggle at 1,0 even though 0,0 was initial toggle because FullRowSelect is on
  1326. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1327. Assert.Empty (tableView.MultiSelectedRegions.Where (r => r.IsToggled));
  1328. }
  1329. [Fact, AutoInitShutdown]
  1330. public void TestToggleCells_MultiSelectOn_SquareSelectToggled ()
  1331. {
  1332. // 3 row table
  1333. var tableView = GetABCDEFTableView (out var dt);
  1334. tableView.LayoutSubviews ();
  1335. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1336. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1337. tableView.MultiSelect = true;
  1338. tableView.AddKeyBinding (Key.Space, Command.ToggleChecked);
  1339. // Make a square selection
  1340. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorDown });
  1341. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorRight });
  1342. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  1343. // Toggle the square selected region on
  1344. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1345. // Go Right
  1346. tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  1347. //Toggled on square + the active cell (x=2,y=1)
  1348. Assert.Equal (5, tableView.GetAllSelectedCells ().Count ());
  1349. Assert.Equal (2, tableView.SelectedColumn);
  1350. Assert.Equal (1, tableView.SelectedRow);
  1351. // Untoggle the rectangular region by hitting toggle in
  1352. // any cell in that rect
  1353. tableView.ProcessKey (new KeyEvent { Key = Key.CursorUp });
  1354. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1355. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  1356. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1357. Assert.Single (tableView.GetAllSelectedCells ());
  1358. }
  1359. [Fact, AutoInitShutdown]
  1360. public void TestToggleCells_MultiSelectOn_Two_SquareSelects_BothToggled ()
  1361. {
  1362. // 6 row table
  1363. var tableView = GetABCDEFTableView (out var dt);
  1364. tableView.LayoutSubviews ();
  1365. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1366. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1367. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1368. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1369. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1370. tableView.MultiSelect = true;
  1371. tableView.AddKeyBinding (Key.Space, Command.ToggleChecked);
  1372. // Make first square selection (0,0 to 1,1)
  1373. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorDown });
  1374. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorRight });
  1375. tableView.ProcessKey (new KeyEvent { Key = Key.Space });
  1376. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  1377. // Make second square selection leaving 1 unselected line between them
  1378. tableView.ProcessKey (new KeyEvent { Key = Key.CursorLeft });
  1379. tableView.ProcessKey (new KeyEvent { Key = Key.CursorDown });
  1380. tableView.ProcessKey (new KeyEvent { Key = Key.CursorDown });
  1381. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorDown });
  1382. tableView.ProcessKey (new KeyEvent { Key = Key.ShiftMask | Key.CursorRight });
  1383. // 2 square selections
  1384. Assert.Equal (8, tableView.GetAllSelectedCells ().Count ());
  1385. }
  1386. [Theory, AutoInitShutdown]
  1387. [InlineData (new object [] { true, true })]
  1388. [InlineData (new object [] { false, true })]
  1389. [InlineData (new object [] { true, false })]
  1390. [InlineData (new object [] { false, false })]
  1391. public void TestColumnStyle_VisibleFalse_DoesNotEffect_EnsureSelectedCellIsVisible (bool smooth, bool invisibleCol)
  1392. {
  1393. var tableView = GetABCDEFTableView (out var dt);
  1394. tableView.LayoutSubviews ();
  1395. tableView.Style.SmoothHorizontalScrolling = smooth;
  1396. if (invisibleCol) {
  1397. tableView.Style.GetOrCreateColumnStyle (3).Visible = false;
  1398. }
  1399. // New TableView should have first cell selected
  1400. Assert.Equal (0, tableView.SelectedColumn);
  1401. // With no scrolling
  1402. Assert.Equal (0, tableView.ColumnOffset);
  1403. // A,B and C are visible on screen at the moment so these should have no effect
  1404. tableView.SelectedColumn = 1;
  1405. tableView.EnsureSelectedCellIsVisible ();
  1406. Assert.Equal (0, tableView.ColumnOffset);
  1407. tableView.SelectedColumn = 2;
  1408. tableView.EnsureSelectedCellIsVisible ();
  1409. Assert.Equal (0, tableView.ColumnOffset);
  1410. // Selecting D should move the visible table area to fit D onto the screen
  1411. tableView.SelectedColumn = 3;
  1412. tableView.EnsureSelectedCellIsVisible ();
  1413. Assert.Equal (smooth ? 1 : 3, tableView.ColumnOffset);
  1414. }
  1415. [Fact, AutoInitShutdown]
  1416. public void LongColumnTest ()
  1417. {
  1418. var tableView = new TableView ();
  1419. Application.Top.Add (tableView);
  1420. Application.Begin (Application.Top);
  1421. tableView.ColorScheme = Colors.TopLevel;
  1422. // 25 characters can be printed into table
  1423. tableView.Bounds = new Rect (0, 0, 25, 5);
  1424. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1425. tableView.Style.ShowHorizontalHeaderOverline = false;
  1426. tableView.Style.AlwaysShowHeaders = true;
  1427. tableView.Style.SmoothHorizontalScrolling = true;
  1428. var dt = new DataTable ();
  1429. dt.Columns.Add ("A");
  1430. dt.Columns.Add ("B");
  1431. dt.Columns.Add ("Very Long Column");
  1432. dt.Rows.Add (1, 2, new string ('a', 500));
  1433. dt.Rows.Add (1, 2, "aaa");
  1434. tableView.Table = new DataTableSource (dt);
  1435. tableView.LayoutSubviews ();
  1436. tableView.Draw ();
  1437. // default behaviour of TableView is not to render
  1438. // columns unless there is sufficient space
  1439. string expected =
  1440. @"
  1441. │A│B │
  1442. ├─┼─────────────────────►
  1443. │1│2 │
  1444. │1│2 │
  1445. ";
  1446. TestHelpers.AssertDriverContentsAre (expected, output);
  1447. // get a style for the long column
  1448. var style = tableView.Style.GetOrCreateColumnStyle (2);
  1449. // one way the API user can fix this for long columns
  1450. // is to specify a MinAcceptableWidth for the column
  1451. style.MaxWidth = 10;
  1452. tableView.LayoutSubviews ();
  1453. tableView.Draw ();
  1454. expected =
  1455. @"
  1456. │A│B│Very Long Column │
  1457. ├─┼─┼───────────────────┤
  1458. │1│2│aaaaaaaaaaaaaaaaaaa│
  1459. │1│2│aaa │
  1460. ";
  1461. TestHelpers.AssertDriverContentsAre (expected, output);
  1462. // revert the style change
  1463. style.MaxWidth = TableView.DefaultMaxCellWidth;
  1464. // another way API user can fix problem is to implement
  1465. // RepresentationGetter and apply max length there
  1466. style.RepresentationGetter = (s) => {
  1467. return s.ToString ().Length < 15 ? s.ToString () : s.ToString ().Substring (0, 13) + "...";
  1468. };
  1469. tableView.LayoutSubviews ();
  1470. tableView.Draw ();
  1471. expected =
  1472. @"
  1473. │A│B│Very Long Column │
  1474. ├─┼─┼───────────────────┤
  1475. │1│2│aaaaaaaaaaaaa... │
  1476. │1│2│aaa │
  1477. ";
  1478. TestHelpers.AssertDriverContentsAre (expected, output);
  1479. // revert style change
  1480. style.RepresentationGetter = null;
  1481. // Both of the above methods rely on having a fixed
  1482. // size limit for the column. These are awkward if a
  1483. // table is resizeable e.g. Dim.Fill(). Ideally we want
  1484. // to render in any space available and truncate the content
  1485. // of the column dynamically so it fills the free space at
  1486. // the end of the table.
  1487. // We can now specify that the column can be any length
  1488. // (Up to MaxWidth) but the renderer can accept using
  1489. // less space down to this limit
  1490. style.MinAcceptableWidth = 5;
  1491. tableView.LayoutSubviews ();
  1492. tableView.Draw ();
  1493. expected =
  1494. @"
  1495. │A│B│Very Long Column │
  1496. ├─┼─┼───────────────────┤
  1497. │1│2│aaaaaaaaaaaaaaaaaaa│
  1498. │1│2│aaa │
  1499. ";
  1500. TestHelpers.AssertDriverContentsAre (expected, output);
  1501. // Now test making the width too small for the MinAcceptableWidth
  1502. // the Column won't fit so should not be rendered
  1503. var driver = ((FakeDriver)Application.Driver);
  1504. driver.ClearContents ();
  1505. tableView.Bounds = new Rect (0, 0, 9, 5);
  1506. tableView.LayoutSubviews ();
  1507. tableView.Draw ();
  1508. expected =
  1509. @"
  1510. │A│B │
  1511. ├─┼─────►
  1512. │1│2 │
  1513. │1│2 │
  1514. ";
  1515. TestHelpers.AssertDriverContentsAre (expected, output);
  1516. // setting width to 10 leaves just enough space for the column to
  1517. // meet MinAcceptableWidth of 5. Column width includes terminator line
  1518. // symbol (e.g. ┤ or │)
  1519. tableView.Bounds = new Rect (0, 0, 10, 5);
  1520. tableView.LayoutSubviews ();
  1521. tableView.Draw ();
  1522. expected =
  1523. @"
  1524. │A│B│Very│
  1525. ├─┼─┼────┤
  1526. │1│2│aaaa│
  1527. │1│2│aaa │
  1528. ";
  1529. TestHelpers.AssertDriverContentsAre (expected, output);
  1530. tableView.Bounds = new Rect (0, 0, 25, 5);
  1531. // revert style change
  1532. style.MinAcceptableWidth = TableView.DefaultMinAcceptableWidth;
  1533. // Now let's test the global MaxCellWidth and MinCellWidth
  1534. tableView.Style.ExpandLastColumn = false;
  1535. tableView.MaxCellWidth = 10;
  1536. tableView.MinCellWidth = 3;
  1537. tableView.LayoutSubviews ();
  1538. tableView.Draw ();
  1539. expected =
  1540. @"
  1541. │A │B │Very Long │ │
  1542. ├───┼───┼──────────┼────┤
  1543. │1 │2 │aaaaaaaaaa│ │
  1544. │1 │2 │aaa │ │
  1545. ";
  1546. TestHelpers.AssertDriverContentsAre (expected, output);
  1547. // MaxCellWidth limits MinCellWidth
  1548. tableView.MaxCellWidth = 5;
  1549. tableView.MinCellWidth = 10;
  1550. tableView.LayoutSubviews ();
  1551. tableView.Draw ();
  1552. expected =
  1553. @"
  1554. │A │B │Very │ │
  1555. ├─────┼─────┼─────┼─────┤
  1556. │1 │2 │aaaaa│ │
  1557. │1 │2 │aaa │ │
  1558. ";
  1559. TestHelpers.AssertDriverContentsAre (expected, output);
  1560. Application.Shutdown ();
  1561. }
  1562. [Fact, AutoInitShutdown]
  1563. public void ScrollIndicators ()
  1564. {
  1565. var tableView = new TableView ();
  1566. tableView.BeginInit (); tableView.EndInit ();
  1567. tableView.ColorScheme = Colors.TopLevel;
  1568. // 3 columns are visibile
  1569. tableView.Bounds = new Rect (0, 0, 7, 5);
  1570. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1571. tableView.Style.ShowHorizontalHeaderOverline = false;
  1572. tableView.Style.AlwaysShowHeaders = true;
  1573. tableView.Style.SmoothHorizontalScrolling = true;
  1574. var dt = new DataTable ();
  1575. dt.Columns.Add ("A");
  1576. dt.Columns.Add ("B");
  1577. dt.Columns.Add ("C");
  1578. dt.Columns.Add ("D");
  1579. dt.Columns.Add ("E");
  1580. dt.Columns.Add ("F");
  1581. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1582. tableView.Table = new DataTableSource (dt);
  1583. // select last visible column
  1584. tableView.SelectedColumn = 2; // column C
  1585. tableView.Draw ();
  1586. // user can only scroll right so sees right indicator
  1587. // Because first column in table is A
  1588. string expected =
  1589. @"
  1590. │A│B│C│
  1591. ├─┼─┼─►
  1592. │1│2│3│";
  1593. TestHelpers.AssertDriverContentsAre (expected, output);
  1594. // Scroll right
  1595. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1596. // since A is now pushed off screen we get indicator showing
  1597. // that user can scroll left to see first column
  1598. tableView.Draw ();
  1599. expected =
  1600. @"
  1601. │B│C│D│
  1602. ◄─┼─┼─►
  1603. │2│3│4│";
  1604. TestHelpers.AssertDriverContentsAre (expected, output);
  1605. // Scroll right twice more (to end of columns)
  1606. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1607. tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
  1608. tableView.Draw ();
  1609. expected =
  1610. @"
  1611. │D│E│F│
  1612. ◄─┼─┼─┤
  1613. │4│5│6│";
  1614. TestHelpers.AssertDriverContentsAre (expected, output);
  1615. // Shutdown must be called to safely clean up Application if Init has been called
  1616. Application.Shutdown ();
  1617. }
  1618. [Fact, AutoInitShutdown]
  1619. public void CellEventsBackgroundFill ()
  1620. {
  1621. var tv = new TableView () {
  1622. Width = 20,
  1623. Height = 4
  1624. };
  1625. var dt = new DataTable ();
  1626. dt.Columns.Add ("C1");
  1627. dt.Columns.Add ("C2");
  1628. dt.Columns.Add ("C3");
  1629. dt.Rows.Add ("Hello", DBNull.Value, "f");
  1630. tv.Table = new DataTableSource (dt);
  1631. tv.NullSymbol = string.Empty;
  1632. Application.Top.Add (tv);
  1633. Application.Begin (Application.Top);
  1634. tv.Draw ();
  1635. var expected =
  1636. @"
  1637. ┌─────┬──┬─────────┐
  1638. │C1 │C2│C3 │
  1639. ├─────┼──┼─────────┤
  1640. │Hello│ │f │
  1641. ";
  1642. TestHelpers.AssertDriverContentsAre (expected, output);
  1643. var color = new Attribute (Color.Magenta, Color.BrightBlue);
  1644. var scheme = new ColorScheme {
  1645. Normal = color,
  1646. HotFocus = color,
  1647. Focus = color,
  1648. Disabled = color,
  1649. HotNormal = color,
  1650. };
  1651. // Now the thing we really want to test is the styles!
  1652. // All cells in the column have a column style that says
  1653. // the cell is pink!
  1654. for (int i = 0; i < dt.Columns.Count; i++) {
  1655. var style = tv.Style.GetOrCreateColumnStyle (i);
  1656. style.ColorGetter = (e) => {
  1657. return scheme;
  1658. };
  1659. }
  1660. tv.Draw ();
  1661. expected =
  1662. @"
  1663. 00000000000000000000
  1664. 00000000000000000000
  1665. 00000000000000000000
  1666. 01111101101111111110
  1667. ";
  1668. TestHelpers.AssertDriverColorsAre (expected, new Attribute [] { tv.ColorScheme.Normal, color });
  1669. }
  1670. [Fact, AutoInitShutdown]
  1671. public void ShowHorizontalBottomLine_WithVerticalCellLines ()
  1672. {
  1673. var tableView = GetABCDEFTableView (out _);
  1674. tableView.BeginInit (); tableView.EndInit ();
  1675. tableView.ColorScheme = Colors.TopLevel;
  1676. // 3 columns are visibile
  1677. tableView.Bounds = new Rect (0, 0, 7, 5);
  1678. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1679. tableView.Style.ShowHorizontalHeaderOverline = false;
  1680. tableView.Style.AlwaysShowHeaders = true;
  1681. tableView.Style.SmoothHorizontalScrolling = true;
  1682. tableView.Style.ShowHorizontalBottomline = true;
  1683. tableView.Draw ();
  1684. // user can only scroll right so sees right indicator
  1685. // Because first column in table is A
  1686. string expected =
  1687. @"
  1688. │A│B│C│
  1689. ├─┼─┼─►
  1690. │1│2│3│
  1691. └─┴─┴─┘";
  1692. TestHelpers.AssertDriverContentsAre (expected, output);
  1693. }
  1694. [Fact, AutoInitShutdown]
  1695. public void ShowHorizontalBottomLine_NoCellLines ()
  1696. {
  1697. var tableView = GetABCDEFTableView (out _);
  1698. tableView.BeginInit (); tableView.EndInit ();
  1699. tableView.ColorScheme = Colors.TopLevel;
  1700. // 3 columns are visibile
  1701. tableView.Bounds = new Rect (0, 0, 7, 5);
  1702. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1703. tableView.Style.ShowHorizontalHeaderOverline = false;
  1704. tableView.Style.AlwaysShowHeaders = true;
  1705. tableView.Style.SmoothHorizontalScrolling = true;
  1706. tableView.Style.ShowHorizontalBottomline = true;
  1707. tableView.Style.ShowVerticalCellLines = false;
  1708. tableView.Draw ();
  1709. // user can only scroll right so sees right indicator
  1710. // Because first column in table is A
  1711. string expected =
  1712. @"
  1713. │A│B│C│
  1714. └─┴─┴─►
  1715. 1 2 3
  1716. ───────";
  1717. TestHelpers.AssertDriverContentsAre (expected, output);
  1718. }
  1719. [Fact, AutoInitShutdown]
  1720. public void TestFullRowSelect_SelectionColorStopsAtTableEdge_WithCellLines ()
  1721. {
  1722. var tv = GetTwoRowSixColumnTable (out var dt);
  1723. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1724. tv.Bounds = new Rect (0, 0, 7, 6);
  1725. tv.Frame = new Rect (0, 0, 7, 6);
  1726. tv.LayoutSubviews ();
  1727. tv.FullRowSelect = true;
  1728. tv.Style.ShowHorizontalBottomline = true;
  1729. // Clicking in bottom row
  1730. tv.MouseEvent (new MouseEvent {
  1731. X = 1,
  1732. Y = 4,
  1733. Flags = MouseFlags.Button1Clicked
  1734. });
  1735. // should select that row
  1736. Assert.Equal (2, tv.SelectedRow);
  1737. tv.OnDrawContent (tv.Bounds);
  1738. string expected =
  1739. @"
  1740. │A│B│C│
  1741. ├─┼─┼─►
  1742. │1│2│3│
  1743. │1│2│3│
  1744. │1│2│3│
  1745. └─┴─┴─┘";
  1746. TestHelpers.AssertDriverContentsAre (expected, output);
  1747. var normal = tv.ColorScheme.Normal;
  1748. var focus = tv.ColorScheme.HotFocus = new Attribute (Color.Magenta, Color.White);
  1749. tv.Draw ();
  1750. // HotFocus color (1) should be used for rendering the selected line
  1751. // But should not spill into the borders. Normal color (0) should be
  1752. // used for the rest.
  1753. expected =
  1754. @"
  1755. 0000000
  1756. 0000000
  1757. 0000000
  1758. 0000000
  1759. 0111110
  1760. 0000000";
  1761. TestHelpers.AssertDriverColorsAre (expected, normal, focus);
  1762. }
  1763. [Fact, AutoInitShutdown]
  1764. public void TestFullRowSelect_AlwaysUseNormalColorForVerticalCellLines ()
  1765. {
  1766. var tv = GetTwoRowSixColumnTable (out var dt);
  1767. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1768. tv.Bounds = new Rect (0, 0, 7, 6);
  1769. tv.Frame = new Rect (0, 0, 7, 6);
  1770. tv.LayoutSubviews ();
  1771. tv.FullRowSelect = true;
  1772. tv.Style.ShowHorizontalBottomline = true;
  1773. tv.Style.AlwaysUseNormalColorForVerticalCellLines = true;
  1774. // Clicking in bottom row
  1775. tv.MouseEvent (new MouseEvent {
  1776. X = 1,
  1777. Y = 4,
  1778. Flags = MouseFlags.Button1Clicked
  1779. });
  1780. // should select that row
  1781. Assert.Equal (2, tv.SelectedRow);
  1782. tv.OnDrawContent (tv.Bounds);
  1783. string expected =
  1784. @"
  1785. │A│B│C│
  1786. ├─┼─┼─►
  1787. │1│2│3│
  1788. │1│2│3│
  1789. │1│2│3│
  1790. └─┴─┴─┘";
  1791. TestHelpers.AssertDriverContentsAre (expected, output);
  1792. var normal = tv.ColorScheme.Normal;
  1793. var focus = tv.ColorScheme.HotFocus = new Attribute (Color.Magenta, Color.White);
  1794. tv.Draw ();
  1795. // HotFocus color (1) should be used for cells only because
  1796. // AlwaysUseNormalColorForVerticalCellLines is true
  1797. expected =
  1798. @"
  1799. 0000000
  1800. 0000000
  1801. 0000000
  1802. 0000000
  1803. 0101010
  1804. 0000000";
  1805. TestHelpers.AssertDriverColorsAre (expected, normal, focus);
  1806. }
  1807. [Fact, AutoInitShutdown]
  1808. public void TestTableViewCheckboxes_Simple()
  1809. {
  1810. var tv = GetTwoRowSixColumnTable (out var dt);
  1811. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1812. tv.LayoutSubviews ();
  1813. var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
  1814. tv.Table = wrapper;
  1815. tv.Draw ();
  1816. string expected =
  1817. @"
  1818. │ │A│B│
  1819. ├─┼─┼─►
  1820. │☐│1│2│
  1821. │☐│1│2│
  1822. │☐│1│2│";
  1823. TestHelpers.AssertDriverContentsAre (expected, output);
  1824. Assert.Empty (wrapper.CheckedRows);
  1825. //toggle the top cell
  1826. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  1827. Assert.Single (wrapper.CheckedRows, 0);
  1828. tv.Draw();
  1829. expected =
  1830. @"
  1831. │ │A│B│
  1832. ├─┼─┼─►
  1833. │☑│1│2│
  1834. │☐│1│2│
  1835. │☐│1│2│";
  1836. TestHelpers.AssertDriverContentsAre (expected, output);
  1837. tv.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ()));
  1838. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  1839. Assert.Contains (0,wrapper.CheckedRows);
  1840. Assert.Contains (1,wrapper.CheckedRows);
  1841. Assert.Equal (2, wrapper.CheckedRows.Count);
  1842. tv.Draw();
  1843. expected =
  1844. @"
  1845. │ │A│B│
  1846. ├─┼─┼─►
  1847. │☑│1│2│
  1848. │☑│1│2│
  1849. │☐│1│2│";
  1850. TestHelpers.AssertDriverContentsAre (expected, output);
  1851. // untoggle top one
  1852. tv.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ()));
  1853. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  1854. Assert.Single (wrapper.CheckedRows, 1);
  1855. tv.Draw();
  1856. expected =
  1857. @"
  1858. │ │A│B│
  1859. ├─┼─┼─►
  1860. │☐│1│2│
  1861. │☑│1│2│
  1862. │☐│1│2│";
  1863. TestHelpers.AssertDriverContentsAre (expected, output);
  1864. }
  1865. [Fact, AutoInitShutdown]
  1866. public void TestTableViewCheckboxes_SelectAllToggle ()
  1867. {
  1868. var tv = GetTwoRowSixColumnTable (out var dt);
  1869. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1870. tv.LayoutSubviews ();
  1871. var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
  1872. tv.Table = wrapper;
  1873. //toggle all cells
  1874. tv.ProcessKey (new KeyEvent (Key.A | Key.CtrlMask, new KeyModifiers { Ctrl = true }));
  1875. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  1876. tv.Draw();
  1877. string expected =
  1878. @"
  1879. │ │A│B│
  1880. ├─┼─┼─►
  1881. │☑│1│2│
  1882. │☑│1│2│
  1883. │☑│1│2│";
  1884. TestHelpers.AssertDriverContentsAre (expected, output);
  1885. Assert.Contains (0, wrapper.CheckedRows);
  1886. Assert.Contains (1, wrapper.CheckedRows);
  1887. Assert.Contains (2, wrapper.CheckedRows);
  1888. Assert.Equal (3, wrapper.CheckedRows.Count);
  1889. // Untoggle all again
  1890. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  1891. tv.Draw();
  1892. expected =
  1893. @"
  1894. │ │A│B│
  1895. ├─┼─┼─►
  1896. │☐│1│2│
  1897. │☐│1│2│
  1898. │☐│1│2│";
  1899. TestHelpers.AssertDriverContentsAre (expected, output);
  1900. Assert.Empty (wrapper.CheckedRows);
  1901. }
  1902. [Fact, AutoInitShutdown]
  1903. public void TestTableViewCheckboxes_MultiSelectIsUnion_WhenToggling ()
  1904. {
  1905. var tv = GetTwoRowSixColumnTable (out var dt);
  1906. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1907. tv.LayoutSubviews ();
  1908. var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
  1909. tv.Table = wrapper;
  1910. wrapper.CheckedRows.Add (0);
  1911. wrapper.CheckedRows.Add (2);
  1912. tv.Draw();
  1913. string expected =
  1914. @"
  1915. │ │A│B│
  1916. ├─┼─┼─►
  1917. │☑│1│2│
  1918. │☐│1│2│
  1919. │☑│1│2│";
  1920. //toggle top two at once
  1921. tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
  1922. Assert.True (tv.IsSelected (0, 0));
  1923. Assert.True (tv.IsSelected (0, 1));
  1924. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  1925. // Because at least 1 of the rows is not yet ticked we toggle them all to ticked
  1926. TestHelpers.AssertDriverContentsAre (expected, output);
  1927. Assert.Contains (0, wrapper.CheckedRows);
  1928. Assert.Contains (1, wrapper.CheckedRows);
  1929. Assert.Contains (2, wrapper.CheckedRows);
  1930. Assert.Equal (3, wrapper.CheckedRows.Count);
  1931. tv.Draw();
  1932. expected =
  1933. @"
  1934. │ │A│B│
  1935. ├─┼─┼─►
  1936. │☑│1│2│
  1937. │☑│1│2│
  1938. │☑│1│2│";
  1939. TestHelpers.AssertDriverContentsAre (expected, output);
  1940. // Untoggle the top 2
  1941. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  1942. tv.Draw();
  1943. expected =
  1944. @"
  1945. │ │A│B│
  1946. ├─┼─┼─►
  1947. │☐│1│2│
  1948. │☐│1│2│
  1949. │☑│1│2│";
  1950. TestHelpers.AssertDriverContentsAre (expected, output);
  1951. Assert.Single (wrapper.CheckedRows, 2);
  1952. }
  1953. [Fact, AutoInitShutdown]
  1954. public void TestTableViewCheckboxes_ByObject ()
  1955. {
  1956. var tv = GetPetTable (out var source);
  1957. tv.LayoutSubviews ();
  1958. var pets = source.Data;
  1959. var wrapper = new CheckBoxTableSourceWrapperByObject<PickablePet>(
  1960. tv,
  1961. source,
  1962. (p)=>p.IsPicked,
  1963. (p,b)=>p.IsPicked = b);
  1964. tv.Table = wrapper;
  1965. tv.Draw();
  1966. string expected =
  1967. @"
  1968. ┌─┬───────┬─────────────┐
  1969. │ │Name │Kind │
  1970. ├─┼───────┼─────────────┤
  1971. │☐│Tammy │Cat │
  1972. │☐│Tibbles│Cat │
  1973. │☐│Ripper │Dog │";
  1974. TestHelpers.AssertDriverContentsAre (expected, output);
  1975. Assert.Empty (pets.Where(p=>p.IsPicked));
  1976. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  1977. Assert.True (pets.First ().IsPicked);
  1978. tv.Draw();
  1979. expected =
  1980. @"
  1981. ┌─┬───────┬─────────────┐
  1982. │ │Name │Kind │
  1983. ├─┼───────┼─────────────┤
  1984. │☑│Tammy │Cat │
  1985. │☐│Tibbles│Cat │
  1986. │☐│Ripper │Dog │";
  1987. TestHelpers.AssertDriverContentsAre (expected, output);
  1988. tv.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ()));
  1989. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  1990. Assert.True (pets.ElementAt(0).IsPicked);
  1991. Assert.True (pets.ElementAt (1).IsPicked);
  1992. Assert.False (pets.ElementAt (2).IsPicked);
  1993. tv.Draw();
  1994. expected =
  1995. @"
  1996. ┌─┬───────┬─────────────┐
  1997. │ │Name │Kind │
  1998. ├─┼───────┼─────────────┤
  1999. │☑│Tammy │Cat │
  2000. │☑│Tibbles│Cat │
  2001. │☐│Ripper │Dog │";
  2002. TestHelpers.AssertDriverContentsAre (expected, output);
  2003. tv.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ()));
  2004. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  2005. Assert.False (pets.ElementAt (0).IsPicked);
  2006. Assert.True (pets.ElementAt (1).IsPicked);
  2007. Assert.False (pets.ElementAt (2).IsPicked);
  2008. tv.Draw();
  2009. expected =
  2010. @"
  2011. ┌─┬───────┬─────────────┐
  2012. │ │Name │Kind │
  2013. ├─┼───────┼─────────────┤
  2014. │☐│Tammy │Cat │
  2015. │☑│Tibbles│Cat │
  2016. │☐│Ripper │Dog │";
  2017. TestHelpers.AssertDriverContentsAre (expected, output);
  2018. }
  2019. [Fact, AutoInitShutdown]
  2020. public void TestTableViewCheckboxes_SelectAllToggle_ByObject ()
  2021. {
  2022. var tv = GetPetTable (out var source);
  2023. tv.LayoutSubviews ();
  2024. var pets = source.Data;
  2025. var wrapper = new CheckBoxTableSourceWrapperByObject<PickablePet> (
  2026. tv,
  2027. source,
  2028. (p) => p.IsPicked,
  2029. (p, b) => p.IsPicked = b);
  2030. tv.Table = wrapper;
  2031. Assert.DoesNotContain (pets, p => p.IsPicked);
  2032. //toggle all cells
  2033. tv.ProcessKey (new KeyEvent (Key.A | Key.CtrlMask, new KeyModifiers { Ctrl = true }));
  2034. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  2035. Assert.True (pets.All (p => p.IsPicked));
  2036. tv.Draw();
  2037. string expected =
  2038. @"
  2039. ┌─┬───────┬─────────────┐
  2040. │ │Name │Kind │
  2041. ├─┼───────┼─────────────┤
  2042. │☑│Tammy │Cat │
  2043. │☑│Tibbles│Cat │
  2044. │☑│Ripper │Dog │";
  2045. TestHelpers.AssertDriverContentsAre (expected, output);
  2046. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  2047. Assert.Empty (pets.Where (p => p.IsPicked));
  2048. tv.Draw();
  2049. expected =
  2050. @"
  2051. ┌─┬───────┬─────────────┐
  2052. │ │Name │Kind │
  2053. ├─┼───────┼─────────────┤
  2054. │☐│Tammy │Cat │
  2055. │☐│Tibbles│Cat │
  2056. │☐│Ripper │Dog │
  2057. ";
  2058. TestHelpers.AssertDriverContentsAre (expected, output);
  2059. }
  2060. [Fact, AutoInitShutdown]
  2061. public void TestTableViewRadioBoxes_Simple_ByObject ()
  2062. {
  2063. var tv = GetPetTable (out var source);
  2064. tv.LayoutSubviews ();
  2065. var pets = source.Data;
  2066. var wrapper = new CheckBoxTableSourceWrapperByObject<PickablePet> (
  2067. tv,
  2068. source,
  2069. (p) => p.IsPicked,
  2070. (p, b) => p.IsPicked = b);
  2071. wrapper.UseRadioButtons = true;
  2072. tv.Table = wrapper;
  2073. tv.Draw();
  2074. string expected =
  2075. @"
  2076. ┌─┬───────┬─────────────┐
  2077. │ │Name │Kind │
  2078. ├─┼───────┼─────────────┤
  2079. │○│Tammy │Cat │
  2080. │○│Tibbles│Cat │
  2081. │○│Ripper │Dog │
  2082. ";
  2083. TestHelpers.AssertDriverContentsAre (expected, output);
  2084. Assert.Empty (pets.Where (p => p.IsPicked));
  2085. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  2086. Assert.True (pets.First ().IsPicked);
  2087. tv.Draw();
  2088. expected =
  2089. @"
  2090. ┌─┬───────┬─────────────┐
  2091. │ │Name │Kind │
  2092. ├─┼───────┼─────────────┤
  2093. │◉│Tammy │Cat │
  2094. │○│Tibbles│Cat │
  2095. │○│Ripper │Dog │";
  2096. TestHelpers.AssertDriverContentsAre (expected, output);
  2097. tv.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ()));
  2098. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  2099. Assert.False (pets.ElementAt (0).IsPicked);
  2100. Assert.True (pets.ElementAt (1).IsPicked);
  2101. Assert.False (pets.ElementAt (2).IsPicked);
  2102. tv.Draw();
  2103. expected =
  2104. @"
  2105. ┌─┬───────┬─────────────┐
  2106. │ │Name │Kind │
  2107. ├─┼───────┼─────────────┤
  2108. │○│Tammy │Cat │
  2109. │◉│Tibbles│Cat │
  2110. │○│Ripper │Dog │";
  2111. TestHelpers.AssertDriverContentsAre (expected, output);
  2112. tv.ProcessKey (new KeyEvent (Key.CursorUp, new KeyModifiers ()));
  2113. tv.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ()));
  2114. Assert.True (pets.ElementAt (0).IsPicked);
  2115. Assert.False (pets.ElementAt (1).IsPicked);
  2116. Assert.False (pets.ElementAt (2).IsPicked);
  2117. tv.Draw();
  2118. expected =
  2119. @"
  2120. ┌─┬───────┬─────────────┐
  2121. │ │Name │Kind │
  2122. ├─┼───────┼─────────────┤
  2123. │◉│Tammy │Cat │
  2124. │○│Tibbles│Cat │
  2125. │○│Ripper │Dog │";
  2126. TestHelpers.AssertDriverContentsAre (expected, output);
  2127. }
  2128. [Fact, AutoInitShutdown]
  2129. public void TestFullRowSelect_SelectionColorDoesNotStop_WhenShowVerticalCellLinesIsFalse ()
  2130. {
  2131. var tv = GetTwoRowSixColumnTable (out var dt);
  2132. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2133. tv.LayoutSubviews ();
  2134. tv.Bounds = new Rect (0, 0, 7, 6);
  2135. tv.FullRowSelect = true;
  2136. tv.Style.ShowVerticalCellLines = false;
  2137. tv.Style.ShowVerticalHeaderLines = false;
  2138. // Clicking in bottom row
  2139. tv.MouseEvent (new MouseEvent {
  2140. X = 1,
  2141. Y = 4,
  2142. Flags = MouseFlags.Button1Clicked
  2143. });
  2144. // should select that row
  2145. Assert.Equal (2, tv.SelectedRow);
  2146. tv.Draw ();
  2147. string expected =
  2148. @"
  2149. A B C
  2150. ───────
  2151. 1 2 3
  2152. 1 2 3
  2153. 1 2 3";
  2154. TestHelpers.AssertDriverContentsAre (expected, output);
  2155. var normal = tv.ColorScheme.Normal;
  2156. var focus = tv.ColorScheme.HotFocus = new Attribute (Color.Magenta, Color.White);
  2157. tv.Draw ();
  2158. // HotFocus color (1) should be used for rendering the selected line
  2159. // Note that because there are no vertical cell lines we use the hot focus
  2160. // color for the whole row
  2161. expected =
  2162. @"
  2163. 000000
  2164. 000000
  2165. 000000
  2166. 000000
  2167. 111111";
  2168. TestHelpers.AssertDriverColorsAre (expected, normal, focus);
  2169. }
  2170. public static DataTableSource BuildTable (int cols, int rows)
  2171. {
  2172. return BuildTable (cols, rows, out _);
  2173. }
  2174. /// <summary>
  2175. /// Builds a simple table of string columns with the requested number of columns and rows
  2176. /// </summary>
  2177. /// <param name="cols"></param>
  2178. /// <param name="rows"></param>
  2179. /// <returns></returns>
  2180. public static DataTableSource BuildTable (int cols, int rows, out DataTable dt)
  2181. {
  2182. dt = new DataTable ();
  2183. for (int c = 0; c < cols; c++) {
  2184. dt.Columns.Add ("Col" + c);
  2185. }
  2186. for (int r = 0; r < rows; r++) {
  2187. var newRow = dt.NewRow ();
  2188. for (int c = 0; c < cols; c++) {
  2189. newRow [c] = $"R{r}C{c}";
  2190. }
  2191. dt.Rows.Add (newRow);
  2192. }
  2193. return new DataTableSource (dt);
  2194. }
  2195. [Fact, AutoInitShutdown]
  2196. public void Test_ScreenToCell ()
  2197. {
  2198. var tableView = GetTwoRowSixColumnTable ();
  2199. tableView.BeginInit (); tableView.EndInit ();
  2200. tableView.LayoutSubviews ();
  2201. tableView.Draw ();
  2202. // user can only scroll right so sees right indicator
  2203. // Because first column in table is A
  2204. string expected =
  2205. @"
  2206. │A│B│C│
  2207. ├─┼─┼─►
  2208. │1│2│3│
  2209. │1│2│3│";
  2210. TestHelpers.AssertDriverContentsAre (expected, output);
  2211. // ---------------- X=0 -----------------------
  2212. // click is before first cell
  2213. Assert.Null (tableView.ScreenToCell (0, 0));
  2214. Assert.Null (tableView.ScreenToCell (0, 1));
  2215. Assert.Null (tableView.ScreenToCell (0, 2));
  2216. Assert.Null (tableView.ScreenToCell (0, 3));
  2217. Assert.Null (tableView.ScreenToCell (0, 4));
  2218. // ---------------- X=1 -----------------------
  2219. // click in header
  2220. Assert.Null (tableView.ScreenToCell (1, 0));
  2221. // click in header row line
  2222. Assert.Null (tableView.ScreenToCell (1, 1));
  2223. // click in cell 0,0
  2224. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (1, 2));
  2225. // click in cell 0,1
  2226. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3));
  2227. // after last row
  2228. Assert.Null (tableView.ScreenToCell (1, 4));
  2229. // ---------------- X=2 -----------------------
  2230. // ( even though there is a horizontal dividing line here we treat it as a hit on the cell before)
  2231. // click in header
  2232. Assert.Null (tableView.ScreenToCell (2, 0));
  2233. // click in header row line
  2234. Assert.Null (tableView.ScreenToCell (2, 1));
  2235. // click in cell 0,0
  2236. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (2, 2));
  2237. // click in cell 0,1
  2238. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3));
  2239. // after last row
  2240. Assert.Null (tableView.ScreenToCell (2, 4));
  2241. // ---------------- X=3 -----------------------
  2242. // click in header
  2243. Assert.Null (tableView.ScreenToCell (3, 0));
  2244. // click in header row line
  2245. Assert.Null (tableView.ScreenToCell (3, 1));
  2246. // click in cell 1,0
  2247. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2));
  2248. // click in cell 1,1
  2249. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3));
  2250. // after last row
  2251. Assert.Null (tableView.ScreenToCell (3, 4));
  2252. }
  2253. [Fact, AutoInitShutdown]
  2254. public void Test_ScreenToCell_DataColumnOverload ()
  2255. {
  2256. var tableView = GetTwoRowSixColumnTable ();
  2257. tableView.LayoutSubviews ();
  2258. tableView.Draw ();
  2259. // user can only scroll right so sees right indicator
  2260. // Because first column in table is A
  2261. string expected =
  2262. @"
  2263. │A│B│C│
  2264. ├─┼─┼─►
  2265. │1│2│3│
  2266. │1│2│3│";
  2267. TestHelpers.AssertDriverContentsAre (expected, output);
  2268. int? col;
  2269. // ---------------- X=0 -----------------------
  2270. // click is before first cell
  2271. Assert.Null (tableView.ScreenToCell (0, 0, out col));
  2272. Assert.Null (col);
  2273. Assert.Null (tableView.ScreenToCell (0, 1, out col));
  2274. Assert.Null (col);
  2275. Assert.Null (tableView.ScreenToCell (0, 2, out col));
  2276. Assert.Null (col);
  2277. Assert.Null (tableView.ScreenToCell (0, 3, out col));
  2278. Assert.Null (col);
  2279. Assert.Null (tableView.ScreenToCell (0, 4, out col));
  2280. Assert.Null (col);
  2281. // ---------------- X=1 -----------------------
  2282. // click in header
  2283. Assert.Null (tableView.ScreenToCell (1, 0, out col));
  2284. Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
  2285. // click in header row line (click in the horizontal line below header counts as click in header above - consistent with the column hit box)
  2286. Assert.Null (tableView.ScreenToCell (1, 1, out col));
  2287. Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
  2288. // click in cell 0,0
  2289. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (1, 2, out col));
  2290. Assert.Null (col);
  2291. // click in cell 0,1
  2292. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3, out col));
  2293. Assert.Null (col);
  2294. // after last row
  2295. Assert.Null (tableView.ScreenToCell (1, 4, out col));
  2296. Assert.Null (col);
  2297. // ---------------- X=2 -----------------------
  2298. // click in header
  2299. Assert.Null (tableView.ScreenToCell (2, 0, out col));
  2300. Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
  2301. // click in header row line
  2302. Assert.Null (tableView.ScreenToCell (2, 1, out col));
  2303. Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
  2304. // click in cell 0,0
  2305. Assert.Equal (new Point (0, 0), tableView.ScreenToCell (2, 2, out col));
  2306. Assert.Null (col);
  2307. // click in cell 0,1
  2308. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3, out col));
  2309. Assert.Null (col);
  2310. // after last row
  2311. Assert.Null (tableView.ScreenToCell (2, 4, out col));
  2312. Assert.Null (col);
  2313. // ---------------- X=3 -----------------------
  2314. // click in header
  2315. Assert.Null (tableView.ScreenToCell (3, 0, out col));
  2316. Assert.Equal ("B", tableView.Table.ColumnNames [col.Value]);
  2317. // click in header row line
  2318. Assert.Null (tableView.ScreenToCell (3, 1, out col));
  2319. Assert.Equal ("B", tableView.Table.ColumnNames [col.Value]);
  2320. // click in cell 1,0
  2321. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2, out col));
  2322. Assert.Null (col);
  2323. // click in cell 1,1
  2324. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3, out col));
  2325. Assert.Null (col);
  2326. // after last row
  2327. Assert.Null (tableView.ScreenToCell (3, 4, out col));
  2328. Assert.Null (col);
  2329. }
  2330. /// <summary>
  2331. /// Builds a simple list with the requested number of string items
  2332. /// </summary>
  2333. /// <param name="items"></param>
  2334. /// <returns></returns>
  2335. public static IList BuildList (int items)
  2336. {
  2337. var list = new List<string> ();
  2338. for (int i = 0; i < items; i++) {
  2339. list.Add ("Item " + i);
  2340. }
  2341. return list.ToArray ();
  2342. }
  2343. [Theory, AutoInitShutdown]
  2344. [InlineData (new object [] { Orientation.Horizontal, false })]
  2345. [InlineData (new object [] { Orientation.Vertical, false })]
  2346. [InlineData (new object [] { Orientation.Horizontal, true })]
  2347. [InlineData (new object [] { Orientation.Vertical, true })]
  2348. public void TestListTableSource (Orientation orient, bool parallel)
  2349. {
  2350. var list = BuildList (16);
  2351. var tv = new TableView ();
  2352. //tv.BeginInit (); tv.EndInit ();
  2353. tv.ColorScheme = Colors.TopLevel;
  2354. tv.Bounds = new Rect (0, 0, 25, 4);
  2355. tv.Style = new () {
  2356. ShowHeaders = false,
  2357. ShowHorizontalHeaderOverline = false,
  2358. ShowHorizontalHeaderUnderline = false
  2359. };
  2360. var listStyle = new ListColumnStyle () {
  2361. Orientation = orient,
  2362. ScrollParallel = parallel
  2363. };
  2364. tv.Table = new ListTableSource (list, tv, listStyle);
  2365. tv.LayoutSubviews ();
  2366. tv.Draw ();
  2367. string horizPerpExpected =
  2368. @"
  2369. │Item 0│Item 1 │
  2370. │Item 2│Item 3 │
  2371. │Item 4│Item 5 │
  2372. │Item 6│Item 7 │";
  2373. string horizParaExpected =
  2374. @"
  2375. │Item 0 │Item 1 │Item 2 │
  2376. │Item 4 │Item 5 │Item 6 │
  2377. │Item 8 │Item 9 │Item 10│
  2378. │Item 12│Item 13│Item 14│";
  2379. string vertPerpExpected =
  2380. @"
  2381. │Item 0│Item 4│Item 8 │
  2382. │Item 1│Item 5│Item 9 │
  2383. │Item 2│Item 6│Item 10 │
  2384. │Item 3│Item 7│Item 11 │";
  2385. string vertParaExpected =
  2386. @"
  2387. │Item 0│Item 8 │
  2388. │Item 1│Item 9 │
  2389. │Item 2│Item 10 │
  2390. │Item 3│Item 11 │";
  2391. string expected;
  2392. if (orient == Orientation.Vertical)
  2393. if (parallel) {
  2394. expected = vertParaExpected;
  2395. } else {
  2396. expected = vertPerpExpected;
  2397. }
  2398. else {
  2399. if (parallel) {
  2400. expected = horizParaExpected;
  2401. } else {
  2402. expected = horizPerpExpected;
  2403. }
  2404. }
  2405. TestHelpers.AssertDriverContentsAre (expected, output);
  2406. }
  2407. [Fact, AutoInitShutdown]
  2408. public void TestEnumerableDataSource_BasicTypes ()
  2409. {
  2410. var tv = new TableView ();
  2411. tv.ColorScheme = Colors.TopLevel;
  2412. tv.Bounds = new Rect (0, 0, 50, 6);
  2413. tv.Table = new EnumerableTableSource<Type> (
  2414. new Type [] { typeof (string), typeof (int), typeof (float) },
  2415. new () {
  2416. { "Name", (t)=>t.Name},
  2417. { "Namespace", (t)=>t.Namespace},
  2418. { "BaseType", (t)=>t.BaseType}
  2419. });
  2420. tv.LayoutSubviews ();
  2421. tv.Draw ();
  2422. string expected =
  2423. @"
  2424. ┌──────┬─────────┬───────────────────────────────┐
  2425. │Name │Namespace│BaseType │
  2426. ├──────┼─────────┼───────────────────────────────┤
  2427. │String│System │System.Object │
  2428. │Int32 │System │System.ValueType │
  2429. │Single│System │System.ValueType │";
  2430. TestHelpers.AssertDriverContentsAre (expected, output);
  2431. }
  2432. [Fact, AutoInitShutdown]
  2433. public void Test_CollectionNavigator ()
  2434. {
  2435. var tv = new TableView ();
  2436. tv.ColorScheme = Colors.TopLevel;
  2437. tv.Bounds = new Rect (0, 0, 50, 7);
  2438. tv.Table = new EnumerableTableSource<string> (
  2439. new string [] { "fish", "troll", "trap", "zoo" },
  2440. new () {
  2441. { "Name", (t)=>t},
  2442. { "EndsWith", (t)=>t.Last()}
  2443. });
  2444. tv.LayoutSubviews ();
  2445. tv.Draw ();
  2446. string expected =
  2447. @"
  2448. ┌─────┬──────────────────────────────────────────┐
  2449. │Name │EndsWith │
  2450. ├─────┼──────────────────────────────────────────┤
  2451. │fish │h │
  2452. │troll│l │
  2453. │trap │p │
  2454. │zoo │o │";
  2455. TestHelpers.AssertDriverContentsAre (expected, output);
  2456. Assert.Equal (0, tv.SelectedRow);
  2457. // this test assumes no focus
  2458. Assert.False (tv.HasFocus);
  2459. // already on fish
  2460. tv.ProcessKey (new KeyEvent { Key = Key.f });
  2461. Assert.Equal (0, tv.SelectedRow);
  2462. // not focused
  2463. tv.ProcessKey (new KeyEvent { Key = Key.z });
  2464. Assert.Equal (0, tv.SelectedRow);
  2465. // ensure that TableView has the input focus
  2466. Application.Top.Add (tv);
  2467. Application.Begin (Application.Top);
  2468. Application.Top.FocusFirst ();
  2469. Assert.True (tv.HasFocus);
  2470. // already on fish
  2471. tv.ProcessKey (new KeyEvent { Key = Key.f });
  2472. Assert.Equal (0, tv.SelectedRow);
  2473. // move to zoo
  2474. tv.ProcessKey (new KeyEvent { Key = Key.z });
  2475. Assert.Equal (3, tv.SelectedRow);
  2476. // move to troll
  2477. tv.ProcessKey (new KeyEvent { Key = Key.t });
  2478. Assert.Equal (1, tv.SelectedRow);
  2479. // move to trap
  2480. tv.ProcessKey (new KeyEvent { Key = Key.t });
  2481. Assert.Equal (2, tv.SelectedRow);
  2482. // change columns to navigate by column 2
  2483. Assert.Equal (0, tv.SelectedColumn);
  2484. Assert.Equal (2, tv.SelectedRow);
  2485. tv.ProcessKey (new KeyEvent { Key = Key.CursorRight });
  2486. Assert.Equal (1, tv.SelectedColumn);
  2487. Assert.Equal (2, tv.SelectedRow);
  2488. // nothing ends with t so stay where you are
  2489. tv.ProcessKey (new KeyEvent { Key = Key.t });
  2490. Assert.Equal (2, tv.SelectedRow);
  2491. //jump to fish which ends in h
  2492. tv.ProcessKey (new KeyEvent { Key = Key.h });
  2493. Assert.Equal (0, tv.SelectedRow);
  2494. // jump to zoo which ends in o
  2495. tv.ProcessKey (new KeyEvent { Key = Key.o });
  2496. Assert.Equal (3, tv.SelectedRow);
  2497. }
  2498. private TableView GetTwoRowSixColumnTable ()
  2499. {
  2500. return GetTwoRowSixColumnTable (out _);
  2501. }
  2502. private TableView GetTwoRowSixColumnTable (out DataTable dt)
  2503. {
  2504. var tableView = new TableView ();
  2505. tableView.ColorScheme = Colors.TopLevel;
  2506. // 3 columns are visible
  2507. tableView.Bounds = new Rect (0, 0, 7, 5);
  2508. tableView.Style.ShowHorizontalHeaderUnderline = true;
  2509. tableView.Style.ShowHorizontalHeaderOverline = false;
  2510. tableView.Style.AlwaysShowHeaders = true;
  2511. tableView.Style.SmoothHorizontalScrolling = true;
  2512. dt = new DataTable ();
  2513. dt.Columns.Add ("A");
  2514. dt.Columns.Add ("B");
  2515. dt.Columns.Add ("C");
  2516. dt.Columns.Add ("D");
  2517. dt.Columns.Add ("E");
  2518. dt.Columns.Add ("F");
  2519. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2520. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2521. tableView.Table = new DataTableSource (dt);
  2522. return tableView;
  2523. }
  2524. private class PickablePet {
  2525. public bool IsPicked { get; set; }
  2526. public string Name{ get; set; }
  2527. public string Kind { get; set; }
  2528. public PickablePet (bool isPicked, string name, string kind)
  2529. {
  2530. IsPicked = isPicked;
  2531. Name = name;
  2532. Kind = kind;
  2533. }
  2534. }
  2535. private TableView GetPetTable (out EnumerableTableSource<PickablePet> source)
  2536. {
  2537. var tv = new TableView ();
  2538. tv.ColorScheme = Colors.TopLevel;
  2539. tv.Bounds = new Rect (0, 0, 25, 6);
  2540. var pets = new List<PickablePet> {
  2541. new PickablePet(false,"Tammy","Cat"),
  2542. new PickablePet(false,"Tibbles","Cat"),
  2543. new PickablePet(false,"Ripper","Dog")};
  2544. tv.Table = source = new EnumerableTableSource<PickablePet> (
  2545. pets,
  2546. new () {
  2547. { "Name", (p) => p.Name},
  2548. { "Kind", (p) => p.Kind},
  2549. });
  2550. tv.LayoutSubviews ();
  2551. return tv;
  2552. }
  2553. }
  2554. }