LayoutTransformControlTests.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using Avalonia.Controls.Presenters;
  2. using Avalonia.Controls.Shapes;
  3. using Avalonia.Controls.Templates;
  4. using Avalonia.Media;
  5. using Xunit;
  6. namespace Avalonia.Controls.UnitTests
  7. {
  8. public class LayoutTransformControlTests
  9. {
  10. [Fact]
  11. public void Measure_On_Scale_x2_Is_Correct()
  12. {
  13. double scale = 2;
  14. TransformMeasureSizeTest(
  15. new Size(100, 50),
  16. new ScaleTransform() { ScaleX = scale, ScaleY = scale },
  17. new Size(200, 100));
  18. }
  19. [Fact]
  20. public void Measure_On_Scale_x0_5_Is_Correct()
  21. {
  22. double scale = 0.5;
  23. TransformMeasureSizeTest(
  24. new Size(100, 50),
  25. new ScaleTransform() { ScaleX = scale, ScaleY = scale },
  26. new Size(50, 25));
  27. }
  28. [Fact]
  29. public void Measure_On_Rotate_90_degrees_Is_Correct()
  30. {
  31. TransformMeasureSizeTest(
  32. new Size(100, 25),
  33. new RotateTransform() { Angle = 90 },
  34. new Size(25, 100));
  35. }
  36. [Fact]
  37. public void Measure_On_Rotate_minus_90_degrees_Is_Correct()
  38. {
  39. TransformMeasureSizeTest(
  40. new Size(100, 25),
  41. new RotateTransform() { Angle = -90 },
  42. new Size(25, 100));
  43. }
  44. [Fact]
  45. public void Measure_On_Rotate_0_degrees_Is_Correct()
  46. {
  47. TransformMeasureSizeTest(
  48. new Size(100, 25),
  49. new RotateTransform() { Angle = 0 },
  50. new Size(100, 25));
  51. }
  52. [Fact]
  53. public void Measure_On_Rotate_180_degrees_Is_Correct()
  54. {
  55. TransformMeasureSizeTest(
  56. new Size(100, 25),
  57. new RotateTransform() { Angle = 180 },
  58. new Size(100, 25));
  59. }
  60. [Fact]
  61. public void Bounds_On_Scale_x2_Are_correct()
  62. {
  63. double scale = 2;
  64. TransformRootBoundsTest(
  65. new Size(100, 50),
  66. new ScaleTransform() { ScaleX = scale, ScaleY = scale },
  67. new Rect(0, 0, 100, 50));
  68. }
  69. [Fact]
  70. public void Bounds_On_Scale_x0_5_Are_correct()
  71. {
  72. double scale = 0.5;
  73. TransformRootBoundsTest(
  74. new Size(100, 50),
  75. new ScaleTransform() { ScaleX = scale, ScaleY = scale },
  76. new Rect(0, 0, 100, 50));
  77. }
  78. [Fact]
  79. public void Bounds_On_Rotate_180_degrees_Are_correct()
  80. {
  81. TransformRootBoundsTest(
  82. new Size(100, 25),
  83. new RotateTransform() { Angle = 180 },
  84. new Rect(100, 25, 100, 25));
  85. }
  86. [Fact]
  87. public void Bounds_On_Rotate_0_degrees_Are_correct()
  88. {
  89. TransformRootBoundsTest(
  90. new Size(100, 25),
  91. new RotateTransform() { Angle = 0 },
  92. new Rect(0, 0, 100, 25));
  93. }
  94. [Fact]
  95. public void Bounds_On_Rotate_90_degrees_Are_correct()
  96. {
  97. TransformRootBoundsTest(
  98. new Size(100, 25),
  99. new RotateTransform() { Angle = 90 },
  100. new Rect(25, 0, 100, 25));
  101. }
  102. [Fact]
  103. public void Bounds_On_Rotate_minus_90_degrees_Are_correct()
  104. {
  105. TransformRootBoundsTest(
  106. new Size(100, 25),
  107. new RotateTransform() { Angle = -90 },
  108. new Rect(0, 100, 100, 25));
  109. }
  110. [Fact]
  111. public void Should_Generate_RenderTransform_90_degrees()
  112. {
  113. LayoutTransformControl lt = CreateWithChildAndMeasureAndTransform(
  114. 100,
  115. 25,
  116. new RotateTransform() { Angle = 90 });
  117. Assert.NotNull(lt.TransformRoot.RenderTransform);
  118. Matrix m = lt.TransformRoot.RenderTransform.Value;
  119. Matrix res = Matrix.CreateRotation(Matrix.ToRadians(90));
  120. Assert.Equal(m.M11, res.M11, 3);
  121. Assert.Equal(m.M12, res.M12, 3);
  122. Assert.Equal(m.M21, res.M21, 3);
  123. Assert.Equal(m.M22, res.M22, 3);
  124. Assert.Equal(m.M31, res.M31, 3);
  125. Assert.Equal(m.M32, res.M32, 3);
  126. }
  127. [Fact]
  128. public void Should_Generate_RenderTransform_minus_90_degrees()
  129. {
  130. LayoutTransformControl lt = CreateWithChildAndMeasureAndTransform(
  131. 100,
  132. 25,
  133. new RotateTransform() { Angle = -90 });
  134. Assert.NotNull(lt.TransformRoot.RenderTransform);
  135. var m = lt.TransformRoot.RenderTransform.Value;
  136. var res = Matrix.CreateRotation(Matrix.ToRadians(-90));
  137. Assert.Equal(m.M11, res.M11, 3);
  138. Assert.Equal(m.M12, res.M12, 3);
  139. Assert.Equal(m.M21, res.M21, 3);
  140. Assert.Equal(m.M22, res.M22, 3);
  141. Assert.Equal(m.M31, res.M31, 3);
  142. Assert.Equal(m.M32, res.M32, 3);
  143. }
  144. [Fact]
  145. public void Should_Generate_ScaleTransform_x2()
  146. {
  147. LayoutTransformControl lt = CreateWithChildAndMeasureAndTransform(
  148. 100,
  149. 50,
  150. new ScaleTransform() { ScaleX = 2, ScaleY = 2 });
  151. Assert.NotNull(lt.TransformRoot.RenderTransform);
  152. Matrix m = lt.TransformRoot.RenderTransform.Value;
  153. Matrix res = Matrix.CreateScale(2, 2);
  154. Assert.Equal(m.M11, res.M11, 3);
  155. Assert.Equal(m.M12, res.M12, 3);
  156. Assert.Equal(m.M21, res.M21, 3);
  157. Assert.Equal(m.M22, res.M22, 3);
  158. Assert.Equal(m.M31, res.M31, 3);
  159. Assert.Equal(m.M32, res.M32, 3);
  160. }
  161. private static void TransformMeasureSizeTest(Size size, Transform transform, Size expectedSize)
  162. {
  163. LayoutTransformControl lt = CreateWithChildAndMeasureAndTransform(
  164. size.Width,
  165. size.Height,
  166. transform);
  167. Size outSize = lt.DesiredSize;
  168. Assert.Equal(outSize.Width, expectedSize.Width);
  169. Assert.Equal(outSize.Height, expectedSize.Height);
  170. }
  171. private static void TransformRootBoundsTest(Size size, Transform transform, Rect expectedBounds)
  172. {
  173. LayoutTransformControl lt = CreateWithChildAndMeasureAndTransform(size.Width, size.Height, transform);
  174. Rect outBounds = lt.TransformRoot.Bounds;
  175. Assert.Equal(outBounds.X, expectedBounds.X);
  176. Assert.Equal(outBounds.Y, expectedBounds.Y);
  177. Assert.Equal(outBounds.Width, expectedBounds.Width);
  178. Assert.Equal(outBounds.Height, expectedBounds.Height);
  179. }
  180. private static LayoutTransformControl CreateWithChildAndMeasureAndTransform(
  181. double width,
  182. double height,
  183. Transform transform)
  184. {
  185. var lt = new LayoutTransformControl()
  186. {
  187. LayoutTransform = transform,
  188. Template = new FuncControlTemplate<LayoutTransformControl>(
  189. p => new ContentPresenter() { Content = p.Content })
  190. };
  191. lt.Content = new Rectangle() { Width = width, Height = height };
  192. lt.ApplyTemplate();
  193. //we need to force create visual child
  194. //so the measure after is correct
  195. (lt.Presenter as ContentPresenter).UpdateChild();
  196. Assert.NotNull(lt.Presenter?.Child);
  197. lt.Measure(Size.Infinity);
  198. lt.Arrange(new Rect(lt.DesiredSize));
  199. return lt;
  200. }
  201. }
  202. }