From ddde81cf806a5534a01483b563651bd84cf7452d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 13 Feb 2016 23:02:55 +0000 Subject: Read embedded blips in wp:inline --- .../zwobble/mammoth/tests/docx/BodyXmlTests.java | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'src/test/java') diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java index cc0815e..0683bff 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java @@ -17,6 +17,7 @@ import org.zwobble.mammoth.xml.XmlNodes; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; +import java.util.Map; import java.util.Optional; import static com.natpryce.makeiteasy.MakeItEasy.*; @@ -403,6 +404,47 @@ public class BodyXmlTests { equalTo("Not an image at all!")); } + @Test + public void canReadInlinePictures() throws IOException { + XmlElement element = inlineImageXml(embeddedBlipXml("rId5"), "It's a hat"); + Relationships relationships = new Relationships(map( + "rId5", new Relationship("media/hat.png"))); + String imageBytes = "Not an image at all!"; + DocxFile file = new InMemoryDocxFile(map("word/media/hat.png", imageBytes)); + + Image image = (Image) readSuccess( + a(bodyReader, with(RELATIONSHIPS, relationships), with(DOCX_FILE, file)), + element); + assertThat(image, allOf( + hasProperty("altText", deepEquals(Optional.of("It's a hat"))), + hasProperty("contentType", deepEquals(Optional.of("image/png"))))); + assertThat( + CharStreams.toString(new InputStreamReader(image.open())), + equalTo("Not an image at all!")); + } + + private XmlElement inlineImageXml(XmlElement blip, String description) { + return element("w:drawing", list( + element("wp:inline", imageXml(blip, description)))); + } + + private List imageXml(XmlElement blip, String description) { + return list( + element("wp:docPr", map("descr", description)), + element("a:graphic", list( + element("a:graphicData", list( + element("pic:pic", list( + element("pic:blipFill", list(blip))))))))); + } + + private XmlElement embeddedBlipXml(String relationshipId) { + return blipXml(map("r:embed", relationshipId)); + } + + private XmlElement blipXml(Map attributes) { + return element("a:blip", attributes); + } + @Test public void appropriateElementsHaveTheirChildrenReadNormally() { assertChildrenAreReadNormally("w:ins"); -- cgit v1.2.3