DeferredDrawingContextImplTests.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Linq;
  3. using Avalonia.Media;
  4. using Avalonia.Rendering.SceneGraph;
  5. using Avalonia.UnitTests;
  6. using Avalonia.VisualTree;
  7. using Moq;
  8. using Xunit;
  9. namespace Avalonia.Visuals.UnitTests.Rendering.SceneGraph
  10. {
  11. public class DeferredDrawingContextImplTests
  12. {
  13. [Fact]
  14. public void Should_Add_VisualNode()
  15. {
  16. var parent = new VisualNode(new TestRoot(), null);
  17. var child = new VisualNode(Mock.Of<IVisual>(), null);
  18. var layers = new SceneLayers(parent.Visual);
  19. var target = new DeferredDrawingContextImpl(null, layers);
  20. target.BeginUpdate(parent);
  21. target.BeginUpdate(child);
  22. Assert.Equal(1, parent.Children.Count);
  23. Assert.Same(child, parent.Children[0]);
  24. }
  25. [Fact]
  26. public void Should_Not_Replace_Identical_VisualNode()
  27. {
  28. var parent = new VisualNode(new TestRoot(), null);
  29. var child = new VisualNode(Mock.Of<IVisual>(), null);
  30. var layers = new SceneLayers(parent.Visual);
  31. parent.AddChild(child);
  32. var target = new DeferredDrawingContextImpl(null, layers);
  33. target.BeginUpdate(parent);
  34. target.BeginUpdate(child);
  35. Assert.Equal(1, parent.Children.Count);
  36. Assert.Same(child, parent.Children[0]);
  37. }
  38. [Fact]
  39. public void Should_Replace_Different_VisualNode()
  40. {
  41. var parent = new VisualNode(new TestRoot(), null);
  42. var child1 = new VisualNode(Mock.Of<IVisual>(), null);
  43. var child2 = new VisualNode(Mock.Of<IVisual>(), null);
  44. var layers = new SceneLayers(parent.Visual);
  45. parent.AddChild(child1);
  46. var target = new DeferredDrawingContextImpl(null, layers);
  47. target.BeginUpdate(parent);
  48. target.BeginUpdate(child2);
  49. Assert.Equal(1, parent.Children.Count);
  50. Assert.Same(child2, parent.Children[0]);
  51. }
  52. [Fact]
  53. public void TrimChildren_Should_Trim_Children()
  54. {
  55. var root = new TestRoot();
  56. var node = new VisualNode(root, null) { LayerRoot = root };
  57. node.AddChild(new VisualNode(Mock.Of<IVisual>(), node) { LayerRoot = root });
  58. node.AddChild(new VisualNode(Mock.Of<IVisual>(), node) { LayerRoot = root });
  59. node.AddChild(new VisualNode(Mock.Of<IVisual>(), node) { LayerRoot = root });
  60. node.AddChild(new VisualNode(Mock.Of<IVisual>(), node) { LayerRoot = root });
  61. var layers = new SceneLayers(root);
  62. var target = new DeferredDrawingContextImpl(null, layers);
  63. var child1 = new VisualNode(Mock.Of<IVisual>(), null) { LayerRoot = root };
  64. var child2 = new VisualNode(Mock.Of<IVisual>(), null) { LayerRoot = root };
  65. target.BeginUpdate(node);
  66. using (target.BeginUpdate(child1)) { }
  67. using (target.BeginUpdate(child2)) { }
  68. target.TrimChildren();
  69. Assert.Equal(2, node.Children.Count);
  70. }
  71. [Fact]
  72. public void Should_Add_DrawOperations()
  73. {
  74. var node = new VisualNode(new TestRoot(), null);
  75. var layers = new SceneLayers(node.Visual);
  76. var target = new DeferredDrawingContextImpl(null, layers);
  77. node.LayerRoot = node.Visual;
  78. using (target.BeginUpdate(node))
  79. {
  80. target.FillRectangle(Brushes.Red, new Rect(0, 0, 100, 100));
  81. target.DrawRectangle(new Pen(Brushes.Green, 1), new Rect(0, 0, 100, 100));
  82. }
  83. Assert.Equal(2, node.DrawOperations.Count);
  84. Assert.IsType<RectangleNode>(node.DrawOperations[0]);
  85. Assert.IsType<RectangleNode>(node.DrawOperations[1]);
  86. }
  87. [Fact]
  88. public void Should_Not_Replace_Identical_DrawOperation()
  89. {
  90. var node = new VisualNode(new TestRoot(), null);
  91. var operation = new RectangleNode(Matrix.Identity, Brushes.Red, null, new Rect(0, 0, 100, 100), 0);
  92. var layers = new SceneLayers(node.Visual);
  93. var target = new DeferredDrawingContextImpl(null, layers);
  94. node.LayerRoot = node.Visual;
  95. node.AddDrawOperation(operation);
  96. using (target.BeginUpdate(node))
  97. {
  98. target.FillRectangle(Brushes.Red, new Rect(0, 0, 100, 100));
  99. }
  100. Assert.Equal(1, node.DrawOperations.Count);
  101. Assert.Same(operation, node.DrawOperations.Single());
  102. Assert.IsType<RectangleNode>(node.DrawOperations[0]);
  103. }
  104. [Fact]
  105. public void Should_Replace_Different_DrawOperation()
  106. {
  107. var node = new VisualNode(new TestRoot(), null);
  108. var operation = new RectangleNode(Matrix.Identity, Brushes.Red, null, new Rect(0, 0, 100, 100), 0);
  109. var layers = new SceneLayers(node.Visual);
  110. var target = new DeferredDrawingContextImpl(null, layers);
  111. node.LayerRoot = node.Visual;
  112. node.AddDrawOperation(operation);
  113. using (target.BeginUpdate(node))
  114. {
  115. target.FillRectangle(Brushes.Green, new Rect(0, 0, 100, 100));
  116. }
  117. Assert.Equal(1, node.DrawOperations.Count);
  118. Assert.NotSame(operation, node.DrawOperations.Single());
  119. Assert.IsType<RectangleNode>(node.DrawOperations[0]);
  120. }
  121. [Fact]
  122. public void Should_Update_DirtyRects()
  123. {
  124. var node = new VisualNode(new TestRoot(), null);
  125. var operation = new RectangleNode(Matrix.Identity, Brushes.Red, null, new Rect(0, 0, 100, 100), 0);
  126. var layers = new SceneLayers(node.Visual);
  127. var target = new DeferredDrawingContextImpl(null, layers);
  128. node.LayerRoot = node.Visual;
  129. using (target.BeginUpdate(node))
  130. {
  131. target.FillRectangle(Brushes.Green, new Rect(0, 0, 100, 100));
  132. }
  133. Assert.Equal(new Rect(0, 0, 100, 100), layers.Single().Dirty.Single());
  134. }
  135. [Fact]
  136. public void Should_Trim_DrawOperations()
  137. {
  138. var node = new VisualNode(new TestRoot(), null);
  139. node.LayerRoot = node.Visual;
  140. node.AddDrawOperation(new RectangleNode(Matrix.Identity, Brushes.Red, null, new Rect(0, 0, 10, 100), 0));
  141. node.AddDrawOperation(new RectangleNode(Matrix.Identity, Brushes.Red, null, new Rect(0, 0, 20, 100), 0));
  142. node.AddDrawOperation(new RectangleNode(Matrix.Identity, Brushes.Red, null, new Rect(0, 0, 30, 100), 0));
  143. node.AddDrawOperation(new RectangleNode(Matrix.Identity, Brushes.Red, null, new Rect(0, 0, 40, 100), 0));
  144. var layers = new SceneLayers(node.Visual);
  145. var target = new DeferredDrawingContextImpl(null, layers);
  146. using (target.BeginUpdate(node))
  147. {
  148. target.FillRectangle(Brushes.Green, new Rect(0, 0, 10, 100));
  149. target.FillRectangle(Brushes.Blue, new Rect(0, 0, 20, 100));
  150. }
  151. Assert.Equal(2, node.DrawOperations.Count);
  152. }
  153. }
  154. }