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