Fixed Visible bug and addd unit tests

This commit is contained in:
Tig Kindel
2024-02-27 08:00:59 -07:00
parent 248efc2cc2
commit cecfb71ee1
3 changed files with 109 additions and 19 deletions

View File

@@ -1376,6 +1376,13 @@ public static partial class Application
var view = View.FindDeepestView (Current, a.MouseEvent.X, a.MouseEvent.Y);
// TODO: Remove this temporary filter:
if (view is Adornment adornment)
{
view = adornment.Parent;
}
if (view is { WantContinuousButtonPressed: true })
{
WantContinuousButtonPressedView = view;

View File

@@ -545,20 +545,27 @@ public partial class View
}
}
#nullable enable
#nullable enable
/// <summary>Finds which view that belong to the <paramref name="start"/> superview at the provided location.</summary>
/// <param name="start">The superview where to look for.</param>
/// <param name="x">The column location in the superview.</param>
/// <param name="y">The row location in the superview.</param>
/// <param name="findAdornments">TODO: Remove this after unit tests are fixed</param>
/// <returns>
/// The view that was found at the <paramref name="x"/> and <paramref name="y"/> coordinates.
/// <see langword="null"/> if no view was found.
/// </returns>
// CONCURRENCY: This method is not thread-safe.
// Undefined behavior and likely program crashes are exposed by unsynchronized access to InternalSubviews.
public static View? FindDeepestView (View? start, int x, int y)
public static View? FindDeepestView (View? start, int x, int y, bool findAdornments = false)
{
if (start is null || !start.Frame.Contains (x, y))
if (start is null || !start.Visible)
{
return null;
}
if (!start.Frame.Contains (x, y))
{
return null;
}
@@ -582,7 +589,7 @@ public partial class View
}
return start;
}
#nullable restore
#nullable restore
/// <summary>Gets the <see cref="Frame"/> with a screen-relative location.</summary>
/// <returns>The location and size of the view in screen-relative coordinates.</returns>

View File

@@ -16,14 +16,14 @@ public class FindDeepestViewTests (ITestOutputHelper output)
[InlineData (2, 2)]
public void Returns_Start_If_No_SubViews (int testX, int testY)
{
var view = new View ()
var start = new View ()
{
Width = 10, Height = 10,
};
Assert.Same (view, View.FindDeepestView (view, testX, testY));
Assert.Same (start, View.FindDeepestView (start, testX, testY));
}
// Test that FindDeepestView returns null if the start view has no subviews and coords are outside the view
[Theory]
[InlineData (0, 0)]
@@ -31,13 +31,29 @@ public class FindDeepestViewTests (ITestOutputHelper output)
[InlineData (20, 20)]
public void Returns_Null_If_No_SubViews_Coords_Outside (int testX, int testY)
{
var view = new View ()
var start = new View ()
{
X = 1, Y = 2,
Width = 10, Height = 10,
};
Assert.Null(View.FindDeepestView (view, testX, testY));
Assert.Null(View.FindDeepestView (start, testX, testY));
}
[Theory]
[InlineData (0, 0)]
[InlineData (2, 1)]
[InlineData (20, 20)]
public void Returns_Null_If_Start_Not_Visible (int testX, int testY)
{
var start = new View ()
{
X = 1, Y = 2,
Width = 10, Height = 10,
Visible = false,
};
Assert.Null (View.FindDeepestView (start, testX, testY));
}
// Test that FindDeepestView returns the correct view if the start view has subviews
@@ -52,7 +68,7 @@ public class FindDeepestViewTests (ITestOutputHelper output)
[InlineData (5, 6, true)]
public void Returns_Correct_If_SubViews (int testX, int testY, bool expectedSubViewFound)
{
var view = new View ()
var start = new View ()
{
Width = 10, Height = 10,
};
@@ -62,13 +78,73 @@ public class FindDeepestViewTests (ITestOutputHelper output)
X = 1, Y = 2,
Width = 5, Height = 5,
};
view.Add (subview);
start.Add (subview);
var found = View.FindDeepestView (view, testX, testY);
var found = View.FindDeepestView (start, testX, testY);
Assert.Equal (expectedSubViewFound, found == subview);
}
[Theory]
[InlineData (0, 0, false)]
[InlineData (1, 1, false)]
[InlineData (9, 9, false)]
[InlineData (10, 10, false)]
[InlineData (6, 7, false)]
[InlineData (1, 2, false)]
[InlineData (5, 6, false)]
public void Returns_Null_If_SubView_NotVisible (int testX, int testY, bool expectedSubViewFound)
{
var start = new View ()
{
Width = 10, Height = 10,
};
var subview = new View ()
{
X = 1, Y = 2,
Width = 5, Height = 5,
Visible = false
};
start.Add (subview);
var found = View.FindDeepestView (start, testX, testY);
Assert.Equal (expectedSubViewFound, found == subview);
}
[Theory]
[InlineData (0, 0, false)]
[InlineData (1, 1, false)]
[InlineData (9, 9, false)]
[InlineData (10, 10, false)]
[InlineData (6, 7, false)]
[InlineData (1, 2, false)]
[InlineData (5, 6, false)]
public void Returns_Null_If_Not_Visible_And_SubView_Visible (int testX, int testY, bool expectedSubViewFound)
{
var start = new View ()
{
Width = 10, Height = 10,
Visible = false
};
var subview = new View ()
{
X = 1, Y = 2,
Width = 5, Height = 5,
};
start.Add (subview);
subview.Visible = true;
Assert.True (subview.Visible);
Assert.False (start.Visible);
var found = View.FindDeepestView (start, testX, testY);
Assert.Equal (expectedSubViewFound, found == subview);
}
// Test that FindDeepestView works if the start view has positive Adornments
[Theory]
[InlineData (0, 0, false)]
@@ -84,20 +160,20 @@ public class FindDeepestViewTests (ITestOutputHelper output)
[InlineData (6, 7, true)]
public void Returns_Correct_If_Start_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
{
var view = new View ()
var start = new View ()
{
Width = 10, Height = 10,
};
view.Margin.Thickness = new Thickness (1);
start.Margin.Thickness = new Thickness (1);
var subview = new View ()
{
X = 1, Y = 2,
Width = 5, Height = 5,
};
view.Add (subview);
start.Add (subview);
var found = View.FindDeepestView (view, testX, testY);
var found = View.FindDeepestView (start, testX, testY);
Assert.Equal (expectedSubViewFound, found == subview);
}
@@ -117,7 +193,7 @@ public class FindDeepestViewTests (ITestOutputHelper output)
[InlineData (2, 3, true)]
public void Returns_Correct_If_SubView_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
{
var view = new View ()
var start = new View ()
{
Width = 10, Height = 10,
};
@@ -128,9 +204,9 @@ public class FindDeepestViewTests (ITestOutputHelper output)
Width = 5, Height = 5,
};
subview.Margin.Thickness = new Thickness (1);
view.Add (subview);
start.Add (subview);
var found = View.FindDeepestView (view, testX, testY);
var found = View.FindDeepestView (start, testX, testY);
Assert.Equal (expectedSubViewFound, found == subview);
}