ImageTests.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Net.Mime;
  5. using System.Threading;
  6. using FluentAssertions;
  7. using NUnit.Framework;
  8. using QuestPDF.Drawing;
  9. using QuestPDF.Drawing.Exceptions;
  10. using QuestPDF.Elements;
  11. using QuestPDF.Fluent;
  12. using QuestPDF.Helpers;
  13. using QuestPDF.Infrastructure;
  14. using QuestPDF.UnitTests.TestEngine;
  15. using SkiaSharp;
  16. using ImageElement = QuestPDF.Elements.Image;
  17. using DocumentImage = QuestPDF.Infrastructure.Image;
  18. namespace QuestPDF.UnitTests
  19. {
  20. [TestFixture]
  21. public class ImageTests
  22. {
  23. [Test]
  24. public void Measure_TakesMinimalSpaceRegardlessOfSize()
  25. {
  26. TestPlan
  27. .For(x => new ImageElement
  28. {
  29. DocumentImage = GenerateDocumentImage(400, 300)
  30. })
  31. .MeasureElement(new Size(300, 200))
  32. .CheckMeasureResult(SpacePlan.FullRender(0, 0));
  33. }
  34. [Test]
  35. public void Draw_TakesAvailableSpaceRegardlessOfSize()
  36. {
  37. TestPlan
  38. .For(x => new ImageElement
  39. {
  40. CompressionQuality = ImageCompressionQuality.High,
  41. TargetDpi = DocumentSettings.DefaultRasterDpi,
  42. DocumentImage = GenerateDocumentImage(400, 300)
  43. })
  44. .DrawElement(new Size(300, 200))
  45. .ExpectCanvasDrawImage(new Position(0, 0), new Size(300, 200))
  46. .CheckDrawResult();
  47. }
  48. [Test]
  49. public void Fluent_RecognizesImageProportions()
  50. {
  51. var image = GenerateDocumentImage(600, 200);
  52. TestPlan
  53. .For(x =>
  54. {
  55. var container = new Container();
  56. container.Image(image);
  57. return container;
  58. })
  59. .MeasureElement(new Size(300, 200))
  60. .CheckMeasureResult(SpacePlan.FullRender(300, 100));;
  61. }
  62. [Test]
  63. public void ImageObject_ThrowsEncodingException_WhenImageDataIsIncorrect()
  64. {
  65. Func<Infrastructure.Image> action = () => Infrastructure.Image.FromBinaryData(new byte[] { 1, 2, 3 });
  66. action.Should().ThrowExactly<DocumentComposeException>().WithMessage("Cannot decode the provided image.");
  67. }
  68. [Test]
  69. public void ImageObject_ThrowsEncodingException_WhenStreamIsIncorrect()
  70. {
  71. Func<Infrastructure.Image> action = () =>
  72. {
  73. using var stream = new MemoryStream(new byte[] { 1, 2, 3 });
  74. return Infrastructure.Image.FromStream(stream);
  75. };
  76. action.Should().ThrowExactly<DocumentComposeException>().WithMessage("Cannot decode the provided image.");
  77. }
  78. [Test]
  79. public void ImageObject_ThrowsFileNotFoundException_FileIsNotFound()
  80. {
  81. Func<Infrastructure.Image> action = () => Infrastructure.Image.FromFile("non-existing-file.jpg");
  82. action.Should().ThrowExactly<DocumentComposeException>().WithMessage("Cannot load provided image, file not found: *");
  83. }
  84. [Test]
  85. public void UsingSharedImageShouldNotDrasticallyIncreaseDocumentSize()
  86. {
  87. var photo = File.ReadAllBytes("Resources/photo.jpg");
  88. var documentWithSingleImageSize = GetDocumentSize(container =>
  89. {
  90. container.Image(photo);
  91. });
  92. var documentWithMultipleImagesSize = GetDocumentSize(container =>
  93. {
  94. container.Column(column =>
  95. {
  96. foreach (var i in Enumerable.Range(0, 10))
  97. column.Item().Image(photo);
  98. });
  99. });
  100. var documentWithSingleImageUsedMultipleTimesSize = GetDocumentSize(container =>
  101. {
  102. container.Column(column =>
  103. {
  104. var sharedImage = DocumentImage.FromBinaryData(photo);
  105. foreach (var i in Enumerable.Range(0, 10))
  106. column.Item().Image(sharedImage);
  107. });
  108. });
  109. (documentWithMultipleImagesSize / (float)documentWithSingleImageSize).Should().BeInRange(9.9f, 10);
  110. (documentWithSingleImageUsedMultipleTimesSize / (float)documentWithSingleImageSize).Should().BeInRange(1f, 1.05f);
  111. }
  112. [Test]
  113. public void ImageCompressionHasImpactOnDocumentSize()
  114. {
  115. var photo = File.ReadAllBytes("Resources/photo.jpg");
  116. var veryLowCompressionSize = GetDocumentSize(container => container.Image(photo).WithCompressionQuality(ImageCompressionQuality.VeryLow));
  117. var bestCompressionSize = GetDocumentSize(container => container.Image(photo).WithCompressionQuality(ImageCompressionQuality.Best));
  118. (bestCompressionSize / (float)veryLowCompressionSize).Should().BeGreaterThan(10);
  119. }
  120. [Test]
  121. public void TargetDpiHasImpactOnDocumentSize()
  122. {
  123. var photo = File.ReadAllBytes("Resources/photo.jpg");
  124. var lowDpiSize = GetDocumentSize(container => container.Image(photo).WithRasterDpi(12));
  125. var highDpiSize = GetDocumentSize(container => container.Image(photo).WithRasterDpi(144));
  126. (highDpiSize / (float)lowDpiSize).Should().BeGreaterThan(40);
  127. }
  128. private static int GetDocumentSize(Action<IContainer> container)
  129. {
  130. return Document
  131. .Create(document =>
  132. {
  133. document.Page(page =>
  134. {
  135. page.Content().Element(container);
  136. });
  137. })
  138. .GeneratePdf()
  139. .Length;
  140. }
  141. DocumentImage GenerateDocumentImage(int width, int height)
  142. {
  143. var image = Placeholders.Image(width, height);
  144. return DocumentImage.FromBinaryData(image);
  145. }
  146. }
  147. }