RenderingTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.CompilerServices;
  4. using NUnit.Framework;
  5. using QuestPDF.Elements;
  6. using QuestPDF.Fluent;
  7. using QuestPDF.Helpers;
  8. using QuestPDF.Infrastructure;
  9. namespace QuestPDF.Examples.Engine
  10. {
  11. public enum RenderingTestResult
  12. {
  13. Pdf,
  14. Images
  15. }
  16. public class RenderingTest
  17. {
  18. private string FileNamePrefix = "test";
  19. private Size Size { get; set; }
  20. private int? MaxPagesThreshold { get; set; }
  21. private bool ShowResult { get; set; }
  22. private bool ApplyCaching { get; set; }
  23. private bool ApplyDebugging { get; set; }
  24. private RenderingTestResult ResultType { get; set; } = RenderingTestResult.Images;
  25. private static readonly bool ShowingResultsEnabled = (Environment.GetEnvironmentVariable("TEST_SHOW_RESULTS") ?? "true") == "true";
  26. private RenderingTest()
  27. {
  28. Console.WriteLine("Showing results: " + ShowingResultsEnabled);
  29. }
  30. public static RenderingTest Create([CallerMemberName] string fileName = "test")
  31. {
  32. return new RenderingTest().FileName(fileName);
  33. }
  34. public RenderingTest FileName(string fileName)
  35. {
  36. FileNamePrefix = fileName;
  37. return this;
  38. }
  39. public RenderingTest PageSize(Size size)
  40. {
  41. Size = size;
  42. return this;
  43. }
  44. public RenderingTest PageSize(int width, int height)
  45. {
  46. return PageSize(new Size(width, height));
  47. }
  48. public RenderingTest ProducePdf()
  49. {
  50. ResultType = RenderingTestResult.Pdf;
  51. return this;
  52. }
  53. public RenderingTest ProduceImages()
  54. {
  55. ResultType = RenderingTestResult.Images;
  56. return this;
  57. }
  58. public RenderingTest ShowResults()
  59. {
  60. ShowResult = true;
  61. return this;
  62. }
  63. public RenderingTest MaxPages(int value)
  64. {
  65. MaxPagesThreshold = value;
  66. return this;
  67. }
  68. public RenderingTest EnableCaching(bool value = true)
  69. {
  70. ApplyCaching = value;
  71. return this;
  72. }
  73. public RenderingTest EnableDebugging(bool value = true)
  74. {
  75. ApplyDebugging = value;
  76. return this;
  77. }
  78. public void Render(Action<IContainer> content)
  79. {
  80. RenderDocument(container =>
  81. {
  82. container.Page(page =>
  83. {
  84. page.Size(new PageSize(Size.Width, Size.Height));
  85. page.Content().Container().Background(Colors.White).Element(content);
  86. });
  87. });
  88. }
  89. public void RenderDocument(Action<IDocumentContainer> content)
  90. {
  91. var document = new SimpleDocument(content, ApplyCaching, ApplyDebugging);
  92. Render(document);
  93. }
  94. public void Render(IDocument document)
  95. {
  96. if (ResultType == RenderingTestResult.Images)
  97. {
  98. var generationSettings = new ImageGenerationSettings { RasterDpi = 144 };
  99. Func<int, string> fileNameSchema = i => $"{FileNamePrefix}-${i}.png";
  100. document.GenerateImages(index => fileNameSchema(index), generationSettings);
  101. if (ShowResult && ShowingResultsEnabled)
  102. {
  103. var firstImagePath = fileNameSchema(0);
  104. OpenFileUsingDefaultProgram(firstImagePath);
  105. }
  106. }
  107. if (ResultType == RenderingTestResult.Pdf)
  108. {
  109. if (ShowResult && ShowingResultsEnabled)
  110. document.GeneratePdfAndShow();
  111. else
  112. document.GeneratePdf();
  113. }
  114. }
  115. static void OpenFileUsingDefaultProgram(string filePath)
  116. {
  117. var process = new Process
  118. {
  119. StartInfo = new ProcessStartInfo(filePath)
  120. {
  121. UseShellExecute = true
  122. }
  123. };
  124. process.Start();
  125. process.WaitForExit();
  126. }
  127. }
  128. }