TableViewTests.cs 103 KB

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