| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223 |
- #nullable enable
- namespace ViewBaseTests.Layout;
- [Trait ("Category", "Layout")]
- public class GetViewsAtLocationTests
- {
- private class TestView : View
- {
- public TestView (int x, int y, int w, int h, bool visible = true)
- {
- X = x;
- Y = y;
- Width = w;
- Height = h;
- base.Visible = visible;
- }
- }
- [Fact]
- public void ReturnsEmpty_WhenRootIsNull ()
- {
- List<View?> result = View.GetViewsAtLocation (null, new (0, 0));
- Assert.Empty (result);
- }
- [Fact]
- public void ReturnsEmpty_WhenRootIsNotVisible ()
- {
- TestView root = new (0, 0, 10, 10, false);
- List<View?> result = View.GetViewsAtLocation (root, new (5, 5));
- Assert.Empty (result);
- }
- [Fact]
- public void ReturnsEmpty_WhenPointOutsideRoot ()
- {
- TestView root = new (0, 0, 10, 10);
- List<View?> result = View.GetViewsAtLocation (root, new (20, 20));
- Assert.Empty (result);
- }
- [Fact]
- public void ReturnsEmpty_WhenPointOutsideRoot_AndSubview ()
- {
- TestView root = new (0, 0, 10, 10);
- TestView sub = new (5, 5, 2, 2);
- root.Add (sub);
- List<View?> result = View.GetViewsAtLocation (root, new (20, 20));
- Assert.Empty (result);
- }
- [Fact]
- public void ReturnsRoot_WhenPointInsideRoot_NoSubviews ()
- {
- TestView root = new (0, 0, 10, 10);
- List<View?> result = View.GetViewsAtLocation (root, new (5, 5));
- Assert.Single (result);
- Assert.Equal (root, result [0]);
- }
- [Fact]
- public void ReturnsRoot_And_Subview_WhenPointInsideRootMargin ()
- {
- TestView root = new (0, 0, 10, 10);
- root.Margin!.Thickness = new (1);
- TestView sub = new (2, 2, 5, 5);
- root.Add (sub);
- List<View?> result = View.GetViewsAtLocation (root, new (3, 3));
- Assert.Equal (2, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (sub, result [1]);
- }
- [Fact]
- public void ReturnsRoot_And_Subview_Border_WhenPointInsideRootMargin ()
- {
- TestView root = new (0, 0, 10, 10);
- root.Margin!.Thickness = new (1);
- TestView sub = new (2, 2, 5, 5);
- sub.BorderStyle = LineStyle.Dotted;
- root.Add (sub);
- List<View?> result = View.GetViewsAtLocation (root, new (3, 3));
- Assert.Equal (3, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (sub, result [1]);
- Assert.Equal (sub.Border, result [2]);
- }
- [Fact]
- public void ReturnsRoot_And_Margin_WhenPointInside_With_Margin ()
- {
- TestView root = new (0, 0, 10, 10);
- root.Margin!.Thickness = new (1);
- List<View?> result = View.GetViewsAtLocation (root, new (0, 0));
- Assert.Equal (2, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (root.Margin, result [1]);
- }
- [Fact]
- public void ReturnsRoot_WhenPointOutsideSubview_With_Margin ()
- {
- TestView root = new (0, 0, 10, 10);
- root.Margin!.Thickness = new (1);
- TestView sub = new (2, 2, 5, 5);
- root.Add (sub);
- List<View?> result = View.GetViewsAtLocation (root, new (2, 2));
- Assert.Single (result);
- Assert.Equal (root, result [0]);
- result = View.GetViewsAtLocation (root, new (0, 0));
- Assert.Equal (2, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (root.Margin, result [1]);
- result = View.GetViewsAtLocation (root, new (1, 1));
- Assert.Single (result);
- Assert.Equal (root, result [0]);
- result = View.GetViewsAtLocation (root, new (8, 8));
- Assert.Single (result);
- Assert.Equal (root, result [0]);
- }
- [Fact]
- public void ReturnsRoot_And_Border_WhenPointInside_With_Border ()
- {
- TestView root = new (0, 0, 10, 10);
- root.Border!.Thickness = new (1);
- List<View?> result = View.GetViewsAtLocation (root, new (0, 0));
- Assert.Equal (2, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (root.Border, result [1]);
- }
- [Fact]
- public void ReturnsRoot_WhenPointOutsideSubview_With_Border ()
- {
- TestView root = new (0, 0, 10, 10);
- root.Border!.Thickness = new (1);
- TestView sub = new (2, 2, 5, 5);
- root.Add (sub);
- List<View?> result = View.GetViewsAtLocation (root, new (2, 2));
- Assert.Single (result);
- Assert.Equal (root, result [0]);
- result = View.GetViewsAtLocation (root, new (0, 0));
- Assert.Equal (2, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (root.Border, result [1]);
- result = View.GetViewsAtLocation (root, new (1, 1));
- Assert.Single (result);
- Assert.Equal (root, result [0]);
- result = View.GetViewsAtLocation (root, new (8, 8));
- Assert.Single (result);
- Assert.Equal (root, result [0]);
- }
- [Fact]
- public void ReturnsRoot_And_Border_WhenPointInsideRootBorder ()
- {
- TestView root = new (0, 0, 10, 10);
- root.Border!.Thickness = new (1);
- List<View?> result = View.GetViewsAtLocation (root, new (0, 0));
- Assert.Equal (2, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (root.Border, result [1]);
- }
- [Fact]
- public void ReturnsRoot_And_Padding_WhenPointInsideRootPadding ()
- {
- TestView root = new (0, 0, 10, 10);
- root.Padding!.Thickness = new (1);
- List<View?> result = View.GetViewsAtLocation (root, new (0, 0));
- Assert.Equal (2, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (root.Padding, result [1]);
- }
- [Fact]
- public void ReturnsRootAndSubview_WhenPointInsideSubview ()
- {
- TestView root = new (0, 0, 10, 10);
- TestView sub = new (2, 2, 5, 5);
- root.Add (sub);
- List<View?> result = View.GetViewsAtLocation (root, new (3, 3));
- Assert.Equal (2, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (sub, result [1]);
- }
- [Fact]
- public void ReturnsRootAndSubviewAndMargin_WhenPointInsideSubviewMargin ()
- {
- TestView root = new (0, 0, 10, 10);
- TestView sub = new (2, 2, 5, 5);
- sub.Margin!.Thickness = new (1);
- root.Add (sub);
- List<View?> result = View.GetViewsAtLocation (root, new (6, 6));
- Assert.Equal (3, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (sub, result [1]);
- Assert.Equal (sub.Margin, result [2]);
- }
- [Fact]
- public void ReturnsRootAndSubviewAndBorder_WhenPointInsideSubviewBorder ()
- {
- TestView root = new (0, 0, 10, 10);
- TestView sub = new (2, 2, 5, 5);
- sub.Border!.Thickness = new (1);
- root.Add (sub);
- List<View?> result = View.GetViewsAtLocation (root, new (2, 2));
- Assert.Equal (3, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (sub, result [1]);
- Assert.Equal (sub.Border, result [2]);
- }
- [Fact]
- public void ReturnsRootAndSubviewAndSubviewAndBorder_WhenPointInsideSubviewBorder ()
- {
- TestView root = new (2, 2, 10, 10);
- TestView sub = new (2, 2, 5, 5);
- sub.Border!.Thickness = new (1);
- root.Add (sub);
- List<View?> result = View.GetViewsAtLocation (root, new (4, 4));
- Assert.Equal (3, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (sub, result [1]);
- Assert.Equal (sub.Border, result [2]);
- }
- [Fact]
- public void ReturnsRootAndSubviewAndBorder_WhenPointInsideSubviewPadding ()
- {
- TestView root = new (0, 0, 10, 10);
- TestView sub = new (2, 2, 5, 5);
- sub.Padding!.Thickness = new (1);
- root.Add (sub);
- List<View?> result = View.GetViewsAtLocation (root, new (2, 2));
- Assert.Equal (3, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (sub, result [1]);
- Assert.Equal (sub.Padding, result [2]);
- }
- [Fact]
- public void ReturnsRootAndSubviewAndMarginAndShadowView_WhenPointInsideSubviewMargin ()
- {
- TestView root = new (0, 0, 10, 10);
- TestView sub = new (2, 2, 5, 5);
- sub.ShadowStyle = ShadowStyle.Opaque;
- root.Add (sub);
- root.Layout ();
- List<View?> result = View.GetViewsAtLocation (root, new (6, 6));
- Assert.Equal (5, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (sub, result [1]);
- Assert.Equal (sub.Margin, result [2]);
- Assert.Equal (sub.Margin!.SubViews.ElementAt (0), result [3]);
- Assert.Equal (sub.Margin!.SubViews.ElementAt (1), result [4]);
- }
- [Fact]
- public void ReturnsRootAndSubviewAndBorderAndButton_WhenPointInsideSubviewBorder ()
- {
- TestView root = new (0, 0, 10, 10);
- TestView sub = new (2, 2, 5, 5);
- sub.Border!.Thickness = new (1);
- var closeButton = new Button
- {
- NoDecorations = true,
- NoPadding = true,
- Title = "X",
- Width = 1,
- Height = 1,
- X = Pos.AnchorEnd (),
- Y = 0,
- ShadowStyle = ShadowStyle.None
- };
- sub.Border!.Add (closeButton);
- root.Add (sub);
- root.Layout ();
- List<View?> result = View.GetViewsAtLocation (root, new (6, 2));
- Assert.Equal (4, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (sub, result [1]);
- Assert.Equal (sub.Border, result [2]);
- Assert.Equal (closeButton, result [3]);
- }
- [Fact]
- public void ReturnsDeepestSubview_WhenNested ()
- {
- TestView root = new (0, 0, 20, 20);
- var sub1 = new TestView (2, 2, 16, 16);
- var sub2 = new TestView (3, 3, 10, 10);
- var sub3 = new TestView (1, 1, 5, 5);
- root.Add (sub1);
- sub1.Add (sub2);
- sub2.Add (sub3);
- // Point inside all
- List<View?> result = View.GetViewsAtLocation (root, new (7, 7));
- Assert.Equal (4, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (sub1, result [1]);
- Assert.Equal (sub2, result [2]);
- Assert.Equal (sub3, result [3]);
- }
- [Fact]
- public void ReturnsTopmostSubview_WhenOverlapping ()
- {
- TestView root = new (0, 0, 10, 10);
- var sub1 = new TestView (2, 2, 6, 6);
- var sub2 = new TestView (4, 4, 6, 6);
- root.Add (sub1);
- root.Add (sub2); // sub2 is on top
- List<View?> result = View.GetViewsAtLocation (root, new (5, 5));
- Assert.Equal (3, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (sub1, result [1]);
- Assert.Equal (sub2, result [2]);
- }
- [Fact]
- public void ReturnsTopmostSubview_WhenNotOverlapping ()
- {
- TestView root = new (0, 0, 10, 10); // under 5,5,
- var sub1 = new TestView (10, 10, 6, 6); // not under location 5,5
- var sub2 = new TestView (4, 4, 6, 6); // under 5,5,
- root.Add (sub1);
- root.Add (sub2); // sub2 is on top
- List<View?> result = View.GetViewsAtLocation (root, new (5, 5));
- Assert.Equal (2, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (sub2, result [1]);
- }
- [Fact]
- public void SkipsInvisibleSubviews ()
- {
- TestView root = new (0, 0, 10, 10);
- var sub1 = new TestView (2, 2, 6, 6, false);
- var sub2 = new TestView (4, 4, 6, 6);
- root.Add (sub1);
- root.Add (sub2);
- List<View?> result = View.GetViewsAtLocation (root, new (5, 5));
- Assert.Equal (2, result.Count);
- Assert.Equal (root, result [0]);
- Assert.Equal (sub2, result [1]);
- }
- [Fact]
- public void ReturnsRoot_WhenPointOnEdge ()
- {
- TestView root = new (0, 0, 10, 10);
- List<View?> result = View.GetViewsAtLocation (root, new (0, 0));
- Assert.Single (result);
- Assert.Equal (root, result [0]);
- }
- [Fact]
- public void ReturnsRoot_WhenPointOnBottomRightCorner ()
- {
- TestView root = new (0, 0, 10, 10);
- List<View?> result = View.GetViewsAtLocation (root, new (9, 9));
- Assert.Single (result);
- Assert.Equal (root, result [0]);
- }
- [Fact]
- public void ReturnsEmpty_WhenAllSubviewsInvisible ()
- {
- TestView root = new (0, 0, 10, 10);
- var sub1 = new TestView (2, 2, 6, 6, false);
- root.Add (sub1);
- List<View?> result = View.GetViewsAtLocation (root, new (3, 3));
- Assert.Single (result);
- Assert.Equal (root, result [0]);
- }
- [Theory]
- [InlineData (0, 0, 0, 0, 0, -1, -1, new string [] { })]
- [InlineData (0, 0, 0, 0, 0, 0, 0, new [] { "Top" })]
- [InlineData (0, 0, 0, 0, 0, 1, 1, new [] { "Top" })]
- [InlineData (0, 0, 0, 0, 0, 4, 4, new [] { "Top" })]
- [InlineData (0, 0, 0, 0, 0, 9, 9, new [] { "Top" })]
- [InlineData (0, 0, 0, 0, 0, 10, 10, new string [] { })]
- [InlineData (1, 1, 0, 0, 0, -1, -1, new string [] { })]
- [InlineData (1, 1, 0, 0, 0, 0, 0, new string [] { })]
- [InlineData (1, 1, 0, 0, 0, 1, 1, new [] { "Top" })]
- [InlineData (1, 1, 0, 0, 0, 4, 4, new [] { "Top" })]
- [InlineData (1, 1, 0, 0, 0, 9, 9, new [] { "Top" })]
- [InlineData (1, 1, 0, 0, 0, 10, 10, new [] { "Top" })]
- [InlineData (0, 0, 1, 0, 0, -1, -1, new string [] { })]
- [InlineData (0, 0, 1, 0, 0, 0, 0, new string [] { })] //margin is ViewportSettings.TransparentToMouse
- [InlineData (0, 0, 1, 0, 0, 1, 1, new [] { "Top" })]
- [InlineData (0, 0, 1, 0, 0, 4, 4, new [] { "Top" })]
- [InlineData (0, 0, 1, 0, 0, 9, 9, new string [] { })] //margin is ViewportSettings.TransparentToMouse
- [InlineData (0, 0, 1, 0, 0, 10, 10, new string [] { })]
- [InlineData (0, 0, 1, 1, 0, -1, -1, new string [] { })]
- [InlineData (0, 0, 1, 1, 0, 0, 0, new string [] { })] //margin is ViewportSettings.TransparentToMouse
- [InlineData (0, 0, 1, 1, 0, 1, 1, new [] { "Top", "Border" })]
- [InlineData (0, 0, 1, 1, 0, 4, 4, new [] { "Top" })]
- [InlineData (0, 0, 1, 1, 0, 9, 9, new string [] { })] //margin is ViewportSettings.TransparentToMouse
- [InlineData (0, 0, 1, 1, 0, 10, 10, new string [] { })]
- [InlineData (0, 0, 1, 1, 1, -1, -1, new string [] { })]
- [InlineData (0, 0, 1, 1, 1, 0, 0, new string [] { })] //margin is ViewportSettings.TransparentToMouse
- [InlineData (0, 0, 1, 1, 1, 1, 1, new [] { "Top", "Border" })]
- [InlineData (0, 0, 1, 1, 1, 2, 2, new [] { "Top", "Padding" })]
- [InlineData (0, 0, 1, 1, 1, 4, 4, new [] { "Top" })]
- [InlineData (0, 0, 1, 1, 1, 9, 9, new string [] { })] //margin is ViewportSettings.TransparentToMouse
- [InlineData (0, 0, 1, 1, 1, 10, 10, new string [] { })]
- [InlineData (1, 1, 1, 0, 0, -1, -1, new string [] { })]
- [InlineData (1, 1, 1, 0, 0, 0, 0, new string [] { })]
- [InlineData (1, 1, 1, 0, 0, 1, 1, new string [] { })] //margin is ViewportSettings.TransparentToMouse
- [InlineData (1, 1, 1, 0, 0, 4, 4, new [] { "Top" })]
- [InlineData (1, 1, 1, 0, 0, 9, 9, new [] { "Top" })]
- [InlineData (1, 1, 1, 0, 0, 10, 10, new string [] { })] //margin is ViewportSettings.TransparentToMouse
- [InlineData (1, 1, 1, 1, 0, -1, -1, new string [] { })]
- [InlineData (1, 1, 1, 1, 0, 0, 0, new string [] { })]
- [InlineData (1, 1, 1, 1, 0, 1, 1, new string [] { })] //margin is ViewportSettings.TransparentToMouse
- [InlineData (1, 1, 1, 1, 0, 4, 4, new [] { "Top" })]
- [InlineData (1, 1, 1, 1, 0, 9, 9, new [] { "Top", "Border" })]
- [InlineData (1, 1, 1, 1, 0, 10, 10, new string [] { })] //margin is ViewportSettings.TransparentToMouse
- [InlineData (1, 1, 1, 1, 1, -1, -1, new string [] { })]
- [InlineData (1, 1, 1, 1, 1, 0, 0, new string [] { })]
- [InlineData (1, 1, 1, 1, 1, 1, 1, new string [] { })] //margin is ViewportSettings.TransparentToMouse
- [InlineData (1, 1, 1, 1, 1, 2, 2, new [] { "Top", "Border" })]
- [InlineData (1, 1, 1, 1, 1, 3, 3, new [] { "Top", "Padding" })]
- [InlineData (1, 1, 1, 1, 1, 4, 4, new [] { "Top" })]
- [InlineData (1, 1, 1, 1, 1, 8, 8, new [] { "Top", "Padding" })]
- [InlineData (1, 1, 1, 1, 1, 9, 9, new [] { "Top", "Border" })]
- [InlineData (1, 1, 1, 1, 1, 10, 10, new string [] { })] //margin is ViewportSettings.TransparentToMouse
- public void Top_Adornments_Returns_Correct_View (
- int frameX,
- int frameY,
- int marginThickness,
- int borderThickness,
- int paddingThickness,
- int testX,
- int testY,
- string [] expectedViewsFound
- )
- {
- // Arrange
- Runnable<bool>? runnable = new ()
- {
- Id = "Top",
- Frame = new (frameX, frameY, 10, 10)
- };
- IApplication? app = Application.Create ();
- app.Begin (runnable);
- runnable.Margin!.Thickness = new (marginThickness);
- runnable.Margin!.Id = "Margin";
- runnable.Border!.Thickness = new (borderThickness);
- runnable.Border!.Id = "Border";
- runnable.Padding!.Thickness = new (paddingThickness);
- runnable.Padding.Id = "Padding";
- var location = new Point (testX, testY);
- // Act
- List<View?> viewsUnderMouse = runnable.GetViewsUnderLocation (location, ViewportSettingsFlags.TransparentMouse);
- // Assert
- if (expectedViewsFound.Length == 0)
- {
- Assert.Empty (viewsUnderMouse);
- }
- else
- {
- string [] foundIds = viewsUnderMouse.Select (v => v!.Id).ToArray ();
- Assert.Equal (expectedViewsFound, foundIds);
- }
- }
- [Theory]
- [InlineData (0, 0)]
- [InlineData (1, 1)]
- [InlineData (2, 2)]
- public void Returns_Top_If_No_SubViews (int testX, int testY)
- {
- // Arrange
- Runnable<bool>? runnable = new ()
- {
- Frame = new (0, 0, 10, 10)
- };
- IApplication? app = Application.Create ();
- app.Begin (runnable);
- var location = new Point (testX, testY);
- // Act
- List<View?> viewsUnderMouse = runnable.GetViewsUnderLocation (location, ViewportSettingsFlags.TransparentMouse);
- // Assert
- Assert.Contains (viewsUnderMouse, v => v == runnable);
- runnable.Dispose ();
- }
- // Test that GetViewsUnderLocation 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)
- {
- Runnable<bool>? runnable = new ()
- {
- Width = 10, Height = 10
- };
- IApplication? app = Application.Create ();
- app.Begin (runnable);
- Assert.Same (runnable, runnable.GetViewsUnderLocation (new (testX, testY), ViewportSettingsFlags.TransparentMouse).LastOrDefault ());
- runnable.Dispose ();
- }
- // Test that GetViewsUnderLocation 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)
- {
- Runnable<bool>? runnable = new ()
- {
- Width = 10, Height = 10
- };
- IApplication? app = Application.Create ();
- app.Begin (runnable);
- var subview = new View
- {
- X = 1, Y = 2,
- Width = 5, Height = 5
- };
- runnable.Add (subview);
- View? found = runnable.GetViewsUnderLocation (new (testX, testY), ViewportSettingsFlags.TransparentMouse).LastOrDefault ();
- Assert.Equal (expectedSubViewFound, found == subview);
- runnable.Dispose ();
- }
- [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)
- {
- Runnable<bool>? runnable = new ()
- {
- Width = 10, Height = 10
- };
- var subview = new View
- {
- X = 1, Y = 2,
- Width = 5, Height = 5,
- Visible = false
- };
- runnable.Add (subview);
- View? found = runnable.GetViewsUnderLocation (new (testX, testY), ViewportSettingsFlags.TransparentMouse).LastOrDefault ();
- Assert.Equal (expectedSubViewFound, found == subview);
- runnable.Dispose ();
- }
- [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)
- {
- Runnable<bool>? runnable = new ()
- {
- Width = 10, Height = 10,
- Visible = false
- };
- var subview = new View
- {
- X = 1, Y = 2,
- Width = 5, Height = 5
- };
- runnable.Add (subview);
- subview.Visible = true;
- Assert.True (subview.Visible);
- Assert.False (runnable.Visible);
- View? found = runnable.GetViewsUnderLocation (new (testX, testY), ViewportSettingsFlags.TransparentMouse).LastOrDefault ();
- Assert.Equal (expectedSubViewFound, found == subview);
- runnable.Dispose ();
- }
- // Test that GetViewsUnderLocation 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 (6, 7, true)]
- public void Returns_Correct_If_Start_Has_Adornments (int testX, int testY, bool expectedSubViewFound)
- {
- Runnable<bool>? runnable = new ()
- {
- Width = 10, Height = 10
- };
- IApplication? app = Application.Create ();
- app.Begin (runnable);
- runnable.Margin!.Thickness = new (1);
- var subview = new View
- {
- X = 1, Y = 2,
- Width = 5, Height = 5
- };
- runnable.Add (subview);
- View? found = runnable.GetViewsUnderLocation (new (testX, testY), ViewportSettingsFlags.TransparentMouse).LastOrDefault ();
- Assert.Equal (expectedSubViewFound, found == subview);
- runnable.Dispose ();
- }
- // Test that GetViewsUnderLocation 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)
- {
- Runnable<bool>? runnable = new ()
- {
- Width = 10, Height = 10,
- ViewportSettings = ViewportSettingsFlags.AllowNegativeLocation
- };
- IApplication? app = Application.Create ();
- app.Begin (runnable);
- runnable.Viewport = new (offset, offset, 10, 10);
- var subview = new View
- {
- X = 1, Y = 1,
- Width = 2, Height = 2
- };
- runnable.Add (subview);
- View? found = runnable.GetViewsUnderLocation (new (testX, testY), ViewportSettingsFlags.TransparentMouse).LastOrDefault ();
- Assert.Equal (expectedSubViewFound, found == subview);
- runnable.Dispose ();
- }
- [Theory]
- [InlineData (9, 9, true)]
- [InlineData (0, 0, false)]
- [InlineData (1, 1, false)]
- [InlineData (10, 10, false)]
- [InlineData (7, 8, false)]
- [InlineData (1, 2, false)]
- [InlineData (2, 3, false)]
- [InlineData (5, 6, false)]
- [InlineData (6, 7, false)]
- public void Returns_Correct_If_Start_Has_Adornment_WithSubView (int testX, int testY, bool expectedSubViewFound)
- {
- Runnable<bool>? runnable = new ()
- {
- Width = 10, Height = 10
- };
- IApplication? app = Application.Create ();
- app.Begin (runnable);
- runnable.Padding!.Thickness = new (1);
- var subview = new View
- {
- X = Pos.AnchorEnd (1), Y = Pos.AnchorEnd (1),
- Width = 1, Height = 1
- };
- runnable.Padding.Add (subview);
- View? found = runnable.GetViewsUnderLocation (new (testX, testY), ViewportSettingsFlags.TransparentMouse).LastOrDefault ();
- Assert.Equal (expectedSubViewFound, found == subview);
- runnable.Dispose ();
- }
- [Theory]
- [InlineData (0, 0, new string [] { })]
- [InlineData (9, 9, new string [] { })]
- [InlineData (1, 1, new [] { "Top", "Border" })]
- [InlineData (8, 8, new [] { "Top", "Border" })]
- [InlineData (2, 2, new [] { "Top", "Padding" })]
- [InlineData (7, 7, new [] { "Top", "Padding" })]
- [InlineData (5, 5, new [] { "Top" })]
- public void Returns_Adornment_If_Start_Has_Adornments (int testX, int testY, string [] expectedViewsFound)
- {
- IApplication? app = Application.Create ();
- Runnable<bool>? runnable = new ()
- {
- Id = "Top",
- Width = 10, Height = 10
- };
- app.Begin (runnable);
- runnable.Margin!.Thickness = new (1);
- runnable.Margin!.Id = "Margin";
- runnable.Border!.Thickness = new (1);
- runnable.Border!.Id = "Border";
- runnable.Padding!.Thickness = new (1);
- runnable.Padding.Id = "Padding";
- var subview = new View
- {
- Id = "SubView",
- X = 1, Y = 1,
- Width = 1, Height = 1
- };
- runnable.Add (subview);
- List<View?> viewsUnderMouse = runnable.GetViewsUnderLocation (new (testX, testY), ViewportSettingsFlags.TransparentMouse);
- string [] foundIds = viewsUnderMouse.Select (v => v!.Id).ToArray ();
- Assert.Equal (expectedViewsFound, foundIds);
- runnable.Dispose ();
- }
- // Test that GetViewsUnderLocation works if the subview has positive Adornments
- [Theory]
- [InlineData (0, 0, new [] { "Top" })]
- [InlineData (1, 1, new [] { "Top" })]
- [InlineData (9, 9, new [] { "Top" })]
- [InlineData (10, 10, new string [] { })]
- [InlineData (7, 8, new [] { "Top" })]
- [InlineData (6, 7, new [] { "Top" })]
- [InlineData (1, 2, new [] { "Top", "subview", "border" })]
- [InlineData (5, 6, new [] { "Top", "subview", "border" })]
- [InlineData (2, 3, new [] { "Top", "subview" })]
- public void Returns_Correct_If_SubView_Has_Adornments (int testX, int testY, string [] expectedViewsFound)
- {
- Runnable<bool>? runnable = new ()
- {
- Id = "Top",
- Width = 10, Height = 10
- };
- IApplication? app = Application.Create ();
- app.Begin (runnable);
- var subview = new View
- {
- Id = "subview",
- X = 1, Y = 2,
- Width = 5, Height = 5
- };
- subview.Border!.Thickness = new (1);
- subview.Border!.Id = "border";
- runnable.Add (subview);
- List<View?> viewsUnderMouse = runnable.GetViewsUnderLocation (new (testX, testY), ViewportSettingsFlags.TransparentMouse);
- string [] foundIds = viewsUnderMouse.Select (v => v!.Id).ToArray ();
- Assert.Equal (expectedViewsFound, foundIds);
- runnable.Dispose ();
- }
- // Test that GetViewsUnderLocation works if the subview has positive Adornments
- [Theory]
- [InlineData (0, 0, new [] { "Top" })]
- [InlineData (1, 1, new [] { "Top" })]
- [InlineData (9, 9, new [] { "Top" })]
- [InlineData (10, 10, new string [] { })]
- [InlineData (7, 8, new [] { "Top" })]
- [InlineData (6, 7, new [] { "Top" })]
- [InlineData (1, 2, new [] { "Top" })]
- [InlineData (5, 6, new [] { "Top" })]
- [InlineData (2, 3, new [] { "Top", "subview" })]
- public void Returns_Correct_If_SubView_Has_Adornments_With_TransparentMouse (int testX, int testY, string [] expectedViewsFound)
- {
- Runnable<bool>? runnable = new ()
- {
- Id = "Top",
- Width = 10, Height = 10
- };
- IApplication? app = Application.Create ();
- app.Begin (runnable);
- var subview = new View
- {
- Id = "subview",
- X = 1, Y = 2,
- Width = 5, Height = 5
- };
- subview.Border!.Thickness = new (1);
- subview.Border!.ViewportSettings = ViewportSettingsFlags.TransparentMouse;
- subview.Border!.Id = "border";
- runnable.Add (subview);
- List<View?> viewsUnderMouse = runnable.GetViewsUnderLocation (new (testX, testY), ViewportSettingsFlags.TransparentMouse);
- string [] foundIds = viewsUnderMouse.Select (v => v!.Id).ToArray ();
- Assert.Equal (expectedViewsFound, foundIds);
- runnable.Dispose ();
- }
- [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)
- {
- Runnable<bool>? runnable = new ()
- {
- Width = 10, Height = 10
- };
- IApplication? app = Application.Create ();
- app.Begin (runnable);
- // 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);
- runnable.Add (subview);
- View? found = runnable.GetViewsUnderLocation (new (testX, testY), ViewportSettingsFlags.TransparentMouse).LastOrDefault ();
- Assert.Equal (expectedSubViewFound, found == paddingSubView);
- runnable.Dispose ();
- }
- [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)
- {
- Runnable<bool>? runnable = new ()
- {
- Width = 10, Height = 10
- };
- IApplication? app = Application.Create ();
- app.Begin (runnable);
- // 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.SetContentSize (new (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);
- runnable.Add (subview);
- View? found = runnable.GetViewsUnderLocation (new (testX, testY), ViewportSettingsFlags.TransparentMouse).LastOrDefault ();
- Assert.Equal (expectedSubViewFound, found == paddingSubView);
- runnable.Dispose ();
- }
- // Test that GetViewsUnderLocation 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)
- {
- Runnable<bool>? runnable = new ()
- {
- Width = 10, Height = 10
- };
- IApplication? app = Application.Create ();
- app.Begin (runnable);
- var numSubViews = 3;
- List<View> subviews = new ();
- for (var 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);
- }
- }
- runnable.Add (subviews [0]);
- View? found = runnable.GetViewsUnderLocation (new (testX, testY), ViewportSettingsFlags.TransparentMouse).LastOrDefault ();
- Assert.Equal (expectedSubViewFound, subviews.IndexOf (found!));
- runnable.Dispose ();
- }
- [Theory]
- [InlineData (0, 0, new [] { "top" })]
- [InlineData (9, 9, new [] { "top" })]
- [InlineData (10, 10, new string [] { })]
- [InlineData (1, 1, new [] { "top", "view" })]
- [InlineData (1, 2, new [] { "top", "view" })]
- [InlineData (2, 1, new [] { "top", "view" })]
- [InlineData (2, 2, new [] { "top", "view", "subView" })]
- [InlineData (3, 3, new [] { "top" })] // clipped
- [InlineData (2, 3, new [] { "top" })] // clipped
- public void Tiled_SubViews (int mouseX, int mouseY, string [] viewIdStrings)
- {
- // Arrange
- Runnable<bool>? runnable = new ()
- {
- Frame = new (0, 0, 10, 10),
- Id = "top"
- };
- IApplication? app = Application.Create ();
- app.Begin (runnable);
- var view = new View
- {
- Id = "view",
- X = 1,
- Y = 1,
- Width = 2,
- Height = 2,
- Arrangement = ViewArrangement.Overlapped
- }; // at 1,1 to 3,2 (screen)
- var subView = new View
- {
- Id = "subView",
- X = 1,
- Y = 1,
- Width = 2,
- Height = 2,
- Arrangement = ViewArrangement.Overlapped
- }; // at 2,2 to 4,3 (screen)
- view.Add (subView);
- runnable.Add (view);
- List<View?> found = runnable.GetViewsUnderLocation (new (mouseX, mouseY), ViewportSettingsFlags.TransparentMouse);
- string [] foundIds = found.Select (v => v!.Id).ToArray ();
- Assert.Equal (viewIdStrings, foundIds);
- runnable.Dispose ();
- }
- [Theory]
- [InlineData (0, 0, new [] { "top" })]
- [InlineData (9, 9, new [] { "top" })]
- [InlineData (10, 10, new string [] { })]
- [InlineData (-1, -1, new string [] { })]
- [InlineData (1, 1, new [] { "top", "view" })]
- [InlineData (1, 2, new [] { "top", "view" })]
- [InlineData (2, 1, new [] { "top", "view" })]
- [InlineData (2, 2, new [] { "top", "view", "popover" })]
- [InlineData (3, 3, new [] { "top" })] // clipped
- [InlineData (2, 3, new [] { "top" })] // clipped
- public void Popover (int mouseX, int mouseY, string [] viewIdStrings)
- {
- // Arrange
- Runnable<bool>? runnable = new ()
- {
- Frame = new (0, 0, 10, 10),
- Id = "top"
- };
- IApplication? app = Application.Create ();
- app.Begin (runnable);
- var view = new View
- {
- Id = "view",
- X = 1,
- Y = 1,
- Width = 2,
- Height = 2,
- Arrangement = ViewArrangement.Overlapped
- }; // at 1,1 to 3,2 (screen)
- var popOver = new View
- {
- Id = "popover",
- X = 1,
- Y = 1,
- Width = 2,
- Height = 2,
- Arrangement = ViewArrangement.Overlapped
- }; // at 2,2 to 4,3 (screen)
- view.Add (popOver);
- runnable.Add (view);
- List<View?> found = runnable.GetViewsUnderLocation (new (mouseX, mouseY), ViewportSettingsFlags.TransparentMouse);
- string [] foundIds = found.Select (v => v!.Id).ToArray ();
- Assert.Equal (viewIdStrings, foundIds);
- runnable.Dispose ();
- }
- [Fact]
- public void Returns_TopRunnable_When_Point_Inside_Only_TopRunnable ()
- {
- IApplication? app = Application.Create ();
- Runnable<bool> runnable = new ()
- {
- Id = "topRunnable",
- Frame = new (0, 0, 20, 20)
- };
- Runnable<bool> secondaryRunnable = new ()
- {
- Id = "secondaryRunnable",
- Frame = new (5, 5, 10, 10)
- };
- secondaryRunnable.Margin!.Thickness = new (1);
- secondaryRunnable.Layout ();
- app.Begin (runnable);
- app.Begin (secondaryRunnable);
- List<View?> found = runnable.GetViewsUnderLocation (new (2, 2), ViewportSettingsFlags.TransparentMouse);
- Assert.Contains (found, v => v?.Id == runnable.Id);
- Assert.Contains (found, v => v == runnable);
- runnable.Dispose ();
- secondaryRunnable.Dispose ();
- }
- [Fact]
- public void Returns_SecondaryRunnable_When_Point_Inside_Only_SecondaryRunnable ()
- {
- IApplication? app = Application.Create ();
- Runnable<bool> runnable = new ()
- {
- Id = "topRunnable",
- Frame = new (0, 0, 20, 20)
- };
- Runnable<bool> secondaryRunnable = new ()
- {
- Id = "secondaryRunnable",
- Frame = new (5, 5, 10, 10)
- };
- secondaryRunnable.Margin!.Thickness = new (1);
- secondaryRunnable.Layout ();
- app.Begin (runnable);
- app.Begin (secondaryRunnable);
- List<View?> found = runnable.GetViewsUnderLocation (new (7, 7), ViewportSettingsFlags.TransparentMouse);
- Assert.Contains (found, v => v?.Id == secondaryRunnable.Id);
- Assert.DoesNotContain (found, v => v?.Id == runnable.Id);
- runnable.Dispose ();
- secondaryRunnable.Dispose ();
- }
- [Fact]
- public void Returns_Depends_On_Margin_ViewportSettings_When_Point_In_Margin_Of_SecondaryRunnable ()
- {
- IApplication? app = Application.Create ();
- Runnable<bool> runnable = new ()
- {
- Id = "topRunnable",
- Frame = new (0, 0, 20, 20)
- };
- Runnable<bool> secondaryRunnable = new ()
- {
- Id = "secondaryRunnable",
- Frame = new (5, 5, 10, 10)
- };
- secondaryRunnable.Margin!.Thickness = new (1);
- app.Begin (runnable);
- app.Begin (secondaryRunnable);
- secondaryRunnable.Margin!.ViewportSettings = ViewportSettingsFlags.None;
- List<View?> found = runnable.GetViewsUnderLocation (new (5, 5), ViewportSettingsFlags.TransparentMouse);
- Assert.Contains (found, v => v == secondaryRunnable);
- Assert.Contains (found, v => v == secondaryRunnable.Margin);
- Assert.DoesNotContain (found, v => v?.Id == runnable.Id);
- secondaryRunnable.Margin!.ViewportSettings = ViewportSettingsFlags.TransparentMouse;
- found = runnable.GetViewsUnderLocation (new (5, 5), ViewportSettingsFlags.TransparentMouse);
- Assert.DoesNotContain (found, v => v == secondaryRunnable);
- Assert.DoesNotContain (found, v => v == secondaryRunnable.Margin);
- Assert.Contains (found, v => v?.Id == runnable.Id);
- runnable.Dispose ();
- secondaryRunnable.Dispose ();
- }
- [Fact]
- public void Returns_Empty_When_Point_Outside_All_Runnables ()
- {
- IApplication? app = Application.Create ();
- Runnable<bool> runnable = new ()
- {
- Id = "topRunnable",
- Frame = new (0, 0, 20, 20)
- };
- Runnable<bool> secondaryRunnable = new ()
- {
- Id = "secondaryRunnable",
- Frame = new (5, 5, 10, 10)
- };
- secondaryRunnable.Margin!.Thickness = new (1);
- secondaryRunnable.Layout ();
- app.Begin (runnable);
- app.Begin (secondaryRunnable);
- List<View?> found = runnable.GetViewsUnderLocation (new (20, 20), ViewportSettingsFlags.TransparentMouse);
- Assert.Empty (found);
- runnable.Dispose ();
- secondaryRunnable.Dispose ();
- }
- }
|