Fixed culture sensitivity when parsing AspectRatio components

This commit is contained in:
Sergio Pedri 2021-09-16 11:06:30 +02:00
Родитель 6fd3327228
Коммит 7642602ed6
5 изменённых файлов: 78 добавлений и 20 удалений

Просмотреть файл

@ -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 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
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);

Просмотреть файл

@ -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
{

Просмотреть файл

@ -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;

Просмотреть файл

@ -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;

Просмотреть файл

@ -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 @@ namespace UnitTests.UWP.UI.Controls
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;
}
}
}
}