LayoutTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 class LayoutBuilderDescriptor
  11. {
  12. public void Compose(Action<IContainer> container)
  13. {
  14. }
  15. }
  16. internal class DocumentLayoutBuilder
  17. {
  18. public List<PageDrawingCommand> Commands { get; } = new();
  19. public PageLayoutDescriptor Page()
  20. {
  21. var page = new PageDrawingCommand();
  22. Commands.Add(page);
  23. return new PageLayoutDescriptor(page);
  24. }
  25. }
  26. internal class PageLayoutDescriptor
  27. {
  28. private PageDrawingCommand Command { get; }
  29. public PageLayoutDescriptor(PageDrawingCommand command)
  30. {
  31. Command = command;
  32. }
  33. public PageLayoutDescriptor TakenAreaSize(float width, float height)
  34. {
  35. Command.RequiredArea = new Size(width, height);
  36. return this;
  37. }
  38. public PageLayoutDescriptor Content(Action<PageLayoutBuilder> content)
  39. {
  40. var pageContent = new PageLayoutBuilder();
  41. content(pageContent);
  42. Command.Children = pageContent.Commands;
  43. return this;
  44. }
  45. }
  46. internal class PageLayoutBuilder
  47. {
  48. public List<ChildDrawingCommand> Commands { get;} = new();
  49. public ChildLayoutDescriptor Child(string childId)
  50. {
  51. var child = new ChildDrawingCommand { ChildId = childId };
  52. Commands.Add(child);
  53. return new ChildLayoutDescriptor(child);
  54. }
  55. }
  56. internal class ChildLayoutDescriptor
  57. {
  58. private ChildDrawingCommand Command { get; }
  59. public ChildLayoutDescriptor(ChildDrawingCommand command)
  60. {
  61. Command = command;
  62. }
  63. public ChildLayoutDescriptor Position(float x, float y)
  64. {
  65. Command.Position = new Position(x, y);
  66. return this;
  67. }
  68. public ChildLayoutDescriptor Size(float width, float height)
  69. {
  70. Command.Size = new Size(width, height);
  71. return this;
  72. }
  73. }
  74. internal class ExpectedLayoutChildPosition
  75. {
  76. public string ElementId { get; set; }
  77. public int PageNumber { get; set; }
  78. public int DepthIndex { get; set; }
  79. public Position Position { get; set; }
  80. public Size Size { get; set; }
  81. }
  82. public static class ElementExtensions
  83. {
  84. public static void Mock(this IContainer element, string id, float width, float height)
  85. {
  86. var mock = new MockChild
  87. {
  88. Id = id,
  89. TotalWidth = width,
  90. TotalHeight = height
  91. };
  92. element.Element(mock);
  93. }
  94. }
  95. internal class LayoutTest
  96. {
  97. private const string DocumentColor = Colors.Grey.Lighten1;
  98. private const string PageColor = Colors.Grey.Lighten3;
  99. private const string TargetColor = Colors.White;
  100. public Size PageSize { get; set; }
  101. public ICollection<PageDrawingCommand> ActualCommands { get; set; }
  102. public ICollection<PageDrawingCommand> ExpectedCommands { get; set; }
  103. public static LayoutTest HavingSpaceOfSize(float width, float height)
  104. {
  105. return new LayoutTest
  106. {
  107. PageSize = new Size(width, height)
  108. };
  109. }
  110. public LayoutTest WithContent(Action<IContainer> handler)
  111. {
  112. // compose content
  113. var container = new Container();
  114. container.Element(handler);
  115. ActualCommands = GenerateResult(PageSize, container);
  116. return this;
  117. }
  118. private static ICollection<PageDrawingCommand> GenerateResult(Size pageSize, Container container)
  119. {
  120. // inject dependencies
  121. var pageContext = new PageContext();
  122. pageContext.ResetPageNumber();
  123. var canvas = new PreviewerCanvas();
  124. container.InjectDependencies(pageContext, canvas);
  125. // distribute global state
  126. container.ApplyInheritedAndGlobalTexStyle(TextStyle.Default);
  127. container.ApplyContentDirection(ContentDirection.LeftToRight);
  128. container.ApplyDefaultImageConfiguration(DocumentSettings.Default.ImageRasterDpi, DocumentSettings.Default.ImageCompressionQuality, true);
  129. // render
  130. container.VisitChildren(x => (x as IStateResettable)?.ResetState());
  131. canvas.BeginDocument();
  132. var pageSizes = new List<Size>();
  133. while(true)
  134. {
  135. var spacePlan = container.Measure(pageSize);
  136. pageSizes.Add(spacePlan);
  137. if (spacePlan.Type == SpacePlanType.Wrap)
  138. {
  139. throw new Exception();
  140. }
  141. try
  142. {
  143. canvas.BeginPage(pageSize);
  144. container.Draw(pageSize);
  145. pageContext.IncrementPageNumber();
  146. }
  147. catch (Exception exception)
  148. {
  149. canvas.EndDocument();
  150. throw new Exception();
  151. }
  152. canvas.EndPage();
  153. if (spacePlan.Type == SpacePlanType.FullRender)
  154. break;
  155. }
  156. // extract results
  157. var mocks = container.ExtractElementsOfType<MockChild>().Select(x => x.Value); // mock cannot contain another mock, flat structure
  158. return mocks
  159. .SelectMany(x => x.DrawingCommands)
  160. .GroupBy(x => x.PageNumber)
  161. .Select(x => new PageDrawingCommand
  162. {
  163. RequiredArea = pageSizes[x.Key - 1],
  164. Children = x
  165. .Select(y => new ChildDrawingCommand
  166. {
  167. ChildId = y.ChildId,
  168. Size = y.Size,
  169. Position = y.Position
  170. })
  171. .ToList()
  172. })
  173. .ToList();
  174. }
  175. public void ExpectWrap()
  176. {
  177. }
  178. public LayoutTest ExpectedDrawResult(Action<DocumentLayoutBuilder> handler)
  179. {
  180. var builder = new DocumentLayoutBuilder();
  181. handler(builder);
  182. ExpectedCommands = builder.Commands;
  183. return this;
  184. }
  185. public void CompareVisually()
  186. {
  187. VisualizeExpectedResult(PageSize, ActualCommands, ExpectedCommands);
  188. }
  189. public void Validate()
  190. {
  191. if (ActualCommands.Count != ExpectedCommands.Count)
  192. throw new Exception($"Generated {ActualCommands.Count} but expected {ExpectedCommands.Count} pages.");
  193. var numberOfPages = ActualCommands.Count;
  194. foreach (var i in Enumerable.Range(0, numberOfPages))
  195. {
  196. try
  197. {
  198. var actual = ActualCommands.ElementAt(i);
  199. var expected = ExpectedCommands.ElementAt(i);
  200. if (Math.Abs(actual.RequiredArea.Width - expected.RequiredArea.Width) > Size.Epsilon)
  201. throw new Exception($"Taken area width is equal to {actual.RequiredArea.Width} but expected {expected.RequiredArea.Width}");
  202. if (Math.Abs(actual.RequiredArea.Height - expected.RequiredArea.Height) > Size.Epsilon)
  203. throw new Exception($"Taken area height is equal to {actual.RequiredArea.Height} but expected {expected.RequiredArea.Height}");
  204. if (actual.Children.Count != expected.Children.Count)
  205. throw new Exception($"Visible {actual.Children.Count} but expected {expected.Children.Count}");
  206. foreach (var child in expected.Children)
  207. {
  208. var matchingActualElements = actual
  209. .Children
  210. .Where(x => x.ChildId == child.ChildId)
  211. .Where(x => Position.Equal(x.Position, child.Position))
  212. .Where(x => Size.Equal(x.Size, child.Size))
  213. .Count();
  214. if (matchingActualElements == 0)
  215. throw new Exception($"Cannot find actual drawing command for child {child.ChildId} on position {child.Position} and size {child.Size}");
  216. if (matchingActualElements > 1)
  217. throw new Exception($"Found multiple drawing commands for child {child.ChildId} on position {child.Position} and size {child.Size}");
  218. }
  219. // todo: add z-depth testing
  220. var actualOverlaps = GetOverlappingItems(actual.Children);
  221. var expectedOverlaps = GetOverlappingItems(expected.Children);
  222. foreach (var overlap in expectedOverlaps)
  223. {
  224. var matchingActualElements = actualOverlaps.Count(x => x.Item1 == overlap.Item1 && x.Item2 == overlap.Item2);
  225. if (matchingActualElements != 1)
  226. throw new Exception($"Element {overlap.Item1} should be visible underneath element {overlap.Item2}");
  227. }
  228. IEnumerable<(string, string)> GetOverlappingItems(ICollection<ChildDrawingCommand> items)
  229. {
  230. for (var i = 0; i < items.Count; i++)
  231. {
  232. for (var j = i; j < items.Count; j++)
  233. {
  234. var beforeChild = items.ElementAt(i);
  235. var afterChild = items.ElementAt(j);
  236. var beforeBoundingBox = BoundingBox.From(beforeChild.Position, beforeChild.Size);
  237. var afterBoundingBox = BoundingBox.From(afterChild.Position, afterChild.Size);
  238. var intersection = BoundingBoxExtensions.Intersection(beforeBoundingBox, afterBoundingBox);
  239. if (intersection == null)
  240. continue;
  241. yield return (beforeChild.ChildId, afterChild.ChildId);
  242. }
  243. }
  244. }
  245. }
  246. catch (Exception e)
  247. {
  248. throw new Exception($"Error on page {i + 1}: {e.Message}");
  249. }
  250. }
  251. }
  252. private static void VisualizeExpectedResult(Size pageSize, ICollection<PageDrawingCommand> left, ICollection<PageDrawingCommand> right)
  253. {
  254. var path = "test.pdf";
  255. if (File.Exists(path))
  256. File.Delete(path);
  257. // default colors
  258. var defaultColors = new string[]
  259. {
  260. Colors.Red.Medium,
  261. Colors.Green.Medium,
  262. Colors.Blue.Medium,
  263. Colors.Pink.Medium,
  264. Colors.Orange.Medium,
  265. Colors.Lime.Medium,
  266. Colors.Cyan.Medium,
  267. Colors.Indigo.Medium
  268. };
  269. // determine children colors
  270. var children = Enumerable
  271. .Concat(left, right)
  272. .SelectMany(x => x.Children)
  273. .Select(x => x.ChildId)
  274. .Distinct()
  275. .ToList();
  276. var colors = Enumerable
  277. .Range(0, children.Count)
  278. .ToDictionary(i => children[i], i => defaultColors[i]);
  279. // create new pdf document output
  280. var matrixHeight = Math.Max(left.Count, right.Count);
  281. const int pagePadding = 25;
  282. var imageInfo = new SKImageInfo((int)pageSize.Width * 2 + pagePadding * 4, (int)(pageSize.Height * matrixHeight + pagePadding * (matrixHeight + 2)));
  283. using var pdf = SKDocument.CreatePdf(path);
  284. using var canvas = pdf.BeginPage(imageInfo.Width, imageInfo.Height);
  285. // page background
  286. canvas.Clear(SKColor.Parse(DocumentColor));
  287. // chain titles
  288. // available area
  289. using var titlePaint = TextStyle.LibraryDefault.FontSize(16).Bold().ToPaint().Clone();
  290. titlePaint.TextAlign = SKTextAlign.Center;
  291. canvas.Save();
  292. canvas.Translate(pagePadding + pageSize.Width / 2f, pagePadding + titlePaint.TextSize / 2);
  293. canvas.DrawText("RESULT", 0, 0, titlePaint);
  294. canvas.Translate(pagePadding * 2 + pageSize.Width, 0);
  295. canvas.DrawText("EXPECTED", 0, 0, titlePaint);
  296. canvas.Restore();
  297. // side visualization
  298. canvas.Save();
  299. canvas.Translate(pagePadding, pagePadding * 2);
  300. DrawSide(left);
  301. canvas.Translate(pageSize.Width + pagePadding * 2, 0);
  302. DrawSide(right);
  303. canvas.Restore();
  304. // draw page numbers
  305. canvas.Save();
  306. canvas.Translate(pagePadding * 2 + pageSize.Width, pagePadding * 2 + titlePaint.TextSize);
  307. foreach (var i in Enumerable.Range(0, matrixHeight))
  308. {
  309. canvas.DrawText((i + 1).ToString(), 0, 0, titlePaint);
  310. canvas.Translate(0, pagePadding + pageSize.Height);
  311. }
  312. canvas.Restore();
  313. pdf.EndPage();
  314. pdf.Close();
  315. void DrawSide(ICollection<PageDrawingCommand> commands)
  316. {
  317. canvas.Save();
  318. foreach (var pageDrawingCommand in commands)
  319. {
  320. DrawPage(pageDrawingCommand);
  321. canvas.Translate(0, pageSize.Height + pagePadding);
  322. }
  323. canvas.Restore();
  324. }
  325. void DrawPage(PageDrawingCommand command)
  326. {
  327. // available area
  328. using var availableAreaPaint = new SKPaint
  329. {
  330. Color = SKColor.Parse(PageColor)
  331. };
  332. canvas.DrawRect(0, 0, pageSize.Width, pageSize.Height, availableAreaPaint);
  333. // taken area
  334. using var takenAreaPaint = new SKPaint
  335. {
  336. Color = SKColor.Parse(TargetColor)
  337. };
  338. canvas.DrawRect(0, 0, command.RequiredArea.Width, command.RequiredArea.Height, takenAreaPaint);
  339. // draw children
  340. foreach (var child in command.Children)
  341. {
  342. canvas.Save();
  343. var color = colors[child.ChildId];
  344. using var childBorderPaint = new SKPaint
  345. {
  346. Color = SKColor.Parse(color),
  347. IsStroke = true,
  348. StrokeWidth = 2
  349. };
  350. using var childAreaPaint = new SKPaint
  351. {
  352. Color = SKColor.Parse(color).WithAlpha(128)
  353. };
  354. canvas.Translate(child.Position.X, child.Position.Y);
  355. canvas.DrawRect(0, 0, child.Size.Width, child.Size.Height, childAreaPaint);
  356. canvas.DrawRect(0, 0, child.Size.Width, child.Size.Height, childBorderPaint);
  357. canvas.Restore();
  358. }
  359. }
  360. // save
  361. GenerateExtensions.OpenFileUsingDefaultProgram(path);
  362. }
  363. }