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.

TestBugs.java 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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.hslf.usermodel;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertNull;
  20. import static org.junit.Assert.assertTrue;
  21. import java.awt.Color;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.io.OutputStream;
  25. import java.io.PrintStream;
  26. import java.text.AttributedCharacterIterator;
  27. import java.text.AttributedCharacterIterator.Attribute;
  28. import java.util.ArrayList;
  29. import java.util.Date;
  30. import java.util.HashMap;
  31. import java.util.HashSet;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.util.Set;
  35. import org.apache.poi.ddf.AbstractEscherOptRecord;
  36. import org.apache.poi.ddf.EscherArrayProperty;
  37. import org.apache.poi.ddf.EscherColorRef;
  38. import org.apache.poi.ddf.EscherProperties;
  39. import org.apache.poi.hslf.HSLFTestDataSamples;
  40. import org.apache.poi.hslf.exceptions.OldPowerPointFormatException;
  41. import org.apache.poi.hslf.extractor.PowerPointExtractor;
  42. import org.apache.poi.hslf.model.HeadersFooters;
  43. import org.apache.poi.hslf.record.Document;
  44. import org.apache.poi.hslf.record.Record;
  45. import org.apache.poi.hslf.record.SlideListWithText;
  46. import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
  47. import org.apache.poi.hslf.record.TextHeaderAtom;
  48. import org.apache.poi.hssf.usermodel.DummyGraphics2d;
  49. import org.apache.poi.sl.draw.DrawPaint;
  50. import org.apache.poi.sl.draw.DrawTextParagraph;
  51. import org.apache.poi.sl.usermodel.PaintStyle;
  52. import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
  53. import org.apache.poi.sl.usermodel.PictureData.PictureType;
  54. import org.apache.poi.sl.usermodel.Placeholder;
  55. import org.apache.poi.sl.usermodel.Slide;
  56. import org.apache.poi.sl.usermodel.SlideShow;
  57. import org.apache.poi.sl.usermodel.SlideShowFactory;
  58. import org.apache.poi.sl.usermodel.TextBox;
  59. import org.apache.poi.sl.usermodel.TextParagraph;
  60. import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;
  61. import org.apache.poi.sl.usermodel.TextRun;
  62. import org.apache.poi.util.LittleEndian;
  63. import org.apache.poi.util.StringUtil;
  64. import org.apache.poi.util.Units;
  65. import org.junit.Test;
  66. /**
  67. * Testcases for bugs entered in bugzilla
  68. * the Test name contains the bugzilla bug id
  69. */
  70. public final class TestBugs {
  71. /**
  72. * Bug 41384: Array index wrong in record creation
  73. */
  74. @Test
  75. public void bug41384() throws IOException {
  76. HSLFSlideShow ppt = open("41384.ppt");
  77. assertEquals(1, ppt.getSlides().size());
  78. List<HSLFPictureData> pict = ppt.getPictureData();
  79. assertEquals(2, pict.size());
  80. assertEquals(PictureType.JPEG, pict.get(0).getType());
  81. assertEquals(PictureType.JPEG, pict.get(1).getType());
  82. ppt.close();
  83. }
  84. /**
  85. * First fix from Bug 42474: NPE in RichTextRun.isBold()
  86. * when the RichTextRun comes from a Notes model object
  87. */
  88. @Test
  89. public void bug42474_1() throws IOException {
  90. HSLFSlideShow ppt = open("42474-1.ppt");
  91. assertEquals(2, ppt.getSlides().size());
  92. List<HSLFTextParagraph> txrun;
  93. HSLFNotes notes;
  94. notes = ppt.getSlides().get(0).getNotes();
  95. assertNotNull(notes);
  96. txrun = notes.getTextParagraphs().get(0);
  97. assertEquals("Notes-1", HSLFTextParagraph.getRawText(txrun));
  98. assertEquals(false, txrun.get(0).getTextRuns().get(0).isBold());
  99. //notes for the second slide are in bold
  100. notes = ppt.getSlides().get(1).getNotes();
  101. assertNotNull(notes);
  102. txrun = notes.getTextParagraphs().get(0);
  103. assertEquals("Notes-2", HSLFTextParagraph.getRawText(txrun));
  104. assertEquals(true, txrun.get(0).getTextRuns().get(0).isBold());
  105. ppt.close();
  106. }
  107. /**
  108. * Second fix from Bug 42474: Incorrect matching of notes to slides
  109. */
  110. @Test
  111. public void bug42474_2() throws IOException {
  112. HSLFSlideShow ppt = open("42474-2.ppt");
  113. //map slide number and starting phrase of its notes
  114. Map<Integer, String> notesMap = new HashMap<Integer, String>();
  115. notesMap.put(Integer.valueOf(4), "For decades before calculators");
  116. notesMap.put(Integer.valueOf(5), "Several commercial applications");
  117. notesMap.put(Integer.valueOf(6), "There are three variations of LNS that are discussed here");
  118. notesMap.put(Integer.valueOf(7), "Although multiply and square root are easier");
  119. notesMap.put(Integer.valueOf(8), "The bus Z is split into Z_H and Z_L");
  120. for (HSLFSlide slide : ppt.getSlides()) {
  121. Integer slideNumber = Integer.valueOf(slide.getSlideNumber());
  122. HSLFNotes notes = slide.getNotes();
  123. if (notesMap.containsKey(slideNumber)){
  124. assertNotNull(notes);
  125. String text = HSLFTextParagraph.getRawText(notes.getTextParagraphs().get(0));
  126. String startingPhrase = notesMap.get(slideNumber);
  127. assertTrue("Notes for slide " + slideNumber + " must start with " +
  128. startingPhrase , text.startsWith(startingPhrase));
  129. }
  130. }
  131. ppt.close();
  132. }
  133. /**
  134. * Bug 42485: All TextBoxes inside ShapeGroups have null TextRuns
  135. */
  136. @Test
  137. public void bug42485 () throws IOException {
  138. HSLFSlideShow ppt = open("42485.ppt");
  139. for (HSLFShape shape : ppt.getSlides().get(0).getShapes()) {
  140. if(shape instanceof HSLFGroupShape){
  141. HSLFGroupShape group = (HSLFGroupShape)shape;
  142. for (HSLFShape sh : group.getShapes()) {
  143. if(sh instanceof HSLFTextBox){
  144. HSLFTextBox txt = (HSLFTextBox)sh;
  145. assertNotNull(txt.getTextParagraphs());
  146. }
  147. }
  148. }
  149. }
  150. ppt.close();
  151. }
  152. /**
  153. * Bug 42484: NullPointerException from ShapeGroup.getAnchor()
  154. */
  155. @Test
  156. public void bug42484 () throws IOException {
  157. HSLFSlideShow ppt = open("42485.ppt");
  158. for (HSLFShape shape : ppt.getSlides().get(0).getShapes()) {
  159. if(shape instanceof HSLFGroupShape){
  160. HSLFGroupShape group = (HSLFGroupShape)shape;
  161. assertNotNull(group.getAnchor());
  162. for (HSLFShape sh : group.getShapes()) {
  163. assertNotNull(sh.getAnchor());
  164. }
  165. }
  166. }
  167. ppt.close();
  168. }
  169. /**
  170. * Bug 41381: Exception from Slide.getMasterSheet() on a seemingly valid PPT file
  171. */
  172. @Test
  173. public void bug41381() throws IOException {
  174. HSLFSlideShow ppt = open("alterman_security.ppt");
  175. assertTrue("No Exceptions while reading file", true);
  176. assertEquals(1, ppt.getSlideMasters().size());
  177. assertEquals(1, ppt.getTitleMasters().size());
  178. boolean isFirst = true;
  179. for (HSLFSlide slide : ppt.getSlides()) {
  180. HSLFMasterSheet master = slide.getMasterSheet();
  181. // the first slide follows TitleMaster
  182. assertTrue(isFirst ? master instanceof HSLFTitleMaster : master instanceof HSLFSlideMaster);
  183. isFirst = false;
  184. }
  185. ppt.close();
  186. }
  187. /**
  188. * Bug 42486: Failure parsing a seemingly valid PPT
  189. */
  190. @SuppressWarnings("unused")
  191. @Test
  192. public void bug42486 () throws IOException {
  193. HSLFSlideShow ppt = open("42486.ppt");
  194. for (HSLFSlide slide : ppt.getSlides()) {
  195. List<HSLFShape> shape = slide.getShapes();
  196. }
  197. ppt.close();
  198. }
  199. /**
  200. * Bug 42524: NPE in Shape.getShapeType()
  201. */
  202. @Test
  203. public void bug42524 () throws IOException {
  204. HSLFSlideShow ppt = open("42486.ppt");
  205. //walk down the tree and see if there were no errors while reading
  206. for (HSLFSlide slide : ppt.getSlides()) {
  207. for (HSLFShape shape : slide.getShapes()) {
  208. assertNotNull(shape.getShapeName());
  209. if (shape instanceof HSLFGroupShape){
  210. HSLFGroupShape group = (HSLFGroupShape)shape;
  211. for (HSLFShape comps : group.getShapes()) {
  212. assertNotNull(comps.getShapeName());
  213. }
  214. }
  215. }
  216. }
  217. ppt.close();
  218. }
  219. /**
  220. * Bug 42520: NPE in Picture.getPictureData()
  221. */
  222. @SuppressWarnings("unused")
  223. @Test
  224. public void bug42520 () throws IOException {
  225. HSLFSlideShow ppt = open("42520.ppt");
  226. //test case from the bug report
  227. HSLFGroupShape shapeGroup = (HSLFGroupShape)ppt.getSlides().get(11).getShapes().get(10);
  228. HSLFPictureShape picture = (HSLFPictureShape)shapeGroup.getShapes().get(0);
  229. picture.getPictureData();
  230. //walk down the tree and see if there were no errors while reading
  231. for (HSLFSlide slide : ppt.getSlides()) {
  232. for (HSLFShape shape : slide.getShapes()) {
  233. if (shape instanceof HSLFGroupShape){
  234. HSLFGroupShape group = (HSLFGroupShape)shape;
  235. for (HSLFShape comp : group.getShapes()) {
  236. if (comp instanceof HSLFPictureShape){
  237. HSLFPictureData pict = ((HSLFPictureShape)comp).getPictureData();
  238. }
  239. }
  240. }
  241. }
  242. }
  243. ppt.close();
  244. }
  245. /**
  246. * Bug 38256: RuntimeException: Couldn't instantiate the class for type with id 0.
  247. * ( also fixed followup: getTextRuns() returns no text )
  248. */
  249. @Test
  250. public void bug38256 () throws IOException {
  251. HSLFSlideShow ppt = open("38256.ppt");
  252. List<HSLFSlide> slide = ppt.getSlides();
  253. assertEquals(1, slide.size());
  254. List<List<HSLFTextParagraph>> paras = slide.get(0).getTextParagraphs();
  255. assertEquals(4, paras.size());
  256. Set<String> txt = new HashSet<String>();
  257. txt.add("\u201CHAPPY BIRTHDAY SCOTT\u201D");
  258. txt.add("Have a HAPPY DAY");
  259. txt.add("PS Nobody is allowed to hassle Scott TODAY\u2026");
  260. txt.add("Drinks will be in the Boardroom at 5pm today to celebrate Scott\u2019s B\u2019Day\u2026 See you all there!");
  261. for (List<HSLFTextParagraph> para : paras) {
  262. String text = HSLFTextParagraph.getRawText(para);
  263. assertTrue(text, txt.contains(text));
  264. }
  265. ppt.close();
  266. }
  267. /**
  268. * Bug 38256: RuntimeException: Couldn't instantiate the class for type with id 0.
  269. * ( also fixed followup: getTextRuns() returns no text )
  270. */
  271. @Test
  272. public void bug43781() throws IOException {
  273. HSLFSlideShow ppt = open("43781.ppt");
  274. // Check the first slide
  275. HSLFSlide slide = ppt.getSlides().get(0);
  276. List<List<HSLFTextParagraph>> slTr = slide.getTextParagraphs();
  277. // Has 3 text paragraphs, two from slide text (empty title / filled body), one from drawing
  278. assertEquals(3, slTr.size());
  279. assertFalse(slTr.get(0).get(0).isDrawingBased());
  280. assertFalse(slTr.get(1).get(0).isDrawingBased());
  281. assertTrue(slTr.get(2).get(0).isDrawingBased());
  282. assertEquals("", HSLFTextParagraph.getRawText(slTr.get(0)));
  283. assertEquals("First run", HSLFTextParagraph.getRawText(slTr.get(1)));
  284. assertEquals("Second run", HSLFTextParagraph.getRawText(slTr.get(2)));
  285. // Check the shape based text runs
  286. List<HSLFTextParagraph> lst = new ArrayList<HSLFTextParagraph>();
  287. for (HSLFShape shape : slide.getShapes()) {
  288. if (shape instanceof HSLFTextShape){
  289. List<HSLFTextParagraph> textRun = ((HSLFTextShape)shape).getTextParagraphs();
  290. lst.addAll(textRun);
  291. }
  292. }
  293. // There are two shapes in the ppt
  294. assertEquals(2, lst.size());
  295. assertEquals("First runSecond run", HSLFTextParagraph.getRawText(lst));
  296. ppt.close();
  297. }
  298. /**
  299. * Bug 44296: HSLF Not Extracting Slide Background Image
  300. */
  301. @Test
  302. public void bug44296 () throws IOException {
  303. HSLFSlideShow ppt = open("44296.ppt");
  304. HSLFSlide slide = ppt.getSlides().get(0);
  305. HSLFBackground b = slide.getBackground();
  306. HSLFFill f = b.getFill();
  307. assertEquals(HSLFFill.FILL_PICTURE, f.getFillType());
  308. HSLFPictureData pict = f.getPictureData();
  309. assertNotNull(pict);
  310. assertEquals(PictureType.JPEG, pict.getType());
  311. ppt.close();
  312. }
  313. /**
  314. * Bug 44770: java.lang.RuntimeException: Couldn't instantiate the class for
  315. * type with id 1036 on class class org.apache.poi.hslf.record.PPDrawing
  316. */
  317. @Test
  318. public void bug44770() throws IOException {
  319. open("44770.ppt").close();
  320. }
  321. /**
  322. * Bug 41071: Will not extract text from Powerpoint TextBoxes
  323. */
  324. @Test
  325. public void bug41071() throws IOException {
  326. HSLFSlideShow ppt = open("41071.ppt");
  327. HSLFSlide slide = ppt.getSlides().get(0);
  328. List<HSLFShape> sh = slide.getShapes();
  329. assertEquals(1, sh.size());
  330. assertTrue(sh.get(0) instanceof HSLFTextShape);
  331. HSLFTextShape tx = (HSLFTextShape)sh.get(0);
  332. assertEquals("Fundera, planera och involvera.", HSLFTextParagraph.getRawText(tx.getTextParagraphs()));
  333. List<List<HSLFTextParagraph>> run = slide.getTextParagraphs();
  334. assertEquals(3, run.size());
  335. assertEquals("Fundera, planera och involvera.", HSLFTextParagraph.getRawText(run.get(2)));
  336. ppt.close();
  337. }
  338. /**
  339. * PowerPoint 95 files should throw a more helpful exception
  340. * @throws IOException
  341. */
  342. @Test(expected=OldPowerPointFormatException.class)
  343. public void bug41711() throws IOException {
  344. // New file is fine
  345. open("SampleShow.ppt").close();
  346. // PowerPoint 95 gives an old format exception
  347. open("PPT95.ppt").close();
  348. }
  349. /**
  350. * Changing text from Ascii to Unicode
  351. */
  352. @Test
  353. public void bug49648() throws IOException {
  354. HSLFSlideShow ppt = open("49648.ppt");
  355. for (HSLFSlide slide : ppt.getSlides()) {
  356. for (List<HSLFTextParagraph> run : slide.getTextParagraphs()) {
  357. String text = HSLFTextParagraph.getRawText(run);
  358. text.replace("{txtTot}", "With \u0123\u1234\u5678 unicode");
  359. HSLFTextParagraph.setText(run, text);
  360. }
  361. }
  362. ppt.close();
  363. }
  364. /**
  365. * Bug 41246: AIOOB with illegal note references
  366. */
  367. @Test
  368. public void bug41246a() throws IOException {
  369. HSLFSlideShow ppt = open("41246-1.ppt");
  370. HSLFTestDataSamples.writeOutAndReadBack(ppt).close();
  371. ppt.close();
  372. }
  373. @Test
  374. public void bug41246b() throws IOException {
  375. HSLFSlideShow ppt = open("41246-2.ppt");
  376. HSLFTestDataSamples.writeOutAndReadBack(ppt).close();
  377. ppt.close();
  378. }
  379. /**
  380. * Bug 45776: Fix corrupt file problem using TextRun.setText
  381. */
  382. @Test
  383. public void bug45776() throws IOException {
  384. HSLFSlideShow ppt = open("45776.ppt");
  385. // get slides
  386. for (HSLFSlide slide : ppt.getSlides()) {
  387. for (HSLFShape shape : slide.getShapes()) {
  388. if (!(shape instanceof HSLFTextBox)) continue;
  389. HSLFTextBox tb = (HSLFTextBox) shape;
  390. // work with TextBox
  391. String str = tb.getText();
  392. if (!str.contains("$$DATE$$")) continue;
  393. str = str.replace("$$DATE$$", new Date().toString());
  394. tb.setText(str);
  395. List<HSLFTextParagraph> tr = tb.getTextParagraphs();
  396. assertEquals(str.length()+1,tr.get(0).getParagraphStyle().getCharactersCovered());
  397. assertEquals(str.length()+1,tr.get(0).getTextRuns().get(0).getCharacterStyle().getCharactersCovered());
  398. }
  399. }
  400. ppt.close();
  401. }
  402. @Test
  403. public void bug55732() throws IOException {
  404. HSLFSlideShow ppt = open("bug55732.ppt");
  405. List<HSLFSlide> _slides = ppt.getSlides();
  406. /* Iterate over slides and extract text */
  407. for( HSLFSlide slide : _slides ) {
  408. HeadersFooters hf = slide.getHeadersFooters();
  409. /*boolean visible =*/ hf.isHeaderVisible(); // exception happens here
  410. }
  411. ppt.close();
  412. }
  413. @Test
  414. public void bug56260() throws IOException {
  415. HSLFSlideShow ppt = open("56260.ppt");
  416. List<HSLFSlide> _slides = ppt.getSlides();
  417. assertEquals(13, _slides.size());
  418. // Check the number of TextHeaderAtoms on Slide 1
  419. Document dr = ppt.getDocumentRecord();
  420. SlideListWithText slidesSLWT = dr.getSlideSlideListWithText();
  421. SlideAtomsSet s1 = slidesSLWT.getSlideAtomsSets()[0];
  422. int tha = 0;
  423. for (Record r : s1.getSlideRecords()) {
  424. if (r instanceof TextHeaderAtom) tha++;
  425. }
  426. assertEquals(2, tha);
  427. // Check to see that we have a pair next to each other
  428. assertEquals(TextHeaderAtom.class, s1.getSlideRecords()[0].getClass());
  429. assertEquals(TextHeaderAtom.class, s1.getSlideRecords()[1].getClass());
  430. // Check the number of text runs based on the slide (not textbox)
  431. // Will have skipped the empty one
  432. int str = 0;
  433. for (List<HSLFTextParagraph> tr : _slides.get(0).getTextParagraphs()) {
  434. if (! tr.get(0).isDrawingBased()) str++;
  435. }
  436. assertEquals(2, str);
  437. ppt.close();
  438. }
  439. @Test
  440. public void bug37625() throws IOException {
  441. HSLFSlideShow ppt1 = open("37625.ppt");
  442. assertEquals(29, ppt1.getSlides().size());
  443. HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
  444. assertNotNull(ppt2);
  445. assertEquals(29, ppt2.getSlides().size());
  446. ppt2.close();
  447. ppt1.close();
  448. }
  449. @Test
  450. public void bug57272() throws IOException {
  451. HSLFSlideShow ppt1 = open("57272_corrupted_usereditatom.ppt");
  452. assertEquals(6, ppt1.getSlides().size());
  453. HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
  454. assertNotNull(ppt2);
  455. assertEquals(6, ppt2.getSlides().size());
  456. ppt2.close();
  457. ppt1.close();
  458. }
  459. @Test
  460. public void bug49541() throws IOException {
  461. HSLFSlideShow ppt = open("49541_symbol_map.ppt");
  462. HSLFSlide slide = ppt.getSlides().get(0);
  463. HSLFGroupShape sg = (HSLFGroupShape)slide.getShapes().get(0);
  464. HSLFTextBox tb = (HSLFTextBox)sg.getShapes().get(0);
  465. String text = StringUtil.mapMsCodepointString(tb.getText());
  466. assertEquals("\u226575 years", text);
  467. ppt.close();
  468. }
  469. @Test
  470. public void bug47261() throws IOException {
  471. HSLFSlideShow ppt = open("bug47261.ppt");
  472. ppt.removeSlide(0);
  473. ppt.createSlide();
  474. HSLFTestDataSamples.writeOutAndReadBack(ppt).close();
  475. ppt.close();
  476. }
  477. @Test
  478. public void bug56240() throws IOException {
  479. HSLFSlideShow ppt = open("bug56240.ppt");
  480. int slideCnt = ppt.getSlides().size();
  481. assertEquals(105, slideCnt);
  482. HSLFTestDataSamples.writeOutAndReadBack(ppt).close();
  483. ppt.close();
  484. }
  485. @Test
  486. public void bug46441() throws IOException {
  487. HSLFSlideShow ppt = open("bug46441.ppt");
  488. HSLFAutoShape as = (HSLFAutoShape)ppt.getSlides().get(0).getShapes().get(0);
  489. AbstractEscherOptRecord opt = as.getEscherOptRecord();
  490. EscherArrayProperty ep = HSLFShape.getEscherProperty(opt, EscherProperties.FILL__SHADECOLORS);
  491. double exp[][] = {
  492. // r, g, b, position
  493. { 94, 158, 255, 0 },
  494. { 133, 194, 255, 0.399994 },
  495. { 196, 214, 235, 0.699997 },
  496. { 255, 235, 250, 1 }
  497. };
  498. int i = 0;
  499. for (byte data[] : ep) {
  500. EscherColorRef ecr = new EscherColorRef(data, 0, 4);
  501. int rgb[] = ecr.getRGB();
  502. double pos = Units.fixedPointToDouble(LittleEndian.getInt(data, 4));
  503. assertEquals((int)exp[i][0], rgb[0]);
  504. assertEquals((int)exp[i][1], rgb[1]);
  505. assertEquals((int)exp[i][2], rgb[2]);
  506. assertEquals(exp[i][3], pos, 0.01);
  507. i++;
  508. }
  509. ppt.close();
  510. }
  511. @Test
  512. public void bug58516() throws IOException {
  513. open("bug58516.ppt").close();
  514. }
  515. @Test
  516. public void bug45124() throws IOException {
  517. SlideShow<?,?> ppt = open("bug45124.ppt");
  518. Slide<?,?> slide1 = ppt.getSlides().get(1);
  519. TextBox<?,?> res = slide1.createTextBox();
  520. res.setAnchor(new java.awt.Rectangle(60, 150, 700, 100));
  521. res.setText("I am italic-false, bold-true inserted text");
  522. TextParagraph<?,?,?> tp = res.getTextParagraphs().get(0);
  523. TextRun rt = tp.getTextRuns().get(0);
  524. rt.setItalic(false);
  525. assertTrue(rt.isBold());
  526. tp.setBulletStyle(Color.red, 'A');
  527. SlideShow<?,?> ppt2 = HSLFTestDataSamples.writeOutAndReadBack((HSLFSlideShow)ppt);
  528. ppt.close();
  529. res = (TextBox<?,?>)ppt2.getSlides().get(1).getShapes().get(1);
  530. tp = res.getTextParagraphs().get(0);
  531. rt = tp.getTextRuns().get(0);
  532. assertFalse(rt.isItalic());
  533. assertTrue(rt.isBold());
  534. PaintStyle ps = tp.getBulletStyle().getBulletFontColor();
  535. assertTrue(ps instanceof SolidPaint);
  536. Color actColor = DrawPaint.applyColorTransform(((SolidPaint)ps).getSolidColor());
  537. assertEquals(Color.red, actColor);
  538. assertEquals("A", tp.getBulletStyle().getBulletCharacter());
  539. ppt2.close();
  540. }
  541. @Test
  542. public void bug45088() throws IOException {
  543. String template = "[SYSDATE]";
  544. String textExp = "REPLACED_DATE_WITH_A_LONG_ONE";
  545. HSLFSlideShow ppt1 = open("bug45088.ppt");
  546. for (HSLFSlide slide : ppt1.getSlides()) {
  547. for (List<HSLFTextParagraph> paraList : slide.getTextParagraphs()) {
  548. for (HSLFTextParagraph para : paraList) {
  549. for (HSLFTextRun run : para.getTextRuns()) {
  550. String text = run.getRawText();
  551. if (text != null && text.contains(template)) {
  552. String replacedText = text.replace(template, textExp);
  553. run.setText(replacedText);
  554. para.setDirty();
  555. }
  556. }
  557. }
  558. }
  559. }
  560. HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
  561. ppt1.close();
  562. HSLFTextBox tb = (HSLFTextBox)ppt2.getSlides().get(0).getShapes().get(1);
  563. String textAct = tb.getTextParagraphs().get(0).getTextRuns().get(0).getRawText().trim();
  564. assertEquals(textExp, textAct);
  565. ppt2.close();
  566. }
  567. @Test
  568. public void bug45908() throws IOException {
  569. HSLFSlideShow ppt1 = open("bug45908.ppt");
  570. HSLFSlide slide = ppt1.getSlides().get(0);
  571. HSLFAutoShape styleShape = (HSLFAutoShape)slide.getShapes().get(1);
  572. HSLFTextParagraph tp0 = styleShape.getTextParagraphs().get(0);
  573. HSLFTextRun tr0 = tp0.getTextRuns().get(0);
  574. int rows = 5;
  575. int cols = 2;
  576. HSLFTable table = slide.createTable(rows, cols);
  577. for (int i = 0; i < rows; i++) {
  578. for (int j = 0; j < cols; j++) {
  579. HSLFTableCell cell = table.getCell(i, j);
  580. cell.setText("Test");
  581. HSLFTextParagraph tp = cell.getTextParagraphs().get(0);
  582. tp.setBulletStyle('%', tp0.getBulletColor(), tp0.getBulletFont(), tp0.getBulletSize());
  583. tp.setIndent(tp0.getIndent());
  584. tp.setTextAlign(tp0.getTextAlign());
  585. tp.setIndentLevel(tp0.getIndentLevel());
  586. tp.setSpaceAfter(tp0.getSpaceAfter());
  587. tp.setSpaceBefore(tp0.getSpaceBefore());
  588. tp.setBulletStyle();
  589. HSLFTextRun tr = tp.getTextRuns().get(0);
  590. tr.setBold(tr0.isBold());
  591. // rt.setEmbossed();
  592. tr.setFontColor(Color.BLACK);
  593. tr.setFontFamily(tr0.getFontFamily());
  594. tr.setFontSize(tr0.getFontSize());
  595. tr.setItalic(tr0.isItalic());
  596. tr.setShadowed(tr0.isShadowed());
  597. tr.setStrikethrough(tr0.isStrikethrough());
  598. tr.setUnderlined(tr0.isUnderlined());
  599. }
  600. }
  601. table.moveTo(100, 100);
  602. HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
  603. ppt1.close();
  604. HSLFTable tab = (HSLFTable)ppt2.getSlides().get(0).getShapes().get(2);
  605. HSLFTableCell c2 = tab.getCell(0, 0);
  606. HSLFTextParagraph tp1 = c2.getTextParagraphs().get(0);
  607. HSLFTextRun tr1 = tp1.getTextRuns().get(0);
  608. assertFalse(tp1.isBullet());
  609. assertEquals(tp0.getBulletColor(), tp1.getBulletColor());
  610. assertEquals(tp0.getBulletFont(), tp1.getBulletFont());
  611. assertEquals(tp0.getBulletSize(), tp1.getBulletSize());
  612. assertEquals(tp0.getIndent(), tp1.getIndent());
  613. assertEquals(tp0.getTextAlign(), tp1.getTextAlign());
  614. assertEquals(tp0.getIndentLevel(), tp1.getIndentLevel());
  615. assertEquals(tp0.getSpaceAfter(), tp1.getSpaceAfter());
  616. assertEquals(tp0.getSpaceBefore(), tp1.getSpaceBefore());
  617. assertEquals(tr0.isBold(), tr1.isBold());
  618. assertEquals(Color.black, DrawPaint.applyColorTransform(tr1.getFontColor().getSolidColor()));
  619. assertEquals(tr0.getFontFamily(), tr1.getFontFamily());
  620. assertEquals(tr0.getFontSize(), tr1.getFontSize());
  621. assertEquals(tr0.isItalic(), tr1.isItalic());
  622. assertEquals(tr0.isShadowed(), tr1.isShadowed());
  623. assertEquals(tr0.isStrikethrough(), tr1.isStrikethrough());
  624. assertEquals(tr0.isUnderlined(), tr1.isUnderlined());
  625. ppt2.close();
  626. }
  627. @Test
  628. public void bug47904() throws IOException {
  629. HSLFSlideShow ppt1 = new HSLFSlideShow();
  630. HSLFSlideMaster sm = ppt1.getSlideMasters().get(0);
  631. HSLFAutoShape as = (HSLFAutoShape)sm.getShapes().get(0);
  632. HSLFTextParagraph tp = as.getTextParagraphs().get(0);
  633. HSLFTextRun tr = tp.getTextRuns().get(0);
  634. tr.setFontFamily("Tahoma");
  635. tr.setShadowed(true);
  636. tr.setFontSize(44.);
  637. tr.setFontColor(Color.red);
  638. tp.setTextAlign(TextAlign.RIGHT);
  639. ppt1.createSlide().addTitle().setText("foobaa");
  640. HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
  641. ppt1.close();
  642. HSLFTextShape ts = (HSLFTextShape)ppt2.getSlides().get(0).getShapes().get(0);
  643. tp = ts.getTextParagraphs().get(0);
  644. tr = tp.getTextRuns().get(0);
  645. assertEquals(44., tr.getFontSize(), 0);
  646. assertEquals("Tahoma", tr.getFontFamily());
  647. Color colorAct = DrawPaint.applyColorTransform(tr.getFontColor().getSolidColor());
  648. assertEquals(Color.red, colorAct);
  649. assertEquals(TextAlign.RIGHT, tp.getTextAlign());
  650. assertEquals("foobaa", tr.getRawText());
  651. ppt2.close();
  652. }
  653. @Test
  654. public void bug58718() throws IOException {
  655. String files[] = { "bug58718_008524.ppt","bug58718_008558.ppt","bug58718_349008.ppt","bug58718_008495.ppt", };
  656. for (String f : files) {
  657. File sample = HSLFTestDataSamples.getSampleFile(f);
  658. PowerPointExtractor ex = new PowerPointExtractor(sample.getAbsolutePath());
  659. assertNotNull(ex.getText());
  660. ex.close();
  661. }
  662. }
  663. @Test
  664. public void bug58733() throws IOException {
  665. File sample = HSLFTestDataSamples.getSampleFile("bug58733_671884.ppt");
  666. PowerPointExtractor ex = new PowerPointExtractor(sample.getAbsolutePath());
  667. assertNotNull(ex.getText());
  668. ex.close();
  669. }
  670. @Test
  671. public void bug58159() throws IOException {
  672. HSLFSlideShow ppt = open("bug58159_headers-and-footers.ppt");
  673. HeadersFooters hf = ppt.getSlideHeadersFooters();
  674. assertNull(hf.getHeaderText());
  675. assertEquals("Slide footer", hf.getFooterText());
  676. hf = ppt.getNotesHeadersFooters();
  677. assertEquals("Notes header", hf.getHeaderText());
  678. assertEquals("Notes footer", hf.getFooterText());
  679. HSLFSlide sl = ppt.getSlides().get(0);
  680. hf = sl.getHeadersFooters();
  681. assertNull(hf.getHeaderText());
  682. assertEquals("Slide footer", hf.getFooterText());
  683. for (HSLFShape shape : sl.getShapes()) {
  684. if (shape instanceof HSLFTextShape) {
  685. HSLFTextShape ts = (HSLFTextShape)shape;
  686. Placeholder ph = ts.getPlaceholder();
  687. if (Placeholder.FOOTER == ph) {
  688. assertEquals("Slide footer", ts.getText());
  689. }
  690. }
  691. }
  692. ppt.close();
  693. }
  694. @Test
  695. public void bug55030() throws IOException {
  696. HSLFSlideShow ppt = open("bug55030.ppt");
  697. String expFamily = "\u96b6\u4e66";
  698. HSLFSlide sl = ppt.getSlides().get(0);
  699. for (List<HSLFTextParagraph> paraList : sl.getTextParagraphs()) {
  700. for (HSLFTextParagraph htp : paraList) {
  701. for (HSLFTextRun htr : htp) {
  702. String actFamily = htr.getFontFamily();
  703. assertEquals(expFamily, actFamily);
  704. }
  705. }
  706. }
  707. ppt.close();
  708. }
  709. @Test
  710. public void bug57796() throws IOException {
  711. HSLFSlideShow ppt = open("WithLinks.ppt");
  712. HSLFSlide slide = ppt.getSlides().get(0);
  713. HSLFTextShape shape = (HSLFTextShape)slide.getShapes().get(1);
  714. List<HSLFHyperlink> hlList = HSLFHyperlink.find(shape);
  715. HSLFHyperlink hlShape = hlList.get(0);
  716. HSLFTextRun r = shape.getTextParagraphs().get(1).getTextRuns().get(0);
  717. HSLFHyperlink hlRun = r.getHyperlink();
  718. assertEquals(hlRun.getId(), hlShape.getId());
  719. assertEquals(hlRun.getAddress(), hlShape.getAddress());
  720. assertEquals(hlRun.getLabel(), hlShape.getLabel());
  721. assertEquals(hlRun.getType(), hlShape.getType());
  722. assertEquals(hlRun.getStartIndex(), hlShape.getStartIndex());
  723. assertEquals(hlRun.getEndIndex(), hlShape.getEndIndex());
  724. OutputStream nullOutput = new OutputStream(){
  725. public void write(int b) throws IOException {}
  726. };
  727. final boolean found[] = { false };
  728. DummyGraphics2d dgfx = new DummyGraphics2d(new PrintStream(nullOutput)){
  729. public void drawString(AttributedCharacterIterator iterator, float x, float y) {
  730. // For the test file, common sl draws textruns one by one and not mixed
  731. // so we evaluate the whole iterator
  732. Map<Attribute, Object> attributes = null;
  733. StringBuffer sb = new StringBuffer();
  734. for (char c = iterator.first();
  735. c != AttributedCharacterIterator.DONE;
  736. c = iterator.next()) {
  737. sb.append(c);
  738. attributes = iterator.getAttributes();
  739. }
  740. if ("Jakarta HSSF".equals(sb.toString())) {
  741. // this is a test for a manually modified ppt, for real hyperlink label
  742. // one would need to access the screen tip record
  743. String href = (String)attributes.get(DrawTextParagraph.HYPERLINK_HREF);
  744. String label = (String)attributes.get(DrawTextParagraph.HYPERLINK_LABEL);
  745. assertEquals("http://jakarta.apache.org/poi/hssf/", href);
  746. assertEquals("Open Jakarta POI HSSF module test ", label);
  747. found[0] = true;
  748. }
  749. }
  750. };
  751. ppt.getSlides().get(1).draw(dgfx);
  752. assertTrue(found[0]);
  753. ppt.close();
  754. }
  755. private static HSLFSlideShow open(String fileName) throws IOException {
  756. File sample = HSLFTestDataSamples.getSampleFile(fileName);
  757. return (HSLFSlideShow)SlideShowFactory.create(sample);
  758. }
  759. }