Skip to content

Commit ccb65dc

Browse files
Add initial basic tests for infinite dimension with ConstrainedBox
1 parent 857695f commit ccb65dc

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.Coerce.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
namespace UnitTests.UWP.UI.Controls
1818
{
19+
/// <summary>
20+
/// These tests check for the various values which can be coerced and changed if out of bounds for each property.
21+
/// </summary>
1922
public partial class Test_ConstrainedBox : VisualUITestBase
2023
{
2124
[TestCategory("ConstrainedBox")]

UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.Combined.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
namespace UnitTests.UWP.UI.Controls
1919
{
20+
/// <summary>
21+
/// These tests check multiple constraints are applied together in the correct order.
22+
/// </summary>
2023
public partial class Test_ConstrainedBox : VisualUITestBase
2124
{
2225
[TestCategory("ConstrainedBox")]
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 one of the bounds provided by the parent panel is infinite.
21+
/// </summary>
22+
public partial class Test_ConstrainedBox : VisualUITestBase
23+
{
24+
[TestCategory("ConstrainedBox")]
25+
[TestMethod]
26+
public async Task Test_ConstrainedBox_AllInfinite_AspectWidthFallback()
27+
{
28+
await App.DispatcherQueue.EnqueueAsync(async () =>
29+
{
30+
// Even though we constrain the size of the ScrollViewer, it initially reports infinite measure
31+
// which is what we want to test. We constrain the dimension so that we have a specific value
32+
// that we can measure against. If we instead restrained the inner Border, it'd just set
33+
// it's side within the constrained space of the parent layout...
34+
var treeRoot = XamlReader.Load(@"<Page
35+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
36+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
37+
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
38+
<ScrollViewer HorizontalScrollMode=""Enabled"" HorizontalScrollBarVisibility=""Visible""
39+
VerticalScrollMode=""Enabled"" VerticalScrollBarVisibility=""Visible""
40+
Width=""200"">
41+
<controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""2:1"">
42+
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
43+
</controls:ConstrainedBox>
44+
</ScrollViewer>
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_AllInfinite_AspectHeightFallback()
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+
<ScrollViewer HorizontalScrollMode=""Enabled"" HorizontalScrollBarVisibility=""Visible""
80+
VerticalScrollMode=""Enabled"" VerticalScrollBarVisibility=""Visible""
81+
Height=""200"">
82+
<controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""1:2"">
83+
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
84+
</controls:ConstrainedBox>
85+
</ScrollViewer>
86+
</Page>") as FrameworkElement;
87+
88+
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
89+
90+
// Initialize Visual Tree
91+
await SetTestContentAsync(treeRoot);
92+
93+
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;
94+
95+
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");
96+
97+
// Force Layout calculations
98+
panel.UpdateLayout();
99+
100+
var child = panel.Content as Border;
101+
102+
Assert.IsNotNull(child, "Could not find inner Border");
103+
104+
// Check Size
105+
Assert.AreEqual(100, child.ActualWidth, 0.01, "Actual width does not meet expected value of 100");
106+
Assert.AreEqual(200, child.ActualHeight, 0.01, "Actual height does not meet expected value of 200");
107+
});
108+
}
109+
}
110+
}

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.Infinity.cs" />
232233
<Compile Include="UI\Controls\Test_ConstrainedBox.AspectRatio.cs" />
233234
<Compile Include="UI\Controls\Test_ConstrainedBox.Coerce.cs" />
234235
<Compile Include="UI\Controls\Test_ConstrainedBox.Multiple.cs" />

0 commit comments

Comments
 (0)