123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845 |
- 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;
- namespace Terminal.Gui.Views {
- 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 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.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);
- tableView.Redraw(tableView.Bounds);
- tableView.Table.Columns.Remove(tableView.Table.Columns[0]);
- tableView.Redraw(tableView.Bounds);
- }
- [Fact]
- public void SelectedCellChanged_NotFiredForSameValue ()
- {
- var tableView = new TableView () {
- Table = BuildTable (25, 50)
- };
- bool called = false;
- tableView.SelectedCellChanged += (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 += (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 += (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".Sum (c => Rune.ColumnWidth (c)));
- // Creates a string with the peculiar (french?) r symbol
- String 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.Sum (c => Rune.ColumnWidth (c)));
- 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.Top.FocusFirst ();
- Assert.True (tableView.HasFocus);
- Assert.Equal (0, tableView.RowOffset);
- tableView.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ()));
- // 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.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ()));
- 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),
- MultiSelect = true,
- Bounds = new Rect (0, 0, 10, 5)
- };
- tableView.SelectAll ();
- Assert.Equal (16, tableView.GetAllSelectedCells ().Count ());
- // delete one of the columns
- tableView.Table.Columns.RemoveAt (2);
- // table should now be 3x4
- Assert.Equal (12, tableView.GetAllSelectedCells ().Count ());
- // remove a row
- tableView.Table.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),
- MultiSelect = true,
- Bounds = new Rect (0, 0, 10, 5)
- };
- // select the last row
- tableView.MultiSelectedRegions.Clear ();
- tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (0, 3), new Rect (0, 3, 4, 1)));
- Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
- // remove a row
- tableView.Table.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.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)
- };
- // 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)
- };
- // 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)
- };
- /*
- Sets up disconnected selections like:
- 00000000000
- 01100000000
- 01100000000
- 00000001100
- 00000000000
- */
- tableView.MultiSelectedRegions.Clear ();
- tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (1, 1), new Rect (1, 1, 2, 2)));
- tableView.MultiSelectedRegions.Push (new TableView.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]
- public void TableView_ExpandLastColumn_True ()
- {
- var tv = SetUpMiniTable ();
- // the thing we are testing
- tv.Style.ExpandLastColumn = true;
- tv.Redraw (tv.Bounds);
- 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]
- public void TableView_ExpandLastColumn_False ()
- {
- var tv = SetUpMiniTable ();
- // the thing we are testing
- tv.Style.ExpandLastColumn = false;
- tv.Redraw (tv.Bounds);
- 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]
- 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.Redraw (tv.Bounds);
- 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 += (c) => activatedValue = c.Table.Rows[c.Row][c.Col].ToString();
- Application.Top.Add (tv);
- Application.Begin (Application.Top);
- // pressing enter should activate the first cell (selected cell)
- tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
- Assert.Equal ("R0C0",activatedValue);
- // reset the test
- activatedValue = null;
- // clear keybindings and ensure that Enter does not trigger the event anymore
- tv.ClearKeybindings ();
- tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
- Assert.Null(activatedValue);
- // New method for changing the activation key
- tv.AddKeyBinding (Key.z, Command.Accept);
- tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ()));
- Assert.Equal ("R0C0", activatedValue);
- // reset the test
- activatedValue = null;
- tv.ClearKeybindings ();
- // Old method for changing the activation key
- tv.CellActivationKey = Key.z;
- tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ()));
- Assert.Equal ("R0C0", activatedValue);
- }
- [Fact]
- public void TableViewMultiSelect_CannotFallOffLeft()
- {
- var tv = SetUpMiniTable ();
- tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
- tv.MultiSelect = true;
- tv.SelectedColumn = 1;
- tv.SelectedRow = 1;
- tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
- 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.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
- 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]
- public void TableViewMultiSelect_CannotFallOffRight()
- {
- var tv = SetUpMiniTable ();
- tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
- tv.MultiSelect = true;
- tv.SelectedColumn = 0;
- tv.SelectedRow = 1;
- tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
- 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.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
- 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]
- public void TableViewMultiSelect_CannotFallOffBottom ()
- {
- var tv = SetUpMiniTable ();
- tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
- tv.MultiSelect = true;
- tv.SelectedColumn = 0;
- tv.SelectedRow = 0;
- tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
- tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
- 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.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
- tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
- 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]
- public void TableViewMultiSelect_CannotFallOffTop()
- {
- var tv = SetUpMiniTable ();
- tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
- tv.MultiSelect = true;
- tv.SelectedColumn = 1;
- tv.SelectedRow = 1;
- tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
- tv.ProcessKey (new KeyEvent (Key.CursorUp | Key.ShiftMask, new KeyModifiers { Shift = true }));
- 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.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
- tv.ProcessKey (new KeyEvent (Key.CursorUp | Key.ShiftMask, new KeyModifiers { Shift = true }));
- Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
- Assert.Equal (0, tv.SelectedColumn);
- Assert.Equal (0, tv.SelectedRow);
- Application.Shutdown ();
- }
- [Theory]
- [InlineData (false)]
- [InlineData (true)]
- public void TableView_ColorTests_FocusedOrNot (bool focused)
- {
- var tv = SetUpMiniTable ();
- // 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.Redraw (tv.Bounds);
- string expected = @"
- ┌─┬─┐
- │A│B│
- ├─┼─┤
- │1│2│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- string expectedColors = @"
- 00000
- 00000
- 00000
- 01000
- ";
-
- TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
- // 0
- tv.ColorScheme.Normal,
- // 1
- focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal});
- Application.Shutdown();
- }
- [Theory]
- [InlineData (false)]
- [InlineData (true)]
- public void TableView_ColorTests_InvertSelectedCellFirstCharacter (bool focused)
- {
- var tv = SetUpMiniTable ();
- tv.Style.InvertSelectedCellFirstCharacter = true;
- // 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.Redraw (tv.Bounds);
- string expected = @"
- ┌─┬─┐
- │A│B│
- ├─┼─┤
- │1│2│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- string expectedColors = @"
- 00000
- 00000
- 00000
- 01000
- ";
-
- var invertHotFocus = new Attribute(tv.ColorScheme.HotFocus.Background,tv.ColorScheme.HotFocus.Foreground);
- var invertHotNormal = new Attribute(tv.ColorScheme.HotNormal.Background,tv.ColorScheme.HotNormal.Foreground);
- TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
- // 0
- tv.ColorScheme.Normal,
- // 1
- focused ? invertHotFocus : invertHotNormal});
-
- Application.Shutdown();
- }
- [Theory]
- [InlineData (false)]
- [InlineData (true)]
- public void TableView_ColorsTest_RowColorGetter (bool focused)
- {
- var tv = SetUpMiniTable ();
- // width exactly matches the max col widths
- tv.Bounds = new Rect (0, 0, 5, 4);
- var rowHighlight = new ColorScheme () {
- Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
- HotNormal = Attribute.Make (Color.Green, Color.Blue),
- HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
- Focus = Attribute.Make (Color.Cyan, Color.Magenta),
- };
- // when B is 2 use the custom highlight colour for the row
- tv.Style.RowColorGetter += (e)=>Convert.ToInt32(e.Table.Rows[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.Redraw (tv.Bounds);
- string expected = @"
- ┌─┬─┐
- │A│B│
- ├─┼─┤
- │1│2│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- string expectedColors = @"
- 00000
- 00000
- 00000
- 21222
- ";
-
- TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
- // 0
- tv.ColorScheme.Normal,
- // 1
- focused ? rowHighlight.HotFocus : 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)
- tv.Table.Rows[0][1] = 5;
- tv.Redraw (tv.Bounds);
- 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.AssertDriverColorsAre (expectedColors, new Attribute [] {
- // 0
- tv.ColorScheme.Normal,
- // 1
- focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Theory]
- [InlineData (false)]
- [InlineData (true)]
- public void TableView_ColorsTest_ColorGetter (bool focused)
- {
- var tv = SetUpMiniTable ();
- // 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 (tv.Table.Columns ["B"]);
- // when B is 2 use the custom highlight colour
- var cellHighlight = new ColorScheme () {
- Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
- HotNormal = Attribute.Make (Color.Green, Color.Blue),
- HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
- Focus = Attribute.Make (Color.Cyan, Color.Magenta),
- };
- 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.Redraw (tv.Bounds);
- string expected = @"
- ┌─┬─┐
- │A│B│
- ├─┼─┤
- │1│2│
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- string expectedColors = @"
- 00000
- 00000
- 00000
- 01020
- ";
-
- TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
- // 0
- tv.ColorScheme.Normal,
- // 1
- focused ? tv.ColorScheme.HotFocus : 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)
- tv.Table.Rows[0][1] = 5;
- tv.Redraw (tv.Bounds);
- 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.AssertDriverColorsAre (expectedColors, new Attribute [] {
- // 0
- tv.ColorScheme.Normal,
- // 1
- focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- private TableView SetUpMiniTable ()
- {
- var tv = new TableView ();
- tv.Bounds = new Rect (0, 0, 10, 4);
- var dt = new DataTable ();
- var colA = dt.Columns.Add ("A");
- var colB = dt.Columns.Add ("B");
- dt.Rows.Add (1, 2);
- tv.Table = dt;
- tv.Style.GetOrCreateColumnStyle (colA).MinWidth = 1;
- tv.Style.GetOrCreateColumnStyle (colA).MinWidth = 1;
- tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
- tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
- GraphViewTests.InitFakeDriver ();
- tv.ColorScheme = Colors.Base;
- return tv;
- }
- [Fact]
- [AutoInitShutdown]
- public void ScrollDown_OneLineAtATime ()
- {
- var tableView = new TableView ();
- // 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.ProcessKey (new KeyEvent () { Key = Key.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]
- public void ScrollRight_SmoothScrolling ()
- {
- GraphViewTests.InitFakeDriver ();
- var tableView = new TableView ();
- tableView.ColorScheme = Colors.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 = 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 = dt;
- // select last visible column
- tableView.SelectedColumn = 2; // column C
- tableView.Redraw (tableView.Bounds);
- string expected =
- @"
- │A│B│C│
- │1│2│3│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Scroll right
- tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
- tableView.Redraw (tableView.Bounds);
- // 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]
- public void ScrollRight_WithoutSmoothScrolling ()
- {
- GraphViewTests.InitFakeDriver ();
- var tableView = new TableView ();
- tableView.ColorScheme = Colors.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 = dt;
- // select last visible column
- tableView.SelectedColumn = 2; // column C
- tableView.Redraw (tableView.Bounds);
- string expected =
- @"
- │A│B│C│
- │1│2│3│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Scroll right
- tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
- tableView.Redraw (tableView.Bounds);
- // 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.ColorScheme = Colors.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 = dt;
- return tableView;
- }
- [Fact, AutoInitShutdown]
- public void TestColumnStyle_VisibleFalse_IsNotRendered()
- {
- var tableView = GetABCDEFTableView (out DataTable dt);
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
- tableView.Redraw (tableView.Bounds);
- 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 (dt.Columns ["A"]).Visible = false;
- tableView.Redraw (tableView.Bounds);
- 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);
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["C"]).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
- // expect nothing to be rendered when all columns are invisible
- string expected =
- @"
- ";
- tableView.Redraw (tableView.Bounds);
- TestHelpers.AssertDriverContentsAre (expected, output);
- // expect behavior to match when Table is null
- tableView.Table = null;
- tableView.Redraw (tableView.Bounds);
- 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.Redraw (tableView.Bounds);
- // 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 (dt.Columns ["D"]).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
- expected =
- @"
- │A│B│C│
- ├─┼─┼─┤
- │1│2│3│";
- tableView.Redraw (tableView.Bounds);
- 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.Redraw (tableView.Bounds);
- // 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 (dt.Columns ["E"]).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
- expected =
- @"
- │B│C│D│
- ◄─┼─┼─┤
- │2│3│4│";
- tableView.Redraw (tableView.Bounds);
- TestHelpers.AssertDriverContentsAre (expected, output);
- // now also A is invisible so we cannot scroll in either direction
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).Visible = false;
- expected =
- @"
- │B│C│D│
- ├─┼─┼─┤
- │2│3│4│";
- tableView.Redraw (tableView.Bounds);
- TestHelpers.AssertDriverContentsAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void TestColumnStyle_VisibleFalse_CursorStepsOverInvisibleColumns ()
- {
- var tableView = GetABCDEFTableView (out var dt);
-
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
- tableView.SelectedColumn = 0;
- tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight });
- // Expect the cursor navigation to skip over the invisible column(s)
- Assert.Equal(2,tableView.SelectedColumn);
- tableView.ProcessKey (new KeyEvent { Key = Key.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.Style.GetOrCreateColumnStyle (dt.Columns ["A"]).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.ProcessKey (new KeyEvent
- {
- Key = useHome ? Key.Home : Key.CursorLeft
- });
- // Expect the cursor to stay at 1
- Assert.Equal (1, tableView.SelectedColumn);
- }
- [InlineData (true)]
- [InlineData (false)]
- [Theory, AutoInitShutdown]
- public void TestColumnStyle_LastColumnVisibleFalse_CursorStaysAt2 (bool useEnd)
- {
- var tableView = GetABCDEFTableView (out var dt);
-
- // select D
- tableView.SelectedColumn = 3;
- Assert.Equal (3, tableView.SelectedColumn);
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["E"]).Visible = false;
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["F"]).Visible = false;
- // column D is invisible so this method should move to 2 (C)
- tableView.EnsureValidSelection ();
- Assert.Equal (2, tableView.SelectedColumn);
- tableView.ProcessKey (new KeyEvent {
- Key = useEnd ? Key.End : Key.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);
- // user has rectangular selection
- tableView.MultiSelectedRegions.Push (
- new TableView.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 (dt.Columns ["B"]).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 var dt);
- // if middle column is invisible
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["B"]).Visible = false;
- tableView.ProcessKey (new KeyEvent { Key = Key.CursorRight | Key.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 ());
- }
-
- [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.Style.SmoothHorizontalScrolling = smooth;
-
- if(invisibleCol) {
- tableView.Style.GetOrCreateColumnStyle (dt.Columns ["D"]).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]
- public void LongColumnTest ()
- {
- GraphViewTests.InitFakeDriver ();
- var tableView = new TableView ();
- tableView.ColorScheme = Colors.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 = dt;
- tableView.Redraw (tableView.Bounds);
- // 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(dt.Columns[2]);
-
- // one way the API user can fix this for long columns
- // is to specify a max width for the column
- style.MaxWidth = 10;
- tableView.Redraw (tableView.Bounds);
- expected =
- @"
- │A│B│Very Long │
- ├─┼─┼───────────────────┤
- │1│2│aaaaaaaaaa │
- │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.Redraw (tableView.Bounds);
- 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.Redraw (tableView.Bounds);
- 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
- Application.Shutdown ();
- GraphViewTests.InitFakeDriver ();
- tableView.Bounds = new Rect(0,0,9,5);
- tableView.Redraw (tableView.Bounds);
- 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.Redraw (tableView.Bounds);
- expected =
- @"
- │A│B│Very│
- ├─┼─┼────┤
- │1│2│aaaa│
- │1│2│aaa │
- ";
- TestHelpers.AssertDriverContentsAre (expected, output);
- Application.Shutdown ();
- }
- [Fact]
- public void ScrollIndicators ()
- {
- GraphViewTests.InitFakeDriver ();
- var tableView = new TableView ();
- tableView.ColorScheme = Colors.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 = dt;
- // select last visible column
- tableView.SelectedColumn = 2; // column C
- tableView.Redraw (tableView.Bounds);
- // 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.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
- // since A is now pushed off screen we get indicator showing
- // that user can scroll left to see first column
- tableView.Redraw (tableView.Bounds);
- expected =
- @"
- │B│C│D│
- ◄─┼─┼─►
- │2│3│4│";
- TestHelpers.AssertDriverContentsAre (expected, output);
- // Scroll right twice more (to end of columns)
- tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
- tableView.ProcessKey (new KeyEvent () { Key = Key.CursorRight });
- tableView.Redraw (tableView.Bounds);
- 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 ();
- }
- /// <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 DataTable BuildTable (int cols, int rows)
- {
- var 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 dt;
- }
- [Fact, AutoInitShutdown]
- public void Test_ScreenToCell ()
- {
- var tableView = GetTwoRowSixColumnTable ();
- tableView.Redraw (tableView.Bounds);
- // 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.Redraw (tableView.Bounds);
- // 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);
- DataColumn 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", col.ColumnName);
- // 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", col.ColumnName);
- // 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", col.ColumnName);
- // click in header row line
- Assert.Null (tableView.ScreenToCell (2, 1, out col));
- Assert.Equal ("A", col.ColumnName);
- // 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", col.ColumnName);
- // click in header row line
- Assert.Null (tableView.ScreenToCell (3, 1, out col));
- Assert.Equal ("B", col.ColumnName);
- // 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);
- }
- private TableView GetTwoRowSixColumnTable ()
- {
- var tableView = new TableView ();
- tableView.ColorScheme = Colors.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;
- 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);
- dt.Rows.Add (1, 2, 3, 4, 5, 6);
- tableView.Table = dt;
- return tableView;
- }
- }
- }
|