TableViewTests.cs 110 KB

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