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.

BaseTestSlideShow.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.sl.usermodel;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertNull;
  19. import static org.junit.Assert.assertSame;
  20. import java.awt.Color;
  21. import java.awt.geom.Rectangle2D;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.util.List;
  26. import org.apache.poi.POIDataSamples;
  27. import org.apache.poi.sl.usermodel.PictureData.PictureType;
  28. import org.apache.poi.sl.usermodel.TabStop.TabStopType;
  29. import org.junit.Test;
  30. public abstract class BaseTestSlideShow {
  31. protected static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
  32. public abstract SlideShow<?, ?> createSlideShow();
  33. public abstract SlideShow<?, ?> reopen(SlideShow<?, ?> show);
  34. @Test
  35. public void addPicture_File() throws IOException {
  36. SlideShow<?,?> show = createSlideShow();
  37. File f = slTests.getFile("clock.jpg");
  38. assertEquals(0, show.getPictureData().size());
  39. PictureData picture = show.addPicture(f, PictureType.JPEG);
  40. assertEquals(1, show.getPictureData().size());
  41. assertSame(picture, show.getPictureData().get(0));
  42. show.close();
  43. }
  44. @Test
  45. public void addPicture_Stream() throws IOException {
  46. SlideShow<?,?> show = createSlideShow();
  47. try {
  48. InputStream stream = slTests.openResourceAsStream("clock.jpg");
  49. try {
  50. assertEquals(0, show.getPictureData().size());
  51. PictureData picture = show.addPicture(stream, PictureType.JPEG);
  52. assertEquals(1, show.getPictureData().size());
  53. assertSame(picture, show.getPictureData().get(0));
  54. } finally {
  55. stream.close();
  56. }
  57. } finally {
  58. show.close();
  59. }
  60. }
  61. @Test
  62. public void addPicture_ByteArray() throws IOException {
  63. SlideShow<?,?> show = createSlideShow();
  64. byte[] data = slTests.readFile("clock.jpg");
  65. assertEquals(0, show.getPictureData().size());
  66. PictureData picture = show.addPicture(data, PictureType.JPEG);
  67. assertEquals(1, show.getPictureData().size());
  68. assertSame(picture, show.getPictureData().get(0));
  69. show.close();
  70. }
  71. @Test
  72. public void findPicture() throws IOException {
  73. SlideShow<?,?> show = createSlideShow();
  74. byte[] data = slTests.readFile("clock.jpg");
  75. assertNull(show.findPictureData(data));
  76. PictureData picture = show.addPicture(data, PictureType.JPEG);
  77. PictureData found = show.findPictureData(data);
  78. assertNotNull(found);
  79. assertEquals(picture, found);
  80. show.close();
  81. }
  82. @Test
  83. public void addTabStops() throws IOException {
  84. try (final SlideShow<?,?> show1 = createSlideShow()) {
  85. // first set the TabStops in the Master sheet
  86. final MasterSheet<?, ?> master1 = show1.getSlideMasters().get(0);
  87. final AutoShape<?, ?> master1_as = (AutoShape<?,?>)master1.getPlaceholder(Placeholder.BODY);
  88. final TextParagraph<?, ?, ? extends TextRun> master1_tp = master1_as.getTextParagraphs().get(0);
  89. master1_tp.clearTabStops();
  90. int i1 = 0;
  91. for (final TabStopType tst : TabStopType.values()) {
  92. master1_tp.addTabStops(10+i1*10, tst);
  93. i1++;
  94. }
  95. // then set it on a normal slide
  96. final Slide<?,?> slide1 = show1.createSlide();
  97. final AutoShape<?, ?> slide1_as = slide1.createAutoShape();
  98. slide1_as.setText("abc");
  99. slide1_as.setAnchor(new Rectangle2D.Double(100,100,100,100));
  100. final TextParagraph<?, ?, ? extends TextRun> slide1_tp = slide1_as.getTextParagraphs().get(0);
  101. slide1_tp.getTextRuns().get(0).setFontColor(new Color(0x563412));
  102. slide1_tp.clearTabStops();
  103. int i2 = 0;
  104. for (final TabStopType tst : TabStopType.values()) {
  105. slide1_tp.addTabStops(15+i2*5, tst);
  106. i2++;
  107. }
  108. try (final SlideShow<?, ?> show2 = reopen(show1)) {
  109. final MasterSheet<?, ?> master2 = show2.getSlideMasters().get(0);
  110. final AutoShape<?, ?> master2_as = (AutoShape<?,?>)master2.getPlaceholder(Placeholder.BODY);
  111. final TextParagraph<?, ?, ? extends TextRun> master2_tp = master2_as.getTextParagraphs().get(0);
  112. final List<? extends TabStop> master2_tabStops = master2_tp.getTabStops();
  113. assertNotNull(master2_tabStops);
  114. int i3 = 0;
  115. for (final TabStopType tst : TabStopType.values()) {
  116. final TabStop ts = master2_tabStops.get(i3);
  117. assertEquals(10+i3*10, ts.getPositionInPoints(), 0.0);
  118. assertEquals(tst, ts.getType());
  119. i3++;
  120. }
  121. final Slide<?,?> slide2 = show2.getSlides().get(0);
  122. final AutoShape<?,?> slide2_as = (AutoShape<?,?>)slide2.getShapes().get(0);
  123. final TextParagraph<?, ?, ? extends TextRun> slide2_tp = slide2_as.getTextParagraphs().get(0);
  124. final List<? extends TabStop> slide2_tabStops = slide2_tp.getTabStops();
  125. assertNotNull(slide2_tabStops);
  126. int i4 = 0;
  127. for (final TabStopType tst : TabStopType.values()) {
  128. final TabStop ts = slide2_tabStops.get(i4);
  129. assertEquals(15+i4*5, ts.getPositionInPoints(), 0.0);
  130. assertEquals(tst, ts.getType());
  131. i4++;
  132. }
  133. }
  134. }
  135. }
  136. @Test
  137. public void shapeAndSlideName() throws IOException {
  138. final String file = "SampleShow.ppt"+(getClass().getSimpleName().contains("XML")?"x":"");
  139. try (final InputStream is = slTests.openResourceAsStream(file);
  140. final SlideShow<? extends Shape, ?> ppt = SlideShowFactory.create(is)) {
  141. final List<? extends Shape> shapes1 = ppt.getSlides().get(0).getShapes();
  142. assertEquals("The Title", shapes1.get(0).getShapeName());
  143. assertEquals("Another Subtitle", shapes1.get(1).getShapeName());
  144. final List<? extends Shape> shapes2 = ppt.getSlides().get(1).getShapes();
  145. assertEquals("Title 1", shapes2.get(0).getShapeName());
  146. assertEquals("Content Placeholder 2", shapes2.get(1).getShapeName());
  147. for (final Slide<?,?> slide : ppt.getSlides()) {
  148. final String expected = slide.getSlideNumber()==1 ? "FirstSlide" : "Slide2";
  149. assertEquals(expected, slide.getSlideName());
  150. }
  151. }
  152. }
  153. }