Skip to content

Commit

Permalink
Merge pull request #4253 from Sergio0694/bugfix/aspectratio-localized…
Browse files Browse the repository at this point in the history
…-parsing

Fixed culture sensitivity when parsing AspectRatio components
  • Loading branch information
Rosuavio authored Sep 16, 2021
2 parents 0d73af7 + 7642602 commit 0dfba3b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
}
}

0 comments on commit 0dfba3b

Please sign in to comment.