SceneBuilderTests_Layers.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. using Avalonia.Layout;
  10. using System.Reactive.Subjects;
  11. using Avalonia.Data;
  12. namespace Avalonia.Visuals.UnitTests.Rendering.SceneGraph
  13. {
  14. public partial class SceneBuilderTests
  15. {
  16. [Fact]
  17. public void Control_With_Animated_Opacity_Should_Start_New_Layer()
  18. {
  19. using (TestApplication())
  20. {
  21. Decorator decorator;
  22. Border border;
  23. Canvas canvas;
  24. var tree = new TestRoot
  25. {
  26. Padding = new Thickness(10),
  27. Width = 100,
  28. Height = 120,
  29. Child = decorator = new Decorator
  30. {
  31. Padding = new Thickness(11),
  32. Child = border = new Border
  33. {
  34. Background = Brushes.Red,
  35. Padding = new Thickness(12),
  36. Child = canvas = new Canvas(),
  37. }
  38. }
  39. };
  40. var layout = AvaloniaLocator.Current.GetService<ILayoutManager>();
  41. layout.ExecuteInitialLayoutPass(tree);
  42. var animation = new BehaviorSubject<double>(0.5);
  43. border.Bind(Border.OpacityProperty, animation, BindingPriority.Animation);
  44. var scene = new Scene(tree);
  45. var sceneBuilder = new SceneBuilder();
  46. sceneBuilder.UpdateAll(scene);
  47. var rootNode = (VisualNode)scene.Root;
  48. var borderNode = (VisualNode)scene.FindNode(border);
  49. var canvasNode = (VisualNode)scene.FindNode(canvas);
  50. Assert.Same(tree, rootNode.LayerRoot);
  51. Assert.Same(border, borderNode.LayerRoot);
  52. Assert.Same(border, canvasNode.LayerRoot);
  53. Assert.Equal(0.5, scene.Layers[border].Opacity);
  54. Assert.Equal(2, scene.Layers.Count());
  55. Assert.Empty(scene.Layers.Select(x => x.LayerRoot).Except(new IVisual[] { tree, border }));
  56. animation.OnCompleted();
  57. scene = scene.Clone();
  58. sceneBuilder.Update(scene, border);
  59. rootNode = (VisualNode)scene.Root;
  60. borderNode = (VisualNode)scene.FindNode(border);
  61. canvasNode = (VisualNode)scene.FindNode(canvas);
  62. Assert.Same(tree, rootNode.LayerRoot);
  63. Assert.Same(tree, borderNode.LayerRoot);
  64. Assert.Same(tree, canvasNode.LayerRoot);
  65. Assert.Single(scene.Layers);
  66. var rootDirty = scene.Layers[tree].Dirty;
  67. Assert.Single(rootDirty);
  68. Assert.Equal(new Rect(21, 21, 58, 78), rootDirty.Single());
  69. }
  70. }
  71. [Fact]
  72. public void Removing_Control_With_Animated_Opacity_Should_Remove_Layers()
  73. {
  74. using (TestApplication())
  75. {
  76. Decorator decorator;
  77. Border border;
  78. Canvas canvas;
  79. var tree = new TestRoot
  80. {
  81. Padding = new Thickness(10),
  82. Width = 100,
  83. Height = 120,
  84. Child = decorator = new Decorator
  85. {
  86. Padding = new Thickness(11),
  87. Child = border = new Border
  88. {
  89. Background = Brushes.Red,
  90. Padding = new Thickness(12),
  91. Child = canvas = new Canvas()
  92. }
  93. }
  94. };
  95. var layout = AvaloniaLocator.Current.GetService<ILayoutManager>();
  96. layout.ExecuteInitialLayoutPass(tree);
  97. var animation = new BehaviorSubject<double>(0.5);
  98. border.Bind(Border.OpacityProperty, animation, BindingPriority.Animation);
  99. canvas.Bind(Canvas.OpacityProperty, animation, BindingPriority.Animation);
  100. var scene = new Scene(tree);
  101. var sceneBuilder = new SceneBuilder();
  102. sceneBuilder.UpdateAll(scene);
  103. Assert.Equal(3, scene.Layers.Count);
  104. decorator.Child = null;
  105. scene = scene.Clone();
  106. sceneBuilder.Update(scene, border);
  107. Assert.Equal(1, scene.Layers.Count);
  108. }
  109. }
  110. [Fact]
  111. public void Hiding_Transparent_Control_Should_Remove_Layers()
  112. {
  113. using (TestApplication())
  114. {
  115. Decorator decorator;
  116. Border border;
  117. Canvas canvas;
  118. var tree = new TestRoot
  119. {
  120. Padding = new Thickness(10),
  121. Width = 100,
  122. Height = 120,
  123. Child = decorator = new Decorator
  124. {
  125. Padding = new Thickness(11),
  126. Child = border = new Border
  127. {
  128. Background = Brushes.Red,
  129. Padding = new Thickness(12),
  130. Child = canvas = new Canvas(),
  131. }
  132. }
  133. };
  134. var layout = AvaloniaLocator.Current.GetService<ILayoutManager>();
  135. layout.ExecuteInitialLayoutPass(tree);
  136. var animation = new BehaviorSubject<double>(0.5);
  137. border.Bind(Border.OpacityProperty, animation, BindingPriority.Animation);
  138. canvas.Bind(Canvas.OpacityProperty, animation, BindingPriority.Animation);
  139. var scene = new Scene(tree);
  140. var sceneBuilder = new SceneBuilder();
  141. sceneBuilder.UpdateAll(scene);
  142. Assert.Equal(3, scene.Layers.Count);
  143. border.IsVisible = false;
  144. scene = scene.Clone();
  145. sceneBuilder.Update(scene, border);
  146. Assert.Equal(1, scene.Layers.Count);
  147. }
  148. }
  149. [Fact]
  150. public void GeometryClip_Should_Affect_Child_Layers()
  151. {
  152. using (TestApplication())
  153. {
  154. var clip = StreamGeometry.Parse("M100,0 L0,100 100,100");
  155. Decorator decorator;
  156. Border border;
  157. var tree = new TestRoot
  158. {
  159. Child = decorator = new Decorator
  160. {
  161. Clip = clip,
  162. Margin = new Thickness(12, 16),
  163. Child = border = new Border
  164. {
  165. Opacity = 0.5,
  166. }
  167. }
  168. };
  169. var layout = AvaloniaLocator.Current.GetService<ILayoutManager>();
  170. layout.ExecuteInitialLayoutPass(tree);
  171. var animation = new BehaviorSubject<double>(0.5);
  172. border.Bind(Border.OpacityProperty, animation, BindingPriority.Animation);
  173. var scene = new Scene(tree);
  174. var sceneBuilder = new SceneBuilder();
  175. sceneBuilder.UpdateAll(scene);
  176. var borderLayer = scene.Layers[border];
  177. Assert.Equal(
  178. Matrix.CreateTranslation(12, 16),
  179. ((MockStreamGeometryImpl)borderLayer.GeometryClip).Transform);
  180. }
  181. }
  182. }
  183. }