using System.Reflection.Emit;
using System.Text;
using Xunit.Abstractions;
namespace Terminal.Gui.ViewTests;
/// Tests of the property which auto sizes Views based on .
public class AutoSizeTrueTests
{
private readonly ITestOutputHelper _output;
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 │
└────────────────────┘"
};
private static readonly Size _size1x1 = new (1, 1);
public AutoSizeTrueTests (ITestOutputHelper output) { _output = output; }
[Fact]
[AutoInitShutdown]
public void AutoSize_AnchorEnd_Better_Than_Bottom_Equal_Inside_Window ()
{
var win = new Window ();
// Label is AutoSize == true
var label = new Label
{
Text = "This should be the last line.",
ColorScheme = Colors.ColorSchemes ["Menu"],
//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);
((FakeDriver)Application.Driver).SetBufferSize (40, 10);
Assert.True (label.AutoSize);
Assert.Equal (29, label.Text.Length);
Assert.Equal (new Rectangle (0, 0, 40, 10), top.Frame);
Assert.Equal (new Rectangle (0, 0, 40, 10), win.Frame);
Assert.Equal (new Rectangle (0, 7, 29, 1), label.Frame);
var expected = @"
┌──────────────────────────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│This should be the last line. │
└──────────────────────────────────────┘
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Application.End (rs);
}
[Fact]
[AutoInitShutdown]
public void AutoSize_AnchorEnd_Better_Than_Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
{
var win = new Window ();
// Label is AutoSize == true
var label = new Label
{
Text = "This should be the last line.",
ColorScheme = Colors.ColorSchemes ["Menu"],
//Width = Dim.Fill (),
X = 0,
Y = Pos.AnchorEnd (1)
};
win.Add (label);
var menu = new MenuBar { Menus = new MenuBarItem [] { new ("Menu", "", null) } };
var status = new StatusBar (new StatusItem [] { new (KeyCode.F1, "~F1~ Help", null) });
Toplevel top = new ();
top.Add (win, menu, status);
RunState rs = Application.Begin (top);
Assert.True (label.AutoSize);
Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
Assert.Equal (new Rectangle (0, 0, 80, 1), menu.Frame);
Assert.Equal (new Rectangle (0, 24, 80, 1), status.Frame);
Assert.Equal (new Rectangle (0, 1, 80, 23), win.Frame);
Assert.Equal (new Rectangle (0, 20, 29, 1), label.Frame);
var expected = @"
Menu
┌──────────────────────────────────────────────────────────────────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│This should be the last line. │
└──────────────────────────────────────────────────────────────────────────────┘
F1 Help
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Application.End (rs);
}
[Fact]
[AutoInitShutdown]
public void AutoSize_Bottom_Equal_Inside_Window ()
{
var win = new Window ();
// Label is AutoSize == true
var label = new Label
{
Text = "This should be the last line.",
ColorScheme = Colors.ColorSchemes ["Menu"],
//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);
((FakeDriver)Application.Driver).SetBufferSize (40, 10);
Assert.True (label.AutoSize);
Assert.Equal (new Rectangle (0, 0, 40, 10), top.Frame);
Assert.Equal (new Rectangle (0, 0, 40, 10), win.Frame);
Assert.Equal (new Rectangle (0, 7, 29, 1), label.Frame);
var expected = @"
┌──────────────────────────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│This should be the last line. │
└──────────────────────────────────────┘
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Application.End (rs);
}
[Fact]
[AutoInitShutdown]
public void AutoSize_Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
{
var win = new Window ();
// Label is AutoSize == true
var label = new Label
{
Text = "This should be the last line.",
ColorScheme = Colors.ColorSchemes ["Menu"],
//Width = Dim.Fill (),
X = 0,
Y = Pos.Bottom (win)
- 4 // two lines top and bottom borders more two lines above border
};
win.Add (label);
var menu = new MenuBar { Menus = new MenuBarItem [] { new ("Menu", "", null) } };
var status = new StatusBar (new StatusItem [] { new (KeyCode.F1, "~F1~ Help", null) });
Toplevel top = new ();
top.Add (win, menu, status);
RunState rs = Application.Begin (top);
Assert.True (label.AutoSize);
Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
Assert.Equal (new Rectangle (0, 0, 80, 1), menu.Frame);
Assert.Equal (new Rectangle (0, 24, 80, 1), status.Frame);
Assert.Equal (new Rectangle (0, 1, 80, 23), win.Frame);
Assert.Equal (new Rectangle (0, 20, 29, 1), label.Frame);
var expected = @"
Menu
┌──────────────────────────────────────────────────────────────────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│This should be the last line. │
└──────────────────────────────────────────────────────────────────────────────┘
F1 Help
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Application.End (rs);
}
[Fact]
[AutoInitShutdown]
public void AutoSize_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;
// Label is AutoSize == true
List