LayoutManagerTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Avalonia.Controls;
  4. using Xunit;
  5. namespace Avalonia.Layout.UnitTests
  6. {
  7. public class LayoutManagerTests
  8. {
  9. [Fact]
  10. public void Measures_And_Arranges_InvalidateMeasured_Control()
  11. {
  12. var control = new LayoutTestControl();
  13. var root = new LayoutTestRoot { Child = control };
  14. root.LayoutManager.ExecuteInitialLayoutPass();
  15. control.Measured = control.Arranged = false;
  16. control.InvalidateMeasure();
  17. root.LayoutManager.ExecuteLayoutPass();
  18. Assert.True(control.Measured);
  19. Assert.True(control.Arranged);
  20. }
  21. [Fact]
  22. public void Arranges_InvalidateArranged_Control()
  23. {
  24. var control = new LayoutTestControl();
  25. var root = new LayoutTestRoot { Child = control };
  26. root.LayoutManager.ExecuteInitialLayoutPass();
  27. control.Measured = control.Arranged = false;
  28. control.InvalidateArrange();
  29. root.LayoutManager.ExecuteLayoutPass();
  30. Assert.False(control.Measured);
  31. Assert.True(control.Arranged);
  32. }
  33. [Fact]
  34. public void Measures_Parent_Of_Newly_Added_Control()
  35. {
  36. var control = new LayoutTestControl();
  37. var root = new LayoutTestRoot();
  38. root.LayoutManager.ExecuteInitialLayoutPass();
  39. root.Child = control;
  40. root.Measured = root.Arranged = false;
  41. root.LayoutManager.ExecuteLayoutPass();
  42. Assert.True(root.Measured);
  43. Assert.True(root.Arranged);
  44. Assert.True(control.Measured);
  45. Assert.True(control.Arranged);
  46. }
  47. [Fact]
  48. public void Measures_In_Correct_Order()
  49. {
  50. LayoutTestControl control1;
  51. LayoutTestControl control2;
  52. var root = new LayoutTestRoot
  53. {
  54. Child = control1 = new LayoutTestControl
  55. {
  56. Child = control2 = new LayoutTestControl(),
  57. }
  58. };
  59. var order = new List<ILayoutable>();
  60. Size MeasureOverride(ILayoutable control, Size size)
  61. {
  62. order.Add(control);
  63. return new Size(10, 10);
  64. }
  65. root.DoMeasureOverride = MeasureOverride;
  66. control1.DoMeasureOverride = MeasureOverride;
  67. control2.DoMeasureOverride = MeasureOverride;
  68. root.LayoutManager.ExecuteInitialLayoutPass();
  69. control2.InvalidateMeasure();
  70. control1.InvalidateMeasure();
  71. root.InvalidateMeasure();
  72. order.Clear();
  73. root.LayoutManager.ExecuteLayoutPass();
  74. Assert.Equal(new ILayoutable[] { root, control1, control2 }, order);
  75. }
  76. [Fact]
  77. public void Measures_Root_And_Grandparent_In_Correct_Order()
  78. {
  79. LayoutTestControl control1;
  80. LayoutTestControl control2;
  81. var root = new LayoutTestRoot
  82. {
  83. Child = control1 = new LayoutTestControl
  84. {
  85. Child = control2 = new LayoutTestControl(),
  86. }
  87. };
  88. var order = new List<ILayoutable>();
  89. Size MeasureOverride(ILayoutable control, Size size)
  90. {
  91. order.Add(control);
  92. return new Size(10, 10);
  93. }
  94. root.DoMeasureOverride = MeasureOverride;
  95. control1.DoMeasureOverride = MeasureOverride;
  96. control2.DoMeasureOverride = MeasureOverride;
  97. root.LayoutManager.ExecuteInitialLayoutPass();
  98. control2.InvalidateMeasure();
  99. root.InvalidateMeasure();
  100. order.Clear();
  101. root.LayoutManager.ExecuteLayoutPass();
  102. Assert.Equal(new ILayoutable[] { root, control2 }, order);
  103. }
  104. [Fact]
  105. public void Doesnt_Measure_Non_Invalidated_Root()
  106. {
  107. var control = new LayoutTestControl();
  108. var root = new LayoutTestRoot { Child = control };
  109. root.LayoutManager.ExecuteInitialLayoutPass();
  110. root.Measured = root.Arranged = false;
  111. control.Measured = control.Arranged = false;
  112. control.InvalidateMeasure();
  113. root.LayoutManager.ExecuteLayoutPass();
  114. Assert.False(root.Measured);
  115. Assert.False(root.Arranged);
  116. Assert.True(control.Measured);
  117. Assert.True(control.Arranged);
  118. }
  119. [Fact]
  120. public void Doesnt_Measure_Removed_Control()
  121. {
  122. var control = new LayoutTestControl();
  123. var root = new LayoutTestRoot { Child = control };
  124. root.LayoutManager.ExecuteInitialLayoutPass();
  125. control.Measured = control.Arranged = false;
  126. control.InvalidateMeasure();
  127. root.Child = null;
  128. root.LayoutManager.ExecuteLayoutPass();
  129. Assert.False(control.Measured);
  130. Assert.False(control.Arranged);
  131. }
  132. [Fact]
  133. public void Measures_Root_With_Infinity()
  134. {
  135. var root = new LayoutTestRoot();
  136. var availableSize = default(Size);
  137. // Should not measure with this size.
  138. root.MaxClientSize = new Size(123, 456);
  139. root.DoMeasureOverride = (_, s) =>
  140. {
  141. availableSize = s;
  142. return new Size(100, 100);
  143. };
  144. root.LayoutManager.ExecuteInitialLayoutPass();
  145. Assert.Equal(Size.Infinity, availableSize);
  146. }
  147. [Fact]
  148. public void Arranges_Root_With_DesiredSize()
  149. {
  150. var root = new LayoutTestRoot
  151. {
  152. Width = 100,
  153. Height = 100,
  154. };
  155. var arrangeSize = default(Size);
  156. root.DoArrangeOverride = (_, s) =>
  157. {
  158. arrangeSize = s;
  159. return s;
  160. };
  161. root.LayoutManager.ExecuteInitialLayoutPass();
  162. Assert.Equal(new Size(100, 100), arrangeSize);
  163. root.Width = 120;
  164. root.LayoutManager.ExecuteLayoutPass();
  165. Assert.Equal(new Size(120, 100), arrangeSize);
  166. }
  167. [Fact]
  168. public void Invalidating_Child_Remeasures_Parent()
  169. {
  170. Border border;
  171. StackPanel panel;
  172. var root = new LayoutTestRoot
  173. {
  174. Child = panel = new StackPanel
  175. {
  176. Children =
  177. {
  178. (border = new Border())
  179. }
  180. }
  181. };
  182. root.LayoutManager.ExecuteInitialLayoutPass();
  183. Assert.Equal(new Size(0, 0), root.DesiredSize);
  184. border.Width = 100;
  185. border.Height = 100;
  186. root.LayoutManager.ExecuteLayoutPass();
  187. Assert.Equal(new Size(100, 100), panel.DesiredSize);
  188. }
  189. [Fact]
  190. public void LayoutManager_Should_Prevent_Infinite_Loop_On_Measure()
  191. {
  192. var control = new LayoutTestControl();
  193. var root = new LayoutTestRoot { Child = control };
  194. root.LayoutManager.ExecuteInitialLayoutPass();
  195. control.Measured = false;
  196. int cnt = 0;
  197. int maxcnt = 100;
  198. control.DoMeasureOverride = (l, s) =>
  199. {
  200. //emulate a problem in the logic of a control that triggers
  201. //invalidate measure during measure
  202. //it can lead to an infinite loop in layoutmanager
  203. if (++cnt < maxcnt)
  204. {
  205. control.InvalidateMeasure();
  206. }
  207. return new Size(100, 100);
  208. };
  209. control.InvalidateMeasure();
  210. root.LayoutManager.ExecuteLayoutPass();
  211. Assert.True(cnt < 100);
  212. }
  213. [Fact]
  214. public void LayoutManager_Should_Prevent_Infinite_Loop_On_Arrange()
  215. {
  216. var control = new LayoutTestControl();
  217. var root = new LayoutTestRoot { Child = control };
  218. root.LayoutManager.ExecuteInitialLayoutPass();
  219. control.Arranged = false;
  220. int cnt = 0;
  221. int maxcnt = 100;
  222. control.DoArrangeOverride = (l, s) =>
  223. {
  224. //emulate a problem in the logic of a control that triggers
  225. //invalidate measure during arrange
  226. //it can lead to infinity loop in layoutmanager
  227. if (++cnt < maxcnt)
  228. {
  229. control.InvalidateArrange();
  230. }
  231. return new Size(100, 100);
  232. };
  233. control.InvalidateArrange();
  234. root.LayoutManager.ExecuteLayoutPass();
  235. Assert.True(cnt < 100);
  236. }
  237. [Fact]
  238. public void LayoutManager_Should_Properly_Arrange_Visuals_Even_When_There_Are_Issues_With_Previous_Arranged()
  239. {
  240. var nonArrageableTargets = Enumerable.Range(1, 10).Select(_ => new LayoutTestControl()).ToArray();
  241. var targets = Enumerable.Range(1, 10).Select(_ => new LayoutTestControl()).ToArray();
  242. StackPanel panel;
  243. var root = new LayoutTestRoot
  244. {
  245. Child = panel = new StackPanel()
  246. };
  247. panel.Children.AddRange(nonArrageableTargets);
  248. panel.Children.AddRange(targets);
  249. root.LayoutManager.ExecuteInitialLayoutPass();
  250. foreach (var c in panel.Children.OfType<LayoutTestControl>())
  251. {
  252. c.Measured = c.Arranged = false;
  253. c.InvalidateMeasure();
  254. }
  255. foreach (var c in nonArrageableTargets)
  256. {
  257. c.DoArrangeOverride = (l, s) =>
  258. {
  259. //emulate a problem in the logic of a control that triggers
  260. //invalidate measure during arrange
  261. c.InvalidateMeasure();
  262. return new Size(100, 100);
  263. };
  264. }
  265. root.LayoutManager.ExecuteLayoutPass();
  266. //altough nonArrageableTargets has rubbish logic and can't be measured/arranged properly
  267. //layoutmanager should process properly other visuals
  268. Assert.All(targets, c => Assert.True(c.Arranged));
  269. }
  270. [Fact]
  271. public void LayoutManager_Should_Recover_From_Infinite_Loop_On_Measure()
  272. {
  273. // Test for issue #3041.
  274. var control = new LayoutTestControl();
  275. var root = new LayoutTestRoot { Child = control };
  276. root.LayoutManager.ExecuteInitialLayoutPass();
  277. control.Measured = false;
  278. control.DoMeasureOverride = (l, s) =>
  279. {
  280. control.InvalidateMeasure();
  281. return new Size(100, 100);
  282. };
  283. control.InvalidateMeasure();
  284. root.LayoutManager.ExecuteLayoutPass();
  285. // This is the important part: running a second layout pass in which we exceed the maximum
  286. // retries causes LayoutQueue<T>.Info.Count to exceed _maxEnqueueCountPerLoop.
  287. root.LayoutManager.ExecuteLayoutPass();
  288. control.Measured = false;
  289. control.DoMeasureOverride = null;
  290. root.LayoutManager.ExecuteLayoutPass();
  291. Assert.True(control.Measured);
  292. Assert.True(control.IsMeasureValid);
  293. }
  294. [Fact]
  295. public void Calling_ExecuteLayoutPass_From_ExecuteInitialLayoutPass_Does_Not_Break_Measure()
  296. {
  297. // Test for issue #3550.
  298. var control = new LayoutTestControl();
  299. var root = new LayoutTestRoot { Child = control };
  300. var count = 0;
  301. root.LayoutManager.ExecuteInitialLayoutPass();
  302. control.Measured = false;
  303. control.DoMeasureOverride = (l, s) =>
  304. {
  305. if (count++ == 0)
  306. {
  307. control.InvalidateMeasure();
  308. root.LayoutManager.ExecuteLayoutPass();
  309. return new Size(100, 100);
  310. }
  311. else
  312. {
  313. return new Size(200, 200);
  314. }
  315. };
  316. root.InvalidateMeasure();
  317. control.InvalidateMeasure();
  318. root.LayoutManager.ExecuteInitialLayoutPass();
  319. Assert.Equal(new Size(200, 200), control.Bounds.Size);
  320. Assert.Equal(new Size(200, 200), control.DesiredSize);
  321. }
  322. }
  323. }