123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
-
- #nullable enable
- using Xunit.Abstractions;
- namespace Terminal.Gui.ViewTests;
- /// <summary>
- /// Tests View.FindDeepestView
- /// </summary>
- /// <param name="output"></param>
- public class FindDeepestViewTests (ITestOutputHelper output)
- {
- [Theory]
- [InlineData (0, 0, 0, 0, 0, -1, -1, null)]
- [InlineData (0, 0, 0, 0, 0, 0, 0, typeof (View))]
- [InlineData (0, 0, 0, 0, 0, 1, 1, typeof (View))]
- [InlineData (0, 0, 0, 0, 0, 4, 4, typeof (View))]
- [InlineData (0, 0, 0, 0, 0, 9, 9, typeof (View))]
- [InlineData (0, 0, 0, 0, 0, 10, 10, null)]
- [InlineData (1, 1, 0, 0, 0, -1, -1, null)]
- [InlineData (1, 1, 0, 0, 0, 0, 0, null)]
- [InlineData (1, 1, 0, 0, 0, 1, 1, typeof (View))]
- [InlineData (1, 1, 0, 0, 0, 4, 4, typeof (View))]
- [InlineData (1, 1, 0, 0, 0, 9, 9, typeof (View))]
- [InlineData (1, 1, 0, 0, 0, 10, 10, typeof (View))]
- [InlineData (0, 0, 1, 0, 0, -1, -1, null)]
- [InlineData (0, 0, 1, 0, 0, 0, 0, typeof (Margin))]
- [InlineData (0, 0, 1, 0, 0, 1, 1, typeof (View))]
- [InlineData (0, 0, 1, 0, 0, 4, 4, typeof (View))]
- [InlineData (0, 0, 1, 0, 0, 9, 9, typeof (Margin))]
- [InlineData (0, 0, 1, 0, 0, 10, 10, null)]
- [InlineData (0, 0, 1, 1, 0, -1, -1, null)]
- [InlineData (0, 0, 1, 1, 0, 0, 0, typeof (Margin))]
- [InlineData (0, 0, 1, 1, 0, 1, 1, typeof (Border))]
- [InlineData (0, 0, 1, 1, 0, 4, 4, typeof (View))]
- [InlineData (0, 0, 1, 1, 0, 9, 9, typeof (Margin))]
- [InlineData (0, 0, 1, 1, 0, 10, 10, null)]
- [InlineData (0, 0, 1, 1, 1, -1, -1, null)]
- [InlineData (0, 0, 1, 1, 1, 0, 0, typeof (Margin))]
- [InlineData (0, 0, 1, 1, 1, 1, 1, typeof (Border))]
- [InlineData (0, 0, 1, 1, 1, 2, 2, typeof (Padding))]
- [InlineData (0, 0, 1, 1, 1, 4, 4, typeof (View))]
- [InlineData (0, 0, 1, 1, 1, 9, 9, typeof (Margin))]
- [InlineData (0, 0, 1, 1, 1, 10, 10, null)]
- [InlineData (1, 1, 1, 0, 0, -1, -1, null)]
- [InlineData (1, 1, 1, 0, 0, 0, 0, null)]
- [InlineData (1, 1, 1, 0, 0, 1, 1, typeof (Margin))]
- [InlineData (1, 1, 1, 0, 0, 4, 4, typeof (View))]
- [InlineData (1, 1, 1, 0, 0, 9, 9, typeof (View))]
- [InlineData (1, 1, 1, 0, 0, 10, 10, typeof (Margin))]
- [InlineData (1, 1, 1, 1, 0, -1, -1, null)]
- [InlineData (1, 1, 1, 1, 0, 0, 0, null)]
- [InlineData (1, 1, 1, 1, 0, 1, 1, typeof (Margin))]
- [InlineData (1, 1, 1, 1, 0, 4, 4, typeof (View))]
- [InlineData (1, 1, 1, 1, 0, 9, 9, typeof (Border))]
- [InlineData (1, 1, 1, 1, 0, 10, 10, typeof (Margin))]
- [InlineData (1, 1, 1, 1, 1, -1, -1, null)]
- [InlineData (1, 1, 1, 1, 1, 0, 0, null)]
- [InlineData (1, 1, 1, 1, 1, 1, 1, typeof (Margin))]
- [InlineData (1, 1, 1, 1, 1, 2, 2, typeof (Border))]
- [InlineData (1, 1, 1, 1, 1, 3, 3, typeof (Padding))]
- [InlineData (1, 1, 1, 1, 1, 4, 4, typeof (View))]
- [InlineData (1, 1, 1, 1, 1, 8, 8, typeof (Padding))]
- [InlineData (1, 1, 1, 1, 1, 9, 9, typeof (Border))]
- [InlineData (1, 1, 1, 1, 1, 10, 10, typeof (Margin))]
- public void Contains (int frameX, int frameY, int marginThickness, int borderThickness, int paddingThickness, int testX, int testY, Type? expectedAdornmentType)
- {
- var view = new View ()
- {
- X = frameX, Y = frameY,
- Width = 10, Height = 10,
- };
- view.Margin.Thickness = new Thickness (marginThickness);
- view.Border.Thickness = new Thickness (borderThickness);
- view.Padding.Thickness = new Thickness (paddingThickness);
- Type? containedType = null;
- if (view.Contains (testX, testY))
- {
- containedType = view.GetType ();
- }
- if (view.Margin.Contains (testX, testY))
- {
- containedType = view.Margin.GetType ();
- }
- if (view.Border.Contains (testX, testY))
- {
- containedType = view.Border.GetType ();
- }
- if (view.Padding.Contains (testX, testY))
- {
- containedType = view.Padding.GetType ();
- }
- Assert.Equal (expectedAdornmentType, containedType);
- }
- // Test that FindDeepestView returns the correct view if the start view has no subviews
- [Theory]
- [InlineData (0, 0)]
- [InlineData (1, 1)]
- [InlineData (2, 2)]
- public void Returns_Start_If_No_SubViews (int testX, int testY)
- {
- var start = new View ()
- {
- Width = 10, Height = 10,
- };
- 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)]
- [InlineData (2, 1)]
- [InlineData (20, 20)]
- public void Returns_Null_If_No_SubViews_Coords_Outside (int testX, int testY)
- {
- var start = new View ()
- {
- X = 1, Y = 2,
- Width = 10, Height = 10,
- };
- 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
- [Theory]
- [InlineData (0, 0, false)]
- [InlineData (1, 1, false)]
- [InlineData (9, 9, false)]
- [InlineData (10, 10, false)]
- [InlineData (6, 7, false)]
- [InlineData (1, 2, true)]
- [InlineData (5, 6, true)]
- public void Returns_Correct_If_SubViews (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,
- };
- 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_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)]
- [InlineData (1, 1, false)]
- [InlineData (9, 9, false)]
- [InlineData (10, 10, false)]
- [InlineData (7, 8, false)]
- [InlineData (1, 2, false)]
- [InlineData (2, 3, true)]
- [InlineData (5, 6, true)]
- [InlineData (2, 3, true)]
- [InlineData (6, 7, true)]
- public void Returns_Correct_If_Start_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
- {
- var start = new View ()
- {
- Width = 10, Height = 10,
- };
- start.Margin.Thickness = new Thickness (1);
- var subview = new View ()
- {
- X = 1, Y = 2,
- Width = 5, Height = 5,
- };
- start.Add (subview);
- var found = View.FindDeepestView (start, testX, testY);
- Assert.Equal (expectedSubViewFound, found == subview);
- }
- // Test that FindDeepestView works if the start view has offset Viewport location
- [Theory]
- [InlineData (1, 0, 0, true)]
- [InlineData (1, 1, 1, true)]
- [InlineData (1, 2, 2, false)]
- [InlineData (-1, 3, 3, true)]
- [InlineData (-1, 2, 2, true)]
- [InlineData (-1, 1, 1, false)]
- [InlineData (-1, 0, 0, false)]
- public void Returns_Correct_If_Start_Has_Offset_Viewport (int offset, int testX, int testY, bool expectedSubViewFound)
- {
- var start = new View ()
- {
- Width = 10, Height = 10,
- ViewportSettings = ViewportSettings.AllowNegativeLocation
- };
- start.Viewport = new (offset, offset, 10, 10);
- var subview = new View ()
- {
- X = 1, Y = 1,
- Width = 2, Height = 2,
- };
- 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, 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))]
- [InlineData (1, 1, typeof (Border))]
- [InlineData (8, 8, typeof (Border))]
- [InlineData (2, 2, typeof (Padding))]
- [InlineData (7, 7, typeof (Padding))]
- [InlineData (5, 5, typeof (View))]
- public void Returns_Adornment_If_Start_Has_Adornments (int testX, int testY, Type expectedAdornmentType)
- {
- var start = new View ()
- {
- Width = 10, Height = 10,
- };
- start.Margin.Thickness = new Thickness (1);
- start.Border.Thickness = new Thickness (1);
- start.Padding.Thickness = new Thickness (1);
- var subview = new View ()
- {
- X = 1, Y = 1,
- Width = 1, Height = 1,
- };
- start.Add (subview);
- var found = View.FindDeepestView (start, testX, testY);
- Assert.Equal (expectedAdornmentType, found.GetType ());
- }
- // Test that FindDeepestView works if the subview has positive Adornments
- [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 (2, 3, true)]
- [InlineData (2, 3, true)]
- public void Returns_Correct_If_SubView_Has_Adornments (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,
- };
- subview.Margin.Thickness = new Thickness (1);
- 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 (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)]
- [InlineData (9, 9, -1)]
- [InlineData (10, 10, -1)]
- [InlineData (1, 1, 0)]
- [InlineData (1, 2, 0)]
- [InlineData (2, 2, 1)]
- [InlineData (3, 3, 2)]
- [InlineData (5, 5, 2)]
- public void Returns_Correct_With_NestedSubViews (int testX, int testY, int expectedSubViewFound)
- {
- var start = new View ()
- {
- Width = 10, Height = 10
- };
- int numSubViews = 3;
- List<View> subviews = new List<View> ();
- for (int i = 0; i < numSubViews; i++)
- {
- var subview = new View ()
- {
- X = 1, Y = 1,
- Width = 5, Height = 5,
- };
- subviews.Add (subview);
- if (i > 0)
- {
- subviews [i - 1].Add (subview);
- }
- }
- start.Add (subviews [0]);
- var found = View.FindDeepestView (start, testX, testY);
- Assert.Equal (expectedSubViewFound, subviews.IndexOf (found));
- }
- }
|