瀏覽代碼

Refactored DimAuto unit tests

Tig 1 年之前
父節點
當前提交
51433c0112

+ 226 - 0
UnitTests/View/Layout/Dim.AutoTests.DimTypes.cs

@@ -0,0 +1,226 @@
+namespace Terminal.Gui.LayoutTests;
+
+public partial class DimAutoTests
+{
+    // Tests all the Dim Types in Subview scenarios to ensure DimAutoStyle.Content is calculated correctly
+
+    #region DimAbsolute
+
+    [Theory]
+    [InlineData (0, 15, 15)]
+    [InlineData (1, 15, 16)]
+    [InlineData (-1, 15, 14)]
+    public void With_Subview_Using_DimAbsolute (int subViewOffset, int dimAbsoluteSize, int expectedSize)
+    {
+        var view = new View ();
+
+        var subview = new View
+        {
+            X = subViewOffset,
+            Y = subViewOffset,
+            Width = Dim.Absolute (dimAbsoluteSize),
+            Height = Dim.Absolute (dimAbsoluteSize)
+        };
+        view.Add (subview);
+
+        Dim dim = Dim.Auto (DimAutoStyle.Content);
+
+        int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
+        int calculatedHeight = dim.Calculate (0, 100, view, Dimension.Height);
+
+        Assert.Equal (expectedSize, calculatedWidth);
+        Assert.Equal (expectedSize, calculatedHeight);
+    }
+
+    #endregion DimAbsolute
+
+    #region DimPercent
+
+    [Theory]
+    [InlineData (0, 0, 0, 0, 0, 0, 0, 0)]
+    [InlineData (0, 50, 0, 0, 0, 0, 0, 0)]
+    [InlineData (0, 0, 100, 100, 100, 100, 100, 100)]
+    [InlineData (0, 50, 100, 100, 100, 100, 100, 100)]
+    public void With_Subview_Using_DimPercent (
+        int subViewOffset,
+        int percent,
+        int minWidth,
+        int maxWidth,
+        int minHeight,
+        int maxHeight,
+        int expectedWidth,
+        int expectedHeight
+    )
+    {
+        var view = new View
+        {
+            Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
+            Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
+        };
+
+        var subview = new View
+        {
+            X = subViewOffset,
+            Y = subViewOffset,
+            Width = Dim.Percent (percent),
+            Height = Dim.Percent (percent)
+        };
+        view.Add (subview);
+
+        // Assuming the calculation is done after layout
+        int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
+        int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
+        int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
+        int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
+
+        Assert.Equal (expectedWidth, calculatedWidth); // subview's width
+        Assert.Equal (expectedHeight, calculatedHeight); // subview's height
+        Assert.Equal (subViewOffset, calculatedX);
+        Assert.Equal (subViewOffset, calculatedY);
+
+        view.SetRelativeLayout (new (100, 100));
+        view.LayoutSubviews ();
+
+        Assert.Equal (expectedWidth * (percent / 100f), subview.Viewport.Width);
+        Assert.Equal (expectedHeight * (percent / 100f), subview.Viewport.Height);
+    }
+    #endregion DimPercent
+
+    #region DimFill
+
+    [Theory]
+    [InlineData (0, 0, 0, 0, 0, 0)]
+    [InlineData (0, 19, 0, 9, 0, 0)]
+    [InlineData (0, 20, 0, 10, 0, 0)]
+    [InlineData (0, 21, 0, 11, 0, 0)]
+    [InlineData (1, 21, 1, 11, 1, 1)]
+    [InlineData (21, 21, 11, 11, 21, 11)]
+    public void With_Subview_Using_DimFill (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
+    {
+        var view = new View
+        {
+            Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
+            Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
+        };
+
+        var subview = new View
+        {
+            X = 0,
+            Y = 0,
+            Width = Dim.Fill (),
+            Height = Dim.Fill ()
+        };
+        view.Add (subview);
+
+        // Assuming the calculation is done after layout
+        int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
+        int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
+        int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
+        int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
+
+        Assert.Equal (expectedWidth, calculatedWidth);
+        Assert.Equal (expectedHeight, calculatedHeight);
+
+        Assert.Equal (0, calculatedX);
+        Assert.Equal (0, calculatedY);
+    }
+
+    [Theory]
+    [InlineData (0, 0, 0, 0, 0, 0)]
+    [InlineData (0, 19, 0, 9, 2, 4)]
+    [InlineData (0, 20, 0, 10, 2, 4)]
+    [InlineData (0, 21, 0, 11, 2, 4)]
+    [InlineData (1, 21, 1, 11, 2, 4)]
+    [InlineData (21, 21, 11, 11, 21, 11)]
+    public void With_Subview_Using_DimFill_And_Another_Subview (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
+    {
+        var view = new View
+        {
+            Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
+            Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
+        };
+
+        var absView = new View
+        {
+            X = 1,
+            Y = 2,
+            Width = 1,
+            Height = 2
+        };
+        view.Add (absView);
+
+        var subview = new View
+        {
+            X = 0,
+            Y = 0,
+            Width = Dim.Fill (),
+            Height = Dim.Fill ()
+        };
+        view.Add (subview);
+
+        // Assuming the calculation is done after layout
+        int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
+        int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
+        int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
+        int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
+
+        Assert.Equal (expectedWidth, calculatedWidth);
+        Assert.Equal (expectedHeight, calculatedHeight);
+
+        Assert.Equal (0, calculatedX);
+        Assert.Equal (0, calculatedY);
+    }
+
+    #endregion
+
+    #region DimFunc
+
+    [Fact]
+    public void With_Subview_Using_DimFunc ()
+    {
+        var view = new View ();
+        var subview = new View { Width = Dim.Func (() => 20), Height = Dim.Func (() => 25) };
+        view.Add (subview);
+
+        subview.SetRelativeLayout (new (100, 100));
+
+        Dim dim = Dim.Auto (DimAutoStyle.Content);
+
+        int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
+        int calculatedHeight = dim.Calculate (0, 100, view, Dimension.Height);
+
+        Assert.Equal (20, calculatedWidth);
+        Assert.Equal (25, calculatedHeight);
+    }
+
+    #endregion DimFunc
+
+    #region DimView
+
+    [Fact]
+    public void With_Subview_Using_DimView ()
+    {
+        var view = new View ();
+        var subview = new View { Width = 30, Height = 40 };
+        var subSubview = new View { Width = Dim.Width (subview), Height = Dim.Height (subview) };
+        view.Add (subview);
+        view.Add (subSubview);
+
+        subview.SetRelativeLayout (new (100, 100));
+
+        Dim dim = Dim.Auto (DimAutoStyle.Content);
+
+        int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
+        int calculatedHeight = dim.Calculate (0, 100, view, Dimension.Height);
+
+        // Expecting the size to match the subview, which is the largest
+        Assert.Equal (30, calculatedWidth);
+        Assert.Equal (40, calculatedHeight);
+    }
+
+    #endregion DimView
+
+    #region DimCombine
+    // TODO: Need DimCombine tests
+    #endregion DimCombine
+}

+ 195 - 0
UnitTests/View/Layout/Dim.AutoTests.MinMax.cs

@@ -0,0 +1,195 @@
+namespace Terminal.Gui.LayoutTests;
+
+public partial class DimAutoTests
+{
+    #region minimumContentDim Tests
+
+    [Fact]
+    public void Min_Is_Honored ()
+    {
+        var superView = new View
+        {
+            X = 0,
+            Y = 0,
+            Width = Dim.Auto (minimumContentDim: 10),
+            Height = Dim.Auto (minimumContentDim: 10),
+            ValidatePosDim = true
+        };
+
+        var subView = new View
+        {
+            X = 0,
+            Y = 0,
+            Width = 5,
+            Height = 5
+        };
+
+        superView.Add (subView);
+        superView.BeginInit ();
+        superView.EndInit ();
+
+        superView.SetRelativeLayout (new (10, 10));
+        superView.LayoutSubviews (); // no throw
+
+        Assert.Equal (10, superView.Frame.Width);
+        Assert.Equal (10, superView.Frame.Height);
+    }
+
+    [Theory]
+    [InlineData (0, 2, 4)]
+    [InlineData (1, 2, 4)]
+    [InlineData (2, 2, 4)]
+    [InlineData (3, 2, 5)]
+    [InlineData (1, 0, 3)]
+    public void Min_Absolute_Is_Content_Relative (int contentSize, int minAbsolute, int expected)
+    {
+        var view = new View
+        {
+            X = 0,
+            Y = 0,
+            Width = Dim.Auto (minimumContentDim: minAbsolute),
+            BorderStyle = LineStyle.Single, // a 1 thick adornment
+            ValidatePosDim = true
+        };
+
+        view.SetContentSize (new (contentSize, 0));
+
+        Assert.Equal (expected, view.Frame.Width);
+    }
+
+    [Theory]
+    [InlineData (1, 100, 100)]
+    [InlineData (1, 50, 50)]
+    public void Min_Percent (int contentSize, int minPercent, int expected)
+    {
+        var view = new View
+        {
+            X = 0,
+            Y = 0,
+            Width = Dim.Auto (minimumContentDim: Dim.Percent (minPercent)),
+            ValidatePosDim = true
+        };
+
+        view.SetContentSize (new (contentSize, 0));
+        view.SetRelativeLayout (new (100, 100));
+
+        Assert.Equal (expected, view.Frame.Width);
+    }
+
+    [Theory]
+    [InlineData (1, 100, 102)]
+    [InlineData (1, 50, 52)] // 50% of 100 is 50, but the border adds 2
+    [InlineData (1, 30, 32)] // 30% of 100 is 30, but the border adds 2
+    [InlineData (2, 30, 32)] // 30% of 100 is 30, but the border adds 2
+    public void Min_Percent_Is_Content_Relative (int contentSize, int minPercent, int expected)
+    {
+        var view = new View
+        {
+            X = 0,
+            Y = 0,
+            Width = Dim.Auto (minimumContentDim: Dim.Percent (minPercent)),
+            BorderStyle = LineStyle.Single, // a 1 thick adornment
+            ValidatePosDim = true
+        };
+
+        view.SetContentSize (new (contentSize, 0));
+        view.SetRelativeLayout (new (100, 100));
+
+        Assert.Equal (expected, view.Frame.Width);
+    }
+
+    [Fact]
+    public void Min_Resets_If_Subview_Moves_Negative ()
+    {
+        var superView = new View
+        {
+            X = 0,
+            Y = 0,
+            Width = Dim.Auto (minimumContentDim: 10),
+            Height = Dim.Auto (minimumContentDim: 10),
+            ValidatePosDim = true
+        };
+
+        var subView = new View
+        {
+            X = 0,
+            Y = 0,
+            Width = 5,
+            Height = 5
+        };
+
+        superView.Add (subView);
+        superView.BeginInit ();
+        superView.EndInit ();
+
+        superView.SetRelativeLayout (new (10, 10));
+        superView.LayoutSubviews (); // no throw
+
+        Assert.Equal (10, superView.Frame.Width);
+        Assert.Equal (10, superView.Frame.Height);
+
+        subView.X = -1;
+        subView.Y = -1;
+        superView.SetRelativeLayout (new (10, 10));
+        superView.LayoutSubviews (); // no throw
+
+        Assert.Equal (5, subView.Frame.Width);
+        Assert.Equal (5, subView.Frame.Height);
+
+        Assert.Equal (10, superView.Frame.Width);
+        Assert.Equal (10, superView.Frame.Height);
+    }
+
+    [Fact]
+    public void Min_Resets_If_Subview_Shrinks ()
+    {
+        var superView = new View
+        {
+            X = 0,
+            Y = 0,
+            Width = Dim.Auto (minimumContentDim: 10),
+            Height = Dim.Auto (minimumContentDim: 10),
+            ValidatePosDim = true
+        };
+
+        var subView = new View
+        {
+            X = 0,
+            Y = 0,
+            Width = 5,
+            Height = 5
+        };
+
+        superView.Add (subView);
+        superView.BeginInit ();
+        superView.EndInit ();
+
+        superView.SetRelativeLayout (new (10, 10));
+        superView.LayoutSubviews (); // no throw
+
+        Assert.Equal (10, superView.Frame.Width);
+        Assert.Equal (10, superView.Frame.Height);
+
+        subView.Width = 3;
+        subView.Height = 3;
+        superView.SetRelativeLayout (new (10, 10));
+        superView.LayoutSubviews (); // no throw
+
+        Assert.Equal (3, subView.Frame.Width);
+        Assert.Equal (3, subView.Frame.Height);
+
+        Assert.Equal (10, superView.Frame.Width);
+        Assert.Equal (10, superView.Frame.Height);
+    }
+
+    #endregion minimumContentDim Tests
+
+    // Test min - ensure that if min is specified in the DimAuto constructor it is honored
+
+    // what happens if DimAuto (min: 10) and the subview moves to a negative coord?
+
+    #region maximumContentDim Tests
+    // TODO: Add tests
+
+    #endregion maximumContentDim Tests
+}

+ 334 - 0
UnitTests/View/Layout/Dim.AutoTests.PosTypes.cs

@@ -0,0 +1,334 @@
+namespace Terminal.Gui.LayoutTests;
+
+public partial class DimAutoTests
+{
+    // Tests all the Pos Types in Subview scenarios to ensure DimAutoStyle.Content is calculated correctly
+
+    #region PosAbsolute
+
+    [Theory]
+    [InlineData (0, 0, 0, 0, 0, 0)]
+    [InlineData (0, 19, 0, 9, 19, 9)]
+    [InlineData (0, 20, 0, 10, 20, 10)]
+    [InlineData (0, 21, 0, 11, 21, 11)]
+    [InlineData (1, 21, 1, 11, 21, 11)]
+    [InlineData (21, 21, 11, 11, 21, 11)]
+    public void With_Subview_Using_PosAbsolute (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
+    {
+        var view = new View
+        {
+            Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
+            Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
+        };
+
+        var subview = new View
+        {
+            X = Pos.Absolute (10),
+            Y = Pos.Absolute (5),
+            Width = 20,
+            Height = 10
+        };
+        view.Add (subview);
+
+        // Assuming the calculation is done after layout
+        int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
+        int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
+        int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
+        int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
+
+        Assert.Equal (expectedWidth, calculatedWidth);
+        Assert.Equal (expectedHeight, calculatedHeight);
+
+        Assert.Equal (0, calculatedX);
+        Assert.Equal (0, calculatedY);
+    }
+
+    #endregion PosAbsolute
+
+    #region PosPercent
+
+    [Theory]
+    [InlineData (0, 0, 0, 0, 0, 0)]
+    [InlineData (0, 19, 0, 9, 19, 9)]
+    [InlineData (0, 20, 0, 10, 20, 10)]
+    [InlineData (0, 21, 0, 11, 20, 10)]
+    [InlineData (1, 21, 1, 11, 20, 10)]
+    [InlineData (21, 21, 11, 11, 21, 11)]
+    public void With_Subview_Using_PosPercent (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
+    {
+        var view = new View
+        {
+            Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
+            Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
+        };
+
+        var subview = new View
+        {
+            X = Pos.Percent (50),
+            Y = Pos.Percent (50),
+            Width = 20,
+            Height = 10
+        };
+        view.Add (subview);
+
+        // Assuming the calculation is done after layout
+        int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
+        int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
+        int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
+        int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
+
+        Assert.Equal (expectedWidth, calculatedWidth);
+        Assert.Equal (expectedHeight, calculatedHeight);
+
+        Assert.Equal (0, calculatedX);
+        Assert.Equal (0, calculatedY);
+
+        view.BeginInit ();
+        view.EndInit ();
+
+        // subview should be at 50% in the parent view
+        Assert.Equal ((int)(view.Viewport.Width * .50), subview.Frame.X);
+        Assert.Equal ((int)(view.Viewport.Height * .50), subview.Frame.Y);
+    }
+
+    [Theory]
+    [InlineData (0, 0, 0, 0, 0, 0)]
+    [InlineData (0, 19, 0, 9, 19, 9)]
+    [InlineData (0, 20, 0, 10, 20, 10)]
+    [InlineData (0, 21, 0, 11, 21, 11)]
+    [InlineData (1, 21, 1, 11, 21, 11)]
+    [InlineData (21, 21, 11, 11, 21, 11)]
+    public void With_Subview_Using_PosPercent_Combine (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
+    {
+        var view = new View
+        {
+            Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
+            Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
+        };
+
+        var subview = new View
+        {
+            X = Pos.Percent (50) + 1,
+            Y = 1 + Pos.Percent (50),
+            Width = 20,
+            Height = 10
+        };
+        view.Add (subview);
+
+        // Assuming the calculation is done after layout
+        int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
+        int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
+        int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
+        int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
+
+        Assert.Equal (expectedWidth, calculatedWidth);
+        Assert.Equal (expectedHeight, calculatedHeight);
+
+        Assert.Equal (0, calculatedX);
+        Assert.Equal (0, calculatedY);
+
+        view.BeginInit ();
+        view.EndInit ();
+
+        // subview should be at 50% in the parent view
+        Assert.Equal ((int)(view.Viewport.Width * .50) + 1, subview.Frame.X);
+        Assert.Equal ((int)(view.Viewport.Height * .50) + 1, subview.Frame.Y);
+    }
+
+    #endregion PosPercent
+
+    #region PosCenter
+
+    [Theory]
+    [InlineData (0, 0, 0, 0, 0, 0)]
+    [InlineData (0, 19, 0, 9, 19, 9)]
+    [InlineData (0, 20, 0, 10, 20, 10)]
+    [InlineData (0, 21, 0, 11, 20, 10)]
+    [InlineData (1, 21, 1, 11, 20, 10)]
+    [InlineData (21, 21, 11, 11, 21, 11)]
+    public void With_Subview_Using_PosCenter (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
+    {
+        var view = new View
+        {
+            Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
+            Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
+        };
+
+        var subview = new View
+        {
+            X = Pos.Center (),
+            Y = Pos.Center (),
+            Width = 20,
+            Height = 10
+        };
+        view.Add (subview);
+
+        // Assuming the calculation is done after layout
+        int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
+        int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
+        int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
+        int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
+
+        Assert.Equal (expectedWidth, calculatedWidth);
+        Assert.Equal (expectedHeight, calculatedHeight);
+
+        Assert.Equal (0, calculatedX);
+        Assert.Equal (0, calculatedY);
+
+        view.BeginInit ();
+        view.EndInit ();
+
+        // subview should be centered in the parent view + 1
+        Assert.Equal ((view.Viewport.Width - subview.Frame.Width) / 2, subview.Frame.X);
+        Assert.Equal ((view.Viewport.Height - subview.Frame.Height) / 2, subview.Frame.Y);
+    }
+
+    [Theory]
+    [InlineData (0, 0, 0, 0, 0, 0)]
+    [InlineData (0, 19, 0, 9, 19, 9)]
+    [InlineData (0, 18, 0, 8, 18, 8)]
+    [InlineData (0, 20, 0, 10, 20, 10)]
+    [InlineData (0, 21, 0, 11, 21, 11)]
+    [InlineData (1, 21, 1, 11, 21, 11)]
+    [InlineData (21, 21, 11, 11, 21, 11)]
+    public void With_Subview_Using_PosCenter_Combine (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
+    {
+        var view = new View
+        {
+            Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
+            Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
+        };
+
+        var subview = new View
+        {
+            X = Pos.Center () + 1,
+            Y = 1 + Pos.Center (),
+            Width = 20,
+            Height = 10
+        };
+        view.Add (subview);
+
+        // Assuming the calculation is done after layout
+        int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
+        int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
+        int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
+        int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
+
+        Assert.Equal (expectedWidth, calculatedWidth);
+        Assert.Equal (expectedHeight, calculatedHeight);
+
+        Assert.Equal (0, calculatedX);
+        Assert.Equal (0, calculatedY);
+
+        view.BeginInit ();
+        view.EndInit ();
+
+        // subview should be centered in the parent view + 1
+        Assert.Equal ((view.Viewport.Width - subview.Frame.Width) / 2 + 1, subview.Frame.X);
+        Assert.Equal ((view.Viewport.Height - subview.Frame.Height) / 2 + 1, subview.Frame.Y);
+    }
+
+    #endregion PosCenter
+
+    #region PosView
+
+    // TODO: Need PosView tests
+
+    [Fact]
+    public void With_Subview_Using_PosView ()
+    {
+        var view = new View ()
+        {
+            Width = Dim.Auto (),
+            Height = Dim.Auto (),
+        };
+        var subview1 = new View { X = 1, Y = 2, Width = 1, Height = 2 };
+        var subview2 = new View { X = Pos.Top (subview1), Y = Pos.Bottom (subview1), Width = 1, Height = 2 };
+        view.Add (subview1, subview2);
+
+        view.SetRelativeLayout (new (100, 100));
+
+        // subview1.X + subview1.Width + subview2.Width
+        Assert.Equal (1 + 1 + 1, view.Frame.Width);
+        // subview1.Y + subview1.Height + subview2.Height
+        Assert.Equal (2 + 2 + 2, view.Frame.Height);
+    }
+
+    #endregion PosView
+
+    #region PosAnchorEnd
+
+    [Theory]
+    [InlineData (0, 0, 0, 0, 0, 0)]
+    [InlineData (0, 19, 0, 9, 19, 9)]
+    [InlineData (0, 18, 0, 8, 18, 8)]
+    [InlineData (0, 20, 0, 10, 20, 10)]
+    [InlineData (0, 21, 0, 11, 20, 10)]
+    [InlineData (1, 21, 1, 11, 20, 10)]
+    [InlineData (21, 21, 11, 11, 21, 11)]
+    public void With_Subview_Using_PosAnchorEnd (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
+    {
+        var view = new View
+        {
+            Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
+            Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
+        };
+
+        var subview = new View
+        {
+            X = Pos.AnchorEnd (),
+            Y = Pos.AnchorEnd (),
+            Width = 20,
+            Height = 10
+        };
+        view.Add (subview);
+
+        // Assuming the calculation is done after layout
+        int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
+        int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
+        int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
+        int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
+
+        Assert.Equal (expectedWidth, calculatedWidth);
+        Assert.Equal (expectedHeight, calculatedHeight);
+
+        Assert.Equal (0, calculatedX);
+        Assert.Equal (0, calculatedY);
+
+        view.BeginInit ();
+        view.EndInit ();
+
+        // subview should be at the end of the view
+        Assert.Equal (view.Viewport.Width - subview.Frame.Width, subview.Frame.X);
+        Assert.Equal (view.Viewport.Height - subview.Frame.Height, subview.Frame.Y);
+    }
+
+    #endregion PosAnchorEnd
+
+    #region PosFunc
+
+    [Fact]
+    public void With_Subview_Using_PosFunc ()
+    {
+        var view = new View ()
+        {
+            Width = Dim.Auto (),
+            Height = Dim.Auto (),
+        };
+        var subview = new View { X = Pos.Func (() => 20), Y = Pos.Func (() => 25) };
+        view.Add (subview);
+
+        view.SetRelativeLayout (new (100, 100));
+
+        Assert.Equal (20, view.Frame.Width);
+        Assert.Equal (25, view.Frame.Height);
+    }
+
+    #endregion PosFunc
+
+    #region PosCombine
+
+    // TODO: Need more PosCombine tests
+
+    #endregion PosCombine
+}

文件差異過大導致無法顯示
+ 142 - 827
UnitTests/View/Layout/Dim.AutoTests.cs


部分文件因文件數量過多而無法顯示