VisualTests.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.Data;
  8. using Avalonia.Media;
  9. using Avalonia.Rendering;
  10. using Avalonia.UnitTests;
  11. using Avalonia.VisualTree;
  12. using Moq;
  13. using Xunit;
  14. namespace Avalonia.Visuals.UnitTests
  15. {
  16. public class VisualTests
  17. {
  18. [Fact]
  19. public void Added_Child_Should_Have_VisualParent_Set()
  20. {
  21. var target = new TestVisual();
  22. var child = new Visual();
  23. target.AddChild(child);
  24. Assert.Equal(target, child.GetVisualParent());
  25. }
  26. [Fact]
  27. public void Added_Child_Should_Notify_VisualParent_Changed()
  28. {
  29. var target = new TestVisual();
  30. var child = new TestVisual();
  31. var parents = new List<IVisual>();
  32. child.GetObservable(Visual.VisualParentProperty).Subscribe(x => parents.Add(x));
  33. target.AddChild(child);
  34. target.RemoveChild(child);
  35. Assert.Equal(new IVisual[] { null, target, null }, parents);
  36. }
  37. [Fact]
  38. public void Removed_Child_Should_Have_VisualParent_Cleared()
  39. {
  40. var target = new TestVisual();
  41. var child = new Visual();
  42. target.AddChild(child);
  43. target.RemoveChild(child);
  44. Assert.Null(child.GetVisualParent());
  45. }
  46. [Fact]
  47. public void Clearing_Children_Should_Clear_VisualParent()
  48. {
  49. var children = new[] { new Visual(), new Visual() };
  50. var target = new TestVisual();
  51. target.AddChildren(children);
  52. target.ClearChildren();
  53. var result = children.Select(x => x.GetVisualParent()).ToList();
  54. Assert.Equal(new Visual[] { null, null }, result);
  55. }
  56. [Fact]
  57. public void Adding_Children_Should_Fire_OnAttachedToVisualTree()
  58. {
  59. var child2 = new Decorator();
  60. var child1 = new Decorator { Child = child2 };
  61. var root = new TestRoot();
  62. var called1 = false;
  63. var called2 = false;
  64. child1.AttachedToVisualTree += (s, e) =>
  65. {
  66. Assert.Equal(e.Parent, root);
  67. Assert.Equal(e.Root, root);
  68. called1 = true;
  69. };
  70. child2.AttachedToVisualTree += (s, e) =>
  71. {
  72. Assert.Equal(e.Parent, root);
  73. Assert.Equal(e.Root, root);
  74. called2 = true;
  75. };
  76. root.Child = child1;
  77. Assert.True(called1);
  78. Assert.True(called2);
  79. }
  80. [Fact]
  81. public void Removing_Children_Should_Fire_OnDetachedFromVisualTree()
  82. {
  83. var child2 = new Decorator();
  84. var child1 = new Decorator { Child = child2 };
  85. var root = new TestRoot();
  86. var called1 = false;
  87. var called2 = false;
  88. root.Child = child1;
  89. child1.DetachedFromVisualTree += (s, e) =>
  90. {
  91. Assert.Equal(e.Parent, root);
  92. Assert.Equal(e.Root, root);
  93. called1 = true;
  94. };
  95. child2.DetachedFromVisualTree += (s, e) =>
  96. {
  97. Assert.Equal(e.Parent, root);
  98. Assert.Equal(e.Root, root);
  99. called2 = true;
  100. };
  101. root.Child = null;
  102. Assert.True(called1);
  103. Assert.True(called2);
  104. }
  105. [Fact]
  106. public void Root_Should_Retun_Self_As_VisualRoot()
  107. {
  108. var root = new TestRoot();
  109. Assert.Same(root, ((IVisual)root).VisualRoot);
  110. }
  111. [Fact]
  112. public void Descendants_Should_RetunVisualRoot()
  113. {
  114. var root = new TestRoot();
  115. var child1 = new Decorator();
  116. var child2 = new Decorator();
  117. root.Child = child1;
  118. child1.Child = child2;
  119. Assert.Same(root, ((IVisual)child1).VisualRoot);
  120. Assert.Same(root, ((IVisual)child2).VisualRoot);
  121. }
  122. [Fact]
  123. public void Attaching_To_Visual_Tree_Should_Invalidate_Visual()
  124. {
  125. var renderer = new Mock<IRenderer>();
  126. var child = new Decorator();
  127. var root = new TestRoot
  128. {
  129. Renderer = renderer.Object,
  130. };
  131. root.Child = child;
  132. renderer.Verify(x => x.AddDirty(child));
  133. }
  134. [Fact]
  135. public void Detaching_From_Visual_Tree_Should_Invalidate_Visual()
  136. {
  137. var renderer = new Mock<IRenderer>();
  138. var child = new Decorator();
  139. var root = new TestRoot
  140. {
  141. Renderer = renderer.Object,
  142. };
  143. root.Child = child;
  144. renderer.ResetCalls();
  145. root.Child = null;
  146. renderer.Verify(x => x.AddDirty(child));
  147. }
  148. [Fact]
  149. public void Adding_Already_Parented_Control_Should_Throw()
  150. {
  151. var root1 = new TestRoot();
  152. var root2 = new TestRoot();
  153. var child = new Canvas();
  154. root1.Child = child;
  155. Assert.Throws<InvalidOperationException>(() => root2.Child = child);
  156. Assert.Empty(root2.GetVisualChildren());
  157. }
  158. [Fact]
  159. public void TransformToVisual_Should_Work()
  160. {
  161. var child = new Decorator { Width = 100, Height = 100 };
  162. var root = new TestRoot() { Child = child, Width = 400, Height = 400 };
  163. root.Measure(Size.Infinity);
  164. root.Arrange(new Rect(new Point(), root.DesiredSize));
  165. var tr = child.TransformToVisual(root);
  166. Assert.NotNull(tr);
  167. var point = root.Bounds.TopLeft * tr;
  168. //child is centered (400 - 100)/2
  169. Assert.Equal(new Point(150, 150), point);
  170. }
  171. [Fact]
  172. public void TransformToVisual_With_RenderTransform_Should_Work()
  173. {
  174. var child = new Decorator
  175. {
  176. Width = 100,
  177. Height = 100,
  178. RenderTransform = new ScaleTransform() { ScaleX = 2, ScaleY = 2 }
  179. };
  180. var root = new TestRoot() { Child = child, Width = 400, Height = 400 };
  181. root.Measure(Size.Infinity);
  182. root.Arrange(new Rect(new Point(), root.DesiredSize));
  183. var tr = child.TransformToVisual(root);
  184. Assert.NotNull(tr);
  185. var point = root.Bounds.TopLeft * tr;
  186. //child is centered (400 - 100*2 scale)/2
  187. Assert.Equal(new Point(100, 100), point);
  188. }
  189. [Fact]
  190. public void Should_Not_Log_Binding_Error_When_Not_Attached_To_Logical_Tree()
  191. {
  192. var target = new Decorator { DataContext = "foo" };
  193. var called = false;
  194. LogCallback checkLogMessage = (level, area, src, mt, pv) =>
  195. {
  196. if (level >= Logging.LogEventLevel.Warning)
  197. {
  198. called = true;
  199. }
  200. };
  201. using (TestLogSink.Start(checkLogMessage))
  202. {
  203. target.Bind(Decorator.TagProperty, new Binding("Foo"));
  204. }
  205. Assert.False(called);
  206. }
  207. [Fact]
  208. public void Should_Log_Binding_Error_When_Attached_To_Logical_Tree()
  209. {
  210. var target = new Decorator();
  211. var root = new TestRoot { Child = target, DataContext = "foo" };
  212. var called = false;
  213. LogCallback checkLogMessage = (level, area, src, mt, pv) =>
  214. {
  215. if (level >= Logging.LogEventLevel.Warning)
  216. {
  217. called = true;
  218. }
  219. };
  220. using (TestLogSink.Start(checkLogMessage))
  221. {
  222. target.Bind(Decorator.TagProperty, new Binding("Foo"));
  223. }
  224. Assert.True(called);
  225. }
  226. }
  227. }