TableViewTests.cs 101 KB

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