LayoutManagerTests.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 Avalonia.Controls;
  4. using Avalonia.UnitTests;
  5. using System;
  6. using Xunit;
  7. using System.Collections.Generic;
  8. namespace Avalonia.Layout.UnitTests
  9. {
  10. public class LayoutManagerTests
  11. {
  12. [Fact]
  13. public void Measures_And_Arranges_InvalidateMeasured_Control()
  14. {
  15. var target = new LayoutManager();
  16. using (Start(target))
  17. {
  18. var control = new LayoutTestControl();
  19. var root = new LayoutTestRoot { Child = control };
  20. target.ExecuteInitialLayoutPass(root);
  21. control.Measured = control.Arranged = false;
  22. control.InvalidateMeasure();
  23. target.ExecuteLayoutPass();
  24. Assert.True(control.Measured);
  25. Assert.True(control.Arranged);
  26. }
  27. }
  28. [Fact]
  29. public void Arranges_InvalidateArranged_Control()
  30. {
  31. var target = new LayoutManager();
  32. using (Start(target))
  33. {
  34. var control = new LayoutTestControl();
  35. var root = new LayoutTestRoot { Child = control };
  36. target.ExecuteInitialLayoutPass(root);
  37. control.Measured = control.Arranged = false;
  38. control.InvalidateArrange();
  39. target.ExecuteLayoutPass();
  40. Assert.False(control.Measured);
  41. Assert.True(control.Arranged);
  42. }
  43. }
  44. [Fact]
  45. public void Measures_Parent_Of_Newly_Added_Control()
  46. {
  47. var target = new LayoutManager();
  48. using (Start(target))
  49. {
  50. var control = new LayoutTestControl();
  51. var root = new LayoutTestRoot();
  52. target.ExecuteInitialLayoutPass(root);
  53. root.Child = control;
  54. root.Measured = root.Arranged = false;
  55. target.ExecuteLayoutPass();
  56. Assert.True(root.Measured);
  57. Assert.True(root.Arranged);
  58. Assert.True(control.Measured);
  59. Assert.True(control.Arranged);
  60. }
  61. }
  62. [Fact]
  63. public void Measures_In_Correct_Order()
  64. {
  65. var target = new LayoutManager();
  66. using (Start(target))
  67. {
  68. LayoutTestControl control1;
  69. LayoutTestControl control2;
  70. var root = new LayoutTestRoot
  71. {
  72. Child = control1 = new LayoutTestControl
  73. {
  74. Child = control2 = new LayoutTestControl(),
  75. }
  76. };
  77. var order = new List<ILayoutable>();
  78. Size MeasureOverride(ILayoutable control, Size size)
  79. {
  80. order.Add(control);
  81. return new Size(10, 10);
  82. }
  83. root.DoMeasureOverride = MeasureOverride;
  84. control1.DoMeasureOverride = MeasureOverride;
  85. control2.DoMeasureOverride = MeasureOverride;
  86. target.ExecuteInitialLayoutPass(root);
  87. control2.InvalidateMeasure();
  88. control1.InvalidateMeasure();
  89. root.InvalidateMeasure();
  90. order.Clear();
  91. target.ExecuteLayoutPass();
  92. Assert.Equal(new ILayoutable[] { root, control1, control2 }, order);
  93. }
  94. }
  95. [Fact]
  96. public void Measures_Root_And_Grandparent_In_Correct_Order()
  97. {
  98. var target = new LayoutManager();
  99. using (Start(target))
  100. {
  101. LayoutTestControl control1;
  102. LayoutTestControl control2;
  103. var root = new LayoutTestRoot
  104. {
  105. Child = control1 = new LayoutTestControl
  106. {
  107. Child = control2 = new LayoutTestControl(),
  108. }
  109. };
  110. var order = new List<ILayoutable>();
  111. Size MeasureOverride(ILayoutable control, Size size)
  112. {
  113. order.Add(control);
  114. return new Size(10, 10);
  115. }
  116. root.DoMeasureOverride = MeasureOverride;
  117. control1.DoMeasureOverride = MeasureOverride;
  118. control2.DoMeasureOverride = MeasureOverride;
  119. target.ExecuteInitialLayoutPass(root);
  120. control2.InvalidateMeasure();
  121. root.InvalidateMeasure();
  122. order.Clear();
  123. target.ExecuteLayoutPass();
  124. Assert.Equal(new ILayoutable[] { root, control2 }, order);
  125. }
  126. }
  127. [Fact]
  128. public void Doesnt_Measure_Non_Invalidated_Root()
  129. {
  130. var target = new LayoutManager();
  131. using (Start(target))
  132. {
  133. var control = new LayoutTestControl();
  134. var root = new LayoutTestRoot { Child = control };
  135. target.ExecuteInitialLayoutPass(root);
  136. root.Measured = root.Arranged = false;
  137. control.Measured = control.Arranged = false;
  138. control.InvalidateMeasure();
  139. target.ExecuteLayoutPass();
  140. Assert.False(root.Measured);
  141. Assert.False(root.Arranged);
  142. Assert.True(control.Measured);
  143. Assert.True(control.Arranged);
  144. }
  145. }
  146. [Fact]
  147. public void Doesnt_Measure_Removed_Control()
  148. {
  149. var target = new LayoutManager();
  150. using (Start(target))
  151. {
  152. var control = new LayoutTestControl();
  153. var root = new LayoutTestRoot { Child = control };
  154. target.ExecuteInitialLayoutPass(root);
  155. control.Measured = control.Arranged = false;
  156. control.InvalidateMeasure();
  157. root.Child = null;
  158. target.ExecuteLayoutPass();
  159. Assert.False(control.Measured);
  160. Assert.False(control.Arranged);
  161. }
  162. }
  163. [Fact]
  164. public void Measures_Root_With_Infinity()
  165. {
  166. var target = new LayoutManager();
  167. using (Start(target))
  168. {
  169. var root = new LayoutTestRoot();
  170. var availableSize = default(Size);
  171. // Should not measure with this size.
  172. root.MaxClientSize = new Size(123, 456);
  173. root.DoMeasureOverride = (_, s) =>
  174. {
  175. availableSize = s;
  176. return new Size(100, 100);
  177. };
  178. target.ExecuteInitialLayoutPass(root);
  179. Assert.Equal(Size.Infinity, availableSize);
  180. }
  181. }
  182. [Fact]
  183. public void Arranges_Root_With_DesiredSize()
  184. {
  185. var target = new LayoutManager();
  186. using (Start(target))
  187. {
  188. var root = new LayoutTestRoot
  189. {
  190. Width = 100,
  191. Height = 100,
  192. };
  193. var arrangeSize = default(Size);
  194. root.DoArrangeOverride = (_, s) =>
  195. {
  196. arrangeSize = s;
  197. return s;
  198. };
  199. target.ExecuteInitialLayoutPass(root);
  200. Assert.Equal(new Size(100, 100), arrangeSize);
  201. root.Width = 120;
  202. target.ExecuteLayoutPass();
  203. Assert.Equal(new Size(120, 100), arrangeSize);
  204. }
  205. }
  206. [Fact]
  207. public void Invalidating_Child_Remeasures_Parent()
  208. {
  209. var target = new LayoutManager();
  210. using (Start(target))
  211. {
  212. AvaloniaLocator.CurrentMutable.Bind<ILayoutManager>().ToConstant(target);
  213. Border border;
  214. StackPanel panel;
  215. var root = new LayoutTestRoot
  216. {
  217. Child = panel = new StackPanel
  218. {
  219. Children =
  220. {
  221. (border = new Border())
  222. }
  223. }
  224. };
  225. target.ExecuteInitialLayoutPass(root);
  226. Assert.Equal(new Size(0, 0), root.DesiredSize);
  227. border.Width = 100;
  228. border.Height = 100;
  229. target.ExecuteLayoutPass();
  230. Assert.Equal(new Size(100, 100), panel.DesiredSize);
  231. }
  232. }
  233. private IDisposable Start(LayoutManager layoutManager)
  234. {
  235. var result = AvaloniaLocator.EnterScope();
  236. AvaloniaLocator.CurrentMutable.Bind<ILayoutManager>().ToConstant(layoutManager);
  237. return result;
  238. }
  239. }
  240. }