WrapPanelTests.cs 10 KB

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