LayoutTest.cs 1.5 KB

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