Files
Terminal.Gui/UnitTests/View/FrameTests.cs
Tig 42b9ad1d61 Fixes #2578 - Updates mouse events to be relative to View.Bounds (#2920)
* initial commit

* Clarified RootMouseEvent

* Added application mouse coord tests

* ViewToScreen -> BoundsToScreen

* Simplified View.Move

* Simplified View.Move

* Updated API docs; made some functions private

* more ViewLayout cleanup

* more ViewLayout cleanup

* Added View.ScreenToBounds and low-level coord unit tests

* Partial fix

* Refactored Application.OnMouseEvent... Tests still fail and views are broken

* Added Bounds/FrameToScreen

* Remamed ScreenToView->ScreenToFrame

* All unit tests pass

* Fixed ListView

* Fixed TableView

* Fixed ColorPicker

* Fixed RadioGroup

* Fixed ListView unit tests

* Fixed line drawing scenario

* Updated comment

* fixed api doc typo

* fixed formatting

* added some thickness Contains unit tests

* MouseEvent api doc updates

* More thickness tests

* More thickness tests
2023-11-26 16:58:52 -07:00

723 lines
16 KiB
C#

using Xunit;
using Xunit.Abstractions;
namespace Terminal.Gui.ViewTests;
public class FrameTests {
readonly ITestOutputHelper _output;
public FrameTests (ITestOutputHelper output)
{
this._output = output;
}
// Test FrameToScreen
[Theory]
[InlineData (0, 0, 0, 0)]
[InlineData (1, 0, 1, 0)]
[InlineData (0, 1, 0, 1)]
[InlineData (1, 1, 1, 1)]
[InlineData (10, 10, 10, 10)]
void FrameToScreen_NoSuperView (int frameX, int frameY, int expectedScreenX, int expectedScreenY)
{
var view = new View () {
X = frameX,
Y = frameY,
Width = 10,
Height = 10
};
var expected = new Rect (expectedScreenX, expectedScreenY, 10, 10);
var actual = view.FrameToScreen ();
Assert.Equal (expected, actual);
}
[Theory]
[InlineData (0, 0, 0, 0, 0)]
[InlineData (1, 0, 0, 1, 1)]
[InlineData (2, 0, 0, 2, 2)]
[InlineData (1, 1, 0, 2, 1)]
[InlineData (1, 0, 1, 1, 2)]
[InlineData (1, 1, 1, 2, 2)]
[InlineData (1, 10, 10, 11, 11)]
void FrameToScreen_SuperView (int superOffset, int frameX, int frameY, int expectedScreenX, int expectedScreenY)
{
var super = new View() {
X = superOffset,
Y = superOffset,
Width = 20,
Height = 20
};
var view = new View () {
X = frameX,
Y = frameY,
Width = 10,
Height = 10
};
super.Add (view);
var expected = new Rect (expectedScreenX, expectedScreenY, 10, 10);
var actual = view.FrameToScreen ();
Assert.Equal (expected, actual);
}
[Theory]
[InlineData (0, 0, 0, 1, 1)]
[InlineData (1, 0, 0, 2, 2)]
[InlineData (2, 0, 0, 3, 3)]
[InlineData (1, 1, 0, 3, 2)]
[InlineData (1, 0, 1, 2, 3)]
[InlineData (1, 1, 1, 3, 3)]
[InlineData (1, 10, 10, 12, 12)]
void FrameToScreen_SuperView_WithBorder (int superOffset, int frameX, int frameY, int expectedScreenX, int expectedScreenY)
{
var super = new View () {
X = superOffset,
Y = superOffset,
Width = 20,
Height = 20,
BorderStyle = LineStyle.Single
};
var view = new View () {
X = frameX,
Y = frameY,
Width = 10,
Height = 10
};
super.Add (view);
var expected = new Rect (expectedScreenX, expectedScreenY, 10, 10);
var actual = view.FrameToScreen ();
Assert.Equal (expected, actual);
}
[Theory]
[InlineData (0, 0, 0, 2, 2)]
[InlineData (1, 0, 0, 4, 4)]
[InlineData (2, 0, 0, 6, 6)]
[InlineData (1, 1, 0, 5, 4)]
[InlineData (1, 0, 1, 4, 5)]
[InlineData (1, 1, 1, 5, 5)]
[InlineData (1, 10, 10, 14, 14)]
void FrameToScreen_NestedSuperView_WithBorder (int superOffset, int frameX, int frameY, int expectedScreenX, int expectedScreenY)
{
var superSuper = new View () {
X = superOffset,
Y = superOffset,
Width = 30,
Height = 30,
BorderStyle = LineStyle.Single
};
var super = new View () {
X = superOffset,
Y = superOffset,
Width = 20,
Height = 20,
BorderStyle = LineStyle.Single
};
superSuper.Add (super);
var view = new View () {
X = frameX,
Y = frameY,
Width = 10,
Height = 10
};
super.Add (view);
var expected = new Rect (expectedScreenX, expectedScreenY, 10, 10);
var actual = view.FrameToScreen ();
Assert.Equal (expected, actual);
}
[Fact]
public void GetFramesThickness ()
{
var view = new View ();
Assert.Equal (Thickness.Empty, view.GetFramesThickness ());
view.Margin.Thickness = new Thickness (1);
Assert.Equal (new Thickness (1), view.GetFramesThickness ());
view.Border.Thickness = new Thickness (1);
Assert.Equal (new Thickness (2), view.GetFramesThickness ());
view.Padding.Thickness = new Thickness (1);
Assert.Equal (new Thickness (3), view.GetFramesThickness ());
view.Padding.Thickness = new Thickness (2);
Assert.Equal (new Thickness (4), view.GetFramesThickness ());
view.Padding.Thickness = new Thickness (1, 2, 3, 4);
Assert.Equal (new Thickness (3, 4, 5, 6), view.GetFramesThickness ());
view.Margin.Thickness = new Thickness (1, 2, 3, 4);
Assert.Equal (new Thickness (3, 5, 7, 9), view.GetFramesThickness ());
view.Dispose ();
}
[Theory, AutoInitShutdown]
[InlineData (0)]
[InlineData (1)]
[InlineData (2)]
[InlineData (3)]
public void Border_With_Title_Size_Height (int height)
{
var win = new Window () {
Title = "1234",
Width = Dim.Fill (),
Height = Dim.Fill ()
};
var rs = Application.Begin (win);
bool firstIteration = false;
((FakeDriver)Application.Driver).SetBufferSize (20, height);
Application.RunIteration (ref rs, ref firstIteration);
var expected = string.Empty;
switch (height) {
case 0:
//Assert.Equal (new Rect (0, 0, 17, 0), subview.Frame);
expected = @"
";
break;
case 1:
//Assert.Equal (new Rect (0, 0, 17, 0), subview.Frame);
expected = @"
────────────────────";
break;
case 2:
//Assert.Equal (new Rect (0, 0, 17, 1), subview.Frame);
expected = @"
┌┤1234├────────────┐
└──────────────────┘
";
break;
case 3:
//Assert.Equal (new Rect (0, 0, 17, 2), subview.Frame);
expected = @"
┌┤1234├────────────┐
│ │
└──────────────────┘
";
break;
}
_ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Application.End (rs);
}
[Theory, AutoInitShutdown]
[InlineData (0)]
[InlineData (1)]
[InlineData (2)]
[InlineData (3)]
[InlineData (4)]
[InlineData (5)]
[InlineData (6)]
[InlineData (7)]
[InlineData (8)]
[InlineData (9)]
[InlineData (10)]
public void Border_With_Title_Size_Width (int width)
{
var win = new Window () {
Title = "1234",
Width = Dim.Fill (),
Height = Dim.Fill ()
};
var rs = Application.Begin (win);
bool firstIteration = false;
((FakeDriver)Application.Driver).SetBufferSize (width, 3);
Application.RunIteration (ref rs, ref firstIteration);
var expected = string.Empty;
switch (width) {
case 1:
//Assert.Equal (new Rect (0, 0, 17, 0), subview.Frame);
expected = @"
│";
break;
case 2:
//Assert.Equal (new Rect (0, 0, 17, 1), subview.Frame);
expected = @"
┌┐
││
└┘";
break;
case 3:
//Assert.Equal (new Rect (0, 0, 17, 2), subview.Frame);
expected = @"
┌─┐
│ │
└─┘
";
break;
case 4:
//Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
expected = @"
┌┤├┐
│ │
└──┘";
break;
case 5:
//Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
expected = @"
┌┤1├┐
│ │
└───┘";
break;
case 6:
//Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
expected = @"
┌┤12├┐
│ │
└────┘";
break;
case 7:
//Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
expected = @"
┌┤123├┐
│ │
└─────┘";
break;
case 8:
//Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
expected = @"
┌┤1234├┐
│ │
└──────┘";
break;
case 9:
//Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
expected = @"
┌┤1234├─┐
│ │
└───────┘";
break;
case 10:
//Assert.Equal (new Rect (0, 0, 17, 3), subview.Frame);
expected = @"
┌┤1234├──┐
│ │
└────────┘";
break;
}
_ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
}
[Fact, AutoInitShutdown]
public void NoSuperView ()
{
var win = new Window () {
Width = Dim.Fill (),
Height = Dim.Fill ()
};
var rs = Application.Begin (win);
bool firstIteration = false;
((FakeDriver)Application.Driver).SetBufferSize (3, 3);
Application.RunIteration (ref rs, ref firstIteration);
var expected = @"
┌─┐
│ │
└─┘";
_ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
}
[Fact, AutoInitShutdown]
public void HasSuperView ()
{
Application.Top.BorderStyle = LineStyle.Double;
var frame = new FrameView () {
Width = Dim.Fill (),
Height = Dim.Fill ()
};
Application.Top.Add (frame);
var rs = Application.Begin (Application.Top);
bool firstIteration = false;
((FakeDriver)Application.Driver).SetBufferSize (5, 5);
Application.RunIteration (ref rs, ref firstIteration);
var expected = @"
╔═══╗
║┌─┐║
║│ │║
║└─┘║
╚═══╝";
_ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Application.End (rs);
}
[Fact, AutoInitShutdown]
public void HasSuperView_Title ()
{
Application.Top.BorderStyle = LineStyle.Double;
var frame = new FrameView () {
Title = "1234",
Width = Dim.Fill (),
Height = Dim.Fill ()
};
Application.Top.Add (frame);
var rs = Application.Begin (Application.Top);
bool firstIteration = false;
((FakeDriver)Application.Driver).SetBufferSize (10, 4);
Application.RunIteration (ref rs, ref firstIteration);
var expected = @"
╔════════╗
║┌┤1234├┐║
║└──────┘║
╚════════╝";
_ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Application.End (rs);
}
[Theory, AutoInitShutdown]
[InlineData (0)]
[InlineData (1)]
[InlineData (2)]
[InlineData (3)]
[InlineData (4)]
[InlineData (5)]
[InlineData (6)]
[InlineData (7)]
[InlineData (8)]
[InlineData (9)]
[InlineData (10)]
public void Border_With_Title_Border_Double_Thickness_Top_Two_Size_Width (int width)
{
var win = new Window () {
Title = "1234",
Width = Dim.Fill (),
Height = Dim.Fill (),
BorderStyle = LineStyle.Double,
};
win.Border.Thickness.Top = 2;
var rs = Application.Begin (win);
bool firstIteration = false;
((FakeDriver)Application.Driver).SetBufferSize (width, 4);
Application.RunIteration (ref rs, ref firstIteration);
var expected = string.Empty;
switch (width) {
case 1:
Assert.Equal (new Rect (0, 0, 1, 4), win.Frame);
expected = @"
║";
break;
case 2:
Assert.Equal (new Rect (0, 0, 2, 4), win.Frame);
expected = @"
╔╗
║║
╚╝";
break;
case 3:
Assert.Equal (new Rect (0, 0, 3, 4), win.Frame);
expected = @"
╔═╗
║ ║
╚═╝";
break;
case 4:
Assert.Equal (new Rect (0, 0, 4, 4), win.Frame);
expected = @"
╒╕
╔╛╘╗
║ ║
╚══╝";
break;
case 5:
Assert.Equal (new Rect (0, 0, 5, 4), win.Frame);
expected = @"
╒═╕
╔╛1╘╗
║ ║
╚═══╝";
break;
case 6:
Assert.Equal (new Rect (0, 0, 6, 4), win.Frame);
expected = @"
╒══╕
╔╛12╘╗
║ ║
╚════╝";
break;
case 7:
Assert.Equal (new Rect (0, 0, 7, 4), win.Frame);
expected = @"
╒═══╕
╔╛123╘╗
║ ║
╚═════╝";
break;
case 8:
Assert.Equal (new Rect (0, 0, 8, 4), win.Frame);
expected = @"
╒════╕
╔╛1234╘╗
║ ║
╚══════╝";
break;
case 9:
Assert.Equal (new Rect (0, 0, 9, 4), win.Frame);
expected = @"
╒════╕
╔╛1234╘═╗
║ ║
╚═══════╝";
break;
case 10:
Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
expected = @"
╒════╕
╔╛1234╘══╗
║ ║
╚════════╝";
break;
}
_ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Application.End (rs);
}
[Theory, AutoInitShutdown]
[InlineData (0)]
[InlineData (1)]
[InlineData (2)]
[InlineData (3)]
[InlineData (4)]
[InlineData (5)]
[InlineData (6)]
[InlineData (7)]
[InlineData (8)]
[InlineData (9)]
[InlineData (10)]
public void Border_With_Title_Border_Double_Thickness_Top_Three_Size_Width (int width)
{
var win = new Window () {
Title = "1234",
Width = Dim.Fill (),
Height = Dim.Fill (),
BorderStyle = LineStyle.Double,
};
win.Border.Thickness.Top = 3;
var rs = Application.Begin (win);
bool firstIteration = false;
((FakeDriver)Application.Driver).SetBufferSize (width, 4);
Application.RunIteration (ref rs, ref firstIteration);
var expected = string.Empty;
switch (width) {
case 1:
Assert.Equal (new Rect (0, 0, 1, 4), win.Frame);
expected = @"
║";
break;
case 2:
Assert.Equal (new Rect (0, 0, 2, 4), win.Frame);
expected = @"
╔╗
║║
╚╝";
break;
case 3:
Assert.Equal (new Rect (0, 0, 3, 4), win.Frame);
expected = @"
╔═╗
║ ║
╚═╝";
break;
case 4:
Assert.Equal (new Rect (0, 0, 4, 4), win.Frame);
expected = @"
╒╕
╔╡╞╗
║╘╛║
╚══╝";
break;
case 5:
Assert.Equal (new Rect (0, 0, 5, 4), win.Frame);
expected = @"
╒═╕
╔╡1╞╗
║╘═╛║
╚═══╝";
break;
case 6:
Assert.Equal (new Rect (0, 0, 6, 4), win.Frame);
expected = @"
╒══╕
╔╡12╞╗
║╘══╛║
╚════╝";
break;
case 7:
Assert.Equal (new Rect (0, 0, 7, 4), win.Frame);
expected = @"
╒═══╕
╔╡123╞╗
║╘═══╛║
╚═════╝";
break;
case 8:
Assert.Equal (new Rect (0, 0, 8, 4), win.Frame);
expected = @"
╒════╕
╔╡1234╞╗
║╘════╛║
╚══════╝";
break;
case 9:
Assert.Equal (new Rect (0, 0, 9, 4), win.Frame);
expected = @"
╒════╕
╔╡1234╞═╗
║╘════╛ ║
╚═══════╝";
break;
case 10:
Assert.Equal (new Rect (0, 0, 10, 4), win.Frame);
expected = @"
╒════╕
╔╡1234╞══╗
║╘════╛ ║
╚════════╝";
break;
}
_ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Application.End (rs);
}
[Theory, AutoInitShutdown]
[InlineData (0)]
[InlineData (1)]
[InlineData (2)]
[InlineData (3)]
[InlineData (4)]
[InlineData (5)]
[InlineData (6)]
[InlineData (7)]
[InlineData (8)]
[InlineData (9)]
[InlineData (10)]
public void Border_With_Title_Border_Double_Thickness_Top_Four_Size_Width (int width)
{
var win = new Window () {
Title = "1234",
Width = Dim.Fill (),
Height = Dim.Fill (),
BorderStyle = LineStyle.Double,
};
win.Border.Thickness.Top = 4;
var rs = Application.Begin (win);
bool firstIteration = false;
((FakeDriver)Application.Driver).SetBufferSize (width, 5);
Application.RunIteration (ref rs, ref firstIteration);
var expected = string.Empty;
switch (width) {
case 1:
Assert.Equal (new Rect (0, 0, 1, 5), win.Frame);
expected = @"
║";
break;
case 2:
Assert.Equal (new Rect (0, 0, 2, 5), win.Frame);
expected = @"
╔╗
║║
╚╝";
break;
case 3:
Assert.Equal (new Rect (0, 0, 3, 5), win.Frame);
expected = @"
╔═╗
║ ║
╚═╝";
break;
case 4:
Assert.Equal (new Rect (0, 0, 4, 5), win.Frame);
expected = @"
╒╕
╔╡╞╗
║╘╛║
╚══╝";
break;
case 5:
Assert.Equal (new Rect (0, 0, 5, 5), win.Frame);
expected = @"
╒═╕
╔╡1╞╗
║╘═╛║
╚═══╝";
break;
case 6:
Assert.Equal (new Rect (0, 0, 6, 5), win.Frame);
expected = @"
╒══╕
╔╡12╞╗
║╘══╛║
╚════╝";
break;
case 7:
Assert.Equal (new Rect (0, 0, 7, 5), win.Frame);
expected = @"
╒═══╕
╔╡123╞╗
║╘═══╛║
╚═════╝";
break;
case 8:
Assert.Equal (new Rect (0, 0, 8, 5), win.Frame);
expected = @"
╒════╕
╔╡1234╞╗
║╘════╛║
╚══════╝";
break;
case 9:
Assert.Equal (new Rect (0, 0, 9, 5), win.Frame);
expected = @"
╒════╕
╔╡1234╞═╗
║╘════╛ ║
╚═══════╝";
break;
case 10:
Assert.Equal (new Rect (0, 0, 10, 5), win.Frame);
expected = @"
╒════╕
╔╡1234╞══╗
║╘════╛ ║
╚════════╝";
break;
}
_ = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Application.End (rs);
}
}