Skip to content

Commit 5b3da4c

Browse files
committed
add tests
1 parent e47bce3 commit 5b3da4c

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using System;
2+
using Avalonia.Base.UnitTests.Layout;
3+
using Avalonia.Controls;
4+
using Avalonia.Styling;
5+
using Xunit;
6+
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 Container();
15+
var style = new Style();
16+
17+
Assert.Throws<InvalidOperationException>(() => style.Children.Add(target));
18+
}
19+
20+
[Fact]
21+
public void Container_Width_Queries_Matches()
22+
{
23+
var root = new LayoutTestRoot()
24+
{
25+
ClientSize = new Size(400, 400)
26+
};
27+
var containerQuery1 = new Container(x => new WidthQuery(x, QueryComparisonOperator.LessThanOrEquals, 500));
28+
containerQuery1.Children.Add(new Style(x => x.Is<Border>())
29+
{
30+
Setters = { new Setter(Control.WidthProperty, 200.0) }
31+
});
32+
var containerQuery2 = new Container(x => new WidthQuery(x, QueryComparisonOperator.GreaterThan, 500));
33+
containerQuery2.Children.Add(new Style(x => x.Is<Border>())
34+
{
35+
Setters = { new Setter(Control.WidthProperty, 500.0) }
36+
});
37+
root.Styles.Add(containerQuery1);
38+
root.Styles.Add(containerQuery2);
39+
var child = new Border()
40+
{
41+
Name = "Child",
42+
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch
43+
};
44+
var border = new Border()
45+
{
46+
ContainerType = Avalonia.Layout.ContainerType.Width,
47+
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch,
48+
VerticalAlignment = Avalonia.Layout.VerticalAlignment.Stretch,
49+
Child = child,
50+
Name = "Parent"
51+
};
52+
53+
root.Child = border;
54+
55+
root.LayoutManager.ExecuteInitialLayoutPass();
56+
Assert.Equal(child.Width, 200.0);
57+
58+
root.ClientSize = new Size(600, 600);
59+
root.InvalidateMeasure();
60+
61+
root.LayoutManager.ExecuteLayoutPass();
62+
Assert.Equal(child.Width, 500.0);
63+
}
64+
65+
[Fact]
66+
public void Container_Height_Queries_Matches()
67+
{
68+
var root = new LayoutTestRoot()
69+
{
70+
ClientSize = new Size(400, 400)
71+
};
72+
var containerQuery1 = new Container(x => new HeightQuery(x, QueryComparisonOperator.LessThanOrEquals, 500));
73+
containerQuery1.Children.Add(new Style(x => x.Is<Border>())
74+
{
75+
Setters = { new Setter(Control.HeightProperty, 200.0) }
76+
});
77+
var containerQuery2 = new Container(x => new HeightQuery(x, QueryComparisonOperator.GreaterThan, 500));
78+
containerQuery2.Children.Add(new Style(x => x.Is<Border>())
79+
{
80+
Setters = { new Setter(Control.HeightProperty, 500.0) }
81+
});
82+
root.Styles.Add(containerQuery1);
83+
root.Styles.Add(containerQuery2);
84+
var child = new Border()
85+
{
86+
Name = "Child",
87+
VerticalAlignment = Avalonia.Layout.VerticalAlignment.Stretch
88+
};
89+
var border = new Border()
90+
{
91+
ContainerType = Avalonia.Layout.ContainerType.Height,
92+
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch,
93+
VerticalAlignment = Avalonia.Layout.VerticalAlignment.Stretch,
94+
Child = child,
95+
Name = "Parent"
96+
};
97+
98+
root.Child = border;
99+
100+
root.LayoutManager.ExecuteInitialLayoutPass();
101+
Assert.Equal(child.Height, 200.0);
102+
103+
root.ClientSize = new Size(600, 600);
104+
root.InvalidateMeasure();
105+
106+
root.LayoutManager.ExecuteLayoutPass();
107+
Assert.Equal(child.Height, 500.0);
108+
}
109+
110+
[Fact]
111+
public void Container_Queries_Matches_Name()
112+
{
113+
var root = new LayoutTestRoot()
114+
{
115+
ClientSize = new Size(600, 600)
116+
};
117+
var containerQuery1 = new Container(x => new WidthQuery(x, QueryComparisonOperator.LessThanOrEquals, 500));
118+
containerQuery1.Children.Add(new Style(x => x.Is<Border>())
119+
{
120+
Setters = { new Setter(Control.WidthProperty, 200.0) }
121+
});
122+
var containerQuery2 = new Container(x => new WidthQuery(x, QueryComparisonOperator.LessThanOrEquals, 500), "TEST");
123+
containerQuery2.Children.Add(new Style(x => x.Is<Border>())
124+
{
125+
Setters = { new Setter(Control.WidthProperty, 300.0) }
126+
});
127+
root.Styles.Add(containerQuery2);
128+
root.Styles.Add(containerQuery1);
129+
var child = new Border()
130+
{
131+
Name = "Child",
132+
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch
133+
};
134+
var controlInner = new ContentControl()
135+
{
136+
ContainerType = Avalonia.Layout.ContainerType.Width,
137+
ContainerName = "TEST",
138+
Width = 400,
139+
Height = 400,
140+
Content = child,
141+
Name = "Inner"
142+
143+
};
144+
var border = new Border()
145+
{
146+
ContainerType = Avalonia.Layout.ContainerType.Width,
147+
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Stretch,
148+
VerticalAlignment = Avalonia.Layout.VerticalAlignment.Stretch,
149+
Child = controlInner,
150+
Name = "Parent"
151+
};
152+
153+
root.Child = border;
154+
155+
root.LayoutManager.ExecuteInitialLayoutPass();
156+
157+
root.LayoutManager.ExecuteLayoutPass();
158+
Assert.Equal(child.Width, 300.0);
159+
}
160+
}
161+
}

0 commit comments

Comments
 (0)