TableViewTests.cs 112 KB

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