123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770 |
- 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;
- namespace Terminal.Gui.Views {
- public class TabViewTests {
- readonly ITestOutputHelper output;
- public TabViewTests (ITestOutputHelper output)
- {
- this.output = output;
- }
- private TabView GetTabView ()
- {
- return GetTabView (out _, out _);
- }
- private TabView GetTabView (out TabView.Tab tab1, out TabView.Tab tab2, bool initFakeDriver = true)
- {
- if (initFakeDriver)
- InitFakeDriver ();
- var tv = new TabView ();
- tv.ColorScheme = new ColorScheme ();
- tv.AddTab (tab1 = new TabView.Tab ("Tab1", new TextField ("hi")), false);
- tv.AddTab (tab2 = new TabView.Tab ("Tab2", new Label ("hi2")), false);
- return tv;
- }
- [Fact]
- public void AddTwoTabs_SecondIsSelected ()
- {
- InitFakeDriver ();
- var tv = new TabView ();
- TabView.Tab tab1;
- TabView.Tab tab2;
- tv.AddTab (tab1 = new TabView.Tab ("Tab1", new TextField ("hi")), false);
- tv.AddTab (tab2 = new TabView.Tab ("Tab1", new Label ("hi2")), true);
- Assert.Equal (2, tv.Tabs.Count);
- Assert.Equal (tab2, tv.SelectedTab);
- Application.Shutdown ();
- }
- [Fact]
- public void EnsureSelectedTabVisible_NullSelect ()
- {
- var 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);
- Application.Shutdown ();
- }
- [Fact]
- public void EnsureSelectedTabVisible_MustScroll ()
- {
- var tv = GetTabView (out var tab1, out var 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);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void SelectedTabChanged_Called ()
- {
- var tv = GetTabView (out var tab1, out var tab2);
- tv.SelectedTab = tab1;
- TabView.Tab oldTab = null;
- TabView.Tab newTab = null;
- int 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);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void RemoveTab_ChangesSelection ()
- {
- var tv = GetTabView (out var tab1, out var tab2);
- tv.SelectedTab = tab1;
- tv.RemoveTab (tab1);
- Assert.Equal (tab2, tv.SelectedTab);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void RemoveTab_MultipleCalls_NotAnError ()
- {
- var tv = GetTabView (out var tab1, out var 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);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void RemoveAllTabs_ClearsSelection ()
- {
- var tv = GetTabView (out var tab1, out var tab2);
- tv.SelectedTab = tab1;
- tv.RemoveTab (tab1);
- tv.RemoveTab (tab2);
- Assert.Null (tv.SelectedTab);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void SwitchTabBy_NormalUsage ()
- {
- var tv = GetTabView (out var tab1, out var tab2);
- TabView.Tab tab3;
- TabView.Tab tab4;
- TabView.Tab tab5;
- tv.AddTab (tab3 = new TabView.Tab (), false);
- tv.AddTab (tab4 = new TabView.Tab (), false);
- tv.AddTab (tab5 = new TabView.Tab (), false);
- tv.SelectedTab = tab1;
- int 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);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void AddTab_SameTabMoreThanOnce ()
- {
- var tv = GetTabView (out var tab1, out var 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);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact]
- public void SwitchTabBy_OutOfTabsRange ()
- {
- var tv = GetTabView (out var tab1, out var tab2);
- tv.SelectedTab = tab1;
- tv.SwitchTabBy (500);
- Assert.Equal (tab2, tv.SelectedTab);
- tv.SwitchTabBy (-500);
- Assert.Equal (tab1, tv.SelectedTab);
- // Shutdown must be called to safely clean up Application if Init has been called
- Application.Shutdown ();
- }
- [Fact, AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_False_TestThinTabView_WithLongNames ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 10;
- tv.Height = 5;
- // Ensures that the tab bar subview gets the bounds of the parent TabView
- tv.LayoutSubviews ();
- // Test two tab names that fit
- tab1.Text = "12";
- tab2.Text = "13";
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌──┐
- │12│13
- │ └─────┐
- │hi │
- └────────┘", output);
- tv.SelectedTab = tab2;
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌──┐
- 12│13│
- ┌──┘ └──┐
- │hi2 │
- └────────┘", output);
- tv.SelectedTab = tab1;
- // Test first tab name too long
- tab1.Text = "12345678910";
- tab2.Text = "13";
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌───────┐
- │1234567│
- │ └►
- │hi │
- └────────┘", output);
- //switch to tab2
- tv.SelectedTab = tab2;
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌──┐
- │13│
- ◄ └─────┐
- │hi2 │
- └────────┘", output);
- // now make both tabs too long
- tab1.Text = "12345678910";
- tab2.Text = "abcdefghijklmnopq";
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌───────┐
- │abcdefg│
- ◄ └┐
- │hi2 │
- └────────┘", output);
- }
- [Fact, AutoInitShutdown]
- public void ShowTopLine_False_TabsOnBottom_False_TestThinTabView_WithLongNames ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 10;
- tv.Height = 5;
- tv.Style = new TabView.TabStyle { ShowTopLine = false };
- tv.ApplyStyleChanges ();
- // Ensures that the tab bar subview gets the bounds of the parent TabView
- tv.LayoutSubviews ();
- // Test two tab names that fit
- tab1.Text = "12";
- tab2.Text = "13";
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- │12│13
- │ └─────┐
- │hi │
- │ │
- └────────┘", output);
- tv.SelectedTab = tab2;
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- 12│13│
- ┌──┘ └──┐
- │hi2 │
- │ │
- └────────┘", output);
- tv.SelectedTab = tab1;
- // Test first tab name too long
- tab1.Text = "12345678910";
- tab2.Text = "13";
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- │1234567│
- │ └►
- │hi │
- │ │
- └────────┘", output);
- //switch to tab2
- tv.SelectedTab = tab2;
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- │13│
- ◄ └─────┐
- │hi2 │
- │ │
- └────────┘", output);
- // now make both tabs too long
- tab1.Text = "12345678910";
- tab2.Text = "abcdefghijklmnopq";
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- │abcdefg│
- ◄ └┐
- │hi2 │
- │ │
- └────────┘", output);
- }
- [Fact, AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_False_TestTabView_Width4 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 4;
- tv.Height = 5;
- tv.LayoutSubviews ();
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌─┐
- │T│
- │ └►
- │hi│
- └──┘", output);
- }
- [Fact, AutoInitShutdown]
- public void ShowTopLine_False_TabsOnBottom_False_TestTabView_Width4 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 4;
- tv.Height = 5;
- tv.Style = new TabView.TabStyle { ShowTopLine = false };
- tv.ApplyStyleChanges ();
- tv.LayoutSubviews ();
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- │T│
- │ └►
- │hi│
- │ │
- └──┘", output);
- }
- [Fact, AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_False_TestTabView_Width3 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 3;
- tv.Height = 5;
- tv.LayoutSubviews ();
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌┐
- ││
- │└►
- │h│
- └─┘", output);
- }
- [Fact, AutoInitShutdown]
- public void ShowTopLine_False_TabsOnBottom_False_TestTabView_Width3 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 3;
- tv.Height = 5;
- tv.Style = new TabView.TabStyle { ShowTopLine = false };
- tv.ApplyStyleChanges ();
- tv.LayoutSubviews ();
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ││
- │└►
- │h│
- │ │
- └─┘", output);
- }
- [Fact, AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_True_TestThinTabView_WithLongNames ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 10;
- tv.Height = 5;
- tv.Style = new TabView.TabStyle { TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- // Ensures that the tab bar subview gets the bounds of the parent TabView
- tv.LayoutSubviews ();
- // Test two tab names that fit
- tab1.Text = "12";
- tab2.Text = "13";
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi │
- │ ┌─────┘
- │12│13
- └──┘ ", output);
- // Test first tab name too long
- tab1.Text = "12345678910";
- tab2.Text = "13";
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi │
- │ ┌►
- │1234567│
- └───────┘ ", output);
- //switch to tab2
- tv.SelectedTab = tab2;
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi2 │
- ◄ ┌─────┘
- │13│
- └──┘ ", output);
- // now make both tabs too long
- tab1.Text = "12345678910";
- tab2.Text = "abcdefghijklmnopq";
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi2 │
- ◄ ┌┘
- │abcdefg│
- └───────┘ ", output);
- }
- [Fact, AutoInitShutdown]
- public void ShowTopLine_False_TabsOnBottom_True_TestThinTabView_WithLongNames ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 10;
- tv.Height = 5;
- tv.Style = new TabView.TabStyle { ShowTopLine = false, TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- // Ensures that the tab bar subview gets the bounds of the parent TabView
- tv.LayoutSubviews ();
- // Test two tab names that fit
- tab1.Text = "12";
- tab2.Text = "13";
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi │
- │ │
- │ ┌─────┘
- │12│13 ", output);
- tv.SelectedTab = tab2;
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi2 │
- │ │
- └──┐ ┌──┘
- 12│13│ ", output);
- tv.SelectedTab = tab1;
- // Test first tab name too long
- tab1.Text = "12345678910";
- tab2.Text = "13";
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi │
- │ │
- │ ┌►
- │1234567│ ", output);
- //switch to tab2
- tv.SelectedTab = tab2;
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi2 │
- │ │
- ◄ ┌─────┘
- │13│ ", output);
- // now make both tabs too long
- tab1.Text = "12345678910";
- tab2.Text = "abcdefghijklmnopq";
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌────────┐
- │hi2 │
- │ │
- ◄ ┌┘
- │abcdefg│ ", output);
- }
- [Fact, AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_True_TestTabView_Width4 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 4;
- tv.Height = 5;
- tv.Style = new TabView.TabStyle { TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.LayoutSubviews ();
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌──┐
- │hi│
- │ ┌►
- │T│
- └─┘ ", output);
- }
- [Fact, AutoInitShutdown]
- public void ShowTopLine_False_TabsOnBottom_True_TestTabView_Width4 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 4;
- tv.Height = 5;
- tv.Style = new TabView.TabStyle { ShowTopLine = false, TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.LayoutSubviews ();
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌──┐
- │hi│
- │ │
- │ ┌►
- │T│ ", output);
- }
- [Fact, AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_True_TestTabView_Width3 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 3;
- tv.Height = 5;
- tv.Style = new TabView.TabStyle { TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.LayoutSubviews ();
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌─┐
- │h│
- │┌►
- ││
- └┘ ", output);
- }
- [Fact, AutoInitShutdown]
- public void ShowTopLine_False_TabsOnBottom_True_TestTabView_Width3 ()
- {
- var tv = GetTabView (out _, out _, false);
- tv.Width = 3;
- tv.Height = 5;
- tv.Style = new TabView.TabStyle { ShowTopLine = false, TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.LayoutSubviews ();
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌─┐
- │h│
- │ │
- │┌►
- ││ ", output);
- }
- [Fact, AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_False_With_Unicode ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 20;
- tv.Height = 5;
- tv.LayoutSubviews ();
- tab1.Text = "Tab0";
- tab2.Text = "Les Mise" + Char.ConvertFromUtf32 (Int32.Parse ("0301", NumberStyles.HexNumber)) + "rables";
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌────┐
- │Tab0│
- │ └─────────────►
- │hi │
- └──────────────────┘", output);
- tv.SelectedTab = tab2;
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌──────────────┐
- │Les Misérables│
- ◄ └───┐
- │hi2 │
- └──────────────────┘", output);
- }
- [Fact, AutoInitShutdown]
- public void ShowTopLine_True_TabsOnBottom_True_With_Unicode ()
- {
- var tv = GetTabView (out var tab1, out var tab2, false);
- tv.Width = 20;
- tv.Height = 5;
- tv.Style = new TabView.TabStyle { TabsOnBottom = true };
- tv.ApplyStyleChanges ();
- tv.LayoutSubviews ();
- tab1.Text = "Tab0";
- tab2.Text = "Les Mise" + Char.ConvertFromUtf32 (Int32.Parse ("0301", NumberStyles.HexNumber)) + "rables";
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌──────────────────┐
- │hi │
- │ ┌─────────────►
- │Tab0│
- └────┘ ", output);
- tv.SelectedTab = tab2;
- tv.Redraw (tv.Bounds);
- GraphViewTests.AssertDriverContentsWithFrameAre (@"
- ┌──────────────────┐
- │hi2 │
- ◄ ┌───┘
- │Les Misérables│
- └──────────────┘ ", output);
- }
- private void InitFakeDriver ()
- {
- var driver = new FakeDriver ();
- Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
- driver.Init (() => { });
- }
- }
- }
|