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.

TestXSLFSheet.java 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.usermodel;
  16. import static org.apache.poi.xslf.XSLFTestDataSamples.openSampleDocument;
  17. import static org.apache.poi.xslf.XSLFTestDataSamples.writeOutAndReadBack;
  18. import static org.junit.jupiter.api.Assertions.*;
  19. import java.io.IOException;
  20. import java.util.List;
  21. import org.apache.poi.xslf.XSLFTestDataSamples;
  22. import org.junit.jupiter.api.Test;
  23. /**
  24. * test common properties for sheets (slides, masters, layouts, etc.)
  25. */
  26. class TestXSLFSheet {
  27. @Test
  28. void testCreateShapes() throws IOException {
  29. XMLSlideShow ppt = new XMLSlideShow();
  30. XSLFSlide slide = ppt.createSlide();
  31. assertTrue(slide.getShapes().isEmpty());
  32. XSLFSimpleShape shape1 = slide.createAutoShape();
  33. assertEquals(1, slide.getShapes().size());
  34. assertSame(shape1, slide.getShapes().get(0));
  35. XSLFTextBox shape2 = slide.createTextBox();
  36. assertEquals(2, slide.getShapes().size());
  37. assertSame(shape1, slide.getShapes().get(0));
  38. assertSame(shape2, slide.getShapes().get(1));
  39. XSLFConnectorShape shape3 = slide.createConnector();
  40. assertEquals(3, slide.getShapes().size());
  41. assertSame(shape1, slide.getShapes().get(0));
  42. assertSame(shape2, slide.getShapes().get(1));
  43. assertSame(shape3, slide.getShapes().get(2));
  44. XSLFGroupShape shape4 = slide.createGroup();
  45. assertEquals(4, slide.getShapes().size());
  46. assertSame(shape1, slide.getShapes().get(0));
  47. assertSame(shape2, slide.getShapes().get(1));
  48. assertSame(shape3, slide.getShapes().get(2));
  49. assertSame(shape4, slide.getShapes().get(3));
  50. XMLSlideShow ppt2 = XSLFTestDataSamples.writeOutAndReadBack(ppt);
  51. slide = ppt2.getSlides().get(0);
  52. List<XSLFShape> shapes = slide.getShapes();
  53. assertEquals(4, shapes.size());
  54. assertTrue(shapes.get(0) instanceof XSLFAutoShape);
  55. assertTrue(shapes.get(1) instanceof XSLFTextBox);
  56. assertTrue(shapes.get(2) instanceof XSLFConnectorShape);
  57. assertTrue(shapes.get(3) instanceof XSLFGroupShape);
  58. ppt.close();
  59. ppt2.close();
  60. }
  61. @Test
  62. void testImportContent() throws Exception {
  63. try (XMLSlideShow ppt = openSampleDocument("chart-slide-bg.pptx")) {
  64. XSLFSlide sourceSlide = ppt.getSlides().get(0);
  65. XSLFSlide targetSlide = ppt.createSlide();
  66. targetSlide.importContent(sourceSlide);
  67. XSLFShape shape = targetSlide.getShapes().get(0);
  68. assertNotNull(((XSLFGraphicFrame) shape).getChart(), "chart found?");
  69. assertEquals(2, targetSlide.getSlideNumber());
  70. try (XMLSlideShow ppt2 = writeOutAndReadBack(ppt)) {
  71. XSLFSlide slide1 = ppt2.getSlides().get(1);
  72. assertEquals(2, slide1.getSlideNumber());
  73. XSLFShape shape1 = targetSlide.getShapes().get(0);
  74. assertNotNull(((XSLFGraphicFrame) shape1).getChart(), "chart found in slide1?");
  75. }
  76. }
  77. }
  78. }