TestPlan.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.Json;
  4. using FluentAssertions;
  5. using NUnit.Framework;
  6. using QuestPDF.Drawing;
  7. using QuestPDF.Drawing.SpacePlan;
  8. using QuestPDF.Elements;
  9. using QuestPDF.Helpers;
  10. using QuestPDF.Infrastructure;
  11. using QuestPDF.UnitTests.TestEngine.Operations;
  12. namespace QuestPDF.UnitTests.TestEngine
  13. {
  14. internal class TestPlan
  15. {
  16. private const string DefaultChildName = "child";
  17. private static Random Random { get; } = new Random();
  18. private Element Element { get; set; }
  19. private ICanvas Canvas { get; }
  20. private Size OperationInput { get; set; }
  21. private Queue<OperationBase> Operations { get; } = new Queue<OperationBase>();
  22. public TestPlan()
  23. {
  24. Canvas = CreateCanvas();
  25. }
  26. public static TestPlan For(Func<TestPlan, Element> create)
  27. {
  28. var plan = new TestPlan();
  29. plan.Element = create(plan);
  30. return plan;
  31. }
  32. private T GetExpected<T>() where T : OperationBase
  33. {
  34. if (Operations.TryDequeue(out var value) && value is T result)
  35. return result;
  36. var gotType = value?.GetType()?.Name ?? "null";
  37. Assert.Fail($"Expected: {typeof(T).Name}, got {gotType}: {JsonSerializer.Serialize(value)}");
  38. return null;
  39. }
  40. private ICanvas CreateCanvas()
  41. {
  42. return new MockCanvas
  43. {
  44. TranslateFunc = position =>
  45. {
  46. var expected = GetExpected<CanvasTranslateOperation>();
  47. Assert.AreEqual(expected.Position.X, position.X, "Translate X");
  48. Assert.AreEqual(expected.Position.Y, position.Y, "Translate Y");
  49. },
  50. RotateFunc = angle =>
  51. {
  52. var expected = GetExpected<CanvasRotateOperation>();
  53. Assert.AreEqual(expected.Angle, angle, "Rotate angle");
  54. },
  55. ScaleFunc = (scaleX, scaleY) =>
  56. {
  57. var expected = GetExpected<CanvasScaleOperation>();
  58. Assert.AreEqual(expected.ScaleX, scaleX, "Scale X");
  59. Assert.AreEqual(expected.ScaleY, scaleY, "Scale Y");
  60. },
  61. DrawRectFunc = (position, size, color) =>
  62. {
  63. var expected = GetExpected<CanvasDrawRectangleOperation>();
  64. Assert.AreEqual(expected.Position.X, position.X, "Draw rectangle: X");
  65. Assert.AreEqual(expected.Position.Y, position.Y, "Draw rectangle: Y");
  66. Assert.AreEqual(expected.Size.Width, size.Width, "Draw rectangle: width");
  67. Assert.AreEqual(expected.Size.Height, size.Height, "Draw rectangle: height");
  68. Assert.AreEqual(expected.Color, color, "Draw rectangle: color");
  69. },
  70. DrawTextFunc = (text, position, style) =>
  71. {
  72. var expected = GetExpected<CanvasDrawTextOperation>();
  73. Assert.AreEqual(expected.Text, text);
  74. Assert.AreEqual(expected.Position.X, position.X, "Draw text: X");
  75. Assert.AreEqual(expected.Position.Y, position.Y, "Draw text: Y");
  76. Assert.AreEqual(expected.Style.Color, style.Color, "Draw text: color");
  77. Assert.AreEqual(expected.Style.FontType, style.FontType, "Draw text: font");
  78. Assert.AreEqual(expected.Style.Size, style.Size, "Draw text: size");
  79. },
  80. DrawImageFunc = (image, position, size) =>
  81. {
  82. var expected = GetExpected<CanvasDrawImageOperation>();
  83. Assert.AreEqual(expected.Position.X, position.X, "Draw image: X");
  84. Assert.AreEqual(expected.Position.Y, position.Y, "Draw image: Y");
  85. Assert.AreEqual(expected.Size.Width, size.Width, "Draw image: width");
  86. Assert.AreEqual(expected.Size.Height, size.Height, "Draw image: height");
  87. }
  88. };
  89. }
  90. public Element CreateChild() => CreateChild(DefaultChildName);
  91. public Element CreateChild(string id)
  92. {
  93. return new ElementMock
  94. {
  95. Id = id,
  96. MeasureFunc = availableSpace =>
  97. {
  98. var expected = GetExpected<ChildMeasureOperation>();
  99. Assert.AreEqual(expected.ChildId, id);
  100. Assert.AreEqual(expected.Input.Width, availableSpace.Width, $"Measure: width of child '{expected.ChildId}'");
  101. Assert.AreEqual(expected.Input.Height, availableSpace.Height, $"Measure: height of child '{expected.ChildId}'");
  102. return expected.Output;
  103. },
  104. DrawFunc = availableSpace =>
  105. {
  106. var expected = GetExpected<ChildDrawOperation>();
  107. Assert.AreEqual(expected.ChildId, id);
  108. Assert.AreEqual(expected.Input.Width, availableSpace.Width, $"Draw: width of child '{expected.ChildId}'");
  109. Assert.AreEqual(expected.Input.Height, availableSpace.Height, $"Draw: width of child '{expected.ChildId}'");
  110. }
  111. };
  112. }
  113. public TestPlan MeasureElement(Size input)
  114. {
  115. OperationInput = input;
  116. return this;
  117. }
  118. public TestPlan DrawElement(Size input)
  119. {
  120. OperationInput = input;
  121. return this;
  122. }
  123. private TestPlan AddOperation(OperationBase operationBase)
  124. {
  125. Operations.Enqueue(operationBase);
  126. return this;
  127. }
  128. public TestPlan ExpectChildMeasure(Size expectedInput, ISpacePlan returns)
  129. {
  130. return ExpectChildMeasure(DefaultChildName, expectedInput, returns);
  131. }
  132. public TestPlan ExpectChildMeasure(string child, Size expectedInput, ISpacePlan returns)
  133. {
  134. return AddOperation(new ChildMeasureOperation(child, expectedInput, returns));
  135. }
  136. public TestPlan ExpectChildDraw(Size expectedInput)
  137. {
  138. return ExpectChildDraw(DefaultChildName, expectedInput);
  139. }
  140. public TestPlan ExpectChildDraw(string child, Size expectedInput)
  141. {
  142. return AddOperation(new ChildDrawOperation(child, expectedInput));
  143. }
  144. public TestPlan ExpectCanvasTranslate(Position position)
  145. {
  146. return AddOperation(new CanvasTranslateOperation(position));
  147. }
  148. public TestPlan ExpectCanvasTranslate(float left, float top)
  149. {
  150. return AddOperation(new CanvasTranslateOperation(new Position(left, top)));
  151. }
  152. public TestPlan ExpectCanvasScale(float scaleX, float scaleY)
  153. {
  154. return AddOperation(new CanvasScaleOperation(scaleX, scaleY));
  155. }
  156. public TestPlan ExpectCanvasRotate(float angle)
  157. {
  158. return AddOperation(new CanvasRotateOperation(angle));
  159. }
  160. public TestPlan ExpectCanvasDrawRectangle(Position position, Size size, string color)
  161. {
  162. return AddOperation(new CanvasDrawRectangleOperation(position, size, color));
  163. }
  164. public TestPlan ExpectCanvasDrawText(string text, Position position, TextStyle style)
  165. {
  166. return AddOperation(new CanvasDrawTextOperation(text, position, style));
  167. }
  168. public TestPlan ExpectCanvasDrawImage(Position position, Size size)
  169. {
  170. return AddOperation(new CanvasDrawImageOperation(position, size));
  171. }
  172. public TestPlan CheckMeasureResult(ISpacePlan expected)
  173. {
  174. Element.HandleVisitor(x => x?.Initialize(null, Canvas));
  175. var actual = Element.Measure(OperationInput);
  176. Assert.AreEqual(expected.GetType(), actual.GetType());
  177. var expectedSize = expected as Size;
  178. var actualSize = actual as Size;
  179. if (expectedSize != null)
  180. {
  181. Assert.AreEqual(expectedSize.Width, actualSize.Width, "Measure: width");
  182. Assert.AreEqual(expectedSize.Height, actualSize.Height, "Measure: height");
  183. }
  184. return this;
  185. }
  186. public TestPlan CheckDrawResult()
  187. {
  188. Element.HandleVisitor(x => x?.Initialize(null, Canvas));
  189. Element.Draw(OperationInput);
  190. return this;
  191. }
  192. public TestPlan CheckState(Func<Element, bool> condition)
  193. {
  194. Assert.IsTrue(condition(Element), "Checking condition");
  195. return this;
  196. }
  197. public TestPlan CheckState<T>(Func<T, bool> condition) where T : Element
  198. {
  199. Assert.IsTrue(Element is T);
  200. Assert.IsTrue(condition(Element as T), "Checking condition");
  201. return this;
  202. }
  203. public static Element CreateUniqueElement()
  204. {
  205. return new Constrained
  206. {
  207. MinWidth = 90,
  208. MinHeight = 60,
  209. Child = new DynamicImage
  210. {
  211. Source = Placeholders.Image
  212. }
  213. };
  214. }
  215. public static void CompareOperations(Element value, Element expected, Size? availableSpace = null)
  216. {
  217. CompareMeasureOperations(value, expected, availableSpace);
  218. CompareDrawOperations(value, expected, availableSpace);
  219. }
  220. private static void CompareMeasureOperations(Element value, Element expected, Size? availableSpace = null)
  221. {
  222. availableSpace ??= new Size(400, 300);
  223. var canvas = new FreeCanvas();
  224. value.HandleVisitor(x => x.Initialize(null, canvas));
  225. var valueMeasure = value.Measure(availableSpace);
  226. expected.HandleVisitor(x => x.Initialize(null, canvas));
  227. var expectedMeasure = expected.Measure(availableSpace);
  228. valueMeasure.Should().BeEquivalentTo(expectedMeasure);
  229. }
  230. private static void CompareDrawOperations(Element value, Element expected, Size? availableSpace = null)
  231. {
  232. availableSpace ??= new Size(400, 300);
  233. var valueCanvas = new OperationRecordingCanvas();
  234. value.HandleVisitor(x => x.Initialize(null, valueCanvas));
  235. value.Draw(availableSpace);
  236. var expectedCanvas = new OperationRecordingCanvas();
  237. expected.HandleVisitor(x => x.Initialize(null, expectedCanvas));
  238. expected.Draw(availableSpace);
  239. valueCanvas.Operations.Should().BeEquivalentTo(expectedCanvas.Operations);
  240. }
  241. }
  242. }