|  | 
|  | 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 the various values which can be coerced and changed if out of bounds for each property. | 
|  | 21 | +    /// </summary> | 
|  | 22 | +    public partial class Test_ConstrainedBox : VisualUITestBase | 
|  | 23 | +    { | 
|  | 24 | +        [TestCategory("ConstrainedBox")] | 
|  | 25 | +        [TestMethod] | 
|  | 26 | +        public async Task Test_ConstrainedBox_Coerce_Scale() | 
|  | 27 | +        { | 
|  | 28 | +            await App.DispatcherQueue.EnqueueAsync(async () => | 
|  | 29 | +            { | 
|  | 30 | +                var treeRoot = XamlReader.Load(@"<Page | 
|  | 31 | +    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" | 
|  | 32 | +    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" | 
|  | 33 | +    xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls""> | 
|  | 34 | +      <controls:ConstrainedBox x:Name=""ConstrainedBox"" ScaleX=""0.5"" ScaleY=""0.5""/> | 
|  | 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 | +                // Check Size | 
|  | 47 | +                Assert.AreEqual(0.5, panel.ScaleX, 0.01, "ScaleX does not meet expected initial value of 0.5"); | 
|  | 48 | +                Assert.AreEqual(0.5, panel.ScaleY, 0.01, "ScaleY does not meet expected initial value of 0.5"); | 
|  | 49 | + | 
|  | 50 | +                // Change values now to be invalid | 
|  | 51 | +                panel.ScaleX = double.NaN; | 
|  | 52 | +                panel.ScaleY = double.NaN; | 
|  | 53 | + | 
|  | 54 | +                Assert.AreEqual(1.0, panel.ScaleX, 0.01, "ScaleX does not meet expected value of 1.0 after change."); | 
|  | 55 | +                Assert.AreEqual(1.0, panel.ScaleY, 0.01, "ScaleY does not meet expected value of 1.0 after change."); | 
|  | 56 | + | 
|  | 57 | +                // Change values now to be invalid | 
|  | 58 | +                panel.ScaleX = double.NegativeInfinity; | 
|  | 59 | +                panel.ScaleY = double.NegativeInfinity; | 
|  | 60 | + | 
|  | 61 | +                Assert.AreEqual(0.0, panel.ScaleX, 0.01, "ScaleX does not meet expected value of 0.0 after change."); | 
|  | 62 | +                Assert.AreEqual(0.0, panel.ScaleY, 0.01, "ScaleY does not meet expected value of 0.0 after change."); | 
|  | 63 | + | 
|  | 64 | +                // Change values now to be invalid | 
|  | 65 | +                panel.ScaleX = double.PositiveInfinity; | 
|  | 66 | +                panel.ScaleY = double.PositiveInfinity; | 
|  | 67 | + | 
|  | 68 | +                Assert.AreEqual(1.0, panel.ScaleX, 0.01, "ScaleX does not meet expected value of 1.0 after change."); | 
|  | 69 | +                Assert.AreEqual(1.0, panel.ScaleY, 0.01, "ScaleY does not meet expected value of 1.0 after change."); | 
|  | 70 | +            }); | 
|  | 71 | +        } | 
|  | 72 | + | 
|  | 73 | +        [TestCategory("ConstrainedBox")] | 
|  | 74 | +        [TestMethod] | 
|  | 75 | +        public async Task Test_ConstrainedBox_Coerce_Multiple() | 
|  | 76 | +        { | 
|  | 77 | +            await App.DispatcherQueue.EnqueueAsync(async () => | 
|  | 78 | +            { | 
|  | 79 | +                var treeRoot = XamlReader.Load(@"<Page | 
|  | 80 | +    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" | 
|  | 81 | +    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" | 
|  | 82 | +    xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls""> | 
|  | 83 | +      <controls:ConstrainedBox x:Name=""ConstrainedBox"" MultipleX=""32"" MultipleY=""32""/> | 
|  | 84 | +</Page>") as FrameworkElement; | 
|  | 85 | + | 
|  | 86 | +                Assert.IsNotNull(treeRoot, "Could not load XAML tree."); | 
|  | 87 | + | 
|  | 88 | +                // Initialize Visual Tree | 
|  | 89 | +                await SetTestContentAsync(treeRoot); | 
|  | 90 | + | 
|  | 91 | +                var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox; | 
|  | 92 | + | 
|  | 93 | +                Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree."); | 
|  | 94 | + | 
|  | 95 | +                // Check Size | 
|  | 96 | +                Assert.AreEqual(32, panel.MultipleX, "MultipleX does not meet expected value of 32"); | 
|  | 97 | +                Assert.AreEqual(32, panel.MultipleY, "MultipleY does not meet expected value of 32"); | 
|  | 98 | + | 
|  | 99 | +                // Change values now to be invalid | 
|  | 100 | +                panel.MultipleX = 0; | 
|  | 101 | +                panel.MultipleY = int.MinValue; | 
|  | 102 | + | 
|  | 103 | +                Assert.AreEqual(DependencyProperty.UnsetValue, panel.ReadLocalValue(ConstrainedBox.MultipleXProperty), "MultipleX does not meet expected value of UnsetValue after change."); | 
|  | 104 | +                Assert.AreEqual(DependencyProperty.UnsetValue, panel.ReadLocalValue(ConstrainedBox.MultipleYProperty), "MultipleY does not meet expected value of UnsetValue after change."); | 
|  | 105 | +            }); | 
|  | 106 | +        } | 
|  | 107 | + | 
|  | 108 | +        [TestCategory("ConstrainedBox")] | 
|  | 109 | +        [TestMethod] | 
|  | 110 | +        public async Task Test_ConstrainedBox_Coerce_AspectRatio() | 
|  | 111 | +        { | 
|  | 112 | +            await App.DispatcherQueue.EnqueueAsync(async () => | 
|  | 113 | +            { | 
|  | 114 | +                var treeRoot = XamlReader.Load(@"<Page | 
|  | 115 | +    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" | 
|  | 116 | +    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" | 
|  | 117 | +    xmlns:controls=""using:Microsoft.Toolkit.Uwp.UI.Controls""> | 
|  | 118 | +      <controls:ConstrainedBox x:Name=""ConstrainedBox"" AspectRatio=""1.25:3.5""/> | 
|  | 119 | +</Page>") as FrameworkElement; | 
|  | 120 | + | 
|  | 121 | +                Assert.IsNotNull(treeRoot, "Could not load XAML tree."); | 
|  | 122 | + | 
|  | 123 | +                // Initialize Visual Tree | 
|  | 124 | +                await SetTestContentAsync(treeRoot); | 
|  | 125 | + | 
|  | 126 | +                var panel = treeRoot.FindChild("ConstrainedBox") as ConstrainedBox; | 
|  | 127 | + | 
|  | 128 | +                Assert.IsNotNull(panel, "Could not find ConstrainedBox in tree."); | 
|  | 129 | + | 
|  | 130 | +                // Check Size | 
|  | 131 | +                Assert.AreEqual(1.25 / 3.5, panel.AspectRatio, 0.01, "ApectRatio does not meet expected value of 1.25/3.5"); | 
|  | 132 | + | 
|  | 133 | +                // Change values now to be invalid | 
|  | 134 | +                panel.AspectRatio = -1; | 
|  | 135 | + | 
|  | 136 | +                Assert.AreEqual(DependencyProperty.UnsetValue, panel.ReadLocalValue(ConstrainedBox.AspectRatioProperty), "AspectRatio does not meet expected value of UnsetValue after change."); | 
|  | 137 | +            }); | 
|  | 138 | +        } | 
|  | 139 | +    } | 
|  | 140 | +} | 
0 commit comments