Files
Terminal.Gui/Tests/UnitTestsParallelizable/View/Layout/Dim.Tests.cs
Tig 47bcf1bf57 Partial on #2975 - Replaces Menu v1 in many places with v2 (#4040)
* touching publish.yml

* Fixed UICatalog bugs. Added fluent tests.

* marked v1 menu stuff as obsolte

* Tweaks.
Added View.GetSubMenus<type>().

* fixed unit tests

* general messing around

* general messing around

* Playing with Fluent

* ColorScheme tweaks

* WIP: ColorScheme tweaks

* Playing with Fluent

* Merged from laptop2

* Hacky-ish fixes to:
- #4016
- #4014

* Fixed Region bug preventing menus without borders from working

* Tweaks

* Fixed a bunch of CM issues

* Fixed OoptionSelector

* ip

* FixedCM issues

* Fixed CM issues2

* Revert "FixedCM issues"

This reverts commit dd6c6a70a3.

* Reverted stuff

* Found and fixed bug in AllViews_Center_Properly

* Fixed CM issues2

* removed menuv2 onapplied.
Changed how UICatalog Applys CM

* changed test time out to see if it helkps with ubuntu fails

* reset app on fail?

* back to 1500ms

* Made StatusBar nullable.

* Code Cleanup.

* HexEditor Code Cleanup.

* HexEditor Code Cleanup.

* Back to 3000ms. Sigh.

* Trying different logic

* Trying different logic2

* Fixed potential crash in runlop

* Fixed potential crash in runlop2

* Tweaked Spinner stuff

* Removed TabView from TextEffects scenario. Not needed and possible culprit.

* back to 2000ms

* WIP: Revamping menu scenarios

* Menu Scenario refinements.
Fixed a few bugs.
Code cleanup.

* fixed unit test

* Fixed warnings

* Fixed warnings2

* Fixed File.Exit

* WIP: Dealing with QuitKey struggles

* WIP: Dealing with QuitKey struggles 2

* WIP: Dealing with QuitKey struggles 3

* Fixed ListView collection nav bug

* Fixed a bunch of menu stuff.
Fixed Appv2 stuff.

* Lots of refactoring and fixing

* Lots of unit test issues

* Fixed DebugIDisposable issues

* Fixed release build issue

* Fixed release build issue 2

* DebugIDisposable -> EnableDebugIDisposableAsserts and more

* DebugIDisposable -> EnableDebugIDisposableAsserts and more 2

* Fixed Menus scenario - context menu

* Added @bdisp suggested assert. Commented it out as it breaks tests.

* Code cleanup

* Fixed disposed but

* Fixed UICatalog exit

* Fixed Unit test I broke.
Added 'Minimal' Theme that turns off all borders etc...
2025-04-24 05:17:58 -06:00

247 lines
7.0 KiB
C#

using System.Globalization;
using System.Text;
using UnitTests;
using Xunit.Abstractions;
using static Terminal.Gui.Dim;
namespace Terminal.Gui.LayoutTests;
[Collection ("Global Test Setup")]
public class DimTests
{
[Fact]
public void DimAbsolute_Calculate_ReturnsCorrectValue ()
{
var dim = new DimAbsolute (10);
int result = dim.Calculate (0, 100, null, Dimension.None);
Assert.Equal (10, result);
}
// TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
// TODO: A new test that calls SetRelativeLayout directly is needed.
[Fact]
public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
{
var t = new View { Width = 80, Height = 25, Text = "top" };
var w = new View
{
BorderStyle = LineStyle.Single,
X = 1,
Y = 2,
Width = 4,
Height = 5,
Title = "w"
};
t.Add (w);
t.LayoutSubViews ();
Assert.Equal (3, w.Width = 3);
Assert.Equal (4, w.Height = 4);
t.Dispose ();
}
[Fact]
public void DimHeight_Set_To_Null_Throws ()
{
Dim dim = Height (null);
Assert.Throws<NullReferenceException> (() => dim.ToString ());
}
[Fact]
public void DimHeight_SetsValue ()
{
var testVal = Rectangle.Empty;
var testValview = new View { Frame = testVal };
Dim dim = Height (testValview);
Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
testValview.Dispose ();
testVal = new (1, 2, 3, 4);
testValview = new () { Frame = testVal };
dim = Height (testValview);
Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
testValview.Dispose ();
}
[Fact]
public void Internal_Tests ()
{
var dimFactor = new DimPercent (10);
Assert.Equal (10, dimFactor.GetAnchor (100));
var dimAbsolute = new DimAbsolute (10);
Assert.Equal (10, dimAbsolute.GetAnchor (0));
var dimFill = new DimFill (1);
Assert.Equal (99, dimFill.GetAnchor (100));
var dimCombine = new DimCombine (AddOrSubtract.Add, dimFactor, dimAbsolute);
Assert.Equal (dimCombine.Left, dimFactor);
Assert.Equal (dimCombine.Right, dimAbsolute);
Assert.Equal (20, dimCombine.GetAnchor (100));
var view = new View { Frame = new (20, 10, 20, 1) };
var dimViewHeight = new DimView (view, Dimension.Height);
Assert.Equal (1, dimViewHeight.GetAnchor (0));
var dimViewWidth = new DimView (view, Dimension.Width);
Assert.Equal (20, dimViewWidth.GetAnchor (0));
view.Dispose ();
}
// TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
// TODO: A new test that calls SetRelativeLayout directly is needed.
[Fact]
public void Referencing_SuperView_Does_Not_Throw ()
{
var super = new View { Width = 10, Height = 10, Text = "super" };
var view = new View
{
Width = Width (super), // this is allowed
Height = Height (super), // this is allowed
Text = "view"
};
super.Add (view);
super.BeginInit ();
super.EndInit ();
Exception exception = Record.Exception (super.LayoutSubViews);
Assert.Null (exception);
super.Dispose ();
}
[Fact]
public void DimSized_Equals ()
{
var n1 = 0;
var n2 = 0;
Dim dim1 = Absolute (n1);
Dim dim2 = Absolute (n2);
Assert.Equal (dim1, dim2);
n1 = n2 = 1;
dim1 = Absolute (n1);
dim2 = Absolute (n2);
Assert.Equal (dim1, dim2);
n1 = n2 = -1;
dim1 = Absolute (n1);
dim2 = Absolute (n2);
Assert.Equal (dim1, dim2);
n1 = 0;
n2 = 1;
dim1 = Absolute (n1);
dim2 = Absolute (n2);
Assert.NotEqual (dim1, dim2);
}
[Fact]
public void DimSized_SetsValue ()
{
Dim dim = Absolute (0);
Assert.Equal ("Absolute(0)", dim.ToString ());
var testVal = 5;
dim = Absolute (testVal);
Assert.Equal ($"Absolute({testVal})", dim.ToString ());
testVal = -1;
dim = Absolute (testVal);
Assert.Equal ($"Absolute({testVal})", dim.ToString ());
}
// TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
// TODO: A new test that calls SetRelativeLayout directly is needed.
[Fact]
public void Validation_Does_Not_Throw_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
{
var t = new View { Width = 80, Height = 25, Text = "top" };
var w = new Window
{
X = 1,
Y = 2,
Width = 4,
Height = 5,
Title = "w"
};
t.Add (w);
t.LayoutSubViews ();
Assert.Equal (3, w.Width = 3);
Assert.Equal (4, w.Height = 4);
t.Dispose ();
}
[Fact]
public void DimWidth_Equals ()
{
var testRect1 = Rectangle.Empty;
var view1 = new View { Frame = testRect1 };
var testRect2 = Rectangle.Empty;
var view2 = new View { Frame = testRect2 };
Dim dim1 = Width (view1);
Dim dim2 = Width (view1);
// FIXED: Dim.Width should support Equals() and this should change to Equal.
Assert.Equal (dim1, dim2);
dim2 = Width (view2);
Assert.NotEqual (dim1, dim2);
testRect1 = new (0, 1, 2, 3);
view1 = new () { Frame = testRect1 };
testRect2 = new (0, 1, 2, 3);
dim1 = Width (view1);
dim2 = Width (view1);
// FIXED: Dim.Width should support Equals() and this should change to Equal.
Assert.Equal (dim1, dim2);
testRect1 = new (0, -1, 2, 3);
view1 = new () { Frame = testRect1 };
testRect2 = new (0, -1, 2, 3);
dim1 = Width (view1);
dim2 = Width (view1);
// FIXED: Dim.Width should support Equals() and this should change to Equal.
Assert.Equal (dim1, dim2);
testRect1 = new (0, -1, 2, 3);
view1 = new () { Frame = testRect1 };
testRect2 = Rectangle.Empty;
view2 = new () { Frame = testRect2 };
dim1 = Width (view1);
dim2 = Width (view2);
Assert.NotEqual (dim1, dim2);
}
[Fact]
public void DimWidth_Set_To_Null_Throws ()
{
Dim dim = Width (null);
Assert.Throws<NullReferenceException> (() => dim.ToString ());
}
[Fact]
public void DimWidth_SetsValue ()
{
var testVal = Rectangle.Empty;
var testValView = new View { Frame = testVal };
Dim dim = Width (testValView);
Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
testValView.Dispose ();
testVal = new (1, 2, 3, 4);
testValView = new () { Frame = testVal };
dim = Width (testValView);
Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
testValView.Dispose ();
}
}