Skip to content

Commit 6057802

Browse files
Add initial primitive test for each property in isolation
1 parent b4fe600 commit 6057802

File tree

5 files changed

+245
-14
lines changed

5 files changed

+245
-14
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Primitives/ConstrainedBox.bind

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
AspectRatio="1:1"
1616
MinWidth="64" MaxWidth="512">
1717
<controls:ConstrainedBox.Background>
18-
<!-- TODO: Should I make a DPI aware image? As otherwise scaling doesn't quite work at 150%? -->
18+
<!-- TODO: TilesBrush doesn't support Dpi image loading for this scenario
19+
This example is configured for 100% DPI at the moment.
20+
See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4150
21+
-->
1922
<brushes:TilesBrush TextureUri="ms-appx:///Assets/checker.png"/>
2023
</controls:ConstrainedBox.Background>
2124
</controls:ConstrainedBox>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
[TestClass]
20+
public partial class Test_ConstrainedBox : VisualUITestBase
21+
{
22+
[TestCategory("ConstrainedBox")]
23+
[TestMethod]
24+
public async Task Test_ConstrainedBox_Normal_AspectHorizontal()
25+
{
26+
await App.DispatcherQueue.EnqueueAsync(async () =>
27+
{
28+
var treeRoot = XamlReader.Load(@"<Page
29+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
30+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
31+
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
32+
<controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""2:1"" Width=""200"">
33+
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
34+
</controls:ConstrainedBox>
35+
</Page>") as FrameworkElement;
36+
37+
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
38+
39+
// Initialize Visual Tree
40+
await SetTestContentAsync(treeRoot);
41+
42+
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;
43+
44+
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");
45+
46+
// Force Layout calculations
47+
panel.UpdateLayout();
48+
49+
var child = panel.Content as Border;
50+
51+
Assert.IsNotNull(child, "Could not find inner Border");
52+
53+
// Check Size
54+
Assert.AreEqual(child.ActualWidth, 200, "Width unexpected");
55+
Assert.AreEqual(child.ActualHeight, 100, "Height unexpected");
56+
});
57+
}
58+
59+
[TestCategory("ConstrainedBox")]
60+
[TestMethod]
61+
public async Task Test_ConstrainedBox_Normal_AspectVertical()
62+
{
63+
await App.DispatcherQueue.EnqueueAsync(async () =>
64+
{
65+
var treeRoot = XamlReader.Load(@"<Page
66+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
67+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
68+
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
69+
<controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""1:2"" Height=""200"">
70+
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
71+
</controls:ConstrainedBox>
72+
</Page>") as FrameworkElement;
73+
74+
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
75+
76+
// Initialize Visual Tree
77+
await SetTestContentAsync(treeRoot);
78+
79+
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;
80+
81+
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");
82+
83+
// Force Layout calculations
84+
panel.UpdateLayout();
85+
86+
var child = panel.Content as Border;
87+
88+
Assert.IsNotNull(child, "Could not find inner Border");
89+
90+
// Check Size
91+
Assert.AreEqual(child.ActualWidth, 100, "Width unexpected");
92+
Assert.AreEqual(child.ActualHeight, 200, "Height unexpected");
93+
});
94+
}
95+
}
96+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
public partial class Test_ConstrainedBox : VisualUITestBase
20+
{
21+
[TestCategory("ConstrainedBox")]
22+
[TestMethod]
23+
public async Task Test_ConstrainedBox_Normal_MultipleX()
24+
{
25+
await App.DispatcherQueue.EnqueueAsync(async () =>
26+
{
27+
var treeRoot = XamlReader.Load(@"<Page
28+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
29+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
30+
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
31+
<Grid x:Name=""ParentGrid"" Width=""200"" Height=""200"">
32+
<controls:ConstrainedBox x:Name=""ConstrainedBox"" MultipleX=""32""
33+
HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"">
34+
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
35+
</controls:ConstrainedBox>
36+
</Grid>
37+
</Page>") as FrameworkElement;
38+
39+
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
40+
41+
// Initialize Visual Tree
42+
await SetTestContentAsync(treeRoot);
43+
44+
var grid = treeRoot.FindChild("ParentGrid") as Grid;
45+
46+
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;
47+
48+
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");
49+
50+
// Force Layout calculations
51+
panel.UpdateLayout();
52+
53+
var child = panel.Content as Border;
54+
55+
Assert.IsNotNull(child, "Could not find inner Border");
56+
57+
// Check Size
58+
Assert.AreEqual(child.ActualWidth, 192);
59+
Assert.AreEqual(child.ActualHeight, 200);
60+
61+
// Check inner Positioning, we do this from the Grid as the ConstainedBox also modifies its own size
62+
// and is hugging the child.
63+
var position = grid.CoordinatesTo(child);
64+
65+
Assert.AreEqual(position.X, 4);
66+
Assert.AreEqual(position.Y, 0);
67+
});
68+
}
69+
70+
[TestCategory("ConstrainedBox")]
71+
[TestMethod]
72+
public async Task Test_ConstrainedBox_Normal_MultipleY()
73+
{
74+
await App.DispatcherQueue.EnqueueAsync(async () =>
75+
{
76+
var treeRoot = XamlReader.Load(@"<Page
77+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
78+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
79+
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
80+
<Grid x:Name=""ParentGrid"" Width=""200"" Height=""200"">
81+
<controls:ConstrainedBox x:Name=""ConstrainedBox"" MultipleY=""32""
82+
HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"">
83+
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
84+
</controls:ConstrainedBox>
85+
</Grid>
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 grid = treeRoot.FindChild("ParentGrid") as Grid;
94+
95+
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;
96+
97+
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");
98+
99+
// Force Layout calculations
100+
panel.UpdateLayout();
101+
102+
var child = panel.Content as Border;
103+
104+
Assert.IsNotNull(child, "Could not find inner Border");
105+
106+
// Check Size
107+
Assert.AreEqual(child.ActualWidth, 200);
108+
Assert.AreEqual(child.ActualHeight, 192);
109+
110+
// Check inner Positioning, we do this from the Grid as the ConstainedBox also modifies its own size
111+
// and is hugging the child.
112+
var position = grid.CoordinatesTo(child);
113+
114+
Assert.AreEqual(position.X, 0);
115+
Assert.AreEqual(position.Y, 4);
116+
});
117+
}
118+
}
119+
}

UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.cs renamed to UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.Scale.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,33 @@
1616

1717
namespace UnitTests.UWP.UI.Controls
1818
{
19-
[TestClass]
20-
public class Test_ConstrainedBox : VisualUITestBase
19+
public partial class Test_ConstrainedBox : VisualUITestBase
2120
{
2221
[TestCategory("ConstrainedBox")]
2322
[TestMethod]
24-
public async Task Test_ConstrainedBox_Normal_Horizontal()
23+
public async Task Test_ConstrainedBox_Normal_ScaleX()
2524
{
2625
await App.DispatcherQueue.EnqueueAsync(async () =>
2726
{
2827
var treeRoot = XamlReader.Load(@"<Page
2928
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
3029
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
3130
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
32-
<controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""2:1"" Width=""200"">
31+
<Grid x:Name=""ParentGrid"" Width=""200"" Height=""200"">
32+
<controls:ConstrainedBox x:Name=""ConstrainedBox"" ScaleX=""0.5""
33+
HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"">
3334
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
3435
</controls:ConstrainedBox>
36+
</Grid>
3537
</Page>") as FrameworkElement;
3638

3739
Assert.IsNotNull(treeRoot, "Could not load XAML tree.");
3840

3941
// Initialize Visual Tree
4042
await SetTestContentAsync(treeRoot);
4143

44+
var grid = treeRoot.FindChild("ParentGrid") as Grid;
45+
4246
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox;
4347

4448
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree.");
@@ -51,14 +55,21 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
5155
Assert.IsNotNull(child, "Could not find inner Border");
5256

5357
// Check Size
54-
Assert.AreEqual(child.ActualWidth, 200, "Width unexpected");
55-
Assert.AreEqual(child.ActualHeight, 100, "Height unexpected");
58+
Assert.AreEqual(child.ActualWidth, 100);
59+
Assert.AreEqual(child.ActualHeight, 200);
60+
61+
// Check inner Positioning, we do this from the Grid as the ConstainedBox also modifies its own size
62+
// and is hugging the child.
63+
var position = grid.CoordinatesTo(child);
64+
65+
Assert.AreEqual(position.X, 50);
66+
Assert.AreEqual(position.Y, 0);
5667
});
5768
}
5869

5970
[TestCategory("ConstrainedBox")]
6071
[TestMethod]
61-
public async Task Test_ConstrainedBox_Normal_ScaleX()
72+
public async Task Test_ConstrainedBox_Normal_ScaleY()
6273
{
6374
await App.DispatcherQueue.EnqueueAsync(async () =>
6475
{
@@ -67,7 +78,7 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
6778
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
6879
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls"">
6980
<Grid x:Name=""ParentGrid"" Width=""200"" Height=""200"">
70-
<controls:ConstrainedBox x:Name=""ConstrainedBox"" ScaleX=""0.5""
81+
<controls:ConstrainedBox x:Name=""ConstrainedBox"" ScaleY=""0.5""
7182
HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"">
7283
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/>
7384
</controls:ConstrainedBox>
@@ -93,15 +104,15 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
93104
Assert.IsNotNull(child, "Could not find inner Border");
94105

95106
// Check Size
96-
Assert.AreEqual(child.ActualWidth, 100);
97-
Assert.AreEqual(child.ActualHeight, 200);
107+
Assert.AreEqual(child.ActualWidth, 200);
108+
Assert.AreEqual(child.ActualHeight, 100);
98109

99110
// Check inner Positioning, we do this from the Grid as the ConstainedBox also modifies its own size
100111
// and is hugging the child.
101112
var position = grid.CoordinatesTo(child);
102113

103-
Assert.AreEqual(position.X, 50);
104-
Assert.AreEqual(position.Y, 0);
114+
Assert.AreEqual(position.X, 0);
115+
Assert.AreEqual(position.Y, 50);
105116
});
106117
}
107118
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@
219219
<Compile Include="UI\Collection\Test_IncrementalLoadingCollection.cs" />
220220
<Compile Include="UI\Controls\Test_Carousel.cs" />
221221
<Compile Include="UI\Controls\Test_BladeView.cs" />
222+
<Compile Include="UI\Controls\Test_ConstrainedBox.AspectRatio.cs" />
223+
<Compile Include="UI\Controls\Test_ConstrainedBox.Multiple.cs" />
222224
<Compile Include="UI\Controls\Test_ImageEx.cs" />
223225
<Compile Include="UI\Controls\Test_RadialGauge.cs" />
224226
<Compile Include="UI\Controls\Test_TextToolbar_Localization.cs" />
@@ -231,7 +233,7 @@
231233
<Compile Include="UI\Controls\Test_UniformGrid_FreeSpots.cs" />
232234
<Compile Include="UI\Controls\Test_UniformGrid_Dimensions.cs" />
233235
<Compile Include="UI\Controls\Test_RangeSelector.cs" />
234-
<Compile Include="UI\Controls\Test_ConstrainedBox.cs" />
236+
<Compile Include="UI\Controls\Test_ConstrainedBox.Scale.cs" />
235237
<Compile Include="UI\Controls\Test_WrapPanel_Visibility.cs" />
236238
<Compile Include="UI\Controls\Test_WrapPanel_BasicLayout.cs" />
237239
<Compile Include="UI\Extensions\Test_VisualExtensions.cs" />

0 commit comments

Comments
 (0)