| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- using System;
- using System.Linq;
- using Avalonia.Base.UnitTests.VisualTree;
- using Avalonia.Controls;
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Shapes;
- using Avalonia.Layout;
- using Avalonia.Media;
- using Avalonia.Platform;
- using Avalonia.Rendering;
- using Avalonia.UnitTests;
- using Avalonia.VisualTree;
- using Moq;
- using Xunit;
- namespace Avalonia.Base.UnitTests.Rendering;
- public class CompositorHitTestingTests : CompositorTestsBase
- {
- [Fact]
- public void HitTest_Should_Find_Controls_At_Point()
- {
- using (var s = new CompositorTestServices(new Size(200, 200)))
- {
- var border = new Border
- {
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center
- };
- s.TopLevel.Content = border;
-
- s.AssertHitTest(new Point(100, 100), null, border);
- }
- }
- [Fact]
- public void HitTest_Should_Not_Find_Empty_Controls_At_Point()
- {
- using (var s = new CompositorTestServices(new Size(200, 200)))
- {
- var border = new Border
- {
- Width = 100,
- Height = 100,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center
- };
- s.TopLevel.Content = border;
- s.AssertHitTest(new Point(100, 100), null);
- }
- }
- [Fact]
- public void HitTest_Should_Not_Find_Invisible_Controls_At_Point()
- {
- using (var s = new CompositorTestServices(new Size(200, 200)))
- {
- Border visible, border;
- s.TopLevel.Content = border = new Border
- {
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center,
- IsVisible = false,
- Child = visible = new Border
- {
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Stretch,
- VerticalAlignment = VerticalAlignment.Stretch,
- }
- };
- s.AssertHitTest(new Point(100, 100), null);
- }
- }
-
- [Theory,
- InlineData(false, false),
- InlineData(true, false),
- InlineData(false, true),
- InlineData(true, true),
- ]
- public void HitTest_Should_Find_Zero_Opacity_Controls_At_Point(bool parent, bool child)
- {
-
- using (var s = new CompositorTestServices(new Size(200, 200)))
- {
- Border visible, border;
- s.TopLevel.Content = border = new Border
- {
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center,
- Opacity = parent ? 0 : 1,
- Child = visible = new Border
- {
- Opacity = child ? 0 : 1,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Stretch,
- VerticalAlignment = VerticalAlignment.Stretch,
- }
- };
- s.AssertHitTest(new Point(100, 100), null, visible, border);
- }
- }
-
- [Fact]
- public void HitTest_Should_Not_Find_Control_Outside_Point()
- {
- using (var s = new CompositorTestServices(new Size(200, 200)))
- {
- var border = new Border
- {
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center
- };
- s.TopLevel.Content = border;
- s.AssertHitTest(new Point(10, 10), null);
- }
- }
- [Fact]
- public void HitTest_Should_Return_Top_Controls_First()
- {
- using (var s = new CompositorTestServices(new Size(200, 200)))
- {
- Panel container = new Panel
- {
- Width = 200,
- Height = 200,
- Children =
- {
- new Border
- {
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center
- },
- new Border
- {
- Width = 50,
- Height = 50,
- Background = Brushes.Blue,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center
- }
- }
- };
- s.TopLevel.Content = container;
- s.AssertHitTest(new Point(100, 100), null, container.Children[1], container.Children[0]);
- }
- }
- [Fact]
- public void HitTest_Should_Return_Top_Controls_First_With_ZIndex()
- {
- using (var s = new CompositorTestServices(new Size(200, 200)))
- {
- Panel container = new Panel
- {
- Width = 200,
- Height = 200,
- Children =
- {
- new Border
- {
- Width = 100,
- Height = 100,
- ZIndex = 1,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center
- },
- new Border
- {
- Width = 50,
- Height = 50,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center
- },
- new Border
- {
- Width = 75,
- Height = 75,
- ZIndex = 2,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center
- }
- }
- };
- s.TopLevel.Content = container;
- s.AssertHitTest(new Point(100, 100), null, new[] { container.Children[2], container.Children[0], container.Children[1] });
- }
- }
- [Fact]
- public void HitTest_Should_Find_Control_Translated_Outside_Parent_Bounds()
- {
- using (var s = new CompositorTestServices(new Size(200, 200)))
- {
- Border target;
- Panel container = new Panel
- {
- Width = 200,
- Height = 200,
- Background = Brushes.Red,
- ClipToBounds = false,
- Children =
- {
- new Border
- {
- Width = 100,
- Height = 100,
- ZIndex = 1,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Left,
- VerticalAlignment = VerticalAlignment.Top,
- Child = target = new Border
- {
- Width = 50,
- Height = 50,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Left,
- VerticalAlignment = VerticalAlignment.Top,
- RenderTransform = new TranslateTransform(110, 110),
- }
- },
- }
- };
- s.TopLevel.Content = container;
- s.AssertHitTest(new Point(120, 120), null, target, container);
- }
- }
- [Fact]
- public void HitTest_Should_Not_Find_Control_Outside_Parent_Bounds_When_Clipped()
- {
- using (var s = new CompositorTestServices(new Size(200, 200)))
- {
- Border target;
- Panel container = new Panel
- {
- Width = 100,
- Height = 200,
- Background = Brushes.Red,
- Children =
- {
- new Panel()
- {
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- Margin = new Thickness(0, 100, 0, 0),
- ClipToBounds = true,
- Children =
- {
- (target = new Border()
- {
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- Margin = new Thickness(0, -100, 0, 0)
- })
- }
- }
- }
- };
- s.TopLevel.Content = container;
- s.AssertHitTest(new Point(50, 50), null, container);
- }
- }
- [Fact]
- public void HitTest_Should_Not_Find_Control_Outside_Scroll_Viewport()
- {
- using (var s = new CompositorTestServices(new Size(100, 200)))
- {
- Border target;
- Border item1;
- Border item2;
- ScrollContentPresenter scroll;
- Panel container = new Panel
- {
- Width = 100,
- Height = 200,
- Background = Brushes.Red,
- Children =
- {
- (target = new Border()
- {
- Name = "b1",
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- }),
- new Border()
- {
- Name = "b2",
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- Margin = new Thickness(0, 100, 0, 0),
- Child = scroll = new ScrollContentPresenter()
- {
- CanHorizontallyScroll = true,
- CanVerticallyScroll = true,
- Content = new StackPanel()
- {
- Children =
- {
- (item1 = new Border()
- {
- Name = "b3",
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- }),
- (item2 = new Border()
- {
- Name = "b4",
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- }),
- }
- }
- }
- }
- }
- };
- s.TopLevel.Content = container;
- scroll.UpdateChild();
-
- s.AssertHitTestFirst(new Point(50, 150), null, item1);
- s.AssertHitTestFirst(new Point(50,50), null, target);
-
- scroll.Offset = new Vector(0, 100);
- s.AssertHitTestFirst(new Point(50, 150), null, item2);
-
- s.AssertHitTestFirst(new Point(50,50), null, target);
- }
- }
- [Fact]
- public void HitTest_Should_Not_Find_Path_When_Outside_Fill()
- {
- using (var s = new CompositorTestServices(new Size(200, 200)))
- {
- Path path = new Path
- {
- Width = 200,
- Height = 200,
- Fill = Brushes.Red,
- Data = StreamGeometry.Parse("M100,0 L0,100 100,100")
- };
- s.TopLevel.Content = path;
- s.AssertHitTest(new Point(100, 100), null, path);
- s.AssertHitTest(new Point(10, 10), null);
- }
- }
- [Fact]
- public void HitTest_Should_Respect_Geometry_Clip()
- {
- using (var s = new CompositorTestServices(new Size(400, 400)))
- {
- Canvas canvas;
- Border border = new Border
- {
- Background = Brushes.Red,
- Clip = StreamGeometry.Parse("M100,0 L0,100 100,100"),
- Width = 200,
- Height = 200,
- Child = canvas = new Canvas
- {
- Background = Brushes.Yellow,
- Margin = new Thickness(10),
- }
- };
- s.TopLevel.Content = border;
- s.RunJobs();
- Assert.Equal(new Rect(100, 100, 200, 200), border.Bounds);
- s.AssertHitTest(new Point(200,200), null, canvas, border);
- s.AssertHitTest(new Point(110, 110), null);
- }
- }
- [Fact]
- public void HitTest_Should_Accommodate_ICustomHitTest()
- {
- using (var s = new CompositorTestServices(new Size(300, 200)))
- {
- Border border = new CustomHitTestBorder
- {
- ClipToBounds = false,
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center
- };
- s.TopLevel.Content = border;
-
- s.AssertHitTest(75, 100, null, border);
- s.AssertHitTest(125, 100, null, border);
- s.AssertHitTest(175, 100, null);
- }
- }
-
- [Fact]
- public void HitTest_Should_Not_Hit_Controls_Next_Pixel()
- {
- using (var s = new CompositorTestServices(new Size(200, 200)))
- {
- Border targetRectangle;
- var stackPanel = new StackPanel
- {
- Orientation = Orientation.Vertical,
- HorizontalAlignment = HorizontalAlignment.Left,
- Children =
- {
- new Border { Width = 10, Height = 10, Background= Brushes.Red},
- { targetRectangle = new Border { Width = 10, Height = 10, Background = Brushes.Green} }
- }
- };
- s.TopLevel.Content = stackPanel;
-
- s.AssertHitTest(new Point(5, 10), null, targetRectangle);
- }
- }
-
-
- [Fact]
- public void HitTest_Filter_Should_Filter_Out_Children()
- {
- using (var s = new CompositorTestServices(new Size(200, 200)))
- {
- Border child, parent;
- s.TopLevel.Content = parent = new Border
- {
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center,
- Child = child = new Border
- {
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Stretch,
- VerticalAlignment = VerticalAlignment.Stretch,
- }
- };
- s.AssertHitTest(new Point(100, 100), null, child, parent);
- s.AssertHitTest(new Point(100, 100), v => v != parent);
- }
- }
- private static IDisposable TestApplication()
- {
- return UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
- }
- }
|