1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504 |
- using System.Globalization;
- using Xunit.Abstractions;
- namespace Terminal.Gui.ViewsTests;
- public class TabViewTests (ITestOutputHelper output)
- {
- [Fact]
- public void AddTab_SameTabMoreThanOnce ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- Assert.Equal (2, tv.Tabs.Count);
- // Tab is already part of the control so shouldn't result in duplication
- tv.AddTab (tab1, false);
- tv.AddTab (tab1, false);
- tv.AddTab (tab1, false);
- tv.AddTab (tab1, false);
- Assert.Equal (2, tv.Tabs.Count);
- }
- [Fact]
- [SetupFakeDriver]
- public void AddTwoTabs_SecondIsSelected ()
- {
- var tv = new TabView ();
- Tab tab1;
- Tab tab2;
- tv.AddTab (tab1 = new () { DisplayText = "Tab1", View = new TextField { Text = "hi" } }, false);
- tv.AddTab (tab2 = new () { DisplayText = "Tab1", View = new Label { Text = "hi2" } }, true);
- Assert.Equal (2, tv.Tabs.Count);
- Assert.Equal (tab2, tv.SelectedTab);
- }
- [Fact]
- public void EnsureSelectedTabVisible_MustScroll ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- // Make tab width small to force only one tab visible at once
- tv.Width = 4;
- tv.SelectedTab = tab1;
- Assert.Equal (0, tv.TabScrollOffset);
- tv.EnsureSelectedTabIsVisible ();
- Assert.Equal (0, tv.TabScrollOffset);
- // Asking to show tab2 should automatically move scroll offset accordingly
- tv.SelectedTab = tab2;
- Assert.Equal (1, tv.TabScrollOffset);
- }
- [Fact]
- public void EnsureSelectedTabVisible_NullSelect ()
- {
- TabView tv = GetTabView ();
- tv.SelectedTab = null;
- Assert.Null (tv.SelectedTab);
- Assert.Equal (0, tv.TabScrollOffset);
- tv.EnsureSelectedTabIsVisible ();
- Assert.Null (tv.SelectedTab);
- Assert.Equal (0, tv.TabScrollOffset);
- }
- [Fact]
- public void EnsureValidScrollOffsets_TabScrollOffset ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- // Make tab width small to force only one tab visible at once
- tv.Width = 4;
- tv.SelectedTab = tab1;
- Assert.Equal (0, tv.TabScrollOffset);
- tv.TabScrollOffset = 10;
- tv.SelectedTab = tab2;
- Assert.Equal (1, tv.TabScrollOffset);
- tv.TabScrollOffset = -1;
- tv.SelectedTab = tab1;
- Assert.Equal (0, tv.TabScrollOffset);
- }
- [Fact]
- [AutoInitShutdown]
- public void MouseClick_ChangesTab ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.Width = 20;
- tv.Height = 5;
- tv.Draw ();
- View tabRow = tv.Subviews [0];
- Assert.Equal ("TabRow", tabRow.GetType ().Name);
- TestHelpers.AssertDriverContentsAre (
- @"
- ╭────┬────╮
- │Tab1│Tab2│
- │ ╰────┴────────╮
- │hi │
- └──────────────────┘
- ",
- output
- );
- Tab clicked = null;
- tv.TabClicked += (s, e) => { clicked = e.Tab; };
- var top = new Toplevel ();
- top.Add (tv);
- Application.Begin (top);
- MouseEventArgs args;
- // Waving mouse around does not trigger click
- for (var i = 0; i < 100; i++)
- {
- args = new () { ScreenPosition = new (i, 1), Flags = MouseFlags.ReportMousePosition };
- Application.RaiseMouseEvent (args);
- Application.LayoutAndDraw ();
- Assert.Null (clicked);
- Assert.Equal (tab1, tv.SelectedTab);
- }
- args = new () { ScreenPosition = new (3, 1), Flags = MouseFlags.Button1Clicked };
- Application.RaiseMouseEvent (args);
- Application.LayoutAndDraw ();
- Assert.Equal (tab1, clicked);
- Assert.Equal (tab1, tv.SelectedTab);
- // Click to tab2
- args = new () { ScreenPosition = new (6, 1), Flags = MouseFlags.Button1Clicked };
- Application.RaiseMouseEvent (args);
- Application.LayoutAndDraw ();
- Assert.Equal (tab2, clicked);
- Assert.Equal (tab2, tv.SelectedTab);
- // cancel navigation
- tv.TabClicked += (s, e) =>
- {
- clicked = e.Tab;
- e.MouseEvent.Handled = true;
- };
- args = new () { ScreenPosition = new (3, 1), Flags = MouseFlags.Button1Clicked };
- Application.RaiseMouseEvent (args);
- Application.LayoutAndDraw ();
- // Tab 1 was clicked but event handler blocked navigation
- Assert.Equal (tab1, clicked);
- Assert.Equal (tab2, tv.SelectedTab);
- args = new () { ScreenPosition = new (12, 1), Flags = MouseFlags.Button1Clicked };
- Application.RaiseMouseEvent (args);
- Application.LayoutAndDraw ();
- // Clicking beyond last tab should raise event with null Tab
- Assert.Null (clicked);
- Assert.Equal (tab2, tv.SelectedTab);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void MouseClick_Right_Left_Arrows_ChangesTab ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.Width = 7;
- tv.Height = 5;
- tv.Draw ();
- View tabRow = tv.Subviews [0];
- Assert.Equal ("TabRow", tabRow.GetType ().Name);
- TestHelpers.AssertDriverContentsAre (
- @"
- ╭────╮
- │Tab1│
- │ ╰►
- │hi │
- └─────┘
- ",
- output
- );
- Tab clicked = null;
- tv.TabClicked += (s, e) => { clicked = e.Tab; };
- Tab oldChanged = null;
- Tab newChanged = null;
- tv.SelectedTabChanged += (s, e) =>
- {
- oldChanged = e.OldTab;
- newChanged = e.NewTab;
- };
- var top = new Toplevel ();
- top.Add (tv);
- Application.Begin (top);
- // Click the right arrow
- var args = new MouseEventArgs { ScreenPosition = new (6, 2), Flags = MouseFlags.Button1Clicked };
- Application.RaiseMouseEvent (args);
- Application.LayoutAndDraw ();
- Assert.Null (clicked);
- Assert.Equal (tab1, oldChanged);
- Assert.Equal (tab2, newChanged);
- Assert.Equal (tab2, tv.SelectedTab);
- TestHelpers.AssertDriverContentsAre (
- @"
- ╭────╮
- │Tab2│
- ◄ ╰╮
- │hi2 │
- └─────┘
- ",
- output
- );
- // Click the left arrow
- args = new () { ScreenPosition = new (0, 2), Flags = MouseFlags.Button1Clicked };
- Application.RaiseMouseEvent (args);
- Application.LayoutAndDraw ();
- Assert.Null (clicked);
- Assert.Equal (tab2, oldChanged);
- Assert.Equal (tab1, newChanged);
- Assert.Equal (tab1, tv.SelectedTab);
- TestHelpers.AssertDriverContentsAre (
- @"
- ╭────╮
- │Tab1│
- │ ╰►
- │hi │
- └─────┘
- ",
- output
- );
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void MouseClick_Right_Left_Arrows_ChangesTab_With_Border ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.Width = 9;
- tv.Height = 7;
- Assert.Equal (LineStyle.None, tv.BorderStyle);
- tv.BorderStyle = LineStyle.Single;
- tv.Layout ();
- tv.Draw ();
- View tabRow = tv.Subviews [0];
- Assert.Equal ("TabRow", tabRow.GetType ().Name);
- TestHelpers.AssertDriverContentsAre (
- @"
- ┌───────┐
- │╭────╮ │
- ││Tab1│ │
- ││ ╰►│
- ││hi ││
- │└─────┘│
- └───────┘
- ",
- output
- );
- Tab clicked = null;
- tv.TabClicked += (s, e) => { clicked = e.Tab; };
- Tab oldChanged = null;
- Tab newChanged = null;
- tv.SelectedTabChanged += (s, e) =>
- {
- oldChanged = e.OldTab;
- newChanged = e.NewTab;
- };
- var top = new Toplevel ();
- top.Add (tv);
- Application.Begin (top);
- // Click the right arrow
- var args = new MouseEventArgs { ScreenPosition = new (7, 3), Flags = MouseFlags.Button1Clicked };
- Application.RaiseMouseEvent (args);
- Application.LayoutAndDraw ();
- Assert.Null (clicked);
- Assert.Equal (tab1, oldChanged);
- Assert.Equal (tab2, newChanged);
- Assert.Equal (tab2, tv.SelectedTab);
- TestHelpers.AssertDriverContentsAre (
- @"
- ┌───────┐
- │╭────╮ │
- ││Tab2│ │
- │◄ ╰╮│
- ││hi2 ││
- │└─────┘│
- └───────┘
- ",
- output
- );
- // Click the left arrow
- args = new () { ScreenPosition = new (1, 3), Flags = MouseFlags.Button1Clicked };
- Application.RaiseMouseEvent (args);
- Application.LayoutAndDraw ();
- Assert.Null (clicked);
- Assert.Equal (tab2, oldChanged);
- Assert.Equal (tab1, newChanged);
- Assert.Equal (tab1, tv.SelectedTab);
- TestHelpers.AssertDriverContentsAre (
- @"
- ┌───────┐
- │╭────╮ │
- ││Tab1│ │
- ││ ╰►│
- ││hi ││
- │└─────┘│
- └───────┘
- ",
- output
- );
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void ProcessKey_Down_Up_Right_Left_Home_End_PageDown_PageUp_F6 ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.Width = 7;
- tv.Height = 5;
- var btn = new Button
- {
- Id = "btn",
- Y = Pos.Bottom (tv) + 1,
- Height = 1,
- Width = 7,
- Text = "Ok"
- };
- Toplevel top = new ();
- top.Add (tv, btn);
- Application.Begin (top);
- // Is the selected tab view hosting focused
- Assert.Equal (tab1, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- Assert.Equal (tv.SelectedTab.View, top.Focused.MostFocused);
- // Press the cursor up key to focus the selected tab
- Assert.True (Application.RaiseKeyDownEvent (Key.CursorUp));
- Application.LayoutAndDraw ();
- // Is the selected tab focused
- Assert.Equal (tab1, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- Tab oldChanged = null;
- Tab newChanged = null;
- tv.SelectedTabChanged += (s, e) =>
- {
- oldChanged = e.OldTab;
- newChanged = e.NewTab;
- };
- // Press the cursor right key to select the next tab
- Assert.True (Application.RaiseKeyDownEvent (Key.CursorRight));
- Application.LayoutAndDraw ();
- Assert.Equal (tab1, oldChanged);
- Assert.Equal (tab2, newChanged);
- Assert.Equal (tab2, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- // Press the cursor down key. Since the selected tab has no focusable views, the focus should move to the next view in the toplevel
- Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
- Assert.Equal (tab2, tv.SelectedTab);
- Assert.Equal (btn, top.MostFocused);
- // Add a focusable subview to Selected Tab View which is a Label with CanFocus as false
- var btnSubView = new View ()
- {
- Id = "btnSubView",
- Title = "_Subview",
- CanFocus = true
- };
- tv.SelectedTab.View.Add (btnSubView);
- Assert.False (tv.SelectedTab.View.CanFocus);
- // Press cursor up. Should focus the subview in the selected tab.
- Assert.True (Application.RaiseKeyDownEvent (Key.CursorUp));
- Assert.Equal (tab2, tv.SelectedTab);
- Assert.NotEqual (btnSubView, top.MostFocused);
- Assert.Equal (tab2, top.MostFocused);
- tv.SelectedTab.View.CanFocus = true;
- Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
- Assert.Equal (tab2, tv.SelectedTab);
- Assert.Equal (btnSubView, top.MostFocused);
- Assert.True (Application.RaiseKeyDownEvent (Key.CursorUp));
- // TabRow now has TabGroup which only F6 is allowed
- Assert.NotEqual (tab2, top.MostFocused);
- Assert.Equal (btn, top.MostFocused);
- Assert.True (Application.RaiseKeyDownEvent (Key.CursorUp));
- Assert.Equal (btnSubView, top.MostFocused);
- Assert.True (Application.RaiseKeyDownEvent (Key.F6));
- Assert.Equal (tab2, top.MostFocused);
- // Press the cursor down key twice.
- Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
- Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
- Assert.Equal (btn, top.MostFocused);
- // Press the cursor down key again will focus next view in the toplevel, which is the TabView
- Assert.True (Application.RaiseKeyDownEvent (Key.CursorDown));
- Assert.Equal (tab2, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- // Due to the RestoreFocus method prioritize the _previouslyFocused, so btnSubView will be focused again
- Assert.Equal (btnSubView, tv.MostFocused);
- // Press the cursor up key to focus the selected tab which it's the only way to do that
- Assert.True (Application.RaiseKeyDownEvent (Key.CursorUp));
- Assert.Equal (tab2, tv.SelectedTab);
- Assert.Equal (btn, top.Focused);
- Assert.True (Application.RaiseKeyDownEvent (Key.CursorUp));
- Assert.Equal (tv, top.Focused);
- Assert.Equal (btnSubView, top.MostFocused);
- Assert.True (Application.RaiseKeyDownEvent (Key.F6));
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tab2, top.Focused.MostFocused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- // Press the cursor left key to select the previous tab
- Assert.True (Application.RaiseKeyDownEvent (Key.CursorLeft));
- Application.LayoutAndDraw ();
- Assert.Equal (tab2, oldChanged);
- Assert.Equal (tab1, newChanged);
- Assert.Equal (tab1, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- Assert.Equal (tab1, top.Focused.MostFocused);
- // Press the end key to select the last tab
- Assert.True (Application.RaiseKeyDownEvent (Key.End));
- Application.LayoutAndDraw ();
- Assert.Equal (tab1, oldChanged);
- Assert.Equal (tab2, newChanged);
- Assert.Equal (tab2, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- // Press the home key to select the first tab
- Assert.True (Application.RaiseKeyDownEvent (Key.Home));
- Application.LayoutAndDraw ();
- Assert.Equal (tab2, oldChanged);
- Assert.Equal (tab1, newChanged);
- Assert.Equal (tab1, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- // Press the page down key to select the next set of tabs
- Assert.True (Application.RaiseKeyDownEvent (Key.PageDown));
- Application.LayoutAndDraw ();
- Assert.Equal (tab1, oldChanged);
- Assert.Equal (tab2, newChanged);
- Assert.Equal (tab2, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- // Press the page up key to select the previous set of tabs
- Assert.True (Application.RaiseKeyDownEvent (Key.PageUp));
- Application.LayoutAndDraw ();
- Assert.Equal (tab2, oldChanged);
- Assert.Equal (tab1, newChanged);
- Assert.Equal (tab1, tv.SelectedTab);
- Assert.Equal (tv, top.Focused);
- Assert.Equal (tv.MostFocused, top.Focused.MostFocused);
- top.Dispose ();
- }
- [Fact]
- public void RemoveAllTabs_ClearsSelection ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.SelectedTab = tab1;
- tv.RemoveTab (tab1);
- tv.RemoveTab (tab2);
- Assert.Null (tv.SelectedTab);
- }
- [Fact]
- public void RemoveTab_ChangesSelection ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.SelectedTab = tab1;
- tv.RemoveTab (tab1);
- Assert.Equal (tab2, tv.SelectedTab);
- }
- [Fact]
- public void RemoveTab_MultipleCalls_NotAnError ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.SelectedTab = tab1;
- // Repeated calls to remove a tab that is not part of
- // the collection should be ignored
- tv.RemoveTab (tab1);
- tv.RemoveTab (tab1);
- tv.RemoveTab (tab1);
- tv.RemoveTab (tab1);
- Assert.Equal (tab2, tv.SelectedTab);
- }
- [Fact]
- public void SelectedTabChanged_Called ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.SelectedTab = tab1;
- Tab oldTab = null;
- Tab newTab = null;
- var called = 0;
- tv.SelectedTabChanged += (s, e) =>
- {
- oldTab = e.OldTab;
- newTab = e.NewTab;
- called++;
- };
- tv.SelectedTab = tab2;
- Assert.Equal (1, called);
- Assert.Equal (tab1, oldTab);
- Assert.Equal (tab2, newTab);
- }
- [Fact]
- [SetupFakeDriver]
- public void ShowTopLine_False_TabsOnBottom_False_TestTabView_Width3 ()
- {
- TabView tv = GetTabView (out _, out _);
- tv.Width = 3;
- tv.Height = 5;
- tv.Style = new () { ShowTopLine = false };
- tv.ApplyStyleChanges ();
- tv.Layout ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ││
- │╰►
- │h│
- │ │
- └─┘",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ShowTopLine_False_TabsOnBottom_False_TestTabView_Width4 ()
- {
- TabView tv = GetTabView (out _, out _);
- tv.Width = 4;
- tv.Height = 5;
- tv.Style = new () { ShowTopLine = false };
- tv.ApplyStyleChanges ();
- tv.Layout ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- │T│
- │ ╰►
- │hi│
- │ │
- └──┘",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ShowTopLine_False_TabsOnBottom_False_TestThinTabView_WithLongNames ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.Width = 10;
- tv.Height = 5;
- tv.Style = new () { ShowTopLine = false };
- tv.ApplyStyleChanges ();
- // Test two tab names that fit
- tab1.DisplayText = "12";
- tab2.DisplayText = "13";
- // Ensures that the tab bar subview gets the bounds of the parent TabView
- tv.Layout ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- │12│13│
- │ ╰──┴──╮
- │hi │
- │ │
- └────────┘",
- output
- );
- tv.SelectedTab = tab2;
- Assert.Equal (tab2, tv.Subviews.First (v => v.Id.Contains ("tabRow")).MostFocused);
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- │12│13│
- ├──╯ ╰──╮
- │hi2 │
- │ │
- └────────┘",
- output
- );
- tv.SelectedTab = tab1;
- // Test first tab name too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "13";
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- │1234567│
- │ ╰►
- │hi │
- │ │
- └────────┘",
- output
- );
- //switch to tab2
- tv.SelectedTab = tab2;
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- │13│
- ◄ ╰─────╮
- │hi2 │
- │ │
- └────────┘",
- output
- );
- // now make both tabs too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "abcdefghijklmnopq";
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- │abcdefg│
- ◄ ╰╮
- │hi2 │
- │ │
- └────────┘",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ShowTopLine_False_TabsOnBottom_True_TestTabView_Width3 ()
- {
- TabView tv = GetTabView (out _, out _);
- tv.Width = 3;
- tv.Height = 5;
- tv.Style = new () { ShowTopLine = false, TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.Layout ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌─┐
- │h│
- │ │
- │╭►
- ││ ",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ShowTopLine_False_TabsOnBottom_True_TestTabView_Width4 ()
- {
- TabView tv = GetTabView (out _, out _);
- tv.Width = 4;
- tv.Height = 5;
- tv.Style = new () { ShowTopLine = false, TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.Layout ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌──┐
- │hi│
- │ │
- │ ╭►
- │T│ ",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ShowTopLine_False_TabsOnBottom_True_TestThinTabView_WithLongNames ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.Width = 10;
- tv.Height = 5;
- tv.Style = new () { ShowTopLine = false, TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.Layout ();
- // Test two tab names that fit
- tab1.DisplayText = "12";
- tab2.DisplayText = "13";
- tv.Layout ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │hi │
- │ │
- │ ╭──┬──╯
- │12│13│ ",
- output
- );
- tv.SelectedTab = tab2;
- Assert.Equal (tab2, tv.Subviews.First (v => v.Id.Contains ("tabRow")).MostFocused);
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │hi2 │
- │ │
- ├──╮ ╭──╯
- │12│13│ ",
- output
- );
- tv.SelectedTab = tab1;
- // Test first tab name too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "13";
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │hi │
- │ │
- │ ╭►
- │1234567│ ",
- output
- );
- //switch to tab2
- tv.SelectedTab = tab2;
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │hi2 │
- │ │
- ◄ ╭─────╯
- │13│ ",
- output
- );
- // now make both tabs too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "abcdefghijklmnopq";
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │hi2 │
- │ │
- ◄ ╭╯
- │abcdefg│ ",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ShowTopLine_True_TabsOnBottom_False_TestTabView_Width3 ()
- {
- TabView tv = GetTabView (out _, out _);
- tv.Width = 3;
- tv.Height = 5;
- tv.Layout ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ╭╮
- ││
- │╰►
- │h│
- └─┘",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ShowTopLine_True_TabsOnBottom_False_TestTabView_Width4 ()
- {
- TabView tv = GetTabView (out _, out _);
- tv.Width = 4;
- tv.Height = 5;
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ╭─╮
- │T│
- │ ╰►
- │hi│
- └──┘",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ShowTopLine_True_TabsOnBottom_False_TestThinTabView_WithLongNames ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.Width = 10;
- tv.Height = 5;
- // Test two tab names that fit
- tab1.DisplayText = "12";
- tab2.DisplayText = "13";
- tv.Layout ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ╭──┬──╮
- │12│13│
- │ ╰──┴──╮
- │hi │
- └────────┘",
- output
- );
- tv.SelectedTab = tab2;
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ╭──┬──╮
- │12│13│
- ├──╯ ╰──╮
- │hi2 │
- └────────┘",
- output
- );
- tv.SelectedTab = tab1;
- // Test first tab name too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "13";
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ╭───────╮
- │1234567│
- │ ╰►
- │hi │
- └────────┘",
- output
- );
- //switch to tab2
- tv.SelectedTab = tab2;
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ╭──╮
- │13│
- ◄ ╰─────╮
- │hi2 │
- └────────┘",
- output
- );
- // now make both tabs too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "abcdefghijklmnopq";
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ╭───────╮
- │abcdefg│
- ◄ ╰╮
- │hi2 │
- └────────┘",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ShowTopLine_True_TabsOnBottom_False_With_Unicode ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.Width = 20;
- tv.Height = 5;
- tab1.DisplayText = "Tab0";
- tab2.DisplayText = "Les Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
- tv.Layout ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ╭────╮
- │Tab0│
- │ ╰─────────────►
- │hi │
- └──────────────────┘",
- output
- );
- tv.SelectedTab = tab2;
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ╭──────────────╮
- │Les Misérables│
- ◄ ╰───╮
- │hi2 │
- └──────────────────┘",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ShowTopLine_True_TabsOnBottom_True_TestTabView_Width3 ()
- {
- TabView tv = GetTabView (out _, out _);
- tv.Width = 3;
- tv.Height = 5;
- tv.Style = new () { TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.Layout ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌─┐
- │h│
- │╭►
- ││
- ╰╯ ",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ShowTopLine_True_TabsOnBottom_True_TestTabView_Width4 ()
- {
- TabView tv = GetTabView (out _, out _);
- tv.Width = 4;
- tv.Height = 5;
- tv.Style = new () { TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.Layout ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌──┐
- │hi│
- │ ╭►
- │T│
- ╰─╯ ",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ShowTopLine_True_TabsOnBottom_True_TestThinTabView_WithLongNames ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.Width = 10;
- tv.Height = 5;
- tv.Style = new () { TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.Layout ();
- // Test two tab names that fit
- tab1.DisplayText = "12";
- tab2.DisplayText = "13";
- tv.Layout ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │hi │
- │ ╭──┬──╯
- │12│13│
- ╰──┴──╯ ",
- output
- );
- // Test first tab name too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "13";
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │hi │
- │ ╭►
- │1234567│
- ╰───────╯ ",
- output
- );
- //switch to tab2
- tv.SelectedTab = tab2;
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │hi2 │
- ◄ ╭─────╯
- │13│
- ╰──╯ ",
- output
- );
- // now make both tabs too long
- tab1.DisplayText = "12345678910";
- tab2.DisplayText = "abcdefghijklmnopq";
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌────────┐
- │hi2 │
- ◄ ╭╯
- │abcdefg│
- ╰───────╯ ",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void ShowTopLine_True_TabsOnBottom_True_With_Unicode ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.Width = 20;
- tv.Height = 5;
- tv.Style = new () { TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tab1.DisplayText = "Tab0";
- tab2.DisplayText = "Les Mise" + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber)) + "rables";
- tv.Layout ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌──────────────────┐
- │hi │
- │ ╭─────────────►
- │Tab0│
- ╰────╯ ",
- output
- );
- tv.SelectedTab = tab2;
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌──────────────────┐
- │hi2 │
- ◄ ╭───╯
- │Les Misérables│
- ╰──────────────╯ ",
- output
- );
- }
- [Fact]
- public void SwitchTabBy_NormalUsage ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- Tab tab3;
- Tab tab4;
- Tab tab5;
- tv.AddTab (tab3 = new (), false);
- tv.AddTab (tab4 = new (), false);
- tv.AddTab (tab5 = new (), false);
- tv.SelectedTab = tab1;
- var called = 0;
- tv.SelectedTabChanged += (s, e) => { called++; };
- tv.SwitchTabBy (1);
- Assert.Equal (1, called);
- Assert.Equal (tab2, tv.SelectedTab);
- //reset called counter
- called = 0;
- // go right 2
- tv.SwitchTabBy (2);
- // even though we go right 2 indexes the event should only be called once
- Assert.Equal (1, called);
- Assert.Equal (tab4, tv.SelectedTab);
- }
- [Fact]
- public void SwitchTabBy_OutOfTabsRange ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.SelectedTab = tab1;
- tv.SwitchTabBy (500);
- Assert.Equal (tab2, tv.SelectedTab);
- tv.SwitchTabBy (-500);
- Assert.Equal (tab1, tv.SelectedTab);
- }
- [Fact]
- public void RemoveTab_ThatHasFocus ()
- {
- TabView tv = GetTabView (out Tab _, out Tab tab2);
- tv.SelectedTab = tab2;
- tab2.HasFocus = true;
- Assert.Equal (2, tv.Tabs.Count);
- foreach (Tab t in tv.Tabs.ToArray ())
- {
- tv.RemoveTab (t);
- }
- Assert.Empty (tv.Tabs);
- }
- [Fact]
- [SetupFakeDriver]
- public void Add_Three_TabsOnTop_ChangesTab ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- Tab tab3;
- tv.AddTab (
- tab3 = new () { Id = "tab3", DisplayText = "Tab3", View = new TextView { Id = "tab3.TextView", Width = 3, Height = 1, Text = "hi3" } },
- false);
- tv.Width = 20;
- tv.Height = 5;
- tv.Layout ();
- tv.Draw ();
- Assert.Equal (tab1, tv.SelectedTab);
- TestHelpers.AssertDriverContentsAre (
- @"
- ╭────┬────┬────╮
- │Tab1│Tab2│Tab3│
- │ ╰────┴────┴───╮
- │hi │
- └──────────────────┘
- ",
- output
- );
- tv.SelectedTab = tab2;
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ╭────┬────┬────╮
- │Tab1│Tab2│Tab3│
- ├────╯ ╰────┴───╮
- │hi2 │
- └──────────────────┘
- ",
- output
- );
- tv.SelectedTab = tab3;
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ╭────┬────┬────╮
- │Tab1│Tab2│Tab3│
- ├────┴────╯ ╰───╮
- │hi3 │
- └──────────────────┘
- ",
- output
- );
- }
- [Fact]
- [SetupFakeDriver]
- public void Add_Three_TabsOnBottom_ChangesTab ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- Tab tab3;
- tv.AddTab (
- tab3 = new () { Id = "tab3", DisplayText = "Tab3", View = new TextView { Id = "tab3.TextView", Width = 3, Height = 1, Text = "hi3" } },
- false);
- tv.Width = 20;
- tv.Height = 5;
- tv.Style = new () { TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.Layout ();
- tv.Draw ();
- Assert.Equal (tab1, tv.SelectedTab);
- TestHelpers.AssertDriverContentsAre (
- @"
- ┌──────────────────┐
- │hi │
- │ ╭────┬────┬───╯
- │Tab1│Tab2│Tab3│
- ╰────┴────┴────╯
- ",
- output
- );
- tv.SelectedTab = tab2;
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌──────────────────┐
- │hi2 │
- ├────╮ ╭────┬───╯
- │Tab1│Tab2│Tab3│
- ╰────┴────┴────╯
- ",
- output
- );
- tv.SelectedTab = tab3;
- tv.Layout ();
- View.SetClipToScreen ();
- tv.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌──────────────────┐
- │hi3 │
- ├────┬────╮ ╭───╯
- │Tab1│Tab2│Tab3│
- ╰────┴────┴────╯
- ",
- output
- );
- }
- [Fact]
- [AutoInitShutdown]
- public void Tab_Get_Focus_By_Press_F6 ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.Width = 20;
- tv.Height = 5;
- Toplevel top = new ();
- top.Add (tv);
- Application.Begin (top);
- Assert.False (tab1.HasFocus);
- Assert.True (Application.RaiseKeyDownEvent (Key.F6));
- Assert.True (tab1.HasFocus);
- top.Dispose ();
- }
- [Fact]
- [SetupFakeDriver]
- public void Mouse_Wheel_Changes_Tab ()
- {
- TabView tv = GetTabView (out Tab tab1, out Tab tab2);
- tv.Width = 20;
- tv.Height = 5;
- Toplevel top = new ();
- top.Add (tv);
- Application.Begin (top);
- Assert.False (tab1.HasFocus);
- Application.RaiseMouseEvent (new () { Position = new (1, 1), Flags = MouseFlags.WheeledDown });
- Assert.True (tab2.HasFocus);
- Application.RaiseMouseEvent (new () { Position = new (1, 1), Flags = MouseFlags.WheeledUp });
- Assert.True (tab1.HasFocus);
- Application.RaiseMouseEvent (new () { Position = new (1, 1), Flags = MouseFlags.WheeledRight });
- Assert.True (tab2.HasFocus);
- Application.RaiseMouseEvent (new () { Position = new (1, 1), Flags = MouseFlags.WheeledLeft });
- Assert.True (tab1.HasFocus);
- top.Dispose ();
- }
- private TabView GetTabView () { return GetTabView (out _, out _); }
- private TabView GetTabView (out Tab tab1, out Tab tab2)
- {
- var tv = new TabView () { Id = "tv " };
- tv.BeginInit ();
- tv.EndInit ();
- tv.ColorScheme = new ();
- tv.AddTab (
- tab1 = new () { Id = "tab1", DisplayText = "Tab1", View = new TextField { Id = "tab1.TextField", Width = 2, Text = "hi" } },
- false
- );
- tv.AddTab (tab2 = new () { Id = "tab2", DisplayText = "Tab2", View = new Label { Id = "tab1.Label", Text = "hi2" } }, false);
- return tv;
- }
- }
|