ContentPresenterTests_Layout.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 Avalonia.Controls.Presenters;
  4. using Avalonia.Layout;
  5. using Xunit;
  6. namespace Avalonia.Controls.UnitTests.Presenters
  7. {
  8. public class ContentPresenterTests_Layout
  9. {
  10. [Theory]
  11. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Stretch, 0, 0, 100, 100)]
  12. [InlineData(HorizontalAlignment.Left, VerticalAlignment.Stretch, 0, 0, 16, 100)]
  13. [InlineData(HorizontalAlignment.Right, VerticalAlignment.Stretch, 84, 0, 16, 100)]
  14. [InlineData(HorizontalAlignment.Center, VerticalAlignment.Stretch, 42, 0, 16, 100)]
  15. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Top, 0, 0, 100, 16)]
  16. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Bottom, 0, 84, 100, 16)]
  17. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Center, 0, 42, 100, 16)]
  18. public void Content_Alignment_Is_Applied_To_Child_Bounds(
  19. HorizontalAlignment h,
  20. VerticalAlignment v,
  21. double expectedX,
  22. double expectedY,
  23. double expectedWidth,
  24. double expectedHeight)
  25. {
  26. Border content;
  27. var target = new ContentPresenter
  28. {
  29. HorizontalContentAlignment = h,
  30. VerticalContentAlignment = v,
  31. Content = content = new Border
  32. {
  33. MinWidth = 16,
  34. MinHeight = 16,
  35. },
  36. };
  37. target.UpdateChild();
  38. target.Measure(new Size(100, 100));
  39. target.Arrange(new Rect(0, 0, 100, 100));
  40. Assert.Equal(new Rect(expectedX, expectedY, expectedWidth, expectedHeight), content.Bounds);
  41. }
  42. [Theory]
  43. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Stretch, 10, 10, 80, 80)]
  44. [InlineData(HorizontalAlignment.Left, VerticalAlignment.Stretch, 10, 10, 16, 80)]
  45. [InlineData(HorizontalAlignment.Right, VerticalAlignment.Stretch, 74, 10, 16, 80)]
  46. [InlineData(HorizontalAlignment.Center, VerticalAlignment.Stretch, 42, 10, 16, 80)]
  47. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Top, 10, 10, 80, 16)]
  48. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Bottom, 10, 74, 80, 16)]
  49. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Center, 10, 42, 80, 16)]
  50. public void Content_Alignment_And_Padding_Are_Applied_To_Child_Bounds(
  51. HorizontalAlignment h,
  52. VerticalAlignment v,
  53. double expectedX,
  54. double expectedY,
  55. double expectedWidth,
  56. double expectedHeight)
  57. {
  58. Border content;
  59. var target = new ContentPresenter
  60. {
  61. HorizontalContentAlignment = h,
  62. VerticalContentAlignment = v,
  63. Padding = new Thickness(10),
  64. Content = content = new Border
  65. {
  66. MinWidth = 16,
  67. MinHeight = 16,
  68. },
  69. };
  70. target.UpdateChild();
  71. target.Measure(new Size(100, 100));
  72. target.Arrange(new Rect(0, 0, 100, 100));
  73. Assert.Equal(new Rect(expectedX, expectedY, expectedWidth, expectedHeight), content.Bounds);
  74. }
  75. [Fact]
  76. public void Should_Correctly_Align_Child_With_Fixed_Size()
  77. {
  78. Border content;
  79. var target = new ContentPresenter
  80. {
  81. HorizontalContentAlignment = HorizontalAlignment.Stretch,
  82. VerticalContentAlignment = VerticalAlignment.Stretch,
  83. Content = content = new Border
  84. {
  85. HorizontalAlignment = HorizontalAlignment.Left,
  86. VerticalAlignment = VerticalAlignment.Bottom,
  87. Width = 16,
  88. Height = 16,
  89. },
  90. };
  91. target.UpdateChild();
  92. target.Measure(new Size(100, 100));
  93. target.Arrange(new Rect(0, 0, 100, 100));
  94. // Check correct result for Issue #1447.
  95. Assert.Equal(new Rect(0, 84, 16, 16), content.Bounds);
  96. }
  97. [Fact]
  98. public void Content_Can_Be_Stretched()
  99. {
  100. Border content;
  101. var target = new ContentPresenter
  102. {
  103. Content = content = new Border
  104. {
  105. MinWidth = 16,
  106. MinHeight = 16,
  107. },
  108. };
  109. target.UpdateChild();
  110. target.Measure(new Size(100, 100));
  111. target.Arrange(new Rect(0, 0, 100, 100));
  112. Assert.Equal(new Rect(0, 0, 100, 100), content.Bounds);
  113. }
  114. [Fact]
  115. public void Content_Can_Be_Right_Aligned()
  116. {
  117. Border content;
  118. var target = new ContentPresenter
  119. {
  120. Content = content = new Border
  121. {
  122. MinWidth = 16,
  123. MinHeight = 16,
  124. HorizontalAlignment = HorizontalAlignment.Right
  125. },
  126. };
  127. target.UpdateChild();
  128. target.Measure(new Size(100, 100));
  129. target.Arrange(new Rect(0, 0, 100, 100));
  130. Assert.Equal(new Rect(84, 0, 16, 100), content.Bounds);
  131. }
  132. [Fact]
  133. public void Content_Can_Be_Bottom_Aligned()
  134. {
  135. Border content;
  136. var target = new ContentPresenter
  137. {
  138. Content = content = new Border
  139. {
  140. MinWidth = 16,
  141. MinHeight = 16,
  142. VerticalAlignment = VerticalAlignment.Bottom,
  143. },
  144. };
  145. target.UpdateChild();
  146. target.Measure(new Size(100, 100));
  147. target.Arrange(new Rect(0, 0, 100, 100));
  148. Assert.Equal(new Rect(0, 84, 100, 16), content.Bounds);
  149. }
  150. [Fact]
  151. public void Content_Can_Be_TopLeft_Aligned()
  152. {
  153. Border content;
  154. var target = new ContentPresenter
  155. {
  156. Content = content = new Border
  157. {
  158. MinWidth = 16,
  159. MinHeight = 16,
  160. HorizontalAlignment = HorizontalAlignment.Right,
  161. VerticalAlignment = VerticalAlignment.Top,
  162. },
  163. };
  164. target.UpdateChild();
  165. target.Measure(new Size(100, 100));
  166. target.Arrange(new Rect(0, 0, 100, 100));
  167. Assert.Equal(new Rect(84, 0, 16, 16), content.Bounds);
  168. }
  169. [Fact]
  170. public void Content_Can_Be_TopRight_Aligned()
  171. {
  172. Border content;
  173. var target = new ContentPresenter
  174. {
  175. Content = content = new Border
  176. {
  177. MinWidth = 16,
  178. MinHeight = 16,
  179. HorizontalAlignment = HorizontalAlignment.Right,
  180. VerticalAlignment = VerticalAlignment.Top,
  181. },
  182. };
  183. target.UpdateChild();
  184. target.Measure(new Size(100, 100));
  185. target.Arrange(new Rect(0, 0, 100, 100));
  186. Assert.Equal(new Rect(84, 0, 16, 16), content.Bounds);
  187. }
  188. [Fact]
  189. public void Child_Arrange_With_Zero_Height_When_Padding_Height_Greater_Than_Child_Height()
  190. {
  191. Border content;
  192. var target = new ContentPresenter
  193. {
  194. Padding = new Thickness(32),
  195. MaxHeight = 32,
  196. MaxWidth = 32,
  197. HorizontalContentAlignment = HorizontalAlignment.Center,
  198. VerticalContentAlignment = VerticalAlignment.Center,
  199. Content = content = new Border
  200. {
  201. Height = 0,
  202. Width = 0,
  203. },
  204. };
  205. target.UpdateChild();
  206. target.Arrange(new Rect(0, 0, 100, 100));
  207. Assert.Equal(new Rect(48, 48, 0, 0), content.Bounds);
  208. }
  209. }
  210. }