TableViewTests.cs 108 KB

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