| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- using System;
- using System.Linq;
- using Avalonia.Controls;
- using Avalonia.Media;
- using Avalonia.Rendering.SceneGraph;
- using Avalonia.UnitTests;
- using Avalonia.VisualTree;
- using Xunit;
- namespace Avalonia.Visuals.UnitTests.Rendering.SceneGraph
- {
- public class SceneBuilderTests
- {
- [Fact]
- public void Should_Build_Initial_Scene()
- {
- using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
- {
- Border border;
- TextBlock textBlock;
- var tree = new TestRoot
- {
- Child = border = new Border
- {
- Background = Brushes.Red,
- Child = textBlock = new TextBlock
- {
- Text = "Hello World",
- }
- }
- };
- var initial = new Scene(tree);
- var result = SceneBuilder.Update(initial);
- Assert.NotSame(initial, result);
- Assert.Equal(1, result.Root.Children.Count);
- var borderNode = (VisualNode)result.Root.Children[0];
- Assert.Same(borderNode, result.FindNode(border));
- Assert.Same(border, borderNode.Visual);
- Assert.Equal(2, borderNode.Children.Count);
- var backgroundNode = (RectangleNode)borderNode.Children[0];
- Assert.Equal(Brushes.Red, backgroundNode.Brush);
- var textBlockNode = (VisualNode)borderNode.Children[1];
- Assert.Same(textBlockNode, result.FindNode(textBlock));
- Assert.Same(textBlock, textBlockNode.Visual);
- Assert.Equal(1, textBlockNode.Children.Count);
- var textNode = (TextNode)textBlockNode.Children[0];
- Assert.NotNull(textNode.Text);
- }
- }
- [Fact]
- public void Should_Respect_ZIndex()
- {
- using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
- {
- Border front;
- Border back;
- var tree = new TestRoot
- {
- Child = new Panel
- {
- Children =
- {
- (front = new Border
- {
- ZIndex = 1,
- }),
- (back = new Border
- {
- ZIndex = 0,
- }),
- }
- }
- };
- var result = SceneBuilder.Update(new Scene(tree));
- var panelNode = result.FindNode(tree.Child);
- var expected = new IVisual[] { back, front };
- var actual = panelNode.Children.OfType<IVisualNode>().Select(x => x.Visual).ToArray();
- Assert.Equal(expected, actual);
- }
- }
- [Fact]
- public void ClipBounds_Should_Be_In_Global_Coordinates()
- {
- using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
- {
- Border target;
- var tree = new TestRoot
- {
- Child = new Decorator
- {
- Margin = new Thickness(24, 26),
- Child = target = new Border
- {
- Margin = new Thickness(26, 24),
- Width = 100,
- Height = 100,
- }
- }
- };
- tree.Measure(Size.Infinity);
- tree.Arrange(new Rect(tree.DesiredSize));
- var result = SceneBuilder.Update(new Scene(tree));
- var targetNode = result.FindNode(target);
- Assert.Equal(new Rect(50, 50, 100, 100), targetNode.ClipBounds);
- }
- }
- [Fact]
- public void Should_Update_Border_Background_Node()
- {
- using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
- {
- Border border;
- TextBlock textBlock;
- var tree = new TestRoot
- {
- Child = border = new Border
- {
- Background = Brushes.Red,
- Child = textBlock = new TextBlock
- {
- Text = "Hello World",
- }
- }
- };
- var initial = SceneBuilder.Update(new Scene(tree));
- var initialBackgroundNode = initial.FindNode(border).Children[0];
- var initialTextNode = initial.FindNode(textBlock).Children[0];
- Assert.NotNull(initialBackgroundNode);
- Assert.NotNull(initialTextNode);
- border.Background = Brushes.Green;
- var result = SceneBuilder.Update(initial);
- Assert.NotSame(initial, result);
- var borderNode = (VisualNode)result.Root.Children[0];
- Assert.Same(border, borderNode.Visual);
- var backgroundNode = (RectangleNode)borderNode.Children[0];
- Assert.NotSame(initialBackgroundNode, backgroundNode);
- Assert.Equal(Brushes.Green, backgroundNode.Brush);
- var textBlockNode = (VisualNode)borderNode.Children[1];
- Assert.Same(textBlock, textBlockNode.Visual);
- var textNode = (TextNode)textBlockNode.Children[0];
- Assert.Same(initialTextNode, textNode);
- }
- }
- [Fact]
- public void Should_Update_When_Control_Removed()
- {
- using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
- {
- Border border;
- TextBlock textBlock;
- var tree = new TestRoot
- {
- Child = border = new Border
- {
- Background = Brushes.Red,
- Child = textBlock = new TextBlock
- {
- Text = "Hello World",
- }
- }
- };
- var initial = SceneBuilder.Update(new Scene(tree));
- border.Child = null;
- var result = SceneBuilder.Update(initial);
- Assert.NotSame(initial, result);
- var borderNode = (VisualNode)result.Root.Children[0];
- Assert.Equal(1, borderNode.Children.Count);
- }
- }
- }
- }
|