ImageTests.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 Moq;
  4. using Avalonia.Media;
  5. using Avalonia.Media.Imaging;
  6. using Xunit;
  7. namespace Avalonia.Controls.UnitTests
  8. {
  9. public class ImageTests
  10. {
  11. [Fact]
  12. public void Measure_Should_Return_Correct_Size_For_No_Stretch()
  13. {
  14. var bitmap = CreateBitmap(50, 100);
  15. var target = new Image();
  16. target.Stretch = Stretch.None;
  17. target.Source = bitmap;
  18. target.Measure(new Size(50, 50));
  19. Assert.Equal(new Size(50, 50), target.DesiredSize);
  20. }
  21. [Fact]
  22. public void Measure_Should_Return_Correct_Size_For_Fill_Stretch()
  23. {
  24. var bitmap = CreateBitmap(50, 100);
  25. var target = new Image();
  26. target.Stretch = Stretch.Fill;
  27. target.Source = bitmap;
  28. target.Measure(new Size(50, 50));
  29. Assert.Equal(new Size(50, 50), target.DesiredSize);
  30. }
  31. [Fact]
  32. public void Measure_Should_Return_Correct_Size_For_Uniform_Stretch()
  33. {
  34. var bitmap = CreateBitmap(50, 100);
  35. var target = new Image();
  36. target.Stretch = Stretch.Uniform;
  37. target.Source = bitmap;
  38. target.Measure(new Size(50, 50));
  39. Assert.Equal(new Size(25, 50), target.DesiredSize);
  40. }
  41. [Fact]
  42. public void Measure_Should_Return_Correct_Size_For_UniformToFill_Stretch()
  43. {
  44. var bitmap = CreateBitmap(50, 100);
  45. var target = new Image();
  46. target.Stretch = Stretch.UniformToFill;
  47. target.Source = bitmap;
  48. target.Measure(new Size(50, 50));
  49. Assert.Equal(new Size(50, 50), target.DesiredSize);
  50. }
  51. [Fact]
  52. public void Measure_Should_Return_Correct_Size_With_StretchDirection_DownOnly()
  53. {
  54. var bitmap = CreateBitmap(50, 100);
  55. var target = new Image();
  56. target.StretchDirection = StretchDirection.DownOnly;
  57. target.Source = bitmap;
  58. target.Measure(new Size(150, 150));
  59. Assert.Equal(new Size(50, 100), target.DesiredSize);
  60. }
  61. [Fact]
  62. public void Measure_Should_Return_Correct_Size_For_Infinite_Height()
  63. {
  64. var bitmap = CreateBitmap(50, 100);
  65. var image = new Image();
  66. image.Source = bitmap;
  67. image.Measure(new Size(200, double.PositiveInfinity));
  68. Assert.Equal(new Size(200, 400), image.DesiredSize);
  69. }
  70. [Fact]
  71. public void Measure_Should_Return_Correct_Size_For_Infinite_Width()
  72. {
  73. var bitmap = CreateBitmap(50, 100);
  74. var image = new Image();
  75. image.Source = bitmap;
  76. image.Measure(new Size(double.PositiveInfinity, 400));
  77. Assert.Equal(new Size(200, 400), image.DesiredSize);
  78. }
  79. [Fact]
  80. public void Measure_Should_Return_Correct_Size_For_Infinite_Width_Height()
  81. {
  82. var bitmap = CreateBitmap(50, 100);
  83. var image = new Image();
  84. image.Source = bitmap;
  85. image.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  86. Assert.Equal(new Size(50, 100), image.DesiredSize);
  87. }
  88. [Fact]
  89. public void Arrange_Should_Return_Correct_Size_For_No_Stretch()
  90. {
  91. var bitmap = CreateBitmap(50, 100);
  92. var target = new Image();
  93. target.Stretch = Stretch.None;
  94. target.Source = bitmap;
  95. target.Measure(new Size(50, 50));
  96. target.Arrange(new Rect(0, 0, 100, 400));
  97. Assert.Equal(new Size(50, 100), target.Bounds.Size);
  98. }
  99. [Fact]
  100. public void Arrange_Should_Return_Correct_Size_For_Fill_Stretch()
  101. {
  102. var bitmap = CreateBitmap(50, 100);
  103. var target = new Image();
  104. target.Stretch = Stretch.Fill;
  105. target.Source = bitmap;
  106. target.Measure(new Size(50, 50));
  107. target.Arrange(new Rect(0, 0, 25, 100));
  108. Assert.Equal(new Size(25, 100), target.Bounds.Size);
  109. }
  110. [Fact]
  111. public void Arrange_Should_Return_Correct_Size_For_Uniform_Stretch()
  112. {
  113. var bitmap = CreateBitmap(50, 100);
  114. var target = new Image();
  115. target.Stretch = Stretch.Uniform;
  116. target.Source = bitmap;
  117. target.Measure(new Size(50, 50));
  118. target.Arrange(new Rect(0, 0, 25, 100));
  119. Assert.Equal(new Size(25, 50), target.Bounds.Size);
  120. }
  121. [Fact]
  122. public void Arrange_Should_Return_Correct_Size_For_UniformToFill_Stretch()
  123. {
  124. var bitmap = CreateBitmap(50, 100);
  125. var target = new Image();
  126. target.Stretch = Stretch.UniformToFill;
  127. target.Source = bitmap;
  128. target.Measure(new Size(50, 50));
  129. target.Arrange(new Rect(0, 0, 25, 100));
  130. Assert.Equal(new Size(25, 100), target.Bounds.Size);
  131. }
  132. private IBitmap CreateBitmap(int width, int height)
  133. {
  134. return Mock.Of<IBitmap>(x => x.Size == new Size(width, height));
  135. }
  136. }
  137. }