ContentPresenterTests_Layout.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using Avalonia.Controls.Presenters;
  2. using Avalonia.Layout;
  3. using Avalonia.UnitTests;
  4. using Xunit;
  5. namespace Avalonia.Controls.UnitTests.Presenters
  6. {
  7. public class ContentPresenterTests_Layout : ScopedTestBase
  8. {
  9. [Theory]
  10. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Stretch, 0, 0, 100, 100)]
  11. [InlineData(HorizontalAlignment.Left, VerticalAlignment.Stretch, 0, 0, 16, 100)]
  12. [InlineData(HorizontalAlignment.Right, VerticalAlignment.Stretch, 84, 0, 16, 100)]
  13. [InlineData(HorizontalAlignment.Center, VerticalAlignment.Stretch, 42, 0, 16, 100)]
  14. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Top, 0, 0, 100, 16)]
  15. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Bottom, 0, 84, 100, 16)]
  16. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Center, 0, 42, 100, 16)]
  17. public void Content_Alignment_Is_Applied_To_Child_Bounds(
  18. HorizontalAlignment h,
  19. VerticalAlignment v,
  20. double expectedX,
  21. double expectedY,
  22. double expectedWidth,
  23. double expectedHeight)
  24. {
  25. Border content;
  26. var target = new ContentPresenter
  27. {
  28. HorizontalContentAlignment = h,
  29. VerticalContentAlignment = v,
  30. Content = content = new Border
  31. {
  32. MinWidth = 16,
  33. MinHeight = 16,
  34. },
  35. };
  36. target.UpdateChild();
  37. target.Measure(new Size(100, 100));
  38. target.Arrange(new Rect(0, 0, 100, 100));
  39. Assert.Equal(new Rect(expectedX, expectedY, expectedWidth, expectedHeight), content.Bounds);
  40. }
  41. [Theory]
  42. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Stretch, 10, 10, 80, 80)]
  43. [InlineData(HorizontalAlignment.Left, VerticalAlignment.Stretch, 10, 10, 16, 80)]
  44. [InlineData(HorizontalAlignment.Right, VerticalAlignment.Stretch, 74, 10, 16, 80)]
  45. [InlineData(HorizontalAlignment.Center, VerticalAlignment.Stretch, 42, 10, 16, 80)]
  46. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Top, 10, 10, 80, 16)]
  47. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Bottom, 10, 74, 80, 16)]
  48. [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Center, 10, 42, 80, 16)]
  49. public void Content_Alignment_And_Padding_Are_Applied_To_Child_Bounds(
  50. HorizontalAlignment h,
  51. VerticalAlignment v,
  52. double expectedX,
  53. double expectedY,
  54. double expectedWidth,
  55. double expectedHeight)
  56. {
  57. Border content;
  58. var target = new ContentPresenter
  59. {
  60. HorizontalContentAlignment = h,
  61. VerticalContentAlignment = v,
  62. Padding = new Thickness(10),
  63. Content = content = new Border
  64. {
  65. MinWidth = 16,
  66. MinHeight = 16,
  67. },
  68. };
  69. target.UpdateChild();
  70. target.Measure(new Size(100, 100));
  71. target.Arrange(new Rect(0, 0, 100, 100));
  72. Assert.Equal(new Rect(expectedX, expectedY, expectedWidth, expectedHeight), content.Bounds);
  73. }
  74. [Fact]
  75. public void Should_Correctly_Align_Child_With_Fixed_Size()
  76. {
  77. Border content;
  78. var target = new ContentPresenter
  79. {
  80. HorizontalContentAlignment = HorizontalAlignment.Stretch,
  81. VerticalContentAlignment = VerticalAlignment.Stretch,
  82. Content = content = new Border
  83. {
  84. HorizontalAlignment = HorizontalAlignment.Left,
  85. VerticalAlignment = VerticalAlignment.Bottom,
  86. Width = 16,
  87. Height = 16,
  88. },
  89. };
  90. target.UpdateChild();
  91. target.Measure(new Size(100, 100));
  92. target.Arrange(new Rect(0, 0, 100, 100));
  93. // Check correct result for Issue #1447.
  94. Assert.Equal(new Rect(0, 84, 16, 16), content.Bounds);
  95. }
  96. [Fact]
  97. public void Content_Can_Be_Stretched()
  98. {
  99. Border content;
  100. var target = new ContentPresenter
  101. {
  102. Content = content = new Border
  103. {
  104. MinWidth = 16,
  105. MinHeight = 16,
  106. },
  107. };
  108. target.UpdateChild();
  109. target.Measure(new Size(100, 100));
  110. target.Arrange(new Rect(0, 0, 100, 100));
  111. Assert.Equal(new Rect(0, 0, 100, 100), content.Bounds);
  112. }
  113. [Fact]
  114. public void Content_Can_Be_Right_Aligned()
  115. {
  116. Border content;
  117. var target = new ContentPresenter
  118. {
  119. Content = content = new Border
  120. {
  121. MinWidth = 16,
  122. MinHeight = 16,
  123. HorizontalAlignment = HorizontalAlignment.Right
  124. },
  125. };
  126. target.UpdateChild();
  127. target.Measure(new Size(100, 100));
  128. target.Arrange(new Rect(0, 0, 100, 100));
  129. Assert.Equal(new Rect(84, 0, 16, 100), content.Bounds);
  130. }
  131. [Fact]
  132. public void Content_Can_Be_Bottom_Aligned()
  133. {
  134. Border content;
  135. var target = new ContentPresenter
  136. {
  137. Content = content = new Border
  138. {
  139. MinWidth = 16,
  140. MinHeight = 16,
  141. VerticalAlignment = VerticalAlignment.Bottom,
  142. },
  143. };
  144. target.UpdateChild();
  145. target.Measure(new Size(100, 100));
  146. target.Arrange(new Rect(0, 0, 100, 100));
  147. Assert.Equal(new Rect(0, 84, 100, 16), content.Bounds);
  148. }
  149. [Fact]
  150. public void Content_Can_Be_TopLeft_Aligned()
  151. {
  152. Border content;
  153. var target = new ContentPresenter
  154. {
  155. Content = content = new Border
  156. {
  157. MinWidth = 16,
  158. MinHeight = 16,
  159. HorizontalAlignment = HorizontalAlignment.Right,
  160. VerticalAlignment = VerticalAlignment.Top,
  161. },
  162. };
  163. target.UpdateChild();
  164. target.Measure(new Size(100, 100));
  165. target.Arrange(new Rect(0, 0, 100, 100));
  166. Assert.Equal(new Rect(84, 0, 16, 16), content.Bounds);
  167. }
  168. [Fact]
  169. public void Content_Can_Be_TopRight_Aligned()
  170. {
  171. Border content;
  172. var target = new ContentPresenter
  173. {
  174. Content = content = new Border
  175. {
  176. MinWidth = 16,
  177. MinHeight = 16,
  178. HorizontalAlignment = HorizontalAlignment.Right,
  179. VerticalAlignment = VerticalAlignment.Top,
  180. },
  181. };
  182. target.UpdateChild();
  183. target.Measure(new Size(100, 100));
  184. target.Arrange(new Rect(0, 0, 100, 100));
  185. Assert.Equal(new Rect(84, 0, 16, 16), content.Bounds);
  186. }
  187. [Fact]
  188. public void Child_Arrange_With_Zero_Height_When_Padding_Height_Greater_Than_Child_Height()
  189. {
  190. Border content;
  191. var target = new ContentPresenter
  192. {
  193. Padding = new Thickness(32),
  194. MaxHeight = 32,
  195. MaxWidth = 32,
  196. HorizontalContentAlignment = HorizontalAlignment.Center,
  197. VerticalContentAlignment = VerticalAlignment.Center,
  198. Content = content = new Border
  199. {
  200. Height = 0,
  201. Width = 0,
  202. },
  203. };
  204. target.UpdateChild();
  205. target.Arrange(new Rect(0, 0, 100, 100));
  206. Assert.Equal(new Rect(32, 32, 0, 0), content.Bounds);
  207. }
  208. public class UseLayoutRounding : ScopedTestBase
  209. {
  210. [Fact]
  211. public void Measure_Rounds_Padding()
  212. {
  213. var target = new ContentPresenter
  214. {
  215. Padding = new Thickness(1),
  216. Content = new Canvas
  217. {
  218. Width = 101,
  219. Height = 101,
  220. }
  221. };
  222. var root = CreatedRoot(1.5, target);
  223. root.LayoutManager.ExecuteInitialLayoutPass();
  224. // - 1 pixel padding is rounded up to 1.3333; for both sides it is 2.6666
  225. // - Size of 101 gets rounded up to 101.3333
  226. // - Desired size = 101.3333 + 2.6666 = 104
  227. Assert.Equal(new Size(104, 104), target.DesiredSize);
  228. }
  229. [Fact]
  230. public void Measure_Rounds_BorderThickness()
  231. {
  232. var target = new ContentPresenter
  233. {
  234. BorderThickness = new Thickness(1),
  235. Content = new Canvas
  236. {
  237. Width = 101,
  238. Height = 101,
  239. }
  240. };
  241. var root = CreatedRoot(1.5, target);
  242. root.LayoutManager.ExecuteInitialLayoutPass();
  243. // - 1 pixel border thickness is rounded up to 1.3333; for both sides it is 2.6666
  244. // - Size of 101 gets rounded up to 101.3333
  245. // - Desired size = 101.3333 + 2.6666 = 104
  246. Assert.Equal(new Size(104, 104), target.DesiredSize);
  247. }
  248. private static TestRoot CreatedRoot(
  249. double scaling,
  250. Control child,
  251. Size? constraint = null)
  252. {
  253. return new TestRoot
  254. {
  255. LayoutScaling = scaling,
  256. UseLayoutRounding = true,
  257. Child = child,
  258. ClientSize = constraint ?? new Size(1000, 1000),
  259. };
  260. }
  261. }
  262. }
  263. }