TableViewTests.cs 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313
  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, Bounds = 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, Bounds = 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.Bounds = 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, Bounds = 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, Bounds = 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, Bounds = 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, Bounds = 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.Bounds = 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.Bounds = 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.Bounds = 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.Bounds = 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, Bounds = 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 (new Key (KeyCode.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 (new Key (KeyCode.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.Bounds = 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.Bounds = 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.Bounds = 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.Bounds = 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.Bounds = 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.Bounds = 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.Bounds = 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 (new Key (KeyCode.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 (new Key (KeyCode.Enter));
  794. Assert.Null (activatedValue);
  795. // New method for changing the activation key
  796. tv.KeyBindings.Add (KeyCode.Z, Command.Accept);
  797. tv.NewKeyDownEvent (new Key (KeyCode.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 (new Key (KeyCode.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.Bounds = 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.Bounds = 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.Bounds = 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.Bounds = 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.Bounds = 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.Bounds = 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.Bounds = 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.Bounds = 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.Bounds = 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 (new Key (KeyCode.CursorRight | KeyCode.ShiftMask));
  1174. tv.NewKeyDownEvent (new Key (KeyCode.CursorDown | KeyCode.ShiftMask));
  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 (new Key (KeyCode.CursorRight | KeyCode.ShiftMask));
  1178. tv.NewKeyDownEvent (new Key (KeyCode.CursorDown | KeyCode.ShiftMask));
  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 (new Key (KeyCode.CursorLeft | KeyCode.ShiftMask));
  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 (new Key (KeyCode.CursorLeft | KeyCode.ShiftMask));
  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 (new Key (KeyCode.CursorRight | KeyCode.ShiftMask));
  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 (new Key (KeyCode.CursorRight | KeyCode.ShiftMask));
  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 (new Key (KeyCode.CursorLeft | KeyCode.ShiftMask));
  1231. tv.NewKeyDownEvent (new Key (KeyCode.CursorUp | KeyCode.ShiftMask));
  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 (new Key (KeyCode.CursorLeft | KeyCode.ShiftMask));
  1235. tv.NewKeyDownEvent (new Key (KeyCode.CursorUp | KeyCode.ShiftMask));
  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.Bounds = 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 (new Point (0, 0), 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 (new Point (0, 0), 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 (new Point (0, 0), 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 (new Point (0, 0), 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. new Point (0, 0),
  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 (new Key { KeyCode = KeyCode.CursorRight | KeyCode.ShiftMask });
  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.MouseEvent (
  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.MouseEvent (
  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 (new Point (0, 0), 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.Bounds = 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.Bounds = 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.MouseEvent (
  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.Bounds);
  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.Bounds = 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.MouseEvent (
  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.Bounds = 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.MouseEvent (
  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.Bounds);
  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.Bounds = 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 (
  1973. new Key { KeyCode = KeyCode.Home | KeyCode.CtrlMask }
  1974. );
  1975. if (withFullRowSelect)
  1976. {
  1977. // Should not be any horizontal movement when
  1978. // using navigate to Start/End and FullRowSelect
  1979. Assert.Equal (1, tableView.SelectedColumn);
  1980. Assert.Equal (0, tableView.SelectedRow);
  1981. }
  1982. else
  1983. {
  1984. Assert.Equal (0, tableView.SelectedColumn);
  1985. Assert.Equal (0, tableView.SelectedRow);
  1986. }
  1987. tableView.NewKeyDownEvent (
  1988. new Key (
  1989. KeyCode.End | KeyCode.CtrlMask
  1990. )
  1991. );
  1992. if (withFullRowSelect)
  1993. {
  1994. Assert.Equal (1, tableView.SelectedColumn);
  1995. Assert.Equal (1, tableView.SelectedRow);
  1996. }
  1997. else
  1998. {
  1999. Assert.Equal (5, tableView.SelectedColumn);
  2000. Assert.Equal (1, tableView.SelectedRow);
  2001. }
  2002. }
  2003. [Fact]
  2004. [AutoInitShutdown]
  2005. public void TestShiftClick_MultiSelect_TwoRowTable_FullRowSelect ()
  2006. {
  2007. TableView tv = GetTwoRowSixColumnTable ();
  2008. tv.LayoutSubviews ();
  2009. tv.MultiSelect = true;
  2010. // Clicking in bottom row
  2011. tv.MouseEvent (
  2012. new MouseEvent { X = 1, Y = 3, Flags = MouseFlags.Button1Clicked }
  2013. );
  2014. // should select that row
  2015. Assert.Equal (1, tv.SelectedRow);
  2016. // shift clicking top row
  2017. tv.MouseEvent (
  2018. new MouseEvent { X = 1, Y = 2, Flags = MouseFlags.Button1Clicked | MouseFlags.ButtonShift }
  2019. );
  2020. // should extend the selection
  2021. Assert.Equal (0, tv.SelectedRow);
  2022. Point [] selected = tv.GetAllSelectedCells ().ToArray ();
  2023. Assert.Contains (new Point (0, 0), selected);
  2024. Assert.Contains (new Point (0, 1), selected);
  2025. }
  2026. [Fact]
  2027. [AutoInitShutdown]
  2028. public void TestTableViewCheckboxes_ByObject ()
  2029. {
  2030. TableView tv = GetPetTable (out EnumerableTableSource<PickablePet> source);
  2031. tv.LayoutSubviews ();
  2032. IReadOnlyCollection<PickablePet> pets = source.Data;
  2033. CheckBoxTableSourceWrapperByObject<PickablePet> wrapper = new (
  2034. tv,
  2035. source,
  2036. p => p.IsPicked,
  2037. (p, b) => p.IsPicked = b
  2038. );
  2039. tv.Table = wrapper;
  2040. tv.Draw ();
  2041. var expected =
  2042. @"
  2043. ┌─┬───────┬─────────────┐
  2044. │ │Name │Kind │
  2045. ├─┼───────┼─────────────┤
  2046. │☐│Tammy │Cat │
  2047. │☐│Tibbles│Cat │
  2048. │☐│Ripper │Dog │";
  2049. TestHelpers.AssertDriverContentsAre (expected, output);
  2050. Assert.Empty (pets.Where (p => p.IsPicked));
  2051. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2052. Assert.True (pets.First ().IsPicked);
  2053. tv.Draw ();
  2054. expected =
  2055. @"
  2056. ┌─┬───────┬─────────────┐
  2057. │ │Name │Kind │
  2058. ├─┼───────┼─────────────┤
  2059. │☑│Tammy │Cat │
  2060. │☐│Tibbles│Cat │
  2061. │☐│Ripper │Dog │";
  2062. TestHelpers.AssertDriverContentsAre (expected, output);
  2063. tv.NewKeyDownEvent (new Key (KeyCode.CursorDown));
  2064. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2065. Assert.True (pets.ElementAt (0).IsPicked);
  2066. Assert.True (pets.ElementAt (1).IsPicked);
  2067. Assert.False (pets.ElementAt (2).IsPicked);
  2068. tv.Draw ();
  2069. expected =
  2070. @"
  2071. ┌─┬───────┬─────────────┐
  2072. │ │Name │Kind │
  2073. ├─┼───────┼─────────────┤
  2074. │☑│Tammy │Cat │
  2075. │☑│Tibbles│Cat │
  2076. │☐│Ripper │Dog │";
  2077. TestHelpers.AssertDriverContentsAre (expected, output);
  2078. tv.NewKeyDownEvent (new Key (KeyCode.CursorUp));
  2079. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2080. Assert.False (pets.ElementAt (0).IsPicked);
  2081. Assert.True (pets.ElementAt (1).IsPicked);
  2082. Assert.False (pets.ElementAt (2).IsPicked);
  2083. tv.Draw ();
  2084. expected =
  2085. @"
  2086. ┌─┬───────┬─────────────┐
  2087. │ │Name │Kind │
  2088. ├─┼───────┼─────────────┤
  2089. │☐│Tammy │Cat │
  2090. │☑│Tibbles│Cat │
  2091. │☐│Ripper │Dog │";
  2092. TestHelpers.AssertDriverContentsAre (expected, output);
  2093. }
  2094. [Fact]
  2095. [AutoInitShutdown]
  2096. public void TestTableViewCheckboxes_MultiSelectIsUnion_WhenToggling ()
  2097. {
  2098. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  2099. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2100. tv.LayoutSubviews ();
  2101. var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
  2102. tv.Table = wrapper;
  2103. wrapper.CheckedRows.Add (0);
  2104. wrapper.CheckedRows.Add (2);
  2105. tv.Draw ();
  2106. var expected =
  2107. @"
  2108. │ │A│B│
  2109. ├─┼─┼─►
  2110. │☑│1│2│
  2111. │☐│1│2│
  2112. │☑│1│2│";
  2113. //toggle top two at once
  2114. tv.NewKeyDownEvent (new Key (KeyCode.CursorDown | KeyCode.ShiftMask));
  2115. Assert.True (tv.IsSelected (0, 0));
  2116. Assert.True (tv.IsSelected (0, 1));
  2117. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2118. // Because at least 1 of the rows is not yet ticked we toggle them all to ticked
  2119. TestHelpers.AssertDriverContentsAre (expected, output);
  2120. Assert.Contains (0, wrapper.CheckedRows);
  2121. Assert.Contains (1, wrapper.CheckedRows);
  2122. Assert.Contains (2, wrapper.CheckedRows);
  2123. Assert.Equal (3, wrapper.CheckedRows.Count);
  2124. tv.Draw ();
  2125. expected =
  2126. @"
  2127. │ │A│B│
  2128. ├─┼─┼─►
  2129. │☑│1│2│
  2130. │☑│1│2│
  2131. │☑│1│2│";
  2132. TestHelpers.AssertDriverContentsAre (expected, output);
  2133. // Untoggle the top 2
  2134. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2135. tv.Draw ();
  2136. expected =
  2137. @"
  2138. │ │A│B│
  2139. ├─┼─┼─►
  2140. │☐│1│2│
  2141. │☐│1│2│
  2142. │☑│1│2│";
  2143. TestHelpers.AssertDriverContentsAre (expected, output);
  2144. Assert.Single (wrapper.CheckedRows, 2);
  2145. }
  2146. [Fact]
  2147. [AutoInitShutdown]
  2148. public void TestTableViewCheckboxes_SelectAllToggle ()
  2149. {
  2150. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  2151. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2152. tv.LayoutSubviews ();
  2153. var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
  2154. tv.Table = wrapper;
  2155. //toggle all cells
  2156. tv.NewKeyDownEvent (new Key (KeyCode.A | KeyCode.CtrlMask));
  2157. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2158. tv.Draw ();
  2159. var expected =
  2160. @"
  2161. │ │A│B│
  2162. ├─┼─┼─►
  2163. │☑│1│2│
  2164. │☑│1│2│
  2165. │☑│1│2│";
  2166. TestHelpers.AssertDriverContentsAre (expected, output);
  2167. Assert.Contains (0, wrapper.CheckedRows);
  2168. Assert.Contains (1, wrapper.CheckedRows);
  2169. Assert.Contains (2, wrapper.CheckedRows);
  2170. Assert.Equal (3, wrapper.CheckedRows.Count);
  2171. // Untoggle all again
  2172. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2173. tv.Draw ();
  2174. expected =
  2175. @"
  2176. │ │A│B│
  2177. ├─┼─┼─►
  2178. │☐│1│2│
  2179. │☐│1│2│
  2180. │☐│1│2│";
  2181. TestHelpers.AssertDriverContentsAre (expected, output);
  2182. Assert.Empty (wrapper.CheckedRows);
  2183. }
  2184. [Fact]
  2185. [AutoInitShutdown]
  2186. public void TestTableViewCheckboxes_SelectAllToggle_ByObject ()
  2187. {
  2188. TableView tv = GetPetTable (out EnumerableTableSource<PickablePet> source);
  2189. tv.LayoutSubviews ();
  2190. IReadOnlyCollection<PickablePet> pets = source.Data;
  2191. CheckBoxTableSourceWrapperByObject<PickablePet> wrapper = new (
  2192. tv,
  2193. source,
  2194. p => p.IsPicked,
  2195. (p, b) => p.IsPicked = b
  2196. );
  2197. tv.Table = wrapper;
  2198. Assert.DoesNotContain (pets, p => p.IsPicked);
  2199. //toggle all cells
  2200. tv.NewKeyDownEvent (new Key (KeyCode.A | KeyCode.CtrlMask));
  2201. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2202. Assert.True (pets.All (p => p.IsPicked));
  2203. tv.Draw ();
  2204. var expected =
  2205. @"
  2206. ┌─┬───────┬─────────────┐
  2207. │ │Name │Kind │
  2208. ├─┼───────┼─────────────┤
  2209. │☑│Tammy │Cat │
  2210. │☑│Tibbles│Cat │
  2211. │☑│Ripper │Dog │";
  2212. TestHelpers.AssertDriverContentsAre (expected, output);
  2213. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2214. Assert.Empty (pets.Where (p => p.IsPicked));
  2215. tv.Draw ();
  2216. expected =
  2217. @"
  2218. ┌─┬───────┬─────────────┐
  2219. │ │Name │Kind │
  2220. ├─┼───────┼─────────────┤
  2221. │☐│Tammy │Cat │
  2222. │☐│Tibbles│Cat │
  2223. │☐│Ripper │Dog │
  2224. ";
  2225. TestHelpers.AssertDriverContentsAre (expected, output);
  2226. }
  2227. [Fact]
  2228. [AutoInitShutdown]
  2229. public void TestTableViewCheckboxes_Simple ()
  2230. {
  2231. TableView tv = GetTwoRowSixColumnTable (out DataTable dt);
  2232. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2233. tv.LayoutSubviews ();
  2234. var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
  2235. tv.Table = wrapper;
  2236. tv.Draw ();
  2237. var expected =
  2238. @"
  2239. │ │A│B│
  2240. ├─┼─┼─►
  2241. │☐│1│2│
  2242. │☐│1│2│
  2243. │☐│1│2│";
  2244. TestHelpers.AssertDriverContentsAre (expected, output);
  2245. Assert.Empty (wrapper.CheckedRows);
  2246. //toggle the top cell
  2247. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2248. Assert.Single (wrapper.CheckedRows, 0);
  2249. tv.Draw ();
  2250. expected =
  2251. @"
  2252. │ │A│B│
  2253. ├─┼─┼─►
  2254. │☑│1│2│
  2255. │☐│1│2│
  2256. │☐│1│2│";
  2257. TestHelpers.AssertDriverContentsAre (expected, output);
  2258. tv.NewKeyDownEvent (new Key (KeyCode.CursorDown));
  2259. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2260. Assert.Contains (0, wrapper.CheckedRows);
  2261. Assert.Contains (1, wrapper.CheckedRows);
  2262. Assert.Equal (2, wrapper.CheckedRows.Count);
  2263. tv.Draw ();
  2264. expected =
  2265. @"
  2266. │ │A│B│
  2267. ├─┼─┼─►
  2268. │☑│1│2│
  2269. │☑│1│2│
  2270. │☐│1│2│";
  2271. TestHelpers.AssertDriverContentsAre (expected, output);
  2272. // untoggle top one
  2273. tv.NewKeyDownEvent (new Key (KeyCode.CursorUp));
  2274. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2275. Assert.Single (wrapper.CheckedRows, 1);
  2276. tv.Draw ();
  2277. expected =
  2278. @"
  2279. │ │A│B│
  2280. ├─┼─┼─►
  2281. │☐│1│2│
  2282. │☑│1│2│
  2283. │☐│1│2│";
  2284. TestHelpers.AssertDriverContentsAre (expected, output);
  2285. }
  2286. [Fact]
  2287. [AutoInitShutdown]
  2288. public void TestTableViewRadioBoxes_Simple_ByObject ()
  2289. {
  2290. TableView tv = GetPetTable (out EnumerableTableSource<PickablePet> source);
  2291. tv.LayoutSubviews ();
  2292. IReadOnlyCollection<PickablePet> pets = source.Data;
  2293. CheckBoxTableSourceWrapperByObject<PickablePet> wrapper = new (
  2294. tv,
  2295. source,
  2296. p => p.IsPicked,
  2297. (p, b) => p.IsPicked = b
  2298. );
  2299. wrapper.UseRadioButtons = true;
  2300. tv.Table = wrapper;
  2301. tv.Draw ();
  2302. var expected =
  2303. @"
  2304. ┌─┬───────┬─────────────┐
  2305. │ │Name │Kind │
  2306. ├─┼───────┼─────────────┤
  2307. │○│Tammy │Cat │
  2308. │○│Tibbles│Cat │
  2309. │○│Ripper │Dog │
  2310. ";
  2311. TestHelpers.AssertDriverContentsAre (expected, output);
  2312. Assert.Empty (pets.Where (p => p.IsPicked));
  2313. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2314. Assert.True (pets.First ().IsPicked);
  2315. tv.Draw ();
  2316. expected =
  2317. @"
  2318. ┌─┬───────┬─────────────┐
  2319. │ │Name │Kind │
  2320. ├─┼───────┼─────────────┤
  2321. │◉│Tammy │Cat │
  2322. │○│Tibbles│Cat │
  2323. │○│Ripper │Dog │";
  2324. TestHelpers.AssertDriverContentsAre (expected, output);
  2325. tv.NewKeyDownEvent (new Key (KeyCode.CursorDown));
  2326. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2327. Assert.False (pets.ElementAt (0).IsPicked);
  2328. Assert.True (pets.ElementAt (1).IsPicked);
  2329. Assert.False (pets.ElementAt (2).IsPicked);
  2330. tv.Draw ();
  2331. expected =
  2332. @"
  2333. ┌─┬───────┬─────────────┐
  2334. │ │Name │Kind │
  2335. ├─┼───────┼─────────────┤
  2336. │○│Tammy │Cat │
  2337. │◉│Tibbles│Cat │
  2338. │○│Ripper │Dog │";
  2339. TestHelpers.AssertDriverContentsAre (expected, output);
  2340. tv.NewKeyDownEvent (new Key (KeyCode.CursorUp));
  2341. tv.NewKeyDownEvent (new Key (KeyCode.Space));
  2342. Assert.True (pets.ElementAt (0).IsPicked);
  2343. Assert.False (pets.ElementAt (1).IsPicked);
  2344. Assert.False (pets.ElementAt (2).IsPicked);
  2345. tv.Draw ();
  2346. expected =
  2347. @"
  2348. ┌─┬───────┬─────────────┐
  2349. │ │Name │Kind │
  2350. ├─┼───────┼─────────────┤
  2351. │◉│Tammy │Cat │
  2352. │○│Tibbles│Cat │
  2353. │○│Ripper │Dog │";
  2354. TestHelpers.AssertDriverContentsAre (expected, output);
  2355. }
  2356. [Fact]
  2357. [AutoInitShutdown]
  2358. public void TestToggleCells_MultiSelectOn ()
  2359. {
  2360. // 2 row table
  2361. TableView tableView = GetABCDEFTableView (out DataTable dt);
  2362. tableView.LayoutSubviews ();
  2363. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2364. tableView.MultiSelect = true;
  2365. tableView.KeyBindings.Add (KeyCode.Space, Command.ToggleChecked);
  2366. Point selectedCell = tableView.GetAllSelectedCells ().Single ();
  2367. Assert.Equal (0, selectedCell.X);
  2368. Assert.Equal (0, selectedCell.Y);
  2369. // Go Right
  2370. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorRight });
  2371. selectedCell = tableView.GetAllSelectedCells ().Single ();
  2372. Assert.Equal (1, selectedCell.X);
  2373. Assert.Equal (0, selectedCell.Y);
  2374. // Toggle Select
  2375. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.Space });
  2376. TableSelection m = tableView.MultiSelectedRegions.Single ();
  2377. Assert.True (m.IsToggled);
  2378. Assert.Equal (1, m.Origin.X);
  2379. Assert.Equal (0, m.Origin.Y);
  2380. selectedCell = tableView.GetAllSelectedCells ().Single ();
  2381. Assert.Equal (1, selectedCell.X);
  2382. Assert.Equal (0, selectedCell.Y);
  2383. // Go Left
  2384. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorLeft });
  2385. // Both Toggled and Moved to should be selected
  2386. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  2387. Point s1 = tableView.GetAllSelectedCells ().ElementAt (0);
  2388. Point s2 = tableView.GetAllSelectedCells ().ElementAt (1);
  2389. Assert.Equal (1, s1.X);
  2390. Assert.Equal (0, s1.Y);
  2391. Assert.Equal (0, s2.X);
  2392. Assert.Equal (0, s2.Y);
  2393. // Go Down
  2394. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorDown });
  2395. // Both Toggled and Moved to should be selected but not 0,0
  2396. // which we moved down from
  2397. Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
  2398. s1 = tableView.GetAllSelectedCells ().ElementAt (0);
  2399. s2 = tableView.GetAllSelectedCells ().ElementAt (1);
  2400. Assert.Equal (1, s1.X);
  2401. Assert.Equal (0, s1.Y);
  2402. Assert.Equal (0, s2.X);
  2403. Assert.Equal (1, s2.Y);
  2404. // Go back to the toggled cell
  2405. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorRight });
  2406. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorUp });
  2407. // Toggle off
  2408. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.Space });
  2409. // Go Left
  2410. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorLeft });
  2411. selectedCell = tableView.GetAllSelectedCells ().Single ();
  2412. Assert.Equal (0, selectedCell.X);
  2413. Assert.Equal (0, selectedCell.Y);
  2414. }
  2415. [Fact]
  2416. [AutoInitShutdown]
  2417. public void TestToggleCells_MultiSelectOn_FullRowSelect ()
  2418. {
  2419. // 2 row table
  2420. TableView tableView = GetABCDEFTableView (out DataTable dt);
  2421. tableView.LayoutSubviews ();
  2422. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2423. tableView.FullRowSelect = true;
  2424. tableView.MultiSelect = true;
  2425. tableView.KeyBindings.Add (KeyCode.Space, Command.ToggleChecked);
  2426. // Toggle Select Cell 0,0
  2427. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.Space });
  2428. // Go Down
  2429. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorDown });
  2430. TableSelection m = tableView.MultiSelectedRegions.Single ();
  2431. Assert.True (m.IsToggled);
  2432. Assert.Equal (0, m.Origin.X);
  2433. Assert.Equal (0, m.Origin.Y);
  2434. //First row toggled and Second row active = 12 selected cells
  2435. Assert.Equal (12, tableView.GetAllSelectedCells ().Count ());
  2436. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorRight });
  2437. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorUp });
  2438. Assert.Single (tableView.MultiSelectedRegions.Where (r => r.IsToggled));
  2439. // Can untoggle at 1,0 even though 0,0 was initial toggle because FullRowSelect is on
  2440. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.Space });
  2441. Assert.Empty (tableView.MultiSelectedRegions.Where (r => r.IsToggled));
  2442. }
  2443. [Fact]
  2444. [AutoInitShutdown]
  2445. public void TestToggleCells_MultiSelectOn_SquareSelectToggled ()
  2446. {
  2447. // 3 row table
  2448. TableView tableView = GetABCDEFTableView (out DataTable dt);
  2449. tableView.LayoutSubviews ();
  2450. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2451. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2452. tableView.MultiSelect = true;
  2453. tableView.KeyBindings.Add (KeyCode.Space, Command.ToggleChecked);
  2454. // Make a square selection
  2455. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.ShiftMask | KeyCode.CursorDown });
  2456. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.ShiftMask | KeyCode.CursorRight });
  2457. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  2458. // Toggle the square selected region on
  2459. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.Space });
  2460. // Go Right
  2461. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorRight });
  2462. //Toggled on square + the active cell (x=2,y=1)
  2463. Assert.Equal (5, tableView.GetAllSelectedCells ().Count ());
  2464. Assert.Equal (2, tableView.SelectedColumn);
  2465. Assert.Equal (1, tableView.SelectedRow);
  2466. // Untoggle the rectangular region by hitting toggle in
  2467. // any cell in that rect
  2468. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorUp });
  2469. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorLeft });
  2470. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  2471. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.Space });
  2472. Assert.Single (tableView.GetAllSelectedCells ());
  2473. }
  2474. [Fact]
  2475. [AutoInitShutdown]
  2476. public void TestToggleCells_MultiSelectOn_Two_SquareSelects_BothToggled ()
  2477. {
  2478. // 6 row table
  2479. TableView tableView = GetABCDEFTableView (out DataTable dt);
  2480. tableView.LayoutSubviews ();
  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. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2485. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2486. tableView.MultiSelect = true;
  2487. tableView.KeyBindings.Add (KeyCode.Space, Command.ToggleChecked);
  2488. // Make first square selection (0,0 to 1,1)
  2489. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.ShiftMask | KeyCode.CursorDown });
  2490. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.ShiftMask | KeyCode.CursorRight });
  2491. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.Space });
  2492. Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
  2493. // Make second square selection leaving 1 unselected line between them
  2494. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorLeft });
  2495. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorDown });
  2496. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.CursorDown });
  2497. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.ShiftMask | KeyCode.CursorDown });
  2498. tableView.NewKeyDownEvent (new Key { KeyCode = KeyCode.ShiftMask | KeyCode.CursorRight });
  2499. // 2 square selections
  2500. Assert.Equal (8, tableView.GetAllSelectedCells ().Count ());
  2501. }
  2502. private TableView GetABCDEFTableView (out DataTable dt)
  2503. {
  2504. var tableView = new TableView ();
  2505. tableView.BeginInit ();
  2506. tableView.EndInit ();
  2507. tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  2508. // 3 columns are visible
  2509. tableView.Bounds = new Rectangle (0, 0, 7, 5);
  2510. tableView.Style.ShowHorizontalHeaderUnderline = false;
  2511. tableView.Style.ShowHorizontalHeaderOverline = false;
  2512. tableView.Style.AlwaysShowHeaders = true;
  2513. tableView.Style.SmoothHorizontalScrolling = false;
  2514. dt = new DataTable ();
  2515. dt.Columns.Add ("A");
  2516. dt.Columns.Add ("B");
  2517. dt.Columns.Add ("C");
  2518. dt.Columns.Add ("D");
  2519. dt.Columns.Add ("E");
  2520. dt.Columns.Add ("F");
  2521. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2522. tableView.Table = new DataTableSource (dt);
  2523. return tableView;
  2524. }
  2525. private TableView GetPetTable (out EnumerableTableSource<PickablePet> source)
  2526. {
  2527. var tv = new TableView ();
  2528. tv.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  2529. tv.Bounds = new Rectangle (0, 0, 25, 6);
  2530. List<PickablePet> pets = new ()
  2531. {
  2532. new PickablePet (false, "Tammy", "Cat"),
  2533. new PickablePet (false, "Tibbles", "Cat"),
  2534. new PickablePet (false, "Ripper", "Dog")
  2535. };
  2536. tv.Table = source = new EnumerableTableSource<PickablePet> (
  2537. pets,
  2538. new Dictionary<string, Func<PickablePet, object>>
  2539. {
  2540. { "Name", p => p.Name }, { "Kind", p => p.Kind }
  2541. }
  2542. );
  2543. tv.LayoutSubviews ();
  2544. return tv;
  2545. }
  2546. private TableView GetTwoRowSixColumnTable () { return GetTwoRowSixColumnTable (out _); }
  2547. private TableView GetTwoRowSixColumnTable (out DataTable dt)
  2548. {
  2549. var tableView = new TableView ();
  2550. tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  2551. // 3 columns are visible
  2552. tableView.Bounds = new Rectangle (0, 0, 7, 5);
  2553. tableView.Style.ShowHorizontalHeaderUnderline = true;
  2554. tableView.Style.ShowHorizontalHeaderOverline = false;
  2555. tableView.Style.AlwaysShowHeaders = true;
  2556. tableView.Style.SmoothHorizontalScrolling = true;
  2557. dt = new DataTable ();
  2558. dt.Columns.Add ("A");
  2559. dt.Columns.Add ("B");
  2560. dt.Columns.Add ("C");
  2561. dt.Columns.Add ("D");
  2562. dt.Columns.Add ("E");
  2563. dt.Columns.Add ("F");
  2564. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2565. dt.Rows.Add (1, 2, 3, 4, 5, 6);
  2566. tableView.Table = new DataTableSource (dt);
  2567. return tableView;
  2568. }
  2569. private TableView SetUpMiniTable () { return SetUpMiniTable (out _); }
  2570. private TableView SetUpMiniTable (out DataTable dt)
  2571. {
  2572. var tv = new TableView ();
  2573. tv.BeginInit ();
  2574. tv.EndInit ();
  2575. tv.Bounds = new Rectangle (0, 0, 10, 4);
  2576. dt = new DataTable ();
  2577. dt.Columns.Add ("A");
  2578. dt.Columns.Add ("B");
  2579. dt.Rows.Add (1, 2);
  2580. tv.Table = new DataTableSource (dt);
  2581. tv.Style.GetOrCreateColumnStyle (0).MinWidth = 1;
  2582. tv.Style.GetOrCreateColumnStyle (0).MinWidth = 1;
  2583. tv.Style.GetOrCreateColumnStyle (1).MaxWidth = 1;
  2584. tv.Style.GetOrCreateColumnStyle (1).MaxWidth = 1;
  2585. tv.ColorScheme = Colors.ColorSchemes ["Base"];
  2586. return tv;
  2587. }
  2588. private class PickablePet
  2589. {
  2590. public PickablePet (bool isPicked, string name, string kind)
  2591. {
  2592. IsPicked = isPicked;
  2593. Name = name;
  2594. Kind = kind;
  2595. }
  2596. public bool IsPicked { get; set; }
  2597. public string Kind { get; }
  2598. public string Name { get; }
  2599. }
  2600. }