From d90276cf4fb20e6252a44b4a1aa0d81e1b970d13 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 4 Oct 2026 12:41:57 +0200 Subject: [PATCH] SkiaSharp images: net10.0, SkiaSharp 4.153.1, run the tests in CI - Retarget both projects to net10.0; SkiaSharp 2.88.8 to 4.153.1. - Rename SkiaSharpLibraryLiveTest to ImageServiceTests so the CI filter on "Live" stops skipping all of its tests, and add SkiaSharp.NativeAssets.Linux to the test project in the same commit, because the SkiaSharp package ships no Linux native binary. - DrawSquareOnImage accepts a square at coordinate 0 (the top-left corner is a valid pixel); a zero or negative size is still rejected. - CreateBlankImage validates its dimensions and throws ArgumentException, so the test of that name asserts the type it names. - Two tests added: a square at the origin, and the layout of a fresh SKBitmap (color type, alpha type, stride, buffer size). - Test packages: Microsoft.NET.Test.Sdk 18.10.1, coverlet.collector 10.1.0, xunit 2.9.3, xunit.runner.visualstudio 4.0.0. --- .../SkiaSharpLibrary/ImageService.cs | 9 ++++-- .../SkiaSharpLibrary/SkiaSharpLibrary.csproj | 4 +-- ...ibraryLiveTest.cs => ImageServiceTests.cs} | 32 +++++++++++++++++-- .../Tests/Tests.csproj | 11 ++++--- 4 files changed, 44 insertions(+), 12 deletions(-) rename csharp-images/GenerateImagesInCSharpUsingSkiaSharp/Tests/{SkiaSharpLibraryLiveTest.cs => ImageServiceTests.cs} (71%) diff --git a/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/SkiaSharpLibrary/ImageService.cs b/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/SkiaSharpLibrary/ImageService.cs index 6e366221c9..f0e341a9d0 100644 --- a/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/SkiaSharpLibrary/ImageService.cs +++ b/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/SkiaSharpLibrary/ImageService.cs @@ -6,6 +6,11 @@ public static class ImageService { public static SKBitmap CreateBlankImage(int width, int height) { + if (width <= 0 || height <= 0) + { + throw new ArgumentException("Image width and height must be greater than zero."); + } + var bitmap = new SKBitmap(width, height); using var canvas = new SKCanvas(bitmap); canvas.Clear(SKColors.White); @@ -15,9 +20,9 @@ public static SKBitmap CreateBlankImage(int width, int height) public static void DrawSquareOnImage(SKBitmap bitmap, int squareSize, int startX, int startY) { - if (squareSize <= 0 || startX <= 0 || startY <= 0) + if (squareSize <= 0 || startX < 0 || startY < 0) { - throw new ArgumentException("Square size and coordinates must be greater than zero."); + throw new ArgumentException("Square size must be greater than zero and coordinates cannot be negative."); } using var canvas = new SKCanvas(bitmap); using var paint = new SKPaint(); diff --git a/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/SkiaSharpLibrary/SkiaSharpLibrary.csproj b/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/SkiaSharpLibrary/SkiaSharpLibrary.csproj index f60da0e05e..7a381816cb 100644 --- a/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/SkiaSharpLibrary/SkiaSharpLibrary.csproj +++ b/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/SkiaSharpLibrary/SkiaSharpLibrary.csproj @@ -2,13 +2,13 @@ Exe - net8.0 + net10.0 enable enable - + diff --git a/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/Tests/SkiaSharpLibraryLiveTest.cs b/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/Tests/ImageServiceTests.cs similarity index 71% rename from csharp-images/GenerateImagesInCSharpUsingSkiaSharp/Tests/SkiaSharpLibraryLiveTest.cs rename to csharp-images/GenerateImagesInCSharpUsingSkiaSharp/Tests/ImageServiceTests.cs index 58d1fa887e..302410db70 100644 --- a/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/Tests/SkiaSharpLibraryLiveTest.cs +++ b/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/Tests/ImageServiceTests.cs @@ -3,7 +3,7 @@ namespace Tests; -public class SkiaSharpLibraryLiveTest : IDisposable +public class ImageServiceTests : IDisposable { private const string OutputPath = @"outputImage.png"; private const string NullOutputPath = ""; @@ -13,7 +13,7 @@ public class SkiaSharpLibraryLiveTest : IDisposable private bool _isDisposed = false; - public SkiaSharpLibraryLiveTest() + public ImageServiceTests() { _bitmap = ImageService.CreateBlankImage(_width, _height); } @@ -34,7 +34,7 @@ public void GivenValidDimensions_WhenCreateBlankImageIsCalled_ThenReturnBitmapWi public void GivenInvalidDimensions_WhenCreateBlankImageIsCalled_ThenThrowArgumentException(int width, int height) { // Act & Assert - Assert.Throws(() => ImageService.CreateBlankImage(width, height)); + Assert.Throws(() => ImageService.CreateBlankImage(width, height)); } [Fact] @@ -50,6 +50,32 @@ public void GivenValidSquareDetails_WhenDrawSquareOnImageIsCalled_ThenSquareIsDr Assert.Contains(_bitmap.Pixels, pixel => pixel != SKColors.White); } + [Fact] + public void GivenSquareAtTopLeftCorner_WhenDrawSquareOnImageIsCalled_ThenSquareIsDrawn() + { + // Act + ImageService.DrawSquareOnImage(_bitmap, 120, 0, 0); + + // Assert + Assert.Equal(SKColors.Red, _bitmap.GetPixel(0, 0)); + } + + [Fact] + public void GivenNewBitmap_WhenItsLayoutIsRead_ThenItMatchesThePlatformColorType() + { + // Arrange + using var bitmap = new SKBitmap(_width, _height); + + // Assert + Assert.Equal(SKImageInfo.PlatformColorType, bitmap.ColorType); + Assert.Equal(SKColorType.Bgra8888, bitmap.ColorType); + Assert.Equal(SKAlphaType.Premul, bitmap.AlphaType); + Assert.Equal(4, bitmap.BytesPerPixel); + Assert.Equal(4 * _width, bitmap.RowBytes); + Assert.Equal(_width * _height, bitmap.Pixels.Length); + Assert.NotEqual(IntPtr.Zero, bitmap.GetPixels()); + } + [Fact] public void GivenInvalidSquareSize_WhenDrawSquareOnImageIsCalled_ThenThrowArgumentException() { diff --git a/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/Tests/Tests.csproj b/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/Tests/Tests.csproj index f065c757c8..dff721e534 100644 --- a/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/Tests/Tests.csproj +++ b/csharp-images/GenerateImagesInCSharpUsingSkiaSharp/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net8.0 + net10.0 enable enable @@ -10,10 +10,11 @@ - - - - + + + + +