From 7642602ed6d0f89dc32cdc0fe6cab901244d5dc7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 16 Sep 2021 11:06:30 +0200 Subject: [PATCH] Fixed culture sensitivity when parsing AspectRatio components --- .../ConstrainedBox/AspectRatio.cs | 9 ++- .../ConstrainedBox.Properties.cs | 7 -- .../ConstrainedBox/ConstrainedBox.cs | 4 - .../Controls/Test_ConstrainedBox.Alignment.cs | 3 - .../Test_ConstrainedBox.AspectRatio.cs | 75 ++++++++++++++++++- 5 files changed, 78 insertions(+), 20 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/AspectRatio.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/AspectRatio.cs index 4475a247abf..becc62737a9 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/AspectRatio.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/AspectRatio.cs @@ -2,7 +2,7 @@ // 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; +using System.Globalization; namespace Microsoft.Toolkit.Uwp.UI.Controls { @@ -75,11 +75,14 @@ public static AspectRatio ConvertToAspectRatio(string rawString) if (ratio.Length == 2) { - return new AspectRatio(Convert.ToDouble(ratio[0]), Convert.ToDouble(ratio[1])); + double width = double.Parse(ratio[0], NumberStyles.Float, CultureInfo.InvariantCulture); + double height = double.Parse(ratio[1], NumberStyles.Float, CultureInfo.InvariantCulture); + + return new AspectRatio(width, height); } else if (ratio.Length == 1) { - return new AspectRatio(Convert.ToDouble(ratio[0])); + return new AspectRatio(double.Parse(ratio[0], NumberStyles.Float, CultureInfo.InvariantCulture)); } return new AspectRatio(1); diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/ConstrainedBox.Properties.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/ConstrainedBox.Properties.cs index dc1cb62dfab..47feb8e6b29 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/ConstrainedBox.Properties.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/ConstrainedBox.Properties.cs @@ -2,14 +2,7 @@ // 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; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Windows.Foundation; using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; namespace Microsoft.Toolkit.Uwp.UI.Controls { diff --git a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/ConstrainedBox.cs b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/ConstrainedBox.cs index c5c6dc9c48e..68ebd6e3430 100644 --- a/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/ConstrainedBox.cs +++ b/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/ConstrainedBox/ConstrainedBox.cs @@ -3,10 +3,6 @@ // See the LICENSE file in the project root for more information. using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using Windows.Foundation; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; diff --git a/UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.Alignment.cs b/UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.Alignment.cs index c5571a4c32e..644f265f073 100644 --- a/UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.Alignment.cs +++ b/UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.Alignment.cs @@ -2,14 +2,11 @@ // 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; diff --git a/UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.AspectRatio.cs b/UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.AspectRatio.cs index f5762c97eb0..fb90df2d123 100644 --- a/UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.AspectRatio.cs +++ b/UnitTests/UnitTests.UWP/UI/Controls/Test_ConstrainedBox.AspectRatio.cs @@ -2,14 +2,12 @@ // 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.Globalization; 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; @@ -92,5 +90,76 @@ await App.DispatcherQueue.EnqueueAsync(async () => Assert.AreEqual(200, child.ActualHeight, 0.01, "Actual height does not meet expected value of 200"); }); } + + [TestCategory("ConstrainedBox")] + [TestMethod] + public void Test_ConstrainedBox_AspectRatioParsing_WidthAndHeight() + { + CultureInfo currentCulture = CultureInfo.CurrentCulture; + + try + { + CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; + + AspectRatio ratio = AspectRatio.ConvertToAspectRatio("1.666:1.2"); + + Assert.AreEqual(ratio.Width, 1.666); + Assert.AreEqual(ratio.Height, 1.2); + + // Explicit tests for other culture infos, see https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4252 + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("it-IT"); + + ratio = AspectRatio.ConvertToAspectRatio("1.666:1.2"); + + Assert.AreEqual(ratio.Width, 1.666); + Assert.AreEqual(ratio.Height, 1.2); + + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fr-FR"); + + ratio = AspectRatio.ConvertToAspectRatio("1.666:1.2"); + + Assert.AreEqual(ratio.Width, 1.666); + Assert.AreEqual(ratio.Height, 1.2); + } + finally + { + CultureInfo.CurrentCulture = currentCulture; + } + } + + [TestCategory("ConstrainedBox")] + [TestMethod] + public void Test_ConstrainedBox_AspectRatioParsing_Ratio() + { + CultureInfo currentCulture = CultureInfo.CurrentCulture; + + try + { + CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; + + AspectRatio ratio = AspectRatio.ConvertToAspectRatio("1.666"); + + Assert.AreEqual(ratio.Width, 1.666); + Assert.AreEqual(ratio.Height, 1); + + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("it-IT"); + + ratio = AspectRatio.ConvertToAspectRatio("1.666"); + + Assert.AreEqual(ratio.Width, 1.666); + Assert.AreEqual(ratio.Height, 1); + + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fr-FR"); + + ratio = AspectRatio.ConvertToAspectRatio("1.666"); + + Assert.AreEqual(ratio.Width, 1.666); + Assert.AreEqual(ratio.Height, 1); + } + finally + { + CultureInfo.CurrentCulture = currentCulture; + } + } } }