|
| 1 | +package com.demcha.smoke; |
| 2 | + |
| 3 | +import com.demcha.compose.GraphCompose; |
| 4 | +import com.demcha.compose.document.api.DocumentPageSize; |
| 5 | +import com.demcha.compose.document.api.DocumentSession; |
| 6 | +import com.demcha.compose.document.backend.fixed.BackendProviders; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import java.io.ByteArrayInputStream; |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.zip.ZipEntry; |
| 15 | +import java.util.zip.ZipInputStream; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +/** |
| 20 | + * Scenario 7 — {@code graph-compose-core} + {@code graph-compose-render-pptx} |
| 21 | + * resolved from Maven Central produces a real, editable PowerPoint deck. |
| 22 | + * |
| 23 | + * <p>The deck is inspected with {@link ZipInputStream} rather than Apache POI: |
| 24 | + * the point is to prove the <em>published</em> artifacts work for a consumer who |
| 25 | + * installed nothing else, so the scenario must not pull a parsing library of its |
| 26 | + * own to make its assertions pass.</p> |
| 27 | + * |
| 28 | + * <p>An empty file with a valid ZIP header would satisfy a naive smoke test, so |
| 29 | + * the assertions go further: the slide part must exist, carry real text runs, and |
| 30 | + * consist of native shapes rather than one full-slide picture — the failure mode |
| 31 | + * that would mean the backend silently fell back to rasterising everything.</p> |
| 32 | + */ |
| 33 | +class CoreRenderPptxTest { |
| 34 | + |
| 35 | + /** EMU per PostScript point, the unit OOXML stores slide dimensions in. */ |
| 36 | + private static final int EMU_PER_POINT = 12700; |
| 37 | + |
| 38 | + @Test |
| 39 | + void coreWithRenderPptxProducesAnEditableDeck() throws Exception { |
| 40 | + byte[] deck; |
| 41 | + try (DocumentSession document = GraphCompose.document() |
| 42 | + .pageSize(DocumentPageSize.SLIDE_16_9) |
| 43 | + .margin(48f, 48f, 48f, 48f) |
| 44 | + .create()) { |
| 45 | + document.add(document.dsl().paragraph() |
| 46 | + .text("graph-compose-core + graph-compose-render-pptx renders via the SPI.") |
| 47 | + .build()); |
| 48 | + deck = document.toPptxBytes(); |
| 49 | + } |
| 50 | + |
| 51 | + assertThat(deck).isNotEmpty(); |
| 52 | + assertThat(new String(deck, 0, 2, StandardCharsets.US_ASCII)) |
| 53 | + .describedAs("a .pptx is an OPC package, so it must start with the ZIP signature") |
| 54 | + .isEqualTo("PK"); |
| 55 | + |
| 56 | + Map<String, byte[]> parts = unzip(deck); |
| 57 | + assertThat(parts).containsKey("[Content_Types].xml"); |
| 58 | + assertThat(parts) |
| 59 | + .describedAs("one resolved page must become one slide part") |
| 60 | + .containsKey("ppt/slides/slide1.xml"); |
| 61 | + assertThat(parts.keySet().stream().filter(name -> name.startsWith("ppt/slides/slide"))) |
| 62 | + .hasSize(1); |
| 63 | + |
| 64 | + String presentation = text(parts.get("ppt/presentation.xml")); |
| 65 | + assertThat(presentation) |
| 66 | + .describedAs("SLIDE_16_9 is 960x540 pt, which OOXML stores in EMU") |
| 67 | + .contains("cx=\"" + (960 * EMU_PER_POINT) + "\"") |
| 68 | + .contains("cy=\"" + (540 * EMU_PER_POINT) + "\""); |
| 69 | + |
| 70 | + String slide = text(parts.get("ppt/slides/slide1.xml")); |
| 71 | + assertThat(slide) |
| 72 | + .describedAs("the paragraph must land as an editable text run, not as pixels") |
| 73 | + .contains("<a:t>"); |
| 74 | + assertThat(countOccurrences(slide, "<p:sp>")) |
| 75 | + .describedAs("the slide must be built from native shapes") |
| 76 | + .isPositive(); |
| 77 | + // POI writes both elements without attributes, verified against a generated |
| 78 | + // deck that does contain a picture — so neither assertion is vacuous, and |
| 79 | + // "<p:sp>" cannot accidentally match "<p:spTree" or "<p:spPr>". |
| 80 | + assertThat(countOccurrences(slide, "<p:pic>")) |
| 81 | + .describedAs("a text-only document must produce no pictures at all in vector mode") |
| 82 | + .isZero(); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void thePptxProviderIsDiscoveredByFormat() { |
| 87 | + assertThat(BackendProviders.fixedLayout("pptx").format()).isEqualTo("pptx"); |
| 88 | + } |
| 89 | + |
| 90 | + private static Map<String, byte[]> unzip(byte[] archive) throws Exception { |
| 91 | + Map<String, byte[]> parts = new HashMap<>(); |
| 92 | + try (ZipInputStream zip = new ZipInputStream(new ByteArrayInputStream(archive))) { |
| 93 | + ZipEntry entry; |
| 94 | + while ((entry = zip.getNextEntry()) != null) { |
| 95 | + if (entry.isDirectory()) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 99 | + zip.transferTo(bytes); |
| 100 | + parts.put(entry.getName(), bytes.toByteArray()); |
| 101 | + } |
| 102 | + } |
| 103 | + return parts; |
| 104 | + } |
| 105 | + |
| 106 | + private static String text(byte[] part) { |
| 107 | + assertThat(part).isNotNull(); |
| 108 | + return new String(part, StandardCharsets.UTF_8); |
| 109 | + } |
| 110 | + |
| 111 | + private static int countOccurrences(String haystack, String needle) { |
| 112 | + int count = 0; |
| 113 | + for (int at = haystack.indexOf(needle); at >= 0; at = haystack.indexOf(needle, at + needle.length())) { |
| 114 | + count++; |
| 115 | + } |
| 116 | + return count; |
| 117 | + } |
| 118 | +} |
0 commit comments