Преглед изворни кода

Fixed bug in SetViewport; added unit tests

Tig пре 1 година
родитељ
комит
2203f0b164

+ 14 - 13
Terminal.Gui/View/ViewContent.cs

@@ -319,6 +319,15 @@ public partial class View
 
         void ApplySettings (ref Rectangle newViewport)
         {
+            if (!ViewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth))
+            {
+                if (newViewport.X >= ContentSize.Width)
+                {
+                    newViewport.X = ContentSize.Width - 1;
+                }
+            }
+
+            // IMPORTANT: Check for negative location AFTER checking for location greater than content width
             if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeX))
             {
 
@@ -328,14 +337,15 @@ public partial class View
                 }
             }
 
-            if (!ViewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth))
+            if (!ViewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight))
             {
-                if (newViewport.X >= ContentSize.Width)
+                if (newViewport.Y >= ContentSize.Height)
                 {
-                    newViewport.X = ContentSize.Width - 1;
+                    newViewport.Y = ContentSize.Height - 1;
                 }
             }
 
+            // IMPORTANT: Check for negative location AFTER checking for location greater than content width
             if (!ViewportSettings.HasFlag (ViewportSettings.AllowNegativeY))
             {
                 if (newViewport.Y < 0)
@@ -344,18 +354,9 @@ public partial class View
                 }
             }
 
-            if (!ViewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight))
-            {
-                if (newViewport.Y >= ContentSize.Height)
-                {
-                    newViewport.Y = ContentSize.Height - 1;
-                }
-            }
-
         }
     }
-
-
+    
     /// <summary>
     ///     Converts a <see cref="Viewport"/>-relative location to a screen-relative location.
     /// </summary>

+ 5 - 5
Terminal.Gui/Views/GraphView/Axis.cs

@@ -140,9 +140,9 @@ public class HorizontalAxis : Axis
             return;
         }
 
-        Rectangle bounds = graph.Viewport;
+        Rectangle viewport = graph.Viewport;
 
-        IEnumerable<AxisIncrementToRender> labels = GetLabels (graph, bounds);
+        IEnumerable<AxisIncrementToRender> labels = GetLabels (graph, viewport);
 
         foreach (AxisIncrementToRender label in labels)
         {
@@ -225,7 +225,7 @@ public class HorizontalAxis : Axis
         Application.Driver.AddRune (Glyphs.HLine);
     }
 
-    private IEnumerable<AxisIncrementToRender> GetLabels (GraphView graph, Rectangle bounds)
+    private IEnumerable<AxisIncrementToRender> GetLabels (GraphView graph, Rectangle viewport)
     {
         // if no labels
         if (Increment == 0)
@@ -237,7 +237,7 @@ public class HorizontalAxis : Axis
         int y = GetAxisYPosition (graph);
 
         RectangleF start = graph.ScreenToGraphSpace ((int)graph.MarginLeft, y);
-        RectangleF end = graph.ScreenToGraphSpace (bounds.Width, y);
+        RectangleF end = graph.ScreenToGraphSpace (viewport.Width, y);
 
         // don't draw labels below the minimum
         if (Minimum.HasValue)
@@ -266,7 +266,7 @@ public class HorizontalAxis : Axis
                 ;
             }
 
-            // Label or no label definetly render it
+            // Label or no label definitely render it
             yield return toRender;
 
             current.X += Increment;

+ 91 - 0
UnitTests/View/Layout/ViewportTests.cs

@@ -229,6 +229,97 @@ public class ViewportTests (ITestOutputHelper output)
         Assert.Equal (Dim.Sized (6), v.Height);
     }
 
+    [Theory]
+    [InlineData (0, 0, 10, 10, 0, 0)]
+    [InlineData (10, 0, 10, 10, 9, 0)] // 9 because without AllowGreaterThanContentWidth, the location is clamped to size - 1
+    [InlineData (0, 10, 10, 10, 0, 9)]
+    [InlineData (10, 10, 10, 10, 9, 9)]
+    public void Set_Viewport_ValidValue_UpdatesViewport (int viewWidth, int viewHeight, int viewportX, int viewportY, int expectedX, int expectedY)
+    {
+        // Arrange
+        var view = new View ()
+        {
+            Width = viewWidth,
+            Height = viewHeight,
+        };
+        var newViewport = new Rectangle (viewportX, viewportY, viewWidth, viewHeight);
+
+        // Act
+        view.Viewport = newViewport;
+
+        // Assert
+        Assert.Equal (new Rectangle(expectedX, expectedY, viewWidth, viewHeight), view.Viewport);
+    }
+
+    [Theory]
+    [InlineData (0, 0, 10, 10, 10, 10)]
+    [InlineData (10, 0, 10, 10, 10, 10)]
+    [InlineData (0, 10, 10, 10, 10, 10)]
+    [InlineData (10, 10, 10, 10, 10, 10)]
+    public void Set_Viewport_ValidValue_UpdatesViewport_AllowLocationGreaterThanContentSize (int viewWidth, int viewHeight, int viewportX, int viewportY, int expectedX, int expectedY)
+    {
+        // Arrange
+        var view = new View ()
+        {
+            Width = viewWidth,
+            Height = viewHeight,
+            ViewportSettings = ViewportSettings.AllowLocationGreaterThanContentSize
+        };
+        var newViewport = new Rectangle (viewportX, viewportY, viewWidth, viewHeight);
+
+        // Act
+        view.Viewport = newViewport;
+
+        // Assert
+        Assert.Equal (new Rectangle (expectedX, expectedY, viewWidth, viewHeight), view.Viewport);
+    }
+
+    [Fact]
+    public void Set_Viewport_ValueGreaterThanContentSize_UpdatesViewportToContentSize ()
+    {
+        // Arrange
+        var view = new View ();
+        view.ContentSize = new Size (100, 100);
+        var newViewport = new Rectangle (0, 0, 200, 200);
+        view.ViewportSettings = ViewportSettings.AllowLocationGreaterThanContentSize;
+
+        // Act
+        view.Viewport = newViewport;
+
+        // Assert
+        Assert.Equal (newViewport, view.Viewport);
+    }
+
+    [Fact]
+    public void Set_Viewport_NegativeValue_AllowedBySettings ()
+    {
+        // Arrange
+        var view = new View ();
+        var newViewport = new Rectangle (-10, -10, 100, 100);
+        view.ViewportSettings = ViewportSettings.AllowNegativeLocation;
+
+        // Act
+        view.Viewport = newViewport;
+
+        // Assert
+        Assert.Equal (newViewport, view.Viewport);
+    }
+
+    [Fact]
+    public void Set_Viewport_NegativeValue_NotAllowedBySettings ()
+    {
+        // Arrange
+        var view = new View ();
+        var newViewport = new Rectangle (-10, -10, 100, 100);
+        view.ViewportSettings = ViewportSettings.None;
+
+        // Act
+        view.Viewport = newViewport;
+
+        // Assert
+        Assert.Equal (new Rectangle (0, 0, 100, 100), view.Viewport);
+    }
+
     [Theory]
     [InlineData (0, 0, 0)]
     [InlineData (1, 0, 0)]