123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908 |
- using System;
- using Xunit;
- using Xunit.Abstractions;
- namespace Terminal.Gui.ViewsTests {
- public class LabelTests {
- readonly ITestOutputHelper output;
- public LabelTests (ITestOutputHelper output)
- {
- this.output = output;
- }
- [Fact, AutoInitShutdown]
- public void Constructors_Defaults ()
- {
- var label = new Label ();
- Assert.Equal (string.Empty, label.Text);
- Application.Top.Add (label);
- var rs = Application.Begin (Application.Top);
- Assert.Equal (TextAlignment.Left, label.TextAlignment);
- Assert.True (label.AutoSize);
- Assert.False (label.CanFocus);
- Assert.Equal (new Rect (0, 0, 0, 1), label.Frame);
- Assert.Equal (Key.Null, label.HotKey);
- var expected = @"";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Application.End (rs);
- label = new Label ("ARGS", true) { Text = "Test" };
- Assert.True (label.AutoSize);
- Assert.Equal ("Test", label.Text);
- Application.Top.Add (label);
- rs = Application.Begin (Application.Top);
- Assert.Equal ("Test", label.TextFormatter.Text);
- Assert.Equal (new Rect (0, 0, 4, 1), label.Frame);
- expected = @"
- Test
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Application.End (rs);
- label = new Label (3, 4, "Test", true);
- Assert.Equal ("Test", label.Text);
- Application.Top.Add (label);
- rs = Application.Begin (Application.Top);
- Assert.Equal ("Test", label.TextFormatter.Text);
- Assert.Equal (new Rect (3, 4, 4, 1), label.Frame);
- expected = @"
- Test
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Application.End (rs);
- }
- [Fact]
- public void TestAssignTextToLabel ()
- {
- View b = new Label () { Text = "heya" };
- Assert.Equal ("heya", b.Text);
- Assert.True (b.TextFormatter.Text.Contains ("heya"));
- b.Text = "heyb";
- Assert.Equal ("heyb", b.Text);
- Assert.True (b.TextFormatter.Text.Contains ("heyb"));
- // with cast
- Assert.Equal ("heyb", ((Label)b).Text);
- }
- [Fact, AutoInitShutdown]
- public void Update_Only_On_Or_After_Initialize ()
- {
- var label = new Label ("Say Hello 你") {
- X = Pos.Center (),
- Y = Pos.Center ()
- };
- var win = new Window () {
- Width = Dim.Fill (),
- Height = Dim.Fill ()
- };
- win.Add (label);
- Application.Top.Add (win);
- Assert.False (label.IsInitialized);
- Application.Begin (Application.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 Rect (0, 0, 12, 1), label.Bounds);
- var expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你 │
- │ │
- └────────────────────────────┘
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (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 你",
- AutoSize = true
- };
- var win = new Window () {
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- };
- win.Add (label);
- Application.Top.Add (win);
- Assert.False (label.IsInitialized);
- Application.Begin (Application.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 Rect (0, 0, 12, 1), label.Bounds);
- var expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你 │
- │ │
- └────────────────────────────┘
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 30, 5), pos);
- }
- [Fact, AutoInitShutdown]
- public void AutoSize_Stays_True_With_EmptyText ()
- {
- var label = new Label () {
- X = Pos.Center (),
- Y = Pos.Center (),
- AutoSize = true
- };
- var win = new Window () {
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- };
- win.Add (label);
- Application.Top.Add (win);
- Assert.True (label.AutoSize);
- label.Text = "Say Hello 你";
- Assert.True (label.AutoSize);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- var expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你 │
- │ │
- └────────────────────────────┘
- ";
- 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);
- Application.Top.Add (win);
- Assert.True (label.AutoSize);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- var expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你 │
- │ │
- └────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.True (label.AutoSize);
- label.Text = "Say Hello 你 changed";
- Assert.True (label.AutoSize);
- Application.Refresh ();
- expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你 changed │
- │ │
- └────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void AutoSize_Stays_True_AnchorEnd ()
- {
- var label = new Label () {
- Y = Pos.Center (),
- Text = "Say Hello 你",
- AutoSize = true
- };
- label.X = Pos.AnchorEnd () - Pos.Function (() => TextFormatter.GetTextWidth (label.TextFormatter.Text));
- var win = new Window () {
- Width = Dim.Fill (),
- Height = Dim.Fill (),
- };
- win.Add (label);
- Application.Top.Add (win);
- Assert.True (label.AutoSize);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- var expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你│
- │ │
- └────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.True (label.AutoSize);
- label.Text = "Say Hello 你 changed";
- Assert.True (label.AutoSize);
- Application.Refresh ();
- expected = @"
- ┌────────────────────────────┐
- │ │
- │ Say Hello 你 changed│
- │ │
- └────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void Pos_Center_Layout_AutoSize_True ()
- {
- var Label = new Label ("012345678901") {
- X = Pos.Center (),
- Y = Pos.Center (),
- };
- var win = new Window () {
- Width = Dim.Fill (),
- Height = Dim.Fill ()
- };
- win.Add (Label);
- Application.Top.Add (win);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- Assert.True (Label.AutoSize);
- //Assert.Equal (new Rect (5, 1, 18, 1), Label.Frame);
- var expected = @"
- ┌────────────────────────────┐
- │ │
- │ 012345678901 │
- │ │
- └────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- }
- [Fact, AutoInitShutdown]
- public void Pos_Center_Layout_AutoSize_False ()
- {
- var Label = new Label ("012345678901") {
- X = Pos.Center (),
- Y = Pos.Center (),
- AutoSize = false,
- Width = 20,
- TextAlignment = TextAlignment.Centered
- };
- var win = new Window () {
- Width = Dim.Fill (),
- Height = Dim.Fill ()
- };
- win.Add (Label);
- Application.Top.Add (win);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
- Assert.False (Label.AutoSize);
- Assert.Equal (new Rect (4, 1, 20, 1), Label.Frame);
- var expected = @"
- ┌────────────────────────────┐
- │ │
- │ 012345678901 │
- │ │
- └────────────────────────────┘
- ";
- TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- }
- //[Fact, AutoInitShutdown]
- //public void Label_HotKeyChanged_EventFires ()
- //{
- // var label = new Label ("Yar");
- // object sender = null;
- // KeyChangedEventArgs args = null;
- // label.HotKeyChanged += (s, e) =>{
- // sender = s;
- // args = e;
- // };
- // label.HotKey = Key.r;
- // Assert.Same (label, sender);
- // Assert.Equal (Key.Y, 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 = Key.r;
- Assert.Same (label, sender);
- Assert.Equal (Key.Null, args.OldKey);
- Assert.Equal (Key.r, args.NewKey);
- }
- [Fact, AutoInitShutdown]
- public void Label_WordWrap_PreserveTrailingSpaces_Horizontal_With_Simple_Runes ()
- {
- var text = "A sentence has words.";
- var width = 3;
- var height = 8;
- var wrappedLines = TextFormatter.WordWrapText (text, width, true);
- var breakLines = "";
- foreach (var line in wrappedLines) breakLines += $"{line}{Environment.NewLine}";
- var label = new Label (breakLines) { Width = Dim.Fill (), Height = Dim.Fill () };
- var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
- frame.Add (label);
- Application.Top.Add (frame);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
- Assert.True (label.AutoSize);
- Assert.Equal (new Rect (0, 0, width, height + 1), label.Frame);
- Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
- var expected = @"
- ┌───┐
- │A │
- │sen│
- │ten│
- │ce │
- │has│
- │ │
- │wor│
- │ds.│
- └───┘
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
- }
- [Fact, AutoInitShutdown]
- public void Label_WordWrap_PreserveTrailingSpaces_Vertical_With_Simple_Runes ()
- {
- var text = "A sentence has words.";
- var width = 8;
- var height = 3;
- var wrappedLines = TextFormatter.WordWrapText (text, height, true);
- var breakLines = "";
- for (int i = 0; i < wrappedLines.Count; i++) breakLines += $"{wrappedLines [i]}{(i < wrappedLines.Count - 1 ? Environment.NewLine : string.Empty)}";
- var label = new Label (breakLines) {
- TextDirection = TextDirection.TopBottom_LeftRight,
- Width = Dim.Fill (),
- Height = Dim.Fill ()
- };
- var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
- frame.Add (label);
- Application.Top.Add (frame);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
- Assert.True (label.AutoSize);
- Assert.Equal (new Rect (0, 0, width, height), label.Frame);
- Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
- var expected = @"
- ┌────────┐
- │Astch wd│
- │ eeea os│
- │ nn s r.│
- └────────┘
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
- }
- [Fact, AutoInitShutdown]
- public void Label_WordWrap_PreserveTrailingSpaces_Horizontal_With_Wide_Runes ()
- {
- var text = "文に は言葉 があり ます。";
- var width = 6;
- var height = 8;
- var wrappedLines = TextFormatter.WordWrapText (text, width, true);
- var breakLines = "";
- foreach (var line in wrappedLines) breakLines += $"{line}{Environment.NewLine}";
- var label = new Label (breakLines) { Width = Dim.Fill (), Height = Dim.Fill () };
- var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
- frame.Add (label);
- Application.Top.Add (frame);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
- Assert.True (label.AutoSize);
- Assert.Equal (new Rect (0, 0, width, height), label.Frame);
- Assert.Equal (new Size (width, height), label.TextFormatter.Size);
- Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
- var expected = @"
- ┌──────┐
- │文に │
- │は言葉│
- │ があ │
- │り ま │
- │す。 │
- │ │
- │ │
- │ │
- └──────┘
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
- }
- [Fact, AutoInitShutdown]
- public void Label_WordWrap_PreserveTrailingSpaces_Vertical_With_Wide_Runes ()
- {
- var text = "文に は言葉 があり ます。";
- var width = 8;
- var height = 4;
- var wrappedLines = TextFormatter.WordWrapText (text, width, true);
- var breakLines = "";
- for (int i = 0; i < wrappedLines.Count; i++) breakLines += $"{wrappedLines [i]}{(i < wrappedLines.Count - 1 ? Environment.NewLine : string.Empty)}";
- var label = new Label (breakLines) {
- TextDirection = TextDirection.TopBottom_LeftRight,
- Width = Dim.Fill (),
- Height = Dim.Fill ()
- };
- var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
- frame.Add (label);
- Application.Top.Add (frame);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
- Assert.True (label.AutoSize);
- Assert.Equal (new Rect (0, 0, width, height), label.Frame);
- Assert.Equal (new Rect (0, 0, width + 2, height + 2), frame.Frame);
- var expected = @"
- ┌────────┐
- │文言あす│
- │に葉り。│
- │ │
- │はがま │
- └────────┘
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, width + 2, height + 2), pos);
- }
- [Fact, AutoInitShutdown]
- public void Label_Draw_Horizontal_Simple_TextAlignments_Justified ()
- {
- var text = "01234 01234";
- var width = 20;
- var lblJust = new Label (text) { Y = 0, Width = width, TextAlignment = TextAlignment.Justified };
- var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
- frame.Add (lblJust);
- Application.Top.Add (frame);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 3);
- var expected = @"
- ┌────────────────────┐
- │01234 01234│
- └────────────────────┘
- ";
- Assert.Equal (new Rect (0, 0, width, 1), lblJust.Frame);
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, width + 2, 3), pos);
- }
- [Fact, AutoInitShutdown]
- public void Label_Draw_Horizontal_Simple_Runes ()
- {
- var label = new Label ("Demo Simple Rune");
- Application.Top.Add (label);
- Application.Begin (Application.Top);
- Assert.True (label.AutoSize);
- Assert.Equal (new Rect (0, 0, 16, 1), label.Frame);
- var expected = @"
- Demo Simple Rune
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 16, 1), pos);
- }
- [Fact, AutoInitShutdown]
- public void Label_Draw_Vertical_Simple_Runes ()
- {
- var label = new Label ("Demo Simple Rune") {
- TextDirection = TextDirection.TopBottom_LeftRight
- };
- Application.Top.Add (label);
- Application.Begin (Application.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
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 1, 16), pos);
- }
- [Fact, AutoInitShutdown]
- public void Label_Draw_Horizontal_Wide_Runes ()
- {
- var label = new Label ("デモエムポンズ");
- Application.Top.Add (label);
- Application.Begin (Application.Top);
- Assert.True (label.AutoSize);
- Assert.Equal (new Rect (0, 0, 14, 1), label.Frame);
- var expected = @"
- デモエムポンズ
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 14, 1), pos);
- }
- [Fact, AutoInitShutdown]
- public void Label_Draw_Vertical_Wide_Runes ()
- {
- var label = new Label ("デモエムポンズ") {
- TextDirection = TextDirection.TopBottom_LeftRight
- };
- Application.Top.Add (label);
- Application.Begin (Application.Top);
- var expected = @"
- デ
- モ
- エ
- ム
- ポ
- ン
- ズ
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 2, 7), pos);
- }
- [Fact, AutoInitShutdown]
- public void Label_Draw_Vertical_Wide_Runes_With_ForceValidatePosDim ()
- {
- var label = new Label ("デモエムポンズ") {
- Width = Dim.Fill (),
- Height = Dim.Percent (50f),
- TextDirection = TextDirection.TopBottom_LeftRight,
- ForceValidatePosDim = true
- };
- Application.Top.Add (label);
- Application.Begin (Application.Top);
- Assert.True (label.AutoSize);
- Assert.Equal (new Rect (0, 0, 80, 12), label.Frame);
- var expected = @"
- デ
- モ
- エ
- ム
- ポ
- ン
- ズ
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 2, 7), pos);
- }
- [Fact, AutoInitShutdown]
- public void Label_Draw_Horizontal_Simple_TextAlignments ()
- {
- var text = "Hello World";
- var width = 20;
- var lblLeft = new Label (text) { Width = width };
- var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered };
- var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right };
- var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified };
- var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
- frame.Add (lblLeft, lblCenter, lblRight, lblJust);
- Application.Top.Add (frame);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
- Assert.True (lblLeft.AutoSize);
- Assert.True (lblCenter.AutoSize);
- Assert.True (lblRight.AutoSize);
- Assert.True (lblJust.AutoSize);
- Assert.Equal (new Rect (0, 0, width, 1), lblLeft.Frame);
- Assert.Equal (new Rect (0, 1, width, 1), lblCenter.Frame);
- Assert.Equal (new Rect (0, 2, width, 1), lblRight.Frame);
- Assert.Equal (new Rect (0, 3, width, 1), lblJust.Frame);
- Assert.Equal (new Rect (0, 0, width + 2, 6), frame.Frame);
- var expected = @"
- ┌────────────────────┐
- │Hello World │
- │ Hello World │
- │ Hello World│
- │Hello World│
- └────────────────────┘
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, width + 2, 6), pos);
- }
- [Fact, AutoInitShutdown]
- public void Label_Draw_Vertical_Simple_TextAlignments ()
- {
- var text = "Hello World";
- var height = 20;
- var lblLeft = new Label (text, direction: TextDirection.TopBottom_LeftRight) { Height = height };
- var lblCenter = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 2, Height = height, VerticalTextAlignment = VerticalTextAlignment.Middle };
- var lblRight = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 4, Height = height, VerticalTextAlignment = VerticalTextAlignment.Bottom };
- var lblJust = new Label (text, direction: TextDirection.TopBottom_LeftRight) { X = 6, Height = height, VerticalTextAlignment = VerticalTextAlignment.Justified };
- var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
- frame.Add (lblLeft, lblCenter, lblRight, lblJust);
- Application.Top.Add (frame);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (9, height + 2);
- Assert.True (lblLeft.AutoSize);
- Assert.True (lblCenter.AutoSize);
- Assert.True (lblRight.AutoSize);
- Assert.True (lblJust.AutoSize);
- Assert.Equal (new Rect (0, 0, 1, height), lblLeft.Frame);
- Assert.Equal (new Rect (2, 0, 1, height), lblCenter.Frame);
- Assert.Equal (new Rect (4, 0, 1, height), lblRight.Frame);
- Assert.Equal (new Rect (6, 0, 1, height), lblJust.Frame);
- Assert.Equal (new Rect (0, 0, 9, height + 2), frame.Frame);
- var 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│
- └───────┘
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 9, height + 2), pos);
- }
- [Fact, AutoInitShutdown]
- public void Label_Draw_Horizontal_Wide_TextAlignments ()
- {
- var text = "こんにちは 世界";
- var width = 25;
- var lblLeft = new Label (text) { Width = width };
- var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered };
- var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right };
- var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified };
- var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
- frame.Add (lblLeft, lblCenter, lblRight, lblJust);
- Application.Top.Add (frame);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (width + 2, 6);
- Assert.True (lblLeft.AutoSize);
- Assert.True (lblCenter.AutoSize);
- Assert.True (lblRight.AutoSize);
- Assert.True (lblJust.AutoSize);
- Assert.Equal (new Rect (0, 0, width, 1), lblLeft.Frame);
- Assert.Equal (new Rect (0, 1, width, 1), lblCenter.Frame);
- Assert.Equal (new Rect (0, 2, width, 1), lblRight.Frame);
- Assert.Equal (new Rect (0, 3, width, 1), lblJust.Frame);
- Assert.Equal (new Rect (0, 0, width + 2, 6), frame.Frame);
- var expected = @"
- ┌─────────────────────────┐
- │こんにちは 世界 │
- │ こんにちは 世界 │
- │ こんにちは 世界│
- │こんにちは 世界│
- └─────────────────────────┘
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, width + 2, 6), pos);
- }
- [Fact, AutoInitShutdown]
- public void Label_Draw_Vertical_Wide_TextAlignments ()
- {
- var text = "こんにちは 世界";
- var height = 23;
- var lblLeft = new Label (text) { Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight };
- var lblCenter = new Label (text) { X = 3, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Middle };
- var lblRight = new Label (text) { X = 6, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Bottom };
- var lblJust = new Label (text) { X = 9, Width = 2, Height = height, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Justified };
- var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
- frame.Add (lblLeft, lblCenter, lblRight, lblJust);
- Application.Top.Add (frame);
- Application.Begin (Application.Top);
- ((FakeDriver)Application.Driver).SetBufferSize (13, height + 2);
- // All AutoSize are false because the Frame.Height != TextFormatter.Size.Height
- Assert.True (lblLeft.AutoSize);
- Assert.True (lblCenter.AutoSize);
- Assert.True (lblRight.AutoSize);
- Assert.True (lblJust.AutoSize);
- Assert.Equal (new Rect (0, 0, 2, height), lblLeft.Frame);
- Assert.Equal (new Rect (3, 0, 2, height), lblCenter.Frame);
- Assert.Equal (new Rect (6, 0, 2, height), lblRight.Frame);
- Assert.Equal (new Rect (9, 0, 2, height), lblJust.Frame);
- Assert.Equal (new Rect (0, 0, 13, height + 2), frame.Frame);
- var expected = @"
- ┌───────────┐
- │こ こ│
- │ん ん│
- │に に│
- │ち ち│
- │は は│
- │ │
- │世 │
- │界 こ │
- │ ん │
- │ に │
- │ ち │
- │ は │
- │ │
- │ 世 │
- │ 界 │
- │ こ │
- │ ん │
- │ に │
- │ ち │
- │ は │
- │ │
- │ 世 世│
- │ 界 界│
- └───────────┘
- ";
- var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
- Assert.Equal (new Rect (0, 0, 13, height + 2), pos);
- }
- [Fact, AutoInitShutdown]
- public void Label_Draw_Fill_Remaining ()
- {
- var view = new View ("This view needs to be cleared before rewritten.");
- var tf1 = new TextFormatter ();
- tf1.Text = "This TextFormatter (tf1) without fill will not be cleared on rewritten.";
- var tf1Size = tf1.Size;
- var tf2 = new TextFormatter ();
- tf2.Text = "This TextFormatter (tf2) with fill will be cleared on rewritten.";
- var tf2Size = tf2.Size;
- Application.Top.Add (view);
- Application.Begin (Application.Top);
- tf1.Draw (new Rect (new Point (0, 1), tf1Size), view.GetNormalColor (), view.ColorScheme.HotNormal, default, false);
- tf2.Draw (new Rect (new Point (0, 2), tf2Size), view.GetNormalColor (), view.ColorScheme.HotNormal);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- This view 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);
- view.Text = "This view is rewritten.";
- view.Draw ();
- tf1.Text = "This TextFormatter (tf1) is rewritten.";
- tf1.Draw (new Rect (new Point (0, 1), tf1Size), view.GetNormalColor (), view.ColorScheme.HotNormal, default, false);
- tf2.Text = "This TextFormatter (tf2) is rewritten.";
- tf2.Draw (new Rect (new Point (0, 2), tf2Size), view.GetNormalColor (), view.ColorScheme.HotNormal);
- TestHelpers.AssertDriverContentsWithFrameAre (@"
- This view is rewritten.
- This TextFormatter (tf1) is rewritten.will not be cleared on rewritten.
- This TextFormatter (tf2) is rewritten.
- ", output);
- }
- }
- }
|