VisualTestEngine.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System.Text.RegularExpressions;
  2. using QuestPDF.Fluent;
  3. using QuestPDF.Helpers;
  4. using QuestPDF.Infrastructure;
  5. using SkiaSharp;
  6. namespace QuestPDF.VisualTests;
  7. public static class Helpers
  8. {
  9. public static TOutput Apply<TInput, TOutput>(this TInput input, Func<TInput, TOutput> func)
  10. {
  11. return func(input);
  12. }
  13. }
  14. public static class ImageComparer
  15. {
  16. public static bool AreImagesIdentical(SKBitmap bitmap1, SKBitmap bitmap2)
  17. {
  18. if (bitmap1.Width != bitmap2.Width || bitmap1.Height != bitmap2.Height)
  19. {
  20. Assert.Fail("Different image sizes: " +
  21. $"Image 1: {bitmap1.Width}x{bitmap1.Height}, " +
  22. $"Image 2: {bitmap2.Width}x{bitmap2.Height}");
  23. }
  24. if (bitmap1.ColorType != bitmap2.ColorType)
  25. {
  26. Assert.Fail("Different image color types: " +
  27. $"Image 1: {bitmap1.ColorType}, " +
  28. $"Image 2: {bitmap2.ColorType}");
  29. }
  30. var pixels1 = bitmap1.Pixels;
  31. var pixels2 = bitmap2.Pixels;
  32. if (pixels1.Length != pixels2.Length)
  33. {
  34. Assert.Fail("Different image pixel counts: " +
  35. $"Image 1: {pixels1.Length}, " +
  36. $"Image 2: {pixels2.Length}");
  37. }
  38. var differences = pixels1.Zip(pixels2, (p1, p2) => new[] {p1.Red - p2.Red, p1.Green - p2.Green, p1.Blue - p2.Blue, p1.Alpha - p2.Alpha })
  39. .Select(x => x.Select(Math.Abs))
  40. .Select(x => x.Max())
  41. .Where(diff => diff > 1)
  42. .ToArray();
  43. if (differences.Length > 0)
  44. {
  45. var min = differences.Min();
  46. var max = differences.Max();
  47. var average = differences.Average(x => x);
  48. var message = $"Images differ by {min} (min), {max} (max), {average:F2} (avg). Different pixels: {differences.Length}.";
  49. Assert.Fail(message);
  50. }
  51. for (var i = 0; i < pixels1.Length; i++)
  52. {
  53. if (Math.Abs(pixels1[i].Alpha - pixels2[i].Alpha) > 1)
  54. return false;
  55. if (Math.Abs(pixels1[i].Red - pixels2[i].Red) > 1)
  56. return false;
  57. if (Math.Abs(pixels1[i].Green - pixels2[i].Green) > 1)
  58. return false;
  59. if (Math.Abs(pixels1[i].Blue - pixels2[i].Blue) > 1)
  60. return false;
  61. }
  62. return true;
  63. }
  64. public static bool AreImagesIdentical(byte[] imageData1, byte[] imageData2)
  65. {
  66. using var bitmap1 = SKBitmap.Decode(imageData1);
  67. using var bitmap2 = SKBitmap.Decode(imageData2);
  68. return AreImagesIdentical(bitmap1, bitmap2);
  69. }
  70. }
  71. public static class VisualTestEngine
  72. {
  73. private static string ActualOutputDirectoryName => Path.Combine(TestContext.CurrentContext.TestDirectory, "ActualOutput");
  74. private static string ExpectedOutputDirectoryName => Path.Combine(TestContext.CurrentContext.TestDirectory, "ExpectedOutput");
  75. private static readonly Regex TestNameRegex = new(@"QuestPDF\.VisualTests\.(?<name>.*)Tests");
  76. public static void ClearActualOutputDirectories()
  77. {
  78. if (Directory.Exists(ActualOutputDirectoryName))
  79. Directory.Delete(ActualOutputDirectoryName, true);
  80. }
  81. public static void ShouldMatchExpectedImage(this IDocument document)
  82. {
  83. if (TestContext.CurrentContext.Test.ClassName == null)
  84. throw new Exception("Test class name is not set.");
  85. var match = TestNameRegex.Match(TestContext.CurrentContext.Test.ClassName);
  86. var testCategory = match.Groups["name"].Value;
  87. var imageGenerationSettings = new ImageGenerationSettings
  88. {
  89. ImageFormat = ImageFormat.Png,
  90. RasterDpi = 144
  91. };
  92. var actualImages = document.GenerateImages(imageGenerationSettings).ToList();
  93. var hasMultipleImages = actualImages.Count > 1;
  94. var actualOutputPath = Path.Combine(ActualOutputDirectoryName, testCategory);
  95. var expectedOutputPath = Path.Combine(ExpectedOutputDirectoryName, testCategory);
  96. if (!Directory.Exists(expectedOutputPath))
  97. Assert.Inconclusive("Cannot find the expected output folder");
  98. var testName = TestContext.CurrentContext.Test.Name;
  99. var expectedOutputFileCount = Directory.EnumerateFiles(expectedOutputPath, $"{testName}*.png").Count();
  100. Directory.CreateDirectory(actualOutputPath);
  101. string GetFileName(int index)
  102. {
  103. return hasMultipleImages ? $"{testName}_{index}.png" : $"{testName}.png";
  104. }
  105. foreach (var i in Enumerable.Range(0, actualImages.Count))
  106. {
  107. var actualImagePath = Path.Combine(actualOutputPath, GetFileName(i));
  108. File.WriteAllBytes(actualImagePath, actualImages[i]);
  109. }
  110. if (actualImages.Count != expectedOutputFileCount)
  111. Assert.Fail($"Generated {actualImages.Count} images but expected {expectedOutputFileCount}");
  112. foreach (var i in Enumerable.Range(0, actualImages.Count))
  113. {
  114. var expectedImagePath = Path.Combine(expectedOutputPath, GetFileName(i));
  115. if (!File.Exists(expectedImagePath))
  116. Assert.Fail($"Cannot find expected image file {expectedImagePath}");
  117. var expectedImageBytes = File.ReadAllBytes(expectedImagePath);
  118. var actualImageBytes = actualImages[i];
  119. var imagesAreIdentical = ImageComparer.AreImagesIdentical(actualImageBytes, expectedImageBytes);
  120. if (imagesAreIdentical)
  121. continue;
  122. var pageText = actualImages.Count > 1 ? $" (page {i})" : string.Empty;
  123. Assert.Fail($"Generated image does not match expected image{pageText}.");
  124. }
  125. }
  126. }
  127. public static class VisualTest
  128. {
  129. public static void Perform(Action<IDocumentContainer> documentBuilder)
  130. {
  131. Document
  132. .Create(documentBuilder)
  133. .ShouldMatchExpectedImage();
  134. }
  135. public static void PerformWithDefaultPageSettings(Action<IContainer> contentBuilder)
  136. {
  137. Document
  138. .Create(document =>
  139. {
  140. document.Page(page =>
  141. {
  142. page.MinSize(new PageSize(0, 0));
  143. page.MaxSize(new PageSize(1000, 1000));
  144. page.Margin(20);
  145. page.PageColor(Colors.White);
  146. page.DefaultTextStyle(x => x.FontSize(16));
  147. page.Content().Element(contentBuilder);
  148. });
  149. })
  150. .ShouldMatchExpectedImage();
  151. }
  152. }