TableViewTests.cs 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506
  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, output, 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. output,
  858. Application.Driver,
  859. tv.ColorScheme.Normal,
  860. focused ? tv.ColorScheme.Focus : tv.ColorScheme.HotNormal,
  861. cellHighlight.Normal
  862. );
  863. // change the value in the table so that
  864. // it no longer matches the ColorGetter
  865. // delegate conditional ( which checks for
  866. // the value 2)
  867. dt.Rows [0] [1] = 5;
  868. tv.SetNeedsDisplay ();
  869. tv.Draw ();
  870. expected = @"
  871. ┌─┬─┐
  872. │A│B│
  873. ├─┼─┤
  874. │1│5│
  875. ";
  876. TestHelpers.AssertDriverContentsAre (expected, output);
  877. expectedColors = @"
  878. 00000
  879. 00000
  880. 00000
  881. 01000
  882. ";
  883. // now we only see 2 colors used (the selected cell color and Normal
  884. // cellHighlight should no longer be used because the delegate returned null
  885. // (now that the cell value is 5 - which does not match the conditional)
  886. TestHelpers.AssertDriverAttributesAre (
  887. expectedColors,
  888. output,
  889. Application.Driver,
  890. tv.ColorScheme.Normal,
  891. focused ? tv.ColorScheme.Focus : tv.ColorScheme.HotNormal
  892. );
  893. top.Dispose ();
  894. }
  895. [Theory]
  896. [AutoInitShutdown (configLocation: ConfigurationManager.ConfigLocations.DefaultOnly)]
  897. [InlineData (false)]
  898. [InlineData (true)]
  899. public void TableView_ColorsTest_RowColorGetter (bool focused)
  900. {
  901. TableView tv = SetUpMiniTable (out DataTable dt);
  902. tv.LayoutSubviews ();
  903. // width exactly matches the max col widths
  904. tv.Viewport = new (0, 0, 5, 4);
  905. var rowHighlight = new ColorScheme
  906. {
  907. Normal = new (Color.BrightCyan, Color.DarkGray),
  908. HotNormal = new (Color.Green, Color.Blue),
  909. Focus = new (Color.BrightYellow, Color.White),
  910. // Not used by TableView
  911. HotFocus = new (Color.Cyan, Color.Magenta)
  912. };
  913. // when B is 2 use the custom highlight color for the row
  914. tv.Style.RowColorGetter += e => Convert.ToInt32 (e.Table [e.RowIndex, 1]) == 2 ? rowHighlight : null;
  915. var top = new Toplevel ();
  916. top.Add (tv);
  917. Application.Begin (top);
  918. tv.HasFocus = focused;
  919. Assert.Equal (focused, tv.HasFocus);
  920. tv.Draw ();
  921. var expected = @"
  922. ┌─┬─┐
  923. │A│B│
  924. ├─┼─┤
  925. │1│2│
  926. ";
  927. TestHelpers.AssertDriverContentsAre (expected, output);
  928. var expectedColors = @"
  929. 00000
  930. 00000
  931. 00000
  932. 21222
  933. ";
  934. TestHelpers.AssertDriverAttributesAre (
  935. expectedColors,
  936. output,
  937. Application.Driver,
  938. tv.ColorScheme.Normal,
  939. focused ? rowHighlight.Focus : rowHighlight.HotNormal,
  940. rowHighlight.Normal
  941. );
  942. // change the value in the table so that
  943. // it no longer matches the RowColorGetter
  944. // delegate conditional ( which checks for
  945. // the value 2)
  946. dt.Rows [0] [1] = 5;
  947. tv.SetNeedsDisplay ();
  948. tv.Draw ();
  949. expected = @"
  950. ┌─┬─┐
  951. │A│B│
  952. ├─┼─┤
  953. │1│5│
  954. ";
  955. TestHelpers.AssertDriverContentsAre (expected, output);
  956. expectedColors = @"
  957. 00000
  958. 00000
  959. 00000
  960. 01000
  961. ";
  962. // now we only see 2 colors used (the selected cell color and Normal
  963. // rowHighlight should no longer be used because the delegate returned null
  964. // (now that the cell value is 5 - which does not match the conditional)
  965. TestHelpers.AssertDriverAttributesAre (
  966. expectedColors,
  967. output,
  968. Application.Driver,
  969. tv.ColorScheme.Normal,
  970. focused ? tv.ColorScheme.Focus : tv.ColorScheme.HotNormal
  971. );
  972. top.Dispose ();
  973. }
  974. [Theory]
  975. [AutoInitShutdown (configLocation: ConfigurationManager.ConfigLocations.DefaultOnly)]
  976. [InlineData (false)]
  977. [InlineData (true)]
  978. public void TableView_ColorTests_FocusedOrNot (bool focused)
  979. {
  980. TableView tv = SetUpMiniTable ();
  981. tv.LayoutSubviews ();
  982. // width exactly matches the max col widths
  983. tv.Viewport = new (0, 0, 5, 4);
  984. var top = new Toplevel ();
  985. top.Add (tv);
  986. Application.Begin (top);
  987. tv.HasFocus = focused;
  988. Assert.Equal (focused, tv.HasFocus);
  989. tv.Draw ();
  990. var expected = @"
  991. ┌─┬─┐
  992. │A│B│
  993. ├─┼─┤
  994. │1│2│
  995. ";
  996. TestHelpers.AssertDriverContentsAre (expected, output);
  997. var expectedColors = @"
  998. 00000
  999. 00000
  1000. 00000
  1001. 01000
  1002. ";
  1003. TestHelpers.AssertDriverAttributesAre (
  1004. expectedColors,
  1005. output,
  1006. Application.Driver,
  1007. tv.ColorScheme.Normal,
  1008. focused ? tv.ColorScheme.Focus : tv.ColorScheme.HotNormal
  1009. );
  1010. top.Dispose ();
  1011. }
  1012. [Theory]
  1013. [SetupFakeDriver]
  1014. [InlineData (false)]
  1015. [InlineData (true)]
  1016. public void TableView_ColorTests_InvertSelectedCellFirstCharacter (bool focused)
  1017. {
  1018. TableView tv = SetUpMiniTable ();
  1019. tv.Style.InvertSelectedCellFirstCharacter = true;
  1020. tv.LayoutSubviews ();
  1021. // width exactly matches the max col widths
  1022. tv.Viewport = new (0, 0, 5, 4);
  1023. tv.HasFocus = focused;
  1024. Assert.Equal (focused, tv.HasFocus);
  1025. tv.Draw ();
  1026. var expected = @"
  1027. ┌─┬─┐
  1028. │A│B│
  1029. ├─┼─┤
  1030. │1│2│
  1031. ";
  1032. TestHelpers.AssertDriverContentsAre (expected, output);
  1033. var expectedColors = @"
  1034. 00000
  1035. 00000
  1036. 00000
  1037. 01000
  1038. ";
  1039. var invertFocus = new Attribute (tv.ColorScheme.Focus.Background, tv.ColorScheme.Focus.Foreground);
  1040. var invertHotNormal = new Attribute (tv.ColorScheme.HotNormal.Background, tv.ColorScheme.HotNormal.Foreground);
  1041. TestHelpers.AssertDriverAttributesAre (
  1042. expectedColors,
  1043. output,
  1044. Application.Driver,
  1045. tv.ColorScheme.Normal,
  1046. focused ? invertFocus : invertHotNormal
  1047. );
  1048. }
  1049. [Fact]
  1050. [SetupFakeDriver]
  1051. public void TableView_ExpandLastColumn_False ()
  1052. {
  1053. TableView tv = SetUpMiniTable ();
  1054. // the thing we are testing
  1055. tv.Style.ExpandLastColumn = false;
  1056. tv.Draw ();
  1057. var expected = @"
  1058. ┌─┬─┬────┐
  1059. │A│B│ │
  1060. ├─┼─┼────┤
  1061. │1│2│ │
  1062. ";
  1063. TestHelpers.AssertDriverContentsAre (expected, output);
  1064. }
  1065. [Fact]
  1066. [SetupFakeDriver]
  1067. public void TableView_ExpandLastColumn_False_ExactBounds ()
  1068. {
  1069. TableView tv = SetUpMiniTable ();
  1070. // the thing we are testing
  1071. tv.Style.ExpandLastColumn = false;
  1072. // width exactly matches the max col widths
  1073. tv.Viewport = new (0, 0, 5, 4);
  1074. tv.Draw ();
  1075. var expected = @"
  1076. ┌─┬─┐
  1077. │A│B│
  1078. ├─┼─┤
  1079. │1│2│
  1080. ";
  1081. TestHelpers.AssertDriverContentsAre (expected, output);
  1082. }
  1083. [Fact]
  1084. [SetupFakeDriver]
  1085. public void TableView_ExpandLastColumn_True ()
  1086. {
  1087. TableView tv = SetUpMiniTable ();
  1088. // the thing we are testing
  1089. tv.Style.ExpandLastColumn = true;
  1090. tv.Draw ();
  1091. var expected = @"
  1092. ┌─┬──────┐
  1093. │A│B │
  1094. ├─┼──────┤
  1095. │1│2 │
  1096. ";
  1097. TestHelpers.AssertDriverContentsAre (expected, output);
  1098. }
  1099. [Fact]
  1100. [SetupFakeDriver]
  1101. public void TableView_ShowHeadersFalse_AllLines ()
  1102. {
  1103. TableView tv = GetABCDEFTableView (out _);
  1104. tv.Viewport = new (0, 0, 5, 5);
  1105. tv.Style.ShowHeaders = false;
  1106. tv.Style.ShowHorizontalHeaderOverline = true;
  1107. tv.Style.ShowHorizontalHeaderUnderline = true;
  1108. // Horizontal scrolling option is part of the underline
  1109. tv.Style.ShowHorizontalScrollIndicators = true;
  1110. tv.Draw ();
  1111. var expected = @"
  1112. ┌─┬─┐
  1113. ├─┼─►
  1114. │1│2│
  1115. ";
  1116. TestHelpers.AssertDriverContentsAre (expected, output);
  1117. }
  1118. [Fact]
  1119. [SetupFakeDriver]
  1120. public void TableView_ShowHeadersFalse_AndNoHeaderLines ()
  1121. {
  1122. TableView tv = GetABCDEFTableView (out _);
  1123. tv.Viewport = new (0, 0, 5, 5);
  1124. tv.Style.ShowHeaders = false;
  1125. tv.Style.ShowHorizontalHeaderOverline = false;
  1126. tv.Style.ShowHorizontalHeaderUnderline = false;
  1127. tv.Draw ();
  1128. var expected = @"
  1129. │1│2│
  1130. ";
  1131. TestHelpers.AssertDriverContentsAre (expected, output);
  1132. }
  1133. [Fact]
  1134. [SetupFakeDriver]
  1135. public void TableView_ShowHeadersFalse_OverlineTrue ()
  1136. {
  1137. TableView tv = GetABCDEFTableView (out _);
  1138. tv.Viewport = new (0, 0, 5, 5);
  1139. tv.Style.ShowHeaders = false;
  1140. tv.Style.ShowHorizontalHeaderOverline = true;
  1141. tv.Style.ShowHorizontalHeaderUnderline = false;
  1142. tv.Draw ();
  1143. var expected = @"
  1144. ┌─┬─┐
  1145. │1│2│
  1146. ";
  1147. TestHelpers.AssertDriverContentsAre (expected, output);
  1148. }
  1149. [Fact]
  1150. [SetupFakeDriver]
  1151. public void TableView_ShowHeadersFalse_UnderlineTrue ()
  1152. {
  1153. TableView tv = GetABCDEFTableView (out _);
  1154. tv.Viewport = new (0, 0, 5, 5);
  1155. tv.Style.ShowHeaders = false;
  1156. tv.Style.ShowHorizontalHeaderOverline = false;
  1157. tv.Style.ShowHorizontalHeaderUnderline = true;
  1158. // Horizontal scrolling option is part of the underline
  1159. tv.Style.ShowHorizontalScrollIndicators = true;
  1160. tv.Draw ();
  1161. var expected = @"
  1162. ├─┼─►
  1163. │1│2│
  1164. ";
  1165. TestHelpers.AssertDriverContentsAre (expected, output);
  1166. }
  1167. [Fact]
  1168. [SetupFakeDriver]
  1169. public void TableViewMultiSelect_CannotFallOffBottom ()
  1170. {
  1171. TableView tv = SetUpMiniTable (out DataTable dt);
  1172. dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  1173. tv.MultiSelect = true;
  1174. tv.SelectedColumn = 0;
  1175. tv.SelectedRow = 0;
  1176. tv.NewKeyDownEvent (Key.CursorRight.WithShift);
  1177. tv.NewKeyDownEvent (Key.CursorDown.WithShift);
  1178. Assert.Equal (new (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rectangle);
  1179. // this next moves should be ignored because we already selected the whole table
  1180. tv.NewKeyDownEvent (Key.CursorRight.WithShift);
  1181. tv.NewKeyDownEvent (Key.CursorDown.WithShift);
  1182. Assert.Equal (new (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rectangle);
  1183. Assert.Equal (1, tv.SelectedColumn);
  1184. Assert.Equal (1, tv.SelectedRow);
  1185. }
  1186. [Fact]
  1187. [SetupFakeDriver]
  1188. public void TableViewMultiSelect_CannotFallOffLeft ()
  1189. {
  1190. TableView tv = SetUpMiniTable (out DataTable dt);
  1191. dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  1192. tv.MultiSelect = true;
  1193. tv.SelectedColumn = 1;
  1194. tv.SelectedRow = 1;
  1195. tv.NewKeyDownEvent (Key.CursorLeft.WithShift);
  1196. Assert.Equal (new (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rectangle);
  1197. // this next shift left should be ignored because we are already at the bounds
  1198. tv.NewKeyDownEvent (Key.CursorLeft.WithShift);
  1199. Assert.Equal (new (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rectangle);
  1200. Assert.Equal (0, tv.SelectedColumn);
  1201. Assert.Equal (1, tv.SelectedRow);
  1202. }
  1203. [Fact]
  1204. [SetupFakeDriver]
  1205. public void TableViewMultiSelect_CannotFallOffRight ()
  1206. {
  1207. TableView tv = SetUpMiniTable (out DataTable dt);
  1208. dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  1209. tv.MultiSelect = true;
  1210. tv.SelectedColumn = 0;
  1211. tv.SelectedRow = 1;
  1212. tv.NewKeyDownEvent (Key.CursorRight.WithShift);
  1213. Assert.Equal (new (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rectangle);
  1214. // this next shift right should be ignored because we are already at the right bounds
  1215. tv.NewKeyDownEvent (Key.CursorRight.WithShift);
  1216. Assert.Equal (new (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rectangle);
  1217. Assert.Equal (1, tv.SelectedColumn);
  1218. Assert.Equal (1, tv.SelectedRow);
  1219. }
  1220. [Fact]
  1221. [SetupFakeDriver]
  1222. public void TableViewMultiSelect_CannotFallOffTop ()
  1223. {
  1224. TableView tv = SetUpMiniTable (out DataTable dt);
  1225. dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
  1226. tv.LayoutSubviews ();
  1227. tv.MultiSelect = true;
  1228. tv.SelectedColumn = 1;
  1229. tv.SelectedRow = 1;
  1230. tv.NewKeyDownEvent (Key.CursorLeft.WithShift);
  1231. tv.NewKeyDownEvent (Key.CursorUp.WithShift);
  1232. Assert.Equal (new (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rectangle);
  1233. // this next moves should be ignored because we already selected the whole table
  1234. tv.NewKeyDownEvent (Key.CursorLeft.WithShift);
  1235. tv.NewKeyDownEvent (Key.CursorUp.WithShift);
  1236. Assert.Equal (new (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rectangle);
  1237. Assert.Equal (0, tv.SelectedColumn);
  1238. Assert.Equal (0, tv.SelectedRow);
  1239. }
  1240. [Fact]
  1241. [AutoInitShutdown (configLocation: ConfigurationManager.ConfigLocations.DefaultOnly)]
  1242. public void Test_CollectionNavigator ()
  1243. {
  1244. var tv = new TableView ();
  1245. tv.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  1246. tv.Viewport = new (0, 0, 50, 7);
  1247. tv.Table = new EnumerableTableSource<string> (
  1248. new [] { "fish", "troll", "trap", "zoo" },
  1249. new () { { "Name", t => t }, { "EndsWith", t => t.Last () } }
  1250. );
  1251. tv.LayoutSubviews ();
  1252. tv.Draw ();
  1253. var expected =
  1254. @"
  1255. ┌─────┬──────────────────────────────────────────┐
  1256. │Name │EndsWith │
  1257. ├─────┼──────────────────────────────────────────┤
  1258. │fish │h │
  1259. │troll│l │
  1260. │trap │p │
  1261. │zoo │o │";
  1262. TestHelpers.AssertDriverContentsAre (expected, output);
  1263. Assert.Equal (0, tv.SelectedRow);
  1264. // this test assumes no focus
  1265. Assert.False (tv.HasFocus);
  1266. // already on fish
  1267. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.F });
  1268. Assert.Equal (0, tv.SelectedRow);
  1269. // not focused
  1270. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.Z });
  1271. Assert.Equal (0, tv.SelectedRow);
  1272. // ensure that TableView has the input focus
  1273. var top = new Toplevel ();
  1274. top.Add (tv);
  1275. Application.Begin (top);
  1276. top.FocusDeepest (NavigationDirection.Forward, null);
  1277. Assert.True (tv.HasFocus);
  1278. // already on fish
  1279. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.F });
  1280. Assert.Equal (0, tv.SelectedRow);
  1281. // move to zoo
  1282. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.Z });
  1283. Assert.Equal (3, tv.SelectedRow);
  1284. // move to troll
  1285. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.T });
  1286. Assert.Equal (1, tv.SelectedRow);
  1287. // move to trap
  1288. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.T });
  1289. Assert.Equal (2, tv.SelectedRow);
  1290. // change columns to navigate by column 2
  1291. Assert.Equal (0, tv.SelectedColumn);
  1292. Assert.Equal (2, tv.SelectedRow);
  1293. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  1294. Assert.Equal (1, tv.SelectedColumn);
  1295. Assert.Equal (2, tv.SelectedRow);
  1296. // nothing ends with t so stay where you are
  1297. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.T });
  1298. Assert.Equal (2, tv.SelectedRow);
  1299. //jump to fish which ends in h
  1300. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.H });
  1301. Assert.Equal (0, tv.SelectedRow);
  1302. // jump to zoo which ends in o
  1303. tv.NewKeyDownEvent (new () { KeyCode = KeyCode.O });
  1304. Assert.Equal (3, tv.SelectedRow);
  1305. top.Dispose ();
  1306. }
  1307. [Fact]
  1308. [SetupFakeDriver]
  1309. public void Test_ScreenToCell ()
  1310. {
  1311. TableView tableView = GetTwoRowSixColumnTable ();
  1312. tableView.BeginInit ();
  1313. tableView.EndInit ();
  1314. tableView.LayoutSubviews ();
  1315. tableView.Draw ();
  1316. // user can only scroll right so sees right indicator
  1317. // Because first column in table is A
  1318. var expected =
  1319. @"
  1320. │A│B│C│
  1321. ├─┼─┼─►
  1322. │1│2│3│
  1323. │1│2│3│";
  1324. TestHelpers.AssertDriverContentsAre (expected, output);
  1325. // ---------------- X=0 -----------------------
  1326. // click is before first cell
  1327. Assert.Null (tableView.ScreenToCell (0, 0));
  1328. Assert.Null (tableView.ScreenToCell (0, 1));
  1329. Assert.Null (tableView.ScreenToCell (0, 2));
  1330. Assert.Null (tableView.ScreenToCell (0, 3));
  1331. Assert.Null (tableView.ScreenToCell (0, 4));
  1332. // ---------------- X=1 -----------------------
  1333. // click in header
  1334. Assert.Null (tableView.ScreenToCell (1, 0));
  1335. // click in header row line
  1336. Assert.Null (tableView.ScreenToCell (1, 1));
  1337. // click in cell 0,0
  1338. Assert.Equal (Point.Empty, tableView.ScreenToCell (1, 2));
  1339. // click in cell 0,1
  1340. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3));
  1341. // after last row
  1342. Assert.Null (tableView.ScreenToCell (1, 4));
  1343. // ---------------- X=2 -----------------------
  1344. // ( even though there is a horizontal dividing line here we treat it as a hit on the cell before)
  1345. // click in header
  1346. Assert.Null (tableView.ScreenToCell (2, 0));
  1347. // click in header row line
  1348. Assert.Null (tableView.ScreenToCell (2, 1));
  1349. // click in cell 0,0
  1350. Assert.Equal (Point.Empty, tableView.ScreenToCell (2, 2));
  1351. // click in cell 0,1
  1352. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3));
  1353. // after last row
  1354. Assert.Null (tableView.ScreenToCell (2, 4));
  1355. // ---------------- X=3 -----------------------
  1356. // click in header
  1357. Assert.Null (tableView.ScreenToCell (3, 0));
  1358. // click in header row line
  1359. Assert.Null (tableView.ScreenToCell (3, 1));
  1360. // click in cell 1,0
  1361. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2));
  1362. // click in cell 1,1
  1363. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3));
  1364. // after last row
  1365. Assert.Null (tableView.ScreenToCell (3, 4));
  1366. }
  1367. [Fact]
  1368. [SetupFakeDriver]
  1369. public void Test_ScreenToCell_DataColumnOverload ()
  1370. {
  1371. TableView tableView = GetTwoRowSixColumnTable ();
  1372. tableView.LayoutSubviews ();
  1373. tableView.Draw ();
  1374. // user can only scroll right so sees right indicator
  1375. // Because first column in table is A
  1376. var expected =
  1377. @"
  1378. │A│B│C│
  1379. ├─┼─┼─►
  1380. │1│2│3│
  1381. │1│2│3│";
  1382. TestHelpers.AssertDriverContentsAre (expected, output);
  1383. int? col;
  1384. // ---------------- X=0 -----------------------
  1385. // click is before first cell
  1386. Assert.Null (tableView.ScreenToCell (0, 0, out col));
  1387. Assert.Null (col);
  1388. Assert.Null (tableView.ScreenToCell (0, 1, out col));
  1389. Assert.Null (col);
  1390. Assert.Null (tableView.ScreenToCell (0, 2, out col));
  1391. Assert.Null (col);
  1392. Assert.Null (tableView.ScreenToCell (0, 3, out col));
  1393. Assert.Null (col);
  1394. Assert.Null (tableView.ScreenToCell (0, 4, out col));
  1395. Assert.Null (col);
  1396. // ---------------- X=1 -----------------------
  1397. // click in header
  1398. Assert.Null (tableView.ScreenToCell (1, 0, out col));
  1399. Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
  1400. // click in header row line (click in the horizontal line below header counts as click in header above - consistent with the column hit box)
  1401. Assert.Null (tableView.ScreenToCell (1, 1, out col));
  1402. Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
  1403. // click in cell 0,0
  1404. Assert.Equal (Point.Empty, tableView.ScreenToCell (1, 2, out col));
  1405. Assert.Null (col);
  1406. // click in cell 0,1
  1407. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3, out col));
  1408. Assert.Null (col);
  1409. // after last row
  1410. Assert.Null (tableView.ScreenToCell (1, 4, out col));
  1411. Assert.Null (col);
  1412. // ---------------- X=2 -----------------------
  1413. // click in header
  1414. Assert.Null (tableView.ScreenToCell (2, 0, out col));
  1415. Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
  1416. // click in header row line
  1417. Assert.Null (tableView.ScreenToCell (2, 1, out col));
  1418. Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
  1419. // click in cell 0,0
  1420. Assert.Equal (Point.Empty, tableView.ScreenToCell (2, 2, out col));
  1421. Assert.Null (col);
  1422. // click in cell 0,1
  1423. Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3, out col));
  1424. Assert.Null (col);
  1425. // after last row
  1426. Assert.Null (tableView.ScreenToCell (2, 4, out col));
  1427. Assert.Null (col);
  1428. // ---------------- X=3 -----------------------
  1429. // click in header
  1430. Assert.Null (tableView.ScreenToCell (3, 0, out col));
  1431. Assert.Equal ("B", tableView.Table.ColumnNames [col.Value]);
  1432. // click in header row line
  1433. Assert.Null (tableView.ScreenToCell (3, 1, out col));
  1434. Assert.Equal ("B", tableView.Table.ColumnNames [col.Value]);
  1435. // click in cell 1,0
  1436. Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2, out col));
  1437. Assert.Null (col);
  1438. // click in cell 1,1
  1439. Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3, out col));
  1440. Assert.Null (col);
  1441. // after last row
  1442. Assert.Null (tableView.ScreenToCell (3, 4, out col));
  1443. Assert.Null (col);
  1444. }
  1445. [Fact]
  1446. public void Test_SumColumnWidth_UnicodeLength ()
  1447. {
  1448. Assert.Equal (11, "hello there".EnumerateRunes ().Sum (c => c.GetColumns ()));
  1449. // Creates a string with the peculiar (french?) r symbol
  1450. string surrogate = "Les Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
  1451. // The unicode width of this string is shorter than the string length!
  1452. Assert.Equal (14, surrogate.EnumerateRunes ().Sum (c => c.GetColumns ()));
  1453. Assert.Equal (15, surrogate.Length);
  1454. }
  1455. [Fact]
  1456. [SetupFakeDriver]
  1457. public void TestColumnStyle_AllColumnsVisibleFalse_BehavesAsTableNull ()
  1458. {
  1459. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1460. for (var i = 0; i < 6; i++)
  1461. {
  1462. tableView.Style.GetOrCreateColumnStyle (i).Visible = false;
  1463. }
  1464. tableView.LayoutSubviews ();
  1465. // expect nothing to be rendered when all columns are invisible
  1466. var expected =
  1467. @"
  1468. ";
  1469. tableView.Draw ();
  1470. TestHelpers.AssertDriverContentsAre (expected, output);
  1471. // expect behavior to match when Table is null
  1472. tableView.Table = null;
  1473. tableView.Draw ();
  1474. TestHelpers.AssertDriverContentsAre (expected, output);
  1475. }
  1476. [InlineData (true)]
  1477. [InlineData (false)]
  1478. [Theory]
  1479. [SetupFakeDriver]
  1480. public void TestColumnStyle_FirstColumnVisibleFalse_CursorStaysAt1 (bool useHome)
  1481. {
  1482. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1483. tableView.LayoutSubviews ();
  1484. tableView.Style.GetOrCreateColumnStyle (0).Visible = false;
  1485. tableView.SelectedColumn = 0;
  1486. Assert.Equal (0, tableView.SelectedColumn);
  1487. // column 0 is invisible so this method should move to 1
  1488. tableView.EnsureValidSelection ();
  1489. Assert.Equal (1, tableView.SelectedColumn);
  1490. tableView.NewKeyDownEvent (
  1491. new () { KeyCode = useHome ? KeyCode.Home : KeyCode.CursorLeft }
  1492. );
  1493. // Expect the cursor to stay at 1
  1494. Assert.Equal (1, tableView.SelectedColumn);
  1495. }
  1496. [Fact]
  1497. [SetupFakeDriver]
  1498. public void TestColumnStyle_FirstColumnVisibleFalse_IsNotRendered ()
  1499. {
  1500. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1501. tableView.Style.ShowHorizontalScrollIndicators = true;
  1502. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1503. tableView.Style.GetOrCreateColumnStyle (0).Visible = false;
  1504. tableView.LayoutSubviews ();
  1505. tableView.Draw ();
  1506. var expected =
  1507. @"
  1508. │B│C│D│
  1509. ├─┼─┼─►
  1510. │2│3│4│";
  1511. TestHelpers.AssertDriverContentsAre (expected, output);
  1512. }
  1513. [InlineData (true)]
  1514. [InlineData (false)]
  1515. [Theory]
  1516. [SetupFakeDriver]
  1517. public void TestColumnStyle_LastColumnVisibleFalse_CursorStaysAt2 (bool useEnd)
  1518. {
  1519. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1520. tableView.LayoutSubviews ();
  1521. // select D
  1522. tableView.SelectedColumn = 3;
  1523. Assert.Equal (3, tableView.SelectedColumn);
  1524. tableView.Style.GetOrCreateColumnStyle (3).Visible = false;
  1525. tableView.Style.GetOrCreateColumnStyle (4).Visible = false;
  1526. tableView.Style.GetOrCreateColumnStyle (5).Visible = false;
  1527. // column D is invisible so this method should move to 2 (C)
  1528. tableView.EnsureValidSelection ();
  1529. Assert.Equal (2, tableView.SelectedColumn);
  1530. tableView.NewKeyDownEvent (
  1531. new () { KeyCode = useEnd ? KeyCode.End : KeyCode.CursorRight }
  1532. );
  1533. // Expect the cursor to stay at 2
  1534. Assert.Equal (2, tableView.SelectedColumn);
  1535. }
  1536. [Fact]
  1537. [SetupFakeDriver]
  1538. public void TestColumnStyle_PreceedingColumnsInvisible_NoScrollIndicator ()
  1539. {
  1540. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1541. tableView.Style.ShowHorizontalScrollIndicators = true;
  1542. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1543. tableView.ColumnOffset = 1;
  1544. tableView.LayoutSubviews ();
  1545. tableView.Draw ();
  1546. // normally we should have scroll indicators because A,E and F are of screen
  1547. var expected =
  1548. @"
  1549. │B│C│D│
  1550. ◄─┼─┼─►
  1551. │2│3│4│";
  1552. TestHelpers.AssertDriverContentsAre (expected, output);
  1553. // but if E and F are invisible so we shouldn't show right
  1554. tableView.Style.GetOrCreateColumnStyle (4).Visible = false;
  1555. tableView.Style.GetOrCreateColumnStyle (5).Visible = false;
  1556. expected =
  1557. @"
  1558. │B│C│D│
  1559. ◄─┼─┼─┤
  1560. │2│3│4│";
  1561. tableView.SetNeedsDisplay ();
  1562. tableView.Draw ();
  1563. TestHelpers.AssertDriverContentsAre (expected, output);
  1564. // now also A is invisible so we cannot scroll in either direction
  1565. tableView.Style.GetOrCreateColumnStyle (0).Visible = false;
  1566. expected =
  1567. @"
  1568. │B│C│D│
  1569. ├─┼─┼─┤
  1570. │2│3│4│";
  1571. tableView.SetNeedsDisplay ();
  1572. tableView.Draw ();
  1573. TestHelpers.AssertDriverContentsAre (expected, output);
  1574. }
  1575. [Fact]
  1576. [SetupFakeDriver]
  1577. public void TestColumnStyle_RemainingColumnsInvisible_NoScrollIndicator ()
  1578. {
  1579. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1580. tableView.Style.ShowHorizontalScrollIndicators = true;
  1581. tableView.Style.ShowHorizontalHeaderUnderline = true;
  1582. tableView.LayoutSubviews ();
  1583. tableView.SetNeedsDisplay ();
  1584. tableView.Draw ();
  1585. // normally we should have scroll indicators because DEF are of screen
  1586. var expected =
  1587. @"
  1588. │A│B│C│
  1589. ├─┼─┼─►
  1590. │1│2│3│";
  1591. TestHelpers.AssertDriverContentsAre (expected, output);
  1592. // but if DEF are invisible we shouldn't be showing the indicator
  1593. tableView.Style.GetOrCreateColumnStyle (3).Visible = false;
  1594. tableView.Style.GetOrCreateColumnStyle (4).Visible = false;
  1595. tableView.Style.GetOrCreateColumnStyle (5).Visible = false;
  1596. expected =
  1597. @"
  1598. │A│B│C│
  1599. ├─┼─┼─┤
  1600. │1│2│3│";
  1601. tableView.SetNeedsDisplay ();
  1602. tableView.Draw ();
  1603. TestHelpers.AssertDriverContentsAre (expected, output);
  1604. }
  1605. [Fact]
  1606. [SetupFakeDriver]
  1607. public void TestColumnStyle_VisibleFalse_CursorStepsOverInvisibleColumns ()
  1608. {
  1609. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1610. tableView.LayoutSubviews ();
  1611. tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
  1612. tableView.SelectedColumn = 0;
  1613. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  1614. // Expect the cursor navigation to skip over the invisible column(s)
  1615. Assert.Equal (2, tableView.SelectedColumn);
  1616. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorLeft });
  1617. // Expect the cursor navigation backwards to skip over invisible column too
  1618. Assert.Equal (0, tableView.SelectedColumn);
  1619. }
  1620. [Theory]
  1621. [SetupFakeDriver]
  1622. [InlineData (true, true)]
  1623. [InlineData (false, true)]
  1624. [InlineData (true, false)]
  1625. [InlineData (false, false)]
  1626. public void TestColumnStyle_VisibleFalse_DoesNotEffect_EnsureSelectedCellIsVisible (
  1627. bool smooth,
  1628. bool invisibleCol
  1629. )
  1630. {
  1631. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1632. tableView.LayoutSubviews ();
  1633. tableView.Style.SmoothHorizontalScrolling = smooth;
  1634. if (invisibleCol)
  1635. {
  1636. tableView.Style.GetOrCreateColumnStyle (3).Visible = false;
  1637. }
  1638. // New TableView should have first cell selected
  1639. Assert.Equal (0, tableView.SelectedColumn);
  1640. // With no scrolling
  1641. Assert.Equal (0, tableView.ColumnOffset);
  1642. // A,B and C are visible on screen at the moment so these should have no effect
  1643. tableView.SelectedColumn = 1;
  1644. tableView.EnsureSelectedCellIsVisible ();
  1645. Assert.Equal (0, tableView.ColumnOffset);
  1646. tableView.SelectedColumn = 2;
  1647. tableView.EnsureSelectedCellIsVisible ();
  1648. Assert.Equal (0, tableView.ColumnOffset);
  1649. // Selecting D should move the visible table area to fit D onto the screen
  1650. tableView.SelectedColumn = 3;
  1651. tableView.EnsureSelectedCellIsVisible ();
  1652. Assert.Equal (smooth ? 1 : 3, tableView.ColumnOffset);
  1653. }
  1654. [Fact]
  1655. [SetupFakeDriver]
  1656. public void TestColumnStyle_VisibleFalse_IsNotRendered ()
  1657. {
  1658. TableView tableView = GetABCDEFTableView (out _);
  1659. tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
  1660. tableView.LayoutSubviews ();
  1661. tableView.Draw ();
  1662. var expected =
  1663. @"
  1664. │A│C│D│
  1665. │1│3│4│";
  1666. TestHelpers.AssertDriverContentsAre (expected, output);
  1667. }
  1668. [Fact]
  1669. [SetupFakeDriver]
  1670. public void TestColumnStyle_VisibleFalse_MultiSelected ()
  1671. {
  1672. TableView tableView = GetABCDEFTableView (out DataTable dt);
  1673. tableView.LayoutSubviews ();
  1674. // user has rectangular selection
  1675. tableView.MultiSelectedRegions.Push (
  1676. new (
  1677. Point.Empty,
  1678. new (0, 0, 3, 1)
  1679. )
  1680. );
  1681. Assert.Equal (3, tableView.GetAllSelectedCells ().Count ());
  1682. Assert.True (tableView.IsSelected (0, 0));
  1683. Assert.True (tableView.IsSelected (1, 0));
  1684. Assert.True (tableView.IsSelected (2, 0));
  1685. Assert.False (tableView.IsSelected (3, 0));
  1686. // if middle column is invisible
  1687. tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
  1688. // it should not be included in the selection
  1689. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1690. Assert.True (tableView.IsSelected (0, 0));
  1691. Assert.False (tableView.IsSelected (1, 0));
  1692. Assert.True (tableView.IsSelected (2, 0));
  1693. Assert.False (tableView.IsSelected (3, 0));
  1694. Assert.DoesNotContain (new (1, 0), tableView.GetAllSelectedCells ());
  1695. }
  1696. [Fact]
  1697. [SetupFakeDriver]
  1698. public void TestColumnStyle_VisibleFalse_MultiSelectingStepsOverInvisibleColumns ()
  1699. {
  1700. TableView tableView = GetABCDEFTableView (out _);
  1701. tableView.LayoutSubviews ();
  1702. // if middle column is invisible
  1703. tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
  1704. tableView.NewKeyDownEvent (Key.CursorRight.WithShift);
  1705. // Selection should extend from A to C but skip B
  1706. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  1707. Assert.True (tableView.IsSelected (0, 0));
  1708. Assert.False (tableView.IsSelected (1, 0));
  1709. Assert.True (tableView.IsSelected (2, 0));
  1710. Assert.False (tableView.IsSelected (3, 0));
  1711. Assert.DoesNotContain (new (1, 0), tableView.GetAllSelectedCells ());
  1712. }
  1713. [Fact]
  1714. [SetupFakeDriver]
  1715. public void TestControlClick_MultiSelect_ThreeRowTable_FullRowSelect ()
  1716. {
  1717. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  1718. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1719. tv.LayoutSubviews ();
  1720. tv.MultiSelect = true;
  1721. // Clicking in bottom row
  1722. tv.NewMouseEvent (
  1723. new () { Position = new (1, 4), Flags = MouseFlags.Button1Clicked }
  1724. );
  1725. // should select that row
  1726. Assert.Equal (2, tv.SelectedRow);
  1727. // shift clicking top row
  1728. tv.NewMouseEvent (
  1729. new () { Position = new (1, 2), Flags = MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl }
  1730. );
  1731. // should extend the selection
  1732. // to include bottom and top row but not middle
  1733. Assert.Equal (0, tv.SelectedRow);
  1734. Point [] selected = tv.GetAllSelectedCells ().ToArray ();
  1735. Assert.Contains (Point.Empty, selected);
  1736. Assert.DoesNotContain (new (0, 1), selected);
  1737. Assert.Contains (new (0, 2), selected);
  1738. }
  1739. [Fact]
  1740. [SetupFakeDriver]
  1741. public void TestEnumerableDataSource_BasicTypes ()
  1742. {
  1743. ((FakeDriver)Application.Driver!).SetBufferSize (100, 100);
  1744. var tv = new TableView ();
  1745. tv.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  1746. tv.Viewport = new (0, 0, 50, 6);
  1747. tv.Table = new EnumerableTableSource<Type> (
  1748. new [] { typeof (string), typeof (int), typeof (float) },
  1749. new ()
  1750. {
  1751. { "Name", t => t.Name }, { "Namespace", t => t.Namespace },
  1752. { "BaseType", t => t.BaseType }
  1753. }
  1754. );
  1755. tv.LayoutSubviews ();
  1756. tv.Draw ();
  1757. var expected =
  1758. @"
  1759. ┌──────┬─────────┬───────────────────────────────┐
  1760. │Name │Namespace│BaseType │
  1761. ├──────┼─────────┼───────────────────────────────┤
  1762. │String│System │System.Object │
  1763. │Int32 │System │System.ValueType │
  1764. │Single│System │System.ValueType │";
  1765. TestHelpers.AssertDriverContentsAre (expected, output);
  1766. }
  1767. [Fact]
  1768. [SetupFakeDriver]
  1769. public void TestFullRowSelect_AlwaysUseNormalColorForVerticalCellLines ()
  1770. {
  1771. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  1772. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1773. tv.Viewport = new (0, 0, 7, 6);
  1774. tv.Frame = new (0, 0, 7, 6);
  1775. tv.LayoutSubviews ();
  1776. tv.FullRowSelect = true;
  1777. tv.Style.ShowHorizontalBottomline = true;
  1778. tv.Style.AlwaysUseNormalColorForVerticalCellLines = true;
  1779. // Clicking in bottom row
  1780. tv.NewMouseEvent (
  1781. new () { Position = new (1, 4), Flags = MouseFlags.Button1Clicked }
  1782. );
  1783. // should select that row
  1784. Assert.Equal (2, tv.SelectedRow);
  1785. tv.Draw ();
  1786. var expected =
  1787. @"
  1788. │A│B│C│
  1789. ├─┼─┼─►
  1790. │1│2│3│
  1791. │1│2│3│
  1792. │1│2│3│
  1793. └─┴─┴─┘";
  1794. TestHelpers.AssertDriverContentsAre (expected, output);
  1795. Attribute normal = tv.ColorScheme.Normal;
  1796. tv.ColorScheme = new (tv.ColorScheme) { Focus = new (Color.Magenta, Color.White) };
  1797. Attribute focus = tv.ColorScheme.Focus;
  1798. tv.Draw ();
  1799. // Focus color (1) should be used for cells only because
  1800. // AlwaysUseNormalColorForVerticalCellLines is true
  1801. expected =
  1802. @"
  1803. 0000000
  1804. 0000000
  1805. 0000000
  1806. 0000000
  1807. 0101010
  1808. 0000000";
  1809. TestHelpers.AssertDriverAttributesAre (expected, output, Application.Driver, normal, focus);
  1810. }
  1811. [Fact]
  1812. [SetupFakeDriver]
  1813. public void TestFullRowSelect_SelectionColorDoesNotStop_WhenShowVerticalCellLinesIsFalse ()
  1814. {
  1815. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  1816. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1817. tv.LayoutSubviews ();
  1818. tv.Viewport = new (0, 0, 7, 6);
  1819. tv.FullRowSelect = true;
  1820. tv.Style.ShowVerticalCellLines = false;
  1821. tv.Style.ShowVerticalHeaderLines = false;
  1822. // Clicking in bottom row
  1823. tv.NewMouseEvent (
  1824. new () { Position = new (1, 4), Flags = MouseFlags.Button1Clicked }
  1825. );
  1826. // should select that row
  1827. Assert.Equal (2, tv.SelectedRow);
  1828. tv.Draw ();
  1829. var expected =
  1830. @"
  1831. A B C
  1832. ───────
  1833. 1 2 3
  1834. 1 2 3
  1835. 1 2 3";
  1836. TestHelpers.AssertDriverContentsAre (expected, output);
  1837. Attribute normal = tv.ColorScheme.Normal;
  1838. tv.ColorScheme = new (tv.ColorScheme) { Focus = new (Color.Magenta, Color.White) };
  1839. Attribute focus = tv.ColorScheme.Focus;
  1840. tv.Draw ();
  1841. // Focus color (1) should be used for rendering the selected line
  1842. // Note that because there are no vertical cell lines we use the focus
  1843. // color for the whole row
  1844. expected =
  1845. @"
  1846. 000000
  1847. 000000
  1848. 000000
  1849. 000000
  1850. 111111";
  1851. TestHelpers.AssertDriverAttributesAre (expected, output, Application.Driver, normal, focus);
  1852. }
  1853. [Fact]
  1854. [SetupFakeDriver]
  1855. public void TestFullRowSelect_SelectionColorStopsAtTableEdge_WithCellLines ()
  1856. {
  1857. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  1858. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  1859. tv.Viewport = new (0, 0, 7, 6);
  1860. tv.Frame = new (0, 0, 7, 6);
  1861. tv.LayoutSubviews ();
  1862. tv.FullRowSelect = true;
  1863. tv.Style.ShowHorizontalBottomline = true;
  1864. // Clicking in bottom row
  1865. tv.NewMouseEvent (
  1866. new () { Position = new (1, 4), Flags = MouseFlags.Button1Clicked }
  1867. );
  1868. // should select that row
  1869. Assert.Equal (2, tv.SelectedRow);
  1870. tv.Draw ();
  1871. var expected =
  1872. @"
  1873. │A│B│C│
  1874. ├─┼─┼─►
  1875. │1│2│3│
  1876. │1│2│3│
  1877. │1│2│3│
  1878. └─┴─┴─┘";
  1879. TestHelpers.AssertDriverContentsAre (expected, output);
  1880. Attribute normal = tv.ColorScheme.Normal;
  1881. tv.ColorScheme = new (tv.ColorScheme) { Focus = new (Color.Magenta, Color.White) };
  1882. Attribute focus = tv.ColorScheme.Focus;
  1883. tv.Draw ();
  1884. // Focus color (1) should be used for rendering the selected line
  1885. // But should not spill into the borders. Normal color (0) should be
  1886. // used for the rest.
  1887. expected =
  1888. @"
  1889. 0000000
  1890. 0000000
  1891. 0000000
  1892. 0000000
  1893. 0111110
  1894. 0000000";
  1895. TestHelpers.AssertDriverAttributesAre (expected, output, Application.Driver, normal, focus);
  1896. }
  1897. [Theory]
  1898. [SetupFakeDriver]
  1899. [InlineData (Orientation.Horizontal, false)]
  1900. [InlineData (Orientation.Vertical, false)]
  1901. [InlineData (Orientation.Horizontal, true)]
  1902. [InlineData (Orientation.Vertical, true)]
  1903. public void TestListTableSource (Orientation orient, bool parallel)
  1904. {
  1905. IList list = BuildList (16);
  1906. var tv = new TableView ();
  1907. //tv.BeginInit (); tv.EndInit ();
  1908. tv.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  1909. tv.Viewport = new (0, 0, 25, 4);
  1910. tv.Style = new ()
  1911. {
  1912. ShowHeaders = false, ShowHorizontalHeaderOverline = false, ShowHorizontalHeaderUnderline = false
  1913. };
  1914. var listStyle = new ListColumnStyle { Orientation = orient, ScrollParallel = parallel };
  1915. tv.Table = new ListTableSource (list, tv, listStyle);
  1916. tv.LayoutSubviews ();
  1917. tv.Draw ();
  1918. var horizPerpExpected =
  1919. @"
  1920. │Item 0│Item 1 │
  1921. │Item 2│Item 3 │
  1922. │Item 4│Item 5 │
  1923. │Item 6│Item 7 │";
  1924. var horizParaExpected =
  1925. @"
  1926. │Item 0 │Item 1 │Item 2 │
  1927. │Item 4 │Item 5 │Item 6 │
  1928. │Item 8 │Item 9 │Item 10│
  1929. │Item 12│Item 13│Item 14│";
  1930. var vertPerpExpected =
  1931. @"
  1932. │Item 0│Item 4│Item 8 │
  1933. │Item 1│Item 5│Item 9 │
  1934. │Item 2│Item 6│Item 10 │
  1935. │Item 3│Item 7│Item 11 │";
  1936. var vertParaExpected =
  1937. @"
  1938. │Item 0│Item 8 │
  1939. │Item 1│Item 9 │
  1940. │Item 2│Item 10 │
  1941. │Item 3│Item 11 │";
  1942. string expected;
  1943. if (orient == Orientation.Vertical)
  1944. {
  1945. if (parallel)
  1946. {
  1947. expected = vertParaExpected;
  1948. }
  1949. else
  1950. {
  1951. expected = vertPerpExpected;
  1952. }
  1953. }
  1954. else
  1955. {
  1956. if (parallel)
  1957. {
  1958. expected = horizParaExpected;
  1959. }
  1960. else
  1961. {
  1962. expected = horizPerpExpected;
  1963. }
  1964. }
  1965. TestHelpers.AssertDriverContentsAre (expected, output);
  1966. }
  1967. [InlineData (true)]
  1968. [InlineData (false)]
  1969. [Theory]
  1970. [SetupFakeDriver]
  1971. public void TestMoveStartEnd_WithFullRowSelect (bool withFullRowSelect)
  1972. {
  1973. TableView tableView = GetTwoRowSixColumnTable ();
  1974. tableView.LayoutSubviews ();
  1975. tableView.FullRowSelect = withFullRowSelect;
  1976. tableView.SelectedRow = 1;
  1977. tableView.SelectedColumn = 1;
  1978. tableView.NewKeyDownEvent (Key.Home.WithCtrl);
  1979. if (withFullRowSelect)
  1980. {
  1981. // Should not be any horizontal movement when
  1982. // using navigate to Start/End and FullRowSelect
  1983. Assert.Equal (1, tableView.SelectedColumn);
  1984. Assert.Equal (0, tableView.SelectedRow);
  1985. }
  1986. else
  1987. {
  1988. Assert.Equal (0, tableView.SelectedColumn);
  1989. Assert.Equal (0, tableView.SelectedRow);
  1990. }
  1991. tableView.NewKeyDownEvent (
  1992. new (
  1993. KeyCode.End | KeyCode.CtrlMask
  1994. )
  1995. );
  1996. if (withFullRowSelect)
  1997. {
  1998. Assert.Equal (1, tableView.SelectedColumn);
  1999. Assert.Equal (1, tableView.SelectedRow);
  2000. }
  2001. else
  2002. {
  2003. Assert.Equal (5, tableView.SelectedColumn);
  2004. Assert.Equal (1, tableView.SelectedRow);
  2005. }
  2006. }
  2007. [Fact]
  2008. [SetupFakeDriver]
  2009. public void TestShiftClick_MultiSelect_TwoRowTable_FullRowSelect ()
  2010. {
  2011. TableView tv = GetTwoRowSixColumnTable ();
  2012. tv.LayoutSubviews ();
  2013. tv.MultiSelect = true;
  2014. // Clicking in bottom row
  2015. tv.NewMouseEvent (
  2016. new () { Position = new (1, 3), Flags = MouseFlags.Button1Clicked }
  2017. );
  2018. // should select that row
  2019. Assert.Equal (1, tv.SelectedRow);
  2020. // shift clicking top row
  2021. tv.NewMouseEvent (
  2022. new () { Position = new (1, 2), Flags = MouseFlags.Button1Clicked | MouseFlags.ButtonShift }
  2023. );
  2024. // should extend the selection
  2025. Assert.Equal (0, tv.SelectedRow);
  2026. Point [] selected = tv.GetAllSelectedCells ().ToArray ();
  2027. Assert.Contains (Point.Empty, selected);
  2028. Assert.Contains (new (0, 1), selected);
  2029. }
  2030. [Fact]
  2031. [SetupFakeDriver]
  2032. public void TestTableViewCheckboxes_ByObject ()
  2033. {
  2034. Assert.Equal(ConfigurationManager.ConfigLocations.DefaultOnly, ConfigurationManager.Locations);
  2035. TableView tv = GetPetTable (out EnumerableTableSource<PickablePet> source);
  2036. tv.LayoutSubviews ();
  2037. IReadOnlyCollection<PickablePet> pets = source.Data;
  2038. CheckBoxTableSourceWrapperByObject<PickablePet> wrapper = new (
  2039. tv,
  2040. source,
  2041. p => p.IsPicked,
  2042. (p, b) => p.IsPicked = b
  2043. );
  2044. tv.Table = wrapper;
  2045. tv.Draw ();
  2046. var expected =
  2047. @"
  2048. ┌─┬───────┬─────────────┐
  2049. │ │Name │Kind │
  2050. ├─┼───────┼─────────────┤
  2051. │☐│Tammy │Cat │
  2052. │☐│Tibbles│Cat │
  2053. │☐│Ripper │Dog │";
  2054. TestHelpers.AssertDriverContentsAre (expected, output);
  2055. #pragma warning disable xUnit2029
  2056. Assert.Empty (pets.Where (p => p.IsPicked));
  2057. #pragma warning restore xUnit2029
  2058. tv.NewKeyDownEvent (Key.Space);
  2059. Assert.True (pets.First ().IsPicked);
  2060. tv.Draw ();
  2061. expected =
  2062. @"
  2063. ┌─┬───────┬─────────────┐
  2064. │ │Name │Kind │
  2065. ├─┼───────┼─────────────┤
  2066. │☑│Tammy │Cat │
  2067. │☐│Tibbles│Cat │
  2068. │☐│Ripper │Dog │";
  2069. TestHelpers.AssertDriverContentsAre (expected, output);
  2070. tv.NewKeyDownEvent (Key.CursorDown);
  2071. tv.NewKeyDownEvent (Key.Space);
  2072. Assert.True (pets.ElementAt (0).IsPicked);
  2073. Assert.True (pets.ElementAt (1).IsPicked);
  2074. Assert.False (pets.ElementAt (2).IsPicked);
  2075. tv.Draw ();
  2076. expected =
  2077. @"
  2078. ┌─┬───────┬─────────────┐
  2079. │ │Name │Kind │
  2080. ├─┼───────┼─────────────┤
  2081. │☑│Tammy │Cat │
  2082. │☑│Tibbles│Cat │
  2083. │☐│Ripper │Dog │";
  2084. TestHelpers.AssertDriverContentsAre (expected, output);
  2085. tv.NewKeyDownEvent (Key.CursorUp);
  2086. tv.NewKeyDownEvent (Key.Space);
  2087. Assert.False (pets.ElementAt (0).IsPicked);
  2088. Assert.True (pets.ElementAt (1).IsPicked);
  2089. Assert.False (pets.ElementAt (2).IsPicked);
  2090. tv.Draw ();
  2091. expected =
  2092. @"
  2093. ┌─┬───────┬─────────────┐
  2094. │ │Name │Kind │
  2095. ├─┼───────┼─────────────┤
  2096. │☐│Tammy │Cat │
  2097. │☑│Tibbles│Cat │
  2098. │☐│Ripper │Dog │";
  2099. TestHelpers.AssertDriverContentsAre (expected, output);
  2100. }
  2101. [Fact]
  2102. [SetupFakeDriver]
  2103. public void TestTableViewCheckboxes_MultiSelectIsUnion_WhenToggling ()
  2104. {
  2105. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  2106. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2107. tv.LayoutSubviews ();
  2108. var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
  2109. tv.Table = wrapper;
  2110. wrapper.CheckedRows.Add (0);
  2111. wrapper.CheckedRows.Add (2);
  2112. tv.Draw ();
  2113. var expected =
  2114. @"
  2115. │ │A│B│
  2116. ├─┼─┼─►
  2117. │☑│1│2│
  2118. │☐│1│2│
  2119. │☑│1│2│";
  2120. //toggle top two at once
  2121. tv.NewKeyDownEvent (Key.CursorDown.WithShift);
  2122. Assert.True (tv.IsSelected (0, 0));
  2123. Assert.True (tv.IsSelected (0, 1));
  2124. tv.NewKeyDownEvent (Key.Space);
  2125. // Because at least 1 of the rows is not yet ticked we toggle them all to ticked
  2126. TestHelpers.AssertDriverContentsAre (expected, output);
  2127. Assert.Contains (0, wrapper.CheckedRows);
  2128. Assert.Contains (1, wrapper.CheckedRows);
  2129. Assert.Contains (2, wrapper.CheckedRows);
  2130. Assert.Equal (3, wrapper.CheckedRows.Count);
  2131. tv.Draw ();
  2132. expected =
  2133. @"
  2134. │ │A│B│
  2135. ├─┼─┼─►
  2136. │☑│1│2│
  2137. │☑│1│2│
  2138. │☑│1│2│";
  2139. TestHelpers.AssertDriverContentsAre (expected, output);
  2140. // Untoggle the top 2
  2141. tv.NewKeyDownEvent (Key.Space);
  2142. tv.Draw ();
  2143. expected =
  2144. @"
  2145. │ │A│B│
  2146. ├─┼─┼─►
  2147. │☐│1│2│
  2148. │☐│1│2│
  2149. │☑│1│2│";
  2150. TestHelpers.AssertDriverContentsAre (expected, output);
  2151. Assert.Single (wrapper.CheckedRows, 2);
  2152. }
  2153. [Fact]
  2154. [SetupFakeDriver]
  2155. public void TestTableViewCheckboxes_SelectAllToggle ()
  2156. {
  2157. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  2158. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2159. tv.LayoutSubviews ();
  2160. var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
  2161. tv.Table = wrapper;
  2162. //toggle all cells
  2163. tv.NewKeyDownEvent (Key.A.WithCtrl);
  2164. tv.NewKeyDownEvent (Key.Space);
  2165. tv.Draw ();
  2166. var expected =
  2167. @"
  2168. │ │A│B│
  2169. ├─┼─┼─►
  2170. │☑│1│2│
  2171. │☑│1│2│
  2172. │☑│1│2│";
  2173. TestHelpers.AssertDriverContentsAre (expected, output);
  2174. Assert.Contains (0, wrapper.CheckedRows);
  2175. Assert.Contains (1, wrapper.CheckedRows);
  2176. Assert.Contains (2, wrapper.CheckedRows);
  2177. Assert.Equal (3, wrapper.CheckedRows.Count);
  2178. // Untoggle all again
  2179. tv.NewKeyDownEvent (Key.Space);
  2180. tv.Draw ();
  2181. expected =
  2182. @"
  2183. │ │A│B│
  2184. ├─┼─┼─►
  2185. │☐│1│2│
  2186. │☐│1│2│
  2187. │☐│1│2│";
  2188. TestHelpers.AssertDriverContentsAre (expected, output);
  2189. Assert.Empty (wrapper.CheckedRows);
  2190. }
  2191. [Fact]
  2192. [SetupFakeDriver]
  2193. public void TestTableViewCheckboxes_SelectAllToggle_ByObject ()
  2194. {
  2195. TableView tv = GetPetTable (out EnumerableTableSource<PickablePet> source);
  2196. tv.LayoutSubviews ();
  2197. IReadOnlyCollection<PickablePet> pets = source.Data;
  2198. CheckBoxTableSourceWrapperByObject<PickablePet> wrapper = new (
  2199. tv,
  2200. source,
  2201. p => p.IsPicked,
  2202. (p, b) => p.IsPicked = b
  2203. );
  2204. tv.Table = wrapper;
  2205. Assert.DoesNotContain (pets, p => p.IsPicked);
  2206. //toggle all cells
  2207. tv.NewKeyDownEvent (Key.A.WithCtrl);
  2208. tv.NewKeyDownEvent (Key.Space);
  2209. Assert.True (pets.All (p => p.IsPicked));
  2210. tv.Draw ();
  2211. var expected =
  2212. @"
  2213. ┌─┬───────┬─────────────┐
  2214. │ │Name │Kind │
  2215. ├─┼───────┼─────────────┤
  2216. │☑│Tammy │Cat │
  2217. │☑│Tibbles│Cat │
  2218. │☑│Ripper │Dog │";
  2219. TestHelpers.AssertDriverContentsAre (expected, output);
  2220. tv.NewKeyDownEvent (Key.Space);
  2221. #pragma warning disable xUnit2029
  2222. Assert.Empty (pets.Where (p => p.IsPicked));
  2223. #pragma warning restore xUnit2029
  2224. tv.Draw ();
  2225. expected =
  2226. @"
  2227. ┌─┬───────┬─────────────┐
  2228. │ │Name │Kind │
  2229. ├─┼───────┼─────────────┤
  2230. │☐│Tammy │Cat │
  2231. │☐│Tibbles│Cat │
  2232. │☐│Ripper │Dog │
  2233. ";
  2234. TestHelpers.AssertDriverContentsAre (expected, output);
  2235. }
  2236. [Fact]
  2237. [SetupFakeDriver]
  2238. public void TestTableViewCheckboxes_Simple ()
  2239. {
  2240. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  2241. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2242. tv.LayoutSubviews ();
  2243. var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
  2244. tv.Table = wrapper;
  2245. tv.Draw ();
  2246. var expected =
  2247. @"
  2248. │ │A│B│
  2249. ├─┼─┼─►
  2250. │☐│1│2│
  2251. │☐│1│2│
  2252. │☐│1│2│";
  2253. TestHelpers.AssertDriverContentsAre (expected, output);
  2254. Assert.Empty (wrapper.CheckedRows);
  2255. //toggle the top cell
  2256. tv.NewKeyDownEvent (Key.Space);
  2257. Assert.Single (wrapper.CheckedRows, 0);
  2258. tv.Draw ();
  2259. expected =
  2260. @"
  2261. │ │A│B│
  2262. ├─┼─┼─►
  2263. │☑│1│2│
  2264. │☐│1│2│
  2265. │☐│1│2│";
  2266. TestHelpers.AssertDriverContentsAre (expected, output);
  2267. tv.NewKeyDownEvent (Key.CursorDown);
  2268. tv.NewKeyDownEvent (Key.Space);
  2269. Assert.Contains (0, wrapper.CheckedRows);
  2270. Assert.Contains (1, wrapper.CheckedRows);
  2271. Assert.Equal (2, wrapper.CheckedRows.Count);
  2272. tv.Draw ();
  2273. expected =
  2274. @"
  2275. │ │A│B│
  2276. ├─┼─┼─►
  2277. │☑│1│2│
  2278. │☑│1│2│
  2279. │☐│1│2│";
  2280. TestHelpers.AssertDriverContentsAre (expected, output);
  2281. // untoggle top one
  2282. tv.NewKeyDownEvent (Key.CursorUp);
  2283. tv.NewKeyDownEvent (Key.Space);
  2284. Assert.Single (wrapper.CheckedRows, 1);
  2285. tv.Draw ();
  2286. expected =
  2287. @"
  2288. │ │A│B│
  2289. ├─┼─┼─►
  2290. │☐│1│2│
  2291. │☑│1│2│
  2292. │☐│1│2│";
  2293. TestHelpers.AssertDriverContentsAre (expected, output);
  2294. }
  2295. [Fact]
  2296. [SetupFakeDriver]
  2297. public void TestTableViewRadioBoxes_Simple_ByObject ()
  2298. {
  2299. TableView tv = GetPetTable (out EnumerableTableSource<PickablePet> source);
  2300. tv.LayoutSubviews ();
  2301. IReadOnlyCollection<PickablePet> pets = source.Data;
  2302. CheckBoxTableSourceWrapperByObject<PickablePet> wrapper = new (
  2303. tv,
  2304. source,
  2305. p => p.IsPicked,
  2306. (p, b) => p.IsPicked = b
  2307. );
  2308. wrapper.UseRadioButtons = true;
  2309. tv.Table = wrapper;
  2310. tv.Draw ();
  2311. var expected =
  2312. @"
  2313. ┌─┬───────┬─────────────┐
  2314. │ │Name │Kind │
  2315. ├─┼───────┼─────────────┤
  2316. │○│Tammy │Cat │
  2317. │○│Tibbles│Cat │
  2318. │○│Ripper │Dog │
  2319. ";
  2320. TestHelpers.AssertDriverContentsAre (expected, output);
  2321. #pragma warning disable xUnit2029
  2322. Assert.Empty (pets.Where (p => p.IsPicked));
  2323. #pragma warning restore xUnit2029
  2324. tv.NewKeyDownEvent (Key.Space);
  2325. Assert.True (pets.First ().IsPicked);
  2326. tv.Draw ();
  2327. expected =
  2328. @"
  2329. ┌─┬───────┬─────────────┐
  2330. │ │Name │Kind │
  2331. ├─┼───────┼─────────────┤
  2332. │◉│Tammy │Cat │
  2333. │○│Tibbles│Cat │
  2334. │○│Ripper │Dog │";
  2335. TestHelpers.AssertDriverContentsAre (expected, output);
  2336. tv.NewKeyDownEvent (Key.CursorDown);
  2337. tv.NewKeyDownEvent (Key.Space);
  2338. Assert.False (pets.ElementAt (0).IsPicked);
  2339. Assert.True (pets.ElementAt (1).IsPicked);
  2340. Assert.False (pets.ElementAt (2).IsPicked);
  2341. tv.Draw ();
  2342. expected =
  2343. @"
  2344. ┌─┬───────┬─────────────┐
  2345. │ │Name │Kind │
  2346. ├─┼───────┼─────────────┤
  2347. │○│Tammy │Cat │
  2348. │◉│Tibbles│Cat │
  2349. │○│Ripper │Dog │";
  2350. TestHelpers.AssertDriverContentsAre (expected, output);
  2351. tv.NewKeyDownEvent (Key.CursorUp);
  2352. tv.NewKeyDownEvent (Key.Space);
  2353. Assert.True (pets.ElementAt (0).IsPicked);
  2354. Assert.False (pets.ElementAt (1).IsPicked);
  2355. Assert.False (pets.ElementAt (2).IsPicked);
  2356. tv.Draw ();
  2357. expected =
  2358. @"
  2359. ┌─┬───────┬─────────────┐
  2360. │ │Name │Kind │
  2361. ├─┼───────┼─────────────┤
  2362. │◉│Tammy │Cat │
  2363. │○│Tibbles│Cat │
  2364. │○│Ripper │Dog │";
  2365. TestHelpers.AssertDriverContentsAre (expected, output);
  2366. }
  2367. [Fact]
  2368. public void TestToggleCells_MultiSelectOn ()
  2369. {
  2370. // 2 row table
  2371. TableView tableView = GetABCDEFTableView (out DataTable dt);
  2372. tableView.LayoutSubviews ();
  2373. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2374. tableView.MultiSelect = true;
  2375. tableView.KeyBindings.ReplaceCommands (Key.Space, Command.Select);
  2376. Point selectedCell = tableView.GetAllSelectedCells ().Single ();
  2377. Assert.Equal (0, selectedCell.X);
  2378. Assert.Equal (0, selectedCell.Y);
  2379. // Go Right
  2380. tableView.NewKeyDownEvent (Key.CursorRight);
  2381. selectedCell = tableView.GetAllSelectedCells ().Single ();
  2382. Assert.Equal (1, selectedCell.X);
  2383. Assert.Equal (0, selectedCell.Y);
  2384. // Toggle Select
  2385. tableView.NewKeyDownEvent (Key.Space);
  2386. TableSelection m = tableView.MultiSelectedRegions.Single ();
  2387. Assert.True (m.IsToggled);
  2388. Assert.Equal (1, m.Origin.X);
  2389. Assert.Equal (0, m.Origin.Y);
  2390. selectedCell = tableView.GetAllSelectedCells ().Single ();
  2391. Assert.Equal (1, selectedCell.X);
  2392. Assert.Equal (0, selectedCell.Y);
  2393. // Go Left
  2394. tableView.NewKeyDownEvent (Key.CursorLeft);
  2395. // Both Toggled and Moved to should be selected
  2396. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  2397. Point s1 = tableView.GetAllSelectedCells ().ElementAt (0);
  2398. Point s2 = tableView.GetAllSelectedCells ().ElementAt (1);
  2399. Assert.Equal (1, s1.X);
  2400. Assert.Equal (0, s1.Y);
  2401. Assert.Equal (0, s2.X);
  2402. Assert.Equal (0, s2.Y);
  2403. // Go Down
  2404. tableView.NewKeyDownEvent (Key.CursorDown);
  2405. // Both Toggled and Moved to should be selected but not 0,0
  2406. // which we moved down from
  2407. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  2408. s1 = tableView.GetAllSelectedCells ().ElementAt (0);
  2409. s2 = tableView.GetAllSelectedCells ().ElementAt (1);
  2410. Assert.Equal (1, s1.X);
  2411. Assert.Equal (0, s1.Y);
  2412. Assert.Equal (0, s2.X);
  2413. Assert.Equal (1, s2.Y);
  2414. // Go back to the toggled cell
  2415. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  2416. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorUp });
  2417. // Toggle off
  2418. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.Space });
  2419. // Go Left
  2420. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorLeft });
  2421. selectedCell = tableView.GetAllSelectedCells ().Single ();
  2422. Assert.Equal (0, selectedCell.X);
  2423. Assert.Equal (0, selectedCell.Y);
  2424. }
  2425. [Fact]
  2426. public void TestToggleCells_MultiSelectOn_FullRowSelect ()
  2427. {
  2428. // 2 row table
  2429. TableView tableView = GetABCDEFTableView (out DataTable dt);
  2430. tableView.LayoutSubviews ();
  2431. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2432. tableView.FullRowSelect = true;
  2433. tableView.MultiSelect = true;
  2434. tableView.KeyBindings.ReplaceCommands (Key.Space, Command.Select);
  2435. // Toggle Select Cell 0,0
  2436. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.Space });
  2437. // Go Down
  2438. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorDown });
  2439. TableSelection m = tableView.MultiSelectedRegions.Single ();
  2440. Assert.True (m.IsToggled);
  2441. Assert.Equal (0, m.Origin.X);
  2442. Assert.Equal (0, m.Origin.Y);
  2443. //First row toggled and Second row active = 12 selected cells
  2444. Assert.Equal (12, tableView.GetAllSelectedCells ().Count ());
  2445. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  2446. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorUp });
  2447. Assert.Single (tableView.MultiSelectedRegions.Where (r => r.IsToggled));
  2448. // Can untoggle at 1,0 even though 0,0 was initial toggle because FullRowSelect is on
  2449. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.Space });
  2450. #pragma warning disable xUnit2029
  2451. Assert.Empty (tableView.MultiSelectedRegions.Where (r => r.IsToggled));
  2452. #pragma warning restore xUnit2029
  2453. }
  2454. [Fact]
  2455. public void TestToggleCells_MultiSelectOn_SquareSelectToggled ()
  2456. {
  2457. // 3 row table
  2458. TableView tableView = GetABCDEFTableView (out DataTable dt);
  2459. tableView.LayoutSubviews ();
  2460. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2461. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2462. tableView.MultiSelect = true;
  2463. tableView.KeyBindings.ReplaceCommands (Key.Space, Command.Select);
  2464. // Make a square selection
  2465. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorDown });
  2466. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorRight });
  2467. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  2468. // Toggle the square selected region on
  2469. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.Space });
  2470. // Go Right
  2471. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorRight });
  2472. //Toggled on square + the active cell (x=2,y=1)
  2473. Assert.Equal (5, tableView.GetAllSelectedCells ().Count ());
  2474. Assert.Equal (2, tableView.SelectedColumn);
  2475. Assert.Equal (1, tableView.SelectedRow);
  2476. // Untoggle the rectangular region by hitting toggle in
  2477. // any cell in that rect
  2478. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorUp });
  2479. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorLeft });
  2480. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  2481. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.Space });
  2482. Assert.Single (tableView.GetAllSelectedCells ());
  2483. }
  2484. [Fact]
  2485. public void TestToggleCells_MultiSelectOn_Two_SquareSelects_BothToggled ()
  2486. {
  2487. // 6 row table
  2488. TableView tableView = GetABCDEFTableView (out DataTable dt);
  2489. tableView.LayoutSubviews ();
  2490. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2491. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2492. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2493. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2494. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2495. tableView.MultiSelect = true;
  2496. tableView.KeyBindings.ReplaceCommands (Key.Space, Command.Select);
  2497. // Make first square selection (0,0 to 1,1)
  2498. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorDown });
  2499. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorRight });
  2500. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.Space });
  2501. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  2502. // Make second square selection leaving 1 unselected line between them
  2503. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorLeft });
  2504. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorDown });
  2505. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.CursorDown });
  2506. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorDown });
  2507. tableView.NewKeyDownEvent (new () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorRight });
  2508. // 2 square selections
  2509. Assert.Equal (8, tableView.GetAllSelectedCells ().Count ());
  2510. }
  2511. [Fact]
  2512. public void TestDataColumnCaption ()
  2513. {
  2514. var tableView = new TableView ();
  2515. var dt = new DataTable ();
  2516. dt.Columns.Add (new DataColumn ()
  2517. {
  2518. Caption = "Caption 1",
  2519. ColumnName = "Column Name 1"
  2520. });
  2521. dt.Columns.Add (new DataColumn ()
  2522. {
  2523. ColumnName = "Column Name 2"
  2524. });
  2525. var dts = new DataTableSource (dt);
  2526. var cn = dts.ColumnNames;
  2527. Assert.Equal ("Caption 1", cn [0]);
  2528. Assert.Equal ("Column Name 2", cn [1]);
  2529. }
  2530. [Fact]
  2531. public void CanTabOutOfTableViewUsingCursor_Left ()
  2532. {
  2533. GetTableViewWithSiblings (out var tf1, out var tableView, out var tf2);
  2534. // Make the selected cell one in
  2535. tableView.SelectedColumn = 1;
  2536. // Pressing left should move us to the first column without changing focus
  2537. Application.RaiseKeyDownEvent (Key.CursorLeft);
  2538. Assert.Same (tableView, Application.Top!.MostFocused);
  2539. Assert.True (tableView.HasFocus);
  2540. // Because we are now on the leftmost cell a further left press should move focus
  2541. Application.RaiseKeyDownEvent (Key.CursorLeft);
  2542. Assert.NotSame (tableView, Application.Top.MostFocused);
  2543. Assert.False (tableView.HasFocus);
  2544. Assert.Same (tf1, Application.Top.MostFocused);
  2545. Assert.True (tf1.HasFocus);
  2546. Application.Top.Dispose ();
  2547. }
  2548. [Fact]
  2549. public void CanTabOutOfTableViewUsingCursor_Up ()
  2550. {
  2551. GetTableViewWithSiblings (out var tf1, out var tableView, out var tf2);
  2552. // Make the selected cell one in
  2553. tableView.SelectedRow = 1;
  2554. // First press should move us up
  2555. Application.RaiseKeyDownEvent (Key.CursorUp);
  2556. Assert.Same (tableView, Application.Top!.MostFocused);
  2557. Assert.True (tableView.HasFocus);
  2558. // Because we are now on the top row a further press should move focus
  2559. Application.RaiseKeyDownEvent (Key.CursorUp);
  2560. Assert.NotSame (tableView, Application.Top.MostFocused);
  2561. Assert.False (tableView.HasFocus);
  2562. Assert.Same (tf1, Application.Top.MostFocused);
  2563. Assert.True (tf1.HasFocus);
  2564. Application.Top.Dispose ();
  2565. }
  2566. [Fact]
  2567. public void CanTabOutOfTableViewUsingCursor_Right ()
  2568. {
  2569. GetTableViewWithSiblings (out var tf1, out var tableView, out var tf2);
  2570. // Make the selected cell one in from the rightmost column
  2571. tableView.SelectedColumn = tableView.Table.Columns - 2;
  2572. // First press should move us to the rightmost column without changing focus
  2573. Application.RaiseKeyDownEvent (Key.CursorRight);
  2574. Assert.Same (tableView, Application.Top!.MostFocused);
  2575. Assert.True (tableView.HasFocus);
  2576. // Because we are now on the rightmost cell, a further right press should move focus
  2577. Application.RaiseKeyDownEvent (Key.CursorRight);
  2578. Assert.NotSame (tableView, Application.Top.MostFocused);
  2579. Assert.False (tableView.HasFocus);
  2580. Assert.Same (tf2, Application.Top.MostFocused);
  2581. Assert.True (tf2.HasFocus);
  2582. Application.Top.Dispose ();
  2583. }
  2584. [Fact]
  2585. public void CanTabOutOfTableViewUsingCursor_Down ()
  2586. {
  2587. GetTableViewWithSiblings (out var tf1, out var tableView, out var tf2);
  2588. // Make the selected cell one in from the bottommost row
  2589. tableView.SelectedRow = tableView.Table.Rows - 2;
  2590. // First press should move us to the bottommost row without changing focus
  2591. Application.RaiseKeyDownEvent (Key.CursorDown);
  2592. Assert.Same (tableView, Application.Top!.MostFocused);
  2593. Assert.True (tableView.HasFocus);
  2594. // Because we are now on the bottommost cell, a further down press should move focus
  2595. Application.RaiseKeyDownEvent (Key.CursorDown);
  2596. Assert.NotSame (tableView, Application.Top.MostFocused);
  2597. Assert.False (tableView.HasFocus);
  2598. Assert.Same (tf2, Application.Top.MostFocused);
  2599. Assert.True (tf2.HasFocus);
  2600. Application.Top.Dispose ();
  2601. }
  2602. [Fact]
  2603. public void CanTabOutOfTableViewUsingCursor_Left_ClearsSelectionFirst ()
  2604. {
  2605. GetTableViewWithSiblings (out var tf1, out var tableView, out var tf2);
  2606. // Make the selected cell one in
  2607. tableView.SelectedColumn = 1;
  2608. // Pressing shift-left should give us a multi selection
  2609. Application.RaiseKeyDownEvent (Key.CursorLeft.WithShift);
  2610. Assert.Same (tableView, Application.Top!.MostFocused);
  2611. Assert.True (tableView.HasFocus);
  2612. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  2613. // Because we are now on the leftmost cell a further left press would normally move focus
  2614. // However there is an ongoing selection so instead the operation clears the selection and
  2615. // gets swallowed (not resulting in a focus change)
  2616. Application.RaiseKeyDownEvent (Key.CursorLeft);
  2617. // Selection 'clears' just to the single cell and we remain focused
  2618. Assert.Single (tableView.GetAllSelectedCells ());
  2619. Assert.Same (tableView, Application.Top.MostFocused);
  2620. Assert.True (tableView.HasFocus);
  2621. // A further left will switch focus
  2622. Application.RaiseKeyDownEvent (Key.CursorLeft);
  2623. Assert.NotSame (tableView, Application.Top.MostFocused);
  2624. Assert.False (tableView.HasFocus);
  2625. Assert.Same (tf1, Application.Top.MostFocused);
  2626. Assert.True (tf1.HasFocus);
  2627. Application.Top.Dispose ();
  2628. }
  2629. /// <summary>
  2630. /// Creates 3 views on <see cref="Application.Current"/> with the focus in the
  2631. /// <see cref="TableView"/>. This is a helper method to setup tests that want to
  2632. /// explore moving input focus out of a tableview.
  2633. /// </summary>
  2634. /// <param name="tv"></param>
  2635. /// <param name="tf1"></param>
  2636. /// <param name="tf2"></param>
  2637. private void GetTableViewWithSiblings (out TextField tf1, out TableView tableView, out TextField tf2)
  2638. {
  2639. tableView = new TableView ();
  2640. tableView.BeginInit ();
  2641. tableView.EndInit ();
  2642. Application.Navigation = new ();
  2643. Application.Top = new ();
  2644. tf1 = new TextField ();
  2645. tf2 = new TextField ();
  2646. Application.Top.Add (tf1);
  2647. Application.Top.Add (tableView);
  2648. Application.Top.Add (tf2);
  2649. tableView.SetFocus ();
  2650. Assert.Same (tableView, Application.Top.MostFocused);
  2651. Assert.True (tableView.HasFocus);
  2652. // Set big table
  2653. tableView.Table = BuildTable (25, 50);
  2654. }
  2655. private TableView GetABCDEFTableView (out DataTable dt)
  2656. {
  2657. var tableView = new TableView ();
  2658. tableView.BeginInit ();
  2659. tableView.EndInit ();
  2660. tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  2661. // 3 columns are visible
  2662. tableView.Viewport = new (0, 0, 7, 5);
  2663. tableView.Style.ShowHorizontalHeaderUnderline = false;
  2664. tableView.Style.ShowHorizontalHeaderOverline = false;
  2665. tableView.Style.AlwaysShowHeaders = true;
  2666. tableView.Style.SmoothHorizontalScrolling = false;
  2667. dt = new ();
  2668. dt.Columns.Add ("A");
  2669. dt.Columns.Add ("B");
  2670. dt.Columns.Add ("C");
  2671. dt.Columns.Add ("D");
  2672. dt.Columns.Add ("E");
  2673. dt.Columns.Add ("F");
  2674. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2675. tableView.Table = new DataTableSource (dt);
  2676. return tableView;
  2677. }
  2678. private TableView GetPetTable (out EnumerableTableSource<PickablePet> source)
  2679. {
  2680. var tv = new TableView ();
  2681. tv.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  2682. tv.Viewport = new (0, 0, 25, 6);
  2683. List<PickablePet> pets = new ()
  2684. {
  2685. new (false, "Tammy", "Cat"),
  2686. new (false, "Tibbles", "Cat"),
  2687. new (false, "Ripper", "Dog")
  2688. };
  2689. tv.Table = source = new (
  2690. pets,
  2691. new ()
  2692. {
  2693. { "Name", p => p.Name }, { "Kind", p => p.Kind }
  2694. }
  2695. );
  2696. tv.LayoutSubviews ();
  2697. return tv;
  2698. }
  2699. private TableView GetTwoRowSixColumnTable () { return GetTwoRowSixColumnTable (out _); }
  2700. private TableView GetTwoRowSixColumnTable (out DataTable dt)
  2701. {
  2702. var tableView = new TableView ();
  2703. tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  2704. // 3 columns are visible
  2705. tableView.Viewport = new (0, 0, 7, 5);
  2706. tableView.Style.ShowHorizontalHeaderUnderline = true;
  2707. tableView.Style.ShowHorizontalHeaderOverline = false;
  2708. tableView.Style.AlwaysShowHeaders = true;
  2709. tableView.Style.SmoothHorizontalScrolling = true;
  2710. dt = new ();
  2711. dt.Columns.Add ("A");
  2712. dt.Columns.Add ("B");
  2713. dt.Columns.Add ("C");
  2714. dt.Columns.Add ("D");
  2715. dt.Columns.Add ("E");
  2716. dt.Columns.Add ("F");
  2717. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2718. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2719. tableView.Table = new DataTableSource (dt);
  2720. return tableView;
  2721. }
  2722. private TableView SetUpMiniTable () { return SetUpMiniTable (out _); }
  2723. private TableView SetUpMiniTable (out DataTable dt)
  2724. {
  2725. var tv = new TableView ();
  2726. tv.BeginInit ();
  2727. tv.EndInit ();
  2728. tv.Viewport = new (0, 0, 10, 4);
  2729. dt = new ();
  2730. dt.Columns.Add ("A");
  2731. dt.Columns.Add ("B");
  2732. dt.Rows.Add (1, 2);
  2733. tv.Table = new DataTableSource (dt);
  2734. tv.Style.GetOrCreateColumnStyle (0).MinWidth = 1;
  2735. tv.Style.GetOrCreateColumnStyle (0).MinWidth = 1;
  2736. tv.Style.GetOrCreateColumnStyle (1).MaxWidth = 1;
  2737. tv.Style.GetOrCreateColumnStyle (1).MaxWidth = 1;
  2738. tv.ColorScheme = Colors.ColorSchemes ["Base"];
  2739. return tv;
  2740. }
  2741. private class PickablePet
  2742. {
  2743. public PickablePet (bool isPicked, string name, string kind)
  2744. {
  2745. IsPicked = isPicked;
  2746. Name = name;
  2747. Kind = kind;
  2748. }
  2749. public bool IsPicked { get; set; }
  2750. public string Kind { get; }
  2751. public string Name { get; }
  2752. }
  2753. }