StackPanelTests.cs 13 KB

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