|
| 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 | +} |
0 commit comments