浏览代码

Found and fixed bug in non-recursive FindDeepestView; added reltaed unit test

Tig 1 年之前
父节点
当前提交
e8640e9d92
共有 3 个文件被更改,包括 130 次插入3 次删除
  1. 2 2
      Terminal.Gui/View/Layout/ViewLayout.cs
  2. 1 1
      UICatalog/Scenarios/CharacterMap.cs
  3. 127 0
      UnitTests/View/FindDeepestViewTests.cs

+ 2 - 2
Terminal.Gui/View/Layout/ViewLayout.cs

@@ -642,8 +642,8 @@ public partial class View
                     if (nextStart.Visible && nextStart.Contains (startOffsetX + start.Viewport.X, startOffsetY + start.Viewport.Y))
                     {
                         start = nextStart;
-                        x = startOffsetX + start.Viewport.X;
-                        y = startOffsetY + start.Viewport.Y;
+                        x = startOffsetX;
+                        y = startOffsetY;
                         break;
                     }
 

+ 1 - 1
UICatalog/Scenarios/CharacterMap.cs

@@ -1,4 +1,4 @@
-#define OTHER_CONTROLS
+//#define OTHER_CONTROLS
 
 using System;
 using System.Collections.Generic;

+ 127 - 0
UnitTests/View/FindDeepestViewTests.cs

@@ -271,6 +271,40 @@ public class FindDeepestViewTests (ITestOutputHelper output)
         Assert.Equal (expectedSubViewFound, found == subview);
     }
 
+    [Theory]
+    [InlineData (0, 0, false)]
+    [InlineData (1, 1, false)]
+    [InlineData (9, 9, true)]
+    [InlineData (10, 10, false)]
+    [InlineData (7, 8, false)]
+    [InlineData (1, 2, false)]
+    [InlineData (2, 3, false)]
+    [InlineData (5, 6, false)]
+    [InlineData (2, 3, false)]
+    [InlineData (6, 7, false)]
+    public void Returns_Correct_If_Start_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
+    {
+        var start = new View ()
+        {
+            Width = 10, Height = 10,
+        };
+        start.Padding.Thickness = new Thickness (1);
+
+        var subview = new View ()
+        {
+            X = Pos.AnchorEnd(1), Y = Pos.AnchorEnd(1),
+            Width = 1, Height = 1,
+        };
+        start.Padding.Add (subview);
+        start.BeginInit();
+        start.EndInit();
+
+        var found = View.FindDeepestView (start, testX, testY);
+
+        Assert.Equal (expectedSubViewFound, found == subview);
+    }
+
+
     [Theory]
     [InlineData (0, 0, typeof (Margin))]
     [InlineData (9, 9, typeof (Margin))]
@@ -336,6 +370,99 @@ public class FindDeepestViewTests (ITestOutputHelper output)
         Assert.Equal (expectedSubViewFound, found == subview);
     }
 
+    [Theory]
+    [InlineData (0, 0, false)]
+    [InlineData (1, 1, false)]
+    [InlineData (9, 9, false)]
+    [InlineData (10, 10, false)]
+    [InlineData (7, 8, false)]
+    [InlineData (6, 7, false)]
+    [InlineData (1, 2, false)]
+    [InlineData (5, 6, false)]
+    [InlineData (6, 5, false)]
+    [InlineData (5, 5, true)]
+    public void Returns_Correct_If_SubView_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
+    {
+        var start = new View ()
+        {
+            Width = 10, Height = 10,
+        };
+
+        // A subview with + Padding
+        var subview = new View ()
+        {
+            X = 1, Y = 1,
+            Width = 5, Height = 5,
+        };
+        subview.Padding.Thickness = new (1);
+
+        // This subview will be at the bottom-right-corner of subview
+        // So screen-relative location will be X + Width - 1 = 5
+        var paddingSubview = new View ()
+        {
+            X = Pos.AnchorEnd (1),
+            Y = Pos.AnchorEnd (1),
+            Width = 1,
+            Height = 1,
+        };
+        subview.Padding.Add (paddingSubview);
+        start.Add (subview);
+        start.BeginInit();
+        start.EndInit();
+
+        var found = View.FindDeepestView (start, testX, testY);
+
+        Assert.Equal (expectedSubViewFound, found == paddingSubview);
+    }
+
+    [Theory]
+    [InlineData (0, 0, false)]
+    [InlineData (1, 1, false)]
+    [InlineData (9, 9, false)]
+    [InlineData (10, 10, false)]
+    [InlineData (7, 8, false)]
+    [InlineData (6, 7, false)]
+    [InlineData (1, 2, false)]
+    [InlineData (5, 6, false)]
+    [InlineData (6, 5, false)]
+    [InlineData (5, 5, true)]
+    public void Returns_Correct_If_SubView_Is_Scrolled_And_Has_Adornment_WithSubview (int testX, int testY, bool expectedSubViewFound)
+    {
+        var start = new View ()
+        {
+            Width = 10, Height = 10,
+        };
+
+        // A subview with + Padding
+        var subview = new View ()
+        {
+            X = 1, Y = 1,
+            Width = 5, Height = 5,
+        };
+        subview.Padding.Thickness = new (1);
+
+        // Scroll the subview
+        subview.ContentSize = new Size (10, 10);
+        subview.Viewport = subview.Viewport with { Location = new (1, 1) };
+
+        // This subview will be at the bottom-right-corner of subview
+        // So screen-relative location will be X + Width - 1 = 5
+        var paddingSubview = new View ()
+        {
+            X = Pos.AnchorEnd (1),
+            Y = Pos.AnchorEnd (1),
+            Width = 1,
+            Height = 1,
+        };
+        subview.Padding.Add (paddingSubview);
+        start.Add (subview);
+        start.BeginInit ();
+        start.EndInit ();
+
+        var found = View.FindDeepestView (start, testX, testY);
+
+        Assert.Equal (expectedSubViewFound, found == paddingSubview);
+    }
     // Test that FindDeepestView works with nested subviews
     [Theory]
     [InlineData (0, 0, -1)]