Skip to content

Commit 15c5700

Browse files
Add last set of tests for ConstrainedBox for Alignment positioning within a parent for completeness.
1 parent ec72f70 commit 15c5700

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 whether the inner alignment of the box within it's parent works as expected.
21+
/// </summary>
22+
public partial class Test_ConstrainedBox : VisualUITestBase
23+
{
24+
// For this test we're testing within the confines of a 200x200 box to position a contrained
25+
// 50x100 element in all the different alignment combinations.
26+
[TestCategory("ConstrainedBox")]
27+
[TestMethod]
28+
[DataRow("Left", 0, "Center", 50, DisplayName = "LeftCenter")]
29+
[DataRow("Left", 0, "Top", 0, DisplayName = "LeftTop")]
30+
[DataRow("Center", 75, "Top", 0, DisplayName = "CenterTop")]
31+
[DataRow("Right", 150, "Top", 0, DisplayName = "RightTop")]
32+
[DataRow("Right", 150, "Center", 50, DisplayName = "RightCenter")]
33+
[DataRow("Right", 150, "Bottom", 100, DisplayName = "RightBottom")]
34+
[DataRow("Center", 75, "Bottom", 100, DisplayName = "CenterBottom")]
35+
[DataRow("Left", 0, "Bottom", 100, DisplayName = "LeftBottom")]
36+
[DataRow("Center", 75, "Center", 50, DisplayName = "CenterCenter")]
37+
public async Task Test_ConstrainedBox_Alignment_Aspect(string horizontalAlignment, int expectedLeft, string verticalAlignment, int expectedTop)
38+
{
39+
await App.DispatcherQueue.EnqueueAsync(async () =>
40+
{
41+
var treeRoot = XamlReader.Load(@$"<Page
42+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
43+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
44+
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
45+
<Grid x:Name=""ParentGrid""
46+
Width=""200"" Height=""200"">
47+
<controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""1:2"" MaxHeight=""100""
48+
HorizontalAlignment=""{horizontalAlignment}""
49+
VerticalAlignment=""{verticalAlignment}"">
50+
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
51+
</controls:ConstrainedBox>
52+
</Grid>
53+
</Page>") as FrameworkElement;
54+
55+
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
56+
57+
// Initialize Visual Tree
58+
await SetTestContentAsync(treeRoot);
59+
60+
var grid = treeRoot.FindChild("ParentGrid") as Grid;
61+
62+
Assert.IsNotNull(grid, "Could not find the ParentGrid in tree.");
63+
64+
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;
65+
66+
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");
67+
68+
// Force Layout calculations
69+
panel.UpdateLayout();
70+
71+
var child = panel.Content as Border;
72+
73+
Assert.IsNotNull(child, "Could not find inner Border");
74+
75+
// Check Size
76+
Assert.AreEqual(50, child.ActualWidth, 0.01, "Actual width does not meet expected value of 50");
77+
Assert.AreEqual(100, child.ActualHeight, 0.01, "Actual height does not meet expected value of 100");
78+
79+
// Check inner Positioning, we do this from the Grid as the ConstainedBox also modifies its own size
80+
// and is hugging the child.
81+
var position = grid.CoordinatesTo(child);
82+
83+
Assert.AreEqual(expectedLeft, position.X, 0.01, "X position does not meet expected value of 0");
84+
Assert.AreEqual(expectedTop, position.Y, 0.01, "Y position does not meet expected value of 50");
85+
});
86+
}
87+
}
88+
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ public async Task Test_ConstrainedBox_Constrain_AspectMaxHeight()
2929
{
3030
await App.DispatcherQueue.EnqueueAsync(async () =>
3131
{
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...
3632
var treeRoot = XamlReader.Load(@"<Page
3733
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
3834
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""

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

0 commit comments

Comments
 (0)