VisualTests.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Avalonia.Controls;
  7. using Avalonia.Rendering;
  8. using Avalonia.UnitTests;
  9. using Avalonia.VisualTree;
  10. using Moq;
  11. using Xunit;
  12. namespace Avalonia.Visuals.UnitTests
  13. {
  14. public class VisualTests
  15. {
  16. [Fact]
  17. public void Added_Child_Should_Have_VisualParent_Set()
  18. {
  19. var target = new TestVisual();
  20. var child = new Visual();
  21. target.AddChild(child);
  22. Assert.Equal(target, child.GetVisualParent());
  23. }
  24. [Fact]
  25. public void Added_Child_Should_Notify_VisualParent_Changed()
  26. {
  27. var target = new TestVisual();
  28. var child = new TestVisual();
  29. var parents = new List<IVisual>();
  30. child.GetObservable(Visual.VisualParentProperty).Subscribe(x => parents.Add(x));
  31. target.AddChild(child);
  32. target.RemoveChild(child);
  33. Assert.Equal(new IVisual[] { null, target, null }, parents);
  34. }
  35. [Fact]
  36. public void Removed_Child_Should_Have_VisualParent_Cleared()
  37. {
  38. var target = new TestVisual();
  39. var child = new Visual();
  40. target.AddChild(child);
  41. target.RemoveChild(child);
  42. Assert.Null(child.GetVisualParent());
  43. }
  44. [Fact]
  45. public void Clearing_Children_Should_Clear_VisualParent()
  46. {
  47. var children = new[] { new Visual(), new Visual() };
  48. var target = new TestVisual();
  49. target.AddChildren(children);
  50. target.ClearChildren();
  51. var result = children.Select(x => x.GetVisualParent()).ToList();
  52. Assert.Equal(new Visual[] { null, null }, result);
  53. }
  54. [Fact]
  55. public void Adding_Children_Should_Fire_OnAttachedToVisualTree()
  56. {
  57. var child2 = new Decorator();
  58. var child1 = new Decorator { Child = child2 };
  59. var root = new TestRoot();
  60. var called1 = false;
  61. var called2 = false;
  62. child1.AttachedToVisualTree += (s, e) =>
  63. {
  64. Assert.Equal(e.Parent, root);
  65. Assert.Equal(e.Root, root);
  66. called1 = true;
  67. };
  68. child2.AttachedToVisualTree += (s, e) =>
  69. {
  70. Assert.Equal(e.Parent, root);
  71. Assert.Equal(e.Root, root);
  72. called2 = true;
  73. };
  74. root.Child = child1;
  75. Assert.True(called1);
  76. Assert.True(called2);
  77. }
  78. [Fact]
  79. public void Removing_Children_Should_Fire_OnDetachedFromVisualTree()
  80. {
  81. var child2 = new Decorator();
  82. var child1 = new Decorator { Child = child2 };
  83. var root = new TestRoot();
  84. var called1 = false;
  85. var called2 = false;
  86. root.Child = child1;
  87. child1.DetachedFromVisualTree += (s, e) =>
  88. {
  89. Assert.Equal(e.Parent, root);
  90. Assert.Equal(e.Root, root);
  91. called1 = true;
  92. };
  93. child2.DetachedFromVisualTree += (s, e) =>
  94. {
  95. Assert.Equal(e.Parent, root);
  96. Assert.Equal(e.Root, root);
  97. called2 = true;
  98. };
  99. root.Child = null;
  100. Assert.True(called1);
  101. Assert.True(called2);
  102. }
  103. [Fact]
  104. public void Root_Should_Retun_Self_As_VisualRoot()
  105. {
  106. var root = new TestRoot();
  107. Assert.Same(root, ((IVisual)root).VisualRoot);
  108. }
  109. [Fact]
  110. public void Descendents_Should_RetunVisualRoot()
  111. {
  112. var root = new TestRoot();
  113. var child1 = new Decorator();
  114. var child2 = new Decorator();
  115. root.Child = child1;
  116. child1.Child = child2;
  117. Assert.Same(root, ((IVisual)child1).VisualRoot);
  118. Assert.Same(root, ((IVisual)child2).VisualRoot);
  119. }
  120. [Fact]
  121. public void Attaching_To_Visual_Tree_Should_Invalidate_Visual()
  122. {
  123. var renderer = new Mock<IRenderer>();
  124. using (UnitTestApplication.Start(new TestServices(renderer: (root, loop) => renderer.Object)))
  125. {
  126. var child = new Decorator();
  127. var root = new TestRoot();
  128. root.Child = child;
  129. renderer.Verify(x => x.AddDirty(child));
  130. }
  131. }
  132. [Fact]
  133. public void Detaching_From_Visual_Tree_Should_Invalidate_Visual()
  134. {
  135. var renderer = new Mock<IRenderer>();
  136. using (UnitTestApplication.Start(new TestServices(renderer: (root, loop) => renderer.Object)))
  137. {
  138. var child = new Decorator();
  139. var root = new TestRoot();
  140. root.Child = child;
  141. renderer.ResetCalls();
  142. root.Child = null;
  143. renderer.Verify(x => x.AddDirty(child));
  144. }
  145. }
  146. [Fact]
  147. public void Adding_Already_Parented_Control_Should_Throw()
  148. {
  149. var root1 = new TestRoot();
  150. var root2 = new TestRoot();
  151. var child = new Canvas();
  152. root1.Child = child;
  153. Assert.Throws<InvalidOperationException>(() => root2.Child = child);
  154. Assert.Equal(0, root2.GetVisualChildren().Count());
  155. }
  156. }
  157. }