1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420 |
- using System.ComponentModel;
- using Xunit.Abstractions;
- namespace Terminal.Gui.ViewsTests;
- public class LabelTests (ITestOutputHelper output)
- {
- // Test that Title and Text are the same
- [Fact]
- public void Text_Mirrors_Title ()
- {
- var label = new Label ();
- label.Title = "Hello";
- Assert.Equal ("Hello", label.Title);
- Assert.Equal ("Hello", label.TitleTextFormatter.Text);
- Assert.Equal ("Hello", label.Text);
- Assert.Equal ("Hello", label.TextFormatter.Text);
- }
- [Fact]
- public void Title_Mirrors_Text ()
- {
- var label = new Label ();
- label.Text = "Hello";
- Assert.Equal ("Hello", label.Text);
- Assert.Equal ("Hello", label.TextFormatter.Text);
- Assert.Equal ("Hello", label.Title);
- Assert.Equal ("Hello", label.TitleTextFormatter.Text);
- }
- [Fact]
- public void HotKey_Command_SetsFocus_OnNextSubview ()
- {
- var superView = new View { CanFocus = true };
- var label = new Label ();
- var nextSubview = new View { CanFocus = true };
- superView.Add (label, nextSubview);
- superView.BeginInit ();
- superView.EndInit ();
- Assert.False (label.HasFocus);
- Assert.False (nextSubview.HasFocus);
- label.InvokeCommand (Command.HotKey);
- Assert.False (label.HasFocus);
- Assert.True (nextSubview.HasFocus);
- }
- [Fact]
- public void MouseClick_SetsFocus_OnNextSubview ()
- {
- var superView = new View { CanFocus = true, Height = 1, Width = 15 };
- var focusedView = new View { CanFocus = true, Width = 1, Height = 1 };
- var label = new Label { X = 2, Title = "_x" };
- var nextSubview = new View { CanFocus = true, X = 4, Width = 4, Height = 1 };
- superView.Add (focusedView, label, nextSubview);
- superView.BeginInit ();
- superView.EndInit ();
- Assert.False (focusedView.HasFocus);
- Assert.False (label.HasFocus);
- Assert.False (nextSubview.HasFocus);
- label.NewMouseEvent (new() { Position = new (0, 0), Flags = MouseFlags.Button1Clicked });
- Assert.False (label.HasFocus);
- Assert.True (nextSubview.HasFocus);
- }
- [Fact]
- public void HotKey_Command_Does_Not_Accept ()
- {
- var label = new Label ();
- var accepted = false;
- label.Accept += LabelOnAccept;
- label.InvokeCommand (Command.HotKey);
- Assert.False (accepted);
- return;
- void LabelOnAccept (object sender, HandledEventArgs e) { accepted = true; }
- }
- [Fact]
- [AutoInitShutdown]
- public void AutoSize_Stays_True_AnchorEnd ()
- {
- var label = new Label { Y = Pos.Center (), Text = "Say Hello 你" };
- label.X = Pos.AnchorEnd (0) - Pos.Func (() => label.TextFormatter.Text.GetColumns ());
- var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
- win.Add (label);
- var top = new Toplevel ();
- top.Add (win);
- Application.Begin (top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- var expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你│
- │ │
- └────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- label.Text = "Say Hello 你 changed";
- Application.Refresh ();
- expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你 changed│
- │ │
- └────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void AutoSize_Stays_True_Center ()
- {
- var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
- var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
- win.Add (label);
- var top = new Toplevel ();
- top.Add (win);
- Application.Begin (top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- var expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你 │
- │ │
- └────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- label.Text = "Say Hello 你 changed";
- Application.Refresh ();
- expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你 changed │
- │ │
- └────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void AutoSize_Stays_True_With_EmptyText ()
- {
- var label = new Label { X = Pos.Center (), Y = Pos.Center () };
- var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
- win.Add (label);
- var top = new Toplevel ();
- top.Add (win);
- label.Text = "Say Hello 你";
- Application.Begin (top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- var expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你 │
- │ │
- └────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- top.Dispose ();
- }
- [Fact]
- public void Constructors_Defaults ()
- {
- var label = new Label ();
- Assert.Equal (string.Empty, label.Text);
- Assert.Equal (Alignment.Start, label.TextAlignment);
- Assert.False (label.CanFocus);
- Assert.Equal (new (0, 0, 0, 0), label.Frame);
- Assert.Equal (KeyCode.Null, label.HotKey);
- }
- [Fact]
- [AutoInitShutdown]
- public void Label_Draw_Fill_Remaining_AutoSize_False ()
- {
- var tfSize = new Size (80, 1);
- var label = new Label { Text = "This label needs to be cleared before rewritten.", Width = tfSize.Width, Height = tfSize.Height };
- var tf1 = new TextFormatter { Direction = TextDirection.LeftRight_TopBottom, Size = tfSize };
- tf1.Text = "This TextFormatter (tf1) without fill will not be cleared on rewritten.";
- var tf2 = new TextFormatter { Direction = TextDirection.LeftRight_TopBottom, Size = tfSize, FillRemaining = true };
- tf2.Text = "This TextFormatter (tf2) with fill will be cleared on rewritten.";
- var top = new Toplevel ();
- top.Add (label);
- Application.Begin (top);
- Assert.False (label.TextFormatter.AutoSize);
- Assert.False (tf1.AutoSize);
- Assert.False (tf2.AutoSize);
- Assert.False (label.TextFormatter.FillRemaining);
- Assert.False (tf1.FillRemaining);
- Assert.True (tf2.FillRemaining);
- tf1.Draw (new (new (0, 1), tfSize), label.GetNormalColor (), label.ColorScheme.HotNormal);
- tf2.Draw (new (new (0, 2), tfSize), label.GetNormalColor (), label.ColorScheme.HotNormal);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- This label needs to be cleared before rewritten.
- This TextFormatter (tf1) without fill will not be cleared on rewritten.
- This TextFormatter (tf2) with fill will be cleared on rewritten. ",
- output
- );
- Assert.False (label.NeedsDisplay);
- Assert.False (label.LayoutNeeded);
- Assert.False (label.SubViewNeedsDisplay);
- label.Text = "This label is rewritten.";
- Assert.True (label.NeedsDisplay);
- Assert.True (label.LayoutNeeded);
- //Assert.False (label.SubViewNeedsDisplay);
- label.Draw ();
- tf1.Text = "This TextFormatter (tf1) is rewritten.";
- tf1.Draw (new (new (0, 1), tfSize), label.GetNormalColor (), label.ColorScheme.HotNormal);
- tf2.Text = "This TextFormatter (tf2) is rewritten.";
- tf2.Draw (new (new (0, 2), tfSize), label.GetNormalColor (), label.ColorScheme.HotNormal);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- This label is rewritten.
- This TextFormatter (tf1) is rewritten.will not be cleared on rewritten.
- This TextFormatter (tf2) is rewritten. ",
- output
- );
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Label_Draw_Horizontal_Simple_Runes ()
- {
- var label = new Label { Text = "Demo Simple Rune" };
- var top = new Toplevel ();
- top.Add (label);
- Application.Begin (top);
- Assert.Equal (new (0, 0, 16, 1), label.Frame);
- var expected = @"
- Demo Simple Rune
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, 16, 1), pos);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Label_Draw_Vertical_Simple_Runes ()
- {
- var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "Demo Simple Rune" };
- var top = new Toplevel ();
- top.Add (label);
- Application.Begin (top);
- Assert.NotNull (label.Width);
- Assert.NotNull (label.Height);
- var expected = @"
- D
- e
- m
- o
-
- S
- i
- m
- p
- l
- e
-
- R
- u
- n
- e
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, 1, 16), pos);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Label_Draw_Vertical_Wide_Runes ()
- {
- var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "デモエムポンズ" };
- var top = new Toplevel ();
- top.Add (label);
- Application.Begin (top);
- var expected = @"
- デ
- モ
- エ
- ム
- ポ
- ン
- ズ
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, 2, 7), pos);
- top.Dispose ();
- }
- [Fact]
- public void Label_HotKeyChanged_EventFires ()
- {
- var label = new Label { Text = "Yar" };
- label.HotKey = 'Y';
- object sender = null;
- KeyChangedEventArgs args = null;
- label.HotKeyChanged += (s, e) =>
- {
- sender = s;
- args = e;
- };
- label.HotKey = Key.R;
- Assert.Same (label, sender);
- Assert.Equal (KeyCode.Y | KeyCode.ShiftMask, args.OldKey);
- Assert.Equal (Key.R, args.NewKey);
- }
- [Fact]
- public void Label_HotKeyChanged_EventFires_WithNone ()
- {
- var label = new Label ();
- object sender = null;
- KeyChangedEventArgs args = null;
- label.HotKeyChanged += (s, e) =>
- {
- sender = s;
- args = e;
- };
- label.HotKey = KeyCode.R;
- Assert.Same (label, sender);
- Assert.Equal (KeyCode.Null, args.OldKey);
- Assert.Equal (KeyCode.R, args.NewKey);
- }
- [Fact]
- public void TestAssignTextToLabel ()
- {
- View b = new Label { Text = "heya" };
- Assert.Equal ("heya", b.Text);
- Assert.Contains ("heya", b.TextFormatter.Text);
- b.Text = "heyb";
- Assert.Equal ("heyb", b.Text);
- Assert.Contains ("heyb", b.TextFormatter.Text);
- // with cast
- Assert.Equal ("heyb", ((Label)b).Text);
- }
- [Fact]
- [AutoInitShutdown]
- public void Update_Only_On_Or_After_Initialize ()
- {
- var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
- var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
- win.Add (label);
- var top = new Toplevel ();
- top.Add (win);
- Assert.False (label.IsInitialized);
- Application.Begin (top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- Assert.True (label.IsInitialized);
- Assert.Equal ("Say Hello 你", label.Text);
- Assert.Equal ("Say Hello 你", label.TextFormatter.Text);
- Assert.Equal (new (0, 0, 12, 1), label.Viewport);
- var expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你 │
- │ │
- └────────────────────────────┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, 30, 5), pos);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Update_Parameterless_Only_On_Or_After_Initialize ()
- {
- var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
- var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
- win.Add (label);
- var top = new Toplevel ();
- top.Add (win);
- Assert.False (label.IsInitialized);
- Application.Begin (top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- Assert.True (label.IsInitialized);
- Assert.Equal ("Say Hello 你", label.Text);
- Assert.Equal ("Say Hello 你", label.TextFormatter.Text);
- Assert.Equal (new (0, 0, 12, 1), label.Viewport);
- var expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你 │
- │ │
- └────────────────────────────┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, 30, 5), pos);
- top.Dispose ();
- }
- [Fact]
- [SetupFakeDriver]
- public void Full_Border ()
- {
- var label = new Label { BorderStyle = LineStyle.Single, Text = "Test" };
- label.BeginInit ();
- label.EndInit ();
- label.SetRelativeLayout (Application.Screen.Size);
- Assert.Equal (new (0, 0, 4, 1), label.Viewport);
- Assert.Equal (new (0, 0, 6, 3), label.Frame);
- label.Draw ();
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- ┌┤Te├┐
- │Test│
- └────┘",
- output
- );
- label.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void With_Top_Margin_Without_Top_Border ()
- {
- var label = new Label { Text = "Test", /*Width = 6, Height = 3,*/ BorderStyle = LineStyle.Single };
- label.Margin.Thickness = new (0, 1, 0, 0);
- label.Border.Thickness = new (1, 0, 1, 1);
- var top = new Toplevel ();
- top.Add (label);
- Application.Begin (top);
- Assert.Equal (new (0, 0, 6, 3), label.Frame);
- Assert.Equal (new (0, 0, 4, 1), label.Viewport);
- Application.Begin (top);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- │Test│
- └────┘",
- output
- );
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Without_Top_Border ()
- {
- var label = new Label { Text = "Test", /* Width = 6, Height = 3, */BorderStyle = LineStyle.Single };
- label.Border.Thickness = new (1, 0, 1, 1);
- var top = new Toplevel ();
- top.Add (label);
- Application.Begin (top);
- Assert.Equal (new (0, 0, 6, 2), label.Frame);
- Assert.Equal (new (0, 0, 4, 1), label.Viewport);
- Application.Begin (top);
- TestHelpers.AssertDriverContentsWithFrameAre (
- @"
- │Test│
- └────┘",
- output
- );
- top.Dispose ();
- }
- // These tests were formally in AutoSizetrue.cs. They are (poor) Label tests.
- private readonly string [] expecteds = new string [21]
- {
- @"
- ┌────────────────────┐
- │View with long text │
- │ │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 0 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 1 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 2 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 3 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 4 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 5 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 6 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 7 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 8 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 9 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 10 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 11 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 12 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 13 │
- │Label 13 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 13 │
- │Label 14 │
- │Label 14 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 13 │
- │Label 14 │
- │Label 15 │
- │Label 15 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 13 │
- │Label 14 │
- │Label 15 │
- │Label 16 │
- │Label 16 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 13 │
- │Label 14 │
- │Label 15 │
- │Label 16 │
- │Label 17 │
- │Label 17 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 13 │
- │Label 14 │
- │Label 15 │
- │Label 16 │
- │Label 17 │
- │Label 18 │
- │Label 18 │
- └────────────────────┘",
- @"
- ┌────────────────────┐
- │View with long text │
- │Label 0 │
- │Label 1 │
- │Label 2 │
- │Label 3 │
- │Label 4 │
- │Label 5 │
- │Label 6 │
- │Label 7 │
- │Label 8 │
- │Label 9 │
- │Label 10 │
- │Label 11 │
- │Label 12 │
- │Label 13 │
- │Label 14 │
- │Label 15 │
- │Label 16 │
- │Label 17 │
- │Label 18 │
- │Label 19 │
- │Label 19 │
- └────────────────────┘"
- };
- private static readonly Size _size1x1 = new (1, 1);
- // TODO: This is a Label test. Move to label tests if there's not already a test for this.
- [Fact]
- [AutoInitShutdown]
- public void AnchorEnd_Better_Than_Bottom_Equal_Inside_Window ()
- {
- var win = new Window ();
- var label = new Label
- {
- Text = "This should be the last line.",
- ColorScheme = Colors.ColorSchemes ["Menu"],
- //Width = Dim.Fill (),
- X = 0, // keep unit test focused; don't use Center here
- Y = Pos.AnchorEnd (1)
- };
- win.Add (label);
- Toplevel top = new ();
- top.Add (win);
- RunState rs = Application.Begin (top);
- ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
- Assert.Equal (29, label.Text.Length);
- Assert.Equal (new (0, 0, 40, 10), top.Frame);
- Assert.Equal (new (0, 0, 40, 10), win.Frame);
- Assert.Equal (new (0, 7, 29, 1), label.Frame);
- var expected = @"
- ┌──────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │This should be the last line. │
- └──────────────────────────────────────┘
- "
- ;
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Application.End (rs);
- top.Dispose ();
- }
- // TODO: This is a Label test. Move to label tests if there's not already a test for this.
- [Fact]
- [AutoInitShutdown]
- public void Bottom_Equal_Inside_Window ()
- {
- var win = new Window ();
- // Label is AutoSize == true
- var label = new Label
- {
- Text = "This should be the last line.",
- ColorScheme = Colors.ColorSchemes ["Menu"],
- //Width = Dim.Fill (),
- X = 0,
- Y = Pos.Bottom (win)
- - 3 // two lines top and bottom borders more one line above the bottom border
- };
- win.Add (label);
- Toplevel top = new ();
- top.Add (win);
- RunState rs = Application.Begin (top);
- ((FakeDriver)Application.Driver).SetBufferSize (40, 10);
- Assert.Equal (new (0, 0, 40, 10), top.Frame);
- Assert.Equal (new (0, 0, 40, 10), win.Frame);
- Assert.Equal (new (0, 7, 29, 1), label.Frame);
- var expected = @"
- ┌──────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │This should be the last line. │
- └──────────────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Application.End (rs);
- top.Dispose ();
- }
- #if V2_STATUSBAR
- // TODO: This is a Label test. Move to label tests if there's not already a test for this.
- [Fact]
- [AutoInitShutdown]
- public void Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
- {
- var win = new Window ();
- // Label is AutoSize == true
- var label = new Label
- {
- Text = "This should be the last line.",
- ColorScheme = Colors.ColorSchemes ["Menu"],
- //Width = Dim.Fill (),
- X = 0,
- Y = Pos.Bottom (win) - 4 // two lines top and bottom borders more two lines above border
- };
- win.Add (label);
- var menu = new MenuBar { Menus = new MenuBarItem [] { new ("Menu", "", null) } };
- var status = new StatusBar (new StatusItem [] { new (KeyCode.F1, "~F1~ Help", null) });
- Toplevel top = new ();
- top.Add (win, menu, status);
- RunState rs = Application.Begin (top);
- Assert.Equal (new (0, 0, 80, 25), top.Frame);
- Assert.Equal (new (0, 0, 80, 1), menu.Frame);
- Assert.Equal (new (0, 24, 80, 1), status.Frame);
- Assert.Equal (new (0, 1, 80, 23), win.Frame);
- Assert.Equal (new (0, 20, 29, 1), label.Frame);
- var expected = @"
- Menu
- ┌──────────────────────────────────────────────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │This should be the last line. │
- └──────────────────────────────────────────────────────────────────────────────┘
- F1 Help
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Application.End (rs);
- top.Dispose ();
- }
- #endif
- // TODO: This is a Dim test. Move to Dim tests.
- [Fact]
- [AutoInitShutdown]
- public void Dim_Subtract_Operator_With_Text ()
- {
- Toplevel top = new ();
- var view = new View
- {
- Text = "View with long text",
- X = 0,
- Y = 0,
- Width = 20,
- Height = 1
- };
- var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
- var count = 20;
- // Label is AutoSize == true
- List<Label> listLabels = new ();
- for (var i = 0; i < count; i++)
- {
- field.Text = $"Label {i}";
- var label = new Label { Text = field.Text, X = 0, Y = i + 1 /*, Width = 10*/ };
- view.Add (label);
- Assert.Equal ($"Label {i}", label.Text);
- Assert.Equal ($"Absolute({i + 1})", label.Y.ToString ());
- listLabels.Add (label);
- if (i == 0)
- {
- Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
- view.Height += 1;
- Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
- }
- else
- {
- Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
- view.Height += 1;
- Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
- }
- }
- field.KeyDown += (s, k) =>
- {
- if (k.KeyCode == KeyCode.Enter)
- {
- ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], output);
- Assert.Equal (new (0, 0, 22, count + 4), pos);
- if (count > 0)
- {
- Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
- view.Remove (listLabels [count - 1]);
- listLabels [count - 1].Dispose ();
- listLabels.RemoveAt (count - 1);
- Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
- view.Height -= 1;
- count--;
- if (listLabels.Count > 0)
- {
- field.Text = listLabels [count - 1].Text;
- }
- else
- {
- field.Text = string.Empty;
- }
- }
- Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
- }
- };
- Application.Iteration += (s, a) =>
- {
- while (count > -1)
- {
- field.NewKeyDownEvent (Key.Enter);
- if (count == 0)
- {
- field.NewKeyDownEvent (Key.Enter);
- break;
- }
- }
- Application.RequestStop ();
- };
- var win = new Window ();
- win.Add (view);
- win.Add (field);
- top.Add (win);
- Application.Run (top);
- Assert.Equal (0, count);
- Assert.Equal (count, listLabels.Count);
- top.Dispose ();
- }
- // TODO: This is a Label test. Move to Label tests.
- [Fact]
- [SetupFakeDriver]
- public void Label_Height_Zero_Stays_Zero ()
- {
- ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
- var text = "Label";
- var label = new Label
- {
- Text = text
- };
- label.Width = Dim.Fill () - text.Length;
- label.Height = 0;
- var win = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
- win.Add (label);
- win.BeginInit ();
- win.EndInit ();
- win.LayoutSubviews ();
- win.Draw ();
- Assert.Equal (5, text.Length);
- Assert.Equal (new (0, 0, 3, 0), label.Frame);
- //Assert.Equal (new (5, 1), label.TextFormatter.Size);
- Assert.Single (label.TextFormatter.GetLines ());
- Assert.Equal (new (0, 0, 10, 4), win.Frame);
- var expected = @"
- ┌────────┐
- │ │
- │ │
- └────────┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, 10, 4), pos);
- text = "0123456789";
- Assert.Equal (10, text.Length);
- label.Width = Dim.Fill () - text.Length;
- win.LayoutSubviews ();
- win.Clear ();
- win.Draw ();
- Assert.Equal (Rectangle.Empty, label.Frame);
- // Assert.Equal (new (5, 1), label.TextFormatter.Size);
- //Exception exception = Record.Exception (
- // () => Assert.Equal (
- // new List<string> { string.Empty },
- // label.TextFormatter.GetLines ()
- // )
- // );
- //Assert.Null (exception);
- expected = @"
- ┌────────┐
- │ │
- │ │
- └────────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, 10, 4), pos);
- }
- [Fact]
- [AutoInitShutdown]
- public void Dim_Add_Operator_With_Text ()
- {
- Toplevel top = new ();
- var view = new View
- {
- Text = "View with long text",
- X = 0,
- Y = 0,
- Width = 20,
- Height = 1
- };
- var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
- var count = 0;
- // Label is AutoSize == true
- List<Label> listLabels = new ();
- field.KeyDown += (s, k) =>
- {
- if (k.KeyCode == KeyCode.Enter)
- {
- ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], output);
- Assert.Equal (new (0, 0, 22, count + 4), pos);
- if (count < 20)
- {
- field.Text = $"Label {count}";
- // Label is AutoSize = true
- var label = new Label { Text = field.Text, X = 0, Y = view.Viewport.Height /*, Width = 10*/ };
- view.Add (label);
- Assert.Equal ($"Label {count}", label.Text);
- Assert.Equal ($"Absolute({count + 1})", label.Y.ToString ());
- listLabels.Add (label);
- //if (count == 0) {
- // Assert.Equal ($"Absolute({count})", view.Height.ToString ());
- // view.Height += 2;
- //} else {
- Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
- view.Height += 1;
- //}
- count++;
- }
- Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
- }
- };
- Application.Iteration += (s, a) =>
- {
- while (count < 21)
- {
- field.NewKeyDownEvent (Key.Enter);
- if (count == 20)
- {
- field.NewKeyDownEvent (Key.Enter);
- break;
- }
- }
- Application.RequestStop ();
- };
- var win = new Window ();
- win.Add (view);
- win.Add (field);
- top.Add (win);
- Application.Run (top);
- Assert.Equal (20, count);
- Assert.Equal (count, listLabels.Count);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Label_IsEmpty_False_Minimum_Height ()
- {
- var text = "Label";
- var label = new Label
- {
- //Width = Dim.Fill () - text.Length,
- Text = text
- };
- var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
- win.Add (label);
- var top = new Toplevel ();
- top.Add (win);
- Application.Begin (top);
- ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
- Assert.Equal (5, text.Length);
- Assert.Equal (new (0, 0, 5, 1), label.Frame);
- Assert.Equal (new (5, 1), label.TextFormatter.Size);
- Assert.Equal (["Label"], label.TextFormatter.GetLines ());
- Assert.Equal (new (0, 0, 10, 4), win.Frame);
- Assert.Equal (new (0, 0, 10, 4), Application.Top.Frame);
- var expected = @"
- ┌────────┐
- │Label │
- │ │
- └────────┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, 10, 4), pos);
- text = "0123456789";
- Assert.Equal (10, text.Length);
- //label.Width = Dim.Fill () - text.Length;
- Application.Refresh ();
- Assert.Equal (new (0, 0, 5, 1), label.Frame);
- Assert.Equal (new (5, 1), label.TextFormatter.Size);
- Exception exception = Record.Exception (() => Assert.Single (label.TextFormatter.GetLines ()));
- Assert.Null (exception);
- expected = @"
- ┌────────┐
- │Label │
- │ │
- └────────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, 10, 4), pos);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Label_IsEmpty_False_Never_Return_Null_Lines ()
- {
- var text = "Label";
- var label = new Label
- {
- //Width = Dim.Fill () - text.Length,
- //Height = 1,
- Text = text
- };
- var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
- win.Add (label);
- var top = new Toplevel ();
- top.Add (win);
- Application.Begin (top);
- ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
- Assert.Equal (5, text.Length);
- Assert.Equal (new (0, 0, 5, 1), label.Frame);
- Assert.Equal (new (5, 1), label.TextFormatter.Size);
- Assert.Equal (["Label"], label.TextFormatter.GetLines ());
- Assert.Equal (new (0, 0, 10, 4), win.Frame);
- Assert.Equal (new (0, 0, 10, 4), Application.Top.Frame);
- var expected = @"
- ┌────────┐
- │Label │
- │ │
- └────────┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, 10, 4), pos);
- text = "0123456789";
- Assert.Equal (10, text.Length);
- //label.Width = Dim.Fill () - text.Length;
- Application.Refresh ();
- Assert.Equal (new (0, 0, 5, 1), label.Frame);
- Assert.Equal (new (5, 1), label.TextFormatter.Size);
- Assert.Single (label.TextFormatter.GetLines ());
- expected = @"
- ┌────────┐
- │Label │
- │ │
- └────────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, 10, 4), pos);
- top.Dispose ();
- }
- [Fact]
- public void Label_ResizeView_With_Dim_Absolute ()
- {
- var super = new View
- {
- Width = Dim.Fill (),
- Height = Dim.Fill ()
- };
- var label = new Label ();
- label.Text = "New text";
- super.Add (label);
- super.LayoutSubviews ();
- Rectangle expectedLabelBounds = new (0, 0, 8, 1);
- Assert.Equal (expectedLabelBounds, label.Viewport);
- super.Dispose ();
- }
- }
|