You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXSLFSlideShow.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xslf;
  16. import java.awt.Color;
  17. import java.awt.geom.Rectangle2D;
  18. import java.io.IOException;
  19. import org.apache.poi.POIDataSamples;
  20. import org.apache.poi.ooxml.POIXMLProperties.CoreProperties;
  21. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  22. import org.apache.poi.openxml4j.opc.OPCPackage;
  23. import org.apache.poi.openxml4j.opc.PackagePart;
  24. import org.apache.poi.sl.usermodel.ShapeType;
  25. import org.apache.poi.xslf.usermodel.*;
  26. import org.apache.xmlbeans.XmlException;
  27. import org.apache.xmlbeans.XmlObject;
  28. import org.junit.jupiter.api.AfterEach;
  29. import org.junit.jupiter.api.BeforeEach;
  30. import org.junit.jupiter.api.Test;
  31. import org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties;
  32. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry;
  33. import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdListEntry;
  34. import static org.junit.jupiter.api.Assertions.*;
  35. class TestXSLFSlideShow {
  36. private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
  37. private OPCPackage pack;
  38. @BeforeEach
  39. void setUp() throws Exception {
  40. pack = OPCPackage.open(slTests.openResourceAsStream("sample.pptx"));
  41. }
  42. @AfterEach
  43. void tearDown() throws IOException {
  44. pack.close();
  45. }
  46. @Test
  47. void testContainsMainContentType() throws Exception {
  48. boolean found = false;
  49. for(PackagePart part : pack.getParts()) {
  50. if(part.getContentType().equals(XSLFRelation.MAIN.getContentType())) {
  51. found = true;
  52. }
  53. }
  54. assertTrue(found);
  55. }
  56. @Test
  57. void testOpen() throws IOException, OpenXML4JException, XmlException {
  58. // With the finalized uri, should be fine
  59. XSLFSlideShow xml = new XSLFSlideShow(pack);
  60. // Check the core
  61. assertNotNull(xml.getPresentation());
  62. // Check it has some slides
  63. assertNotEquals(0, xml.getSlideReferences().sizeOfSldIdArray());
  64. assertNotEquals(0, xml.getSlideMasterReferences().sizeOfSldMasterIdArray());
  65. xml.close();
  66. }
  67. @Test
  68. void testSlideBasics() throws IOException, OpenXML4JException, XmlException {
  69. XSLFSlideShow xml = new XSLFSlideShow(pack);
  70. // Should have 1 master
  71. assertEquals(1, xml.getSlideMasterReferences().sizeOfSldMasterIdArray());
  72. // Should have three sheets
  73. assertEquals(2, xml.getSlideReferences().sizeOfSldIdArray());
  74. // Check they're as expected
  75. CTSlideIdListEntry[] slides = xml.getSlideReferences().getSldIdArray();
  76. assertEquals(256, slides[0].getId());
  77. assertEquals(257, slides[1].getId());
  78. assertEquals("rId2", slides[0].getId2());
  79. assertEquals("rId3", slides[1].getId2());
  80. // Now get those objects
  81. assertNotNull(xml.getSlide(slides[0]));
  82. assertNotNull(xml.getSlide(slides[1]));
  83. // And check they have notes as expected
  84. assertNotNull(xml.getNotes(slides[0]));
  85. assertNotNull(xml.getNotes(slides[1]));
  86. // And again for the master
  87. CTSlideMasterIdListEntry[] masters = xml.getSlideMasterReferences().getSldMasterIdArray();
  88. // see SlideAtom.USES_MASTER_SLIDE_ID
  89. assertEquals(0x80000000L, masters[0].getId());
  90. assertEquals("rId1", masters[0].getId2());
  91. assertNotNull(xml.getSlideMaster(masters[0]));
  92. xml.close();
  93. }
  94. @Test
  95. void testMetadataBasics() throws IOException, OpenXML4JException, XmlException {
  96. XSLFSlideShow xml = new XSLFSlideShow(pack);
  97. assertNotNull(xml.getProperties().getCoreProperties());
  98. assertNotNull(xml.getProperties().getExtendedProperties());
  99. CTProperties props = xml.getProperties().getExtendedProperties().getUnderlyingProperties();
  100. assertEquals("Microsoft Office PowerPoint", props.getApplication());
  101. assertEquals(0, props.getCharacters());
  102. assertEquals(0, props.getLines());
  103. CoreProperties cprops = xml.getProperties().getCoreProperties();
  104. assertNull(cprops.getTitle());
  105. assertFalse(cprops.getUnderlyingProperties().getSubjectProperty().isPresent());
  106. xml.close();
  107. }
  108. @Test
  109. void testMasterBackground() throws IOException {
  110. XMLSlideShow ppt = new XMLSlideShow();
  111. XSLFBackground b = ppt.getSlideMasters().get(0).getBackground();
  112. b.setFillColor(Color.RED);
  113. XSLFSlide sl = ppt.createSlide();
  114. XSLFAutoShape as = sl.createAutoShape();
  115. as.setAnchor(new Rectangle2D.Double(100,100,100,100));
  116. as.setShapeType(ShapeType.CLOUD);
  117. XMLSlideShow ppt2 = XSLFTestDataSamples.writeOutAndReadBack(ppt);
  118. ppt.close();
  119. XSLFBackground b2 = ppt2.getSlideMasters().get(0).getBackground();
  120. assertEquals(Color.RED, b2.getFillColor());
  121. ppt2.close();
  122. }
  123. @Test
  124. void testSlideImportContent() throws IOException{
  125. try (XMLSlideShow ppt = new XMLSlideShow(slTests.openResourceAsStream("templatePPTWithOnlyOneText.pptx"))) {
  126. XSLFSlide templateSlide = ppt.getSlides().get(0);
  127. XSLFTextShape templateTextShape = (XSLFTextShape) templateSlide.getShapes().get(0);
  128. XmlObject templateTextRunXmlObject = templateTextShape.getTextParagraphs().get(0).getTextRuns().get(0).getXmlObject();
  129. XSLFSlide copySlide = ppt.createSlide();
  130. copySlide.importContent(templateSlide);
  131. XSLFTextShape copyTextShape = (XSLFTextShape) copySlide.getShapes().get(0);
  132. XmlObject copyTextRunXmlObject = copyTextShape.getTextParagraphs().get(0).getTextRuns().get(0).getXmlObject();
  133. assertNotEquals(templateTextRunXmlObject, copyTextRunXmlObject);
  134. }
  135. }
  136. }