From 3dc42027bb5f33da787e893b951d89fdc2d0e7d1 Mon Sep 17 00:00:00 2001 From: Michael Kirschner Date: Wed, 25 Sep 2019 13:50:18 -0400 Subject: [PATCH] use invariant culture to parse inputs to converters - (#10006) * use invariant culture to parse inputs - we were passing the culture before but this was usually set to en-us EVEN in localized enviornments unless something set it explicitly wrap with try catch and return some default conversion * Update src/DynamoCoreWpf/UI/Converters.cs * Update src/DynamoCoreWpf/UI/Converters.cs --- src/DynamoCoreWpf/UI/Converters.cs | 77 +++++++++++++++++++---- test/DynamoCoreWpfTests/ConverterTests.cs | 63 +++++++++++++------ 2 files changed, 111 insertions(+), 29 deletions(-) diff --git a/src/DynamoCoreWpf/UI/Converters.cs b/src/DynamoCoreWpf/UI/Converters.cs index 9f4f20da750..53a2e5f0879 100644 --- a/src/DynamoCoreWpf/UI/Converters.cs +++ b/src/DynamoCoreWpf/UI/Converters.cs @@ -1414,15 +1414,29 @@ public object ConvertBack(object value, Type targetType, object parameter, Syste } } + //TODO remove(this is not used anywhere) in Dynamo 3.0 public class ZoomToVisibilityConverter : IValueConverter { + /// + /// Returns hidden for small zoom sizes - appears unused. + /// + /// zoom size + /// unused + /// unused + /// unused + /// public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { - double zoom = System.Convert.ToDouble(value, culture); - - if (zoom < .5) - return Visibility.Hidden; - + try + { + double zoom = System.Convert.ToDouble(value, CultureInfo.InvariantCulture); + if (zoom < .5) + return Visibility.Hidden; + } + catch(Exception e) + { + Console.WriteLine($"problem attempting to parse zoomsize or param {value}{ e.Message}"); + } return Visibility.Visible; } @@ -2208,12 +2222,31 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu public class MenuItemCheckConverter : IValueConverter { + /// + /// Converts from a fontsize and param to determine if the two numbers are equal.(ie what is the font set to) + /// + /// fontSize + /// unusued + /// target font size + /// unusued + /// public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { - var fontsize = System.Convert.ToDouble(value, culture); - var param = System.Convert.ToDouble(parameter, culture); - - return fontsize == param; + + //use invariant culture, these strings should always be set via our code. + try + { + var fontsize = System.Convert.ToDouble(value, CultureInfo.InvariantCulture); + var param = System.Convert.ToDouble(parameter, CultureInfo.InvariantCulture); + return fontsize == param; + } + + catch(Exception e) + { + Console.WriteLine($"problem attempting to parse fontsize or param {value} {parameter} { e.Message}"); + return false; + } + } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) @@ -2265,10 +2298,32 @@ public class GroupFontSizeToEditorEnabledConverter : IMultiValueConverter { private const double MinFontFactor = 7.0; + /// + /// converts a zoom and fontsize to a bool used to determine if group title editor should be enabled. + /// + /// [0] zoom [1] fontSize - could be strings or doubles + /// unused + /// unused + /// unused + /// public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { - var zoom = System.Convert.ToDouble(values[0], culture); - var fontsize = System.Convert.ToDouble(values[1], culture); + //defaults + var zoom = 1.0; + var fontsize = 36.0; + try + { + //use invariantCulture - + //fontSize should only be serialized in invariant culture + // and zoom should either come from fallback value or runtime value. + zoom = System.Convert.ToDouble(values[0], CultureInfo.InvariantCulture); + fontsize = System.Convert.ToDouble(values[1], CultureInfo.InvariantCulture); + } + //just use defaults, this will enable the text editor. + catch(Exception e) + { + Console.WriteLine($"problem attempting to parse fontsize or zoom {values[1]} {values[0]}. { e.Message}"); + } var factor = zoom*fontsize; if (factor < MinFontFactor) diff --git a/test/DynamoCoreWpfTests/ConverterTests.cs b/test/DynamoCoreWpfTests/ConverterTests.cs index 38648df0761..c109fabdc68 100644 --- a/test/DynamoCoreWpfTests/ConverterTests.cs +++ b/test/DynamoCoreWpfTests/ConverterTests.cs @@ -636,45 +636,72 @@ public void ZoomToVisibilityConverterTest() [Category("UnitTests")] public void MenuItemCheckConverterTest() { - object equal; + object result; MenuItemCheckConverter converter = new MenuItemCheckConverter(); - equal = converter.Convert("1.0", typeof(string), "1.0", new CultureInfo("en-US")); - Assert.AreEqual(equal, true); + result = converter.Convert("1.0", typeof(string), "1.0", new CultureInfo("en-US")); + Assert.AreEqual(result, true); + + result = converter.Convert("1,0", typeof(string), "1,0", new CultureInfo("de-DE")); + Assert.AreEqual(result, true); + + result = converter.Convert(1.0, typeof(double), 1.0, new CultureInfo("en-US")); + Assert.AreEqual(result, true); - equal = converter.Convert("1,0", typeof(string), "1,0", new CultureInfo("de-DE")); - Assert.AreEqual(equal, true); + result = converter.Convert(1.0, typeof(double), 1.0, new CultureInfo("de-DE")); + Assert.AreEqual(result, true); - equal = converter.Convert(1.0, typeof(double), 1.0, new CultureInfo("en-US")); - Assert.AreEqual(equal, true); + result = converter.Convert(1.0, null, 1.0, null); + Assert.AreEqual(result, true); - equal = converter.Convert(1.0, typeof(double), 1.0, new CultureInfo("de-DE")); - Assert.AreEqual(equal, true); + result = converter.Convert(1.0, null, 100, null); + Assert.AreEqual(result, false); } [Test] [Category("UnitTests")] public void GroupFontSizeToEditorEnabledConverterTest() { - object equal; + object result; GroupFontSizeToEditorEnabledConverter converter = new GroupFontSizeToEditorEnabledConverter(); + //obj [1] = zoom, obj [2]= fontsize + string[] stringsUsDefault = { "1.0", "36.0" }; + string[] stringsUsDefaultResultInFalse = { "1.0", "2.0" }; string[] stringsUs = { "2.0", "4.0" }; string[] stringsDe = { "2,0", "4,0" }; double[] doubles = { 2.0, 4.0 }; object[] objects = new object[2]; doubles.CopyTo(objects, 0); - equal = converter.Convert(stringsUs , typeof(string), null, new CultureInfo("en-US")); - Assert.AreEqual(equal, true); + result = converter.Convert(stringsUs , typeof(string), null, new CultureInfo("en-US")); + Assert.AreEqual(result, true); + + result = converter.Convert(stringsDe, typeof(string), null, new CultureInfo("de-DE")); + Assert.AreEqual(result, true); + + result = converter.Convert(objects, typeof(float), null, new CultureInfo("en-US")); + Assert.AreEqual(result, true); + + result = converter.Convert(objects, typeof(float), null, new CultureInfo("de-DE")); + Assert.AreEqual(result, true); + + result = converter.Convert(stringsDe, typeof(float), null, new CultureInfo("de-DE")); + Assert.AreEqual(result, true); + + result = converter.Convert(stringsUsDefault, typeof(float), null, new CultureInfo("de-DE")); + Assert.AreEqual(result, true); + + result = converter.Convert(stringsUsDefault, typeof(float), null, new CultureInfo("fr-FR")); + Assert.AreEqual(result, true); - equal = converter.Convert(stringsDe, typeof(string), null, new CultureInfo("de-DE")); - Assert.AreEqual(equal, true); + result = converter.Convert(stringsDe, typeof(float), null, new CultureInfo("fr-FR")); + Assert.AreEqual(result, true); - equal = converter.Convert(objects, typeof(float), null, new CultureInfo("en-US")); - Assert.AreEqual(equal, true); + result = converter.Convert(stringsUsDefault, null, null, null); + Assert.AreEqual(result, true); - equal = converter.Convert(objects, typeof(float), null, new CultureInfo("de-DE")); - Assert.AreEqual(equal, true); + result = converter.Convert(stringsUsDefaultResultInFalse, null, null, null); + Assert.AreEqual(result, false); } } }