StackPanelTests.cs 13 KB

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