Skip to content

Commit

Permalink
Fix NullReferenceException in PaintExtensions.IsSolid on Android (dot…
Browse files Browse the repository at this point in the history
…net#28116)

* Fix NullReferenceException in PaintExtensions.IsSolid on Android

Added null check to prevent crash when SolidPaint is null, addressing issue seen in Border rendering on Android.

* - simplify

* Updated IsSolid methods using linear and radial gradients

* A few new tests

* Changes based on feedback. Use the IsNullOrEmpty method

* More changes

* Fix test

* More changes

---------

Co-authored-by: Shane Neuville <[email protected]>
Co-authored-by: Javier Suárez <[email protected]>
  • Loading branch information
3 people authored Mar 7, 2025
1 parent 2c74335 commit a730258
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/Core/src/Graphics/PaintExtensions.Android.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using Android.Content;
using Android.Content.Res;
Expand Down Expand Up @@ -84,17 +84,12 @@ internal static bool IsSolid(this AColor color)

internal static bool IsSolid(this SolidPaint paint)
{
return paint.Color.Alpha == 1;
return !paint.IsNullOrEmpty() && paint.Color.Alpha == 1;
}

internal static bool IsSolid(this LinearGradientPaint paint)
internal static bool IsSolid(this GradientPaint paint)
{
return paint.GradientStops.All(s => s.Color.Alpha == 1);
}

internal static bool IsSolid(this RadialGradientPaint paint)
{
return paint.GradientStops.All(s => s.Color.Alpha == 1);
return !paint.IsNullOrEmpty() && paint.GradientStops.All(s => s.Color?.Alpha == 1);
}

internal static bool IsValid(this GradientPaint? gradientPaint) =>
Expand Down
29 changes: 29 additions & 0 deletions src/Core/tests/DeviceTests/Graphics/GraphicsTests.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ public void SolidPaintTest(string hexColor)
Assert.True(solidPaint.IsSolid());
}

[Fact]
public void NullSolidPaintTest()
{
Color nullColor = null;
var solidPaintNullColor = new SolidPaint(nullColor);

Assert.False(solidPaintNullColor.IsSolid());

SolidPaint nullSolidPaint = null;

Assert.False(nullSolidPaint.IsSolid());
}

[Theory]
[InlineData("#FF0000", "#00FF00")]
[InlineData("#00FF00", "#0000FF")]
Expand All @@ -167,6 +180,14 @@ public void LinearGradientPaintTest(string startHexColor, string endHexColor)
Assert.True(linearGradientPaint.IsSolid());
}

[Fact]
public void NullLinearGradientPaintTest()
{
LinearGradientPaintStub nullLinearGradientPaint = null;

Assert.False(nullLinearGradientPaint.IsSolid());
}

[Theory]
[InlineData("#FF0000", "#00FF00")]
[InlineData("#00FF00", "#0000FF")]
Expand All @@ -179,4 +200,12 @@ public void RadialGradientPaintTest(string startHexColor, string endHexColor)

Assert.True(radialGradientPaint.IsSolid());
}

[Fact]
public void NullRadialGradientPaintTest()
{
RadialGradientPaintStub nullRadialGradientPaint = null;

Assert.False(nullRadialGradientPaint.IsSolid());
}
}

0 comments on commit a730258

Please sign in to comment.