//
using System;
+using System.Globalization;
using System.Windows.Markup;
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);
}
}
using System;
using System.ComponentModel;
+using System.Globalization;
using System.Windows.Markup;
using System.Windows.Media.Converters;
using System.Windows.Threading;
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 ()
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)
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)
{
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);
}
}
--- /dev/null
+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
}
[Test]
- [Category ("NotWorking")]
public void Parse ()
{
CheckMatrix (Matrix.Identity, Matrix.Parse ("Identity"));
--- /dev/null
+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
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