VisualTestEngine.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 > 0)
  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 (pixels1[i] != pixels2[i])
  54. return false;
  55. }
  56. return true;
  57. }
  58. public static bool AreImagesIdentical(byte[] imageData1, byte[] imageData2)
  59. {
  60. using var bitmap1 = SKBitmap.Decode(imageData1);
  61. using var bitmap2 = SKBitmap.Decode(imageData2);
  62. return AreImagesIdentical(bitmap1, bitmap2);
  63. }
  64. }
  65. public static class VisualTestEngine
  66. {
  67. private static string ActualOutputDirectoryName => Path.Combine(TestContext.CurrentContext.TestDirectory, "ActualOutput");
  68. private static string ExpectedOutputDirectoryName => Path.Combine(TestContext.CurrentContext.TestDirectory, "ExpectedOutput");
  69. private static readonly Regex TestNameRegex = new(@"QuestPDF\.VisualTests\.(?<name>.*)Tests");
  70. public static void ClearActualOutputDirectories()
  71. {
  72. if (Directory.Exists(ActualOutputDirectoryName))
  73. Directory.Delete(ActualOutputDirectoryName, true);
  74. }
  75. public static void ShouldMatchExpectedImage(this IDocument document)
  76. {
  77. if (TestContext.CurrentContext.Test.ClassName == null)
  78. throw new Exception("Test class name is not set.");
  79. var match = TestNameRegex.Match(TestContext.CurrentContext.Test.ClassName);
  80. var testCategory = match.Groups["name"].Value;
  81. var imageGenerationSettings = new ImageGenerationSettings
  82. {
  83. ImageFormat = ImageFormat.Png,
  84. RasterDpi = 144
  85. };
  86. var actualImages = document.GenerateImages(imageGenerationSettings).ToList();
  87. var hasMultipleImages = actualImages.Count > 1;
  88. var actualOutputPath = Path.Combine(ActualOutputDirectoryName, testCategory);
  89. var expectedOutputPath = Path.Combine(ExpectedOutputDirectoryName, testCategory);
  90. var testName = TestContext.CurrentContext.Test.Name;
  91. Directory.CreateDirectory(actualOutputPath);
  92. string GetFileName(int index)
  93. {
  94. return hasMultipleImages ? $"{testName}_{index}.png" : $"{testName}.png";
  95. }
  96. foreach (var i in Enumerable.Range(0, actualImages.Count))
  97. {
  98. var actualImagePath = Path.Combine(actualOutputPath, GetFileName(i));
  99. File.WriteAllBytes(actualImagePath, actualImages[i]);
  100. }
  101. if (!Directory.Exists(expectedOutputPath))
  102. Assert.Inconclusive("Cannot find the expected output folder");
  103. var expectedOutputFileCount = Directory.EnumerateFiles(expectedOutputPath, $"{testName}*.png").Count();
  104. if (actualImages.Count != expectedOutputFileCount)
  105. Assert.Fail($"Generated {actualImages.Count} images but expected {expectedOutputFileCount}");
  106. foreach (var i in Enumerable.Range(0, actualImages.Count))
  107. {
  108. var expectedImagePath = Path.Combine(expectedOutputPath, GetFileName(i));
  109. if (!File.Exists(expectedImagePath))
  110. Assert.Fail($"Cannot find expected image file {expectedImagePath}");
  111. var expectedImageBytes = File.ReadAllBytes(expectedImagePath);
  112. var actualImageBytes = actualImages[i];
  113. var imagesAreIdentical = ImageComparer.AreImagesIdentical(actualImageBytes, expectedImageBytes);
  114. if (imagesAreIdentical)
  115. continue;
  116. var pageText = actualImages.Count > 1 ? $" (page {i})" : string.Empty;
  117. Assert.Fail($"Generated image does not match expected image{pageText}.");
  118. }
  119. }
  120. }
  121. public static class VisualTest
  122. {
  123. public static void Perform(Action<IDocumentContainer> documentBuilder)
  124. {
  125. Document
  126. .Create(documentBuilder)
  127. .ShouldMatchExpectedImage();
  128. }
  129. public static void PerformWithDefaultPageSettings(Action<IContainer> contentBuilder)
  130. {
  131. Document
  132. .Create(document =>
  133. {
  134. document.Page(page =>
  135. {
  136. page.MinSize(new PageSize(0, 0));
  137. page.MaxSize(new PageSize(1000, 1000));
  138. page.Margin(20);
  139. page.PageColor(Colors.White);
  140. page.DefaultTextStyle(x => x.FontSize(16));
  141. page.Content().Element(contentBuilder);
  142. });
  143. })
  144. .ShouldMatchExpectedImage();
  145. }
  146. }