ViewboxTests.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using Avalonia.Controls.Shapes;
  2. using Avalonia.LogicalTree;
  3. using Avalonia.Media;
  4. using Avalonia.UnitTests;
  5. using Xunit;
  6. namespace Avalonia.Controls.UnitTests
  7. {
  8. public class ViewboxTests
  9. {
  10. [Fact]
  11. public void Viewbox_Stretch_Uniform_Child()
  12. {
  13. using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  14. var target = new Viewbox() { Child = new Rectangle() { Width = 100, Height = 50 } };
  15. target.Measure(new Size(200, 200));
  16. target.Arrange(new Rect(new Point(0, 0), target.DesiredSize));
  17. Assert.Equal(new Size(200, 100), target.DesiredSize);
  18. Assert.True(TryGetScale(target, out Vector scale));
  19. Assert.Equal(2.0, scale.X);
  20. Assert.Equal(2.0, scale.Y);
  21. }
  22. [Fact]
  23. public void Viewbox_Stretch_None_Child()
  24. {
  25. using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  26. var target = new Viewbox() { Stretch = Stretch.None, Child = new Rectangle() { Width = 100, Height = 50 } };
  27. target.Measure(new Size(200, 200));
  28. target.Arrange(new Rect(new Point(0, 0), target.DesiredSize));
  29. Assert.Equal(new Size(100, 50), target.DesiredSize);
  30. Assert.True(TryGetScale(target, out Vector scale));
  31. Assert.Equal(1.0, scale.X);
  32. Assert.Equal(1.0, scale.Y);
  33. }
  34. [Fact]
  35. public void Viewbox_Stretch_Fill_Child()
  36. {
  37. using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  38. var target = new Viewbox() { Stretch = Stretch.Fill, Child = new Rectangle() { Width = 100, Height = 50 } };
  39. target.Measure(new Size(200, 200));
  40. target.Arrange(new Rect(new Point(0, 0), target.DesiredSize));
  41. Assert.Equal(new Size(200, 200), target.DesiredSize);
  42. Assert.True(TryGetScale(target, out Vector scale));
  43. Assert.Equal(2.0, scale.X);
  44. Assert.Equal(4.0, scale.Y);
  45. }
  46. [Fact]
  47. public void Viewbox_Stretch_UniformToFill_Child()
  48. {
  49. using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  50. var target = new Viewbox() { Stretch = Stretch.UniformToFill, Child = new Rectangle() { Width = 100, Height = 50 } };
  51. target.Measure(new Size(200, 200));
  52. target.Arrange(new Rect(new Point(0, 0), target.DesiredSize));
  53. Assert.Equal(new Size(200, 200), target.DesiredSize);
  54. Assert.True(TryGetScale(target, out Vector scale));
  55. Assert.Equal(4.0, scale.X);
  56. Assert.Equal(4.0, scale.Y);
  57. }
  58. [Fact]
  59. public void Viewbox_Stretch_Uniform_Child_With_Unrestricted_Width()
  60. {
  61. using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  62. var target = new Viewbox() { Child = new Rectangle() { Width = 100, Height = 50 } };
  63. target.Measure(new Size(double.PositiveInfinity, 200));
  64. target.Arrange(new Rect(new Point(0, 0), target.DesiredSize));
  65. Assert.Equal(new Size(400, 200), target.DesiredSize);
  66. Assert.True(TryGetScale(target, out Vector scale));
  67. Assert.Equal(4.0, scale.X);
  68. Assert.Equal(4.0, scale.Y);
  69. }
  70. [Fact]
  71. public void Viewbox_Stretch_Uniform_Child_With_Unrestricted_Height()
  72. {
  73. using var app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
  74. var target = new Viewbox() { Child = new Rectangle() { Width = 100, Height = 50 } };
  75. target.Measure(new Size(200, double.PositiveInfinity));
  76. target.Arrange(new Rect(new Point(0, 0), target.DesiredSize));
  77. Assert.Equal(new Size(200, 100), target.DesiredSize);
  78. Assert.True(TryGetScale(target, out Vector scale));
  79. Assert.Equal(2.0, scale.X);
  80. Assert.Equal(2.0, scale.Y);
  81. }
  82. [Theory]
  83. [InlineData(50, 100, 50, 100, 50, 100, 1)]
  84. [InlineData(50, 100, 150, 150, 50, 100, 1)]
  85. [InlineData(50, 100, 25, 50, 25, 50, 0.5)]
  86. public void Viewbox_Should_Return_Correct_SizeAndScale_StretchDirection_DownOnly(
  87. double childWidth, double childHeight,
  88. double viewboxWidth, double viewboxHeight,
  89. double expectedWidth, double expectedHeight,
  90. double expectedScale)
  91. {
  92. var target = new Viewbox
  93. {
  94. Child = new Control { Width = childWidth, Height = childHeight },
  95. StretchDirection = StretchDirection.DownOnly
  96. };
  97. target.Measure(new Size(viewboxWidth, viewboxHeight));
  98. target.Arrange(new Rect(default, target.DesiredSize));
  99. Assert.Equal(new Size(expectedWidth, expectedHeight), target.DesiredSize);
  100. Assert.True(TryGetScale(target, out Vector scale));
  101. Assert.Equal(expectedScale, scale.X);
  102. Assert.Equal(expectedScale, scale.Y);
  103. }
  104. [Theory]
  105. [InlineData(50, 100, 50, 100, 50, 100, 1)]
  106. [InlineData(50, 100, 25, 50, 25, 50, 1)]
  107. [InlineData(50, 100, 150, 150, 75, 150, 1.5)]
  108. public void Viewbox_Should_Return_Correct_SizeAndScale_StretchDirection_UpOnly(
  109. double childWidth, double childHeight,
  110. double viewboxWidth, double viewboxHeight,
  111. double expectedWidth, double expectedHeight,
  112. double expectedScale)
  113. {
  114. var target = new Viewbox
  115. {
  116. Child = new Control { Width = childWidth, Height = childHeight },
  117. StretchDirection = StretchDirection.UpOnly
  118. };
  119. target.Measure(new Size(viewboxWidth, viewboxHeight));
  120. target.Arrange(new Rect(default, target.DesiredSize));
  121. Assert.Equal(new Size(expectedWidth, expectedHeight), target.DesiredSize);
  122. Assert.True(TryGetScale(target, out Vector scale));
  123. Assert.Equal(expectedScale, scale.X);
  124. Assert.Equal(expectedScale, scale.Y);
  125. }
  126. [Fact]
  127. public void Child_Should_Be_Logical_Child_Of_Viewbox()
  128. {
  129. var target = new Viewbox();
  130. Assert.Empty(target.GetLogicalChildren());
  131. var child = new Canvas();
  132. target.Child = child;
  133. Assert.Single(target.GetLogicalChildren(), child);
  134. Assert.Same(child.GetLogicalParent(), target);
  135. target.Child = null;
  136. Assert.Empty(target.GetLogicalChildren());
  137. Assert.Null(child.GetLogicalParent());
  138. }
  139. [Fact]
  140. public void Changing_Child_Should_Invalidate_Layout()
  141. {
  142. var target = new Viewbox();
  143. target.Child = new Canvas
  144. {
  145. Width = 100,
  146. Height = 100,
  147. };
  148. target.Measure(Size.Infinity);
  149. target.Arrange(new Rect(target.DesiredSize));
  150. Assert.Equal(new Size(100, 100), target.DesiredSize);
  151. target.Child = new Canvas
  152. {
  153. Width = 200,
  154. Height = 200,
  155. };
  156. target.Measure(Size.Infinity);
  157. target.Arrange(new Rect(target.DesiredSize));
  158. Assert.Equal(new Size(200, 200), target.DesiredSize);
  159. }
  160. private bool TryGetScale(Viewbox viewbox, out Vector scale)
  161. {
  162. if (viewbox.InternalTransform is null)
  163. {
  164. scale = default;
  165. return false;
  166. }
  167. var matrix = viewbox.InternalTransform.Value;
  168. Matrix.TryDecomposeTransform(matrix, out var decomposed);
  169. scale = decomposed.Scale;
  170. return true;
  171. }
  172. }
  173. }