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: import org.apache.poi.hslf.usermodel.SlideShow;
022: import org.apache.poi.hslf.usermodel.RichTextRun;
023: import org.apache.poi.hslf.HSLFSlideShow;
024: import org.apache.poi.hslf.model.textproperties.CharFlagsTextProp;
025: import org.apache.poi.hslf.record.*;
026: import org.apache.poi.hslf.record.StyleTextPropAtom.*;
027:
028: import java.io.ByteArrayOutputStream;
029: import java.io.ByteArrayInputStream;
030: import java.awt.*;
031:
032: /**
033: * Tests for SlideMaster
034: *
035: * @author Yegor Kozlov
036: */
037: public class TestSlideMaster extends TestCase {
038: String home;
039:
040: public void setUp() throws Exception {
041: home = System.getProperty("HSLF.testdata.path");
042: }
043:
044: /**
045: * The reference ppt has two masters.
046: * Check we can read their attributes.
047: */
048: public void testSlideMaster() throws Exception {
049: SlideShow ppt = new SlideShow(new HSLFSlideShow(home
050: + "/slide_master.ppt"));
051:
052: Environment env = ppt.getDocumentRecord().getEnvironment();
053:
054: SlideMaster[] master = ppt.getSlidesMasters();
055: assertEquals(2, master.length);
056:
057: //character attributes
058: assertEquals(40, master[0].getStyleAttribute(
059: TextHeaderAtom.TITLE_TYPE, 0, "font.size", true)
060: .getValue());
061: assertEquals(48, master[1].getStyleAttribute(
062: TextHeaderAtom.TITLE_TYPE, 0, "font.size", true)
063: .getValue());
064:
065: int font1 = master[0].getStyleAttribute(
066: TextHeaderAtom.TITLE_TYPE, 0, "font.index", true)
067: .getValue();
068: int font2 = master[1].getStyleAttribute(
069: TextHeaderAtom.TITLE_TYPE, 0, "font.index", true)
070: .getValue();
071: assertEquals("Arial", env.getFontCollection().getFontWithId(
072: font1));
073: assertEquals("Georgia", env.getFontCollection().getFontWithId(
074: font2));
075:
076: CharFlagsTextProp prop1 = (CharFlagsTextProp) master[0]
077: .getStyleAttribute(TextHeaderAtom.TITLE_TYPE, 0,
078: "char_flags", true);
079: assertEquals(false, prop1
080: .getSubValue(CharFlagsTextProp.BOLD_IDX));
081: assertEquals(false, prop1
082: .getSubValue(CharFlagsTextProp.ITALIC_IDX));
083: assertEquals(true, prop1
084: .getSubValue(CharFlagsTextProp.UNDERLINE_IDX));
085:
086: CharFlagsTextProp prop2 = (CharFlagsTextProp) master[1]
087: .getStyleAttribute(TextHeaderAtom.TITLE_TYPE, 0,
088: "char_flags", true);
089: assertEquals(false, prop2
090: .getSubValue(CharFlagsTextProp.BOLD_IDX));
091: assertEquals(true, prop2
092: .getSubValue(CharFlagsTextProp.ITALIC_IDX));
093: assertEquals(false, prop2
094: .getSubValue(CharFlagsTextProp.UNDERLINE_IDX));
095:
096: //now paragraph attributes
097: assertEquals(0x266B, master[0].getStyleAttribute(
098: TextHeaderAtom.BODY_TYPE, 0, "bullet.char", false)
099: .getValue());
100: assertEquals(0x2022, master[1].getStyleAttribute(
101: TextHeaderAtom.BODY_TYPE, 0, "bullet.char", false)
102: .getValue());
103:
104: int b1 = master[0].getStyleAttribute(TextHeaderAtom.BODY_TYPE,
105: 0, "bullet.font", false).getValue();
106: int b2 = master[1].getStyleAttribute(TextHeaderAtom.BODY_TYPE,
107: 0, "bullet.font", false).getValue();
108: assertEquals("Arial", env.getFontCollection().getFontWithId(b1));
109: assertEquals("Georgia", env.getFontCollection().getFontWithId(
110: b2));
111: }
112:
113: /**
114: * Test we can read default text attributes for a title master sheet
115: */
116: public void testTitleMasterTextAttributes() throws Exception {
117: SlideShow ppt = new SlideShow(new HSLFSlideShow(home
118: + "/slide_master.ppt"));
119: TitleMaster[] master = ppt.getTitleMasters();
120: assertEquals(1, master.length);
121:
122: assertEquals(32, master[0].getStyleAttribute(
123: TextHeaderAtom.CENTER_TITLE_TYPE, 0, "font.size", true)
124: .getValue());
125: CharFlagsTextProp prop1 = (CharFlagsTextProp) master[0]
126: .getStyleAttribute(TextHeaderAtom.CENTER_TITLE_TYPE, 0,
127: "char_flags", true);
128: assertEquals(true, prop1
129: .getSubValue(CharFlagsTextProp.BOLD_IDX));
130: assertEquals(false, prop1
131: .getSubValue(CharFlagsTextProp.ITALIC_IDX));
132: assertEquals(true, prop1
133: .getSubValue(CharFlagsTextProp.UNDERLINE_IDX));
134:
135: assertEquals(20, master[0].getStyleAttribute(
136: TextHeaderAtom.CENTRE_BODY_TYPE, 0, "font.size", true)
137: .getValue());
138: CharFlagsTextProp prop2 = (CharFlagsTextProp) master[0]
139: .getStyleAttribute(TextHeaderAtom.CENTRE_BODY_TYPE, 0,
140: "char_flags", true);
141: assertEquals(true, prop2
142: .getSubValue(CharFlagsTextProp.BOLD_IDX));
143: assertEquals(false, prop2
144: .getSubValue(CharFlagsTextProp.ITALIC_IDX));
145: assertEquals(false, prop2
146: .getSubValue(CharFlagsTextProp.UNDERLINE_IDX));
147: }
148:
149: /**
150: * Slide 3 has title layout and follows the TitleMaster. Verify that.
151: */
152: public void testTitleMaster() throws Exception {
153: SlideShow ppt = new SlideShow(new HSLFSlideShow(home
154: + "/slide_master.ppt"));
155: Slide slide = ppt.getSlides()[2];
156: MasterSheet masterSheet = slide.getMasterSheet();
157: assertTrue(masterSheet instanceof TitleMaster);
158:
159: TextRun[] txt = slide.getTextRuns();
160: for (int i = 0; i < txt.length; i++) {
161: RichTextRun rt = txt[i].getRichTextRuns()[0];
162: switch (txt[i].getRunType()) {
163: case TextHeaderAtom.CENTER_TITLE_TYPE:
164: assertEquals("Arial", rt.getFontName());
165: assertEquals(32, rt.getFontSize());
166: assertEquals(true, rt.isBold());
167: assertEquals(true, rt.isUnderlined());
168: break;
169: case TextHeaderAtom.CENTRE_BODY_TYPE:
170: assertEquals("Courier New", rt.getFontName());
171: assertEquals(20, rt.getFontSize());
172: assertEquals(true, rt.isBold());
173: assertEquals(false, rt.isUnderlined());
174: break;
175: }
176:
177: }
178: }
179:
180: /**
181: * If a style attribute is not set ensure it is read from the master
182: */
183: public void testMasterAttributes() throws Exception {
184: SlideShow ppt = new SlideShow(new HSLFSlideShow(home
185: + "/slide_master.ppt"));
186: Slide[] slide = ppt.getSlides();
187: assertEquals(3, slide.length);
188: TextRun[] trun;
189:
190: trun = slide[0].getTextRuns();
191: for (int i = 0; i < trun.length; i++) {
192: if (trun[i].getRunType() == TextHeaderAtom.TITLE_TYPE) {
193: RichTextRun rt = trun[i].getRichTextRuns()[0];
194: assertEquals(40, rt.getFontSize());
195: assertEquals(true, rt.isUnderlined());
196: assertEquals("Arial", rt.getFontName());
197: } else if (trun[i].getRunType() == TextHeaderAtom.BODY_TYPE) {
198: RichTextRun rt;
199: rt = trun[i].getRichTextRuns()[0];
200: assertEquals(0, rt.getIndentLevel());
201: assertEquals(32, rt.getFontSize());
202: assertEquals("Arial", rt.getFontName());
203:
204: rt = trun[i].getRichTextRuns()[1];
205: assertEquals(1, rt.getIndentLevel());
206: assertEquals(28, rt.getFontSize());
207: assertEquals("Arial", rt.getFontName());
208:
209: }
210: }
211:
212: trun = slide[1].getTextRuns();
213: for (int i = 0; i < trun.length; i++) {
214: if (trun[i].getRunType() == TextHeaderAtom.TITLE_TYPE) {
215: RichTextRun rt = trun[i].getRichTextRuns()[0];
216: assertEquals(48, rt.getFontSize());
217: assertEquals(true, rt.isItalic());
218: assertEquals("Georgia", rt.getFontName());
219: } else if (trun[i].getRunType() == TextHeaderAtom.BODY_TYPE) {
220: RichTextRun rt;
221: rt = trun[i].getRichTextRuns()[0];
222: assertEquals(0, rt.getIndentLevel());
223: assertEquals(32, rt.getFontSize());
224: assertEquals("Courier New", rt.getFontName());
225: }
226: }
227:
228: }
229:
230: /**
231: * Check we can dynamically assign a slide master to a slide.
232: */
233: public void testChangeSlideMaster() throws Exception {
234: SlideShow ppt = new SlideShow(new HSLFSlideShow(home
235: + "/slide_master.ppt"));
236: SlideMaster[] master = ppt.getSlidesMasters();
237: Slide[] slide = ppt.getSlides();
238: int sheetNo;
239:
240: //each slide uses its own master
241: assertEquals(slide[0].getMasterSheet()._getSheetNumber(),
242: master[0]._getSheetNumber());
243: assertEquals(slide[1].getMasterSheet()._getSheetNumber(),
244: master[1]._getSheetNumber());
245:
246: //all slides use the first master slide
247: sheetNo = master[0]._getSheetNumber();
248: for (int i = 0; i < slide.length; i++) {
249: slide[i].setMasterSheet(master[0]);
250: }
251:
252: ByteArrayOutputStream out;
253:
254: out = new ByteArrayOutputStream();
255: ppt.write(out);
256: out.close();
257:
258: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
259: out.toByteArray())));
260: master = ppt.getSlidesMasters();
261: slide = ppt.getSlides();
262: for (int i = 0; i < slide.length; i++) {
263: assertEquals(sheetNo, slide[i].getMasterSheet()
264: ._getSheetNumber());
265: }
266: }
267:
268: /**
269: * Varify we can read attrubutes for different identtation levels.
270: * (typical for the "bullted body" placeholder)
271: */
272: public void testIndentation() throws Exception {
273: SlideShow ppt = new SlideShow(new HSLFSlideShow(home
274: + "/slide_master.ppt"));
275: Slide slide = ppt.getSlides()[0];
276: TextRun[] trun;
277:
278: trun = slide.getTextRuns();
279: for (int i = 0; i < trun.length; i++) {
280: if (trun[i].getRunType() == TextHeaderAtom.TITLE_TYPE) {
281: RichTextRun rt = trun[i].getRichTextRuns()[0];
282: assertEquals(40, rt.getFontSize());
283: assertEquals(true, rt.isUnderlined());
284: assertEquals("Arial", rt.getFontName());
285: } else if (trun[i].getRunType() == TextHeaderAtom.BODY_TYPE) {
286: RichTextRun[] rt = trun[i].getRichTextRuns();
287: for (int j = 0; j < rt.length; j++) {
288: int indent = rt[j].getIndentLevel();
289: switch (indent) {
290: case 0:
291: assertEquals(32, rt[j].getFontSize());
292: break;
293: case 1:
294: assertEquals(28, rt[j].getFontSize());
295: break;
296: case 2:
297: assertEquals(24, rt[j].getFontSize());
298: break;
299: }
300: }
301: }
302: }
303:
304: }
305:
306: }
|