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.

TestXSLFShape.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertSame;
  18. import static org.junit.Assert.assertTrue;
  19. import java.io.IOException;
  20. import java.util.List;
  21. import org.apache.poi.xslf.XSLFTestDataSamples;
  22. import org.junit.Test;
  23. import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType;
  24. public class TestXSLFShape {
  25. @Test
  26. public void testReadTextShapes() throws IOException {
  27. XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");
  28. List<XSLFSlide> slides = ppt.getSlides();
  29. XSLFSlide slide1 = slides.get(0);
  30. List<XSLFShape> shapes1 = slide1.getShapes();
  31. assertEquals(7, shapes1.size());
  32. assertEquals("TextBox 3", shapes1.get(0).getShapeName());
  33. XSLFAutoShape sh0 = (XSLFAutoShape) shapes1.get(0);
  34. assertEquals("Learning PPTX", sh0.getText());
  35. List<XSLFTextParagraph> paragraphs0 = sh0.getTextParagraphs();
  36. assertEquals(1, paragraphs0.size());
  37. XSLFTextParagraph p0 = paragraphs0.get(0);
  38. assertEquals("Learning PPTX", p0.getText());
  39. assertEquals(1, p0.getTextRuns().size());
  40. XSLFTextRun r0 = p0.getTextRuns().get(0);
  41. assertEquals("Learning PPTX", r0.getRawText());
  42. XSLFSlide slide2 = slides.get(1);
  43. List<XSLFShape> shapes2 = slide2.getShapes();
  44. assertTrue(shapes2.get(0) instanceof XSLFAutoShape);
  45. assertEquals("PPTX Title", ((XSLFAutoShape) shapes2.get(0)).getText());
  46. XSLFAutoShape sh1 = (XSLFAutoShape) shapes2.get(0);
  47. List<XSLFTextParagraph> paragraphs1 = sh1.getTextParagraphs();
  48. assertEquals(1, paragraphs1.size());
  49. XSLFTextParagraph p1 = paragraphs1.get(0);
  50. assertEquals("PPTX Title", p1.getText());
  51. List<XSLFTextRun> r2 = paragraphs1.get(0).getTextRuns();
  52. assertEquals(2, r2.size());
  53. assertEquals("PPTX ", r2.get(0).getRawText());
  54. assertEquals("Title", r2.get(1).getRawText());
  55. // Title is underlined
  56. assertEquals(STTextUnderlineType.SNG, r2.get(1).getRPr(false).getU());
  57. assertTrue(shapes2.get(1) instanceof XSLFAutoShape);
  58. assertEquals("Subtitle\nAnd second line", ((XSLFAutoShape) shapes2.get(1)).getText());
  59. XSLFAutoShape sh2 = (XSLFAutoShape) shapes2.get(1);
  60. List<XSLFTextParagraph> paragraphs2 = sh2.getTextParagraphs();
  61. assertEquals(2, paragraphs2.size());
  62. assertEquals("Subtitle", paragraphs2.get(0).getText());
  63. assertEquals("And second line", paragraphs2.get(1).getText());
  64. assertEquals(1, paragraphs2.get(0).getTextRuns().size());
  65. assertEquals(1, paragraphs2.get(1).getTextRuns().size());
  66. assertEquals("Subtitle", paragraphs2.get(0).getTextRuns().get(0).getRawText());
  67. assertTrue(paragraphs2.get(0).getTextRuns().get(0).getRPr(false).getB());
  68. assertEquals("And second line", paragraphs2.get(1).getTextRuns().get(0).getRawText());
  69. ppt.close();
  70. }
  71. @Test
  72. public void testProblemFile() throws IOException {
  73. try (XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("ececapstonespring2012.pptx")) {
  74. List<XSLFSlide> slides = ppt.getSlides();
  75. assertEquals(24, slides.size());
  76. }
  77. }
  78. @Test
  79. public void testCreateShapes() throws IOException {
  80. XMLSlideShow ppt = new XMLSlideShow();
  81. XSLFSlide slide = ppt.createSlide();
  82. assertTrue(slide.getShapes().isEmpty());
  83. XSLFTextBox textBox = slide.createTextBox();
  84. assertEquals(1, slide.getShapes().size());
  85. assertSame(textBox, slide.getShapes().get(0));
  86. assertEquals("", textBox.getText());
  87. // FIXME: is this correct? Should it be starting out with 0 or 1 text paragraphs?
  88. assertEquals(1, textBox.getTextParagraphs().size());
  89. textBox.addNewTextParagraph().addNewTextRun().setText("Apache");
  90. textBox.addNewTextParagraph().addNewTextRun().setText("POI");
  91. assertEquals("Apache\nPOI", textBox.getText());
  92. assertEquals(3, textBox.getTextParagraphs().size());
  93. ppt.close();
  94. }
  95. }