1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Threading.Tasks;
- using Terminal.Gui;
- using Xunit;
- using System.Globalization;
- using Xunit.Abstractions;
- using System.Reflection;
- using Terminal.Gui.ViewTests;
- using System.Collections;
- using static Terminal.Gui.SpinnerStyle;
- using static System.Runtime.InteropServices.JavaScript.JSType;
- namespace Terminal.Gui.ViewsTests {
- public class TableViewTests {
- readonly ITestOutputHelper output;
- public TableViewTests (ITestOutputHelper output)
- {
- this.output = output;
- }
- [Fact]
- public void EnsureValidScrollOffsets_WithNoCells ()
- {
- var tableView = new TableView ();
- Assert.Equal (0, tableView.RowOffset);
- Assert.Equal (0, tableView.ColumnOffset);
- // Set empty table
- tableView.Table = new DataTableSource (new DataTable ());
- // Since table has no rows or columns scroll offset should default to 0
- tableView.EnsureValidScrollOffsets ();
- Assert.Equal (0, tableView.RowOffset);
- Assert.Equal (0, tableView.ColumnOffset);
- }
- [Fact]
- public void EnsureValidScrollOffsets_LoadSmallerTable ()
- {
- var tableView = new TableView ();
- tableView.BeginInit (); tableView.EndInit ();
- tableView.Bounds = new Rect (0, 0, 25, 10);
- Assert.Equal (0, tableView.RowOffset);
- Assert.Equal (0, tableView.ColumnOffset);
- // Set big table
- tableView.Table = BuildTable (25, 50);
- // Scroll down and along
- tableView.RowOffset = 20;
- tableView.ColumnOffset = 10;
- tableView.EnsureValidScrollOffsets ();
- // The scroll should be valid at the moment
- Assert.Equal (20, tableView.RowOffset);
- Assert.Equal (10, tableView.ColumnOffset);
- // Set small table
- tableView.Table = BuildTable (2, 2);
- // Setting a small table should automatically trigger fixing the scroll offsets to ensure valid cells
- Assert.Equal (0, tableView.RowOffset);
- Assert.Equal (0, tableView.ColumnOffset);
- // Trying to set invalid indexes should not be possible
- tableView.RowOffset = 20;
- tableView.ColumnOffset = 10;
- Assert.Equal (1, tableView.RowOffset);
- Assert.Equal (1, tableView.ColumnOffset);
- }
- [Fact]
- [AutoInitShutdown]
- public void Redraw_EmptyTable ()
- {
- var tableView = new TableView ();
- tableView.ColorScheme = new ColorScheme ();
- tableView.Bounds = new Rect (0, 0, 25, 10);
- // Set a table with 1 column
- tableView.Table = BuildTable (1, 50, out var dt);
- tableView.Draw ();
- dt.Columns.Remove (dt.Columns [0]);
- tableView.Draw ();
- }
- [Fact]
- public void SelectedCellChanged_NotFiredForSameValue ()
- {
- var tableView = new TableView () {
- Table = BuildTable (25, 50)
- };
- bool called = false;
- tableView.SelectedCellChanged += (s, e) => { called = true; };
- Assert.Equal (0, tableView.SelectedColumn);
- Assert.False (called);
- // Changing value to same as it already was should not raise an event
- tableView.SelectedColumn = 0;
- Assert.False (called);
- tableView.SelectedColumn = 10;
- Assert.True (called);
- }
- [Fact]
- public void SelectedCellChanged_SelectedColumnIndexesCorrect ()
- {
- var tableView = new TableView () {
- Table = BuildTable (25, 50)
- };
- bool called = false;
- tableView.SelectedCellChanged += (s, e) => {
- called = true;
- Assert.Equal (0, e.OldCol);
- Assert.Equal (10, e.NewCol);
- };
- tableView.SelectedColumn = 10;
- Assert.True (called);
- }
- [Fact]
- public void SelectedCellChanged_SelectedRowIndexesCorrect ()
- {
- var tableView = new TableView () {
- Table = BuildTable (25, 50)
- };
- bool called = false;
- tableView.SelectedCellChanged += (s, e) => {
- called = true;
- Assert.Equal (0, e.OldRow);
- Assert.Equal (10, e.NewRow);
- };
- tableView.SelectedRow = 10;
- Assert.True (called);
- }
- [Fact]
- public void Test_SumColumnWidth_UnicodeLength ()
- {
- Assert.Equal (11, "hello there".EnumerateRunes ().Sum (c => c.GetColumns ()));
- // Creates a string with the peculiar (french?) r symbol
- var surrogate = "Les Mise" + Char.ConvertFromUtf32 (Int32.Parse ("0301", NumberStyles.HexNumber)) + "rables";
- // The unicode width of this string is shorter than the string length!
- Assert.Equal (14, surrogate.EnumerateRunes ().Sum (c => c.GetColumns ()));
- Assert.Equal (15, surrogate.Length);
- }
- [Fact]
- public void IsSelected_MultiSelectionOn_Vertical ()
- {
- var tableView = new TableView () {
- Table = BuildTable (25, 50),
- MultiSelect = true
- };
- // 3 cell vertical selection
- tableView.SetSelection (1, 1, false);
- tableView.SetSelection (1, 3, true);
- Assert.False (tableView.IsSelected (0, 0));
- Assert.False (tableView.IsSelected (1, 0));
- Assert.False (tableView.IsSelected (2, 0));
- Assert.False (tableView.IsSelected (0, 1));
- Assert.True (tableView.IsSelected (1, 1));
- Assert.False (tableView.IsSelected (2, 1));
- Assert.False (tableView.IsSelected (0, 2));
- Assert.True (tableView.IsSelected (1, 2));
- Assert.False (tableView.IsSelected (2, 2));
- Assert.False (tableView.IsSelected (0, 3));
- Assert.True (tableView.IsSelected (1, 3));
- Assert.False (tableView.IsSelected (2, 3));
- Assert.False (tableView.IsSelected (0, 4));
- Assert.False (tableView.IsSelected (1, 4));
- Assert.False (tableView.IsSelected (2, 4));
- }
- [Fact]
- public void IsSelected_MultiSelectionOn_Horizontal ()
- {
- var tableView = new TableView () {
- Table = BuildTable (25, 50),
- MultiSelect = true
- };
- // 2 cell horizontal selection
- tableView.SetSelection (1, 0, false);
- tableView.SetSelection (2, 0, true);
- Assert.False (tableView.IsSelected (0, 0));
- Assert.True (tableView.IsSelected (1, 0));
- Assert.True (tableView.IsSelected (2, 0));
- Assert.False (tableView.IsSelected (3, 0));
- Assert.False (tableView.IsSelected (0, 1));
- Assert.False (tableView.IsSelected (1, 1));
- Assert.False (tableView.IsSelected (2, 1));
- Assert.False (tableView.IsSelected (3, 1));
- }
- [Fact]
- public void IsSelected_MultiSelectionOn_BoxSelection ()
- {
- var tableView = new TableView () {
- Table = BuildTable (25, 50),
- MultiSelect = true
- };
- // 4 cell horizontal in box 2x2
- tableView.SetSelection (0, 0, false);
- tableView.SetSelection (1, 1, true);
- Assert.True (tableView.IsSelected (0, 0));
- Assert.True (tableView.IsSelected (1, 0));
- Assert.False (tableView.IsSelected (2, 0));
- Assert.True (tableView.IsSelected (0, 1));
- Assert.True (tableView.IsSelected (1, 1));
- Assert.False (tableView.IsSelected (2, 1));
- Assert.False (tableView.IsSelected (0, 2));
- Assert.False (tableView.IsSelected (1, 2));
- Assert.False (tableView.IsSelected (2, 2));
- }
- [AutoInitShutdown]
- [Fact]
- public void PageDown_ExcludesHeaders ()
- {
- var tableView = new TableView () {
- Table = BuildTable (25, 50),
- MultiSelect = true,
- Bounds = new Rect (0, 0, 10, 5)
- };
- // Header should take up 2 lines
- tableView.Style.ShowHorizontalHeaderOverline = false;
- tableView.Style.ShowHorizontalHeaderUnderline = true;
- tableView.Style.AlwaysShowHeaders = false;
- // ensure that TableView has the input focus
- Application.Top.Add (tableView);
- Application.Begin (Application.Top);
- Application.Top.FocusFirst ();
- Assert.True (tableView.HasFocus);
- Assert.Equal (0, tableView.RowOffset);
- tableView.NewKeyDownEvent (new (KeyCode.PageDown));
- // window height is 5 rows 2 are header so page down should give 3 new rows
- Assert.Equal (3, tableView.SelectedRow);
- Assert.Equal (1, tableView.RowOffset);
- // header is no longer visible so page down should give 5 new rows
- tableView.NewKeyDownEvent (new (KeyCode.PageDown));
- Assert.Equal (8, tableView.SelectedRow);
- Assert.Equal (4, tableView.RowOffset);
- }
- [Fact]
- public void DeleteRow_SelectAll_AdjustsSelectionToPreventOverrun ()
- {
- // create a 4 by 4 table
- var tableView = new TableView () {
- Table = BuildTable (4, 4, out var dt),
- MultiSelect = true,
- Bounds = new Rect (0, 0, 10, 5)
- };
- tableView.BeginInit (); tableView.EndInit ();
- tableView.SelectAll ();
- Assert.Equal (16, tableView.GetAllSelectedCells ().Count ());
- // delete one of the columns
- dt.Columns.RemoveAt (2);
- // table should now be 3x4
- Assert.Equal (12, tableView.GetAllSelectedCells ().Count ());
- // remove a row
- dt.Rows.RemoveAt (1);
- // table should now be 3x3
- Assert.Equal (9, tableView.GetAllSelectedCells ().Count ());
- }
- [Fact]
- public void DeleteRow_SelectLastRow_AdjustsSelectionToPreventOverrun ()
- {
- // create a 4 by 4 table
- var tableView = new TableView () {
- Table = BuildTable (4, 4, out var dt),
- MultiSelect = true,
- Bounds = new Rect (0, 0, 10, 5)
- };
- tableView.BeginInit (); tableView.EndInit ();
- tableView.ChangeSelectionToEndOfTable (false);
- // select the last row
- tableView.MultiSelectedRegions.Clear ();
- tableView.MultiSelectedRegions.Push (new TableSelection (new Point (0, 3), new Rect (0, 3, 4, 1)));
- Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
- // remove a row
- dt.Rows.RemoveAt (0);
- tableView.EnsureValidSelection ();
- // since the selection no longer exists it should be removed
- Assert.Empty (tableView.MultiSelectedRegions);
- }
- [Theory]
- [InlineData (true)]
- [InlineData (false)]
- public void GetAllSelectedCells_SingleCellSelected_ReturnsOne (bool multiSelect)
- {
- var tableView = new TableView () {
- Table = BuildTable (3, 3),
- MultiSelect = multiSelect,
- Bounds = new Rect (0, 0, 10, 5)
- };
- tableView.BeginInit (); tableView.EndInit ();
- tableView.SetSelection (1, 1, false);
- Assert.Single (tableView.GetAllSelectedCells ());
- Assert.Equal (new Point (1, 1), tableView.GetAllSelectedCells ().Single ());
- }
- [Fact]
- public void GetAllSelectedCells_SquareSelection_ReturnsFour ()
- {
- var tableView = new TableView () {
- Table = BuildTable (3, 3),
- MultiSelect = true,
- Bounds = new Rect (0, 0, 10, 5)
- };
- tableView.BeginInit (); tableView.EndInit ();
- // move cursor to 1,1
- tableView.SetSelection (1, 1, false);
- // spread selection across to 2,2 (e.g. shift+right then shift+down)
- tableView.SetSelection (2, 2, true);
- var selected = tableView.GetAllSelectedCells ().ToArray ();
- Assert.Equal (4, selected.Length);
- Assert.Equal (new Point (1, 1), selected [0]);
- Assert.Equal (new Point (2, 1), selected [1]);
- Assert.Equal (new Point (1, 2), selected [2]);
- Assert.Equal (new Point (2, 2), selected [3]);
- }
- [Fact]
- public void GetAllSelectedCells_SquareSelection_FullRowSelect ()
- {
- var tableView = new TableView () {
- Table = BuildTable (3, 3),
- MultiSelect = true,
- FullRowSelect = true,
- Bounds = new Rect (0, 0, 10, 5)
- };
- tableView.BeginInit (); tableView.EndInit ();
- // move cursor to 1,1
- tableView.SetSelection (1, 1, false);
- // spread selection across to 2,2 (e.g. shift+right then shift+down)
- tableView.SetSelection (2, 2, true);
- var selected = tableView.GetAllSelectedCells ().ToArray ();
- Assert.Equal (6, selected.Length);
- Assert.Equal (new Point (0, 1), selected [0]);
- Assert.Equal (new Point (1, 1), selected [1]);
- Assert.Equal (new Point (2, 1), selected [2]);
- Assert.Equal (new Point (0, 2), selected [3]);
- Assert.Equal (new Point (1, 2), selected [4]);
- Assert.Equal (new Point (2, 2), selected [5]);
- }
- [Fact]
- public void GetAllSelectedCells_TwoIsolatedSelections_ReturnsSix ()
- {
- var tableView = new TableView () {
- Table = BuildTable (20, 20),
- MultiSelect = true,
- Bounds = new Rect (0, 0, 10, 5)
- };
- tableView.BeginInit (); tableView.EndInit ();
- /*
- Sets up disconnected selections like:
- 00000000000
- 01100000000
- 01100000000
- 00000001100
- 00000000000
- */
- tableView.MultiSelectedRegions.Clear ();
- tableView.MultiSelectedRegions.Push (new TableSelection (new Point (1, 1), new Rect (1, 1, 2, 2)));
- tableView.MultiSelectedRegions.Push (new TableSelection (new Point (7, 3), new Rect (7, 3, 2, 1)));
- tableView.SelectedColumn = 8;
- tableView.SelectedRow = 3;
- var selected = tableView.GetAllSelectedCells ().ToArray ();
- Assert.Equal (6, selected.Length);
- Assert.Equal (new Point (1, 1), selected [0]);
- Assert.Equal (new Point (2, 1), selected [1]);
- Assert.Equal (new Point (1, 2), selected [2]);
- Assert.Equal (new Point (2, 2), selected [3]);
- Assert.Equal (new Point (7, 3), selected [4]);
- Assert.Equal (new Point (8, 3), selected [5]);
- }
- [Fact, AutoInitShutdown]
- public void TableView_ShowHeadersFalse_AndNoHeaderLines ()
- {
- var tv = GetABCDEFTableView (out _);
- tv.Bounds = new Rect (0, 0, 5, 5);
- tv.Style.ShowHeaders = false;
- tv.Style.ShowHorizontalHeaderOverline = false;
- tv.Style.ShowHorizontalHeaderUnderline = false;
- tv.Draw ();
- string expected = @"
- │1│2│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TableView_ShowHeadersFalse_OverlineTrue ()
- {
- var tv = GetABCDEFTableView (out _);
- tv.Bounds = new Rect (0, 0, 5, 5);
- tv.Style.ShowHeaders = false;
- tv.Style.ShowHorizontalHeaderOverline = true;
- tv.Style.ShowHorizontalHeaderUnderline = false;
- tv.Draw ();
- string expected = @"
- ┌─┬─┐
- │1│2│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TableView_ShowHeadersFalse_UnderlineTrue ()
- {
- var tv = GetABCDEFTableView (out _);
- tv.Bounds = new Rect (0, 0, 5, 5);
- tv.Style.ShowHeaders = false;
- tv.Style.ShowHorizontalHeaderOverline = false;
- tv.Style.ShowHorizontalHeaderUnderline = true;
- // Horizontal scrolling option is part of the underline
- tv.Style.ShowHorizontalScrollIndicators = true;
- tv.Draw ();
- string expected = @"
- ├─┼─►
- │1│2│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TableView_ShowHeadersFalse_AllLines ()
- {
- var tv = GetABCDEFTableView (out _);
- tv.Bounds = new Rect (0, 0, 5, 5);
- tv.Style.ShowHeaders = false;
- tv.Style.ShowHorizontalHeaderOverline = true;
- tv.Style.ShowHorizontalHeaderUnderline = true;
- // Horizontal scrolling option is part of the underline
- tv.Style.ShowHorizontalScrollIndicators = true;
- tv.Draw ();
- string expected = @"
- ┌─┬─┐
- ├─┼─►
- │1│2│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TableView_ExpandLastColumn_True ()
- {
- var tv = SetUpMiniTable ();
- // the thing we are testing
- tv.Style.ExpandLastColumn = true;
- tv.Draw ();
- string expected = @"
- ┌─┬──────┐
- │A│B │
- ├─┼──────┤
- │1│2 │
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact, AutoInitShutdown]
- public void TableView_ExpandLastColumn_False ()
- {
- var tv = SetUpMiniTable ();
- // the thing we are testing
- tv.Style.ExpandLastColumn = false;
- tv.Draw ();
- string expected = @"
- ┌─┬─┬────┐
- │A│B│ │
- ├─┼─┼────┤
- │1│2│ │
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact, AutoInitShutdown]
- public void TableView_ExpandLastColumn_False_ExactBounds ()
- {
- var tv = SetUpMiniTable ();
- // the thing we are testing
- tv.Style.ExpandLastColumn = false;
- // width exactly matches the max col widths
- tv.Bounds = new Rect (0, 0, 5, 4);
- tv.Draw ();
- string expected = @"
- ┌─┬─┐
- │A│B│
- ├─┼─┤
- │1│2│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- [AutoInitShutdown]
- public void TableView_Activate ()
- {
- string activatedValue = null;
- var tv = new TableView (BuildTable (1, 1));
- tv.CellActivated += (s, c) => activatedValue = c.Table [c.Row, c.Col].ToString ();
- Application.Top.Add (tv);
- Application.Begin (Application.Top);
- // pressing enter should activate the first cell (selected cell)
- tv.NewKeyDownEvent (new (KeyCode.Enter));
- Assert.Equal ("R0C0", activatedValue);
- // reset the test
- activatedValue = null;
- // clear keybindings and ensure that Enter does not trigger the event anymore
- tv.KeyBindings.Clear ();
- tv.NewKeyDownEvent (new (KeyCode.Enter));
- Assert.Null (activatedValue);
- // New method for changing the activation key
- tv.KeyBindings.Add (KeyCode.Z, Command.Accept);
- tv.NewKeyDownEvent (new (KeyCode.Z));
- Assert.Equal ("R0C0", activatedValue);
- // reset the test
- activatedValue = null;
- tv.KeyBindings.Clear ();
- // Old method for changing the activation key
- tv.CellActivationKey = KeyCode.Z;
- tv.NewKeyDownEvent (new (KeyCode.Z));
- Assert.Equal ("R0C0", activatedValue);
- }
- [Fact, AutoInitShutdown]
- public void TableViewMultiSelect_CannotFallOffLeft ()
- {
- var tv = SetUpMiniTable (out var dt);
- dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
- tv.MultiSelect = true;
- tv.SelectedColumn = 1;
- tv.SelectedRow = 1;
- tv.NewKeyDownEvent (new (KeyCode.CursorLeft | KeyCode.ShiftMask));
- Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
- // this next shift left should be ignored because we are already at the bounds
- tv.NewKeyDownEvent (new (KeyCode.CursorLeft | KeyCode.ShiftMask));
- Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
- Assert.Equal (0, tv.SelectedColumn);
- Assert.Equal (1, tv.SelectedRow);
- Application.Shutdown ();
- }
- [Fact, AutoInitShutdown]
- public void TableViewMultiSelect_CannotFallOffRight ()
- {
- var tv = SetUpMiniTable (out var dt);
- dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
- tv.MultiSelect = true;
- tv.SelectedColumn = 0;
- tv.SelectedRow = 1;
- tv.NewKeyDownEvent (new (KeyCode.CursorRight | KeyCode.ShiftMask));
- Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
- // this next shift right should be ignored because we are already at the right bounds
- tv.NewKeyDownEvent (new (KeyCode.CursorRight | KeyCode.ShiftMask));
- Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
- Assert.Equal (1, tv.SelectedColumn);
- Assert.Equal (1, tv.SelectedRow);
- Application.Shutdown ();
- }
- [Fact, AutoInitShutdown]
- public void TableViewMultiSelect_CannotFallOffBottom ()
- {
- var tv = SetUpMiniTable (out var dt);
- dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
- tv.MultiSelect = true;
- tv.SelectedColumn = 0;
- tv.SelectedRow = 0;
- tv.NewKeyDownEvent (new (KeyCode.CursorRight | KeyCode.ShiftMask));
- tv.NewKeyDownEvent (new (KeyCode.CursorDown | KeyCode.ShiftMask));
- Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
- // this next moves should be ignored because we already selected the whole table
- tv.NewKeyDownEvent (new (KeyCode.CursorRight | KeyCode.ShiftMask));
- tv.NewKeyDownEvent (new (KeyCode.CursorDown | KeyCode.ShiftMask));
- Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
- Assert.Equal (1, tv.SelectedColumn);
- Assert.Equal (1, tv.SelectedRow);
- Application.Shutdown ();
- }
- [Fact, AutoInitShutdown]
- public void TableViewMultiSelect_CannotFallOffTop ()
- {
- var tv = SetUpMiniTable (out var dt);
- dt.Rows.Add (1, 2); // add another row (brings us to 2 rows)
- tv.LayoutSubviews ();
- tv.MultiSelect = true;
- tv.SelectedColumn = 1;
- tv.SelectedRow = 1;
- tv.NewKeyDownEvent (new (KeyCode.CursorLeft | KeyCode.ShiftMask));
- tv.NewKeyDownEvent (new (KeyCode.CursorUp | KeyCode.ShiftMask));
- Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
- // this next moves should be ignored because we already selected the whole table
- tv.NewKeyDownEvent (new (KeyCode.CursorLeft | KeyCode.ShiftMask));
- tv.NewKeyDownEvent (new (KeyCode.CursorUp | KeyCode.ShiftMask));
- Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
- Assert.Equal (0, tv.SelectedColumn);
- Assert.Equal (0, tv.SelectedRow);
- Application.Shutdown ();
- }
- [Fact, AutoInitShutdown]
- public void TestShiftClick_MultiSelect_TwoRowTable_FullRowSelect ()
- {
- var tv = GetTwoRowSixColumnTable ();
- tv.LayoutSubviews ();
- tv.MultiSelect = true;
- // Clicking in bottom row
- tv.MouseEvent (new MouseEvent {
- X = 1,
- Y = 3,
- Flags = MouseFlags.Button1Clicked
- });
- // should select that row
- Assert.Equal (1, tv.SelectedRow);
- // shift clicking top row
- tv.MouseEvent (new MouseEvent {
- X = 1,
- Y = 2,
- Flags = MouseFlags.Button1Clicked | MouseFlags.ButtonShift
- });
- // should extend the selection
- Assert.Equal (0, tv.SelectedRow);
- var selected = tv.GetAllSelectedCells ().ToArray ();
- Assert.Contains (new Point (0, 0), selected);
- Assert.Contains (new Point (0, 1), selected);
- }
- [Fact, AutoInitShutdown]
- public void TestControlClick_MultiSelect_ThreeRowTable_FullRowSelect ()
- {
- var tv = GetTwoRowSixColumnTable (out var dt);
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tv.LayoutSubviews ();
- tv.MultiSelect = true;
- // Clicking in bottom row
- tv.MouseEvent (new MouseEvent {
- X = 1,
- Y = 4,
- Flags = MouseFlags.Button1Clicked
- });
- // should select that row
- Assert.Equal (2, tv.SelectedRow);
- // shift clicking top row
- tv.MouseEvent (new MouseEvent {
- X = 1,
- Y = 2,
- Flags = MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl
- });
- // should extend the selection
- // to include bottom and top row but not middle
- Assert.Equal (0, tv.SelectedRow);
- var selected = tv.GetAllSelectedCells ().ToArray ();
- Assert.Contains (new Point (0, 0), selected);
- Assert.DoesNotContain (new Point (0, 1), selected);
- Assert.Contains (new Point (0, 2), selected);
- }
- [Theory, AutoInitShutdown]
- [InlineData (false)]
- [InlineData (true)]
- public void TableView_ColorTests_FocusedOrNot (bool focused)
- {
- var tv = SetUpMiniTable ();
- tv.LayoutSubviews ();
- // width exactly matches the max col widths
- tv.Bounds = new Rect (0, 0, 5, 4);
- // private method for forcing the view to be focused/not focused
- var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
- // when the view is/isn't focused
- setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
- tv.Draw ();
- string expected = @"
- ┌─┬─┐
- │A│B│
- ├─┼─┤
- │1│2│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- string expectedColors = @"
- 00000
- 00000
- 00000
- 01000
- ";
- TestHelpers.AssertDriverAttributesAre (expectedColors, driver: Application.Driver, new Attribute [] {
- // 0
- tv.ColorScheme.Normal,
- // 1
- focused ? tv.ColorScheme.Focus : tv.ColorScheme.HotNormal});
- Application.Shutdown ();
- }
- [Theory, AutoInitShutdown]
- [InlineData (false)]
- [InlineData (true)]
- public void TableView_ColorTests_InvertSelectedCellFirstCharacter (bool focused)
- {
- var tv = SetUpMiniTable ();
- tv.Style.InvertSelectedCellFirstCharacter = true;
- tv.LayoutSubviews ();
- // width exactly matches the max col widths
- tv.Bounds = new Rect (0, 0, 5, 4);
- // private method for forcing the view to be focused/not focused
- var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
- // when the view is/isn't focused
- setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
- tv.Draw ();
- string expected = @"
- ┌─┬─┐
- │A│B│
- ├─┼─┤
- │1│2│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- string expectedColors = @"
- 00000
- 00000
- 00000
- 01000
- ";
- var invertFocus = new Attribute (tv.ColorScheme.Focus.Background, tv.ColorScheme.Focus.Foreground);
- var invertHotNormal = new Attribute (tv.ColorScheme.HotNormal.Background, tv.ColorScheme.HotNormal.Foreground);
- TestHelpers.AssertDriverAttributesAre (expectedColors, driver: Application.Driver, new Attribute [] {
- // 0
- tv.ColorScheme.Normal,
- // 1
- focused ? invertFocus : invertHotNormal});
- Application.Shutdown ();
- }
- [Theory, AutoInitShutdown]
- [InlineData (false)]
- [InlineData (true)]
- public void TableView_ColorsTest_RowColorGetter (bool focused)
- {
- var tv = SetUpMiniTable (out DataTable dt);
- tv.LayoutSubviews ();
- // width exactly matches the max col widths
- tv.Bounds = new Rect (0, 0, 5, 4);
- var rowHighlight = new ColorScheme () {
- Normal = new Attribute (Color.BrightCyan, Color.DarkGray),
- HotNormal = new Attribute (Color.Green, Color.Blue),
- Focus = new Attribute (Color.BrightYellow, Color.White),
- // Not used by TableView
- HotFocus = new Attribute (Color.Cyan, Color.Magenta),
- };
- // when B is 2 use the custom highlight color for the row
- tv.Style.RowColorGetter += (e) => Convert.ToInt32 (e.Table [e.RowIndex, 1]) == 2 ? rowHighlight : null;
- // private method for forcing the view to be focused/not focused
- var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
- // when the view is/isn't focused
- setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
- tv.Draw ();
- string expected = @"
- ┌─┬─┐
- │A│B│
- ├─┼─┤
- │1│2│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- string expectedColors = @"
- 00000
- 00000
- 00000
- 21222
- ";
- TestHelpers.AssertDriverAttributesAre (expectedColors, driver: Application.Driver, new Attribute [] {
- // 0
- tv.ColorScheme.Normal,
- // 1
- focused ? rowHighlight.Focus : rowHighlight.HotNormal,
- // 2
- rowHighlight.Normal});
- // change the value in the table so that
- // it no longer matches the RowColorGetter
- // delegate conditional ( which checks for
- // the value 2)
- dt.Rows [0] [1] = 5;
- tv.Draw ();
- expected = @"
- ┌─┬─┐
- │A│B│
- ├─┼─┤
- │1│5│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- expectedColors = @"
- 00000
- 00000
- 00000
- 01000
- ";
- // now we only see 2 colors used (the selected cell color and Normal
- // rowHighlight should no longer be used because the delegate returned null
- // (now that the cell value is 5 - which does not match the conditional)
- TestHelpers.AssertDriverAttributesAre (expectedColors, driver: Application.Driver, new Attribute [] {
- // 0
- tv.ColorScheme.Normal,
- // 1
- focused ? tv.ColorScheme.Focus : tv.ColorScheme.HotNormal });
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Theory, AutoInitShutdown]
- [InlineData (false)]
- [InlineData (true)]
- public void TableView_ColorsTest_ColorGetter (bool focused)
- {
- var tv = SetUpMiniTable (out var dt);
- tv.LayoutSubviews ();
- // width exactly matches the max col widths
- tv.Bounds = new Rect (0, 0, 5, 4);
- // Create a style for column B
- var bStyle = tv.Style.GetOrCreateColumnStyle (1);
- // when B is 2 use the custom highlight color
- var cellHighlight = new ColorScheme () {
- Normal = new Attribute (Color.BrightCyan, Color.DarkGray),
- HotNormal = new Attribute (Color.Green, Color.Blue),
- Focus = new Attribute (Color.Cyan, Color.Magenta),
- // Not used by TableView
- HotFocus = new Attribute (Color.BrightYellow, Color.White),
- };
- bStyle.ColorGetter = (a) => Convert.ToInt32 (a.CellValue) == 2 ? cellHighlight : null;
- // private method for forcing the view to be focused/not focused
- var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
- // when the view is/isn't focused
- setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
- tv.Draw ();
- string expected = @"
- ┌─┬─┐
- │A│B│
- ├─┼─┤
- │1│2│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- string expectedColors = @"
- 00000
- 00000
- 00000
- 01020
- ";
- TestHelpers.AssertDriverAttributesAre (expectedColors, driver: Application.Driver, new Attribute [] {
- // 0
- tv.ColorScheme.Normal,
- // 1
- focused ? tv.ColorScheme.Focus : tv.ColorScheme.HotNormal,
- // 2
- cellHighlight.Normal});
- // change the value in the table so that
- // it no longer matches the ColorGetter
- // delegate conditional ( which checks for
- // the value 2)
- dt.Rows [0] [1] = 5;
- tv.Draw ();
- expected = @"
- ┌─┬─┐
- │A│B│
- ├─┼─┤
- │1│5│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- expectedColors = @"
- 00000
- 00000
- 00000
- 01000
- ";
- // now we only see 2 colors used (the selected cell color and Normal
- // cellHighlight should no longer be used because the delegate returned null
- // (now that the cell value is 5 - which does not match the conditional)
- TestHelpers.AssertDriverAttributesAre (expectedColors, driver: Application.Driver, new Attribute [] {
- // 0
- tv.ColorScheme.Normal,
- // 1
- focused ? tv.ColorScheme.Focus : tv.ColorScheme.HotNormal });
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- private TableView SetUpMiniTable ()
- {
- return SetUpMiniTable (out _);
- }
- private TableView SetUpMiniTable (out DataTable dt)
- {
- var tv = new TableView ();
- tv.BeginInit (); tv.EndInit ();
- tv.Bounds = new Rect (0, 0, 10, 4);
- dt = new DataTable ();
- dt.Columns.Add ("A");
- dt.Columns.Add ("B");
- dt.Rows.Add (1, 2);
- tv.Table = new DataTableSource (dt);
- tv.Style.GetOrCreateColumnStyle (0).MinWidth = 1;
- tv.Style.GetOrCreateColumnStyle (0).MinWidth = 1;
- tv.Style.GetOrCreateColumnStyle (1).MaxWidth = 1;
- tv.Style.GetOrCreateColumnStyle (1).MaxWidth = 1;
- tv.ColorScheme = Colors.ColorSchemes ["Base"];
- return tv;
- }
- [Fact]
- [AutoInitShutdown]
- public void ScrollDown_OneLineAtATime ()
- {
- var tableView = new TableView ();
- tableView.BeginInit (); tableView.EndInit ();
- // Set big table
- tableView.Table = BuildTable (25, 50);
- // 1 header + 4 rows visible
- tableView.Bounds = new Rect (0, 0, 25, 5);
- tableView.Style.ShowHorizontalHeaderUnderline = false;
- tableView.Style.ShowHorizontalHeaderOverline = false;
- tableView.Style.AlwaysShowHeaders = true;
- // select last row
- tableView.SelectedRow = 3; // row is 0 indexed so this is the 4th visible row
- // Scroll down
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorDown });
- // Scrolled off the page by 1 row so it should only have moved down 1 line of RowOffset
- Assert.Equal (4, tableView.SelectedRow);
- Assert.Equal (1, tableView.RowOffset);
- }
- [Fact, AutoInitShutdown]
- public void ScrollRight_SmoothScrolling ()
- {
- var tableView = new TableView ();
- tableView.BeginInit (); tableView.EndInit ();
- tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
- tableView.LayoutSubviews ();
- // 3 columns are visibile
- tableView.Bounds = new Rect (0, 0, 7, 5);
- tableView.Style.ShowHorizontalHeaderUnderline = false;
- tableView.Style.ShowHorizontalHeaderOverline = false;
- tableView.Style.AlwaysShowHeaders = true;
- tableView.Style.SmoothHorizontalScrolling = true;
- var dt = new DataTable ();
- dt.Columns.Add ("A");
- dt.Columns.Add ("B");
- dt.Columns.Add ("C");
- dt.Columns.Add ("D");
- dt.Columns.Add ("E");
- dt.Columns.Add ("F");
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tableView.Table = new DataTableSource (dt);
- // select last visible column
- tableView.SelectedColumn = 2; // column C
- tableView.Draw ();
- string expected =
- @"
- │A│B│C│
- │1│2│3│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Scroll right
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorRight });
- tableView.Draw ();
- // Note that with SmoothHorizontalScrolling only a single new column
- // is exposed when scrolling right. This is not always the case though
- // sometimes if the leftmost column is long (i.e. A is a long column)
- // then when A is pushed off the screen multiple new columns could be exposed
- // (not just D but also E and F). This is because TableView never shows
- // 'half cells' or scrolls by console unit (scrolling is done by table row/column increments).
- expected =
- @"
- │B│C│D│
- │2│3│4│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact, AutoInitShutdown]
- public void ScrollRight_WithoutSmoothScrolling ()
- {
- var tableView = new TableView ();
- tableView.BeginInit (); tableView.EndInit ();
- tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
- // 3 columns are visibile
- tableView.Bounds = new Rect (0, 0, 7, 5);
- tableView.Style.ShowHorizontalHeaderUnderline = false;
- tableView.Style.ShowHorizontalHeaderOverline = false;
- tableView.Style.AlwaysShowHeaders = true;
- tableView.Style.SmoothHorizontalScrolling = false;
- var dt = new DataTable ();
- dt.Columns.Add ("A");
- dt.Columns.Add ("B");
- dt.Columns.Add ("C");
- dt.Columns.Add ("D");
- dt.Columns.Add ("E");
- dt.Columns.Add ("F");
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tableView.Table = new DataTableSource (dt);
- // select last visible column
- tableView.SelectedColumn = 2; // column C
- tableView.Draw ();
- string expected =
- @"
- │A│B│C│
- │1│2│3│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Scroll right
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorRight });
- tableView.Draw ();
- // notice that without smooth scrolling we just update the first column
- // rendered in the table to the newly exposed column (D). This is fast
- // since we don't have to worry about repeatedly measuring the content
- // area as we scroll until the new column (D) is exposed. But it makes
- // the view 'jump' to expose all new columns
- expected =
- @"
- │D│E│F│
- │4│5│6│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- private TableView GetABCDEFTableView (out DataTable dt)
- {
- var tableView = new TableView ();
- tableView.BeginInit (); tableView.EndInit ();
- tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
- // 3 columns are visible
- tableView.Bounds = new Rect (0, 0, 7, 5);
- tableView.Style.ShowHorizontalHeaderUnderline = false;
- tableView.Style.ShowHorizontalHeaderOverline = false;
- tableView.Style.AlwaysShowHeaders = true;
- tableView.Style.SmoothHorizontalScrolling = false;
- dt = new DataTable ();
- dt.Columns.Add ("A");
- dt.Columns.Add ("B");
- dt.Columns.Add ("C");
- dt.Columns.Add ("D");
- dt.Columns.Add ("E");
- dt.Columns.Add ("F");
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tableView.Table = new DataTableSource (dt);
- return tableView;
- }
- [Fact, AutoInitShutdown]
- public void TestColumnStyle_VisibleFalse_IsNotRendered ()
- {
- var tableView = GetABCDEFTableView (out _);
- tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
- tableView.LayoutSubviews ();
- tableView.Draw ();
- string expected =
- @"
- │A│C│D│
- │1│3│4│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TestColumnStyle_FirstColumnVisibleFalse_IsNotRendered ()
- {
- var tableView = GetABCDEFTableView (out DataTable dt);
- tableView.Style.ShowHorizontalScrollIndicators = true;
- tableView.Style.ShowHorizontalHeaderUnderline = true;
- tableView.Style.GetOrCreateColumnStyle (0).Visible = false;
- tableView.LayoutSubviews ();
- tableView.Draw ();
- string expected =
- @"
- │B│C│D│
- ├─┼─┼─►
- │2│3│4│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TestColumnStyle_AllColumnsVisibleFalse_BehavesAsTableNull ()
- {
- var tableView = GetABCDEFTableView (out DataTable dt);
- for (int i = 0; i < 6; i++) {
- tableView.Style.GetOrCreateColumnStyle (i).Visible = false;
- }
- tableView.LayoutSubviews ();
- // expect nothing to be rendered when all columns are invisible
- string expected =
- @"
- ";
- tableView.Draw ();
- TestHelpers.AssertDriverContentsAre (expected, output);
- // expect behavior to match when Table is null
- tableView.Table = null;
- tableView.Draw ();
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TestColumnStyle_RemainingColumnsInvisible_NoScrollIndicator ()
- {
- var tableView = GetABCDEFTableView (out DataTable dt);
- tableView.Style.ShowHorizontalScrollIndicators = true;
- tableView.Style.ShowHorizontalHeaderUnderline = true;
- tableView.LayoutSubviews ();
- tableView.Draw ();
- // normally we should have scroll indicators because DEF are of screen
- string expected =
- @"
- │A│B│C│
- ├─┼─┼─►
- │1│2│3│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // but if DEF are invisible we shouldn't be showing the indicator
- tableView.Style.GetOrCreateColumnStyle (3).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (4).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (5).Visible = false;
- expected =
- @"
- │A│B│C│
- ├─┼─┼─┤
- │1│2│3│";
- tableView.Draw ();
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TestColumnStyle_PreceedingColumnsInvisible_NoScrollIndicator ()
- {
- var tableView = GetABCDEFTableView (out DataTable dt);
- tableView.Style.ShowHorizontalScrollIndicators = true;
- tableView.Style.ShowHorizontalHeaderUnderline = true;
- tableView.ColumnOffset = 1;
- tableView.LayoutSubviews ();
- tableView.Draw ();
- // normally we should have scroll indicators because A,E and F are of screen
- string expected =
- @"
- │B│C│D│
- ◄─┼─┼─►
- │2│3│4│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // but if E and F are invisible so we shouldn't show right
- tableView.Style.GetOrCreateColumnStyle (4).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (5).Visible = false;
- expected =
- @"
- │B│C│D│
- ◄─┼─┼─┤
- │2│3│4│";
- tableView.Draw ();
- TestHelpers.AssertDriverContentsAre (expected, output);
- // now also A is invisible so we cannot scroll in either direction
- tableView.Style.GetOrCreateColumnStyle (0).Visible = false;
- expected =
- @"
- │B│C│D│
- ├─┼─┼─┤
- │2│3│4│";
- tableView.Draw ();
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TestColumnStyle_VisibleFalse_CursorStepsOverInvisibleColumns ()
- {
- var tableView = GetABCDEFTableView (out var dt);
- tableView.LayoutSubviews ();
- tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
- tableView.SelectedColumn = 0;
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorRight });
- // Expect the cursor navigation to skip over the invisible column(s)
- Assert.Equal (2, tableView.SelectedColumn);
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorLeft });
- // Expect the cursor navigation backwards to skip over invisible column too
- Assert.Equal (0, tableView.SelectedColumn);
- }
- [InlineData (true)]
- [InlineData (false)]
- [Theory, AutoInitShutdown]
- public void TestColumnStyle_FirstColumnVisibleFalse_CursorStaysAt1 (bool useHome)
- {
- var tableView = GetABCDEFTableView (out var dt);
- tableView.LayoutSubviews ();
- tableView.Style.GetOrCreateColumnStyle (0).Visible = false;
- tableView.SelectedColumn = 0;
- Assert.Equal (0, tableView.SelectedColumn);
- // column 0 is invisible so this method should move to 1
- tableView.EnsureValidSelection ();
- Assert.Equal (1, tableView.SelectedColumn);
- tableView.NewKeyDownEvent (new Key () {
- KeyCode = useHome ? KeyCode.Home : KeyCode.CursorLeft
- });
- // Expect the cursor to stay at 1
- Assert.Equal (1, tableView.SelectedColumn);
- }
- [InlineData (true)]
- [InlineData (false)]
- [Theory, AutoInitShutdown]
- public void TestMoveStartEnd_WithFullRowSelect (bool withFullRowSelect)
- {
- var tableView = GetTwoRowSixColumnTable ();
- tableView.LayoutSubviews ();
- tableView.FullRowSelect = withFullRowSelect;
- tableView.SelectedRow = 1;
- tableView.SelectedColumn = 1;
- tableView.NewKeyDownEvent (new Key () {
- KeyCode = KeyCode.Home | KeyCode.CtrlMask
- });
- if (withFullRowSelect) {
- // Should not be any horizontal movement when
- // using navigate to Start/End and FullRowSelect
- Assert.Equal (1, tableView.SelectedColumn);
- Assert.Equal (0, tableView.SelectedRow);
- } else {
- Assert.Equal (0, tableView.SelectedColumn);
- Assert.Equal (0, tableView.SelectedRow);
- }
- tableView.NewKeyDownEvent (new Key (
- KeyCode.End | KeyCode.CtrlMask));
- if (withFullRowSelect) {
- Assert.Equal (1, tableView.SelectedColumn);
- Assert.Equal (1, tableView.SelectedRow);
- } else {
- Assert.Equal (5, tableView.SelectedColumn);
- Assert.Equal (1, tableView.SelectedRow);
- }
- }
- [InlineData (true)]
- [InlineData (false)]
- [Theory, AutoInitShutdown]
- public void TestColumnStyle_LastColumnVisibleFalse_CursorStaysAt2 (bool useEnd)
- {
- var tableView = GetABCDEFTableView (out var dt);
- tableView.LayoutSubviews ();
- // select D
- tableView.SelectedColumn = 3;
- Assert.Equal (3, tableView.SelectedColumn);
- tableView.Style.GetOrCreateColumnStyle (3).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (4).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (5).Visible = false;
- // column D is invisible so this method should move to 2 (C)
- tableView.EnsureValidSelection ();
- Assert.Equal (2, tableView.SelectedColumn);
- tableView.NewKeyDownEvent (new Key () {
- KeyCode = useEnd ? KeyCode.End : KeyCode.CursorRight
- });
- // Expect the cursor to stay at 2
- Assert.Equal (2, tableView.SelectedColumn);
- }
- [Fact, AutoInitShutdown]
- public void TestColumnStyle_VisibleFalse_MultiSelected ()
- {
- var tableView = GetABCDEFTableView (out var dt);
- tableView.LayoutSubviews ();
- // user has rectangular selection
- tableView.MultiSelectedRegions.Push (
- new TableSelection (
- new Point (0, 0),
- new Rect (0, 0, 3, 1))
- );
- Assert.Equal (3, tableView.GetAllSelectedCells ().Count ());
- Assert.True (tableView.IsSelected (0, 0));
- Assert.True (tableView.IsSelected (1, 0));
- Assert.True (tableView.IsSelected (2, 0));
- Assert.False (tableView.IsSelected (3, 0));
- // if middle column is invisible
- tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
- // it should not be included in the selection
- Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
- Assert.True (tableView.IsSelected (0, 0));
- Assert.False (tableView.IsSelected (1, 0));
- Assert.True (tableView.IsSelected (2, 0));
- Assert.False (tableView.IsSelected (3, 0));
- Assert.DoesNotContain (new Point (1, 0), tableView.GetAllSelectedCells ());
- }
- [Fact, AutoInitShutdown]
- public void TestColumnStyle_VisibleFalse_MultiSelectingStepsOverInvisibleColumns ()
- {
- var tableView = GetABCDEFTableView (out _);
- tableView.LayoutSubviews ();
- // if middle column is invisible
- tableView.Style.GetOrCreateColumnStyle (1).Visible = false;
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorRight | KeyCode.ShiftMask });
- // Selection should extend from A to C but skip B
- Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
- Assert.True (tableView.IsSelected (0, 0));
- Assert.False (tableView.IsSelected (1, 0));
- Assert.True (tableView.IsSelected (2, 0));
- Assert.False (tableView.IsSelected (3, 0));
- Assert.DoesNotContain (new Point (1, 0), tableView.GetAllSelectedCells ());
- }
- [Fact, AutoInitShutdown]
- public void TestToggleCells_MultiSelectOn ()
- {
- // 2 row table
- var tableView = GetABCDEFTableView (out var dt);
- tableView.LayoutSubviews ();
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tableView.MultiSelect = true;
- tableView.KeyBindings.Add (KeyCode.Space, Command.ToggleChecked);
- var selectedCell = tableView.GetAllSelectedCells ().Single ();
- Assert.Equal (0, selectedCell.X);
- Assert.Equal (0, selectedCell.Y);
- // Go Right
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorRight });
- selectedCell = tableView.GetAllSelectedCells ().Single ();
- Assert.Equal (1, selectedCell.X);
- Assert.Equal (0, selectedCell.Y);
- // Toggle Select
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.Space });
- var m = tableView.MultiSelectedRegions.Single ();
- Assert.True (m.IsToggled);
- Assert.Equal (1, m.Origin.X);
- Assert.Equal (0, m.Origin.Y);
- selectedCell = tableView.GetAllSelectedCells ().Single ();
- Assert.Equal (1, selectedCell.X);
- Assert.Equal (0, selectedCell.Y);
- // Go Left
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorLeft });
- // Both Toggled and Moved to should be selected
- Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
- var s1 = tableView.GetAllSelectedCells ().ElementAt (0);
- var s2 = tableView.GetAllSelectedCells ().ElementAt (1);
- Assert.Equal (1, s1.X);
- Assert.Equal (0, s1.Y);
- Assert.Equal (0, s2.X);
- Assert.Equal (0, s2.Y);
- // Go Down
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorDown });
- // Both Toggled and Moved to should be selected but not 0,0
- // which we moved down from
- Assert.Equal (2, tableView.GetAllSelectedCells ().Count ());
- s1 = tableView.GetAllSelectedCells ().ElementAt (0);
- s2 = tableView.GetAllSelectedCells ().ElementAt (1);
- Assert.Equal (1, s1.X);
- Assert.Equal (0, s1.Y);
- Assert.Equal (0, s2.X);
- Assert.Equal (1, s2.Y);
- // Go back to the toggled cell
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorRight });
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorUp });
- // Toggle off
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.Space });
- // Go Left
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorLeft });
- selectedCell = tableView.GetAllSelectedCells ().Single ();
- Assert.Equal (0, selectedCell.X);
- Assert.Equal (0, selectedCell.Y);
- }
- [Fact, AutoInitShutdown]
- public void TestToggleCells_MultiSelectOn_FullRowSelect ()
- {
- // 2 row table
- var tableView = GetABCDEFTableView (out var dt);
- tableView.LayoutSubviews ();
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tableView.FullRowSelect = true;
- tableView.MultiSelect = true;
- tableView.KeyBindings.Add (KeyCode.Space, Command.ToggleChecked);
- // Toggle Select Cell 0,0
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.Space });
- // Go Down
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorDown });
- var m = tableView.MultiSelectedRegions.Single ();
- Assert.True (m.IsToggled);
- Assert.Equal (0, m.Origin.X);
- Assert.Equal (0, m.Origin.Y);
- //First row toggled and Second row active = 12 selected cells
- Assert.Equal (12, tableView.GetAllSelectedCells ().Count ());
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorRight });
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorUp });
- Assert.Single (tableView.MultiSelectedRegions.Where (r => r.IsToggled));
- // Can untoggle at 1,0 even though 0,0 was initial toggle because FullRowSelect is on
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.Space });
- Assert.Empty (tableView.MultiSelectedRegions.Where (r => r.IsToggled));
- }
- [Fact, AutoInitShutdown]
- public void TestToggleCells_MultiSelectOn_SquareSelectToggled ()
- {
- // 3 row table
- var tableView = GetABCDEFTableView (out var dt);
- tableView.LayoutSubviews ();
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tableView.MultiSelect = true;
- tableView.KeyBindings.Add (KeyCode.Space, Command.ToggleChecked);
- // Make a square selection
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorDown });
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorRight });
- Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
- // Toggle the square selected region on
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.Space });
- // Go Right
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorRight });
- //Toggled on square + the active cell (x=2,y=1)
- Assert.Equal (5, tableView.GetAllSelectedCells ().Count ());
- Assert.Equal (2, tableView.SelectedColumn);
- Assert.Equal (1, tableView.SelectedRow);
- // Untoggle the rectangular region by hitting toggle in
- // any cell in that rect
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorUp });
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorLeft });
- Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.Space });
- Assert.Single (tableView.GetAllSelectedCells ());
- }
- [Fact, AutoInitShutdown]
- public void TestToggleCells_MultiSelectOn_Two_SquareSelects_BothToggled ()
- {
- // 6 row table
- var tableView = GetABCDEFTableView (out var dt);
- tableView.LayoutSubviews ();
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tableView.MultiSelect = true;
- tableView.KeyBindings.Add (KeyCode.Space, Command.ToggleChecked);
- // Make first square selection (0,0 to 1,1)
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorDown });
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorRight });
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.Space });
- Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
- // Make second square selection leaving 1 unselected line between them
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorLeft });
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorDown });
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorDown });
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorDown });
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.ShiftMask | KeyCode.CursorRight });
- // 2 square selections
- Assert.Equal (8, tableView.GetAllSelectedCells ().Count ());
- }
- [Theory, AutoInitShutdown]
- [InlineData (new object [] { true, true })]
- [InlineData (new object [] { false, true })]
- [InlineData (new object [] { true, false })]
- [InlineData (new object [] { false, false })]
- public void TestColumnStyle_VisibleFalse_DoesNotEffect_EnsureSelectedCellIsVisible (bool smooth, bool invisibleCol)
- {
- var tableView = GetABCDEFTableView (out var dt);
- tableView.LayoutSubviews ();
- tableView.Style.SmoothHorizontalScrolling = smooth;
- if (invisibleCol) {
- tableView.Style.GetOrCreateColumnStyle (3).Visible = false;
- }
- // New TableView should have first cell selected
- Assert.Equal (0, tableView.SelectedColumn);
- // With no scrolling
- Assert.Equal (0, tableView.ColumnOffset);
- // A,B and C are visible on screen at the moment so these should have no effect
- tableView.SelectedColumn = 1;
- tableView.EnsureSelectedCellIsVisible ();
- Assert.Equal (0, tableView.ColumnOffset);
- tableView.SelectedColumn = 2;
- tableView.EnsureSelectedCellIsVisible ();
- Assert.Equal (0, tableView.ColumnOffset);
- // Selecting D should move the visible table area to fit D onto the screen
- tableView.SelectedColumn = 3;
- tableView.EnsureSelectedCellIsVisible ();
- Assert.Equal (smooth ? 1 : 3, tableView.ColumnOffset);
- }
- [Fact, AutoInitShutdown]
- public void LongColumnTest ()
- {
- var tableView = new TableView ();
- Application.Top.Add (tableView);
- Application.Begin (Application.Top);
- tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
- // 25 characters can be printed into table
- tableView.Bounds = new Rect (0, 0, 25, 5);
- tableView.Style.ShowHorizontalHeaderUnderline = true;
- tableView.Style.ShowHorizontalHeaderOverline = false;
- tableView.Style.AlwaysShowHeaders = true;
- tableView.Style.SmoothHorizontalScrolling = true;
- var dt = new DataTable ();
- dt.Columns.Add ("A");
- dt.Columns.Add ("B");
- dt.Columns.Add ("Very Long Column");
- dt.Rows.Add (1, 2, new string ('a', 500));
- dt.Rows.Add (1, 2, "aaa");
- tableView.Table = new DataTableSource (dt);
- tableView.LayoutSubviews ();
- tableView.Draw ();
- // default behaviour of TableView is not to render
- // columns unless there is sufficient space
- string expected =
- @"
- │A│B │
- ├─┼─────────────────────►
- │1│2 │
- │1│2 │
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // get a style for the long column
- var style = tableView.Style.GetOrCreateColumnStyle (2);
- // one way the API user can fix this for long columns
- // is to specify a MinAcceptableWidth for the column
- style.MaxWidth = 10;
- tableView.LayoutSubviews ();
- tableView.Draw ();
- expected =
- @"
- │A│B│Very Long Column │
- ├─┼─┼───────────────────┤
- │1│2│aaaaaaaaaaaaaaaaaaa│
- │1│2│aaa │
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // revert the style change
- style.MaxWidth = TableView.DefaultMaxCellWidth;
- // another way API user can fix problem is to implement
- // RepresentationGetter and apply max length there
- style.RepresentationGetter = (s) => {
- return s.ToString ().Length < 15 ? s.ToString () : s.ToString ().Substring (0, 13) + "...";
- };
- tableView.LayoutSubviews ();
- tableView.Draw ();
- expected =
- @"
- │A│B│Very Long Column │
- ├─┼─┼───────────────────┤
- │1│2│aaaaaaaaaaaaa... │
- │1│2│aaa │
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // revert style change
- style.RepresentationGetter = null;
- // Both of the above methods rely on having a fixed
- // size limit for the column. These are awkward if a
- // table is resizeable e.g. Dim.Fill(). Ideally we want
- // to render in any space available and truncate the content
- // of the column dynamically so it fills the free space at
- // the end of the table.
- // We can now specify that the column can be any length
- // (Up to MaxWidth) but the renderer can accept using
- // less space down to this limit
- style.MinAcceptableWidth = 5;
- tableView.LayoutSubviews ();
- tableView.Draw ();
- expected =
- @"
- │A│B│Very Long Column │
- ├─┼─┼───────────────────┤
- │1│2│aaaaaaaaaaaaaaaaaaa│
- │1│2│aaa │
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Now test making the width too small for the MinAcceptableWidth
- // the Column won't fit so should not be rendered
- var driver = ((FakeDriver)Application.Driver);
- driver.ClearContents ();
- tableView.Bounds = new Rect (0, 0, 9, 5);
- tableView.LayoutSubviews ();
- tableView.Draw ();
- expected =
- @"
- │A│B │
- ├─┼─────►
- │1│2 │
- │1│2 │
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // setting width to 10 leaves just enough space for the column to
- // meet MinAcceptableWidth of 5. Column width includes terminator line
- // symbol (e.g. ┤ or │)
- tableView.Bounds = new Rect (0, 0, 10, 5);
- tableView.LayoutSubviews ();
- tableView.Draw ();
- expected =
- @"
- │A│B│Very│
- ├─┼─┼────┤
- │1│2│aaaa│
- │1│2│aaa │
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- tableView.Bounds = new Rect (0, 0, 25, 5);
- // revert style change
- style.MinAcceptableWidth = TableView.DefaultMinAcceptableWidth;
- // Now let's test the global MaxCellWidth and MinCellWidth
- tableView.Style.ExpandLastColumn = false;
- tableView.MaxCellWidth = 10;
- tableView.MinCellWidth = 3;
- tableView.LayoutSubviews ();
- tableView.Draw ();
- expected =
- @"
- │A │B │Very Long │ │
- ├───┼───┼──────────┼────┤
- │1 │2 │aaaaaaaaaa│ │
- │1 │2 │aaa │ │
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // MaxCellWidth limits MinCellWidth
- tableView.MaxCellWidth = 5;
- tableView.MinCellWidth = 10;
- tableView.LayoutSubviews ();
- tableView.Draw ();
- expected =
- @"
- │A │B │Very │ │
- ├─────┼─────┼─────┼─────┤
- │1 │2 │aaaaa│ │
- │1 │2 │aaa │ │
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- Application.Shutdown ();
- }
- [Fact, AutoInitShutdown]
- public void ScrollIndicators ()
- {
- var tableView = new TableView ();
- tableView.BeginInit (); tableView.EndInit ();
- tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
- // 3 columns are visibile
- tableView.Bounds = new Rect (0, 0, 7, 5);
- tableView.Style.ShowHorizontalHeaderUnderline = true;
- tableView.Style.ShowHorizontalHeaderOverline = false;
- tableView.Style.AlwaysShowHeaders = true;
- tableView.Style.SmoothHorizontalScrolling = true;
- var dt = new DataTable ();
- dt.Columns.Add ("A");
- dt.Columns.Add ("B");
- dt.Columns.Add ("C");
- dt.Columns.Add ("D");
- dt.Columns.Add ("E");
- dt.Columns.Add ("F");
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tableView.Table = new DataTableSource (dt);
- // select last visible column
- tableView.SelectedColumn = 2; // column C
- tableView.Draw ();
- // user can only scroll right so sees right indicator
- // Because first column in table is A
- string expected =
- @"
- │A│B│C│
- ├─┼─┼─►
- │1│2│3│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Scroll right
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorRight });
- // since A is now pushed off screen we get indicator showing
- // that user can scroll left to see first column
- tableView.Draw ();
- expected =
- @"
- │B│C│D│
- ◄─┼─┼─►
- │2│3│4│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Scroll right twice more (to end of columns)
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorRight });
- tableView.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorRight });
- tableView.Draw ();
- expected =
- @"
- │D│E│F│
- ◄─┼─┼─┤
- │4│5│6│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact, AutoInitShutdown]
- public void CellEventsBackgroundFill ()
- {
- var tv = new TableView () {
- Width = 20,
- Height = 4
- };
- var dt = new DataTable ();
- dt.Columns.Add ("C1");
- dt.Columns.Add ("C2");
- dt.Columns.Add ("C3");
- dt.Rows.Add ("Hello", DBNull.Value, "f");
- tv.Table = new DataTableSource (dt);
- tv.NullSymbol = string.Empty;
- Application.Top.Add (tv);
- Application.Begin (Application.Top);
- tv.Draw ();
- var expected =
- @"
- ┌─────┬──┬─────────┐
- │C1 │C2│C3 │
- ├─────┼──┼─────────┤
- │Hello│ │f │
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- var color = new Attribute (Color.Magenta, Color.BrightBlue);
- var scheme = new ColorScheme {
- Normal = color,
- HotFocus = color,
- Focus = color,
- Disabled = color,
- HotNormal = color,
- };
- // Now the thing we really want to test is the styles!
- // All cells in the column have a column style that says
- // the cell is pink!
- for (int i = 0; i < dt.Columns.Count; i++) {
- var style = tv.Style.GetOrCreateColumnStyle (i);
- style.ColorGetter = (e) => {
- return scheme;
- };
- }
- tv.Draw ();
- expected =
- @"
- 00000000000000000000
- 00000000000000000000
- 00000000000000000000
- 01111101101111111110
- ";
- TestHelpers.AssertDriverAttributesAre (expected, driver: Application.Driver, new Attribute [] { tv.ColorScheme.Normal, color });
- }
- [Fact, AutoInitShutdown]
- public void ShowHorizontalBottomLine_WithVerticalCellLines ()
- {
- var tableView = GetABCDEFTableView (out _);
- tableView.BeginInit (); tableView.EndInit ();
- tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
- // 3 columns are visibile
- tableView.Bounds = new Rect (0, 0, 7, 5);
- tableView.Style.ShowHorizontalHeaderUnderline = true;
- tableView.Style.ShowHorizontalHeaderOverline = false;
- tableView.Style.AlwaysShowHeaders = true;
- tableView.Style.SmoothHorizontalScrolling = true;
- tableView.Style.ShowHorizontalBottomline = true;
- tableView.Draw ();
- // user can only scroll right so sees right indicator
- // Because first column in table is A
- string expected =
- @"
- │A│B│C│
- ├─┼─┼─►
- │1│2│3│
- └─┴─┴─┘";
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void ShowHorizontalBottomLine_NoCellLines ()
- {
- var tableView = GetABCDEFTableView (out _);
- tableView.BeginInit (); tableView.EndInit ();
- tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
- // 3 columns are visibile
- tableView.Bounds = new Rect (0, 0, 7, 5);
- tableView.Style.ShowHorizontalHeaderUnderline = true;
- tableView.Style.ShowHorizontalHeaderOverline = false;
- tableView.Style.AlwaysShowHeaders = true;
- tableView.Style.SmoothHorizontalScrolling = true;
- tableView.Style.ShowHorizontalBottomline = true;
- tableView.Style.ShowVerticalCellLines = false;
- tableView.Draw ();
- // user can only scroll right so sees right indicator
- // Because first column in table is A
- string expected =
- @"
- │A│B│C│
- └─┴─┴─►
- 1 2 3
- ───────";
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TestFullRowSelect_SelectionColorStopsAtTableEdge_WithCellLines ()
- {
- var tv = GetTwoRowSixColumnTable (out var dt);
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tv.Bounds = new Rect (0, 0, 7, 6);
- tv.Frame = new Rect (0, 0, 7, 6);
- tv.LayoutSubviews ();
- tv.FullRowSelect = true;
- tv.Style.ShowHorizontalBottomline = true;
- // Clicking in bottom row
- tv.MouseEvent (new MouseEvent {
- X = 1,
- Y = 4,
- Flags = MouseFlags.Button1Clicked
- });
- // should select that row
- Assert.Equal (2, tv.SelectedRow);
- tv.OnDrawContent (tv.Bounds);
- string expected =
- @"
- │A│B│C│
- ├─┼─┼─►
- │1│2│3│
- │1│2│3│
- │1│2│3│
- └─┴─┴─┘";
- TestHelpers.AssertDriverContentsAre (expected, output);
- var normal = tv.ColorScheme.Normal;
- tv.ColorScheme = new ColorScheme (tv.ColorScheme) {
- Focus = new Attribute (Color.Magenta, Color.White)
- };
- var focus = tv.ColorScheme.Focus;
- tv.Draw ();
- // Focus color (1) should be used for rendering the selected line
- // But should not spill into the borders. Normal color (0) should be
- // used for the rest.
- expected =
- @"
- 0000000
- 0000000
- 0000000
- 0000000
- 0111110
- 0000000";
- TestHelpers.AssertDriverAttributesAre (expected, driver: Application.Driver, normal, focus);
- }
- [Fact, AutoInitShutdown]
- public void TestFullRowSelect_AlwaysUseNormalColorForVerticalCellLines ()
- {
- var tv = GetTwoRowSixColumnTable (out var dt);
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tv.Bounds = new Rect (0, 0, 7, 6);
- tv.Frame = new Rect (0, 0, 7, 6);
- tv.LayoutSubviews ();
- tv.FullRowSelect = true;
- tv.Style.ShowHorizontalBottomline = true;
- tv.Style.AlwaysUseNormalColorForVerticalCellLines = true;
- // Clicking in bottom row
- tv.MouseEvent (new MouseEvent {
- X = 1,
- Y = 4,
- Flags = MouseFlags.Button1Clicked
- });
- // should select that row
- Assert.Equal (2, tv.SelectedRow);
- tv.OnDrawContent (tv.Bounds);
- string expected =
- @"
- │A│B│C│
- ├─┼─┼─►
- │1│2│3│
- │1│2│3│
- │1│2│3│
- └─┴─┴─┘";
- TestHelpers.AssertDriverContentsAre (expected, output);
- var normal = tv.ColorScheme.Normal;
- tv.ColorScheme = new ColorScheme (tv.ColorScheme) {
- Focus = new Attribute (Color.Magenta, Color.White)
- };
- var focus = tv.ColorScheme.Focus;
- tv.Draw ();
- // Focus color (1) should be used for cells only because
- // AlwaysUseNormalColorForVerticalCellLines is true
- expected =
- @"
- 0000000
- 0000000
- 0000000
- 0000000
- 0101010
- 0000000";
- TestHelpers.AssertDriverAttributesAre (expected, driver: Application.Driver, normal, focus);
- }
- [Fact, AutoInitShutdown]
- public void TestTableViewCheckboxes_Simple ()
- {
- var tv = GetTwoRowSixColumnTable (out var dt);
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tv.LayoutSubviews ();
- var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
- tv.Table = wrapper;
- tv.Draw ();
- string expected =
- @"
- │ │A│B│
- ├─┼─┼─►
- │☐│1│2│
- │☐│1│2│
- │☐│1│2│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- Assert.Empty (wrapper.CheckedRows);
- //toggle the top cell
- tv.NewKeyDownEvent (new (KeyCode.Space));
- Assert.Single (wrapper.CheckedRows, 0);
- tv.Draw ();
- expected =
- @"
- │ │A│B│
- ├─┼─┼─►
- │☑│1│2│
- │☐│1│2│
- │☐│1│2│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- tv.NewKeyDownEvent (new (KeyCode.CursorDown));
- tv.NewKeyDownEvent (new (KeyCode.Space));
- Assert.Contains (0, wrapper.CheckedRows);
- Assert.Contains (1, wrapper.CheckedRows);
- Assert.Equal (2, wrapper.CheckedRows.Count);
- tv.Draw ();
- expected =
- @"
- │ │A│B│
- ├─┼─┼─►
- │☑│1│2│
- │☑│1│2│
- │☐│1│2│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // untoggle top one
- tv.NewKeyDownEvent (new (KeyCode.CursorUp));
- tv.NewKeyDownEvent (new (KeyCode.Space));
- Assert.Single (wrapper.CheckedRows, 1);
- tv.Draw ();
- expected =
- @"
- │ │A│B│
- ├─┼─┼─►
- │☐│1│2│
- │☑│1│2│
- │☐│1│2│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TestTableViewCheckboxes_SelectAllToggle ()
- {
- var tv = GetTwoRowSixColumnTable (out var dt);
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tv.LayoutSubviews ();
- var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
- tv.Table = wrapper;
- //toggle all cells
- tv.NewKeyDownEvent (new (KeyCode.A | KeyCode.CtrlMask));
- tv.NewKeyDownEvent (new (KeyCode.Space));
- tv.Draw ();
- string expected =
- @"
- │ │A│B│
- ├─┼─┼─►
- │☑│1│2│
- │☑│1│2│
- │☑│1│2│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- Assert.Contains (0, wrapper.CheckedRows);
- Assert.Contains (1, wrapper.CheckedRows);
- Assert.Contains (2, wrapper.CheckedRows);
- Assert.Equal (3, wrapper.CheckedRows.Count);
- // Untoggle all again
- tv.NewKeyDownEvent (new (KeyCode.Space));
- tv.Draw ();
- expected =
- @"
- │ │A│B│
- ├─┼─┼─►
- │☐│1│2│
- │☐│1│2│
- │☐│1│2│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- Assert.Empty (wrapper.CheckedRows);
- }
- [Fact, AutoInitShutdown]
- public void TestTableViewCheckboxes_MultiSelectIsUnion_WhenToggling ()
- {
- var tv = GetTwoRowSixColumnTable (out var dt);
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tv.LayoutSubviews ();
- var wrapper = new CheckBoxTableSourceWrapperByIndex (tv, tv.Table);
- tv.Table = wrapper;
- wrapper.CheckedRows.Add (0);
- wrapper.CheckedRows.Add (2);
- tv.Draw ();
- string expected =
- @"
- │ │A│B│
- ├─┼─┼─►
- │☑│1│2│
- │☐│1│2│
- │☑│1│2│";
- //toggle top two at once
- tv.NewKeyDownEvent (new (KeyCode.CursorDown | KeyCode.ShiftMask));
- Assert.True (tv.IsSelected (0, 0));
- Assert.True (tv.IsSelected (0, 1));
- tv.NewKeyDownEvent (new (KeyCode.Space));
- // Because at least 1 of the rows is not yet ticked we toggle them all to ticked
- TestHelpers.AssertDriverContentsAre (expected, output);
- Assert.Contains (0, wrapper.CheckedRows);
- Assert.Contains (1, wrapper.CheckedRows);
- Assert.Contains (2, wrapper.CheckedRows);
- Assert.Equal (3, wrapper.CheckedRows.Count);
- tv.Draw ();
- expected =
- @"
- │ │A│B│
- ├─┼─┼─►
- │☑│1│2│
- │☑│1│2│
- │☑│1│2│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Untoggle the top 2
- tv.NewKeyDownEvent (new (KeyCode.Space));
- tv.Draw ();
- expected =
- @"
- │ │A│B│
- ├─┼─┼─►
- │☐│1│2│
- │☐│1│2│
- │☑│1│2│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- Assert.Single (wrapper.CheckedRows, 2);
- }
- [Fact, AutoInitShutdown]
- public void TestTableViewCheckboxes_ByObject ()
- {
- var tv = GetPetTable (out var source);
- tv.LayoutSubviews ();
- var pets = source.Data;
- var wrapper = new CheckBoxTableSourceWrapperByObject<PickablePet> (
- tv,
- source,
- (p) => p.IsPicked,
- (p, b) => p.IsPicked = b);
- tv.Table = wrapper;
- tv.Draw ();
- string expected =
- @"
- ┌─┬───────┬─────────────┐
- │ │Name │Kind │
- ├─┼───────┼─────────────┤
- │☐│Tammy │Cat │
- │☐│Tibbles│Cat │
- │☐│Ripper │Dog │";
- TestHelpers.AssertDriverContentsAre (expected, output);
- Assert.Empty (pets.Where (p => p.IsPicked));
- tv.NewKeyDownEvent (new (KeyCode.Space));
- Assert.True (pets.First ().IsPicked);
- tv.Draw ();
- expected =
- @"
- ┌─┬───────┬─────────────┐
- │ │Name │Kind │
- ├─┼───────┼─────────────┤
- │☑│Tammy │Cat │
- │☐│Tibbles│Cat │
- │☐│Ripper │Dog │";
- TestHelpers.AssertDriverContentsAre (expected, output);
- tv.NewKeyDownEvent (new (KeyCode.CursorDown));
- tv.NewKeyDownEvent (new (KeyCode.Space));
- Assert.True (pets.ElementAt (0).IsPicked);
- Assert.True (pets.ElementAt (1).IsPicked);
- Assert.False (pets.ElementAt (2).IsPicked);
- tv.Draw ();
- expected =
- @"
- ┌─┬───────┬─────────────┐
- │ │Name │Kind │
- ├─┼───────┼─────────────┤
- │☑│Tammy │Cat │
- │☑│Tibbles│Cat │
- │☐│Ripper │Dog │";
- TestHelpers.AssertDriverContentsAre (expected, output);
- tv.NewKeyDownEvent (new (KeyCode.CursorUp));
- tv.NewKeyDownEvent (new (KeyCode.Space));
- Assert.False (pets.ElementAt (0).IsPicked);
- Assert.True (pets.ElementAt (1).IsPicked);
- Assert.False (pets.ElementAt (2).IsPicked);
- tv.Draw ();
- expected =
- @"
- ┌─┬───────┬─────────────┐
- │ │Name │Kind │
- ├─┼───────┼─────────────┤
- │☐│Tammy │Cat │
- │☑│Tibbles│Cat │
- │☐│Ripper │Dog │";
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TestTableViewCheckboxes_SelectAllToggle_ByObject ()
- {
- var tv = GetPetTable (out var source);
- tv.LayoutSubviews ();
- var pets = source.Data;
- var wrapper = new CheckBoxTableSourceWrapperByObject<PickablePet> (
- tv,
- source,
- (p) => p.IsPicked,
- (p, b) => p.IsPicked = b);
- tv.Table = wrapper;
- Assert.DoesNotContain (pets, p => p.IsPicked);
- //toggle all cells
- tv.NewKeyDownEvent (new (KeyCode.A | KeyCode.CtrlMask));
- tv.NewKeyDownEvent (new (KeyCode.Space));
- Assert.True (pets.All (p => p.IsPicked));
- tv.Draw ();
- string expected =
- @"
- ┌─┬───────┬─────────────┐
- │ │Name │Kind │
- ├─┼───────┼─────────────┤
- │☑│Tammy │Cat │
- │☑│Tibbles│Cat │
- │☑│Ripper │Dog │";
- TestHelpers.AssertDriverContentsAre (expected, output);
- tv.NewKeyDownEvent (new (KeyCode.Space));
- Assert.Empty (pets.Where (p => p.IsPicked));
- tv.Draw ();
- expected =
- @"
- ┌─┬───────┬─────────────┐
- │ │Name │Kind │
- ├─┼───────┼─────────────┤
- │☐│Tammy │Cat │
- │☐│Tibbles│Cat │
- │☐│Ripper │Dog │
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TestTableViewRadioBoxes_Simple_ByObject ()
- {
- var tv = GetPetTable (out var source);
- tv.LayoutSubviews ();
- var pets = source.Data;
- var wrapper = new CheckBoxTableSourceWrapperByObject<PickablePet> (
- tv,
- source,
- (p) => p.IsPicked,
- (p, b) => p.IsPicked = b);
- wrapper.UseRadioButtons = true;
- tv.Table = wrapper;
- tv.Draw ();
- string expected =
- @"
- ┌─┬───────┬─────────────┐
- │ │Name │Kind │
- ├─┼───────┼─────────────┤
- │○│Tammy │Cat │
- │○│Tibbles│Cat │
- │○│Ripper │Dog │
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- Assert.Empty (pets.Where (p => p.IsPicked));
- tv.NewKeyDownEvent (new (KeyCode.Space));
- Assert.True (pets.First ().IsPicked);
- tv.Draw ();
- expected =
- @"
- ┌─┬───────┬─────────────┐
- │ │Name │Kind │
- ├─┼───────┼─────────────┤
- │◉│Tammy │Cat │
- │○│Tibbles│Cat │
- │○│Ripper │Dog │";
- TestHelpers.AssertDriverContentsAre (expected, output);
- tv.NewKeyDownEvent (new (KeyCode.CursorDown));
- tv.NewKeyDownEvent (new (KeyCode.Space));
- Assert.False (pets.ElementAt (0).IsPicked);
- Assert.True (pets.ElementAt (1).IsPicked);
- Assert.False (pets.ElementAt (2).IsPicked);
- tv.Draw ();
- expected =
- @"
- ┌─┬───────┬─────────────┐
- │ │Name │Kind │
- ├─┼───────┼─────────────┤
- │○│Tammy │Cat │
- │◉│Tibbles│Cat │
- │○│Ripper │Dog │";
- TestHelpers.AssertDriverContentsAre (expected, output);
- tv.NewKeyDownEvent (new (KeyCode.CursorUp));
- tv.NewKeyDownEvent (new (KeyCode.Space));
- Assert.True (pets.ElementAt (0).IsPicked);
- Assert.False (pets.ElementAt (1).IsPicked);
- Assert.False (pets.ElementAt (2).IsPicked);
- tv.Draw ();
- expected =
- @"
- ┌─┬───────┬─────────────┐
- │ │Name │Kind │
- ├─┼───────┼─────────────┤
- │◉│Tammy │Cat │
- │○│Tibbles│Cat │
- │○│Ripper │Dog │";
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TestFullRowSelect_SelectionColorDoesNotStop_WhenShowVerticalCellLinesIsFalse ()
- {
- var tv = GetTwoRowSixColumnTable (out var dt);
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tv.LayoutSubviews ();
- tv.Bounds = new Rect (0, 0, 7, 6);
- tv.FullRowSelect = true;
- tv.Style.ShowVerticalCellLines = false;
- tv.Style.ShowVerticalHeaderLines = false;
- // Clicking in bottom row
- tv.MouseEvent (new MouseEvent {
- X = 1,
- Y = 4,
- Flags = MouseFlags.Button1Clicked
- });
- // should select that row
- Assert.Equal (2, tv.SelectedRow);
- tv.Draw ();
- string expected =
- @"
- A B C
- ───────
- 1 2 3
- 1 2 3
- 1 2 3";
- TestHelpers.AssertDriverContentsAre (expected, output);
- var normal = tv.ColorScheme.Normal;
- tv.ColorScheme = new ColorScheme (tv.ColorScheme) { Focus = new Attribute (Color.Magenta, Color.White) };
- var focus = tv.ColorScheme.Focus;
- tv.Draw ();
- // Focus color (1) should be used for rendering the selected line
- // Note that because there are no vertical cell lines we use the focus
- // color for the whole row
- expected =
- @"
- 000000
- 000000
- 000000
- 000000
- 111111";
- TestHelpers.AssertDriverAttributesAre (expected, driver: Application.Driver, normal, focus);
- }
- public static DataTableSource BuildTable (int cols, int rows)
- {
- return BuildTable (cols, rows, out _);
- }
- /// <summary>
- /// Builds a simple table of string columns with the requested number of columns and rows
- /// </summary>
- /// <param name="cols"></param>
- /// <param name="rows"></param>
- /// <returns></returns>
- public static DataTableSource BuildTable (int cols, int rows, out DataTable dt)
- {
- dt = new DataTable ();
- for (int c = 0; c < cols; c++) {
- dt.Columns.Add ("Col" + c);
- }
- for (int r = 0; r < rows; r++) {
- var newRow = dt.NewRow ();
- for (int c = 0; c < cols; c++) {
- newRow [c] = $"R{r}C{c}";
- }
- dt.Rows.Add (newRow);
- }
- return new DataTableSource (dt);
- }
- [Fact, AutoInitShutdown]
- public void Test_ScreenToCell ()
- {
- var tableView = GetTwoRowSixColumnTable ();
- tableView.BeginInit (); tableView.EndInit ();
- tableView.LayoutSubviews ();
- tableView.Draw ();
- // user can only scroll right so sees right indicator
- // Because first column in table is A
- string expected =
- @"
- │A│B│C│
- ├─┼─┼─►
- │1│2│3│
- │1│2│3│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // ---------------- X=0 -----------------------
- // click is before first cell
- Assert.Null (tableView.ScreenToCell (0, 0));
- Assert.Null (tableView.ScreenToCell (0, 1));
- Assert.Null (tableView.ScreenToCell (0, 2));
- Assert.Null (tableView.ScreenToCell (0, 3));
- Assert.Null (tableView.ScreenToCell (0, 4));
- // ---------------- X=1 -----------------------
- // click in header
- Assert.Null (tableView.ScreenToCell (1, 0));
- // click in header row line
- Assert.Null (tableView.ScreenToCell (1, 1));
- // click in cell 0,0
- Assert.Equal (new Point (0, 0), tableView.ScreenToCell (1, 2));
- // click in cell 0,1
- Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3));
- // after last row
- Assert.Null (tableView.ScreenToCell (1, 4));
- // ---------------- X=2 -----------------------
- // ( even though there is a horizontal dividing line here we treat it as a hit on the cell before)
- // click in header
- Assert.Null (tableView.ScreenToCell (2, 0));
- // click in header row line
- Assert.Null (tableView.ScreenToCell (2, 1));
- // click in cell 0,0
- Assert.Equal (new Point (0, 0), tableView.ScreenToCell (2, 2));
- // click in cell 0,1
- Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3));
- // after last row
- Assert.Null (tableView.ScreenToCell (2, 4));
- // ---------------- X=3 -----------------------
- // click in header
- Assert.Null (tableView.ScreenToCell (3, 0));
- // click in header row line
- Assert.Null (tableView.ScreenToCell (3, 1));
- // click in cell 1,0
- Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2));
- // click in cell 1,1
- Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3));
- // after last row
- Assert.Null (tableView.ScreenToCell (3, 4));
- }
- [Fact, AutoInitShutdown]
- public void Test_ScreenToCell_DataColumnOverload ()
- {
- var tableView = GetTwoRowSixColumnTable ();
- tableView.LayoutSubviews ();
- tableView.Draw ();
- // user can only scroll right so sees right indicator
- // Because first column in table is A
- string expected =
- @"
- │A│B│C│
- ├─┼─┼─►
- │1│2│3│
- │1│2│3│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- int? col;
- // ---------------- X=0 -----------------------
- // click is before first cell
- Assert.Null (tableView.ScreenToCell (0, 0, out col));
- Assert.Null (col);
- Assert.Null (tableView.ScreenToCell (0, 1, out col));
- Assert.Null (col);
- Assert.Null (tableView.ScreenToCell (0, 2, out col));
- Assert.Null (col);
- Assert.Null (tableView.ScreenToCell (0, 3, out col));
- Assert.Null (col);
- Assert.Null (tableView.ScreenToCell (0, 4, out col));
- Assert.Null (col);
- // ---------------- X=1 -----------------------
- // click in header
- Assert.Null (tableView.ScreenToCell (1, 0, out col));
- Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
- // click in header row line (click in the horizontal line below header counts as click in header above - consistent with the column hit box)
- Assert.Null (tableView.ScreenToCell (1, 1, out col));
- Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
- // click in cell 0,0
- Assert.Equal (new Point (0, 0), tableView.ScreenToCell (1, 2, out col));
- Assert.Null (col);
- // click in cell 0,1
- Assert.Equal (new Point (0, 1), tableView.ScreenToCell (1, 3, out col));
- Assert.Null (col);
- // after last row
- Assert.Null (tableView.ScreenToCell (1, 4, out col));
- Assert.Null (col);
- // ---------------- X=2 -----------------------
- // click in header
- Assert.Null (tableView.ScreenToCell (2, 0, out col));
- Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
- // click in header row line
- Assert.Null (tableView.ScreenToCell (2, 1, out col));
- Assert.Equal ("A", tableView.Table.ColumnNames [col.Value]);
- // click in cell 0,0
- Assert.Equal (new Point (0, 0), tableView.ScreenToCell (2, 2, out col));
- Assert.Null (col);
- // click in cell 0,1
- Assert.Equal (new Point (0, 1), tableView.ScreenToCell (2, 3, out col));
- Assert.Null (col);
- // after last row
- Assert.Null (tableView.ScreenToCell (2, 4, out col));
- Assert.Null (col);
- // ---------------- X=3 -----------------------
- // click in header
- Assert.Null (tableView.ScreenToCell (3, 0, out col));
- Assert.Equal ("B", tableView.Table.ColumnNames [col.Value]);
- // click in header row line
- Assert.Null (tableView.ScreenToCell (3, 1, out col));
- Assert.Equal ("B", tableView.Table.ColumnNames [col.Value]);
- // click in cell 1,0
- Assert.Equal (new Point (1, 0), tableView.ScreenToCell (3, 2, out col));
- Assert.Null (col);
- // click in cell 1,1
- Assert.Equal (new Point (1, 1), tableView.ScreenToCell (3, 3, out col));
- Assert.Null (col);
- // after last row
- Assert.Null (tableView.ScreenToCell (3, 4, out col));
- Assert.Null (col);
- }
- /// <summary>
- /// Builds a simple list with the requested number of string items
- /// </summary>
- /// <param name="items"></param>
- /// <returns></returns>
- public static IList BuildList (int items)
- {
- var list = new List<string> ();
- for (int i = 0; i < items; i++) {
- list.Add ("Item " + i);
- }
- return list.ToArray ();
- }
- [Theory, AutoInitShutdown]
- [InlineData (new object [] { Orientation.Horizontal, false })]
- [InlineData (new object [] { Orientation.Vertical, false })]
- [InlineData (new object [] { Orientation.Horizontal, true })]
- [InlineData (new object [] { Orientation.Vertical, true })]
- public void TestListTableSource (Orientation orient, bool parallel)
- {
- var list = BuildList (16);
- var tv = new TableView ();
- //tv.BeginInit (); tv.EndInit ();
- tv.ColorScheme = Colors.ColorSchemes ["TopLevel"];
- tv.Bounds = new Rect (0, 0, 25, 4);
- tv.Style = new () {
- ShowHeaders = false,
- ShowHorizontalHeaderOverline = false,
- ShowHorizontalHeaderUnderline = false
- };
- var listStyle = new ListColumnStyle () {
- Orientation = orient,
- ScrollParallel = parallel
- };
- tv.Table = new ListTableSource (list, tv, listStyle);
- tv.LayoutSubviews ();
- tv.Draw ();
- string horizPerpExpected =
- @"
- │Item 0│Item 1 │
- │Item 2│Item 3 │
- │Item 4│Item 5 │
- │Item 6│Item 7 │";
- string horizParaExpected =
- @"
- │Item 0 │Item 1 │Item 2 │
- │Item 4 │Item 5 │Item 6 │
- │Item 8 │Item 9 │Item 10│
- │Item 12│Item 13│Item 14│";
- string vertPerpExpected =
- @"
- │Item 0│Item 4│Item 8 │
- │Item 1│Item 5│Item 9 │
- │Item 2│Item 6│Item 10 │
- │Item 3│Item 7│Item 11 │";
- string vertParaExpected =
- @"
- │Item 0│Item 8 │
- │Item 1│Item 9 │
- │Item 2│Item 10 │
- │Item 3│Item 11 │";
- string expected;
- if (orient == Orientation.Vertical)
- if (parallel) {
- expected = vertParaExpected;
- } else {
- expected = vertPerpExpected;
- }
- else {
- if (parallel) {
- expected = horizParaExpected;
- } else {
- expected = horizPerpExpected;
- }
- }
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TestEnumerableDataSource_BasicTypes ()
- {
- var tv = new TableView ();
- tv.ColorScheme = Colors.ColorSchemes ["TopLevel"];
- tv.Bounds = new Rect (0, 0, 50, 6);
- tv.Table = new EnumerableTableSource<Type> (
- new Type [] { typeof (string), typeof (int), typeof (float) },
- new () {
- { "Name", (t)=>t.Name},
- { "Namespace", (t)=>t.Namespace},
- { "BaseType", (t)=>t.BaseType}
- });
- tv.LayoutSubviews ();
- tv.Draw ();
- string expected =
- @"
- ┌──────┬─────────┬───────────────────────────────┐
- │Name │Namespace│BaseType │
- ├──────┼─────────┼───────────────────────────────┤
- │String│System │System.Object │
- │Int32 │System │System.ValueType │
- │Single│System │System.ValueType │";
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void Test_CollectionNavigator ()
- {
- var tv = new TableView ();
- tv.ColorScheme = Colors.ColorSchemes ["TopLevel"];
- tv.Bounds = new Rect (0, 0, 50, 7);
- tv.Table = new EnumerableTableSource<string> (
- new string [] { "fish", "troll", "trap", "zoo" },
- new () {
- { "Name", (t)=>t},
- { "EndsWith", (t)=>t.Last()}
- });
- tv.LayoutSubviews ();
- tv.Draw ();
- string expected =
- @"
- ┌─────┬──────────────────────────────────────────┐
- │Name │EndsWith │
- ├─────┼──────────────────────────────────────────┤
- │fish │h │
- │troll│l │
- │trap │p │
- │zoo │o │";
- TestHelpers.AssertDriverContentsAre (expected, output);
- Assert.Equal (0, tv.SelectedRow);
- // this test assumes no focus
- Assert.False (tv.HasFocus);
- // already on fish
- tv.NewKeyDownEvent (new Key () { KeyCode = KeyCode.F });
- Assert.Equal (0, tv.SelectedRow);
- // not focused
- tv.NewKeyDownEvent (new Key () { KeyCode = KeyCode.Z });
- Assert.Equal (0, tv.SelectedRow);
- // ensure that TableView has the input focus
- Application.Top.Add (tv);
- Application.Begin (Application.Top);
- Application.Top.FocusFirst ();
- Assert.True (tv.HasFocus);
- // already on fish
- tv.NewKeyDownEvent (new Key () { KeyCode = KeyCode.F });
- Assert.Equal (0, tv.SelectedRow);
- // move to zoo
- tv.NewKeyDownEvent (new Key () { KeyCode = KeyCode.Z });
- Assert.Equal (3, tv.SelectedRow);
- // move to troll
- tv.NewKeyDownEvent (new Key () { KeyCode = KeyCode.T });
- Assert.Equal (1, tv.SelectedRow);
- // move to trap
- tv.NewKeyDownEvent (new Key () { KeyCode = KeyCode.T });
- Assert.Equal (2, tv.SelectedRow);
- // change columns to navigate by column 2
- Assert.Equal (0, tv.SelectedColumn);
- Assert.Equal (2, tv.SelectedRow);
- tv.NewKeyDownEvent (new Key () { KeyCode = KeyCode.CursorRight });
- Assert.Equal (1, tv.SelectedColumn);
- Assert.Equal (2, tv.SelectedRow);
- // nothing ends with t so stay where you are
- tv.NewKeyDownEvent (new Key () { KeyCode = KeyCode.T });
- Assert.Equal (2, tv.SelectedRow);
- //jump to fish which ends in h
- tv.NewKeyDownEvent (new Key () { KeyCode = KeyCode.H });
- Assert.Equal (0, tv.SelectedRow);
- // jump to zoo which ends in o
- tv.NewKeyDownEvent (new Key () { KeyCode = KeyCode.O });
- Assert.Equal (3, tv.SelectedRow);
- }
- private TableView GetTwoRowSixColumnTable ()
- {
- return GetTwoRowSixColumnTable (out _);
- }
- private TableView GetTwoRowSixColumnTable (out DataTable dt)
- {
- var tableView = new TableView ();
- tableView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
- // 3 columns are visible
- tableView.Bounds = new Rect (0, 0, 7, 5);
- tableView.Style.ShowHorizontalHeaderUnderline = true;
- tableView.Style.ShowHorizontalHeaderOverline = false;
- tableView.Style.AlwaysShowHeaders = true;
- tableView.Style.SmoothHorizontalScrolling = true;
- dt = new DataTable ();
- dt.Columns.Add ("A");
- dt.Columns.Add ("B");
- dt.Columns.Add ("C");
- dt.Columns.Add ("D");
- dt.Columns.Add ("E");
- dt.Columns.Add ("F");
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tableView.Table = new DataTableSource (dt);
- return tableView;
- }
- private class PickablePet {
- public bool IsPicked { get; set; }
- public string Name { get; set; }
- public string Kind { get; set; }
- public PickablePet (bool isPicked, string name, string kind)
- {
- IsPicked = isPicked;
- Name = name;
- Kind = kind;
- }
- }
- private TableView GetPetTable (out EnumerableTableSource<PickablePet> source)
- {
- var tv = new TableView ();
- tv.ColorScheme = Colors.ColorSchemes ["TopLevel"];
- tv.Bounds = new Rect (0, 0, 25, 6);
- var pets = new List<PickablePet> {
- new PickablePet(false,"Tammy","Cat"),
- new PickablePet(false,"Tibbles","Cat"),
- new PickablePet(false,"Ripper","Dog")};
- tv.Table = source = new EnumerableTableSource<PickablePet> (
- pets,
- new () {
- { "Name", (p) => p.Name},
- { "Kind", (p) => p.Kind},
- });
- tv.LayoutSubviews ();
- return tv;
- }
- }
- }
|