ShowOnceTest.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Moq;
  2. using NUnit.Framework;
  3. using QuestPDF.Drawing.SpacePlan;
  4. using QuestPDF.Elements;
  5. using QuestPDF.Infrastructure;
  6. using QuestPDF.UnitTests.TestEngine;
  7. namespace QuestPDF.UnitTests
  8. {
  9. [TestFixture]
  10. public class ShowOnceTest
  11. {
  12. [Test]
  13. public void Measure_ShouldHandleNullChild() => new ShowOnce().MeasureWithoutChild();
  14. [Test]
  15. public void Draw_ShouldHandleNullChild() => new ShowOnce().DrawWithoutChild();
  16. [Test]
  17. public void ShouldRenderOnce_WhenRenderingCalledMultipleTimes()
  18. {
  19. var child = new Mock<Element>();
  20. child
  21. .Setup(x => x.Measure(It.IsAny<Size>()))
  22. .Returns(() => new FullRender(Size.Zero));
  23. var element = new ShowOnce()
  24. {
  25. Child = child.Object
  26. };
  27. element.Draw(null, Size.Zero);
  28. element.Draw(null, Size.Zero);
  29. child.Verify(x => x.Draw(It.IsAny<ICanvas>(), It.IsAny<Size>()), Times.Once);
  30. }
  31. [Test]
  32. public void Draw_HorizontalRight_VerticalTop()
  33. {
  34. TestPlan
  35. .For(x => new ShowOnce()
  36. {
  37. Child = x.CreateChild("child")
  38. })
  39. // Measure the element and return result
  40. .MeasureElement(new Size(300, 200))
  41. .ExpectChildMeasure("child", new Size(300, 200), new PartialRender(new Size(200, 200)))
  42. .CheckMeasureResult(new PartialRender(new Size(200, 200)))
  43. // Draw element partially
  44. .DrawElement(new Size(200, 200))
  45. .ExpectChildMeasure("child", new Size(200, 200), new PartialRender(new Size(200, 200)))
  46. .ExpectChildDraw("child", new Size(200, 200))
  47. .CheckDrawResult()
  48. // Element was not fully drawn
  49. // It should be measured again for rendering on next page
  50. .MeasureElement(new Size(800, 200))
  51. .ExpectChildMeasure("child", new Size(800, 200), new FullRender(new Size(400, 200)))
  52. .CheckMeasureResult(new FullRender(new Size(400, 200)))
  53. // Draw element on next page
  54. // Element was fully drawn at this point
  55. .DrawElement(new Size(400, 200))
  56. .ExpectChildMeasure("child", new Size(400, 200), new FullRender(new Size(400, 200)))
  57. .ExpectChildDraw("child", new Size(400, 200))
  58. .CheckDrawResult()
  59. // In the next attempt of measuring element, it should behave like empty parent.
  60. .MeasureElement(new Size(600, 200))
  61. .CheckMeasureResult(new FullRender(Size.Zero))
  62. // In the next attempt of measuring element, it should not draw its child
  63. .DrawElement(new Size(600, 200))
  64. .CheckDrawResult();
  65. }
  66. }
  67. }