Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz committed Jul 19, 2023
1 parent 2bf3d11 commit ce38d97
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/Controls/src/Core/Brush/Brush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public static implicit operator Paint(Brush brush)
for (int i = 0; i < gradientStopCollection.Count; i++)
{
var gs = gradientStopCollection[i];
gradientStops[i] = new GraphicsGradientStop(gs.Offset, gs.Color);

if (gs is not null)
gradientStops[i] = new GraphicsGradientStop(gs.Offset, gs.Color);
}

if (gradientBrush is LinearGradientBrush linearGradientBrush)
Expand Down
7 changes: 5 additions & 2 deletions src/Controls/src/Core/GradientBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ void UpdateGradientStops(GradientStopCollection oldCollection, GradientStopColle

foreach (var newStop in newCollection)
{
newStop.Parent = this;
newStop.PropertyChanged += OnGradientStopPropertyChanged;
if (newStop is not null)
{
newStop.Parent = this;
newStop.PropertyChanged += OnGradientStopPropertyChanged;
}
}
}

Expand Down
41 changes: 40 additions & 1 deletion src/Controls/tests/Core.UnitTests/LinearGradientBrushTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void TestNullOrEmptyLinearGradientBrush()
}

[Fact]
public void TestNullOrEmptyLinearGradientPaint()
public void TestNullOrEmptyLinearGradientPaintWithEmptyGradientStop()
{
LinearGradientBrush linearGradientBrush = new LinearGradientBrush
{
Expand All @@ -93,6 +93,45 @@ public void TestNullOrEmptyLinearGradientPaint()
Assert.True(linearGradientPaint.IsNullOrEmpty());
}

[Fact]
public void TestNullOrEmptyLinearGradientPaintWithNullGradientStop()
{
LinearGradientBrush linearGradientBrush = new LinearGradientBrush
{
StartPoint = new Point(0, 0),
EndPoint = new Point(1, 0),
GradientStops = new GradientStopCollection
{
null,
null
}
};

Paint linearGradientPaint = linearGradientBrush;

Assert.True(linearGradientPaint.IsNullOrEmpty());
}

[Fact]
public void TestNullGradientStopLinearGradientPaint()
{
LinearGradientBrush linearGradientBrush = new LinearGradientBrush
{
StartPoint = new Point(0, 0),
EndPoint = new Point(1, 0),
GradientStops = new GradientStopCollection
{
new GradientStop { Color = Colors.Red, Offset = 0.1f },
null,
new GradientStop { Color = Colors.Blue, Offset = 1.0f }
}
};

Paint linearGradientPaint = linearGradientBrush;

Assert.False(linearGradientPaint.IsNullOrEmpty());
}

[Fact]
public void TestLinearGradientBrushPoints()
{
Expand Down
38 changes: 38 additions & 0 deletions src/Controls/tests/Core.UnitTests/RadialGradientBrushTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,44 @@ public void TestConstructor()
Assert.Equal(0, gradientStops);
}

[Fact]
public void TestNullOrEmptyRadialGradientPaintWithEmptyGradientStop()
{
RadialGradientBrush radialGradientBrush = new RadialGradientBrush
{
Center = new Point(0, 0),
Radius = 10,
GradientStops = new GradientStopCollection
{
new GradientStop(),
new GradientStop()
}
};

Paint radialGradientPaint = radialGradientBrush;

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

[Fact]
public void TestNullOrEmptyRadialGradientPaintWithNullGradientStop()
{
RadialGradientBrush radialGradientBrush = new RadialGradientBrush
{
Center = new Point(0, 0),
Radius = 10,
GradientStops = new GradientStopCollection
{
null,
null
}
};

Paint radialGradientPaint = radialGradientBrush;

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

[Fact]
public void TestConstructorUsingGradientStopCollection()
{
Expand Down
10 changes: 5 additions & 5 deletions src/Graphics/src/Graphics/GradientPaint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public PaintGradientStop[] GradientStops

public Color StartColor
{
get => _gradientStops[StartColorIndex].Color;
get => _gradientStops[StartColorIndex]?.Color;
set
{
var startColorIndex = StartColorIndex;
Expand All @@ -49,7 +49,7 @@ public Color StartColor

public Color EndColor
{
get => _gradientStops[EndColorIndex].Color;
get => _gradientStops[EndColorIndex]?.Color;
set
{
var endColorIndex = EndColorIndex;
Expand All @@ -66,7 +66,7 @@ public int StartColorIndex

for (var i = 0; i < _gradientStops.Length; i++)
{
if (_gradientStops[i].Offset <= offset)
if (_gradientStops[i] is not null && _gradientStops[i].Offset <= offset)
{
index = i;
offset = _gradientStops[i].Offset;
Expand All @@ -86,7 +86,7 @@ public int EndColorIndex

for (var i = 0; i < _gradientStops.Length; i++)
{
if (_gradientStops[i].Offset >= offset)
if (_gradientStops[i] is not null && _gradientStops[i].Offset >= offset)
{
index = i;
offset = _gradientStops[i].Offset;
Expand All @@ -103,7 +103,7 @@ public override bool IsTransparent
{
foreach (var stop in GradientStops)
{
if (stop.Color != null && stop.Color.Alpha < 1)
if (stop is not null && stop.Color is not null && stop.Color.Alpha < 1)
{
return true;
}
Expand Down

0 comments on commit ce38d97

Please sign in to comment.