LayoutTest.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Collections;
  2. using QuestPDF.Drawing;
  3. using QuestPDF.Drawing.Proxy;
  4. using QuestPDF.Elements;
  5. using QuestPDF.Fluent;
  6. using QuestPDF.Helpers;
  7. using QuestPDF.Infrastructure;
  8. using SkiaSharp;
  9. namespace QuestPDF.LayoutTests.TestEngine;
  10. internal sealed class LayoutTest
  11. {
  12. private LayoutTestResult TestResult { get; } = new LayoutTestResult();
  13. public static LayoutTest HavingSpaceOfSize(float width, float height)
  14. {
  15. var result = new LayoutTest();
  16. result.TestResult.PageSize = new Size(width, height);
  17. return result;
  18. }
  19. public LayoutTest WithContent(Action<IContainer> handler)
  20. {
  21. // compose content
  22. var container = new Container();
  23. container.Element(handler);
  24. TestResult.ActualLayout = LayoutTestExecutor.Execute(TestResult.PageSize, container);
  25. return this;
  26. }
  27. public void ExpectWrap()
  28. {
  29. }
  30. public LayoutTest ExpectedDrawResult(Action<ExpectedDocumentLayoutDescriptor> handler)
  31. {
  32. var builder = new ExpectedDocumentLayoutDescriptor();
  33. handler(builder);
  34. TestResult.ExpectedLayout = builder.PageLayouts;
  35. return this;
  36. }
  37. public void CompareVisually()
  38. {
  39. var path = "output.pdf";
  40. if (File.Exists(path))
  41. File.Delete(path);
  42. var stream = new FileStream(path, FileMode.CreateNew);
  43. LayoutTestResultVisualization.Visualize(TestResult, stream);
  44. stream.Dispose();
  45. GenerateExtensions.OpenFileUsingDefaultProgram(path);
  46. }
  47. public void Validate()
  48. {
  49. LayoutTestValidator.Validate(TestResult);
  50. }
  51. }