Add implementation and tests for MatrixValueSerializer and MatrixConverter
authorMikhail Filippov <Mikhail.Filippov@jetbrains.com>
Wed, 9 Aug 2017 11:11:54 +0000 (14:11 +0300)
committerMarek Safar <marek.safar@gmail.com>
Thu, 10 Aug 2017 15:53:55 +0000 (17:53 +0200)
mcs/class/WindowsBase/System.Windows.Media.Converters/MatrixValueSerializer.cs
mcs/class/WindowsBase/System.Windows.Media/Matrix.cs
mcs/class/WindowsBase/System.Windows.Media/MatrixConverter.cs
mcs/class/WindowsBase/Test/System.Windows.Media/MatrixConverterTest.cs [new file with mode: 0644]
mcs/class/WindowsBase/Test/System.Windows.Media/MatrixTest.cs
mcs/class/WindowsBase/Test/System.Windows.Media/MatrixValueSerializerTest.cs [new file with mode: 0644]
mcs/class/WindowsBase/WindowsBase_test.dll.sources

index 0d039c6416e17546526d1bdbf22fb4e1213f708e..5a7b95133d353e7578d10e2ceecc975b0ab7b961 100644 (file)
@@ -24,6 +24,7 @@
 //
 
 using System;
+using System.Globalization;
 using System.Windows.Markup;
 
 namespace System.Windows.Media.Converters {
@@ -32,22 +33,26 @@ namespace System.Windows.Media.Converters {
        {
                public override bool CanConvertFromString (string value, IValueSerializerContext context)
                {
-                       throw new NotImplementedException ();
+                       return true;
                }
 
                public override bool CanConvertToString (object value, IValueSerializerContext context)
                {
-                       throw new NotImplementedException ();
+                       return value is Matrix;
                }
 
                public override object ConvertFromString (string value, IValueSerializerContext context)
                {
-                       throw new NotImplementedException ();
+                       if (value == null)
+                               throw new NotSupportedException ("value != null");
+                       return Matrix.Parse (value);
                }
 
                public override string ConvertToString (object value, IValueSerializerContext context)
                {
-                       throw new NotImplementedException ();
+                       if (value is Matrix matrix)
+                               return matrix.ToString (CultureInfo.InvariantCulture);
+                       return base.ConvertToString (value, context);
                }
        }
 
index 4d0245c3746575153ead243e9585a895028811ae..e9c60b528db3bbaa88eab8cad06d50165b7df2c8 100644 (file)
@@ -25,6 +25,7 @@
 
 using System;
 using System.ComponentModel;
+using System.Globalization;
 using System.Windows.Markup;
 using System.Windows.Media.Converters;
 using System.Windows.Threading;
@@ -109,7 +110,16 @@ namespace System.Windows.Media {
 
                public override int GetHashCode ()
                {
-                       throw new NotImplementedException ();
+                       unchecked
+                       {
+                               var hashCode = _m11.GetHashCode ();
+                               hashCode = (hashCode * 397) ^ _m12.GetHashCode ();
+                               hashCode = (hashCode * 397) ^ _m21.GetHashCode ();
+                               hashCode = (hashCode * 397) ^ _m22.GetHashCode ();
+                               hashCode = (hashCode * 397) ^ _offsetX.GetHashCode ();
+                               hashCode = (hashCode * 397) ^ _offsetY.GetHashCode ();
+                               return hashCode;
+                       }
                }
 
                public void Invert ()
@@ -167,7 +177,39 @@ namespace System.Windows.Media {
 
                public static Matrix Parse (string source)
                {
-                       throw new NotImplementedException ();
+                       if (source == null)
+                               throw new ArgumentNullException ("source");
+                       Matrix value;
+                       if (source.Trim () == "Identity")
+                       {
+                               value = Identity;
+                       }
+                       else
+                       {
+                               var parts = source.Split (',');
+                               if (parts.Length != 6)
+                                       throw new FormatException (string.Format ("Invalid Matrix format: {0}", source));
+                               double m11;
+                               double m12;
+                               double m21;
+                               double m22;
+                               double offsetX;
+                               double offsetY;
+                               if (double.TryParse (parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out m11)
+                                   && double.TryParse (parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out m12)
+                                   && double.TryParse (parts[2], NumberStyles.Float, CultureInfo.InvariantCulture, out m21)
+                                   && double.TryParse (parts[3], NumberStyles.Float, CultureInfo.InvariantCulture, out m22)
+                                   && double.TryParse (parts[4], NumberStyles.Float, CultureInfo.InvariantCulture, out offsetX)
+                                   && double.TryParse (parts[5], NumberStyles.Float, CultureInfo.InvariantCulture, out offsetY))
+                               {
+                                       value = new Matrix (m11, m12, m21, m22, offsetX, offsetY);
+                               }
+                               else
+                               {
+                                       throw new FormatException (string.Format ("Invalid Matrix format: {0}", source));
+                               }
+                       }
+                       return value;
                }
 
                public void Prepend (Matrix matrix)
@@ -297,21 +339,19 @@ namespace System.Windows.Media {
                string IFormattable.ToString (string format,
                                              IFormatProvider provider)
                {
-                       throw new NotImplementedException ();
+                       return ToString (provider);
                }
 
                public override string ToString ()
                {
-                       if (IsIdentity)
-                               return "Identity";
-                       else
-                               return string.Format ("{0},{1},{2},{3},{4},{5}",
-                                                     _m11, _m12, _m21, _m22, _offsetX, _offsetY);
+                       return ToString (null);
                }
 
                public string ToString (IFormatProvider provider)
                {
-                       throw new NotImplementedException ();
+                       return IsIdentity
+                               ? "Identity"
+                               : string.Concat (_m11, ",", _m12, ",", _m21, ",", _m22, ",", _offsetX, ",", _offsetY);
                }
 
                public Point Transform (Point point)
index e5ab056a14f95f7c45b2720f5b0fd55cb8f4aca0..596d21199ef2657de3557d3db7ddf536cd42b256 100644 (file)
@@ -33,22 +33,24 @@ namespace System.Windows.Media {
        {
                public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
                {
-                       throw new NotImplementedException ();
+                       return sourceType == typeof (string);
                }
 
                public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
                {
-                       throw new NotImplementedException ();
+                       return destinationType == typeof (string);
                }
 
                public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
                {
-                       throw new NotImplementedException ();
+                       if (!(value is string))
+                               throw new NotSupportedException ("MatrixConverter only supports converting from strings");
+                       return Matrix.Parse ((string)value);
                }
 
                public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
                {
-                       throw new NotImplementedException ();
+                       return ((Matrix)value).ToString (culture);
                }
        }
 
diff --git a/mcs/class/WindowsBase/Test/System.Windows.Media/MatrixConverterTest.cs b/mcs/class/WindowsBase/Test/System.Windows.Media/MatrixConverterTest.cs
new file mode 100644 (file)
index 0000000..9134aec
--- /dev/null
@@ -0,0 +1,64 @@
+using NUnit.Framework;
+using System;
+using System.Windows.Media;
+
+namespace MonoTests.System.Windows.Media {
+
+       [TestFixture]
+       public class MatrixConverterTest {
+               const double DELTA = 0.000000001d;
+
+               void CheckMatrix (Matrix expected, Matrix actual)
+               {
+                       Assert.AreEqual (expected.M11, actual.M11, DELTA);
+                       Assert.AreEqual (expected.M12, actual.M12, DELTA);
+                       Assert.AreEqual (expected.M21, actual.M21, DELTA);
+                       Assert.AreEqual (expected.M22, actual.M22, DELTA);
+                       Assert.AreEqual (expected.OffsetX, actual.OffsetX, DELTA);
+                       Assert.AreEqual (expected.OffsetY, actual.OffsetY, DELTA);
+               }
+
+               [Test]
+               public void CanConvertFrom ()
+               {
+                       var conv = new MatrixConverter ();
+                       Assert.IsTrue (conv.CanConvertFrom (typeof (string)));
+                       Assert.IsFalse (conv.CanConvertFrom (typeof (Matrix)));
+               }
+
+               [Test]
+               public void CanConvertTo ()
+               {
+                       var conv = new MatrixConverter ();
+                       Assert.IsTrue (conv.CanConvertTo (typeof (string)));
+                       Assert.IsFalse (conv.CanConvertTo (typeof (Matrix)));
+               }
+
+               [Test]
+               public void ConvertFrom ()
+               {
+                       var conv = new MatrixConverter ();
+                       object obj = conv.ConvertFrom ("1, 2, 3, 4, 5, 6");
+                       Assert.AreEqual (typeof (Matrix), obj.GetType ());
+                       CheckMatrix (new Matrix (1, 2, 3, 4, 5, 6), (Matrix)obj);
+               }
+
+               [Test]
+               [ExpectedException (typeof (NotSupportedException))]
+               public void ConvertFromInvalidType ()
+               {
+                       var conv = new MatrixConverter ();
+                       conv.ConvertFrom (new Matrix (10, 20, 30, 40, 50, 60));
+               }
+
+               [Test]
+               public void ConvertTo ()
+               {
+                       var conv = new MatrixConverter ();
+                       var matrix = new Matrix (1, 2, 3, 4, 5, 6);
+                       object obj = conv.ConvertTo (matrix, typeof (string));
+                       Assert.AreEqual (typeof (string), obj.GetType ());
+                       Assert.AreEqual ("1,2,3,4,5,6", (string)obj);
+               }
+       }
+}
\ No newline at end of file
index ceb99bdfa2da8d4fd45b6e85404fade72ab6dcba..cbccb4339bb10d2facce83d497a179b8f504f779 100644 (file)
@@ -158,7 +158,6 @@ namespace MonoTests.System.Windows.Media {
                }
 
                [Test]
-               [Category ("NotWorking")]
                public void Parse ()
                {
                        CheckMatrix (Matrix.Identity, Matrix.Parse ("Identity"));
diff --git a/mcs/class/WindowsBase/Test/System.Windows.Media/MatrixValueSerializerTest.cs b/mcs/class/WindowsBase/Test/System.Windows.Media/MatrixValueSerializerTest.cs
new file mode 100644 (file)
index 0000000..f8b9a6a
--- /dev/null
@@ -0,0 +1,79 @@
+using NUnit.Framework;
+using System;
+using System.Windows.Media.Converters;
+using System.Windows.Media;
+
+namespace MonoTests.System.Windows.Media {
+
+       [TestFixture]
+       public class MatrixValueSerializerTest {
+               const double DELTA = 0.000000001d;
+
+               void CheckMatrix (Matrix expected, Matrix actual)
+               {
+                       Assert.AreEqual (expected.M11, actual.M11, DELTA);
+                       Assert.AreEqual (expected.M12, actual.M12, DELTA);
+                       Assert.AreEqual (expected.M21, actual.M21, DELTA);
+                       Assert.AreEqual (expected.M22, actual.M22, DELTA);
+                       Assert.AreEqual (expected.OffsetX, actual.OffsetX, DELTA);
+                       Assert.AreEqual (expected.OffsetY, actual.OffsetY, DELTA);
+               }
+
+               [Test]
+               public void CanConvertFromString ()
+               {
+                       var serializer = new MatrixValueSerializer ();
+                       Assert.IsTrue (serializer.CanConvertFromString ("", null));
+               }
+
+               [Test]
+               public void CanConvertToString ()
+               {
+                       var serializer = new MatrixValueSerializer ();
+                       Assert.IsTrue (serializer.CanConvertToString (new Matrix (1, 2, 3, 4, 5 ,6), null));
+                       Assert.IsFalse (serializer.CanConvertToString ("", null));
+               }
+
+               [Test]
+               public void ConvertFromString ()
+               {
+                       var serializer = new MatrixValueSerializer ();
+                       object obj = serializer.ConvertFromString ("1, 2, 3, 4, 5 ,6", null);
+                       Assert.AreEqual (typeof (Matrix), obj.GetType ());
+                       CheckMatrix (new Matrix (1, 2, 3, 4, 5, 6), (Matrix)obj);
+               }
+
+               [Test]
+               public void RoundTripConvert()
+               {
+                       var serializer = new MatrixValueSerializer ();
+                       var matrix = new Matrix (1.1, 2.2, 3.3, 4.4, 5.5, 6.6);
+                       var obj = serializer.ConvertFromString (serializer.ConvertToString (matrix, null), null);
+                       CheckMatrix (matrix, (Matrix)obj);
+               }
+
+               [Test]
+               [ExpectedException (typeof (FormatException))]
+               public void ConvertFromStringShouldThrowExceptionWhenStringHasInvalidFormat ()
+               {
+                       var serializer = new MatrixValueSerializer ();
+                       serializer.ConvertFromString ("a,b,c,d,e,f", null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (NotSupportedException))]
+               public void ConvertFromStringShouldThrowExceptionWhenStringIsNull ()
+               {
+                       var serializer = new MatrixValueSerializer ();
+                       serializer.ConvertFromString (null, null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (NotSupportedException))]
+               public void ConvertToStringShouldThrowExceptionWhenInvalidType ()
+               {
+                       var serializer = new MatrixValueSerializer ();
+                       serializer.ConvertToString (10, null);
+               }
+       }
+}
\ No newline at end of file
index 51cb1c62d042a60b529b17d10e40cd943d0d03a7..5fba4fc771069f5e0a6d9d4a0e42338e1ab55547 100644 (file)
@@ -43,5 +43,7 @@ System.Windows.Markup/DependsOnAttributeTest.cs
 System.Windows.Markup/MarkupExtensionReturnTypeAttributeTest.cs
 System.Windows.Markup/ValueSerializerTest.cs
 System.Windows.Media/MatrixTest.cs
+System.Windows.Media/MatrixConverterTest.cs
+System.Windows.Media/MatrixValueSerializerTest.cs
 System.Windows.Threading/DispatcherTest.cs
 System.Windows.Threading/DispatcherTimerTest.cs