|
| 1 | +package com.itextpdf.html2pdf; |
| 2 | + |
| 3 | +import com.itextpdf.commons.actions.contexts.IMetaInfo; |
| 4 | +import com.itextpdf.html2pdf.attach.ITagWorker; |
| 5 | +import com.itextpdf.html2pdf.attach.ProcessorContext; |
| 6 | +import com.itextpdf.html2pdf.attach.impl.DefaultTagWorkerFactory; |
| 7 | +import com.itextpdf.html2pdf.attach.impl.tags.DivTagWorker; |
| 8 | +import com.itextpdf.html2pdf.html.TagConstants; |
| 9 | +import com.itextpdf.kernel.pdf.DocumentProperties; |
| 10 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 11 | +import com.itextpdf.kernel.pdf.PdfWriter; |
| 12 | +import com.itextpdf.layout.IPropertyContainer; |
| 13 | +import com.itextpdf.layout.element.Div; |
| 14 | +import com.itextpdf.layout.layout.LayoutContext; |
| 15 | +import com.itextpdf.layout.layout.LayoutResult; |
| 16 | +import com.itextpdf.layout.properties.Property; |
| 17 | +import com.itextpdf.layout.renderer.DivRenderer; |
| 18 | +import com.itextpdf.layout.renderer.IRenderer; |
| 19 | +import com.itextpdf.styledxmlparser.node.IElementNode; |
| 20 | +import com.itextpdf.test.ExtendedITextTest; |
| 21 | +import com.itextpdf.test.annotations.type.UnitTest; |
| 22 | + |
| 23 | +import java.io.ByteArrayOutputStream; |
| 24 | +import org.junit.Assert; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.experimental.categories.Category; |
| 27 | + |
| 28 | +@Category(UnitTest.class) |
| 29 | +public class HtmlConverterMetaInfoTest extends ExtendedITextTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testMetaInfoShouldBePresent() { |
| 33 | + DocumentProperties documentProperties = new DocumentProperties(); |
| 34 | + IMetaInfo o = new IMetaInfo() {}; |
| 35 | + documentProperties.setEventCountingMetaInfo(o); |
| 36 | + ConverterProperties converterProperties = new ConverterProperties(); |
| 37 | + converterProperties.setEventMetaInfo(o); |
| 38 | + InvocationAssert invocationAssert = new InvocationAssert(); |
| 39 | + converterProperties.setTagWorkerFactory(new AssertMetaInfoTagWorkerFactory(invocationAssert)); |
| 40 | + // TODO DEVSIX-5790 fix assertion error - all assertions must pass |
| 41 | + Assert.assertThrows(AssertionError.class, () -> { |
| 42 | + HtmlConverter.convertToDocument("<!DOCTYPE html>\n" |
| 43 | + + "<html>\n" |
| 44 | + + "\n" |
| 45 | + + "<body>\n" |
| 46 | + + "<div>\n" |
| 47 | + + "The content of the div\n" |
| 48 | + + "</div>\n" |
| 49 | + + "</body>\n" |
| 50 | + + "\n" |
| 51 | + + "</html>\n", |
| 52 | + new PdfDocument(new PdfWriter(new ByteArrayOutputStream())), |
| 53 | + converterProperties |
| 54 | + ).close(); |
| 55 | + }); |
| 56 | + // TODO DEVSIX-5790 #isInvoked status must be true |
| 57 | + Assert.assertFalse(invocationAssert.isInvoked()); |
| 58 | + } |
| 59 | + |
| 60 | + private static class AssertMetaInfoTagWorkerFactory extends DefaultTagWorkerFactory { |
| 61 | + private final InvocationAssert invocationAssert; |
| 62 | + |
| 63 | + private AssertMetaInfoTagWorkerFactory(InvocationAssert invocationAssert) { |
| 64 | + this.invocationAssert = invocationAssert; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public ITagWorker getCustomTagWorker(IElementNode tag, ProcessorContext context) { |
| 69 | + if (TagConstants.DIV.equals(tag.name())) { |
| 70 | + return new AssertMetaInfoDivTagWorker(tag, context, invocationAssert); |
| 71 | + } |
| 72 | + return super.getCustomTagWorker(tag, context); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static class AssertMetaInfoDivTagWorker extends DivTagWorker { |
| 77 | + private final InvocationAssert invocationAssert; |
| 78 | + |
| 79 | + public AssertMetaInfoDivTagWorker(IElementNode element, ProcessorContext context, InvocationAssert invocationAssert) { |
| 80 | + super(element, context); |
| 81 | + this.invocationAssert = invocationAssert; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public IPropertyContainer getElementResult() { |
| 86 | + Div result = (Div) super.getElementResult(); |
| 87 | + result.setNextRenderer(new AssertMetaInfoDivTagRenderer(result, invocationAssert)); |
| 88 | + return result; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static class AssertMetaInfoDivTagRenderer extends DivRenderer { |
| 93 | + private final InvocationAssert invocationAssert; |
| 94 | + |
| 95 | + public AssertMetaInfoDivTagRenderer(Div modelElement, InvocationAssert invocationAssert) { |
| 96 | + super(modelElement); |
| 97 | + this.invocationAssert = invocationAssert; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public LayoutResult layout(LayoutContext layoutContext) { |
| 102 | + Assert.assertNotNull(this.getProperty(Property.META_INFO)); |
| 103 | + invocationAssert.setInvoked(true); |
| 104 | + return super.layout(layoutContext); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public IRenderer getNextRenderer() { |
| 109 | + return new AssertMetaInfoDivTagRenderer((Div) modelElement, invocationAssert); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static class InvocationAssert { |
| 114 | + private boolean invoked; |
| 115 | + |
| 116 | + public void setInvoked(boolean invoked) { |
| 117 | + this.invoked = invoked; |
| 118 | + } |
| 119 | + |
| 120 | + public boolean isInvoked() { |
| 121 | + return invoked; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments