Fix LinearGradientMode parameter validation to match corefx (#5672)
authorHugh Bellamy <hughbellars@gmail.com>
Mon, 2 Oct 2017 14:15:28 +0000 (15:15 +0100)
committerAlexander Köplinger <alex.koeplinger@outlook.com>
Mon, 2 Oct 2017 14:15:28 +0000 (16:15 +0200)
mcs/class/System.Drawing/System.Drawing.Drawing2D/LinearGradientBrush.cs
mcs/class/System.Drawing/Test/System.Drawing.Drawing2D/LinearGradientBrushTest.cs

index 3b006fd5c9684a4356f5ef5808b6683e39655277..d2e645d2ccbd23152f27b54043cf6ad60308cdd5 100644 (file)
@@ -67,6 +67,14 @@ namespace System.Drawing.Drawing2D {
 
                public LinearGradientBrush (Rectangle rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
                {
+                       if (linearGradientMode < LinearGradientMode.Horizontal || linearGradientMode > LinearGradientMode.BackwardDiagonal) {
+                               throw new InvalidEnumArgumentException (nameof (linearGradientMode), unchecked ((int)linearGradientMode), typeof (LinearGradientMode));
+                       }
+
+                       if (rect.Width == 0 || rect.Height == 0) {
+                               throw new ArgumentException( string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ()));
+                       }
+
                        IntPtr nativeObject;
                        Status status = GDIPlus.GdipCreateLineBrushFromRectI (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
                        GDIPlus.CheckStatus (status);
@@ -81,6 +89,14 @@ namespace System.Drawing.Drawing2D {
 
                public LinearGradientBrush (RectangleF rect, Color color1, Color color2, LinearGradientMode linearGradientMode)
                {
+                       if (linearGradientMode < LinearGradientMode.Horizontal || linearGradientMode > LinearGradientMode.BackwardDiagonal) {
+                               throw new InvalidEnumArgumentException (nameof (linearGradientMode), unchecked ((int)linearGradientMode), typeof (LinearGradientMode));
+                       }
+
+                       if (rect.Width == 0.0 || rect.Height == 0.0) {
+                               throw new ArgumentException (string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ()));
+                       }
+
                        IntPtr nativeObject;
                        Status status = GDIPlus.GdipCreateLineBrushFromRect (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject);
                        GDIPlus.CheckStatus (status);
@@ -95,6 +111,10 @@ namespace System.Drawing.Drawing2D {
 
                public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle, bool isAngleScaleable)
                {
+                       if (rect.Width == 0 || rect.Height == 0) {
+                               throw new ArgumentException (string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ()));
+                       }
+
                        IntPtr nativeObject;
                        Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngleI (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
                        GDIPlus.CheckStatus (status);
@@ -105,6 +125,10 @@ namespace System.Drawing.Drawing2D {
 
                public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle, bool isAngleScaleable)
                {
+                       if (rect.Width == 0 || rect.Height == 0) {
+                               throw new ArgumentException (string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ()));
+                       }
+
                        IntPtr nativeObject;
                        Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngle (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject);
                        GDIPlus.CheckStatus (status);
index 937c9ebbc792cae616e995b0e53c62cfb8248d3d..ebf1f86e98c011afcb83248fff95483c59b9ce40 100644 (file)
@@ -324,6 +324,42 @@ namespace MonoTests.System.Drawing.Drawing2D {
                        Assert.AreEqual (32, elements[5], 0.0001, "matrix.5");
                }
 
+               [Test]
+               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 ()
                {