mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
* Initial plan * Add ScreenChanged event, SetScreenSize method, and fix FakeDriver buffer initialization Co-authored-by: tig <585482+tig@users.noreply.github.com> * Add comprehensive tests for ScreenChanged event and buffer integrity Co-authored-by: tig <585482+tig@users.noreply.github.com> * Replace obsolete SizeChanged usage with ScreenChanged in core and tests Co-authored-by: tig <585482+tig@users.noreply.github.com> * Refactor terminal size event handling Replaced `ScreenChanged` with `SizeChanged` across the codebase to standardize naming and improve clarity. Updated event handling logic, including subscriptions, unsubscriptions, and raising methods. Removed deprecated `ScreenChanged` event and backward compatibility code. Refactored driver initialization to handle legacy `IConsoleDriver` types separately. Updated tests and mock implementations to align with the new `SizeChanged` event. Improved documentation and comments to reflect these changes. These updates enhance maintainability, consistency, and modernize the architecture. * Refactor & Code Cleanup: Replace IWindowSizeMonitor with IConsoleSizeMonitor Renamed `IWindowSizeMonitor` to `IConsoleSizeMonitor` across the codebase, updating all references, method signatures, and event handlers. Replaced the `WindowSizeMonitor` class with the new `ConsoleSizeMonitor` implementation, which includes improved terminal size change handling via the `Poll` method. Enabled nullable reference types in several files to enhance code safety. Updated test cases to reflect the new `IConsoleSizeMonitor` interface. Removed redundant code, simplified null checks, and corrected minor typos in comments. Streamlined the codebase by removing the obsolete `WindowSizeMonitor` class and its interface. * Code cleanup - Refactor and enhance ShadowView and FakeDriverTests Updated ShadowView.cs to use null-conditional operators and added null checks for safer access to `ScreenContents`. Refined XML documentation in View.Layout.cs for clarity and consistency. Refactored FakeDriverTests.cs to leverage modern C# features, including shorthand object instantiation, inline lambdas, and tuple-like syntax for `Size` and `Rectangle`. Removed redundant tests and improved test readability and reliability. Enhanced error handling with null checks and ensured backward compatibility for deprecated events. Improved test coverage for resizing, clipboard operations, and invalid coordinates. Verified buffer integrity and screen updates after resizing. General improvements include replacing explicit type declarations with `var`, removing unused imports, and aligning code formatting for better readability. Refactor and improve code quality and test coverage Updated `ShadowView` for null safety using null-conditional operators. Simplified object initializations and modernized syntax across the codebase, including shorthand initializations and inline lambdas. Enhanced event handling logic and ensured compatibility with obsolete members. Refactored `FakeDriverTests` by removing redundant code, standardizing formatting, and improving test setup. Suppressed obsolete warnings where necessary. Improved XML documentation in `View.Layout.cs` for clarity and removed outdated references. Performed general cleanup, including removing unused namespaces, redundant comments, and ensuring consistent formatting. These changes enhance readability, maintainability, and runtime safety. * Code cleanup Refactor TimedEventsTests for readability and consistency Improved code readability and maintainability: - Enabled nullable reference types with `#nullable enable`. - Removed unused `using System.Diagnostics;`. - Updated namespace to `UnitTests.ApplicationTests`. - Replaced `Terminal.Gui.App.TimedEvents` with `TimedEvents`. - Reformatted XML documentation comments for alignment. - Used `var` and target-typed new expressions for consistency. - Reformatted `Parallel.For` loops and lambdas for clarity. - Added `Thread.Sleep(10)` to prevent excessive CPU usage in tests. - Improved assertions and event handler formatting in tests. Aligned with modern C# coding practices. * Code Cleanup - No more driver warnings. Refactor codebase and introduce FakeClipboard - Adjusted `.editorconfig` to change severity levels for CS0612, CS0618, and CS0672 diagnostics. - Replaced `FakeDriver.FakeClipboard` with a new `FakeClipboard` class for testing purposes, supporting exception handling and clipboard data manipulation. - Removed redundant methods (`MakeColor`, `MapKey`) and unused classes (`MockConsoleDriver`) to streamline the codebase. - Refactored `ConsoleDriverFacade` and `FakeDriver` to simplify logic and improve maintainability. - Updated tests to use `CreateFakeDriver` and removed or commented out obsolete tests. - Reformatted and cleaned up code for readability across multiple files. * Refactor FakeDriver - Code Cleanup Standardized console size management by replacing `WindowSizeMonitor` with `ConsoleSizeMonitor` across the codebase. Updated methods `GetWindowSize` and `SetWindowSize` to `GetSize` and `SetSize` for consistency. Refactored `FakeDriver` to use `SetScreenSize` and removed redundant methods. Simplified driver initialization by removing legacy `InternalInit` logic. Standardized ANSI escape sequences by replacing `CSI_ReportTerminalSizeInChars` with `CSI_ReportWindowSizeInChars`. Updated test cases to align with the new `ConsoleSizeMonitor` and `SetScreenSize` methods. Removed obsolete test utilities like `FakeSizeMonitor` and `FakeWindowSizeMonitor`. Performed general code cleanup, including removing unused classes, redundant code, and improving formatting. Fixed resizing logic issues and improved exception handling in driver methods. * Update Terminal.Gui/Drivers/OutputBuffer.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Terminal.Gui/Drivers/MouseButtonStateEx.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Terminal.Gui/App/MainLoop/IApplicationMainLoop.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Tests/UnitTests/Views/ToplevelTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Terminal.Gui/ViewBase/View.Layout.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Moved all Drawing tests to Paralleizable - proving Fakedriver works Enhanced `Ruler` and `Thickness` classes by adding an optional `IConsoleDriver? driver` parameter to decouple rendering from the default `Application.Driver`, improving flexibility and testability. Updated `DriverAssert` to support nullable drivers and added defensive checks. Refactored and expanded test cases for `Ruler`, `Thickness`, `LineCanvas`, and `StraightLineExtensions` to ensure comprehensive coverage, including zero-length intersections, line rendering, and exclusion logic. Migrated rendering-dependent tests to a parallelizable namespace. Removed redundant tests, improved naming conventions, and updated documentation for better maintainability and clarity. * Fixed Run<T> startup hang. Refactor: Simplify driver logic and update SetSize methods Removed FakeDriver safeguard in unit tests to simplify CreateDriver logic. Updated SetSize methods in NetOutput, UnixOutput, and WindowsOutput to do nothing instead of throwing NotImplementedException. Modified SizeChanged event in ConsoleDriverFacade to call SetScreenSize directly. Commented out unnecessary debug validation in DimAuto. These changes improve maintainability and reduce complexity. * Fixed intermittent unit test bug. Refactored `_cachedRunStateToplevel` to `CachedRunStateToplevel` as an internal static property, delegating its management to `ApplicationImpl` for improved encapsulation. Updated all references to use the new property and centralized its handling in `ApplicationImpl`. Removed the `MouseGrabHandler` property from `ApplicationImpl` and simplified driver-related assignments by replacing `Application.ForceDriver` and `Application.Screen` with direct references. Reset `CachedRunStateToplevel` during cleanup to ensure proper state management. Updated the `Invoke` method to use `Top` directly, aligning with the refactored design. Improved debug assertions and performed general cleanup to enhance code readability and maintainability. * Fixed intermittent bug an massive code cleanup of warnings. Refactor and enhance codebase for maintainability - Applied null-conditional operator (`!`) to improve null-safety. - Refactored tests for clarity, added new cases, and removed redundancies. - Introduced `FakeApplicationFactory` and `FakeSizeMonitor` for testing. - Removed unused code, including legacy `DecodeEscSeq` logic. - Reformatted code for readability and consistency. - Updated assertions for more accurate validation in tests. - Fixed potential null reference issues across multiple files. - Improved event handling with proper null checks. - Enhanced documentation for new classes and methods. - Modernized code with C# features like `record struct` and `nullable enable`. --------- 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: Copilot <175728472+Copilot@users.noreply.github.com>
1332 lines
43 KiB
C#
1332 lines
43 KiB
C#
using UnitTests;
|
||
using Xunit.Abstractions;
|
||
|
||
namespace UnitTests.ViewsTests;
|
||
|
||
public class LabelTests (ITestOutputHelper output)
|
||
{
|
||
[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);
|
||
Application.Driver!.SetScreenSize (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);
|
||
Application.Driver!.SetScreenSize (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]
|
||
[AutoInitShutdown]
|
||
public void Label_Draw_Fill_Remaining ()
|
||
{
|
||
Size tfSize = new (80, 1);
|
||
|
||
Label label = new () { Text = "This label needs to be cleared before rewritten.", Width = tfSize.Width, Height = tfSize.Height };
|
||
|
||
TextFormatter tf1 = new () { Direction = TextDirection.LeftRight_TopBottom, ConstrainToSize = tfSize };
|
||
tf1.Text = "This TextFormatter (tf1) without fill will not be cleared on rewritten.";
|
||
|
||
TextFormatter tf2 = new () { Direction = TextDirection.LeftRight_TopBottom, ConstrainToSize = tfSize, FillRemaining = true };
|
||
tf2.Text = "This TextFormatter (tf2) with fill will be cleared on rewritten.";
|
||
|
||
Toplevel top = new ();
|
||
top.Add (label);
|
||
RunState runState = Application.Begin (top);
|
||
AutoInitShutdownAttribute.RunIteration ();
|
||
|
||
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]
|
||
[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);
|
||
Application.Driver!.SetScreenSize (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);
|
||
Application.Driver!.SetScreenSize (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);
|
||
Application.Driver!.SetScreenSize (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);
|
||
Application.Driver!.SetScreenSize (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)
|
||
{
|
||
Application.Driver!.SetScreenSize (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 ()
|
||
{
|
||
Application.Driver!.SetScreenSize (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)
|
||
{
|
||
Application.Driver!.SetScreenSize (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);
|
||
Application.Driver!.SetScreenSize (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);
|
||
Application.Driver!.SetScreenSize (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 ();
|
||
}
|
||
}
|