Fix LinearGradientMode parameter validation to match corefx (#5672)
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Drawing2D / LinearGradientBrushTest.cs
index 5f48a2cf1f7ba6cc3c0c55698ec34072e43e76ee..ebf1f86e98c011afcb83248fff95483c59b9ce40 100644 (file)
@@ -4,7 +4,7 @@
 // Authors:
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
-// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2006, 2008 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -70,12 +70,12 @@ namespace MonoTests.System.Drawing.Drawing2D {
                private void CheckDefaultMatrix (Matrix matrix)
                {
                        float[] elements = matrix.Elements;
-                       Assert.AreEqual (1, elements[0], 0.1, "matrix.0");
-                       Assert.AreEqual (1, elements[1], 0.1, "matrix.1");
-                       Assert.AreEqual (-1, elements[2], 0.1, "matrix.2");
-                       Assert.AreEqual (1, elements[3], 0.1, "matrix.3");
-                       Assert.AreEqual (16, elements[4], "matrix.4");
-                       Assert.AreEqual (-16, elements[5], "matrix.5");
+                       Assert.AreEqual (1.0f, elements[0], 0.1, "matrix.0");
+                       Assert.AreEqual (1.0f, elements[1], 0.1, "matrix.1");
+                       Assert.AreEqual (-1.0f, elements[2], 0.1, "matrix.2");
+                       Assert.AreEqual (1.0f, elements[3], 0.1, "matrix.3");
+                       Assert.AreEqual (16.0f, elements[4], 0.1, "matrix.4");
+                       Assert.AreEqual (-16.0f, elements[5], 0.1, "matrix.5");
                }
 
                private void CheckBrushAt45 (LinearGradientBrush lgb)
@@ -325,33 +325,65 @@ namespace MonoTests.System.Drawing.Drawing2D {
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
+               public void Constructor_Rectangle_InvalidWidthHeight ()
+               {
+                       var emptyWidth = new Rectangle (0, 0, 0, 1);
+                       var emptyHeight = new Rectangle (0, 0, 0, 1);
+
+                       Assert.Throws<ArgumentException>(() => new LinearGradientBrush (emptyWidth, Color.Empty, Color.Empty, 1));
+                       Assert.Throws<ArgumentException>(() => new LinearGradientBrush (emptyHeight, Color.Empty, Color.Empty, 1));
+                       Assert.Throws<ArgumentException>(() => new LinearGradientBrush (emptyWidth, Color.Empty, Color.Empty, LinearGradientMode.BackwardDiagonal));
+                       Assert.Throws<ArgumentException>(() => new LinearGradientBrush (emptyHeight, Color.Empty, Color.Empty, LinearGradientMode.BackwardDiagonal));
+               }
+
+               [Test]
+               public void Constructor_RectangleF_InvalidWidthHeight ()
+               {
+                       var emptyWidth = new RectangleF (0, 0, 0, 1);
+                       var emptyHeight = new RectangleF (0, 0, 0, 1);
+
+                       Assert.Throws<ArgumentException>(() => new LinearGradientBrush (emptyWidth, Color.Empty, Color.Empty, 1));
+                       Assert.Throws<ArgumentException>(() => new LinearGradientBrush (emptyHeight, Color.Empty, Color.Empty, 1));
+                       Assert.Throws<ArgumentException>(() => new LinearGradientBrush (emptyWidth, Color.Empty, Color.Empty, LinearGradientMode.BackwardDiagonal));
+                       Assert.Throws<ArgumentException>(() => new LinearGradientBrush (emptyHeight, Color.Empty, Color.Empty, LinearGradientMode.BackwardDiagonal));
+               }
+
+               [Test]
+               public void Constructor_LinearGradientMode_InvalidMode ()
+               {
+                       var rect = new Rectangle (0, 0, 1, 1);
+                       var rectf = new RectangleF (0, 0, 1, 1);
+
+                       Assert.Throws<InvalidEnumArgumentException>(() => new LinearGradientBrush (rect, Color.Empty, Color.Empty, LinearGradientMode.Horizontal - 1));
+                       Assert.Throws<InvalidEnumArgumentException>(() => new LinearGradientBrush (rectf, Color.Empty, Color.Empty, LinearGradientMode.Horizontal - 1));
+                       Assert.Throws<InvalidEnumArgumentException>(() => new LinearGradientBrush (rect, Color.Empty, Color.Empty, LinearGradientMode.BackwardDiagonal + 1));
+                       Assert.Throws<InvalidEnumArgumentException>(() => new LinearGradientBrush (rectf, Color.Empty, Color.Empty, LinearGradientMode.BackwardDiagonal + 1));
+               }
+
+               [Test]
                public void InterpolationColors_Colors_InvalidBlend ()
                {
                        // default Blend doesn't allow getting this property
-                       Assert.IsNotNull (default_brush.InterpolationColors.Colors);
+                       Assert.Throws<ArgumentException> (() => { var x = default_brush.InterpolationColors.Colors; });
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void InterpolationColors_Positions_InvalidBlend ()
                {
                        // default Blend doesn't allow getting this property
-                       Assert.IsNotNull (default_brush.InterpolationColors.Positions);
+                       Assert.Throws<ArgumentException> (() => { var x = default_brush.InterpolationColors.Positions; });
                }
 
                [Test]
-               [ExpectedException (typeof (IndexOutOfRangeException))]
                public void LinearColors_Empty ()
                {
-                       default_brush.LinearColors = new Color[0];
+                       Assert.Throws<IndexOutOfRangeException> (() => default_brush.LinearColors = new Color[0]);
                }
 
                [Test]
-               [ExpectedException (typeof (IndexOutOfRangeException))]
                public void LinearColors_One ()
                {
-                       default_brush.LinearColors = new Color[1];
+                       Assert.Throws<IndexOutOfRangeException> (() => default_brush.LinearColors = new Color[1]);
                }
 
                [Test]
@@ -400,10 +432,9 @@ namespace MonoTests.System.Drawing.Drawing2D {
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentNullException))]
                public void Transform_Null ()
                {
-                       default_brush.Transform = null;
+                       Assert.Throws<ArgumentNullException> (() => default_brush.Transform = null);
                }
 
                [Test]
@@ -415,10 +446,9 @@ namespace MonoTests.System.Drawing.Drawing2D {
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void Transform_NonInvertible ()
                {
-                       default_brush.Transform = new Matrix (123, 24, 82, 16, 47, 30);
+                       Assert.Throws<ArgumentException> (() => default_brush.Transform = new Matrix (123, 24, 82, 16, 47, 30));
                }
 
                [Test]
@@ -436,17 +466,15 @@ namespace MonoTests.System.Drawing.Drawing2D {
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void WrapMode_Clamp ()
                {
-                       default_brush.WrapMode = WrapMode.Clamp;
+                       Assert.Throws<ArgumentException> (() => default_brush.WrapMode = WrapMode.Clamp);
                }
 
                [Test]
-               [ExpectedException (typeof (InvalidEnumArgumentException))]
                public void WrapMode_Invalid ()
                {
-                       default_brush.WrapMode = (WrapMode) Int32.MinValue;
+                       Assert.Throws<InvalidEnumArgumentException> (() => default_brush.WrapMode = (WrapMode) Int32.MinValue);
                }
 
 
@@ -466,17 +494,15 @@ namespace MonoTests.System.Drawing.Drawing2D {
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentNullException))]
                public void MultiplyTransform1_Null ()
                {
-                       default_brush.MultiplyTransform (null);
+                       Assert.Throws<ArgumentNullException> (() => default_brush.MultiplyTransform (null));
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentNullException))]
                public void MultiplyTransform2_Null ()
                {
-                       default_brush.MultiplyTransform (null, MatrixOrder.Append);
+                       Assert.Throws<ArgumentNullException> (() => default_brush.MultiplyTransform (null, MatrixOrder.Append));
                }
 
                [Test]
@@ -486,11 +512,10 @@ namespace MonoTests.System.Drawing.Drawing2D {
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void MultiplyTransform_NonInvertible ()
                {
                        Matrix noninvertible = new Matrix (123, 24, 82, 16, 47, 30);
-                       default_brush.MultiplyTransform (noninvertible);
+                       Assert.Throws<ArgumentException> (() => default_brush.MultiplyTransform (noninvertible));
                }
 
                [Test]
@@ -550,11 +575,10 @@ namespace MonoTests.System.Drawing.Drawing2D {
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void RotateTransform_InvalidOrder ()
                {
                        LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
-                       lgb.RotateTransform (720, (MatrixOrder) Int32.MinValue);
+                       Assert.Throws<ArgumentException> (() => lgb.RotateTransform (720, (MatrixOrder) Int32.MinValue));
                }
 
                [Test]
@@ -603,11 +627,10 @@ namespace MonoTests.System.Drawing.Drawing2D {
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void ScaleTransform_InvalidOrder ()
                {
                        LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
-                       lgb.ScaleTransform (1, 1, (MatrixOrder) Int32.MinValue);
+                       Assert.Throws<ArgumentException> (() => lgb.ScaleTransform (1, 1, (MatrixOrder) Int32.MinValue));
                }
 
                [Test]
@@ -643,31 +666,27 @@ namespace MonoTests.System.Drawing.Drawing2D {
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void SetBlendTriangularShape_FocusTooSmall ()
                {
-                       default_brush.SetBlendTriangularShape (-1);
+                       Assert.Throws<ArgumentException> (() => default_brush.SetBlendTriangularShape (-1));
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void SetBlendTriangularShape_FocusTooBig ()
                {
-                       default_brush.SetBlendTriangularShape (1.01f);
+                       Assert.Throws<ArgumentException> (() => default_brush.SetBlendTriangularShape (1.01f));
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void SetBlendTriangularShape_ScaleTooSmall ()
                {
-                       default_brush.SetBlendTriangularShape (1, -1);
+                       Assert.Throws<ArgumentException> (() => default_brush.SetBlendTriangularShape (1, -1));
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void SetBlendTriangularShape_ScaleTooBig ()
                {
-                       default_brush.SetBlendTriangularShape (1, 1.01f);
+                       Assert.Throws<ArgumentException> (() => default_brush.SetBlendTriangularShape (1, 1.01f));
                }
 
                [Test]
@@ -703,31 +722,27 @@ namespace MonoTests.System.Drawing.Drawing2D {
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void SetSigmaBellShape_FocusTooSmall ()
                {
-                       default_brush.SetSigmaBellShape (-1);
+                       Assert.Throws<ArgumentException> (() => default_brush.SetSigmaBellShape (-1));
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void SetSigmaBellShape_FocusTooBig ()
                {
-                       default_brush.SetSigmaBellShape (1.01f);
+                       Assert.Throws<ArgumentException> (() => default_brush.SetSigmaBellShape (1.01f));
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void SetSigmaBellShape_ScaleTooSmall ()
                {
-                       default_brush.SetSigmaBellShape (1, -1);
+                       Assert.Throws<ArgumentException> (() => default_brush.SetSigmaBellShape (1, -1));
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void SetSigmaBellShape_ScaleTooBig ()
                {
-                       default_brush.SetSigmaBellShape (1, 1.01f);
+                       Assert.Throws<ArgumentException> (() => default_brush.SetSigmaBellShape (1, 1.01f));
                }
 
                [Test]
@@ -755,11 +770,10 @@ namespace MonoTests.System.Drawing.Drawing2D {
                }
 
                [Test]
-               [ExpectedException (typeof (ArgumentException))]
                public void TranslateTransform_InvalidOrder ()
                {
                        LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
-                       lgb.TranslateTransform (1, 1, (MatrixOrder) Int32.MinValue);
+                       Assert.Throws<ArgumentException> (() => lgb.TranslateTransform (1, 1, (MatrixOrder) Int32.MinValue));
                }
 
                [Test]
@@ -873,5 +887,39 @@ namespace MonoTests.System.Drawing.Drawing2D {
                        CheckMatrixForScalableAngle (new RectangleF (30, 60, 90, 150), 215, new float[] { -1.140856f, -1.331394f, 0.4793016f, -1.140856f, 95.85849f, 388.8701f });
                        CheckMatrixForScalableAngle (new RectangleF (30, 60, 90, 150), 300, new float[] { 0.6830127f, -1.971688f, 0.7098075f, 0.6830125f, -72.04998f, 190.6699f });
                }
+
+               [Test]
+               public void LinearColors_Null ()
+               {
+                       Assert.Throws<NullReferenceException> (() => default_brush.LinearColors = null);
+               }
+
+               [Test]
+               public void InterpolationColors_Null ()
+               {
+                       Assert.Throws<ArgumentException> (() => default_brush.InterpolationColors = null);
+               }
+
+               [Test]
+               public void Blend_Null ()
+               {
+                       Assert.Throws<NullReferenceException> (() => default_brush.Blend = null);
+               }
+
+               [Test]
+               public void ZeroWidthRectangle ()
+               {
+                       Rectangle r = new Rectangle (10, 10, 0, 10);
+                       Assert.AreEqual (0, r.Width, "Width");
+                       Assert.Throws<ArgumentException> (() => new LinearGradientBrush (r, Color.Red, Color.Blue, LinearGradientMode.Vertical));
+               }
+
+               [Test]
+               public void ZeroHeightRectangleF ()
+               {
+                       RectangleF r = new RectangleF (10.0f, 10.0f, 10.0f, 0.0f);
+                       Assert.AreEqual (0.0f, r.Height, "Height");
+                       Assert.Throws<ArgumentException> (() => new LinearGradientBrush (r, Color.Red, Color.Blue, LinearGradientMode.Vertical));
+               }
        }
 }