-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add Value Coercion to ConstrainedBox #4169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michael-hawker
merged 7 commits into
CommunityToolkit:main
from
michael-hawker:mhawker/constrained-box-fixes
Aug 16, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2c7e8c2
Fixes #4166 - Missing Callback for Multiple Properties in ConstrainedBox
michael-hawker 857695f
Add Value Coercion and Tests for ConstrainedBox
michael-hawker ccb65dc
Add initial basic tests for infinite dimension with ConstrainedBox
michael-hawker 7698e7e
Add test for constricting the max size of a ConstrainedBox
michael-hawker ec72f70
Add Infinite in Both directions for ConstrainedBox
michael-hawker 15c5700
Add last set of tests for ConstrainedBox for Alignment positioning wi…
michael-hawker 85a457a
Turn off Layout Rounding in UnitTests/UnitTests.UWP/UI/Controls/Test_…
michael-hawker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.Alignment.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Toolkit.Uwp; | ||
using Microsoft.Toolkit.Uwp.UI; | ||
using Microsoft.Toolkit.Uwp.UI.Controls; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer; | ||
using Windows.Foundation; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Controls; | ||
using Windows.UI.Xaml.Markup; | ||
|
||
namespace UnitTests.UWP.UI.Controls | ||
{ | ||
/// <summary> | ||
/// These tests check whether the inner alignment of the box within it's parent works as expected. | ||
/// </summary> | ||
public partial class Test_ConstrainedBox : VisualUITestBase | ||
{ | ||
// For this test we're testing within the confines of a 200x200 box to position a contrained | ||
// 50x100 element in all the different alignment combinations. | ||
[TestCategory("ConstrainedBox")] | ||
[TestMethod] | ||
[DataRow("Left", 0, "Center", 50, DisplayName = "LeftCenter")] | ||
[DataRow("Left", 0, "Top", 0, DisplayName = "LeftTop")] | ||
[DataRow("Center", 75, "Top", 0, DisplayName = "CenterTop")] | ||
michael-hawker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[DataRow("Right", 150, "Top", 0, DisplayName = "RightTop")] | ||
[DataRow("Right", 150, "Center", 50, DisplayName = "RightCenter")] | ||
[DataRow("Right", 150, "Bottom", 100, DisplayName = "RightBottom")] | ||
[DataRow("Center", 75, "Bottom", 100, DisplayName = "CenterBottom")] | ||
[DataRow("Left", 0, "Bottom", 100, DisplayName = "LeftBottom")] | ||
[DataRow("Center", 75, "Center", 50, DisplayName = "CenterCenter")] | ||
public async Task Test_ConstrainedBox_Alignment_Aspect(string horizontalAlignment, int expectedLeft, string verticalAlignment, int expectedTop) | ||
{ | ||
await App.DispatcherQueue.EnqueueAsync(async () => | ||
{ | ||
var treeRoot = XamlReader.Load(@$"<Page | ||
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" | ||
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" | ||
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls""> | ||
<Grid x:Name=""ParentGrid"" | ||
Width=""200"" Height=""200""> | ||
<controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""1:2"" MaxHeight=""100"" | ||
UseLayoutRounding=""False"" | ||
HorizontalAlignment=""{horizontalAlignment}"" | ||
VerticalAlignment=""{verticalAlignment}""> | ||
<Border HorizontalAlignment=""Stretch"" VerticalAlignment=""Stretch"" Background=""Red""/> | ||
</controls:ConstrainedBox> | ||
</Grid> | ||
</Page>") as FrameworkElement; | ||
|
||
Assert.IsNotNull(treeRoot, "Could not load XAML tree."); | ||
|
||
// Initialize Visual Tree | ||
await SetTestContentAsync(treeRoot); | ||
|
||
var grid = treeRoot.FindChild("ParentGrid") as Grid; | ||
|
||
Assert.IsNotNull(grid, "Could not find the ParentGrid in tree."); | ||
|
||
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox; | ||
|
||
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree."); | ||
|
||
// Force Layout calculations | ||
panel.UpdateLayout(); | ||
|
||
var child = panel.Content as Border; | ||
|
||
Assert.IsNotNull(child, "Could not find inner Border"); | ||
|
||
// Check Size | ||
Assert.AreEqual(50, child.ActualWidth, 0.01, "Actual width does not meet expected value of 50"); | ||
Assert.AreEqual(100, child.ActualHeight, 0.01, "Actual height does not meet expected value of 100"); | ||
|
||
// Check inner Positioning, we do this from the Grid as the ConstainedBox also modifies its own size | ||
// and is hugging the child. | ||
var position = grid.CoordinatesTo(child); | ||
|
||
Assert.AreEqual(expectedLeft, position.X, 0.01, "X position does not meet expected value of 0"); | ||
Assert.AreEqual(expectedTop, position.Y, 0.01, "Y position does not meet expected value of 50"); | ||
}); | ||
} | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.Coerce.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Toolkit.Uwp; | ||
using Microsoft.Toolkit.Uwp.UI; | ||
using Microsoft.Toolkit.Uwp.UI.Controls; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer; | ||
using Windows.Foundation; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Controls; | ||
using Windows.UI.Xaml.Markup; | ||
|
||
namespace UnitTests.UWP.UI.Controls | ||
{ | ||
/// <summary> | ||
/// These tests check for the various values which can be coerced and changed if out of bounds for each property. | ||
/// </summary> | ||
public partial class Test_ConstrainedBox : VisualUITestBase | ||
{ | ||
[TestCategory("ConstrainedBox")] | ||
[TestMethod] | ||
public async Task Test_ConstrainedBox_Coerce_Scale() | ||
{ | ||
await App.DispatcherQueue.EnqueueAsync(async () => | ||
{ | ||
var treeRoot = XamlReader.Load(@"<Page | ||
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" | ||
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" | ||
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls""> | ||
<controls:ConstrainedBox x:Name=""ConstrainedBox"" ScaleX=""0.5"" ScaleY=""0.5""/> | ||
</Page>") as FrameworkElement; | ||
|
||
Assert.IsNotNull(treeRoot, "Could not load XAML tree."); | ||
|
||
// Initialize Visual Tree | ||
await SetTestContentAsync(treeRoot); | ||
|
||
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox; | ||
|
||
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree."); | ||
|
||
// Check Size | ||
Assert.AreEqual(0.5, panel.ScaleX, 0.01, "ScaleX does not meet expected initial value of 0.5"); | ||
Assert.AreEqual(0.5, panel.ScaleY, 0.01, "ScaleY does not meet expected initial value of 0.5"); | ||
|
||
// Change values now to be invalid | ||
panel.ScaleX = double.NaN; | ||
panel.ScaleY = double.NaN; | ||
|
||
Assert.AreEqual(1.0, panel.ScaleX, 0.01, "ScaleX does not meet expected value of 1.0 after change."); | ||
Assert.AreEqual(1.0, panel.ScaleY, 0.01, "ScaleY does not meet expected value of 1.0 after change."); | ||
|
||
// Change values now to be invalid | ||
panel.ScaleX = double.NegativeInfinity; | ||
panel.ScaleY = double.NegativeInfinity; | ||
|
||
Assert.AreEqual(0.0, panel.ScaleX, 0.01, "ScaleX does not meet expected value of 0.0 after change."); | ||
Assert.AreEqual(0.0, panel.ScaleY, 0.01, "ScaleY does not meet expected value of 0.0 after change."); | ||
|
||
// Change values now to be invalid | ||
panel.ScaleX = double.PositiveInfinity; | ||
panel.ScaleY = double.PositiveInfinity; | ||
|
||
Assert.AreEqual(1.0, panel.ScaleX, 0.01, "ScaleX does not meet expected value of 1.0 after change."); | ||
Assert.AreEqual(1.0, panel.ScaleY, 0.01, "ScaleY does not meet expected value of 1.0 after change."); | ||
}); | ||
} | ||
|
||
[TestCategory("ConstrainedBox")] | ||
[TestMethod] | ||
public async Task Test_ConstrainedBox_Coerce_Multiple() | ||
{ | ||
await App.DispatcherQueue.EnqueueAsync(async () => | ||
{ | ||
var treeRoot = XamlReader.Load(@"<Page | ||
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" | ||
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" | ||
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls""> | ||
<controls:ConstrainedBox x:Name=""ConstrainedBox"" MultipleX=""32"" MultipleY=""32""/> | ||
</Page>") as FrameworkElement; | ||
|
||
Assert.IsNotNull(treeRoot, "Could not load XAML tree."); | ||
|
||
// Initialize Visual Tree | ||
await SetTestContentAsync(treeRoot); | ||
|
||
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox; | ||
|
||
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree."); | ||
|
||
// Check Size | ||
Assert.AreEqual(32, panel.MultipleX, "MultipleX does not meet expected value of 32"); | ||
Assert.AreEqual(32, panel.MultipleY, "MultipleY does not meet expected value of 32"); | ||
|
||
// Change values now to be invalid | ||
panel.MultipleX = 0; | ||
panel.MultipleY = int.MinValue; | ||
|
||
Assert.AreEqual(DependencyProperty.UnsetValue, panel.ReadLocalValue(ConstrainedBox.MultipleXProperty), "MultipleX does not meet expected value of UnsetValue after change."); | ||
Assert.AreEqual(DependencyProperty.UnsetValue, panel.ReadLocalValue(ConstrainedBox.MultipleYProperty), "MultipleY does not meet expected value of UnsetValue after change."); | ||
}); | ||
} | ||
|
||
[TestCategory("ConstrainedBox")] | ||
[TestMethod] | ||
public async Task Test_ConstrainedBox_Coerce_AspectRatio() | ||
{ | ||
await App.DispatcherQueue.EnqueueAsync(async () => | ||
{ | ||
var treeRoot = XamlReader.Load(@"<Page | ||
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" | ||
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" | ||
xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls""> | ||
<controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""1.25:3.5""/> | ||
</Page>") as FrameworkElement; | ||
|
||
Assert.IsNotNull(treeRoot, "Could not load XAML tree."); | ||
|
||
// Initialize Visual Tree | ||
await SetTestContentAsync(treeRoot); | ||
|
||
var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox; | ||
|
||
Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree."); | ||
|
||
// Check Size | ||
Assert.AreEqual(1.25 / 3.5, panel.AspectRatio, 0.01, "ApectRatio does not meet expected value of 1.25/3.5"); | ||
|
||
// Change values now to be invalid | ||
panel.AspectRatio = -1; | ||
|
||
Assert.AreEqual(DependencyProperty.UnsetValue, panel.ReadLocalValue(ConstrainedBox.AspectRatioProperty), "AspectRatio does not meet expected value of UnsetValue after change."); | ||
}); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.