Skip to content

Commit

Permalink
Additional image related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinungf committed Dec 10, 2023
2 parents 15b7353 + 16ac8de commit 0e5c77f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
Binary file added SpreadCheetah.Test/Images/red-1x1.invalidpng
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions SpreadCheetah.Test/Tests/SpreadsheetImageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ public async Task Spreadsheet_EmbedImage_PngFromReadOnlyStream()
Assert.Null(exception);
}

[Fact]
public async Task Spreadsheet_EmbedImage_PngWithInvalidResolution()
{
// Arrange
using var pngStream = EmbeddedResources.GetStream("red-1x1.invalidpng");
using var outputStream = new MemoryStream();
await using var spreadsheet = await Spreadsheet.CreateNewAsync(outputStream);

// Act
var exception = await Record.ExceptionAsync(() => spreadsheet.EmbedImageAsync(pngStream).AsTask());

// Assert
var concreteException = Assert.IsType<ArgumentOutOfRangeException>(exception);
Assert.Contains("width", concreteException.Message, StringComparison.OrdinalIgnoreCase);
}

[Fact]
public async Task Spreadsheet_AddImage_Png()
{
Expand Down Expand Up @@ -170,6 +186,37 @@ public async Task Spreadsheet_AddImage_Png()
Assert.Equal("Image 1", picture.Name);
}

[Fact]
public async Task Spreadsheet_AddImage_PngWithTransparentBackground()
{
// Arrange
const string reference = "B3";
using var pngStream = EmbeddedResources.GetStream("yellow-500x500-transparent.png");
using var outputStream = new MemoryStream();
await using var spreadsheet = await Spreadsheet.CreateNewAsync(outputStream);
var embeddedImage = await spreadsheet.EmbedImageAsync(pngStream);
await spreadsheet.StartWorksheetAsync("Sheet 1");
var canvas = ImageCanvas.OriginalSize(reference.AsSpan());

// Act
spreadsheet.AddImage(canvas, embeddedImage);

// Assert
await spreadsheet.FinishAsync();
SpreadsheetAssert.Valid(outputStream);

using var workbook = new XLWorkbook(outputStream);
var worksheet = Assert.Single(workbook.Worksheets);
var picture = Assert.Single(worksheet.Pictures);
Assert.Equal(reference, picture.TopLeftCell.Address.ToString());
Assert.Equal(XLPictureFormat.Png, picture.Format);
Assert.Equal(500, picture.OriginalWidth);
Assert.Equal(500, picture.OriginalHeight);
Assert.Equal(0, picture.Top);
Assert.Equal(0, picture.Left);
Assert.Equal("Image 1", picture.Name);
}

[Fact]
public async Task Spreadsheet_AddImage_PngWithLargeResolution()
{
Expand Down

0 comments on commit 0e5c77f

Please sign in to comment.