Skip to content

Commit 7698e7e

Browse files
Add test for constricting the max size of a ConstrainedBox
1 parent ccb65dc commit 7698e7e

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using Microsoft.Toolkit.Uwp;
8+
using Microsoft.Toolkit.Uwp.UI;
9+
using Microsoft.Toolkit.Uwp.UI.Controls;
10+
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
12+
using Windows.Foundation;
13+
using Windows.UI.Xaml;
14+
using Windows.UI.Xaml.Controls;
15+
using Windows.UI.Xaml.Markup;
16+
17+
namespace UnitTests.UWP.UI.Controls
18+
{
19+
/// <summary>
20+
/// These tests check for cases where we want to constrain the size of the ConstrainedBox with other
21+
/// properties like MaxWidth and MaxHeight. The ConstrainedBox is greedy about using all the available
22+
/// space, so Min properties don't really play the same factor? At least hard to create a practical test.
23+
/// </summary>
24+
public partial class Test_ConstrainedBox : VisualUITestBase
25+
{
26+
[TestCategory("ConstrainedBox")]
27+
[TestMethod]
28+
public async Task Test_ConstrainedBox_Constrain_AspectMaxHeight()
29+
{
30+
await App.DispatcherQueue.EnqueueAsync(async () =>
31+
{
32+
// Even though we constrain the size of the ScrollViewer, it initially reports infinite measure
33+
// which is what we want to test. We constrain the dimension so that we have a specific value
34+
// that we can measure against. If we instead restrained the inner Border, it'd just set
35+
// it's side within the constrained space of the parent layout...
36+
var treeRoot = XamlReader.Load(@"<Page
37+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
38+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
39+
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
40+
<Grid HorizontalAlignment=""Center"" VerticalAlignment=""Center"">
41+
<controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""2:1"" MaxHeight=""100"">
42+
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
43+
</controls:ConstrainedBox>
44+
</Grid>
45+
</Page>") as FrameworkElement;
46+
47+
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
48+
49+
// Initialize Visual Tree
50+
await SetTestContentAsync(treeRoot);
51+
52+
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;
53+
54+
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");
55+
56+
// Force Layout calculations
57+
panel.UpdateLayout();
58+
59+
var child = panel.Content as Border;
60+
61+
Assert.IsNotNull(child, "Could not find inner Border");
62+
63+
// Check Size
64+
Assert.AreEqual(200, child.ActualWidth, 0.01, "Actual width does not meet expected value of 200");
65+
Assert.AreEqual(100, child.ActualHeight, 0.01, "Actual height does not meet expected value of 100");
66+
});
67+
}
68+
69+
[TestCategory("ConstrainedBox")]
70+
[TestMethod]
71+
public async Task Test_ConstrainedBox_Constrain_AspectMaxWidth()
72+
{
73+
await App.DispatcherQueue.EnqueueAsync(async () =>
74+
{
75+
var treeRoot = XamlReader.Load(@"<Page
76+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
77+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
78+
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
79+
<Grid HorizontalAlignment=""Center"" VerticalAlignment=""Center"">
80+
<controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""2:1"" MaxWidth=""100"">
81+
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
82+
</controls:ConstrainedBox>
83+
</Grid>
84+
</Page>") as FrameworkElement;
85+
86+
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
87+
88+
// Initialize Visual Tree
89+
await SetTestContentAsync(treeRoot);
90+
91+
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;
92+
93+
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");
94+
95+
// Force Layout calculations
96+
panel.UpdateLayout();
97+
98+
var child = panel.Content as Border;
99+
100+
Assert.IsNotNull(child, "Could not find inner Border");
101+
102+
// Check Size
103+
Assert.AreEqual(100, child.ActualWidth, 0.01, "Actual width does not meet expected value of 100");
104+
Assert.AreEqual(50, child.ActualHeight, 0.01, "Actual height does not meet expected value of 50");
105+
});
106+
}
107+
108+
[TestCategory("ConstrainedBox")]
109+
[TestMethod]
110+
public async Task Test_ConstrainedBox_Constrain_AspectBothMaxWidthHeight()
111+
{
112+
await App.DispatcherQueue.EnqueueAsync(async () =>
113+
{
114+
var treeRoot = XamlReader.Load(@"<Page
115+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
116+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
117+
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
118+
<Grid HorizontalAlignment=""Center"" VerticalAlignment=""Center"">
119+
<controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""1:2"" MaxWidth=""200"" MaxHeight=""200"">
120+
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
121+
</controls:ConstrainedBox>
122+
</Grid>
123+
</Page>") as FrameworkElement;
124+
125+
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
126+
127+
// Initialize Visual Tree
128+
await SetTestContentAsync(treeRoot);
129+
130+
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;
131+
132+
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");
133+
134+
// Force Layout calculations
135+
panel.UpdateLayout();
136+
137+
var child = panel.Content as Border;
138+
139+
Assert.IsNotNull(child, "Could not find inner Border");
140+
141+
// Check Size
142+
Assert.AreEqual(100, child.ActualWidth, 0.01, "Actual width does not meet expected value of 100");
143+
Assert.AreEqual(200, child.ActualHeight, 0.01, "Actual height does not meet expected value of 200");
144+
});
145+
}
146+
}
147+
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
<Compile Include="UI\Collection\Test_IncrementalLoadingCollection.cs" />
230230
<Compile Include="UI\Controls\Test_Carousel.cs" />
231231
<Compile Include="UI\Controls\Test_BladeView.cs" />
232+
<Compile Include="UI\Controls\Test_ConstrainedBox.Constrict.cs" />
232233
<Compile Include="UI\Controls\Test_ConstrainedBox.Infinity.cs" />
233234
<Compile Include="UI\Controls\Test_ConstrainedBox.AspectRatio.cs" />
234235
<Compile Include="UI\Controls\Test_ConstrainedBox.Coerce.cs" />

0 commit comments

Comments
 (0)