Added tests for new image functionality

This commit is contained in:
MarcinZiabek
2023-05-15 00:41:14 +02:00
parent fc8a788cf7
commit f1ff03fede
6 changed files with 59 additions and 16 deletions
+41
View File
@@ -5,6 +5,7 @@ using QuestPDF.Drawing.Exceptions;
using QuestPDF.Examples.Engine;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
namespace QuestPDF.Examples
{
@@ -68,6 +69,46 @@ namespace QuestPDF.Examples
});
}
[Test]
public void DpiSetting()
{
RenderingTest
.Create()
.PageSize(400, 600)
.ProduceImages()
.ShowResults()
.Render(page =>
{
page.Padding(10).Column(column =>
{
column.Spacing(10);
column.Item().Image("photo.jpg").WithRasterDpi(16);
column.Item().Image("photo.jpg").WithRasterDpi(72);
});
});
}
[Test]
public void CompressionSetting()
{
RenderingTest
.Create()
.PageSize(400, 600)
.ProduceImages()
.ShowResults()
.Render(page =>
{
page.Padding(10).Column(column =>
{
column.Spacing(10);
column.Item().Image("photo.jpg").WithCompressionQuality(ImageCompressionQuality.VeryLow);
column.Item().Image("photo.jpg").WithCompressionQuality(ImageCompressionQuality.High);
});
});
}
[Test]
public void Exception()
{
@@ -35,6 +35,9 @@
<None Update="multilingual.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="photo.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

+9 -16
View File
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Mime;
using FluentAssertions;
@@ -62,19 +63,19 @@ namespace QuestPDF.UnitTests
[Test]
public void UsingSharedImageShouldNotDrasticallyIncreaseDocumentSize()
{
var placeholderImage = Placeholders.Image(1000, 200);
var photo = File.ReadAllBytes("Resources/photo.jpg");
var documentWithSingleImageSize = GetDocumentSize(container =>
{
container.Image(placeholderImage);
container.Image(photo);
});
var documentWithMultipleImagesSize = GetDocumentSize(container =>
{
container.Column(column =>
{
foreach (var i in Enumerable.Range(0, 100))
column.Item().Image(placeholderImage);
foreach (var i in Enumerable.Range(0, 10))
column.Item().Image(photo);
});
});
@@ -82,23 +83,15 @@ namespace QuestPDF.UnitTests
{
container.Column(column =>
{
var sharedImage = DocumentImage.FromBinaryData(placeholderImage).DisposeAfterDocumentGeneration();
var sharedImage = DocumentImage.FromBinaryData(photo).DisposeAfterDocumentGeneration();
foreach (var i in Enumerable.Range(0, 100))
foreach (var i in Enumerable.Range(0, 10))
column.Item().Image(sharedImage);
});
});
(documentWithMultipleImagesSize / (float)documentWithSingleImageSize).Should().BeInRange(90, 100);
(documentWithSingleImageUsedMultipleTimesSize / (float)documentWithSingleImageSize).Should().BeInRange(1f, 1.5f);
}
[Test]
public void ImageShouldNotBeScaledAboveItsNativeResolution()
{
var image = Placeholders.Image(200, 200);
// TODO
(documentWithMultipleImagesSize / (float)documentWithSingleImageSize).Should().BeInRange(9.9f, 10);
(documentWithSingleImageUsedMultipleTimesSize / (float)documentWithSingleImageSize).Should().BeInRange(1f, 1.05f);
}
private static int GetDocumentSize(Action<IContainer> container)
@@ -27,4 +27,10 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Update="Resources\photo.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB