Files
Terminal.Gui/UnitTests/Views/LabelTests.cs
Tig dcb3b359ad Fixes #2926 - Refactor KeyEvent and KeyEventEventArgs to simplify (#2927)
* Adds basic MainLoop unit tests

* Remove WinChange action from Curses

* Remove WinChange action from Curses

* Remove ProcessInput action from Windows MainLoop

* Simplified MainLoop/ConsoleDriver by making MainLoop internal and moving impt fns to Application

* Modernized Terminal resize events

* Modernized Terminal resize events

* Removed un used property

* for _isWindowsTerminal devenv->wininit; not sure what changed

* Modernized mouse/keyboard events (Action->EventHandler)

* Updated OnMouseEvent API docs

* Using WT_SESSION to detect WT

* removes hacky GetParentProcess

* Updates to fix #2634 (clear last line)

* removes hacky GetParentProcess2

* Addressed mac resize issue

* Addressed mac resize issue

* Removes ConsoleDriver.PrepareToRun, has Init return MainLoop

* Removes unneeded Attribute methods

* Removed GetProcesssName

* Removed GetProcesssName

* Refactored KeyEvent and KeyEventEventArgs into a single class

* Revert "Refactored KeyEvent and KeyEventEventArgs into a single class"

This reverts commit 88a00658db.

* Fixed key repeat issue; reverted stupidity on 1049/1047 confusion

* Updated CSI API Docs

* merge

* Rearranged Event.cs to Keyboard.cs and Mouse.cs

* Renamed KeyEventEventArgs KeyEventArgs

* temp renamed KeyEvent OldKeyEvent

* Merged KeyEvent into KeyEventArgs

* Renamed Application.ProcessKey members

* Renamed Application.ProcessKey members

* Renamed Application.ProcessKey members

* Added Responder.KeyPressed

* Removed unused references

* Fixed arg naming

* InvokeKeybindings->InvokeKeyBindings

* InvokeKeybindings->InvokeKeyBindings

* Fixed unit tests fail

* More progress on refactoring key input; still broken and probably wrong

* Moved OnKeyPressed out of Responder and made ProcessKeyPrssed non-virtual

* Updated API docs

* Moved key handling from Responder to View

* Updated API docs

* Updated HotKey API docs

* Updated shortcut API docs

* Fixed responder unit tests

* Removed Shortcut from View as it is not used

* Removed unneeded OnHotKey override from Button

* Fixed BackTab logic

* Button now uses Key Bindings exclusively

* Button now uses Key Bindings exclusively

* Updated keyboard.md docs

* Fixed unit tests to account for Toplevel handling default button

* Added View.InvokeCommand API

* Modernized RadioGroup

* Removed ColdKey

* Modernized (partially) StatusBar

* Worked around FileDialog issue with Ctrl-F

* Fixed driver unit test; view must be focused to reciev key pressed

* Application code cleanup

* Start on updaing menu

* Menu now mostly works

* Menu Select refinement

* Fixed known menu bugs!

* Enabled HotKey to cause focus- experimental

* Fixes #3022 & adds unit test to prove it

* Actually Fixes #3022 & adds unit test to prove it

* Working through hotkey issues

* Misc fixes

* removed hot/cold key stuff from Keys scenario

* Fixed scenarios

* Simplified shortcut string handling

* Modernized Checkbox

* Modernized TileView

* Updated API docs

* Updated API docs

* attempting to publish v2 docs

* Revert "attempting to publish v2 docs"

This reverts commit 59dcec111b.

* Playing with api docs

* Removed Key.BackTab

* Removed Caps/Scroll/Numlock

* Partial removal of keymodifiers - unit tests pass

* Partial removal of keymodifiers - broke netdriver somewhere

* WindowsDriver & added KeyEventArgsTests

* Fixing menu shortcut/hotkeys - broke Menu.cs into separate files

* Fixed MenuBar!

* Finished modernizing Menu/MenuBar

* Removed Key.a-z. Broke lots of stuff

* checkout@v4

* progress on key mapping and formatting

* VK tests are still failing

* Fixed some unit tests

* Added Hotkey and Keybinding unit tests

* fixed unit test

* All unit tests pass again...

* Fixed broken unit tests

* KeyEventArgs.KeyValue -> AsRune

* Fixed bugs. Still some broken

* Added KeyEventArgs.IsAlpha. Added KeyEventArgs.cast ops. Fixed bugs. Unit tests pass

* Fixed WindowsDriver

* Oops.

* Refactoring based on bdisp's help. Not complete!

* removed calling into subviews from OnKeyBindings

* removed calling into subviews from OnKeyBindings

* Improved View KeyEvent unit tests

* More hotkey unit tests

* BIg change - Got rid of KeyPress w/in Application/Drivers

* Unit tests now pass again

* Refreshed API docs

* Better HotKey logic. More progress. Getting close.

* Fixed handling of shifted chars like ö

* Minor code cleanup

* Minor code cleanup2

* Why is build Action failing?

* Why is build Action failing??

* upgraded to .net8 to try to fix weird CI/CD build errors

* upgraded to .net8 to try to fix weird CI/CD build errors2

* Disabling TextViewTests to diagnose build errors

* reenable TextViewTests to diagnose build errors

* Arrrrrrg

* Merged v2_develop

* Fixed uppercase accented keys in WindowsDriver

* Fixed key binding api docs

* Experimental impl of CommandScope.SubViews for MenuBar

* Removed dead code from application.cs

* Removed dead code from application.cs

* Removed dead code from ConsoleDriver.cs

* Cleaned up some key binding stuff

* Disabled Alt to activate menu for now

* Updated label commands

* Fixed menu bugs. Upgraded menu unit tests

* Fixed unit tests

* Working on NetDriver

* fixed netdriver

* Fixed issues called out by @bdisp CR

* fixed CursesDriver

* added todo to netdriver

* Cherry picked treeview test fix 1b415e5

* Fix NetDriver.

* CommandScope->KeyBindingScope

* Address some tznind feedback

* Refactored KeyBindings big time!

* Added key consts to KeyEventArgs and renamed Key to ConsoleDriverKey

* Fixed some API docs

* Moved ConsoleDriverKey to ConsoleDriver.cs

* Renamed Key->ConsoleDriverKey

* Renamed Key->ConsoleDriverKey

* Renamed Key->ConsoleDriverKey

* renamed file I forgot to rename before

* Updated name and API docs of KeyEventArgs.isAlpha

* Fixed issues with OnKeyUp not doing the right thing.

* Fixed MainLoop.Running never being used

* Fixed MainLoop.Running never being used - unit tests

* Claned up BUGBUG comments

* Disabled a unit test to see why ci/cd tests are failing

* Removed defunct commented code

* Removed more defunct commented code

* Re-eanbled unit test; jsut removing one test case...

* Disabled more...

* Renambed Global->Applicaton and updated scope API docs

* Disabled more unit tests...

* Removed dead code

* Disabled more unit tests...2

* Disabled more unit tests...3

* Renambed Global->Applicaton and updated scope API docs 2

* Added more KeyBinding scope tests

* Added more KeyBinding scope tests2

* ConsoleDriverKey too long. Key too ambiguous. Settled on KeyCode. (Partialy because eventually I want to intro a class named Key).

* KeyEventArgs improvements. cast to Rune must be explicit as it's lossy

* Fixed warnings

* Renamed KeyEventArgs to Key... progress on fixing broken stuff that resulted

* Fix ConsoleKeyMapping bugs.

* Fix NetDriver issue from converting a lower case to a upper case.

* Started migration to Key from KeyCode - e.g. made HotKeys all consistent.

* Fixed build warnings

* Added key defns to Key

* KeyBindings now uses Key vs. KeyCode

* Verified by tweaking UICatalog

* Fixed treeview test ... again

* Renamed ProcessKeyDown/Up to NewKeyDown/Up and OnKeyPressed to OnProcessKeyDown to make things more clear

* Added test AllViews_KeyDown_All_EventsFire unit tests and fixed a few Views that were wrong

* fixed stupid KeyUp event bug

* If key not handled, return false for datefield

* dotnet test --no-restore --verbosity diag

* dotnet test --blame

* run tests on windows

* Fix TestVKPacket unit test and move it to ConsoleKeyMappingTests.cs file.

* Remove unnecessary commented code.

* Tweaked unit tests and removed Key.BareKey

* Fixed little details and updated api docs

* updated api docs

* AddKeyBindingsForHotKey: KeyCode->Key

* Cleaned up more old KeyCode usages. Added TODOs

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2023-12-16 12:04:23 -07:00

955 lines
30 KiB
C#

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 (KeyCode.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.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 ("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 (() => label.TextFormatter.Text.GetColumns ());
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 = KeyCode.R;
Assert.Same (label, sender);
Assert.Equal (KeyCode.Null, args.OldKey);
Assert.Equal (KeyCode.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);
}
[Theory, AutoInitShutdown]
[InlineData (false)]
[InlineData (true)]
public void Label_WordWrap_PreserveTrailingSpaces_Horizontal_With_Wide_Runes (bool autoSize)
{
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 (), AutoSize = autoSize };
var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () };
frame.Add (label);
Application.Top.Add (frame);
Application.Begin (Application.Top);
Assert.True (label.AutoSize == autoSize);
Assert.Equal (new Rect (0, 0, 78, 23), label.Frame);
if (autoSize) {
// The size of the wrappedLines [1]
Assert.Equal (new Size (width, height - 2), label.TextFormatter.Size);
} else {
Assert.Equal (new Size (78, 23), label.TextFormatter.Size);
}
((FakeDriver)Application.Driver).SetBufferSize (width + 2, height + 2);
Assert.Equal (new Rect (0, 0, width, height), label.Frame);
if (autoSize) {
Assert.Equal (new Size (width, height - 2), label.TextFormatter.Size);
} else {
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);
}
[Theory, AutoInitShutdown]
[InlineData (true)]
[InlineData (false)]
public void Label_Draw_Horizontal_Simple_TextAlignments (bool autoSize)
{
var text = "Hello World";
var width = 20;
var lblLeft = new Label (text) { Width = width, AutoSize = autoSize };
var lblCenter = new Label (text) { Y = 1, Width = width, TextAlignment = TextAlignment.Centered, AutoSize = autoSize };
var lblRight = new Label (text) { Y = 2, Width = width, TextAlignment = TextAlignment.Right, AutoSize = autoSize };
var lblJust = new Label (text) { Y = 3, Width = width, TextAlignment = TextAlignment.Justified, AutoSize = autoSize };
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 == autoSize);
Assert.True (lblCenter.AutoSize == autoSize);
Assert.True (lblRight.AutoSize == autoSize);
Assert.True (lblJust.AutoSize == autoSize);
Assert.True (lblLeft.TextFormatter.AutoSize == autoSize);
Assert.True (lblCenter.TextFormatter.AutoSize == autoSize);
Assert.True (lblRight.TextFormatter.AutoSize == autoSize);
Assert.True (lblJust.TextFormatter.AutoSize == 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);
if (autoSize) {
Assert.Equal (new Size (11, 1), lblLeft.TextFormatter.Size);
Assert.Equal (new Size (11, 1), lblCenter.TextFormatter.Size);
Assert.Equal (new Size (11, 1), lblRight.TextFormatter.Size);
} else {
Assert.Equal (new Size (width, 1), lblLeft.TextFormatter.Size);
Assert.Equal (new Size (width, 1), lblCenter.TextFormatter.Size);
Assert.Equal (new Size (width, 1), lblRight.TextFormatter.Size);
}
Assert.Equal (new Size (width, 1), lblJust.TextFormatter.Size);
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);
}
[Theory, AutoInitShutdown]
[InlineData (true)]
[InlineData (false)]
public void Label_Draw_Vertical_Simple_TextAlignments (bool autoSize)
{
var text = "Hello World";
var height = 20;
var lblLeft = new Label (text, direction: TextDirection.TopBottom_LeftRight, autoSize) { Height = height };
var lblCenter = new Label (text, direction: TextDirection.TopBottom_LeftRight, autoSize) { X = 2, Height = height, VerticalTextAlignment = VerticalTextAlignment.Middle };
var lblRight = new Label (text, direction: TextDirection.TopBottom_LeftRight, autoSize) { X = 4, Height = height, VerticalTextAlignment = VerticalTextAlignment.Bottom };
var lblJust = new Label (text, direction: TextDirection.TopBottom_LeftRight, autoSize) { 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 == autoSize);
Assert.True (lblCenter.AutoSize == autoSize);
Assert.True (lblRight.AutoSize == autoSize);
Assert.True (lblJust.AutoSize == autoSize);
Assert.True (lblLeft.TextFormatter.AutoSize == autoSize);
Assert.True (lblCenter.TextFormatter.AutoSize == autoSize);
Assert.True (lblRight.TextFormatter.AutoSize == autoSize);
Assert.True (lblJust.TextFormatter.AutoSize == 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);
if (autoSize) {
Assert.Equal (new Size (1, 11), lblLeft.TextFormatter.Size);
Assert.Equal (new Size (1, 11), lblCenter.TextFormatter.Size);
Assert.Equal (new Size (1, 11), lblRight.TextFormatter.Size);
} else {
Assert.Equal (new Size (1, height), lblLeft.TextFormatter.Size);
Assert.Equal (new Size (1, height), lblCenter.TextFormatter.Size);
Assert.Equal (new Size (1, height), lblRight.TextFormatter.Size);
}
Assert.Equal (new Size (1, height), lblJust.TextFormatter.Size);
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);
}
}
}