001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hslf.model;
019:
020: import junit.framework.TestCase;
021:
022: import org.apache.poi.hslf.HSLFSlideShow;
023: import org.apache.poi.hslf.model.textproperties.TextPropCollection;
024: import org.apache.poi.hslf.record.TextBytesAtom;
025: import org.apache.poi.hslf.record.TextCharsAtom;
026: import org.apache.poi.hslf.record.TextHeaderAtom;
027: import org.apache.poi.hslf.usermodel.RichTextRun;
028: import org.apache.poi.hslf.usermodel.SlideShow;
029:
030: /**
031: * Tests for TextRuns
032: *
033: * @author Nick Burch (nick at torchbox dot com)
034: */
035: public class TestTextRun extends TestCase {
036: // SlideShow primed on the test data
037: private SlideShow ss;
038: private SlideShow ssRich;
039: private HSLFSlideShow hss;
040: private HSLFSlideShow hssRich;
041:
042: protected void setUp() throws Exception {
043: String dirname = System.getProperty("HSLF.testdata.path");
044:
045: // Basic (non rich) test file
046: String filename = dirname + "/basic_test_ppt_file.ppt";
047: hss = new HSLFSlideShow(filename);
048: ss = new SlideShow(hss);
049:
050: // Rich test file
051: filename = dirname + "/Single_Coloured_Page.ppt";
052: hssRich = new HSLFSlideShow(filename);
053: ssRich = new SlideShow(hssRich);
054: }
055:
056: /**
057: * Test to ensure that getting the text works correctly
058: */
059: public void testGetText() throws Exception {
060: Slide slideOne = ss.getSlides()[0];
061: TextRun[] textRuns = slideOne.getTextRuns();
062:
063: assertEquals(2, textRuns.length);
064:
065: // Get text works with \n
066: assertEquals("This is a test title", textRuns[0].getText());
067: assertEquals("This is a test subtitle\nThis is on page 1",
068: textRuns[1].getText());
069:
070: // Raw text has \r instead
071: assertEquals("This is a test title", textRuns[0].getRawText());
072: assertEquals("This is a test subtitle\rThis is on page 1",
073: textRuns[1].getRawText());
074:
075: // Now check on a rich text run
076: Slide slideOneR = ssRich.getSlides()[0];
077: TextRun[] textRunsR = slideOneR.getTextRuns();
078:
079: assertEquals(2, textRunsR.length);
080: assertEquals("This is a title, it\u2019s in black",
081: textRunsR[0].getText());
082: assertEquals(
083: "This is the subtitle, in bold\nThis bit is blue and italic\nThis bit is red (normal)",
084: textRunsR[1].getText());
085: assertEquals("This is a title, it\u2019s in black",
086: textRunsR[0].getRawText());
087: assertEquals(
088: "This is the subtitle, in bold\rThis bit is blue and italic\rThis bit is red (normal)",
089: textRunsR[1].getRawText());
090: }
091:
092: /**
093: * Test to ensure changing non rich text bytes->bytes works correctly
094: */
095: public void testSetText() throws Exception {
096: Slide slideOne = ss.getSlides()[0];
097: TextRun[] textRuns = slideOne.getTextRuns();
098: TextRun run = textRuns[0];
099:
100: // Check current text
101: assertEquals("This is a test title", run.getText());
102:
103: // Change
104: String changeTo = "New test title";
105: run.setText(changeTo);
106: assertEquals(changeTo, run.getText());
107:
108: // Ensure trailing \n's get stripped
109: run.setText(changeTo + "\n");
110: assertEquals(changeTo, run.getText());
111: }
112:
113: /**
114: * Test to ensure that changing non rich text between bytes and
115: * chars works correctly
116: */
117: public void testAdvancedSetText() throws Exception {
118: Slide slideOne = ss.getSlides()[0];
119: TextRun run = slideOne.getTextRuns()[0];
120:
121: TextHeaderAtom tha = run._headerAtom;
122: TextBytesAtom tba = run._byteAtom;
123: TextCharsAtom tca = run._charAtom;
124:
125: // Bytes -> Bytes
126: assertNull(tca);
127: assertNotNull(tba);
128: assertFalse(run._isUnicode);
129: assertEquals("This is a test title", run.getText());
130:
131: String changeBytesOnly = "New Test Title";
132: run.setText(changeBytesOnly);
133: tba = run._byteAtom;
134: tca = run._charAtom;
135:
136: assertEquals(changeBytesOnly, run.getText());
137: assertFalse(run._isUnicode);
138: assertNull(tca);
139: assertNotNull(tba);
140:
141: // Bytes -> Chars
142: assertNull(tca);
143: assertNotNull(tba);
144: assertFalse(run._isUnicode);
145: assertEquals(changeBytesOnly, run.getText());
146:
147: String changeByteChar = "This is a test title with a '\u0121' g with a dot";
148: run.setText(changeByteChar);
149: tba = run._byteAtom;
150: tca = run._charAtom;
151:
152: assertEquals(changeByteChar, run.getText());
153: assertTrue(run._isUnicode);
154: assertNotNull(tca);
155: assertNull(tba);
156:
157: // Chars -> Chars
158: assertNull(tba);
159: assertNotNull(tca);
160: assertTrue(run._isUnicode);
161: assertEquals(changeByteChar, run.getText());
162:
163: String changeCharChar = "This is a test title with a '\u0147' N with a hat";
164: run.setText(changeCharChar);
165: tba = run._byteAtom;
166: tca = run._charAtom;
167:
168: assertEquals(changeCharChar, run.getText());
169: assertTrue(run._isUnicode);
170: assertNotNull(tca);
171: assertNull(tba);
172: }
173:
174: /**
175: * Tests to ensure that non rich text has the right default rich text run
176: * set up for it
177: */
178: public void testGetRichTextNonRich() throws Exception {
179: Slide slideOne = ss.getSlides()[0];
180: TextRun[] textRuns = slideOne.getTextRuns();
181:
182: assertEquals(2, textRuns.length);
183:
184: TextRun trA = textRuns[0];
185: TextRun trB = textRuns[1];
186:
187: assertEquals(1, trA.getRichTextRuns().length);
188: assertEquals(1, trB.getRichTextRuns().length);
189:
190: RichTextRun rtrA = trA.getRichTextRuns()[0];
191: RichTextRun rtrB = trB.getRichTextRuns()[0];
192:
193: assertEquals(trA.getText(), rtrA.getText());
194: assertEquals(trB.getText(), rtrB.getText());
195:
196: assertNull(rtrA._getRawCharacterStyle());
197: assertNull(rtrA._getRawParagraphStyle());
198: assertNull(rtrB._getRawCharacterStyle());
199: assertNull(rtrB._getRawParagraphStyle());
200: }
201:
202: /**
203: * Tests to ensure that the rich text runs are built up correctly
204: */
205: public void testGetRichText() throws Exception {
206: Slide slideOne = ssRich.getSlides()[0];
207: TextRun[] textRuns = slideOne.getTextRuns();
208:
209: assertEquals(2, textRuns.length);
210:
211: TextRun trA = textRuns[0];
212: TextRun trB = textRuns[1];
213:
214: assertEquals(1, trA.getRichTextRuns().length);
215: assertEquals(3, trB.getRichTextRuns().length);
216:
217: RichTextRun rtrA = trA.getRichTextRuns()[0];
218: RichTextRun rtrB = trB.getRichTextRuns()[0];
219: RichTextRun rtrC = trB.getRichTextRuns()[1];
220: RichTextRun rtrD = trB.getRichTextRuns()[2];
221:
222: assertEquals(trA.getText(), rtrA.getText());
223:
224: assertEquals(trB.getText().substring(0, 30), rtrB.getText());
225: assertEquals(trB.getText().substring(30, 58), rtrC.getText());
226: assertEquals(trB.getText().substring(58, 82), rtrD.getText());
227:
228: assertNull(rtrA._getRawCharacterStyle());
229: assertNull(rtrA._getRawParagraphStyle());
230: assertNotNull(rtrB._getRawCharacterStyle());
231: assertNotNull(rtrB._getRawParagraphStyle());
232: assertNotNull(rtrC._getRawCharacterStyle());
233: assertNotNull(rtrC._getRawParagraphStyle());
234: assertNotNull(rtrD._getRawCharacterStyle());
235: assertNotNull(rtrD._getRawParagraphStyle());
236:
237: // Same paragraph styles
238: assertEquals(rtrB._getRawParagraphStyle(), rtrC
239: ._getRawParagraphStyle());
240: assertEquals(rtrB._getRawParagraphStyle(), rtrD
241: ._getRawParagraphStyle());
242:
243: // Different char styles
244: assertFalse(rtrB._getRawCharacterStyle().equals(
245: rtrC._getRawCharacterStyle()));
246: assertFalse(rtrB._getRawCharacterStyle().equals(
247: rtrD._getRawCharacterStyle()));
248: assertFalse(rtrC._getRawCharacterStyle().equals(
249: rtrD._getRawCharacterStyle()));
250: }
251:
252: /**
253: * Tests to ensure that setting the text where the text isn't rich,
254: * ensuring that everything stays with the same default styling
255: */
256: public void testSetTextWhereNotRich() throws Exception {
257: Slide slideOne = ss.getSlides()[0];
258: TextRun[] textRuns = slideOne.getTextRuns();
259: TextRun trB = textRuns[1];
260: assertEquals(1, trB.getRichTextRuns().length);
261:
262: RichTextRun rtrB = trB.getRichTextRuns()[0];
263: assertEquals(trB.getText(), rtrB.getText());
264: assertNull(rtrB._getRawCharacterStyle());
265: assertNull(rtrB._getRawParagraphStyle());
266:
267: // Change text via normal
268: trB.setText("Test Foo Test");
269: rtrB = trB.getRichTextRuns()[0];
270: assertEquals("Test Foo Test", trB.getText());
271: assertEquals("Test Foo Test", rtrB.getText());
272: assertNull(rtrB._getRawCharacterStyle());
273: assertNull(rtrB._getRawParagraphStyle());
274: }
275:
276: /**
277: * Tests to ensure that setting the text where the text is rich
278: * sets everything to the same styling
279: */
280: public void testSetTextWhereRich() throws Exception {
281: Slide slideOne = ssRich.getSlides()[0];
282: TextRun[] textRuns = slideOne.getTextRuns();
283: TextRun trB = textRuns[1];
284: assertEquals(3, trB.getRichTextRuns().length);
285:
286: RichTextRun rtrB = trB.getRichTextRuns()[0];
287: RichTextRun rtrC = trB.getRichTextRuns()[1];
288: RichTextRun rtrD = trB.getRichTextRuns()[2];
289: TextPropCollection tpBP = rtrB._getRawParagraphStyle();
290: TextPropCollection tpBC = rtrB._getRawCharacterStyle();
291: TextPropCollection tpCP = rtrC._getRawParagraphStyle();
292: TextPropCollection tpCC = rtrC._getRawCharacterStyle();
293: TextPropCollection tpDP = rtrD._getRawParagraphStyle();
294: TextPropCollection tpDC = rtrD._getRawCharacterStyle();
295:
296: assertEquals(trB.getText().substring(0, 30), rtrB.getText());
297: assertNotNull(tpBP);
298: assertNotNull(tpBC);
299: assertNotNull(tpCP);
300: assertNotNull(tpCC);
301: assertNotNull(tpDP);
302: assertNotNull(tpDC);
303: assertTrue(tpBP.equals(tpCP));
304: assertTrue(tpBP.equals(tpDP));
305: assertTrue(tpCP.equals(tpDP));
306: assertFalse(tpBC.equals(tpCC));
307: assertFalse(tpBC.equals(tpDC));
308: assertFalse(tpCC.equals(tpDC));
309:
310: // Change text via normal
311: trB.setText("Test Foo Test");
312:
313: // Ensure now have first style
314: assertEquals(1, trB.getRichTextRuns().length);
315: rtrB = trB.getRichTextRuns()[0];
316: assertEquals("Test Foo Test", trB.getText());
317: assertEquals("Test Foo Test", rtrB.getText());
318: assertNotNull(rtrB._getRawCharacterStyle());
319: assertNotNull(rtrB._getRawParagraphStyle());
320: assertEquals(tpBP, rtrB._getRawParagraphStyle());
321: assertEquals(tpBC, rtrB._getRawCharacterStyle());
322: }
323:
324: /**
325: * Test to ensure the right stuff happens if we change the text
326: * in a rich text run, that doesn't happen to actually be rich
327: */
328: public void testChangeTextInRichTextRunNonRich() throws Exception {
329: Slide slideOne = ss.getSlides()[0];
330: TextRun[] textRuns = slideOne.getTextRuns();
331: TextRun trB = textRuns[1];
332: assertEquals(1, trB.getRichTextRuns().length);
333:
334: RichTextRun rtrB = trB.getRichTextRuns()[0];
335: assertEquals(trB.getText(), rtrB.getText());
336: assertNull(rtrB._getRawCharacterStyle());
337: assertNull(rtrB._getRawParagraphStyle());
338:
339: // Change text via rich
340: rtrB.setText("Test Test Test");
341: assertEquals("Test Test Test", trB.getText());
342: assertEquals("Test Test Test", rtrB.getText());
343:
344: // Will now have dummy props
345: assertNotNull(rtrB._getRawCharacterStyle());
346: assertNotNull(rtrB._getRawParagraphStyle());
347: }
348:
349: /**
350: * Tests to ensure changing the text within rich text runs works
351: * correctly
352: */
353: public void testChangeTextInRichTextRun() throws Exception {
354: Slide slideOne = ssRich.getSlides()[0];
355: TextRun[] textRuns = slideOne.getTextRuns();
356: TextRun trB = textRuns[1];
357: assertEquals(3, trB.getRichTextRuns().length);
358:
359: // We start with 3 text runs, each with their own set of styles,
360: // but all sharing the same paragraph styles
361: RichTextRun rtrB = trB.getRichTextRuns()[0];
362: RichTextRun rtrC = trB.getRichTextRuns()[1];
363: RichTextRun rtrD = trB.getRichTextRuns()[2];
364: TextPropCollection tpBP = rtrB._getRawParagraphStyle();
365: TextPropCollection tpBC = rtrB._getRawCharacterStyle();
366: TextPropCollection tpCP = rtrC._getRawParagraphStyle();
367: TextPropCollection tpCC = rtrC._getRawCharacterStyle();
368: TextPropCollection tpDP = rtrD._getRawParagraphStyle();
369: TextPropCollection tpDC = rtrD._getRawCharacterStyle();
370:
371: // Check text and stylings
372: assertEquals(trB.getText().substring(0, 30), rtrB.getText());
373: assertNotNull(tpBP);
374: assertNotNull(tpBC);
375: assertNotNull(tpCP);
376: assertNotNull(tpCC);
377: assertNotNull(tpDP);
378: assertNotNull(tpDC);
379: assertTrue(tpBP.equals(tpCP));
380: assertTrue(tpBP.equals(tpDP));
381: assertTrue(tpCP.equals(tpDP));
382: assertFalse(tpBC.equals(tpCC));
383: assertFalse(tpBC.equals(tpDC));
384: assertFalse(tpCC.equals(tpDC));
385:
386: // Check text in the rich runs
387: assertEquals("This is the subtitle, in bold\n", rtrB.getText());
388: assertEquals("This bit is blue and italic\n", rtrC.getText());
389: assertEquals("This bit is red (normal)", rtrD.getText());
390:
391: String newBText = "New Subtitle, will still be bold\n";
392: String newCText = "New blue and italic text\n";
393: String newDText = "Funky new normal red text";
394: rtrB.setText(newBText);
395: rtrC.setText(newCText);
396: rtrD.setText(newDText);
397: assertEquals(newBText, rtrB.getText());
398: assertEquals(newCText, rtrC.getText());
399: assertEquals(newDText, rtrD.getText());
400:
401: assertEquals(newBText + newCText + newDText, trB.getText());
402:
403: // The styles should have been updated for the new sizes
404: assertEquals(newBText.length(), tpBC.getCharactersCovered());
405: assertEquals(newCText.length(), tpCC.getCharactersCovered());
406: assertEquals(newDText.length() + 1, tpDC.getCharactersCovered()); // Last one is always one larger
407:
408: assertEquals(newBText.length() + newCText.length()
409: + newDText.length(), tpBP.getCharactersCovered());
410:
411: // Paragraph style should be sum of text length
412: assertEquals(newBText.length() + newCText.length()
413: + newDText.length(), tpBP.getCharactersCovered());
414:
415: // Check stylings still as expected
416: TextPropCollection ntpBC = rtrB._getRawCharacterStyle();
417: TextPropCollection ntpCC = rtrC._getRawCharacterStyle();
418: TextPropCollection ntpDC = rtrD._getRawCharacterStyle();
419: assertEquals(tpBC.getTextPropList(), ntpBC.getTextPropList());
420: assertEquals(tpCC.getTextPropList(), ntpCC.getTextPropList());
421: assertEquals(tpDC.getTextPropList(), ntpDC.getTextPropList());
422: }
423:
424: /**
425: * Test case for Bug 41015.
426: *
427: * In some cases RichTextRun.getText() threw StringIndexOutOfBoundsException because
428: * of the wrong list of potential paragraph properties defined in StyleTextPropAtom.
429: *
430: */
431: public void testBug41015() throws Exception {
432: RichTextRun[] rt;
433:
434: SlideShow ppt = new SlideShow(new HSLFSlideShow(System
435: .getProperty("HSLF.testdata.path")
436: + "/bug-41015.ppt"));
437: Slide sl = ppt.getSlides()[0];
438: TextRun[] txt = sl.getTextRuns();
439: assertEquals(2, txt.length);
440:
441: rt = txt[0].getRichTextRuns();
442: assertEquals(1, rt.length);
443: assertEquals(0, rt[0].getIndentLevel());
444: assertEquals("sdfsdfsdf", rt[0].getText());
445:
446: rt = txt[1].getRichTextRuns();
447: assertEquals(2, rt.length);
448: assertEquals(0, rt[0].getIndentLevel());
449: assertEquals("Sdfsdfsdf\n" + "Dfgdfg\n" + "Dfgdfgdfg\n", rt[0]
450: .getText());
451: assertEquals(1, rt[1].getIndentLevel());
452: assertEquals("Sdfsdfs\n" + "Sdfsdf\n", rt[1].getText());
453: }
454: }
|