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. }
  29. public static RenderingTest Create([CallerMemberName] string fileName = "test")
  30. {
  31. return new RenderingTest().FileName(fileName);
  32. }
  33. public RenderingTest FileName(string fileName)
  34. {
  35. FileNamePrefix = fileName;
  36. return this;
  37. }
  38. public RenderingTest PageSize(Size size)
  39. {
  40. Size = size;
  41. return this;
  42. }
  43. public RenderingTest PageSize(int width, int height)
  44. {
  45. return PageSize(new Size(width, height));
  46. }
  47. public RenderingTest ProducePdf()
  48. {
  49. ResultType = RenderingTestResult.Pdf;
  50. return this;
  51. }
  52. public RenderingTest ProduceImages()
  53. {
  54. ResultType = RenderingTestResult.Images;
  55. return this;
  56. }
  57. public RenderingTest ShowResults()
  58. {
  59. ShowResult = true;
  60. return this;
  61. }
  62. public RenderingTest MaxPages(int value)
  63. {
  64. MaxPagesThreshold = value;
  65. return this;
  66. }
  67. public RenderingTest EnableCaching(bool value = true)
  68. {
  69. ApplyCaching = value;
  70. return this;
  71. }
  72. public RenderingTest EnableDebugging(bool value = true)
  73. {
  74. ApplyDebugging = value;
  75. return this;
  76. }
  77. public void Render(Action<IContainer> content)
  78. {
  79. RenderDocument(container =>
  80. {
  81. container.Page(page =>
  82. {
  83. page.Size(new PageSize(Size.Width, Size.Height));
  84. page.Content().Container().Background(Colors.White).Element(content);
  85. });
  86. });
  87. }
  88. public void RenderDocument(Action<IDocumentContainer> content)
  89. {
  90. var document = new SimpleDocument(content, ApplyCaching, ApplyDebugging);
  91. Render(document);
  92. }
  93. public void Render(IDocument document)
  94. {
  95. if (ResultType == RenderingTestResult.Images)
  96. {
  97. var generationSettings = new ImageGenerationSettings { RasterDpi = 144 };
  98. Func<int, string> fileNameSchema = i => $"{FileNamePrefix}-${i}.png";
  99. document.GenerateImages(index => fileNameSchema(index), generationSettings);
  100. if (ShowResult && ShowingResultsEnabled)
  101. {
  102. var firstImagePath = fileNameSchema(0);
  103. OpenFileUsingDefaultProgram(firstImagePath);
  104. }
  105. }
  106. if (ResultType == RenderingTestResult.Pdf)
  107. {
  108. if (ShowResult && ShowingResultsEnabled)
  109. document.GeneratePdfAndShow();
  110. else
  111. document.GeneratePdf();
  112. }
  113. }
  114. static void OpenFileUsingDefaultProgram(string filePath)
  115. {
  116. var process = new Process
  117. {
  118. StartInfo = new ProcessStartInfo(filePath)
  119. {
  120. UseShellExecute = true
  121. }
  122. };
  123. process.Start();
  124. process.WaitForExit();
  125. }
  126. }
  127. }