WrapPanelTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System;
  2. using Avalonia.Layout;
  3. using Xunit;
  4. namespace Avalonia.Controls.UnitTests
  5. {
  6. public class WrapPanelTests
  7. {
  8. [Fact]
  9. public void Lays_Out_Horizontally_On_Separate_Lines()
  10. {
  11. var target = new WrapPanel()
  12. {
  13. Width = 100,
  14. Children =
  15. {
  16. new Border { Height = 50, Width = 100 },
  17. new Border { Height = 50, Width = 100 },
  18. }
  19. };
  20. target.Measure(Size.Infinity);
  21. target.Arrange(new Rect(target.DesiredSize));
  22. Assert.Equal(new Size(100, 100), target.Bounds.Size);
  23. Assert.Equal(new Rect(0, 0, 100, 50), target.Children[0].Bounds);
  24. Assert.Equal(new Rect(0, 50, 100, 50), target.Children[1].Bounds);
  25. }
  26. [Fact]
  27. public void Lays_Out_Horizontally_On_A_Single_Line()
  28. {
  29. var target = new WrapPanel()
  30. {
  31. Width = 200,
  32. Children =
  33. {
  34. new Border { Height = 50, Width = 100 },
  35. new Border { Height = 50, Width = 100 },
  36. }
  37. };
  38. target.Measure(Size.Infinity);
  39. target.Arrange(new Rect(target.DesiredSize));
  40. Assert.Equal(new Size(200, 50), target.Bounds.Size);
  41. Assert.Equal(new Rect(0, 0, 100, 50), target.Children[0].Bounds);
  42. Assert.Equal(new Rect(100, 0, 100, 50), target.Children[1].Bounds);
  43. }
  44. public static TheoryData<Orientation, WrapPanelItemsAlignment> GetItemsAlignmentValues()
  45. {
  46. var data = new TheoryData<Orientation, WrapPanelItemsAlignment>();
  47. foreach (var orientation in Enum.GetValues<Orientation>())
  48. {
  49. foreach (var alignment in Enum.GetValues<WrapPanelItemsAlignment>())
  50. {
  51. data.Add(orientation, alignment);
  52. }
  53. }
  54. return data;
  55. }
  56. [Theory, MemberData(nameof(GetItemsAlignmentValues))]
  57. public void Lays_Out_With_Items_Alignment(Orientation orientation, WrapPanelItemsAlignment itemsAlignment)
  58. {
  59. var target = new WrapPanel()
  60. {
  61. Width = 200,
  62. Height = 200,
  63. Orientation = orientation,
  64. ItemsAlignment = itemsAlignment,
  65. Children =
  66. {
  67. new Border { Height = 50, Width = 50 },
  68. new Border { Height = 50, Width = 50 },
  69. }
  70. };
  71. target.Measure(Size.Infinity);
  72. target.Arrange(new Rect(target.DesiredSize));
  73. Assert.Equal(new Size(200, 200), target.Bounds.Size);
  74. var rowBounds = target.Children[0].Bounds.Union(target.Children[1].Bounds);
  75. Assert.Equal(orientation switch
  76. {
  77. Orientation.Horizontal => new(100, 50),
  78. Orientation.Vertical => new(50, 100),
  79. _ => throw new NotImplementedException()
  80. }, rowBounds.Size);
  81. Assert.Equal((orientation, itemsAlignment) switch
  82. {
  83. (_, WrapPanelItemsAlignment.Start) => new(0, 0),
  84. (Orientation.Horizontal, WrapPanelItemsAlignment.Center) => new(50, 0),
  85. (Orientation.Vertical, WrapPanelItemsAlignment.Center) => new(0, 50),
  86. (Orientation.Horizontal, WrapPanelItemsAlignment.End) => new(100, 0),
  87. (Orientation.Vertical, WrapPanelItemsAlignment.End) => new(0, 100),
  88. _ => throw new NotImplementedException(),
  89. }, rowBounds.Position);
  90. }
  91. [Fact]
  92. public void Lays_Out_Vertically_Children_On_A_Single_Line()
  93. {
  94. var target = new WrapPanel()
  95. {
  96. Orientation = Orientation.Vertical,
  97. Height = 120,
  98. Children =
  99. {
  100. new Border { Height = 50, Width = 100 },
  101. new Border { Height = 50, Width = 100 },
  102. }
  103. };
  104. target.Measure(Size.Infinity);
  105. target.Arrange(new Rect(target.DesiredSize));
  106. Assert.Equal(new Size(100, 120), target.Bounds.Size);
  107. Assert.Equal(new Rect(0, 0, 100, 50), target.Children[0].Bounds);
  108. Assert.Equal(new Rect(0, 50, 100, 50), target.Children[1].Bounds);
  109. }
  110. [Fact]
  111. public void Lays_Out_Vertically_On_Separate_Lines()
  112. {
  113. var target = new WrapPanel()
  114. {
  115. Orientation = Orientation.Vertical,
  116. Height = 60,
  117. Children =
  118. {
  119. new Border { Height = 50, Width = 100 },
  120. new Border { Height = 50, Width = 100 },
  121. }
  122. };
  123. target.Measure(Size.Infinity);
  124. target.Arrange(new Rect(target.DesiredSize));
  125. Assert.Equal(new Size(200, 60), target.Bounds.Size);
  126. Assert.Equal(new Rect(0, 0, 100, 50), target.Children[0].Bounds);
  127. Assert.Equal(new Rect(100, 0, 100, 50), target.Children[1].Bounds);
  128. }
  129. [Fact]
  130. public void Lays_Out_Horizontally_On_Separate_Lines_With_Spacing()
  131. {
  132. var target = new WrapPanel
  133. {
  134. Width = 100,
  135. ItemSpacing = 10,
  136. LineSpacing = 20,
  137. Children =
  138. {
  139. new Border { Height = 50, Width = 60 }, // line 0
  140. new Border { Height = 50, Width = 30 }, // line 0
  141. new Border { Height = 50, Width = 70 }, // line 1
  142. new Border { Height = 50, Width = 30 }, // line 2
  143. }
  144. };
  145. target.Measure(Size.Infinity);
  146. target.Arrange(new Rect(target.DesiredSize));
  147. Assert.Equal(new Size(100, 190), target.Bounds.Size);
  148. Assert.Equal(new Rect(0, 0, 60, 50), target.Children[0].Bounds);
  149. Assert.Equal(new Rect(70, 0, 30, 50), target.Children[1].Bounds);
  150. Assert.Equal(new Rect(0, 70, 70, 50), target.Children[2].Bounds);
  151. Assert.Equal(new Rect(0, 140, 30, 50), target.Children[3].Bounds);
  152. }
  153. [Fact]
  154. public void Lays_Out_Horizontally_On_Separate_Lines_With_Spacing_Invisible()
  155. {
  156. var target = new WrapPanel
  157. {
  158. ItemSpacing = 10,
  159. Children =
  160. {
  161. new Border { Height = 50, Width = 60 }, // line 0
  162. new Border { Height = 50, Width = 30 , IsVisible = false }, // line 0
  163. new Border { Height = 50, Width = 50 }, // line 0
  164. }
  165. };
  166. target.Measure(Size.Infinity);
  167. target.Arrange(new Rect(target.DesiredSize));
  168. Assert.Equal(new Size(120, 50), target.Bounds.Size);
  169. Assert.Equal(new Rect(0, 0, 60, 50), target.Children[0].Bounds);
  170. Assert.Equal(new Rect(70, 0, 50, 50), target.Children[2].Bounds);
  171. }
  172. [Fact]
  173. public void Lays_Out_Horizontally_On_Separate_Lines_With_Spacing_Vertical()
  174. {
  175. var target = new WrapPanel
  176. {
  177. Height = 100,
  178. Orientation = Orientation.Vertical,
  179. ItemSpacing = 10,
  180. LineSpacing = 20,
  181. Children =
  182. {
  183. new Border { Width = 50, Height = 60 }, // line 0
  184. new Border { Width = 50, Height = 30 }, // line 0
  185. new Border { Width = 50, Height = 70 }, // line 1
  186. new Border { Width = 50, Height = 30 }, // line 2
  187. }
  188. };
  189. target.Measure(Size.Infinity);
  190. target.Arrange(new Rect(target.DesiredSize));
  191. Assert.Equal(new Size(190, 100), target.Bounds.Size);
  192. Assert.Equal(new Rect(0, 0, 50, 60), target.Children[0].Bounds);
  193. Assert.Equal(new Rect(0, 70, 50, 30), target.Children[1].Bounds);
  194. Assert.Equal(new Rect(70, 0, 50, 70), target.Children[2].Bounds);
  195. Assert.Equal(new Rect(140, 0, 50, 30), target.Children[3].Bounds);
  196. }
  197. [Fact]
  198. public void Applies_ItemWidth_And_ItemHeight_Properties()
  199. {
  200. var target = new WrapPanel()
  201. {
  202. Orientation = Orientation.Horizontal,
  203. Width = 50,
  204. ItemWidth = 20,
  205. ItemHeight = 15,
  206. Children =
  207. {
  208. new Border(),
  209. new Border(),
  210. }
  211. };
  212. target.Measure(Size.Infinity);
  213. target.Arrange(new Rect(target.DesiredSize));
  214. Assert.Equal(new Size(50, 15), target.Bounds.Size);
  215. Assert.Equal(new Rect(0, 0, 20, 15), target.Children[0].Bounds);
  216. Assert.Equal(new Rect(20, 0, 20, 15), target.Children[1].Bounds);
  217. }
  218. [Fact]
  219. public void Zero_Size_Visible_Child()
  220. {
  221. var target = new WrapPanel()
  222. {
  223. Orientation = Orientation.Horizontal,
  224. Width = 50,
  225. ItemSpacing = 10,
  226. LineSpacing = 10,
  227. Children =
  228. {
  229. new Border(), // line 0
  230. new Border // line 1
  231. {
  232. Width = 50,
  233. Height = 50
  234. },
  235. }
  236. };
  237. target.Measure(Size.Infinity);
  238. target.Arrange(new Rect(target.DesiredSize));
  239. Assert.Equal(new Size(50, 60), target.Bounds.Size);
  240. Assert.Equal(new Rect(0, 0, 0, 0), target.Children[0].Bounds);
  241. Assert.Equal(new Rect(0, 10, 50, 50), target.Children[1].Bounds);
  242. }
  243. [Fact]
  244. void ItemWidth_Trigger_InvalidateMeasure()
  245. {
  246. var target = new WrapPanel();
  247. target.Measure(new Size(10, 10));
  248. Assert.True(target.IsMeasureValid);
  249. target.ItemWidth = 1;
  250. Assert.False(target.IsMeasureValid);
  251. }
  252. [Fact]
  253. void ItemHeight_Trigger_InvalidateMeasure()
  254. {
  255. var target = new WrapPanel();
  256. target.Measure(new Size(10, 10));
  257. Assert.True(target.IsMeasureValid);
  258. target.ItemHeight = 1;
  259. Assert.False(target.IsMeasureValid);
  260. }
  261. }
  262. }