123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- // Copyright (c) The Avalonia Project. All rights reserved.
- // Licensed under the MIT license. See licence.md file in the project root for full license information.
- using System.Linq;
- using Avalonia.Controls;
- using Avalonia.Controls.Presenters;
- using Avalonia.Layout;
- using Avalonia.Media;
- using Avalonia.Platform;
- using Avalonia.Rendering;
- using Avalonia.UnitTests;
- using Avalonia.VisualTree;
- using Moq;
- using Xunit;
- using System;
- using Avalonia.Controls.Shapes;
- namespace Avalonia.Visuals.UnitTests.VisualTree
- {
- public class VisualExtensionsTests_GetVisualsAt
- {
- [Fact]
- public void GetVisualsAt_Should_Find_Controls_At_Point()
- {
- using (TestApplication())
- {
- var container = new TestRoot
- {
- Width = 200,
- Height = 200,
- Child = new Border
- {
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center
- }
- };
- container.Measure(Size.Infinity);
- container.Arrange(new Rect(container.DesiredSize));
- var result = container.GetVisualsAt(new Point(100, 100));
- Assert.Equal(new[] { container.Child }, result);
- }
- }
- [Fact]
- public void GetVisualsAt_Should_Not_Find_Empty_Controls_At_Point()
- {
- using (TestApplication())
- {
- var container = new TestRoot
- {
- Width = 200,
- Height = 200,
- Child = new Border
- {
- Width = 100,
- Height = 100,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center
- }
- };
- container.Measure(Size.Infinity);
- container.Arrange(new Rect(container.DesiredSize));
- var result = container.GetVisualsAt(new Point(100, 100));
- Assert.Empty(result);
- }
- }
- [Fact]
- public void GetVisualsAt_Should_Not_Find_Invisible_Controls_At_Point()
- {
- using (TestApplication())
- {
- Border visible;
- var container = new TestRoot
- {
- Width = 200,
- Height = 200,
- Child = 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,
- }
- }
- };
- container.Measure(Size.Infinity);
- container.Arrange(new Rect(container.DesiredSize));
- var result = container.GetVisualsAt(new Point(100, 100));
- Assert.Empty(result);
- }
- }
- [Fact]
- public void GetVisualsAt_Should_Not_Find_Control_Outside_Point()
- {
- using (TestApplication())
- {
- var container = new TestRoot
- {
- Width = 200,
- Height = 200,
- Child = new Border
- {
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center
- }
- };
- container.Measure(Size.Infinity);
- container.Arrange(new Rect(container.DesiredSize));
- var result = container.GetVisualsAt(new Point(10, 10));
- Assert.Empty(result);
- }
- }
- [Fact]
- public void GetVisualsAt_Should_Return_Top_Controls_First()
- {
- using (TestApplication())
- {
- Panel container;
- var root = new TestRoot
- {
- Child = container = new Panel
- {
- Width = 200,
- Height = 200,
- Children = new Controls.Controls
- {
- new Border
- {
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center
- },
- new Border
- {
- Width = 50,
- Height = 50,
- Background = Brushes.Red,
- HorizontalAlignment = HorizontalAlignment.Center,
- VerticalAlignment = VerticalAlignment.Center
- }
- }
- }
- };
- root.Measure(Size.Infinity);
- root.Arrange(new Rect(container.DesiredSize));
- var result = container.GetVisualsAt(new Point(100, 100));
- Assert.Equal(new[] { container.Children[1], container.Children[0] }, result);
- }
- }
- [Fact]
- public void GetVisualsAt_Should_Return_Top_Controls_First_With_ZIndex()
- {
- using (TestApplication())
- {
- Panel container;
- var root = new TestRoot
- {
- Child = container = new Panel
- {
- Width = 200,
- Height = 200,
- Children = new Controls.Controls
- {
- 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
- }
- }
- }
- };
- root.Measure(Size.Infinity);
- root.Arrange(new Rect(container.DesiredSize));
- var result = container.GetVisualsAt(new Point(100, 100));
- Assert.Equal(new[] { container.Children[2], container.Children[0], container.Children[1] }, result);
- }
- }
- [Fact]
- public void GetVisualsAt_Should_Find_Control_Translated_Outside_Parent_Bounds()
- {
- using (TestApplication())
- {
- Border target;
- Panel container;
- var root = new TestRoot
- {
- Child = container = new Panel
- {
- Width = 200,
- Height = 200,
- Background = Brushes.Red,
- ClipToBounds = false,
- Children = new Controls.Controls
- {
- 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),
- }
- },
- }
- }
- };
- container.Measure(Size.Infinity);
- container.Arrange(new Rect(container.DesiredSize));
- var result = container.GetVisualsAt(new Point(120, 120));
- Assert.Equal(new IVisual[] { target, container }, result);
- }
- }
- [Fact]
- public void GetVisualsAt_Should_Not_Find_Control_Outside_Parent_Bounds_When_Clipped()
- {
- using (TestApplication())
- {
- Border target;
- Panel container;
- var root = new TestRoot
- {
- Child = container = new Panel
- {
- Width = 100,
- Height = 200,
- Background = Brushes.Red,
- Children = new Controls.Controls
- {
- new Panel()
- {
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- Margin = new Thickness(0, 100, 0, 0),
- ClipToBounds = true,
- Children = new Controls.Controls
- {
- (target = new Border()
- {
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- Margin = new Thickness(0, -100, 0, 0)
- })
- }
- }
- }
- }
- };
- root.Measure(Size.Infinity);
- root.Arrange(new Rect(container.DesiredSize));
- var result = container.GetVisualsAt(new Point(50, 50));
- Assert.Equal(new[] { container }, result);
- }
- }
- [Fact]
- public void GetVisualsAt_Should_Not_Find_Control_Outside_Scroll_Viewport()
- {
- using (TestApplication())
- {
- Border target;
- Border item1;
- Border item2;
- ScrollContentPresenter scroll;
- Panel container;
- var root = new TestRoot
- {
- Child = container = new Panel
- {
- Width = 100,
- Height = 200,
- Background = Brushes.Red,
- Children = new Controls.Controls
- {
- (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()
- {
- Content = new StackPanel()
- {
- Children = new Controls.Controls
- {
- (item1 = new Border()
- {
- Name = "b3",
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- }),
- (item2 = new Border()
- {
- Name = "b4",
- Width = 100,
- Height = 100,
- Background = Brushes.Red,
- }),
- }
- }
- }
- }
- }
- }
- };
- scroll.UpdateChild();
- root.Measure(Size.Infinity);
- root.Arrange(new Rect(container.DesiredSize));
- var result = container.GetVisualsAt(new Point(50, 150)).First();
- Assert.Equal(item1, result);
- result = container.GetVisualsAt(new Point(50, 50)).First();
- Assert.Equal(target, result);
- scroll.Offset = new Vector(0, 100);
- // We don't have LayoutManager set up so do the layout pass manually.
- scroll.Parent.InvalidateArrange();
- container.InvalidateArrange();
- container.Arrange(new Rect(container.DesiredSize));
- result = container.GetVisualsAt(new Point(50, 150)).First();
- Assert.Equal(item2, result);
- result = container.GetVisualsAt(new Point(50, 50)).First();
- Assert.Equal(target, result);
- }
- }
- [Fact]
- public void GetVisualsAt_Should_Not_Find_Path_When_Outside_Fill()
- {
- using (TestApplication())
- {
- Path path;
- var container = new TestRoot
- {
- Width = 200,
- Height = 200,
- Child = path = new Path
- {
- Width = 200,
- Height = 200,
- Fill = Brushes.Red,
- Data = StreamGeometry.Parse("M100,0 L0,100 100,100")
- }
- };
- container.Measure(Size.Infinity);
- container.Arrange(new Rect(container.DesiredSize));
- var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
- var result = container.GetVisualsAt(new Point(100, 100));
- Assert.Equal(new[] { path }, result);
- result = container.GetVisualsAt(new Point(10, 10));
- Assert.Empty(result);
- }
- }
- [Fact]
- public void GetVisualsAt_Should_Respect_Geometry_Clip()
- {
- using (TestApplication())
- {
- Border border;
- Canvas canvas;
- var container = new TestRoot
- {
- Width = 400,
- Height = 400,
- Child = 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),
- }
- }
- };
- container.Measure(Size.Infinity);
- container.Arrange(new Rect(container.DesiredSize));
- Assert.Equal(new Rect(100, 100, 200, 200), border.Bounds);
- var context = new DrawingContext(Mock.Of<IDrawingContextImpl>());
- var result = container.GetVisualsAt(new Point(200, 200));
- Assert.Equal(new IVisual[] { canvas, border }, result);
- result = container.GetVisualsAt(new Point(110, 110));
- Assert.Empty(result);
- }
- }
- private IDisposable TestApplication()
- {
- return UnitTestApplication.Start(
- new TestServices(
- renderInterface: new MockPlatformRenderInterface(),
- renderer: (root, loop) => new ImmediateRenderer(root)));
- }
- }
- }
|