RowTests.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using NUnit.Framework;
  2. using QuestPDF.Drawing;
  3. using QuestPDF.Elements;
  4. using QuestPDF.Fluent;
  5. using QuestPDF.Infrastructure;
  6. using QuestPDF.UnitTests.TestEngine;
  7. namespace QuestPDF.UnitTests
  8. {
  9. [TestFixture]
  10. public class RowTests
  11. {
  12. #region Measure
  13. [Test]
  14. public void Measure_ReturnsWrap_WhenLeftChildReturnsWrap()
  15. {
  16. TestPlan
  17. .For(x => new BinaryRow
  18. {
  19. Left = x.CreateChild("left"),
  20. Right = x.CreateChild("right")
  21. })
  22. .MeasureElement(new Size(400, 300))
  23. .ExpectChildMeasure("left", new Size(400, 300), SpacePlan.Wrap())
  24. .CheckMeasureResult(SpacePlan.Wrap());
  25. }
  26. [Test]
  27. public void Measure_ReturnsWrap_WhenRightChildReturnsWrap()
  28. {
  29. TestPlan
  30. .For(x => new BinaryRow
  31. {
  32. Left = x.CreateChild("left"),
  33. Right = x.CreateChild("right")
  34. })
  35. .MeasureElement(new Size(400, 300))
  36. .ExpectChildMeasure("left", new Size(400, 300), SpacePlan.FullRender(250, 150))
  37. .ExpectChildMeasure("right", new Size(150, 300), SpacePlan.Wrap())
  38. .CheckMeasureResult(SpacePlan.Wrap());
  39. }
  40. [Test]
  41. public void Measure_ReturnsPartialRender_WhenLeftChildReturnsPartialRender()
  42. {
  43. TestPlan
  44. .For(x => new BinaryRow
  45. {
  46. Left = x.CreateChild("left"),
  47. Right = x.CreateChild("right")
  48. })
  49. .MeasureElement(new Size(400, 300))
  50. .ExpectChildMeasure("left", new Size(400, 300), SpacePlan.PartialRender(250, 150))
  51. .ExpectChildMeasure("right", new Size(150, 300), SpacePlan.FullRender(100, 100))
  52. .CheckMeasureResult(SpacePlan.PartialRender(350, 150));
  53. }
  54. [Test]
  55. public void Measure_ReturnsPartialRender_WhenRightChildReturnsPartialRender()
  56. {
  57. TestPlan
  58. .For(x => new BinaryRow
  59. {
  60. Left = x.CreateChild("left"),
  61. Right = x.CreateChild("right")
  62. })
  63. .MeasureElement(new Size(400, 300))
  64. .ExpectChildMeasure("left", new Size(400, 300), SpacePlan.FullRender(250, 150))
  65. .ExpectChildMeasure("right", new Size(150, 300), SpacePlan.PartialRender(100, 100))
  66. .CheckMeasureResult(SpacePlan.PartialRender(350, 150));
  67. }
  68. [Test]
  69. public void Measure_ReturnsFullRender_WhenBothChildrenReturnFullRender()
  70. {
  71. TestPlan
  72. .For(x => new BinaryRow
  73. {
  74. Left = x.CreateChild("left"),
  75. Right = x.CreateChild("right")
  76. })
  77. .MeasureElement(new Size(400, 300))
  78. .ExpectChildMeasure("left", new Size(400, 300), SpacePlan.FullRender(200, 150))
  79. .ExpectChildMeasure("right", new Size(200, 300), SpacePlan.FullRender(100, 100))
  80. .CheckMeasureResult(SpacePlan.FullRender(300, 150));
  81. }
  82. #endregion
  83. #region Draw
  84. [Test]
  85. public void Draw()
  86. {
  87. TestPlan
  88. .For(x => new BinaryRow
  89. {
  90. Left = x.CreateChild("left"),
  91. Right = x.CreateChild("right")
  92. })
  93. .DrawElement(new Size(400, 300))
  94. .ExpectChildMeasure("left", new Size(400, 300), SpacePlan.FullRender(250, 150))
  95. .ExpectChildDraw("left", new Size(250, 300))
  96. .ExpectCanvasTranslate(250, 0)
  97. .ExpectChildDraw("right", new Size(150, 300))
  98. .ExpectCanvasTranslate(-250, 0)
  99. .CheckDrawResult();
  100. }
  101. #endregion
  102. #region Structure
  103. [Test]
  104. public void Structure_RelativeColumnsHandling()
  105. {
  106. // arrange
  107. var childA = TestPlan.CreateUniqueElement();
  108. var childB = TestPlan.CreateUniqueElement();
  109. var childC = TestPlan.CreateUniqueElement();
  110. var childD = TestPlan.CreateUniqueElement();
  111. var childE = TestPlan.CreateUniqueElement();
  112. const int spacing = 25;
  113. var availableSpace = new Size(1100, 400);
  114. // act
  115. var value = new Container();
  116. value.Row(row =>
  117. {
  118. row.Spacing(spacing);
  119. row.ConstantColumn(150).Element(childA);
  120. row.ConstantColumn(250).Element(childB);
  121. row.RelativeColumn(1).Element(childC);
  122. row.RelativeColumn(2).Element(childD);
  123. row.RelativeColumn(3).Element(childE);
  124. });
  125. // assert
  126. var expected = new Container();
  127. expected.Row(row =>
  128. {
  129. row.Spacing(spacing);
  130. row.ConstantColumn(150).Element(childA);
  131. row.ConstantColumn(250).Element(childB);
  132. row.ConstantColumn(100).Element(childC);
  133. row.ConstantColumn(200).Element(childD);
  134. row.ConstantColumn(300).Element(childE);
  135. });
  136. TestPlan.CompareOperations(value, expected, availableSpace);
  137. }
  138. [Test]
  139. public void Structure_Tree()
  140. {
  141. // arrange
  142. var childA = TestPlan.CreateUniqueElement();
  143. var childB = TestPlan.CreateUniqueElement();
  144. var childC = TestPlan.CreateUniqueElement();
  145. var childD = TestPlan.CreateUniqueElement();
  146. var childE = TestPlan.CreateUniqueElement();
  147. const int spacing = 25;
  148. var availableSpace = new Size(1200, 400);
  149. // act
  150. var value = new Container();
  151. value.Row(row =>
  152. {
  153. row.Spacing(spacing);
  154. row.ConstantColumn(150).Element(childA);
  155. row.ConstantColumn(200).Element(childB);
  156. row.ConstantColumn(250).Element(childC);
  157. row.RelativeColumn(2).Element(childD);
  158. row.RelativeColumn(3).Element(childE);
  159. });
  160. // assert
  161. var expected = new BinaryRow
  162. {
  163. Left = new BinaryRow
  164. {
  165. Left = new BinaryRow
  166. {
  167. Left = new Constrained
  168. {
  169. MinWidth = 150,
  170. MaxWidth = 150,
  171. Child = childA
  172. },
  173. Right = new Constrained
  174. {
  175. MinWidth = 25,
  176. MaxWidth = 25
  177. }
  178. },
  179. Right = new BinaryRow
  180. {
  181. Left = new Constrained
  182. {
  183. MinWidth = 200,
  184. MaxWidth = 200,
  185. Child = childB
  186. },
  187. Right = new Constrained
  188. {
  189. MinWidth = 25,
  190. MaxWidth = 25
  191. }
  192. }
  193. },
  194. Right = new BinaryRow
  195. {
  196. Left = new BinaryRow
  197. {
  198. Left = new Constrained
  199. {
  200. MinWidth = 250,
  201. MaxWidth = 250,
  202. Child = childC
  203. },
  204. Right = new Constrained
  205. {
  206. MinWidth = 25,
  207. MaxWidth = 25
  208. }
  209. },
  210. Right = new BinaryRow
  211. {
  212. Left = new Constrained
  213. {
  214. MinWidth = 200,
  215. MaxWidth = 200,
  216. Child = childD
  217. },
  218. Right = new BinaryRow
  219. {
  220. Left = new Constrained
  221. {
  222. MinWidth = 25,
  223. MaxWidth = 25
  224. },
  225. Right = new Constrained
  226. {
  227. MinWidth = 300,
  228. MaxWidth = 300,
  229. Child = childE
  230. }
  231. }
  232. }
  233. }
  234. };
  235. TestPlan.CompareOperations(value, expected, availableSpace);
  236. }
  237. #endregion
  238. }
  239. }