12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301 |
- using System.Runtime.CompilerServices;
- using System.Text;
- using Xunit.Abstractions;
- namespace Terminal.Gui.ViewTests;
- /// <summary>
- /// Tests of the <see cref="View.Text"/> and <see cref="View.TextFormatter"/> properties.
- /// </summary>
- public class TextTests (ITestOutputHelper output)
- {
- // TextFormatter.Size should be empty unless DimAuto is set or ContentSize is set
- [Theory]
- [InlineData ("", 0, 0)]
- [InlineData (" ", 0, 0)]
- [InlineData ("01234", 0, 0)]
- public void TextFormatter_Size_Default (string text, int expectedW, int expectedH)
- {
- var view = new View ();
- view.Text = text;
- view.Layout ();
- Assert.Equal (new (expectedW, expectedH), view.TextFormatter.ConstrainToSize);
- }
- // TextFormatter.Size should track ContentSize (without DimAuto)
- [Theory]
- [InlineData ("", 1, 1)]
- [InlineData (" ", 1, 1)]
- [InlineData ("01234", 1, 1)]
- public void TextFormatter_Size_Tracks_ContentSize (string text, int expectedW, int expectedH)
- {
- var view = new View ();
- view.SetContentSize (new (1, 1));
- view.Text = text;
- view.Layout ();
- Assert.Equal (new (expectedW, expectedH), view.TextFormatter.ConstrainToSize);
- }
- [Fact]
- [SetupFakeDriver]
- public void Setting_With_Height_Horizontal ()
- {
- var top = new View { Width = 25, Height = 25 };
- var label = new Label { Text = "Hello", /* Width = 10, Height = 2, */ ValidatePosDim = true };
- var viewX = new View { Text = "X", X = Pos.Right (label), Width = 1, Height = 1 };
- var viewY = new View { Text = "Y", Y = Pos.Bottom (label), Width = 1, Height = 1 };
- top.Add (label, viewX, viewY);
- top.Layout ();
- Assert.Equal (new (0, 0, 5, 1), label.Frame);
- top.LayoutSubviews ();
- top.Draw ();
- var expected = @"
- HelloX
- Y
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- label.Width = 10;
- label.Height = 2;
- Assert.Equal (new (0, 0, 10, 2), label.Frame);
- top.LayoutSubviews ();
- top.Draw ();
- expected = @"
- Hello X
-
- Y
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- }
- [Fact]
- [AutoInitShutdown]
- public void Setting_With_Height_Vertical ()
- {
- // BUGBUG: Label is Width = Dim.Auto (), Height = Dim.Auto (), so Width & Height are ignored
- var label = new Label
- { /*Width = 2, Height = 10, */
- TextDirection = TextDirection.TopBottom_LeftRight, ValidatePosDim = true
- };
- var viewX = new View { Text = "X", X = Pos.Right (label), Width = 1, Height = 1 };
- var viewY = new View { Text = "Y", Y = Pos.Bottom (label), Width = 1, Height = 1 };
- var top = new Toplevel ();
- top.Add (label, viewX, viewY);
- RunState rs = Application.Begin (top);
- label.Text = "Hello";
- Application.RunIteration (ref rs);
- Assert.Equal (new (0, 0, 1, 5), label.Frame);
- var expected = @"
- HX
- e
- l
- l
- o
- Y
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- label.Width = 2;
- label.Height = 10;
- Application.RunIteration (ref rs);
- Assert.Equal (new (0, 0, 2, 10), label.Frame);
- expected = @"
- H X
- e
- l
- l
- o
-
-
-
-
-
- Y
- "
- ;
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Application.End (rs);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void TextDirection_Toggle ()
- {
- var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
- var view = new View ();
- win.Add (view);
- var top = new Toplevel ();
- top.Add (win);
- RunState rs = Application.Begin (top);
- ((FakeDriver)Application.Driver!).SetBufferSize (15, 15);
- Assert.Equal (new (0, 0, 15, 15), win.Frame);
- Assert.Equal (new (0, 0, 15, 15), win.Margin.Frame);
- Assert.Equal (new (0, 0, 15, 15), win.Border.Frame);
- Assert.Equal (new (1, 1, 13, 13), win.Padding.Frame);
- Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
- Assert.Equal (Rectangle.Empty, view.Frame);
- Assert.Equal ("Absolute(0)", view.X.ToString ());
- Assert.Equal ("Absolute(0)", view.Y.ToString ());
- Assert.Equal ("Absolute(0)", view.Width.ToString ());
- Assert.Equal ("Absolute(0)", view.Height.ToString ());
- var expected = @"
- ┌─────────────┐
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- └─────────────┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- view.Text = "Hello World";
- view.Width = 11;
- view.Height = 1;
- Application.RunIteration (ref rs);
- Assert.Equal (new (0, 0, 11, 1), view.Frame);
- Assert.Equal ("Absolute(0)", view.X.ToString ());
- Assert.Equal ("Absolute(0)", view.Y.ToString ());
- Assert.Equal ("Absolute(11)", view.Width.ToString ());
- Assert.Equal ("Absolute(1)", view.Height.ToString ());
- expected = @"
- ┌─────────────┐
- │Hello World │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- └─────────────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- view.Width = Dim.Auto ();
- view.Height = Dim.Auto ();
- view.Text = "Hello Worlds";
- Application.RunIteration (ref rs);
- int len = "Hello Worlds".Length;
- Assert.Equal (12, len);
- Assert.Equal (new (0, 0, len, 1), view.Frame);
- expected = @"
- ┌─────────────┐
- │Hello Worlds │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- └─────────────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- view.TextDirection = TextDirection.TopBottom_LeftRight;
- Application.RunIteration (ref rs);
- Assert.Equal (new (0, 0, 1, 12), view.Frame);
- Assert.Equal (new (0, 0, 1, 12), view.Frame);
- expected = @"
- ┌─────────────┐
- │H │
- │e │
- │l │
- │l │
- │o │
- │ │
- │W │
- │o │
- │r │
- │l │
- │d │
- │s │
- │ │
- └─────────────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- // Setting to false causes Width and Height to be set to the current ContentSize
- view.Width = 1;
- view.Height = 12;
- Application.RunIteration (ref rs);
- Assert.Equal (new (0, 0, 1, 12), view.Frame);
- view.Width = 12;
- view.Height = 1;
- view.TextFormatter.ConstrainToSize = new (12, 1);
- Application.RunIteration (ref rs);
- Assert.Equal (new (12, 1), view.TextFormatter.ConstrainToSize);
- Assert.Equal (new (0, 0, 12, 1), view.Frame);
- top.ClearViewport ();
- view.SetNeedsDisplay ();
- view.Draw ();
- expected = @" HelloWorlds";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Application.RunIteration (ref rs);
- // TextDirection.TopBottom_LeftRight - Height of 1 and Width of 12 means
- // that the text will be spread "vertically" across 1 line.
- // Hence no space.
- expected = @"
- ┌─────────────┐
- │HelloWorlds │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- └─────────────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- view.PreserveTrailingSpaces = true;
- Application.RunIteration (ref rs);
- Assert.Equal (new (0, 0, 12, 1), view.Frame);
- expected = @"
- ┌─────────────┐
- │Hello Worlds │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- └─────────────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- view.PreserveTrailingSpaces = false;
- Rectangle f = view.Frame;
- view.Width = f.Height;
- view.Height = f.Width;
- view.TextDirection = TextDirection.TopBottom_LeftRight;
- Application.RunIteration (ref rs);
- Assert.Equal (new (0, 0, 1, 12), view.Frame);
- expected = @"
- ┌─────────────┐
- │H │
- │e │
- │l │
- │l │
- │o │
- │ │
- │W │
- │o │
- │r │
- │l │
- │d │
- │s │
- │ │
- └─────────────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- view.Width = Dim.Auto ();
- view.Height = Dim.Auto ();
- Application.RunIteration (ref rs);
- Assert.Equal (new (0, 0, 1, 12), view.Frame);
- expected = @"
- ┌─────────────┐
- │H │
- │e │
- │l │
- │l │
- │o │
- │ │
- │W │
- │o │
- │r │
- │l │
- │d │
- │s │
- │ │
- └─────────────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Application.End (rs);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void View_IsEmpty_False_Minimum_Width ()
- {
- var text = "Views";
- var view = new View
- {
- TextDirection = TextDirection.TopBottom_LeftRight,
- Height = Dim.Fill () - text.Length,
- Text = text
- };
- view.Width = Dim.Auto ();
- view.Height = Dim.Auto ();
- var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
- win.Add (view);
- var top = new Toplevel ();
- top.Add (win);
- RunState rs = Application.Begin (top);
- ((FakeDriver)Application.Driver!).SetBufferSize (4, 10);
- Assert.Equal (5, text.Length);
- Assert.Equal (new (0, 0, 1, 5), view.Frame);
- Assert.Equal (new (1, 5), view.TextFormatter.ConstrainToSize);
- Assert.Equal (new () { "Views" }, view.TextFormatter.GetLines ());
- Assert.Equal (new (0, 0, 4, 10), win.Frame);
- Assert.Equal (new (0, 0, 4, 10), Application.Top.Frame);
- var expected = @"
- ┌──┐
- │V │
- │i │
- │e │
- │w │
- │s │
- │ │
- │ │
- │ │
- └──┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, 4, 10), pos);
- text = "0123456789";
- Assert.Equal (10, text.Length);
- //view.Height = Dim.Fill () - text.Length;
- Application.RunIteration (ref rs);
- Assert.Equal (new (0, 0, 1, 5), view.Frame);
- Assert.Equal (new (1, 5), view.TextFormatter.ConstrainToSize);
- Exception exception = Record.Exception (() => Assert.Single (view.TextFormatter.GetLines ()));
- Assert.Null (exception);
- expected = @"
- ┌──┐
- │V │
- │i │
- │e │
- │w │
- │s │
- │ │
- │ │
- │ │
- └──┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, 4, 10), pos);
- top.Dispose ();
- }
- [Fact]
- [SetupFakeDriver]
- public void DimAuto_Vertical_TextDirection_Wide_Rune ()
- {
- var text = "界View";
- var view = new View
- {
- TextDirection = TextDirection.TopBottom_LeftRight,
- Text = text,
- Width = Dim.Auto (),
- Height = Dim.Auto ()
- };
- view.SetRelativeLayout (new Size (4, 10));
- Assert.Equal (5, text.Length);
- // Vertical text - 2 wide, 5 down
- Assert.Equal (new (0, 0, 2, 5), view.Frame);
- Assert.Equal (new (2, 5), view.TextFormatter.ConstrainToSize);
- Assert.Equal (new () { "界View" }, view.TextFormatter.GetLines ());
- view.Draw ();
- var expected = @"
- 界
- V
- i
- e
- w ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- }
- [Fact]
- [AutoInitShutdown]
- public void Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
- {
- var text = $"0123456789{Environment.NewLine}01234567891";
- var horizontalView = new View
- {
- Width = Dim.Auto (),
- Height = Dim.Auto (),
- Text = text
- };
- var verticalView = new View
- {
- Width = Dim.Auto (),
- Height = Dim.Auto (),
- Y = 3,
- //Height = 11,
- //Width = 2,
- Text = text,
- TextDirection = TextDirection.TopBottom_LeftRight
- };
- var win = new Window
- {
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- Text = "Window"
- };
- win.Add (horizontalView, verticalView);
- var top = new Toplevel ();
- top.Add (win);
- RunState rs = Application.Begin (top);
- ((FakeDriver)Application.Driver!).SetBufferSize (20, 20);
- Assert.Equal (new (0, 0, 11, 2), horizontalView.Frame);
- Assert.Equal (new (0, 3, 2, 11), verticalView.Frame);
- var expected = @"
- ┌──────────────────┐
- │0123456789 │
- │01234567891 │
- │ │
- │00 │
- │11 │
- │22 │
- │33 │
- │44 │
- │55 │
- │66 │
- │77 │
- │88 │
- │99 │
- │ 1 │
- │ │
- │ │
- │ │
- │ │
- └──────────────────┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- verticalView.Text = $"最初の行{Environment.NewLine}二行目";
- Application.RunIteration (ref rs);
- Assert.Equal (new (0, 3, 4, 4), verticalView.Frame);
- expected = @"
- ┌──────────────────┐
- │0123456789 │
- │01234567891 │
- │ │
- │最二 │
- │初行 │
- │の目 │
- │行 │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- └──────────────────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Application.End (rs);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Width_Height_Stay_True_If_TextFormatter_Size_Fit ()
- {
- var text = "Finish 終";
- var horizontalView = new View
- {
- Id = "horizontalView",
- Width = Dim.Auto (), Height = Dim.Auto (), Text = text
- };
- var verticalView = new View
- {
- Id = "verticalView",
- Y = 3,
- Width = Dim.Auto (),
- Height = Dim.Auto (),
- Text = text,
- TextDirection = TextDirection.TopBottom_LeftRight
- };
- var win = new Window { Id = "win", Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window" };
- win.Add (horizontalView, verticalView);
- var top = new Toplevel ();
- top.Add (win);
- RunState rs = Application.Begin (top);
- ((FakeDriver)Application.Driver!).SetBufferSize (22, 22);
- Assert.Equal (new (text.GetColumns (), 1), horizontalView.TextFormatter.ConstrainToSize);
- Assert.Equal (new (2, 8), verticalView.TextFormatter.ConstrainToSize);
- //Assert.Equal (new (0, 0, 10, 1), horizontalView.Frame);
- //Assert.Equal (new (0, 3, 10, 9), verticalView.Frame);
- var expected = @"
- ┌────────────────────┐
- │Finish 終 │
- │ │
- │ │
- │F │
- │i │
- │n │
- │i │
- │s │
- │h │
- │ │
- │終 │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- └────────────────────┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- verticalView.Text = "最初の行二行目";
- Application.RunIteration (ref rs);
- // height was initialized with 8 and can only grow or keep initial value
- Assert.Equal (new (0, 3, 2, 7), verticalView.Frame);
- expected = @"
- ┌────────────────────┐
- │Finish 終 │
- │ │
- │ │
- │最 │
- │初 │
- │の │
- │行 │
- │二 │
- │行 │
- │目 │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- └────────────────────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Application.End (rs);
- top.Dispose ();
- }
- [Fact]
- [AutoInitShutdown]
- public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
- {
- var lbl = new Label { Text = "123" };
- var top = new Toplevel ();
- top.Add (lbl);
- RunState rs = Application.Begin (top);
- Application.RunIteration (ref rs);
- Assert.Equal (new (0, 0, 3, 1), lbl.Frame);
- Assert.Equal ("123 ", GetContents ());
- lbl.Text = "12";
- lbl.Layout ();
- Assert.Equal (new (0, 0, 2, 1), lbl.Frame);
- Assert.Equal (new (0, 0, 2, 1), lbl._needsDisplayRect);
- Assert.Equal (new (0, 0, 80, 25), lbl.SuperView._needsDisplayRect);
- Assert.True (lbl.SuperView.NeedsLayout);
- Application.RunIteration (ref rs);
- Assert.Equal ("12 ", GetContents ());
- string GetContents ()
- {
- var text = "";
- for (var i = 0; i < 4; i++)
- {
- text += Application.Driver?.Contents [0, i].Rune;
- }
- return text;
- }
- Application.End (rs);
- top.Dispose ();
- }
- [Theory]
- [AutoInitShutdown]
- [InlineData (true)]
- [InlineData (false)]
- public void View_Draw_Horizontal_Simple_TextAlignments (bool autoSize)
- {
- var text = "Hello World";
- var width = 20;
- var lblLeft = new View
- {
- Text = text,
- Width = width,
- Height = 1
- };
- if (autoSize)
- {
- lblLeft.Width = Dim.Auto ();
- lblLeft.Height = Dim.Auto ();
- }
- var lblCenter = new View
- {
- Text = text,
- Y = 1,
- Width = width,
- Height = 1,
- TextAlignment = Alignment.Center
- };
- if (autoSize)
- {
- lblCenter.Width = Dim.Auto ();
- lblCenter.Height = Dim.Auto ();
- }
- var lblRight = new View
- {
- Text = text,
- Y = 2,
- Width = width,
- Height = 1,
- TextAlignment = Alignment.End
- };
- if (autoSize)
- {
- lblRight.Width = Dim.Auto ();
- lblRight.Height = Dim.Auto ();
- }
- var lblJust = new View
- {
- Text = text,
- Y = 3,
- Width = width,
- Height = 1,
- TextAlignment = Alignment.Fill
- };
- if (autoSize)
- {
- lblJust.Width = Dim.Auto ();
- lblJust.Height = Dim.Auto ();
- }
- var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
- frame.Add (lblLeft, lblCenter, lblRight, lblJust);
- var top = new Toplevel ();
- top.Add (frame);
- Application.Begin (top);
- ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
- // frame.Width is width + border wide (20 + 2) and 6 high
- if (autoSize)
- {
- Size expectedSize = new (11, 1);
- Assert.Equal (expectedSize, lblLeft.TextFormatter.ConstrainToSize);
- Assert.Equal (expectedSize, lblCenter.TextFormatter.ConstrainToSize);
- Assert.Equal (expectedSize, lblRight.TextFormatter.ConstrainToSize);
- Assert.Equal (expectedSize, lblJust.TextFormatter.ConstrainToSize);
- }
- else
- {
- Size expectedSize = new (width, 1);
- Assert.Equal (expectedSize, lblLeft.TextFormatter.ConstrainToSize);
- Assert.Equal (expectedSize, lblCenter.TextFormatter.ConstrainToSize);
- Assert.Equal (expectedSize, lblRight.TextFormatter.ConstrainToSize);
- Assert.Equal (expectedSize, lblJust.TextFormatter.ConstrainToSize);
- }
- Assert.Equal (new (0, 0, width + 2, 6), frame.Frame);
- string expected;
- if (autoSize)
- {
- expected = @"
- ┌────────────────────┐
- │Hello World │
- │Hello World │
- │Hello World │
- │Hello World │
- └────────────────────┘
- ";
- }
- else
- {
- expected = @"
- ┌────────────────────┐
- │Hello World │
- │ Hello World │
- │ Hello World│
- │Hello World│
- └────────────────────┘
- ";
- }
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, width + 2, 6), pos);
- top.Dispose ();
- }
- [Theory]
- [AutoInitShutdown]
- [InlineData (true)]
- [InlineData (false)]
- public void View_Draw_Vertical_Simple_TextAlignments (bool autoSize)
- {
- var text = "Hello World";
- var height = 20;
- var lblLeft = new View
- {
- Text = text,
- Width = 1,
- Height = height,
- TextDirection = TextDirection.TopBottom_LeftRight
- };
- if (autoSize)
- {
- lblLeft.Width = Dim.Auto ();
- lblLeft.Height = Dim.Auto ();
- }
- var lblCenter = new View
- {
- Text = text,
- X = 2,
- Width = 1,
- Height = height,
- TextDirection = TextDirection.TopBottom_LeftRight,
- VerticalTextAlignment = Alignment.Center
- };
- if (autoSize)
- {
- lblCenter.Width = Dim.Auto ();
- lblCenter.Height = Dim.Auto ();
- }
- var lblRight = new View
- {
- Text = text,
- X = 4,
- Width = 1,
- Height = height,
- TextDirection = TextDirection.TopBottom_LeftRight,
- VerticalTextAlignment = Alignment.End
- };
- if (autoSize)
- {
- lblRight.Width = Dim.Auto ();
- lblRight.Height = Dim.Auto ();
- }
- var lblJust = new View
- {
- Text = text,
- X = 6,
- Width = 1,
- Height = height,
- TextDirection = TextDirection.TopBottom_LeftRight,
- VerticalTextAlignment = Alignment.Fill
- };
- if (autoSize)
- {
- lblJust.Width = Dim.Auto ();
- lblJust.Height = Dim.Auto ();
- }
- var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
- frame.Add (lblLeft, lblCenter, lblRight, lblJust);
- var top = new Toplevel ();
- top.Add (frame);
- Application.Begin (top);
- ((FakeDriver)Application.Driver!).SetBufferSize (9, height + 2);
- if (autoSize)
- {
- Assert.Equal (new (1, 11), lblLeft.TextFormatter.ConstrainToSize);
- Assert.Equal (new (1, 11), lblCenter.TextFormatter.ConstrainToSize);
- Assert.Equal (new (1, 11), lblRight.TextFormatter.ConstrainToSize);
- Assert.Equal (new (1, 11), lblJust.TextFormatter.ConstrainToSize);
- Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
- }
- else
- {
- Assert.Equal (new (1, height), lblLeft.TextFormatter.ConstrainToSize);
- Assert.Equal (new (1, height), lblCenter.TextFormatter.ConstrainToSize);
- Assert.Equal (new (1, height), lblRight.TextFormatter.ConstrainToSize);
- Assert.Equal (new (1, height), lblJust.TextFormatter.ConstrainToSize);
- Assert.Equal (new (0, 0, 9, height + 2), frame.Frame);
- }
- string expected;
- if (autoSize)
- {
- expected = @"
- ┌───────┐
- │H H H H│
- │e e e e│
- │l l l l│
- │l l l l│
- │o o o o│
- │ │
- │W W W W│
- │o o o o│
- │r r r r│
- │l l l l│
- │d d d d│
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- └───────┘
- ";
- }
- else
- {
- expected = @"
- ┌───────┐
- │H H│
- │e e│
- │l l│
- │l l│
- │o H o│
- │ e │
- │W l │
- │o l │
- │r o │
- │l H │
- │d W e │
- │ o l │
- │ r l │
- │ l o │
- │ d │
- │ W W│
- │ o o│
- │ r r│
- │ l l│
- │ d d│
- └───────┘
- ";
- }
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new (0, 0, 9, height + 2), pos);
- top.Dispose ();
- }
- // Test that View.PreserveTrailingSpaces removes trailing spaces
- [Fact]
- public void PreserveTrailingSpaces_Removes_Trailing_Spaces ()
- {
- var view = new View { Text = "Hello World " };
- Assert.Equal ("Hello World ", view.TextFormatter.Text);
- view.TextFormatter.WordWrap = true;
- view.TextFormatter.ConstrainToSize = new (5, 3);
- view.PreserveTrailingSpaces = false;
- Assert.Equal ($"Hello{Environment.NewLine}World", view.TextFormatter.Format ());
- view.PreserveTrailingSpaces = true;
- Assert.Equal ($"Hello{Environment.NewLine} {Environment.NewLine}World", view.TextFormatter.Format ());
- }
- // View.PreserveTrailingSpaces Gets or sets whether trailing spaces at the end of word-wrapped lines are preserved
- // or not when <see cref="TextFormatter.WordWrap"/> is enabled.
- // If <see langword="true"/> trailing spaces at the end of wrapped lines will be removed when
- // <see cref = "Text" / > is formatted for display.The default is <see langword = "false" / >.
- [Fact]
- public void PreserveTrailingSpaces_Set_Get ()
- {
- var view = new View { Text = "Hello World" };
- Assert.False (view.PreserveTrailingSpaces);
- view.PreserveTrailingSpaces = true;
- Assert.True (view.PreserveTrailingSpaces);
- }
- // Setting TextFormatter DOES NOT update Text
- [Fact]
- public void SettingTextFormatterDoesNotUpdateText ()
- {
- var view = new View ();
- view.TextFormatter.Text = "Hello World";
- Assert.True (string.IsNullOrEmpty (view.Text));
- }
- // Setting Text updates TextFormatter
- [Fact]
- public void SettingTextUpdatesTextFormatter ()
- {
- var view = new View { Text = "Hello World" };
- Assert.Equal ("Hello World", view.Text);
- Assert.Equal ("Hello World", view.TextFormatter.Text);
- }
- // Setting Text does NOT set the HotKey
- [Fact]
- public void Text_Does_Not_Set_HotKey ()
- {
- var view = new View { HotKeySpecifier = (Rune)'_', Text = "_Hello World" };
- Assert.NotEqual (Key.H, view.HotKey);
- }
- // Test that TextFormatter is init only
- [Fact]
- public void TextFormatterIsInitOnly ()
- {
- var view = new View ();
- // Use reflection to ensure the TextFormatter property is `init` only
- Assert.Contains (
- typeof (IsExternalInit),
- typeof (View).GetMethod ("set_TextFormatter")
- .ReturnParameter.GetRequiredCustomModifiers ());
- }
- // Test that the Text property is set correctly.
- [Fact]
- public void TextProperty ()
- {
- var view = new View { Text = "Hello World" };
- Assert.Equal ("Hello World", view.Text);
- }
- // Test view.UpdateTextFormatterText overridden in a subclass updates TextFormatter.Text
- [Fact]
- public void UpdateTextFormatterText_Overridden ()
- {
- var view = new TestView { Text = "Hello World" };
- Assert.Equal ("Hello World", view.Text);
- Assert.Equal (">Hello World<", view.TextFormatter.Text);
- }
- private class TestView : View
- {
- protected override void UpdateTextFormatterText () { TextFormatter.Text = $">{Text}<"; }
- }
- [Fact]
- public void TextDirection_Horizontal_Dims_Correct ()
- {
- // Initializes a view with a vertical direction
- var view = new View
- {
- Text = "01234",
- TextDirection = TextDirection.LeftRight_TopBottom,
- Width = Dim.Auto (DimAutoStyle.Text),
- Height = Dim.Auto (DimAutoStyle.Text)
- };
- Assert.True (view.NeedsLayout);
- view.Layout ();
- Assert.Equal (new (0, 0, 5, 1), view.Frame);
- Assert.Equal (new (0, 0, 5, 1), view.Viewport);
- view.BeginInit ();
- view.EndInit ();
- Assert.Equal (new (0, 0, 5, 1), view.Frame);
- Assert.Equal (new (0, 0, 5, 1), view.Viewport);
- }
- // BUGBUG: this is a temporary test that helped identify #3469 - It needs to be expanded upon (and renamed)
- [Fact]
- public void TextDirection_Horizontal_Dims_Correct_WidthAbsolute ()
- {
- var view = new View
- {
- Text = "01234",
- TextDirection = TextDirection.LeftRight_TopBottom,
- TextAlignment = Alignment.Center,
- Width = 10,
- Height = Dim.Auto (DimAutoStyle.Text)
- };
- view.BeginInit ();
- view.EndInit ();
- Assert.Equal (new (0, 0, 10, 1), view.Frame);
- Assert.Equal (new (0, 0, 10, 1), view.Viewport);
- Assert.Equal (new (10, 1), view.TextFormatter.ConstrainToSize);
- }
- [Fact]
- public void TextDirection_Vertical_Dims_Correct ()
- {
- // Initializes a view with a vertical direction
- var view = new View
- {
- TextDirection = TextDirection.TopBottom_LeftRight,
- Text = "01234",
- Width = Dim.Auto (DimAutoStyle.Text),
- Height = Dim.Auto (DimAutoStyle.Text)
- };
- view.Layout ();
- Assert.Equal (new (0, 0, 1, 5), view.Frame);
- Assert.Equal (new (0, 0, 1, 5), view.Viewport);
- }
- [Fact]
- [SetupFakeDriver]
- public void Narrow_Wide_Runes ()
- {
- ((FakeDriver)Application.Driver!).SetBufferSize (32, 32);
- var top = new View { Width = 32, Height = 32 };
- var text = $"First line{Environment.NewLine}Second line";
- var horizontalView = new View { Width = 20, Height = 1, Text = text };
- // Autosize is off, so we have to explicitly set TextFormatter.Size
- horizontalView.TextFormatter.ConstrainToSize = new (20, 1);
- var verticalView = new View
- {
- Y = 3,
- Height = 20,
- Width = 1,
- Text = text,
- TextDirection = TextDirection.TopBottom_LeftRight
- };
- // Autosize is off, so we have to explicitly set TextFormatter.Size
- verticalView.TextFormatter.ConstrainToSize = new (1, 20);
- var frame = new FrameView { Width = Dim.Fill (), Height = Dim.Fill (), Text = "Window" };
- frame.Add (horizontalView, verticalView);
- top.Add (frame);
- top.BeginInit ();
- top.EndInit ();
- Assert.Equal (new (0, 0, 20, 1), horizontalView.Frame);
- Assert.Equal (new (0, 3, 1, 20), verticalView.Frame);
- top.Draw ();
- var expected = @"
- ┌──────────────────────────────┐
- │First line Second li │
- │ │
- │ │
- │F │
- │i │
- │r │
- │s │
- │t │
- │ │
- │l │
- │i │
- │n │
- │e │
- │ │
- │S │
- │e │
- │c │
- │o │
- │n │
- │d │
- │ │
- │l │
- │i │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- └──────────────────────────────┘
- ";
- Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- verticalView.Text = $"最初の行{Environment.NewLine}二行目";
- Assert.True (verticalView.TextFormatter.NeedsFormat);
- // Autosize is off, so we have to explicitly set TextFormatter.Size
- // We know these glpyhs are 2 cols wide, so we need to widen the view
- verticalView.Width = 2;
- verticalView.TextFormatter.ConstrainToSize = new (2, 20);
- Assert.True (verticalView.TextFormatter.NeedsFormat);
- top.Draw ();
- Assert.Equal (new (0, 3, 2, 20), verticalView.Frame);
- expected = @"
- ┌──────────────────────────────┐
- │First line Second li │
- │ │
- │ │
- │最 │
- │初 │
- │の │
- │行 │
- │ │
- │二 │
- │行 │
- │目 │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- │ │
- └──────────────────────────────┘
- ";
- pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- }
- }
|