2
0

TableViewTests.cs 103 KB

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