StackPanelTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. using System.Linq;
  2. using Avalonia.Layout;
  3. using Avalonia.UnitTests;
  4. using Xunit;
  5. namespace Avalonia.Controls.UnitTests
  6. {
  7. public class StackPanelTests : ScopedTestBase
  8. {
  9. [Fact]
  10. public void Lays_Out_Children_Vertically()
  11. {
  12. var target = new StackPanel
  13. {
  14. Children =
  15. {
  16. new Border { Height = 20, Width = 120 },
  17. new Border { Height = 30 },
  18. new Border { Height = 50 },
  19. }
  20. };
  21. target.Measure(Size.Infinity);
  22. target.Arrange(new Rect(target.DesiredSize));
  23. Assert.Equal(new Size(120, 100), target.Bounds.Size);
  24. Assert.Equal(new Rect(0, 0, 120, 20), target.Children[0].Bounds);
  25. Assert.Equal(new Rect(0, 20, 120, 30), target.Children[1].Bounds);
  26. Assert.Equal(new Rect(0, 50, 120, 50), target.Children[2].Bounds);
  27. }
  28. [Fact]
  29. public void Lays_Out_Children_Horizontally()
  30. {
  31. var target = new StackPanel
  32. {
  33. Orientation = Orientation.Horizontal,
  34. Children =
  35. {
  36. new Border { Width = 20, Height = 120 },
  37. new Border { Width = 30 },
  38. new Border { Width = 50 },
  39. }
  40. };
  41. target.Measure(Size.Infinity);
  42. target.Arrange(new Rect(target.DesiredSize));
  43. Assert.Equal(new Size(100, 120), target.Bounds.Size);
  44. Assert.Equal(new Rect(0, 0, 20, 120), target.Children[0].Bounds);
  45. Assert.Equal(new Rect(20, 0, 30, 120), target.Children[1].Bounds);
  46. Assert.Equal(new Rect(50, 0, 50, 120), target.Children[2].Bounds);
  47. }
  48. [Fact]
  49. public void Lays_Out_Children_Vertically_With_Spacing()
  50. {
  51. var target = new StackPanel
  52. {
  53. Spacing = 10,
  54. Children =
  55. {
  56. new Border { Height = 20, Width = 120 },
  57. new Border { Height = 30 },
  58. new Border { Height = 50 },
  59. }
  60. };
  61. target.Measure(Size.Infinity);
  62. target.Arrange(new Rect(target.DesiredSize));
  63. Assert.Equal(new Size(120, 120), target.Bounds.Size);
  64. Assert.Equal(new Rect(0, 0, 120, 20), target.Children[0].Bounds);
  65. Assert.Equal(new Rect(0, 30, 120, 30), target.Children[1].Bounds);
  66. Assert.Equal(new Rect(0, 70, 120, 50), target.Children[2].Bounds);
  67. }
  68. [Fact]
  69. public void Lays_Out_Children_Horizontally_With_Spacing()
  70. {
  71. var target = new StackPanel
  72. {
  73. Spacing = 10,
  74. Orientation = Orientation.Horizontal,
  75. Children =
  76. {
  77. new Border { Width = 20, Height = 120 },
  78. new Border { Width = 30 },
  79. new Border { Width = 50 },
  80. }
  81. };
  82. target.Measure(Size.Infinity);
  83. target.Arrange(new Rect(target.DesiredSize));
  84. Assert.Equal(new Size(120, 120), target.Bounds.Size);
  85. Assert.Equal(new Rect(0, 0, 20, 120), target.Children[0].Bounds);
  86. Assert.Equal(new Rect(30, 0, 30, 120), target.Children[1].Bounds);
  87. Assert.Equal(new Rect(70, 0, 50, 120), target.Children[2].Bounds);
  88. }
  89. [Fact]
  90. public void Lays_Out_Children_Vertically_Even_If_Larger_Than_Panel()
  91. {
  92. var target = new StackPanel
  93. {
  94. Height = 60,
  95. Children =
  96. {
  97. new Border { Height = 20, Width = 120 },
  98. new Border { Height = 30 },
  99. new Border { Height = 50 },
  100. }
  101. };
  102. target.Measure(Size.Infinity);
  103. target.Arrange(new Rect(target.DesiredSize));
  104. Assert.Equal(new Size(120, 60), target.Bounds.Size);
  105. Assert.Equal(new Rect(0, 0, 120, 20), target.Children[0].Bounds);
  106. Assert.Equal(new Rect(0, 20, 120, 30), target.Children[1].Bounds);
  107. Assert.Equal(new Rect(0, 50, 120, 50), target.Children[2].Bounds);
  108. }
  109. [Fact]
  110. public void Lays_Out_Children_Horizontally_Even_If_Larger_Than_Panel()
  111. {
  112. var target = new StackPanel
  113. {
  114. Width = 60,
  115. Orientation = Orientation.Horizontal,
  116. Children =
  117. {
  118. new Border { Width = 20, Height = 120 },
  119. new Border { Width = 30 },
  120. new Border { Width = 50 },
  121. }
  122. };
  123. target.Measure(Size.Infinity);
  124. target.Arrange(new Rect(target.DesiredSize));
  125. Assert.Equal(new Size(60, 120), target.Bounds.Size);
  126. Assert.Equal(new Rect(0, 0, 20, 120), target.Children[0].Bounds);
  127. Assert.Equal(new Rect(20, 0, 30, 120), target.Children[1].Bounds);
  128. Assert.Equal(new Rect(50, 0, 50, 120), target.Children[2].Bounds);
  129. }
  130. [Fact]
  131. public void Arranges_Vertical_Children_With_Correct_Bounds()
  132. {
  133. var target = new StackPanel
  134. {
  135. Orientation = Orientation.Vertical,
  136. Children =
  137. {
  138. new TestControl
  139. {
  140. HorizontalAlignment = HorizontalAlignment.Left,
  141. MeasureSize = new Size(50, 10),
  142. },
  143. new TestControl
  144. {
  145. HorizontalAlignment = HorizontalAlignment.Left,
  146. MeasureSize = new Size(150, 10),
  147. },
  148. new TestControl
  149. {
  150. HorizontalAlignment = HorizontalAlignment.Center,
  151. MeasureSize = new Size(50, 10),
  152. },
  153. new TestControl
  154. {
  155. HorizontalAlignment = HorizontalAlignment.Center,
  156. MeasureSize = new Size(150, 10),
  157. },
  158. new TestControl
  159. {
  160. HorizontalAlignment = HorizontalAlignment.Right,
  161. MeasureSize = new Size(50, 10),
  162. },
  163. new TestControl
  164. {
  165. HorizontalAlignment = HorizontalAlignment.Right,
  166. MeasureSize = new Size(150, 10),
  167. },
  168. new TestControl
  169. {
  170. HorizontalAlignment = HorizontalAlignment.Stretch,
  171. MeasureSize = new Size(50, 10),
  172. },
  173. new TestControl
  174. {
  175. HorizontalAlignment = HorizontalAlignment.Stretch,
  176. MeasureSize = new Size(150, 10),
  177. },
  178. }
  179. };
  180. target.Measure(new Size(100, 150));
  181. Assert.Equal(new Size(100, 80), target.DesiredSize);
  182. target.Arrange(new Rect(target.DesiredSize));
  183. var bounds = target.Children.Select(x => x.Bounds).ToArray();
  184. Assert.Equal(
  185. new[]
  186. {
  187. new Rect(0, 0, 50, 10),
  188. new Rect(0, 10, 100, 10),
  189. new Rect(25, 20, 50, 10),
  190. new Rect(0, 30, 100, 10),
  191. new Rect(50, 40, 50, 10),
  192. new Rect(0, 50, 100, 10),
  193. new Rect(0, 60, 100, 10),
  194. new Rect(0, 70, 100, 10),
  195. }, bounds);
  196. }
  197. [Fact]
  198. public void Arranges_Horizontal_Children_With_Correct_Bounds()
  199. {
  200. var target = new StackPanel
  201. {
  202. Orientation = Orientation.Horizontal,
  203. Children =
  204. {
  205. new TestControl
  206. {
  207. VerticalAlignment = VerticalAlignment.Top,
  208. MeasureSize = new Size(10, 50),
  209. },
  210. new TestControl
  211. {
  212. VerticalAlignment = VerticalAlignment.Top,
  213. MeasureSize = new Size(10, 150),
  214. },
  215. new TestControl
  216. {
  217. VerticalAlignment = VerticalAlignment.Center,
  218. MeasureSize = new Size(10, 50),
  219. },
  220. new TestControl
  221. {
  222. VerticalAlignment = VerticalAlignment.Center,
  223. MeasureSize = new Size(10, 150),
  224. },
  225. new TestControl
  226. {
  227. VerticalAlignment = VerticalAlignment.Bottom,
  228. MeasureSize = new Size(10, 50),
  229. },
  230. new TestControl
  231. {
  232. VerticalAlignment = VerticalAlignment.Bottom,
  233. MeasureSize = new Size(10, 150),
  234. },
  235. new TestControl
  236. {
  237. VerticalAlignment = VerticalAlignment.Stretch,
  238. MeasureSize = new Size(10, 50),
  239. },
  240. new TestControl
  241. {
  242. VerticalAlignment = VerticalAlignment.Stretch,
  243. MeasureSize = new Size(10, 150),
  244. },
  245. }
  246. };
  247. target.Measure(new Size(150, 100));
  248. Assert.Equal(new Size(80, 100), target.DesiredSize);
  249. target.Arrange(new Rect(target.DesiredSize));
  250. var bounds = target.Children.Select(x => x.Bounds).ToArray();
  251. Assert.Equal(
  252. new[]
  253. {
  254. new Rect(0, 0, 10, 50),
  255. new Rect(10, 0, 10, 100),
  256. new Rect(20, 25, 10, 50),
  257. new Rect(30, 0, 10, 100),
  258. new Rect(40, 50, 10, 50),
  259. new Rect(50, 0, 10, 100),
  260. new Rect(60, 0, 10, 100),
  261. new Rect(70, 0, 10, 100),
  262. }, bounds);
  263. }
  264. [Theory]
  265. [InlineData(Orientation.Horizontal)]
  266. [InlineData(Orientation.Vertical)]
  267. public void Spacing_Not_Added_For_Invisible_Children(Orientation orientation)
  268. {
  269. var targetThreeChildrenOneInvisble = new StackPanel
  270. {
  271. Spacing = 40,
  272. Orientation = orientation,
  273. Children =
  274. {
  275. new StackPanel { Width = 10, Height= 10, IsVisible = false },
  276. new StackPanel { Width = 10, Height= 10 },
  277. new StackPanel { Width = 10, Height= 10 },
  278. }
  279. };
  280. var targetTwoChildrenNoneInvisible = new StackPanel
  281. {
  282. Spacing = 40,
  283. Orientation = orientation,
  284. Children =
  285. {
  286. new StackPanel { Width = 10, Height= 10 },
  287. new StackPanel { Width = 10, Height= 10 }
  288. }
  289. };
  290. targetThreeChildrenOneInvisble.Measure(Size.Infinity);
  291. targetThreeChildrenOneInvisble.Arrange(new Rect(targetThreeChildrenOneInvisble.DesiredSize));
  292. targetTwoChildrenNoneInvisible.Measure(Size.Infinity);
  293. targetTwoChildrenNoneInvisible.Arrange(new Rect(targetTwoChildrenNoneInvisible.DesiredSize));
  294. Size sizeWithTwoChildren = targetTwoChildrenNoneInvisible.Bounds.Size;
  295. Size sizeWithThreeChildren = targetThreeChildrenOneInvisble.Bounds.Size;
  296. Assert.Equal(sizeWithTwoChildren, sizeWithThreeChildren);
  297. }
  298. [Theory]
  299. [InlineData(Orientation.Horizontal)]
  300. [InlineData(Orientation.Vertical)]
  301. public void Only_Arrange_Visible_Children(Orientation orientation)
  302. {
  303. var hiddenPanel = new Panel { Width = 10, Height = 10, IsVisible = false };
  304. var panel = new Panel { Width = 10, Height = 10 };
  305. var target = new StackPanel
  306. {
  307. Spacing = 40,
  308. Orientation = orientation,
  309. Children =
  310. {
  311. hiddenPanel,
  312. panel
  313. }
  314. };
  315. target.Measure(Size.Infinity);
  316. target.Arrange(new Rect(target.DesiredSize));
  317. Assert.Equal(new Rect(0, 0, 10, 10), panel.Bounds);
  318. }
  319. private class TestControl : Control
  320. {
  321. public Size MeasureConstraint { get; private set; }
  322. public Size MeasureSize { get; set; }
  323. protected override Size MeasureOverride(Size availableSize)
  324. {
  325. MeasureConstraint = availableSize;
  326. return MeasureSize;
  327. }
  328. }
  329. }
  330. }