SceneBuilderTests.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Linq;
  3. using Avalonia.Controls;
  4. using Avalonia.Media;
  5. using Avalonia.Rendering.SceneGraph;
  6. using Avalonia.UnitTests;
  7. using Avalonia.VisualTree;
  8. using Xunit;
  9. namespace Avalonia.Visuals.UnitTests.Rendering.SceneGraph
  10. {
  11. public class SceneBuilderTests
  12. {
  13. [Fact]
  14. public void Should_Build_Initial_Scene()
  15. {
  16. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  17. {
  18. Border border;
  19. TextBlock textBlock;
  20. var tree = new TestRoot
  21. {
  22. Child = border = new Border
  23. {
  24. Background = Brushes.Red,
  25. Child = textBlock = new TextBlock
  26. {
  27. Text = "Hello World",
  28. }
  29. }
  30. };
  31. var initial = new Scene(tree);
  32. var result = SceneBuilder.Update(initial);
  33. Assert.NotSame(initial, result);
  34. Assert.Equal(1, result.Root.Children.Count);
  35. var borderNode = (VisualNode)result.Root.Children[0];
  36. Assert.Same(borderNode, result.FindNode(border));
  37. Assert.Same(border, borderNode.Visual);
  38. Assert.Equal(2, borderNode.Children.Count);
  39. var backgroundNode = (RectangleNode)borderNode.Children[0];
  40. Assert.Equal(Brushes.Red, backgroundNode.Brush);
  41. var textBlockNode = (VisualNode)borderNode.Children[1];
  42. Assert.Same(textBlockNode, result.FindNode(textBlock));
  43. Assert.Same(textBlock, textBlockNode.Visual);
  44. Assert.Equal(1, textBlockNode.Children.Count);
  45. var textNode = (TextNode)textBlockNode.Children[0];
  46. Assert.NotNull(textNode.Text);
  47. }
  48. }
  49. [Fact]
  50. public void Should_Respect_ZIndex()
  51. {
  52. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  53. {
  54. Border front;
  55. Border back;
  56. var tree = new TestRoot
  57. {
  58. Child = new Panel
  59. {
  60. Children =
  61. {
  62. (front = new Border
  63. {
  64. ZIndex = 1,
  65. }),
  66. (back = new Border
  67. {
  68. ZIndex = 0,
  69. }),
  70. }
  71. }
  72. };
  73. var result = SceneBuilder.Update(new Scene(tree));
  74. var panelNode = result.FindNode(tree.Child);
  75. var expected = new IVisual[] { back, front };
  76. var actual = panelNode.Children.OfType<IVisualNode>().Select(x => x.Visual).ToArray();
  77. Assert.Equal(expected, actual);
  78. }
  79. }
  80. [Fact]
  81. public void ClipBounds_Should_Be_In_Global_Coordinates()
  82. {
  83. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  84. {
  85. Border target;
  86. var tree = new TestRoot
  87. {
  88. Child = new Decorator
  89. {
  90. Margin = new Thickness(24, 26),
  91. Child = target = new Border
  92. {
  93. Margin = new Thickness(26, 24),
  94. Width = 100,
  95. Height = 100,
  96. }
  97. }
  98. };
  99. tree.Measure(Size.Infinity);
  100. tree.Arrange(new Rect(tree.DesiredSize));
  101. var result = SceneBuilder.Update(new Scene(tree));
  102. var targetNode = result.FindNode(target);
  103. Assert.Equal(new Rect(50, 50, 100, 100), targetNode.ClipBounds);
  104. }
  105. }
  106. [Fact]
  107. public void Should_Update_Border_Background_Node()
  108. {
  109. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  110. {
  111. Border border;
  112. TextBlock textBlock;
  113. var tree = new TestRoot
  114. {
  115. Child = border = new Border
  116. {
  117. Background = Brushes.Red,
  118. Child = textBlock = new TextBlock
  119. {
  120. Text = "Hello World",
  121. }
  122. }
  123. };
  124. var initial = SceneBuilder.Update(new Scene(tree));
  125. var initialBackgroundNode = initial.FindNode(border).Children[0];
  126. var initialTextNode = initial.FindNode(textBlock).Children[0];
  127. Assert.NotNull(initialBackgroundNode);
  128. Assert.NotNull(initialTextNode);
  129. border.Background = Brushes.Green;
  130. var result = SceneBuilder.Update(initial);
  131. Assert.NotSame(initial, result);
  132. var borderNode = (VisualNode)result.Root.Children[0];
  133. Assert.Same(border, borderNode.Visual);
  134. var backgroundNode = (RectangleNode)borderNode.Children[0];
  135. Assert.NotSame(initialBackgroundNode, backgroundNode);
  136. Assert.Equal(Brushes.Green, backgroundNode.Brush);
  137. var textBlockNode = (VisualNode)borderNode.Children[1];
  138. Assert.Same(textBlock, textBlockNode.Visual);
  139. var textNode = (TextNode)textBlockNode.Children[0];
  140. Assert.Same(initialTextNode, textNode);
  141. }
  142. }
  143. [Fact]
  144. public void Should_Update_When_Control_Removed()
  145. {
  146. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  147. {
  148. Border border;
  149. TextBlock textBlock;
  150. var tree = new TestRoot
  151. {
  152. Child = border = new Border
  153. {
  154. Background = Brushes.Red,
  155. Child = textBlock = new TextBlock
  156. {
  157. Text = "Hello World",
  158. }
  159. }
  160. };
  161. var initial = SceneBuilder.Update(new Scene(tree));
  162. border.Child = null;
  163. var result = SceneBuilder.Update(initial);
  164. Assert.NotSame(initial, result);
  165. var borderNode = (VisualNode)result.Root.Children[0];
  166. Assert.Equal(1, borderNode.Children.Count);
  167. }
  168. }
  169. }
  170. }