Files
Terminal.Gui/Tests/UnitTests/Views/LabelTests.cs
Copilot 2be8397203 Remove legacy drivers, simplify architecture, and reorganize codebase structure (#4270)
* Initial plan

* Remove legacy drivers and reorganize v2 architecture

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Extract Windows key helper utilities and fix build

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fix all test references to legacy drivers

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Update documentation to reflect new driver architecture

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Remove V2.cd diagram file

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fix test failures: support legacy drivers and update exception handling

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* updated driver names

* Move V2 tests from ConsoleDrivers/V2 to proper locations

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Rename ApplicationV2 to ModernApplicationImpl to remove v2 terminology

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Remove V2 terminology from test drivers and FakeDriver classes

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Merge ModernApplicationImpl into ApplicationImpl and move to App folder

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Create modern FakeDriver with component factory architecture in Terminal.Gui project

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Refactor: Move non-platform-dependent code from /Drivers to /App

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Code cleanup and org

* Unit test reorg

* Refactor MainLoop architecture: rename classes and enhance documentation for clarity

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Add comprehensive FakeDriver tests (WIP - some tests need fixes)

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fixed FakeDriver build failures

* Fix all FakeDriver test failures - Application.Top creation and clipboard behaviors

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fixed FakeDriver build failures2

* Remove hanging legacy FakeDriver tests that use Console.MockKeyPresses

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fixed some tests

* Fixed more tests

* Fixed more tests

* Fix bad copilot (#4277)

* Update Terminal.Gui/Drivers/FakeDriver/FakeConsoleOutput.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Refactor Application Init and Update Tests

Refactored `Application.Init` to improve initialization logic:
- Added fallback to `ForceDriver` when `driverName` is null.
- Changed repeated `Init` calls to throw `InvalidOperationException`.
- Updated `_driverName` assignment logic for robustness.

Enhanced `IConsoleDriver` with detailed remarks on implementations.

Revised test cases to align with updated `Application.Init` behavior:
- Replaced `FakeDriver` with `null` and `driverName: "fake"`.
- Skipped or commented out tests incompatible with new logic.
- Improved formatting and removed redundant setup code.

Improved code style and consistency across the codebase:
- Standardized parameter formatting and spacing.
- Removed outdated comments and unused code.

General cleanup to enhance readability and maintainability.

* Warp fix copilot (#4278)

* More fixes (#4279)

* Fixes/works around test failures and temporarily disable failing test

Updated `FakeDriver` to set `RunningUnitTests` to `true` and initialize dimensions using `FakeConsole`. Modified `TestRespondersDisposedAttribute` to set `ConsoleDriver.RunningUnitTests` in the `Before` method, ensuring proper behavior during unit tests.

Temporarily disabled the `Button_CanFocus_False_Raises_Accepted_Correctly` test in `ViewCommandTests` by adding a `Skip` parameter to the `[Fact]` attribute, referencing issue #4270.

* Allow all tests to run despite failures in UnitTests

Modified the `dotnet test` command in the `Run UnitTestsParallelizable` step to set `xunit.stopOnFail` to `false`. This ensures that the test runner does not stop execution on the first failure, allowing all tests to execute regardless of individual test outcomes.

* Refactor ApplicationScreenTests for cleaner setup/teardown

Refactored `ClearContents_Called_When_Top_Frame_Changes` test:
- Added `[AutoInitShutdown]` attribute for automatic lifecycle management.
- Replaced manual `Application.Init` and `Application.Top` setup with `Application.Begin` and `RunState`.
- Simplified event handling by defining `ClearedContents` handler inline.
- Removed explicit cleanup logic, relying on `Application.End` for teardown.

Updated `using` directives to include `UnitTests` namespace.

* Attempt to fix intermittent local test failures.

Update ApplicationImpl initialization parameter

Changed the second parameter of the `impl.Init` method in the
`FakeApplicationFactory` class from `"dotnet"` to `"fake"`.

* Code cleanup to cause Action to re-run.

* Stop tests on first failure in UnitTestsParallelizable

Updated the `dotnet test` command in `unit-tests.yml` to set the `xunit.stopOnFail` parameter to `true`. This change ensures that test execution halts immediately upon encountering a failure, allowing quicker identification and resolution of issues. Note that this may prevent the full test suite from running in the event of a failure.

* Allow all tests to run despite failures in CI

Updated `unit-tests.yml` to set `xunit.stopOnFail` to `false`
in both `Run UnitTests` and `Run UnitTestsParallelizable`
steps. This ensures that the test runner does not stop
execution on the first test failure, allowing all tests
to complete even if some fail.

* Enhance RuneExtensions docs and update user dictionary

Updated the `<remarks>` section in `RuneExtensions.GetColumns` to include details about the `wcwidth` implementation and improved readability with `<para>` tags. Added `wcwidth` to the user dictionary in `Terminal.sln.DotSettings` to avoid spelling errors.

* Improve XML doc formatting in RuneExtensions.cs

Updated the remarks section of the `GetColumns` method in the
`RuneExtensions` class to enhance readability by reformatting
and properly indenting `<para>` tags. The content remains
unchanged, describing the method's implementation via `wcwidth`
and its role as a Terminal.Gui extension for `System.Text.Rune`.

* Refactor drivers and improve clipboard handling

Replaced legacy drivers (`CursesDriver`, `NetDriver`) with
`UnixDriver` and `DotNetDriver` across the codebase, including
comments, method names, and test cases. Updated documentation
and remarks to reflect the new driver names and platforms.

Revamped clipboard handling with new platform-specific
implementations: `UnixClipboard` for Unix, `MacOSXClipboard`
for macOS, and `WSLClipboard` for Linux under WSL. Removed
the old `CursesClipboard` and consolidated clipboard logic.

Updated test cases to align with the new drivers and clipboard
implementations. Improved naming consistency and cleaned up
redundant code. Updated the README and documentation to
reflect these changes.

* Remove `PlatformColor` from `Attribute` struct

This commit removes the `PlatformColor` property from the `Attribute` struct, simplifying the codebase by eliminating platform-specific color handling. The following changes were made:

- Removed `PlatformColor` from the `Attribute` struct, including its initialization, usage, and related comments.
- Updated constructors to no longer initialize or use `PlatformColor`.
- Modified `Equals` and `GetHashCode` methods to exclude `PlatformColor`.
- Updated `UnixComponentFactory` documentation to remove references to "v2unix."
- Renamed `v2TestDriver` to `testDriver` in the `With` class for clarity.
- Removed `PlatformColor` references in `DriverAssert` and related error messages.
- Deleted test cases in `AttributeTests` that relied on `PlatformColor`.
- Cleaned up comments and TODOs related to `PlatformColor` and `UnixDriver`.

These changes reflect a shift away from platform-dependent color management, improving code clarity and reducing complexity.

Remove `PlatformColor` and simplify `Attribute` logic

The `PlatformColor` property has been removed from the `Attribute` struct, along with its associated logic, simplifying the codebase and eliminating platform-specific dependencies. Constructors, equality checks, and hash code generation in `Attribute` have been updated accordingly.

The `CurrentAttribute` property in `ConsoleDriver` and `OutputBuffer` has been simplified, removing dependencies on `Application.Driver`. The `MakeColor` method logic has been removed or simplified in related classes.

Tests in `AttributeTests` have been refactored to reflect these changes, focusing on `Foreground`, `Background`, and `Style`. Unix-specific logic tied to `PlatformColor` has been eliminated.

Additional updates include renaming parameters in the `With` class for clarity, simplifying `DriverAssert` output, and performing minor code cleanups to improve readability and maintainability.

* Refactor Terminal.Gui driver architecture for v2

Updated documentation to reflect the new modular driver architecture in Terminal.Gui v2.

- Revised `namespace-drivers.md` to include new components (`IConsoleInput`, `IConsoleOutput`, `IInputProcessor`, `IOutputBuffer`, `IWindowSizeMonitor`) and terminal size monitoring.
- Replaced "Key Components" with "Architecture Overview" and added details on the **Component Factory** pattern.
- Documented the four driver implementations (`DotNetDriver`, `WindowsDriver`, `UnixDriver`, `FakeDriver`) and their platform-specific optimizations.
- Added a "Threading Model" section to explain the multi-threaded design for responsive input handling.
- Updated examples to demonstrate driver capabilities and explicit driver selection.

In `drivers.md`:
- Expanded the "Overview" to emphasize the modular, component-based architecture.
- Reorganized "Drivers" into "Available Drivers" and added details on `FakeDriver` for unit testing.
- Added sections on "Initialization Flow," "Shutdown Flow," and platform-specific driver details.
- Provided examples for accessing driver components and creating custom drivers.

In `index.md`:
- Updated "Cross Platform" feature to reflect new driver names and clarified compatibility with SSH and monochrome terminals.

* Moved files around

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: tig <585482+tig@users.noreply.github.com>
Co-authored-by: Tig <tig@users.noreply.github.com>
Co-authored-by: Thomas Nind <31306100+tznind@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-15 13:24:47 -06:00

1482 lines
47 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnitTests;
using Xunit.Abstractions;
namespace Terminal.Gui.ViewsTests;
public class LabelTests (ITestOutputHelper output)
{
// Test that Title and Text are the same
[Fact]
public void Text_Mirrors_Title ()
{
var label = new Label ();
label.Title = "Hello";
Assert.Equal ("Hello", label.Title);
Assert.Equal ("Hello", label.TitleTextFormatter.Text);
Assert.Equal ("Hello", label.Text);
Assert.Equal ("Hello", label.TextFormatter.Text);
}
[Fact]
public void Title_Mirrors_Text ()
{
var label = new Label ();
label.Text = "Hello";
Assert.Equal ("Hello", label.Text);
Assert.Equal ("Hello", label.TextFormatter.Text);
Assert.Equal ("Hello", label.Title);
Assert.Equal ("Hello", label.TitleTextFormatter.Text);
}
[Theory]
[CombinatorialData]
public void HotKey_Command_SetsFocus_OnNextSubView (bool hasHotKey)
{
var superView = new View { CanFocus = true };
var label = new Label ();
label.HotKey = hasHotKey ? Key.A.WithAlt : Key.Empty;
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.Equal (hasHotKey, nextSubView.HasFocus);
}
[Theory]
[CombinatorialData]
public void MouseClick_SetsFocus_OnNextSubView (bool hasHotKey)
{
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 };
label.HotKey = hasHotKey ? Key.X.WithAlt : Key.Empty;
var nextSubView = new View { CanFocus = true, X = 4, Width = 4, Height = 1 };
superView.Add (focusedView, label, nextSubView);
superView.BeginInit ();
superView.EndInit ();
Assert.False (focusedView.HasFocus);
Assert.False (label.HasFocus);
Assert.False (nextSubView.HasFocus);
label.NewMouseEvent (new () { Position = new (0, 0), Flags = MouseFlags.Button1Clicked });
Assert.False (label.HasFocus);
Assert.Equal (hasHotKey, nextSubView.HasFocus);
}
[Fact]
public void HotKey_Command_Does_Not_Accept ()
{
var label = new Label ();
var accepted = false;
label.Accepting += LabelOnAccept;
label.InvokeCommand (Command.HotKey);
Assert.False (accepted);
return;
void LabelOnAccept (object sender, CommandEventArgs e) { accepted = true; }
}
[Fact]
[AutoInitShutdown]
public void Text_Set_With_AnchorEnd_Works ()
{
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);
AutoInitShutdownAttribute.FakeResize (new Size (30, 5));
AutoInitShutdownAttribute.RunIteration ();
var expected = @"
┌────────────────────────────┐
│ │
│ Say Hello 你│
│ │
└────────────────────────────┘
";
DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
label.Text = "Say Hello 你 changed";
AutoInitShutdownAttribute.RunIteration ();
expected = @"
┌────────────────────────────┐
│ │
│ Say Hello 你 changed│
│ │
└────────────────────────────┘
";
DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Set_Text_With_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);
AutoInitShutdownAttribute.FakeResize (new Size (30, 5));
var expected = @"
┌────────────────────────────┐
│ │
│ Say Hello 你 │
│ │
└────────────────────────────┘
";
DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
label.Text = "Say Hello 你 changed";
AutoInitShutdownAttribute.RunIteration ();
expected = @"
┌────────────────────────────┐
│ │
│ Say Hello 你 changed │
│ │
└────────────────────────────┘
";
DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
top.Dispose ();
}
[Fact]
public void Constructors_Defaults ()
{
var label = new Label ();
Assert.Equal (string.Empty, label.Text);
Assert.Equal (Alignment.Start, label.TextAlignment);
Assert.False (label.CanFocus);
Assert.Equal (new (0, 0, 0, 0), label.Frame);
Assert.Equal (KeyCode.Null, label.HotKey);
}
[Fact]
[AutoInitShutdown]
public void Label_Draw_Fill_Remaining ()
{
var tfSize = new Size (80, 1);
var label = new Label { Text = "This label needs to be cleared before rewritten.", Width = tfSize.Width, Height = tfSize.Height };
var tf1 = new TextFormatter { Direction = TextDirection.LeftRight_TopBottom, ConstrainToSize = tfSize };
tf1.Text = "This TextFormatter (tf1) without fill will not be cleared on rewritten.";
var tf2 = new TextFormatter { Direction = TextDirection.LeftRight_TopBottom, ConstrainToSize = tfSize, FillRemaining = true };
tf2.Text = "This TextFormatter (tf2) with fill will be cleared on rewritten.";
var top = new Toplevel ();
top.Add (label);
RunState runState = Application.Begin (top);
Application.RunIteration (ref runState);
Assert.False (label.TextFormatter.FillRemaining);
Assert.False (tf1.FillRemaining);
Assert.True (tf2.FillRemaining);
AutoInitShutdownAttribute.RunIteration ();
tf1.Draw (new (new (0, 1), tfSize), label.GetAttributeForRole (VisualRole.Normal), label.GetAttributeForRole (VisualRole.HotNormal));
tf2.Draw (new (new (0, 2), tfSize), label.GetAttributeForRole (VisualRole.Normal), label.GetAttributeForRole (VisualRole.HotNormal));
DriverAssert.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.NeedsDraw);
Assert.False (label.NeedsLayout);
Assert.False (label.SubViewNeedsDraw);
label.Text = "This label is rewritten.";
Assert.True (label.NeedsDraw);
Assert.True (label.NeedsLayout);
//Assert.False (label.SubViewNeedsDraw);
label.Draw ();
tf1.Text = "This TextFormatter (tf1) is rewritten.";
tf1.Draw (new (new (0, 1), tfSize), label.GetAttributeForRole (VisualRole.Normal), label.GetAttributeForRole (VisualRole.HotNormal));
tf2.Text = "This TextFormatter (tf2) is rewritten.";
tf2.Draw (new (new (0, 2), tfSize), label.GetAttributeForRole (VisualRole.Normal), label.GetAttributeForRole (VisualRole.HotNormal));
DriverAssert.AssertDriverContentsWithFrameAre (
@"
This label is rewritten.
This TextFormatter (tf1) is rewritten.will not be cleared on rewritten.
This TextFormatter (tf2) is rewritten. ",
output
);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Label_Draw_Horizontal_Simple_Runes ()
{
var label = new Label { Text = "Demo Simple Rune" };
var top = new Toplevel ();
top.Add (label);
Application.Begin (top);
AutoInitShutdownAttribute.RunIteration ();
Assert.Equal (new (0, 0, 16, 1), label.Frame);
var expected = @"
Demo Simple Rune
";
Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 16, 1), pos);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Label_Draw_Vertical_Simple_Runes ()
{
var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "Demo Simple Rune" };
var top = new Toplevel ();
top.Add (label);
Application.Begin (top);
AutoInitShutdownAttribute.RunIteration ();
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 = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 1, 16), pos);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Label_Draw_Vertical_Wide_Runes ()
{
var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "デモエムポンズ" };
var top = new Toplevel ();
top.Add (label);
Application.Begin (top);
AutoInitShutdownAttribute.RunIteration ();
var expected = @"
";
Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 2, 7), pos);
top.Dispose ();
}
[Fact]
public void Label_HotKeyChanged_EventFires ()
{
var label = new Label { Text = "Yar" };
label.HotKey = 'Y';
object sender = null;
KeyChangedEventArgs args = null;
label.HotKeyChanged += (s, e) =>
{
sender = s;
args = e;
};
label.HotKey = Key.R;
Assert.Same (label, sender);
Assert.Equal (KeyCode.Y | KeyCode.ShiftMask, args.OldKey);
Assert.Equal (Key.R, args.NewKey);
}
[Fact]
public void Label_HotKeyChanged_EventFires_WithNone ()
{
var label = new Label ();
object sender = null;
KeyChangedEventArgs args = null;
label.HotKeyChanged += (s, e) =>
{
sender = s;
args = e;
};
label.HotKey = KeyCode.R;
Assert.Same (label, sender);
Assert.Equal (KeyCode.Null, args.OldKey);
Assert.Equal (KeyCode.R, args.NewKey);
}
[Fact]
public void TestAssignTextToLabel ()
{
View b = new Label { Text = "heya" };
Assert.Equal ("heya", b.Text);
Assert.Contains ("heya", b.TextFormatter.Text);
b.Text = "heyb";
Assert.Equal ("heyb", b.Text);
Assert.Contains ("heyb", b.TextFormatter.Text);
// with cast
Assert.Equal ("heyb", ((Label)b).Text);
}
[Fact]
[AutoInitShutdown]
public void Update_Only_On_Or_After_Initialize ()
{
var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
win.Add (label);
var top = new Toplevel ();
top.Add (win);
Assert.False (label.IsInitialized);
Application.Begin (top);
AutoInitShutdownAttribute.FakeResize (new Size (30, 5));
Assert.True (label.IsInitialized);
Assert.Equal ("Say Hello 你", label.Text);
Assert.Equal ("Say Hello 你", label.TextFormatter.Text);
Assert.Equal (new (0, 0, 12, 1), label.Viewport);
var expected = @"
┌────────────────────────────┐
│ │
│ Say Hello 你 │
│ │
└────────────────────────────┘
";
Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 30, 5), pos);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Update_Parameterless_Only_On_Or_After_Initialize ()
{
var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" };
var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
win.Add (label);
var top = new Toplevel ();
top.Add (win);
Assert.False (label.IsInitialized);
Application.Begin (top);
AutoInitShutdownAttribute.FakeResize (new Size (30, 5));
Assert.True (label.IsInitialized);
Assert.Equal ("Say Hello 你", label.Text);
Assert.Equal ("Say Hello 你", label.TextFormatter.Text);
Assert.Equal (new (0, 0, 12, 1), label.Viewport);
var expected = @"
┌────────────────────────────┐
│ │
│ Say Hello 你 │
│ │
└────────────────────────────┘
";
Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 30, 5), pos);
top.Dispose ();
}
[Fact]
[SetupFakeDriver]
public void Full_Border ()
{
var label = new Label { BorderStyle = LineStyle.Single, Text = "Test" };
label.BeginInit ();
label.EndInit ();
label.SetRelativeLayout (Application.Screen.Size);
Assert.Equal (new (0, 0, 4, 1), label.Viewport);
Assert.Equal (new (0, 0, 6, 3), label.Frame);
label.Draw ();
DriverAssert.AssertDriverContentsWithFrameAre (
@"
┌┤Te├┐
│Test│
└────┘",
output
);
label.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void With_Top_Margin_Without_Top_Border ()
{
var label = new Label { Text = "Test", /*Width = 6, Height = 3,*/ BorderStyle = LineStyle.Single };
label.Margin.Thickness = new (0, 1, 0, 0);
label.Border.Thickness = new (1, 0, 1, 1);
var top = new Toplevel ();
top.Add (label);
Application.Begin (top);
AutoInitShutdownAttribute.RunIteration ();
Assert.Equal (new (0, 0, 6, 3), label.Frame);
Assert.Equal (new (0, 0, 4, 1), label.Viewport);
Application.Begin (top);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
│Test│
└────┘",
output
);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Without_Top_Border ()
{
var label = new Label { Text = "Test", /* Width = 6, Height = 3, */BorderStyle = LineStyle.Single };
label.Border.Thickness = new (1, 0, 1, 1);
var top = new Toplevel ();
top.Add (label);
Application.Begin (top);
AutoInitShutdownAttribute.RunIteration ();
Assert.Equal (new (0, 0, 6, 2), label.Frame);
Assert.Equal (new (0, 0, 4, 1), label.Viewport);
Application.Begin (top);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
│Test│
└────┘",
output
);
top.Dispose ();
}
// These tests were formally in AutoSizetrue.cs. They are (poor) Label tests.
private readonly string [] _expecteds = new string [21]
{
@"
┌────────────────────┐
│View with long text │
│ │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 0 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 1 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 2 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 3 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 4 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 5 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 6 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 7 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 8 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 9 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 10 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 11 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 12 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 13 │
│Label 13 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 13 │
│Label 14 │
│Label 14 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 13 │
│Label 14 │
│Label 15 │
│Label 15 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 13 │
│Label 14 │
│Label 15 │
│Label 16 │
│Label 16 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 13 │
│Label 14 │
│Label 15 │
│Label 16 │
│Label 17 │
│Label 17 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 13 │
│Label 14 │
│Label 15 │
│Label 16 │
│Label 17 │
│Label 18 │
│Label 18 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 13 │
│Label 14 │
│Label 15 │
│Label 16 │
│Label 17 │
│Label 18 │
│Label 19 │
│Label 19 │
└────────────────────┘"
};
// 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.",
//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);
AutoInitShutdownAttribute.FakeResize (new Size (40, 10));
Assert.Equal (29, label.Text.Length);
Assert.Equal (new (0, 0, 40, 10), top.Frame);
Assert.Equal (new (0, 0, 40, 10), win.Frame);
Assert.Equal (new (0, 7, 29, 1), label.Frame);
var expected = @"
┌──────────────────────────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│This should be the last line. │
└──────────────────────────────────────┘
"
;
DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Application.End (rs);
top.Dispose ();
}
// TODO: This is a Label test. Move to label tests if there's not already a test for this.
[Fact]
[AutoInitShutdown]
public void Bottom_Equal_Inside_Window ()
{
var win = new Window ();
var label = new Label
{
Text = "This should be the last line.",
//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);
AutoInitShutdownAttribute.FakeResize (new Size (40, 10));
Assert.Equal (new (0, 0, 40, 10), top.Frame);
Assert.Equal (new (0, 0, 40, 10), win.Frame);
Assert.Equal (new (0, 7, 29, 1), label.Frame);
var expected = @"
┌──────────────────────────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│This should be the last line. │
└──────────────────────────────────────┘
";
DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Application.End (rs);
top.Dispose ();
}
// 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;
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)
{
AutoInitShutdownAttribute.FakeResize (new Size (22, count + 4));
Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (_expecteds [count], output);
Assert.Equal (new (0, 0, 22, count + 4), pos);
if (count > 0)
{
Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
view.Remove (listLabels [count - 1]);
listLabels [count - 1].Dispose ();
listLabels.RemoveAt (count - 1);
Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
view.Height -= 1;
count--;
if (listLabels.Count > 0)
{
field.Text = listLabels [count - 1].Text;
}
else
{
field.Text = string.Empty;
}
}
Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
}
};
Application.Iteration += (s, a) =>
{
while (count > -1)
{
field.NewKeyDownEvent (Key.Enter);
if (count == 0)
{
field.NewKeyDownEvent (Key.Enter);
break;
}
}
Application.RequestStop ();
};
var win = new Window ();
win.Add (view);
win.Add (field);
top.Add (win);
Application.Run (top);
Assert.Equal (0, count);
Assert.Equal (count, listLabels.Count);
top.Dispose ();
}
// TODO: This is a Label test. Move to Label tests.
[Fact]
[SetupFakeDriver]
public void Label_Height_Zero_Stays_Zero ()
{
((IFakeConsoleDriver)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 View { CanFocus = true, BorderStyle = LineStyle.Single, 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 = DriverAssert.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.ClearViewport ();
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 = DriverAssert.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;
List<Label> listLabels = new ();
field.KeyDown += (s, k) =>
{
if (k.KeyCode == KeyCode.Enter)
{
AutoInitShutdownAttribute.FakeResize (new Size (22, count + 4));
Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (_expecteds [count], output);
Assert.Equal (new (0, 0, 22, count + 4), pos);
if (count < 20)
{
field.Text = $"Label {count}";
var label = new Label { Text = field.Text, X = 0, Y = view.Viewport.Height /*, Width = 10*/ };
view.Add (label);
Assert.Equal ($"Label {count}", label.Text);
Assert.Equal ($"Absolute({count + 1})", label.Y.ToString ());
listLabels.Add (label);
//if (count == 0) {
// Assert.Equal ($"Absolute({count})", view.Height.ToString ());
// view.Height += 2;
//} else {
Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
view.Height += 1;
//}
count++;
}
Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
}
};
Application.Iteration += (s, a) =>
{
while (count < 21)
{
field.NewKeyDownEvent (Key.Enter);
if (count == 20)
{
field.NewKeyDownEvent (Key.Enter);
break;
}
}
Application.RequestStop ();
};
var win = new Window ();
win.Add (view);
win.Add (field);
top.Add (win);
Application.Run (top);
Assert.Equal (20, count);
Assert.Equal (count, listLabels.Count);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Label_IsEmpty_False_Minimum_Height ()
{
var text = "Label";
var label = new Label
{
//Width = Dim.Fill () - text.Length,
Text = text
};
var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
win.Add (label);
var top = new Toplevel ();
top.Add (win);
Application.Begin (top);
AutoInitShutdownAttribute.FakeResize (new Size (10, 4));
Assert.Equal (5, text.Length);
Assert.Equal (new (0, 0, 5, 1), label.Frame);
Assert.Equal (new (5, 1), label.TextFormatter.ConstrainToSize);
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 = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 10, 4), pos);
text = "0123456789";
Assert.Equal (10, text.Length);
//label.Width = Dim.Fill () - text.Length;
AutoInitShutdownAttribute.RunIteration ();
Assert.Equal (new (0, 0, 5, 1), label.Frame);
Assert.Equal (new (5, 1), label.TextFormatter.ConstrainToSize);
Exception exception = Record.Exception (() => Assert.Single (label.TextFormatter.GetLines ()));
Assert.Null (exception);
expected = @"
┌────────┐
│Label │
│ │
└────────┘
";
pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 10, 4), pos);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Label_IsEmpty_False_Never_Return_Null_Lines ()
{
var text = "Label";
var label = new Label
{
//Width = Dim.Fill () - text.Length,
//Height = 1,
Text = text
};
var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
win.Add (label);
var top = new Toplevel ();
top.Add (win);
Application.Begin (top);
AutoInitShutdownAttribute.FakeResize (new Size (10, 4));
Assert.Equal (5, text.Length);
Assert.Equal (new (0, 0, 5, 1), label.Frame);
Assert.Equal (new (5, 1), label.TextFormatter.ConstrainToSize);
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 = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 10, 4), pos);
text = "0123456789";
Assert.Equal (10, text.Length);
//label.Width = Dim.Fill () - text.Length;
AutoInitShutdownAttribute.RunIteration ();
Assert.Equal (new (0, 0, 5, 1), label.Frame);
Assert.Equal (new (5, 1), label.TextFormatter.ConstrainToSize);
Assert.Single (label.TextFormatter.GetLines ());
expected = @"
┌────────┐
│Label │
│ │
└────────┘
";
pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
Assert.Equal (new (0, 0, 10, 4), pos);
top.Dispose ();
}
[Fact]
public void Label_ResizeView_With_Dim_Absolute ()
{
var super = new View
{
Width = Dim.Fill (),
Height = Dim.Fill ()
};
var label = new Label ();
label.Text = "New text";
super.Add (label);
super.LayoutSubViews ();
Rectangle expectedLabelBounds = new (0, 0, 8, 1);
Assert.Equal (expectedLabelBounds, label.Viewport);
super.Dispose ();
}
[Fact]
public void CanFocus_False_HotKey_SetsFocus_Next ()
{
View otherView = new ()
{
Text = "otherView",
CanFocus = true
};
Label label = new ()
{
Text = "_label"
};
View nextView = new ()
{
Text = "nextView",
CanFocus = true
};
Application.Navigation = new ();
Application.Top = new ();
Application.Top.Add (otherView, label, nextView);
Application.Top.SetFocus ();
Assert.True (otherView.HasFocus);
Assert.True (Application.RaiseKeyDownEvent (label.HotKey));
Assert.False (otherView.HasFocus);
Assert.False (label.HasFocus);
Assert.True (nextView.HasFocus);
Application.Top.Dispose ();
Application.ResetState ();
}
[Fact]
public void CanFocus_False_MouseClick_SetsFocus_Next ()
{
View otherView = new () { X = 0, Y = 0, Width = 1, Height = 1, Id = "otherView", CanFocus = true };
Label label = new () { X = 0, Y = 1, Text = "_label" };
View nextView = new () { X = Pos.Right (label), Y = Pos.Top (label), Width = 1, Height = 1, Id = "nextView", CanFocus = true };
Application.Navigation = new ();
Application.Top = new ();
Application.Top.Add (otherView, label, nextView);
Application.Top.Layout ();
Application.Top.SetFocus ();
// click on label
Application.RaiseMouseEvent (new () { ScreenPosition = label.Frame.Location, Flags = MouseFlags.Button1Clicked });
Assert.False (label.HasFocus);
Assert.True (nextView.HasFocus);
Application.Top.Dispose ();
Application.ResetState ();
}
[Fact]
public void CanFocus_True_HotKey_SetsFocus ()
{
Label label = new ()
{
Text = "_label",
CanFocus = true
};
View view = new ()
{
Text = "view",
CanFocus = true
};
Application.Navigation = new ();
Application.Top = new ();
Application.Top.Add (label, view);
view.SetFocus ();
Assert.True (label.CanFocus);
Assert.False (label.HasFocus);
Assert.True (view.CanFocus);
Assert.True (view.HasFocus);
// No focused view accepts Tab, and there's no other view to focus, so OnKeyDown returns false
Assert.True (Application.RaiseKeyDownEvent (label.HotKey));
Assert.True (label.HasFocus);
Assert.False (view.HasFocus);
Application.Top.Dispose ();
Application.ResetState ();
}
[Fact]
public void CanFocus_True_MouseClick_Focuses ()
{
Application.Navigation = new ();
Label label = new ()
{
Text = "label",
X = 0,
Y = 0,
CanFocus = true
};
View otherView = new ()
{
Text = "view",
X = 0,
Y = 1,
Width = 4,
Height = 1,
CanFocus = true
};
Application.Top = new ()
{
Width = 10,
Height = 10
};
Application.Top.Add (label, otherView);
Application.Top.SetFocus ();
Application.Top.Layout ();
Assert.True (label.CanFocus);
Assert.True (label.HasFocus);
Assert.True (otherView.CanFocus);
Assert.False (otherView.HasFocus);
otherView.SetFocus ();
Assert.True (otherView.HasFocus);
// label can focus, so clicking on it set focus
Application.RaiseMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Clicked });
Assert.True (label.HasFocus);
Assert.False (otherView.HasFocus);
// click on view
Application.RaiseMouseEvent (new () { ScreenPosition = new (0, 1), Flags = MouseFlags.Button1Clicked });
Assert.False (label.HasFocus);
Assert.True (otherView.HasFocus);
Application.Top.Dispose ();
Application.ResetState ();
}
// https://github.com/gui-cs/Terminal.Gui/issues/3893
[Fact]
[SetupFakeDriver]
public void TestLabelUnderscoreMinus ()
{
var lbl = new Label
{
Text = "TextView with some more test_- text. Unicode shouldn't 𝔹A𝔽!"
};
lbl.Draw ();
}
}