VisualExtensionsTests.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using Avalonia.Controls;
  2. using Avalonia.Layout;
  3. using Avalonia.Media;
  4. using Avalonia.UnitTests;
  5. using Avalonia.VisualTree;
  6. using Xunit;
  7. namespace Avalonia.Base.UnitTests
  8. {
  9. public class VisualExtensionsTests
  10. {
  11. [Fact]
  12. public void FindAncestorOfType_Finds_Direct_Parent()
  13. {
  14. StackPanel target;
  15. var root = new TestRoot
  16. {
  17. Child = target = new StackPanel()
  18. };
  19. Assert.Equal(root, target.FindAncestorOfType<TestRoot>());
  20. }
  21. [Fact]
  22. public void FindAncestorOfType_Finds_Ancestor_Of_Nested_Child()
  23. {
  24. Button target;
  25. var root = new TestRoot
  26. {
  27. Child = new StackPanel
  28. {
  29. Children =
  30. {
  31. new StackPanel
  32. {
  33. Children =
  34. {
  35. (target = new Button())
  36. }
  37. }
  38. }
  39. }
  40. };
  41. Assert.Equal(root, target.FindAncestorOfType<TestRoot>());
  42. }
  43. [Fact]
  44. public void FindDescendantOfType_Finds_Direct_Child()
  45. {
  46. StackPanel target;
  47. var root = new TestRoot
  48. {
  49. Child = target = new StackPanel()
  50. };
  51. Assert.Equal(target, root.FindDescendantOfType<StackPanel>());
  52. }
  53. [Fact]
  54. public void FindDescendantOfType_Finds_Nested_Child()
  55. {
  56. Button target;
  57. var root = new TestRoot
  58. {
  59. Child = new StackPanel
  60. {
  61. Children =
  62. {
  63. new StackPanel
  64. {
  65. Children =
  66. {
  67. (target = new Button())
  68. }
  69. }
  70. }
  71. }
  72. };
  73. Assert.Equal(target, root.FindDescendantOfType<Button>());
  74. }
  75. [Fact]
  76. public void FindCommonVisualAncestor_First_Is_Parent_Of_Second()
  77. {
  78. Control left, right;
  79. var root = new TestRoot
  80. {
  81. Child = left = new Decorator
  82. {
  83. Child = right = new Decorator()
  84. }
  85. };
  86. var ancestor = left.FindCommonVisualAncestor(right);
  87. Assert.Equal(left, ancestor);
  88. ancestor = right.FindCommonVisualAncestor(left);
  89. Assert.Equal(left, ancestor);
  90. }
  91. [Fact]
  92. public void FindCommonVisualAncestor_Two_Subtrees_Uniform_Height()
  93. {
  94. Control left, right;
  95. var root = new TestRoot
  96. {
  97. Child = new StackPanel
  98. {
  99. Children =
  100. {
  101. new Decorator
  102. {
  103. Child = new Decorator
  104. {
  105. Child = left = new Decorator()
  106. }
  107. },
  108. new Decorator
  109. {
  110. Child = new Decorator
  111. {
  112. Child = right = new Decorator()
  113. }
  114. }
  115. }
  116. }
  117. };
  118. var ancestor = left.FindCommonVisualAncestor(right);
  119. Assert.Equal(root.Child, ancestor);
  120. ancestor = right.FindCommonVisualAncestor(left);
  121. Assert.Equal(root.Child, ancestor);
  122. }
  123. [Fact]
  124. public void FindCommonVisualAncestor_Two_Subtrees_NonUniform_Height()
  125. {
  126. Control left, right;
  127. var root = new TestRoot
  128. {
  129. Child = new StackPanel
  130. {
  131. Children =
  132. {
  133. new Decorator
  134. {
  135. Child = new Decorator
  136. {
  137. Child = left = new Decorator()
  138. }
  139. },
  140. new Decorator
  141. {
  142. Child = new Decorator
  143. {
  144. Child = new Decorator
  145. {
  146. Child = right = new Decorator()
  147. }
  148. }
  149. }
  150. }
  151. }
  152. };
  153. var ancestor = left.FindCommonVisualAncestor(right);
  154. Assert.Equal(root.Child, ancestor);
  155. ancestor = right.FindCommonVisualAncestor(left);
  156. Assert.Equal(root.Child, ancestor);
  157. }
  158. [Fact]
  159. public void TranslatePoint_Should_Respect_RenderTransforms()
  160. {
  161. Border target;
  162. var root = new TestRoot
  163. {
  164. Width = 100,
  165. Height = 100,
  166. Child = new Decorator
  167. {
  168. Width = 50,
  169. Height = 50,
  170. HorizontalAlignment = HorizontalAlignment.Center,
  171. VerticalAlignment = VerticalAlignment.Center,
  172. RenderTransform = new TranslateTransform(25, 25),
  173. Child = target = new Border(),
  174. }
  175. };
  176. root.Measure(Size.Infinity);
  177. root.Arrange(new Rect(root.DesiredSize));
  178. var result = target.TranslatePoint(new Point(0, 0), root);
  179. Assert.Equal(new Point(50, 50), result);
  180. }
  181. }
  182. }