浏览代码

Made View.Move and View.AddRune constrain; added unit tests

Tig 1 年之前
父节点
当前提交
7767bd13fa
共有 2 个文件被更改,包括 175 次插入64 次删除
  1. 20 13
      Terminal.Gui/View/ViewDrawing.cs
  2. 155 51
      UnitTests/View/DrawTests.cs

+ 20 - 13
Terminal.Gui/View/ViewDrawing.cs

@@ -61,27 +61,25 @@ public partial class View
     public virtual bool SuperViewRendersLineCanvas { get; set; } = false;
     public virtual bool SuperViewRendersLineCanvas { get; set; } = false;
 
 
     /// <summary>Draws the specified character in the specified viewport-relative column and row of the View.</summary>
     /// <summary>Draws the specified character in the specified viewport-relative column and row of the View.</summary>
+    /// <para>
+    ///     If the provided coordinates are outside the visible content area, this method does nothing.
+    /// </para>
     /// <remarks>
     /// <remarks>
     ///     The top-left corner of the visible content area is <c>ViewPort.Location</c>.
     ///     The top-left corner of the visible content area is <c>ViewPort.Location</c>.
     /// </remarks>
     /// </remarks>
     /// <param name="col">Column (viewport-relative).</param>
     /// <param name="col">Column (viewport-relative).</param>
     /// <param name="row">Row (viewport-relative).</param>
     /// <param name="row">Row (viewport-relative).</param>
-    /// <param name="ch">Ch.</param>
-    public void AddRune (int col, int row, Rune ch)
+    /// <param name="rune">The Rune.</param>
+    public void AddRune (int col, int row, Rune rune)
     {
     {
-        if (row < 0 || col < 0)
-        {
-            return;
-        }
-
-        // BUGBUG: This should be Viewport.Size
-        if (row > _frame.Height - 1 || col > _frame.Width - 1)
+        if (row < 0 || col < 0 || row >= Viewport.Height || col >= Viewport.Width)
         {
         {
+            // TODO: Change return type to bool so callers can determine success?
             return;
             return;
         }
         }
 
 
         Move (col, row);
         Move (col, row);
-        Driver.AddRune (ch);
+        Driver.AddRune (rune);
     }
     }
 
 
     /// <summary>Clears <see cref="Viewport"/> with the normal background.</summary>
     /// <summary>Clears <see cref="Viewport"/> with the normal background.</summary>
@@ -320,9 +318,14 @@ public partial class View
         return Enabled ? cs.Normal : cs.Disabled;
         return Enabled ? cs.Normal : cs.Disabled;
     }
     }
 
 
-    /// <summary>This moves the cursor to the specified view-relative column and row in the view.</summary>
+    /// <summary>Moves the drawing cursor to the specified view-relative column and row in the view.</summary>
     /// <remarks>
     /// <remarks>
+    /// <para>
+    ///     If the provided coordinates are outside the visible content area, this method does nothing.
+    /// </para>
+    /// <para>
     ///     The top-left corner of the visible content area is <c>ViewPort.Location</c>.
     ///     The top-left corner of the visible content area is <c>ViewPort.Location</c>.
+    /// </para>
     /// </remarks>
     /// </remarks>
     /// <param name="col">Column (viewport-relative).</param>
     /// <param name="col">Column (viewport-relative).</param>
     /// <param name="row">Row (viewport-relative).</param>
     /// <param name="row">Row (viewport-relative).</param>
@@ -333,9 +336,13 @@ public partial class View
             return;
             return;
         }
         }
 
 
-        Rectangle screen = ViewportToScreen (new (col, row, 0, 0));
+        if (col < 0 || row < 0 || col >= Viewport.Size.Width || row >= Viewport.Size.Height)
+        {
+            // TODO: Change return type to bool so callers can determine success?
+            return;
+        }
 
 
-        // TODO: Clamp this to stay within the View's Viewport
+        Rectangle screen = ViewportToScreen (new (col, row, 0, 0));
         Driver?.Move (screen.X, screen.Y);
         Driver?.Move (screen.X, screen.Y);
     }
     }
 
 

+ 155 - 51
UnitTests/View/DrawTests.cs

@@ -4,18 +4,70 @@ using Xunit.Abstractions;
 
 
 namespace Terminal.Gui.ViewTests;
 namespace Terminal.Gui.ViewTests;
 
 
-[Trait("Category","Output")]
-public class DrawTests
+[Trait ("Category", "Output")]
+public class DrawTests (ITestOutputHelper output)
 {
 {
-    private readonly ITestOutputHelper _output;
-    public DrawTests (ITestOutputHelper output) { _output = output; }
+    [Fact]
+    [SetupFakeDriver]
+    public void Move_Is_Constrained_To_Viewport ()
+    {
+        var view = new View
+        {
+            X = 1,
+            Y = 1,
+            Width = 3, Height = 3
+        };
+        view.Margin.Thickness = new Thickness (1);
+
+        // Only valid location w/in Viewport is 0, 0 (view) - 2, 2 (screen)
+
+        view.Move (0, 0);
+        Assert.Equal(new Point(2, 2), new Point (Application.Driver.Col, Application.Driver.Row));
+
+        view.Move (-1, -1);
+        Assert.Equal (new Point (2, 2), new Point (Application.Driver.Col, Application.Driver.Row));
+
+        view.Move (1, 1);
+        Assert.Equal (new Point (2, 2), new Point (Application.Driver.Col, Application.Driver.Row));
+    }
+
+    [Fact]
+    [SetupFakeDriver]
+    public void AddRune_Is_Constrained_To_Viewport ()
+    {
+        var view = new View
+        {
+            X = 1,
+            Y = 1,
+            Width = 3, Height = 3
+        };
+        view.Margin.Thickness = new Thickness (1);
+        View.Diagnostics = ViewDiagnosticFlags.Padding;
+        view.BeginInit();
+        view.EndInit();
+        view.Draw();
+
+        // Only valid location w/in Viewport is 0, 0 (view) - 2, 2 (screen)
+        Assert.Equal ((Rune)' ', Application.Driver.Contents [2, 2].Rune);
+
+        view.AddRune(0, 0, Rune.ReplacementChar);
+        Assert.Equal (Rune.ReplacementChar, Application.Driver.Contents [2, 2].Rune);
+
+        view.AddRune (-1, -1, Rune.ReplacementChar);
+        Assert.Equal ((Rune)'M', Application.Driver.Contents [1, 1].Rune);
+
+        view.AddRune (1, 1, Rune.ReplacementChar);
+        Assert.Equal ((Rune)'M', Application.Driver.Contents [3, 3].Rune);
+
+        View.Diagnostics = ViewDiagnosticFlags.Off;
+    }
 
 
     [Theory]
     [Theory]
     [InlineData (0, 0, 1, 1)]
     [InlineData (0, 0, 1, 1)]
     [InlineData (0, 0, 2, 2)]
     [InlineData (0, 0, 2, 2)]
     [InlineData (-1, -1, 2, 2)]
     [InlineData (-1, -1, 2, 2)]
     [SetupFakeDriver]
     [SetupFakeDriver]
-    public void Clear_Viewport_Clears_Only_Bounds (int x, int y, int width, int height)
+    public void Clear_Clears_Only_Viewport (int x, int y, int width, int height)
     {
     {
         var superView = new View { Width = Dim.Fill (), Height = Dim.Fill () };
         var superView = new View { Width = Dim.Fill (), Height = Dim.Fill () };
 
 
@@ -32,28 +84,80 @@ public class DrawTests
         superView.LayoutSubviews ();
         superView.LayoutSubviews ();
 
 
         superView.Draw ();
         superView.Draw ();
+        TestHelpers.AssertDriverContentsWithFrameAre (
+                                                      @"
+ ┌─┐
+ │X│
+ └─┘",
+                                                      output);
 
 
+        Rectangle toClear = new (x, y, width, height);
+        view.Clear (toClear);
+        TestHelpers.AssertDriverContentsWithFrameAre (
+                                                      @"
+ ┌─┐
+ │ │
+ └─┘",
+                                                      output);
+        // Now try to clear beyond Viewport (invalid)
+        superView.SetNeedsDisplay ();
+        superView.Draw ();
         TestHelpers.AssertDriverContentsWithFrameAre (
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
                                                       @"
  ┌─┐
  ┌─┐
  │X│
  │X│
  └─┘",
  └─┘",
-                                                      _output);
+                                                      output);
+        toClear = new (-width, -height, width, height);
+        view.Clear (toClear);
+        TestHelpers.AssertDriverContentsWithFrameAre (
+                                                      @"
+ ┌─┐
+ │X│
+ └─┘",
+                                                      output);
 
 
-        Rectangle boundsToClear = new (x, y, width, height);
-        view.Clear (boundsToClear);
+        // Now try to clear beyond Viewport (valid)
+        superView.SetNeedsDisplay ();
+        superView.Draw ();
+        TestHelpers.AssertDriverContentsWithFrameAre (
+                                                      @"
+ ┌─┐
+ │X│
+ └─┘",
+                                                      output);
+        toClear = new (-1, -1, width + 1, height + 1);
+        view.Clear (toClear);
         TestHelpers.AssertDriverContentsWithFrameAre (
         TestHelpers.AssertDriverContentsWithFrameAre (
                                                       @"
                                                       @"
  ┌─┐
  ┌─┐
  │ │
  │ │
  └─┘",
  └─┘",
-                                                      _output);
+                                                      output);
 
 
+        // Now clear too much size
+        superView.SetNeedsDisplay ();
+        superView.Draw ();
+        TestHelpers.AssertDriverContentsWithFrameAre (
+                                                      @"
+ ┌─┐
+ │X│
+ └─┘",
+                                                      output);
+        toClear = new (0, 0, width * 2, height * 2);
+        view.Clear (toClear);
+        TestHelpers.AssertDriverContentsWithFrameAre (
+                                                      @"
+ ┌─┐
+ │ │
+ └─┘",
+                                                      output);
     }
     }
 
 
+
     [Fact]
     [Fact]
     [AutoInitShutdown]
     [AutoInitShutdown]
-    [Trait("Category","Unicode")]
+    [Trait ("Category", "Unicode")]
     public void CJK_Compatibility_Ideographs_ConsoleWidth_ColumnWidth_Equal_Two ()
     public void CJK_Compatibility_Ideographs_ConsoleWidth_ColumnWidth_Equal_Two ()
     {
     {
         const string us = "\U0000f900";
         const string us = "\U0000f900";
@@ -83,9 +187,9 @@ public class DrawTests
                                       │豈      │
                                       │豈      │
                                       └────────┘
                                       └────────┘
                                       """;
                                       """;
-        TestHelpers.AssertDriverContentsWithFrameAre (expectedOutput, _output);
+        TestHelpers.AssertDriverContentsWithFrameAre (expectedOutput, output);
 
 
-        TestHelpers.AssertDriverContentsAre (expectedOutput, _output);
+        TestHelpers.AssertDriverContentsAre (expectedOutput, output);
 
 
         Attribute [] expectedColors =
         Attribute [] expectedColors =
         {
         {
@@ -115,7 +219,7 @@ public class DrawTests
     // TODO: Refactor this test to not depend on TextView etc... Make it as primitive as possible
     // TODO: Refactor this test to not depend on TextView etc... Make it as primitive as possible
     [Fact]
     [Fact]
     [AutoInitShutdown]
     [AutoInitShutdown]
-    [Trait("Category","Unicode")]
+    [Trait ("Category", "Unicode")]
     public void Clipping_AddRune_Left_Or_Right_Replace_Previous_Or_Next_Wide_Rune_With_Space ()
     public void Clipping_AddRune_Left_Or_Right_Replace_Previous_Or_Next_Wide_Rune_With_Space ()
     {
     {
         var tv = new TextView
         var tv = new TextView
@@ -161,13 +265,13 @@ public class DrawTests
                                       └────────────────────────────┘
                                       └────────────────────────────┘
                                       """;
                                       """;
 
 
-        Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expectedOutput, _output);
+        Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expectedOutput, output);
         Assert.Equal (new Rectangle (0, 0, 30, 10), pos);
         Assert.Equal (new Rectangle (0, 0, 30, 10), pos);
     }
     }
 
 
     [Fact]
     [Fact]
     [AutoInitShutdown]
     [AutoInitShutdown]
-    [Trait("Category","Output")]
+    [Trait ("Category", "Output")]
     public void Colors_On_TextAlignment_Right_And_Bottom ()
     public void Colors_On_TextAlignment_Right_And_Bottom ()
     {
     {
         var viewRight = new View
         var viewRight = new View
@@ -206,7 +310,7 @@ public class DrawTests
                                                       s     
                                                       s     
                                                       t     
                                                       t     
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
 
 
         TestHelpers.AssertDriverAttributesAre (
         TestHelpers.AssertDriverAttributesAre (
@@ -227,14 +331,14 @@ public class DrawTests
 
 
     [Fact]
     [Fact]
     [SetupFakeDriver]
     [SetupFakeDriver]
-    public void Draw_Minimum_Full_Border_With_Empty_Bounds ()
+    public void Draw_Minimum_Full_Border_With_Empty_Viewport ()
     {
     {
         var view = new View { Width = 2, Height = 2, BorderStyle = LineStyle.Single };
         var view = new View { Width = 2, Height = 2, BorderStyle = LineStyle.Single };
         view.BeginInit ();
         view.BeginInit ();
         view.EndInit ();
         view.EndInit ();
         view.SetRelativeLayout (Application.Driver.Viewport);
         view.SetRelativeLayout (Application.Driver.Viewport);
 
 
-        Assert.Equal (new (0,0,2,2), view.Frame);
+        Assert.Equal (new (0, 0, 2, 2), view.Frame);
         Assert.Equal (Rectangle.Empty, view.Viewport);
         Assert.Equal (Rectangle.Empty, view.Viewport);
 
 
         view.Draw ();
         view.Draw ();
@@ -245,7 +349,7 @@ public class DrawTests
                                                       ┌┐
                                                       ┌┐
                                                       └┘
                                                       └┘
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
     }
     }
 
 
@@ -259,12 +363,12 @@ public class DrawTests
         view.EndInit ();
         view.EndInit ();
         view.SetRelativeLayout (Application.Driver.Viewport);
         view.SetRelativeLayout (Application.Driver.Viewport);
 
 
-        Assert.Equal (new (0,0,2,1), view.Frame);
+        Assert.Equal (new (0, 0, 2, 1), view.Frame);
         Assert.Equal (Rectangle.Empty, view.Viewport);
         Assert.Equal (Rectangle.Empty, view.Viewport);
 
 
         view.Draw ();
         view.Draw ();
 
 
-        TestHelpers.AssertDriverContentsWithFrameAre (string.Empty, _output);
+        TestHelpers.AssertDriverContentsWithFrameAre (string.Empty, output);
     }
     }
 
 
     [Fact]
     [Fact]
@@ -277,7 +381,7 @@ public class DrawTests
         view.EndInit ();
         view.EndInit ();
         view.SetRelativeLayout (Application.Driver.Viewport);
         view.SetRelativeLayout (Application.Driver.Viewport);
 
 
-        Assert.Equal (new (0,0,1,2), view.Frame);
+        Assert.Equal (new (0, 0, 1, 2), view.Frame);
         Assert.Equal (Rectangle.Empty, view.Viewport);
         Assert.Equal (Rectangle.Empty, view.Viewport);
 
 
         view.Draw ();
         view.Draw ();
@@ -288,7 +392,7 @@ public class DrawTests
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
     }
     }
 
 
@@ -302,7 +406,7 @@ public class DrawTests
         view.EndInit ();
         view.EndInit ();
         view.SetRelativeLayout (Application.Driver.Viewport);
         view.SetRelativeLayout (Application.Driver.Viewport);
 
 
-        Assert.Equal (new (0,0,1,2), view.Frame);
+        Assert.Equal (new (0, 0, 1, 2), view.Frame);
         Assert.Equal (Rectangle.Empty, view.Viewport);
         Assert.Equal (Rectangle.Empty, view.Viewport);
 
 
         view.Draw ();
         view.Draw ();
@@ -313,7 +417,7 @@ public class DrawTests
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
     }
     }
 
 
@@ -328,7 +432,7 @@ public class DrawTests
         view.EndInit ();
         view.EndInit ();
         view.SetRelativeLayout (Application.Driver.Viewport);
         view.SetRelativeLayout (Application.Driver.Viewport);
 
 
-        Assert.Equal (new (0,0,2,1), view.Frame);
+        Assert.Equal (new (0, 0, 2, 1), view.Frame);
         Assert.Equal (Rectangle.Empty, view.Viewport);
         Assert.Equal (Rectangle.Empty, view.Viewport);
 
 
         view.Draw ();
         view.Draw ();
@@ -339,7 +443,7 @@ public class DrawTests
 
 
                                                       ┌┐
                                                       ┌┐
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
     }
     }
 
 
@@ -416,7 +520,7 @@ public class DrawTests
                                                        3V
                                                        3V
                                                        4i
                                                        4i
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
 
 
         content.X = -1;
         content.X = -1;
@@ -431,12 +535,12 @@ public class DrawTests
                                                        V
                                                        V
                                                        i
                                                        i
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
 
 
         content.X = -2;
         content.X = -2;
         Application.Refresh ();
         Application.Refresh ();
-        TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
+        TestHelpers.AssertDriverContentsWithFrameAre (@"", output);
 
 
         content.X = 0;
         content.X = 0;
         content.Y = -1;
         content.Y = -1;
@@ -451,7 +555,7 @@ public class DrawTests
                                                        4i
                                                        4i
                                                        5e
                                                        5e
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
 
 
         content.Y = -6;
         content.Y = -6;
@@ -466,7 +570,7 @@ public class DrawTests
                                                        9 
                                                        9 
                                                        0 
                                                        0 
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
 
 
         content.Y = -19;
         content.Y = -19;
@@ -477,17 +581,17 @@ public class DrawTests
 
 
                                                        9
                                                        9
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
 
 
         content.Y = -20;
         content.Y = -20;
         Application.Refresh ();
         Application.Refresh ();
-        TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
+        TestHelpers.AssertDriverContentsWithFrameAre ("", output);
 
 
         content.X = -2;
         content.X = -2;
         content.Y = 0;
         content.Y = 0;
         Application.Refresh ();
         Application.Refresh ();
-        TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
+        TestHelpers.AssertDriverContentsWithFrameAre ("", output);
     }
     }
 
 
     [Fact]
     [Fact]
@@ -533,7 +637,7 @@ public class DrawTests
                                                        01234
                                                        01234
                                                        subVi
                                                        subVi
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
 
 
         content.X = -1;
         content.X = -1;
@@ -545,7 +649,7 @@ public class DrawTests
                                                        12345
                                                        12345
                                                        ubVie
                                                        ubVie
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
 
 
         content.Y = -1;
         content.Y = -1;
@@ -556,17 +660,17 @@ public class DrawTests
 
 
                                                        ubVie
                                                        ubVie
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
 
 
         content.Y = -2;
         content.Y = -2;
         Application.Refresh ();
         Application.Refresh ();
-        TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
+        TestHelpers.AssertDriverContentsWithFrameAre ("", output);
 
 
         content.X = -20;
         content.X = -20;
         content.Y = 0;
         content.Y = 0;
         Application.Refresh ();
         Application.Refresh ();
-        TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
+        TestHelpers.AssertDriverContentsWithFrameAre ("", output);
     }
     }
 
 
     [Fact]
     [Fact]
@@ -618,7 +722,7 @@ public class DrawTests
                                                        3V
                                                        3V
                                                        4i
                                                        4i
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
 
 
         content.X = -1;
         content.X = -1;
@@ -633,12 +737,12 @@ public class DrawTests
                                                        V
                                                        V
                                                        i
                                                        i
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
 
 
         content.X = -2;
         content.X = -2;
         Application.Refresh ();
         Application.Refresh ();
-        TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
+        TestHelpers.AssertDriverContentsWithFrameAre (@"", output);
 
 
         content.X = 0;
         content.X = 0;
         content.Y = -1;
         content.Y = -1;
@@ -653,7 +757,7 @@ public class DrawTests
                                                        4i
                                                        4i
                                                        5e
                                                        5e
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
 
 
         content.Y = -6;
         content.Y = -6;
@@ -668,7 +772,7 @@ public class DrawTests
                                                        9 
                                                        9 
                                                        0 
                                                        0 
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
 
 
         content.Y = -19;
         content.Y = -19;
@@ -679,17 +783,17 @@ public class DrawTests
 
 
                                                        9
                                                        9
                                                       """,
                                                       """,
-                                                      _output
+                                                      output
                                                      );
                                                      );
 
 
         content.Y = -20;
         content.Y = -20;
         Application.Refresh ();
         Application.Refresh ();
-        TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
+        TestHelpers.AssertDriverContentsWithFrameAre ("", output);
 
 
         content.X = -2;
         content.X = -2;
         content.Y = 0;
         content.Y = 0;
         Application.Refresh ();
         Application.Refresh ();
-        TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
+        TestHelpers.AssertDriverContentsWithFrameAre ("", output);
     }
     }
 
 
     [Theory]
     [Theory]
@@ -701,7 +805,7 @@ public class DrawTests
         var view = new View { Width = 10, Height = 1 };
         var view = new View { Width = 10, Height = 1 };
         view.DrawHotString (expected, Attribute.Default, Attribute.Default);
         view.DrawHotString (expected, Attribute.Default, Attribute.Default);
 
 
-        TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
+        TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
     }
     }
 
 
     // TODO: The tests below that use Label should use View instead.
     // TODO: The tests below that use Label should use View instead.
@@ -736,9 +840,9 @@ public class DrawTests
             │𝔹       │
             │𝔹       │
             └────────┘
             └────────┘
             """;
             """;
-        TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
+        TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
 
 
-        TestHelpers.AssertDriverContentsAre (expected, _output);
+        TestHelpers.AssertDriverContentsAre (expected, output);
 
 
         Attribute [] expectedColors =
         Attribute [] expectedColors =
         {
         {