TestPlan.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.Json;
  4. using FluentAssertions;
  5. using NUnit.Framework;
  6. using NUnit.Framework.Legacy;
  7. using QuestPDF.Drawing;
  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 sealed 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. ClassicAssert.AreEqual(expected.Position.X, position.X, "Translate X");
  48. ClassicAssert.AreEqual(expected.Position.Y, position.Y, "Translate Y");
  49. },
  50. RotateFunc = angle =>
  51. {
  52. var expected = GetExpected<CanvasRotateOperation>();
  53. ClassicAssert.AreEqual(expected.Angle, angle, "Rotate angle");
  54. },
  55. ScaleFunc = (scaleX, scaleY) =>
  56. {
  57. var expected = GetExpected<CanvasScaleOperation>();
  58. ClassicAssert.AreEqual(expected.ScaleX, scaleX, "Scale X");
  59. ClassicAssert.AreEqual(expected.ScaleY, scaleY, "Scale Y");
  60. },
  61. DrawRectFunc = (position, size, color) =>
  62. {
  63. var expected = GetExpected<CanvasDrawRectangleOperation>();
  64. ClassicAssert.AreEqual(expected.Position.X, position.X, "Draw rectangle: X");
  65. ClassicAssert.AreEqual(expected.Position.Y, position.Y, "Draw rectangle: Y");
  66. ClassicAssert.AreEqual(expected.Size.Width, size.Width, "Draw rectangle: width");
  67. ClassicAssert.AreEqual(expected.Size.Height, size.Height, "Draw rectangle: height");
  68. ClassicAssert.AreEqual(expected.Color, color, "Draw rectangle: color");
  69. },
  70. DrawImageFunc = (image, position, size) =>
  71. {
  72. var expected = GetExpected<CanvasDrawImageOperation>();
  73. ClassicAssert.AreEqual(expected.Position.X, position.X, "Draw image: X");
  74. ClassicAssert.AreEqual(expected.Position.Y, position.Y, "Draw image: Y");
  75. ClassicAssert.AreEqual(expected.Size.Width, size.Width, "Draw image: width");
  76. ClassicAssert.AreEqual(expected.Size.Height, size.Height, "Draw image: height");
  77. }
  78. };
  79. }
  80. public Element CreateChild() => CreateChild(DefaultChildName);
  81. public Element CreateChild(string id)
  82. {
  83. return new ElementMock
  84. {
  85. Id = id,
  86. MeasureFunc = availableSpace =>
  87. {
  88. var expected = GetExpected<ChildMeasureOperation>();
  89. ClassicAssert.AreEqual(expected.ChildId, id);
  90. ClassicAssert.AreEqual(expected.Input.Width, availableSpace.Width, $"Measure: width of child '{expected.ChildId}'");
  91. ClassicAssert.AreEqual(expected.Input.Height, availableSpace.Height, $"Measure: height of child '{expected.ChildId}'");
  92. return expected.Output;
  93. },
  94. DrawFunc = availableSpace =>
  95. {
  96. var expected = GetExpected<ChildDrawOperation>();
  97. ClassicAssert.AreEqual(expected.ChildId, id);
  98. ClassicAssert.AreEqual(expected.Input.Width, availableSpace.Width, $"Draw: width of child '{expected.ChildId}'");
  99. ClassicAssert.AreEqual(expected.Input.Height, availableSpace.Height, $"Draw: width of child '{expected.ChildId}'");
  100. }
  101. };
  102. }
  103. public TestPlan MeasureElement(Size input)
  104. {
  105. OperationInput = input;
  106. return this;
  107. }
  108. public TestPlan DrawElement(Size input)
  109. {
  110. OperationInput = input;
  111. return this;
  112. }
  113. private TestPlan AddOperation(OperationBase operationBase)
  114. {
  115. Operations.Enqueue(operationBase);
  116. return this;
  117. }
  118. public TestPlan ExpectChildMeasure(Size expectedInput, SpacePlan returns)
  119. {
  120. return ExpectChildMeasure(DefaultChildName, expectedInput, returns);
  121. }
  122. public TestPlan ExpectChildMeasure(string child, Size expectedInput, SpacePlan returns)
  123. {
  124. return AddOperation(new ChildMeasureOperation(child, expectedInput, returns));
  125. }
  126. public TestPlan ExpectChildDraw(Size expectedInput)
  127. {
  128. return ExpectChildDraw(DefaultChildName, expectedInput);
  129. }
  130. public TestPlan ExpectChildDraw(string child, Size expectedInput)
  131. {
  132. return AddOperation(new ChildDrawOperation(child, expectedInput));
  133. }
  134. public TestPlan ExpectCanvasTranslate(Position position)
  135. {
  136. return AddOperation(new CanvasTranslateOperation(position));
  137. }
  138. public TestPlan ExpectCanvasTranslate(float left, float top)
  139. {
  140. return AddOperation(new CanvasTranslateOperation(new Position(left, top)));
  141. }
  142. public TestPlan ExpectCanvasScale(float scaleX, float scaleY)
  143. {
  144. return AddOperation(new CanvasScaleOperation(scaleX, scaleY));
  145. }
  146. public TestPlan ExpectCanvasRotate(float angle)
  147. {
  148. return AddOperation(new CanvasRotateOperation(angle));
  149. }
  150. public TestPlan ExpectCanvasDrawRectangle(Position position, Size size, Color color)
  151. {
  152. return AddOperation(new CanvasDrawRectangleOperation(position, size, color));
  153. }
  154. public TestPlan ExpectCanvasDrawImage(Position position, Size size)
  155. {
  156. return AddOperation(new CanvasDrawImageOperation(position, size));
  157. }
  158. public TestPlan CheckMeasureResult(SpacePlan expected)
  159. {
  160. Element.InjectDependencies(null, Canvas);
  161. var actual = Element.Measure(OperationInput);
  162. ClassicAssert.AreEqual(expected.GetType(), actual.GetType());
  163. ClassicAssert.AreEqual(expected.Width, actual.Width, "Measure: width");
  164. ClassicAssert.AreEqual(expected.Height, actual.Height, "Measure: height");
  165. ClassicAssert.AreEqual(expected.Type, actual.Type, "Measure: height");
  166. return this;
  167. }
  168. public TestPlan CheckDrawResult()
  169. {
  170. Element.InjectDependencies(null, Canvas);
  171. Element.Draw(OperationInput);
  172. return this;
  173. }
  174. public TestPlan CheckState(Func<Element, bool> condition)
  175. {
  176. ClassicAssert.IsTrue(condition(Element), "Checking condition");
  177. return this;
  178. }
  179. public TestPlan CheckState<T>(Func<T, bool> condition) where T : Element
  180. {
  181. ClassicAssert.IsTrue(Element is T);
  182. ClassicAssert.IsTrue(condition(Element as T), "Checking condition");
  183. return this;
  184. }
  185. public static Element CreateUniqueElement()
  186. {
  187. return new Constrained
  188. {
  189. MinWidth = 90,
  190. MinHeight = 60,
  191. Child = new DynamicImage
  192. {
  193. CompressionQuality = ImageCompressionQuality.Medium,
  194. TargetDpi = DocumentSettings.DefaultRasterDpi,
  195. Source = paylaod => Placeholders.Image(paylaod.ImageSize)
  196. }
  197. };
  198. }
  199. public static void CompareOperations(Element value, Element expected, Size? availableSpace = null)
  200. {
  201. CompareMeasureOperations(value, expected, availableSpace);
  202. CompareDrawOperations(value, expected, availableSpace);
  203. }
  204. private static void CompareMeasureOperations(Element value, Element expected, Size? availableSpace = null)
  205. {
  206. availableSpace ??= new Size(400, 300);
  207. var canvas = new FreeCanvas();
  208. value.InjectDependencies(null, canvas);
  209. var valueMeasure = value.Measure(availableSpace.Value);
  210. expected.InjectDependencies(null, canvas);
  211. var expectedMeasure = expected.Measure(availableSpace.Value);
  212. valueMeasure.Should().BeEquivalentTo(expectedMeasure);
  213. }
  214. private static void CompareDrawOperations(Element value, Element expected, Size? availableSpace = null)
  215. {
  216. availableSpace ??= new Size(400, 300);
  217. var valueCanvas = new OperationRecordingCanvas();
  218. value.InjectDependencies(null, valueCanvas);
  219. value.Draw(availableSpace.Value);
  220. var expectedCanvas = new OperationRecordingCanvas();
  221. expected.InjectDependencies(null, expectedCanvas);
  222. expected.Draw(availableSpace.Value);
  223. valueCanvas.Operations.Should().BeEquivalentTo(expectedCanvas.Operations);
  224. }
  225. }
  226. }