ContentPresenterTests_Layout.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 Content_Can_Be_Stretched()
  77. {
  78. Border content;
  79. var target = new ContentPresenter
  80. {
  81. Content = content = new Border
  82. {
  83. MinWidth = 16,
  84. MinHeight = 16,
  85. },
  86. };
  87. target.UpdateChild();
  88. target.Measure(new Size(100, 100));
  89. target.Arrange(new Rect(0, 0, 100, 100));
  90. Assert.Equal(new Rect(0, 0, 100, 100), content.Bounds);
  91. }
  92. [Fact]
  93. public void Content_Can_Be_Right_Aligned()
  94. {
  95. Border content;
  96. var target = new ContentPresenter
  97. {
  98. Content = content = new Border
  99. {
  100. MinWidth = 16,
  101. MinHeight = 16,
  102. HorizontalAlignment = HorizontalAlignment.Right
  103. },
  104. };
  105. target.UpdateChild();
  106. target.Measure(new Size(100, 100));
  107. target.Arrange(new Rect(0, 0, 100, 100));
  108. Assert.Equal(new Rect(84, 0, 16, 100), content.Bounds);
  109. }
  110. [Fact]
  111. public void Content_Can_Be_Bottom_Aligned()
  112. {
  113. Border content;
  114. var target = new ContentPresenter
  115. {
  116. Content = content = new Border
  117. {
  118. MinWidth = 16,
  119. MinHeight = 16,
  120. VerticalAlignment = VerticalAlignment.Bottom,
  121. },
  122. };
  123. target.UpdateChild();
  124. target.Measure(new Size(100, 100));
  125. target.Arrange(new Rect(0, 0, 100, 100));
  126. Assert.Equal(new Rect(0, 84, 100, 16), content.Bounds);
  127. }
  128. [Fact]
  129. public void Content_Can_Be_TopLeft_Aligned()
  130. {
  131. Border content;
  132. var target = new ContentPresenter
  133. {
  134. Content = content = new Border
  135. {
  136. MinWidth = 16,
  137. MinHeight = 16,
  138. HorizontalAlignment = HorizontalAlignment.Right,
  139. VerticalAlignment = VerticalAlignment.Top,
  140. },
  141. };
  142. target.UpdateChild();
  143. target.Measure(new Size(100, 100));
  144. target.Arrange(new Rect(0, 0, 100, 100));
  145. Assert.Equal(new Rect(84, 0, 16, 16), content.Bounds);
  146. }
  147. [Fact]
  148. public void Content_Can_Be_TopRight_Aligned()
  149. {
  150. Border content;
  151. var target = new ContentPresenter
  152. {
  153. Content = content = new Border
  154. {
  155. MinWidth = 16,
  156. MinHeight = 16,
  157. HorizontalAlignment = HorizontalAlignment.Right,
  158. VerticalAlignment = VerticalAlignment.Top,
  159. },
  160. };
  161. target.UpdateChild();
  162. target.Measure(new Size(100, 100));
  163. target.Arrange(new Rect(0, 0, 100, 100));
  164. Assert.Equal(new Rect(84, 0, 16, 16), content.Bounds);
  165. }
  166. }
  167. }