12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481 |
- using System.ComponentModel;
- using Microsoft.VisualStudio.TestPlatform.Utilities;
- using Xunit.Abstractions;
- namespace Terminal.Gui.ViewsTests;
- public class LabelTests
- {
- private readonly ITestOutputHelper _output;
- public LabelTests (ITestOutputHelper output) { _output = 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 MouseEvent () { 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, CancelEventArgs 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);
- }
- [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);
- }
- [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);
- }
- [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 Rectangle (0, 0, 0, 0), label.Frame);
- Assert.Equal (KeyCode.Null, label.HotKey);
- }
- [Fact]
- [AutoInitShutdown]
- public void Label_Draw_Fill_Remaining_AutoSize_False ()
- {
- Size 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 Rectangle (new Point (0, 1), tfSize), label.GetNormalColor (), label.ColorScheme.HotNormal);
- tf2.Draw (new Rectangle (new Point (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 Rectangle (new Point (0, 1), tfSize), label.GetNormalColor (), label.ColorScheme.HotNormal);
- tf2.Text = "This TextFormatter (tf2) is rewritten.";
- tf2.Draw (new Rectangle (new Point (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
- );
- }
- [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 Rectangle (0, 0, 16, 1), label.Frame);
- var expected = @"
- Demo Simple Rune
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
- Assert.Equal (new Rectangle (0, 0, 16, 1), pos);
- }
- [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 Rectangle (0, 0, 1, 16), pos);
- }
- [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 Rectangle (0, 0, 2, 7), pos);
- }
- [Fact]
- [AutoInitShutdown]
- 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]
- [AutoInitShutdown]
- 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 Rectangle (0, 0, 12, 1), label.Viewport);
- var expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你 │
- │ │
- └────────────────────────────┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
- Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
- }
- [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 Rectangle (0, 0, 12, 1), label.Viewport);
- var expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你 │
- │ │
- └────────────────────────────┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
- Assert.Equal (new Rectangle (0, 0, 30, 5), pos);
- }
- [Fact]
- [SetupFakeDriver]
- public void Full_Border ()
- {
- var label = new Label { BorderStyle = LineStyle.Single , Text = "Test",} ;
- label.BeginInit();
- label.EndInit();
- label.SetRelativeLayout (Application.Driver.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 Thickness (0, 1, 0, 0);
- label.Border.Thickness = new Thickness (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
- );
- }
- [Fact]
- [AutoInitShutdown]
- public void Without_Top_Border ()
- {
- var label = new Label { Text = "Test", /* Width = 6, Height = 3, */BorderStyle = LineStyle.Single };
- label.Border.Thickness = new Thickness (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
- );
- }
- // 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 Rectangle (0, 0, 40, 10), top.Frame);
- Assert.Equal (new Rectangle (0, 0, 40, 10), win.Frame);
- Assert.Equal (new Rectangle (0, 7, 29, 1), label.Frame);
- var expected = @"
- ┌──────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │This should be the last line. │
- └──────────────────────────────────────┘
- "
- ;
- TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
- Application.End (rs);
- }
- [Fact]
- [AutoInitShutdown]
- public void AnchorEnd_Better_Than_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"],
- X = 0,
- Y = Pos.AnchorEnd (1)
- };
- 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 Rectangle (0, 0, 80, 25), top.Frame);
- Assert.Equal (new Rectangle (0, 0, 80, 1), menu.Frame);
- Assert.Equal (new Rectangle (0, 24, 80, 1), status.Frame);
- Assert.Equal (new Rectangle (0, 1, 80, 23), win.Frame);
- Assert.Equal (new Rectangle (0, 20, 29, 1), label.Frame);
- var expected = @"
- Menu
- ┌──────────────────────────────────────────────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │This should be the last line. │
- └──────────────────────────────────────────────────────────────────────────────┘
- F1 Help
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
- Application.End (rs);
- }
- // 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 Rectangle (0, 0, 40, 10), top.Frame);
- Assert.Equal (new Rectangle (0, 0, 40, 10), win.Frame);
- Assert.Equal (new Rectangle (0, 7, 29, 1), label.Frame);
- var expected = @"
- ┌──────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │This should be the last line. │
- └──────────────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
- Application.End (rs);
- }
- // 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 Rectangle (0, 0, 80, 25), top.Frame);
- Assert.Equal (new Rectangle (0, 0, 80, 1), menu.Frame);
- Assert.Equal (new Rectangle (0, 24, 80, 1), status.Frame);
- Assert.Equal (new Rectangle (0, 1, 80, 23), win.Frame);
- Assert.Equal (new Rectangle (0, 20, 29, 1), label.Frame);
- var expected = @"
- Menu
- ┌──────────────────────────────────────────────────────────────────────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │This should be the last line. │
- └──────────────────────────────────────────────────────────────────────────────┘
- F1 Help
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
- Application.End (rs);
- }
- // 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 Rectangle (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);
- }
- // 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 Rectangle (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);
- }
- [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);
- }
- [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);
- }
- [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 ();
- }
- }
|