From f8283ecc8342f6aad1f899bb8abf11b71f6c5cea Mon Sep 17 00:00:00 2001 From: Tig Kindel Date: Tue, 27 Feb 2024 09:58:54 -0700 Subject: [PATCH] Fixed Adornment.Bounds and added new tests --- Terminal.Gui/View/Adornment/Adornment.cs | 2 +- UnitTests/View/Adornment/AdornmentTests.cs | 83 ++++++++++++++++++++++ UnitTests/View/FindDeepestViewTests.cs | 1 - 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/View/Adornment/Adornment.cs b/Terminal.Gui/View/Adornment/Adornment.cs index a88186d92..221853b85 100644 --- a/Terminal.Gui/View/Adornment/Adornment.cs +++ b/Terminal.Gui/View/Adornment/Adornment.cs @@ -33,7 +33,7 @@ public class Adornment : View /// Gets the rectangle that describes the inner area of the Adornment. The Location is always (0,0). public override Rectangle Bounds { - get => Thickness?.GetInside (new (Point.Empty, Frame.Size)) ?? new Rectangle (Point.Empty, Frame.Size); + get => new Rectangle (Point.Empty, Thickness?.GetInside (new (Point.Empty, Frame.Size)).Size ?? Frame.Size); // QUESTION: So why even have a setter then? set => throw new InvalidOperationException ("It makes no sense to set Bounds of a Thickness."); } diff --git a/UnitTests/View/Adornment/AdornmentTests.cs b/UnitTests/View/Adornment/AdornmentTests.cs index 8cc054e8b..66a996791 100644 --- a/UnitTests/View/Adornment/AdornmentTests.cs +++ b/UnitTests/View/Adornment/AdornmentTests.cs @@ -121,4 +121,87 @@ public class AdornmentTests adornment.Thickness = new Thickness (1, 2, 3, 4); Assert.True (raised); } + + [Fact] + public void Frames_are_Parent_SuperView_Relative () + { + var view = new View + { + X = 1, + Y = 2, + Width = 20, + Height = 31 + }; + + var marginThickness = 1; + view.Margin.Thickness = new Thickness (marginThickness); + + var borderThickness = 2; + view.Border.Thickness = new Thickness (borderThickness); + + var paddingThickness = 3; + view.Padding.Thickness = new Thickness (paddingThickness); + + view.BeginInit (); + view.EndInit (); + + Assert.Equal (new Rectangle (1, 2, 20, 31), view.Frame); + Assert.Equal (new Rectangle (0, 0, 8, 19), view.Bounds); + + // Margin.Frame is always the same as the view frame + Assert.Equal (new Rectangle (0, 0, 20, 31), view.Margin.Frame); + + // Border.Frame is View.Frame minus the Margin thickness + Assert.Equal ( + new Rectangle (marginThickness, marginThickness, view.Frame.Width - marginThickness * 2, view.Frame.Height - marginThickness * 2), + view.Border.Frame); + + // Padding.Frame is View.Frame minus the Border thickness plus Margin thickness + Assert.Equal ( + new Rectangle ( + marginThickness + borderThickness, + marginThickness + borderThickness, + view.Frame.Width - (marginThickness + borderThickness) * 2, + view.Frame.Height - (marginThickness + borderThickness) * 2), + view.Padding.Frame); + } + + [Fact] + public void Bounds_Location_Always_Empty_Size_Correct () + { + var view = new View + { + X = 1, + Y = 2, + Width = 20, + Height = 31 + }; + + var marginThickness = 1; + view.Margin.Thickness = new Thickness (marginThickness); + + var borderThickness = 2; + view.Border.Thickness = new Thickness (borderThickness); + + var paddingThickness = 3; + view.Padding.Thickness = new Thickness (paddingThickness); + + view.BeginInit (); + view.EndInit (); + + Assert.Equal (new Rectangle (1, 2, 20, 31), view.Frame); + Assert.Equal (new Rectangle (0, 0, 8, 19), view.Bounds); + + Assert.Equal (new Rectangle (0, 0, view.Margin.Frame.Width - marginThickness * 2, view.Margin.Frame.Height - marginThickness * 2), view.Margin.Bounds); + + Assert.Equal (new Rectangle (0, 0, view.Border.Frame.Width - borderThickness * 2, view.Border.Frame.Height - borderThickness * 2), view.Border.Bounds); + + Assert.Equal ( + new Rectangle ( + 0, + 0, + view.Padding.Frame.Width - (marginThickness + borderThickness) * 2, + view.Padding.Frame.Height - (marginThickness + borderThickness) * 2), + view.Padding.Bounds); + } } diff --git a/UnitTests/View/FindDeepestViewTests.cs b/UnitTests/View/FindDeepestViewTests.cs index 755ecd30a..6434eaa8c 100644 --- a/UnitTests/View/FindDeepestViewTests.cs +++ b/UnitTests/View/FindDeepestViewTests.cs @@ -207,7 +207,6 @@ public class FindDeepestViewTests (ITestOutputHelper output) var found = View.FindDeepestView (start, testX, testY, true); Assert.Equal(expectedAdornmentType, found.GetType()); - } // Test that FindDeepestView works if the subview has positive Adornments