1
0

LayoutManagerTests.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 control = new LayoutTestControl();
  16. var root = new LayoutTestRoot { Child = control };
  17. root.LayoutManager.ExecuteInitialLayoutPass(root);
  18. control.Measured = control.Arranged = false;
  19. control.InvalidateMeasure();
  20. root.LayoutManager.ExecuteLayoutPass();
  21. Assert.True(control.Measured);
  22. Assert.True(control.Arranged);
  23. }
  24. [Fact]
  25. public void Arranges_InvalidateArranged_Control()
  26. {
  27. var control = new LayoutTestControl();
  28. var root = new LayoutTestRoot { Child = control };
  29. root.LayoutManager.ExecuteInitialLayoutPass(root);
  30. control.Measured = control.Arranged = false;
  31. control.InvalidateArrange();
  32. root.LayoutManager.ExecuteLayoutPass();
  33. Assert.False(control.Measured);
  34. Assert.True(control.Arranged);
  35. }
  36. [Fact]
  37. public void Measures_Parent_Of_Newly_Added_Control()
  38. {
  39. var control = new LayoutTestControl();
  40. var root = new LayoutTestRoot();
  41. root.LayoutManager.ExecuteInitialLayoutPass(root);
  42. root.Child = control;
  43. root.Measured = root.Arranged = false;
  44. root.LayoutManager.ExecuteLayoutPass();
  45. Assert.True(root.Measured);
  46. Assert.True(root.Arranged);
  47. Assert.True(control.Measured);
  48. Assert.True(control.Arranged);
  49. }
  50. [Fact]
  51. public void Measures_In_Correct_Order()
  52. {
  53. LayoutTestControl control1;
  54. LayoutTestControl control2;
  55. var root = new LayoutTestRoot
  56. {
  57. Child = control1 = new LayoutTestControl
  58. {
  59. Child = control2 = new LayoutTestControl(),
  60. }
  61. };
  62. var order = new List<ILayoutable>();
  63. Size MeasureOverride(ILayoutable control, Size size)
  64. {
  65. order.Add(control);
  66. return new Size(10, 10);
  67. }
  68. root.DoMeasureOverride = MeasureOverride;
  69. control1.DoMeasureOverride = MeasureOverride;
  70. control2.DoMeasureOverride = MeasureOverride;
  71. root.LayoutManager.ExecuteInitialLayoutPass(root);
  72. control2.InvalidateMeasure();
  73. control1.InvalidateMeasure();
  74. root.InvalidateMeasure();
  75. order.Clear();
  76. root.LayoutManager.ExecuteLayoutPass();
  77. Assert.Equal(new ILayoutable[] { root, control1, control2 }, order);
  78. }
  79. [Fact]
  80. public void Measures_Root_And_Grandparent_In_Correct_Order()
  81. {
  82. LayoutTestControl control1;
  83. LayoutTestControl control2;
  84. var root = new LayoutTestRoot
  85. {
  86. Child = control1 = new LayoutTestControl
  87. {
  88. Child = control2 = new LayoutTestControl(),
  89. }
  90. };
  91. var order = new List<ILayoutable>();
  92. Size MeasureOverride(ILayoutable control, Size size)
  93. {
  94. order.Add(control);
  95. return new Size(10, 10);
  96. }
  97. root.DoMeasureOverride = MeasureOverride;
  98. control1.DoMeasureOverride = MeasureOverride;
  99. control2.DoMeasureOverride = MeasureOverride;
  100. root.LayoutManager.ExecuteInitialLayoutPass(root);
  101. control2.InvalidateMeasure();
  102. root.InvalidateMeasure();
  103. order.Clear();
  104. root.LayoutManager.ExecuteLayoutPass();
  105. Assert.Equal(new ILayoutable[] { root, control2 }, order);
  106. }
  107. [Fact]
  108. public void Doesnt_Measure_Non_Invalidated_Root()
  109. {
  110. var control = new LayoutTestControl();
  111. var root = new LayoutTestRoot { Child = control };
  112. root.LayoutManager.ExecuteInitialLayoutPass(root);
  113. root.Measured = root.Arranged = false;
  114. control.Measured = control.Arranged = false;
  115. control.InvalidateMeasure();
  116. root.LayoutManager.ExecuteLayoutPass();
  117. Assert.False(root.Measured);
  118. Assert.False(root.Arranged);
  119. Assert.True(control.Measured);
  120. Assert.True(control.Arranged);
  121. }
  122. [Fact]
  123. public void Doesnt_Measure_Removed_Control()
  124. {
  125. var control = new LayoutTestControl();
  126. var root = new LayoutTestRoot { Child = control };
  127. root.LayoutManager.ExecuteInitialLayoutPass(root);
  128. control.Measured = control.Arranged = false;
  129. control.InvalidateMeasure();
  130. root.Child = null;
  131. root.LayoutManager.ExecuteLayoutPass();
  132. Assert.False(control.Measured);
  133. Assert.False(control.Arranged);
  134. }
  135. [Fact]
  136. public void Measures_Root_With_Infinity()
  137. {
  138. var root = new LayoutTestRoot();
  139. var availableSize = default(Size);
  140. // Should not measure with this size.
  141. root.MaxClientSize = new Size(123, 456);
  142. root.DoMeasureOverride = (_, s) =>
  143. {
  144. availableSize = s;
  145. return new Size(100, 100);
  146. };
  147. root.LayoutManager.ExecuteInitialLayoutPass(root);
  148. Assert.Equal(Size.Infinity, availableSize);
  149. }
  150. [Fact]
  151. public void Arranges_Root_With_DesiredSize()
  152. {
  153. var root = new LayoutTestRoot
  154. {
  155. Width = 100,
  156. Height = 100,
  157. };
  158. var arrangeSize = default(Size);
  159. root.DoArrangeOverride = (_, s) =>
  160. {
  161. arrangeSize = s;
  162. return s;
  163. };
  164. root.LayoutManager.ExecuteInitialLayoutPass(root);
  165. Assert.Equal(new Size(100, 100), arrangeSize);
  166. root.Width = 120;
  167. root.LayoutManager.ExecuteLayoutPass();
  168. Assert.Equal(new Size(120, 100), arrangeSize);
  169. }
  170. [Fact]
  171. public void Invalidating_Child_Remeasures_Parent()
  172. {
  173. Border border;
  174. StackPanel panel;
  175. var root = new LayoutTestRoot
  176. {
  177. Child = panel = new StackPanel
  178. {
  179. Children =
  180. {
  181. (border = new Border())
  182. }
  183. }
  184. };
  185. root.LayoutManager.ExecuteInitialLayoutPass(root);
  186. Assert.Equal(new Size(0, 0), root.DesiredSize);
  187. border.Width = 100;
  188. border.Height = 100;
  189. root.LayoutManager.ExecuteLayoutPass();
  190. Assert.Equal(new Size(100, 100), panel.DesiredSize);
  191. }
  192. }
  193. }