ContainerTests.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using Avalonia.Base.UnitTests.Layout;
  3. using Avalonia.Controls;
  4. using Avalonia.Styling;
  5. using Avalonia.UnitTests;
  6. using Xunit;
  7. namespace Avalonia.Base.UnitTests.Styling
  8. {
  9. public class ContainerTests
  10. {
  11. [Fact]
  12. public void Container_Cannot_Be_Added_To_Style_Children()
  13. {
  14. var target = new ContainerQuery();
  15. var style = new Style();
  16. Assert.Throws<InvalidOperationException>(() => style.Children.Add(target));
  17. }
  18. [Fact]
  19. public void Container_Width_Queries_Matches()
  20. {
  21. using var app = UnitTestApplication.Start();
  22. var root = new LayoutTestRoot()
  23. {
  24. ClientSize = new Size(400, 400)
  25. };
  26. var containerQuery1 = new ContainerQuery(x => new WidthQuery(x, StyleQueryComparisonOperator.LessThanOrEquals, 500));
  27. containerQuery1.Children.Add(new Style(x => x.Is<Border>())
  28. {
  29. Setters = { new Setter(Control.WidthProperty, 200.0) }
  30. });
  31. var containerQuery2 = new ContainerQuery(x => new WidthQuery(x, StyleQueryComparisonOperator.GreaterThan, 500));
  32. containerQuery2.Children.Add(new Style(x => x.Is<Border>())
  33. {
  34. Setters = { new Setter(Control.WidthProperty, 500.0) }
  35. });
  36. root.Styles.Add(containerQuery1);
  37. root.Styles.Add(containerQuery2);
  38. var child = new Border()
  39. {
  40. Name = "Child",
  41. HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch
  42. };
  43. var stack = new StackPanel();
  44. stack.Children.Add(child);
  45. var border = new Border()
  46. {
  47. HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch,
  48. VerticalAlignment = Avalonia.Layout.VerticalAlignment.Stretch,
  49. Child = stack,
  50. Name = "Parent"
  51. };
  52. Container.SetSizing(border, Avalonia.Styling.ContainerSizing.Width);
  53. root.Child = border;
  54. root.LayoutManager.ExecuteInitialLayoutPass();
  55. Assert.Equal(200, child.Width);
  56. root.ClientSize = new Size(600, 600);
  57. root.InvalidateMeasure();
  58. root.LayoutManager.ExecuteLayoutPass();
  59. Assert.Equal(500, child.Width);
  60. }
  61. [Fact]
  62. public void Container_Height_Queries_Matches()
  63. {
  64. using var app = UnitTestApplication.Start();
  65. var root = new LayoutTestRoot()
  66. {
  67. ClientSize = new Size(400, 400)
  68. };
  69. var containerQuery1 = new ContainerQuery(x => new HeightQuery(x, StyleQueryComparisonOperator.LessThanOrEquals, 500));
  70. containerQuery1.Children.Add(new Style(x => x.Is<Border>())
  71. {
  72. Setters = { new Setter(Control.HeightProperty, 200.0) }
  73. });
  74. var containerQuery2 = new ContainerQuery(x => new HeightQuery(x, StyleQueryComparisonOperator.GreaterThan, 500));
  75. containerQuery2.Children.Add(new Style(x => x.Is<Border>())
  76. {
  77. Setters = { new Setter(Control.HeightProperty, 500.0) }
  78. });
  79. root.Styles.Add(containerQuery1);
  80. root.Styles.Add(containerQuery2);
  81. var child = new Border()
  82. {
  83. Name = "Child",
  84. VerticalAlignment = Avalonia.Layout.VerticalAlignment.Stretch
  85. };
  86. var stack = new StackPanel();
  87. stack.Children.Add(child);
  88. var border = new Border()
  89. {
  90. HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch,
  91. VerticalAlignment = Avalonia.Layout.VerticalAlignment.Stretch,
  92. Child = stack,
  93. Name = "Parent"
  94. };
  95. Container.SetSizing(border, Avalonia.Styling.ContainerSizing.Height);
  96. root.Child = border;
  97. root.LayoutManager.ExecuteInitialLayoutPass();
  98. Assert.Equal(200, child.Height);
  99. root.ClientSize = new Size(600, 600);
  100. root.InvalidateMeasure();
  101. root.LayoutManager.ExecuteLayoutPass();
  102. Assert.Equal(500, child.Height);
  103. }
  104. [Fact]
  105. public void Container_Width_Queries_Matches_Name()
  106. {
  107. using var app = UnitTestApplication.Start();
  108. var root = new LayoutTestRoot()
  109. {
  110. ClientSize = new Size(600, 600)
  111. };
  112. var containerQuery1 = new ContainerQuery(x => new WidthQuery(x, StyleQueryComparisonOperator.LessThanOrEquals, 500));
  113. containerQuery1.Children.Add(new Style(x => x.Is<Border>())
  114. {
  115. Setters = { new Setter(Control.WidthProperty, 200.0) }
  116. });
  117. var containerQuery2 = new ContainerQuery(x => new WidthQuery(x, StyleQueryComparisonOperator.LessThanOrEquals, 500), "TEST");
  118. containerQuery2.Children.Add(new Style(x => x.Is<Border>())
  119. {
  120. Setters = { new Setter(Control.WidthProperty, 300.0) }
  121. });
  122. root.Styles.Add(containerQuery1);
  123. root.Styles.Add(containerQuery2);
  124. var child = new Border()
  125. {
  126. Name = "Child",
  127. HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch
  128. };
  129. var controlInner = new ContentControl()
  130. {
  131. Width = 400,
  132. Height = 400,
  133. Content = child,
  134. Name = "Inner"
  135. };
  136. Container.SetSizing(controlInner, Avalonia.Styling.ContainerSizing.Width);
  137. Container.SetName(controlInner, "TEST");
  138. var stack = new StackPanel();
  139. stack.Children.Add(controlInner);
  140. var border = new Border()
  141. {
  142. HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch,
  143. VerticalAlignment = Avalonia.Layout.VerticalAlignment.Stretch,
  144. Child = stack,
  145. Name = "Parent"
  146. };
  147. Container.SetSizing(border, Avalonia.Styling.ContainerSizing.Width);
  148. root.Child = border;
  149. root.LayoutManager.ExecuteInitialLayoutPass();
  150. root.LayoutManager.ExecuteLayoutPass();
  151. Assert.Equal(300, child.Width);
  152. }
  153. [Fact]
  154. public void Container_Height_Queries_Matches_Name()
  155. {
  156. using var app = UnitTestApplication.Start();
  157. var root = new LayoutTestRoot()
  158. {
  159. ClientSize = new Size(600, 600)
  160. };
  161. var containerQuery1 = new ContainerQuery(x => new HeightQuery(x, StyleQueryComparisonOperator.LessThanOrEquals, 500));
  162. containerQuery1.Children.Add(new Style(x => x.Is<Border>())
  163. {
  164. Setters = { new Setter(Control.HeightProperty, 200.0) }
  165. });
  166. var containerQuery2 = new ContainerQuery(x => new HeightQuery(x, StyleQueryComparisonOperator.LessThanOrEquals, 450), "TEST");
  167. containerQuery2.Children.Add(new Style(x => x.Is<Border>())
  168. {
  169. Setters = { new Setter(Control.HeightProperty, 300.0) }
  170. });
  171. root.Styles.Add(containerQuery1);
  172. root.Styles.Add(containerQuery2);
  173. var child = new Border()
  174. {
  175. Name = "Child",
  176. VerticalAlignment = Avalonia.Layout.VerticalAlignment.Stretch
  177. };
  178. var controlInner = new ContentControl()
  179. {
  180. Width = 400,
  181. Height = 400,
  182. Content = child,
  183. Name = "Inner"
  184. };
  185. Container.SetSizing(controlInner, Avalonia.Styling.ContainerSizing.Height);
  186. Container.SetName(controlInner, "TEST");
  187. var stack = new StackPanel();
  188. stack.Children.Add(controlInner);
  189. var border = new Border()
  190. {
  191. HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch,
  192. VerticalAlignment = Avalonia.Layout.VerticalAlignment.Stretch,
  193. Child = stack,
  194. Name = "Parent"
  195. };
  196. Container.SetSizing(border, Avalonia.Styling.ContainerSizing.Height);
  197. root.Child = border;
  198. root.LayoutManager.ExecuteInitialLayoutPass();
  199. root.LayoutManager.ExecuteLayoutPass();
  200. Assert.Equal(300, child.Height);
  201. }
  202. }
  203. }