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.usermodel;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022:
023: import junit.framework.TestCase;
024: import org.apache.poi.hslf.*;
025: import org.apache.poi.hslf.record.Record;
026: import org.apache.poi.hslf.record.RecordTypes;
027: import org.apache.poi.hslf.record.UserEditAtom;
028: import org.apache.poi.hslf.model.*;
029:
030: /**
031: * Tests that SlideShow adds additional sheets properly
032: *
033: * @author Nick Burch (nick at torchbox dot com)
034: */
035: public class TestAddingSlides extends TestCase {
036: // An empty SlideShow
037: private HSLFSlideShow hss_empty;
038: private SlideShow ss_empty;
039:
040: // A SlideShow with one slide
041: private HSLFSlideShow hss_one;
042: private SlideShow ss_one;
043:
044: // A SlideShow with two slides
045: private HSLFSlideShow hss_two;
046: private SlideShow ss_two;
047:
048: /**
049: * Create/open the slideshows
050: */
051: public void setUp() throws Exception {
052: hss_empty = new HSLFSlideShow();
053: ss_empty = new SlideShow(hss_empty);
054:
055: String dirname = System.getProperty("HSLF.testdata.path");
056:
057: String filename = dirname + "/Single_Coloured_Page.ppt";
058: hss_one = new HSLFSlideShow(filename);
059: ss_one = new SlideShow(hss_one);
060:
061: filename = dirname + "/basic_test_ppt_file.ppt";
062: hss_two = new HSLFSlideShow(filename);
063: ss_two = new SlideShow(hss_two);
064: }
065:
066: /**
067: * Test adding a slide to an empty slideshow
068: */
069: public void testAddSlideToEmpty() throws Exception {
070: // Doesn't have any slides
071: assertEquals(0, ss_empty.getSlides().length);
072:
073: // Should only have a master SLWT
074: assertEquals(1, ss_empty.getDocumentRecord()
075: .getSlideListWithTexts().length);
076:
077: //grab UserEditAtom
078: UserEditAtom usredit = null;
079: Record[] _records = hss_empty.getRecords();
080: for (int i = 0; i < _records.length; i++) {
081: Record record = _records[i];
082: if (_records[i].getRecordType() == RecordTypes.UserEditAtom.typeID) {
083: usredit = (UserEditAtom) _records[i];
084: }
085: }
086: assertNotNull(usredit);
087:
088: // Add one
089: Slide slide = ss_empty.createSlide();
090: assertEquals(1, ss_empty.getSlides().length);
091: assertEquals(256, slide._getSheetNumber());
092: assertEquals(3, slide._getSheetRefId());
093: assertEquals(1, slide.getSlideNumber());
094: assertEquals(usredit.getMaxPersistWritten(), slide
095: ._getSheetRefId());
096:
097: // Write out, and read back in
098: ByteArrayOutputStream baos = new ByteArrayOutputStream();
099: hss_empty.write(baos);
100: ByteArrayInputStream bais = new ByteArrayInputStream(baos
101: .toByteArray());
102:
103: HSLFSlideShow hss_read = new HSLFSlideShow(bais);
104: SlideShow ss_read = new SlideShow(hss_read);
105:
106: // Check it now has a slide
107: assertEquals(1, ss_read.getSlides().length);
108:
109: // Check it now has two SLWTs
110: assertEquals(2, ss_empty.getDocumentRecord()
111: .getSlideListWithTexts().length);
112:
113: // And check it's as expected
114: slide = ss_read.getSlides()[0];
115: assertEquals(256, slide._getSheetNumber());
116: assertEquals(3, slide._getSheetRefId());
117: assertEquals(1, slide.getSlideNumber());
118: }
119:
120: /**
121: * Test adding a slide to an existing slideshow
122: */
123: public void testAddSlideToExisting() throws Exception {
124: // Has one slide
125: assertEquals(1, ss_one.getSlides().length);
126: Slide s1 = ss_one.getSlides()[0];
127:
128: // Should have two SLTWs
129: assertEquals(2, ss_one.getDocumentRecord()
130: .getSlideListWithTexts().length);
131:
132: // Check slide 1 is as expected
133: assertEquals(256, s1._getSheetNumber());
134: assertEquals(3, s1._getSheetRefId());
135: assertEquals(1, s1.getSlideNumber());
136:
137: // Add a second one
138: Slide s2 = ss_one.createSlide();
139: assertEquals(2, ss_one.getSlides().length);
140: assertEquals(257, s2._getSheetNumber());
141: assertEquals(4, s2._getSheetRefId());
142: assertEquals(2, s2.getSlideNumber());
143:
144: // Write out, and read back in
145: ByteArrayOutputStream baos = new ByteArrayOutputStream();
146: hss_one.write(baos);
147: ByteArrayInputStream bais = new ByteArrayInputStream(baos
148: .toByteArray());
149:
150: HSLFSlideShow hss_read = new HSLFSlideShow(bais);
151: SlideShow ss_read = new SlideShow(hss_read);
152:
153: // Check it now has two slides
154: assertEquals(2, ss_read.getSlides().length);
155:
156: // Should still have two SLTWs
157: assertEquals(2, ss_read.getDocumentRecord()
158: .getSlideListWithTexts().length);
159:
160: // And check it's as expected
161: s1 = ss_read.getSlides()[0];
162: s2 = ss_read.getSlides()[1];
163: assertEquals(256, s1._getSheetNumber());
164: assertEquals(3, s1._getSheetRefId());
165: assertEquals(1, s1.getSlideNumber());
166: assertEquals(257, s2._getSheetNumber());
167: assertEquals(4, s2._getSheetRefId());
168: assertEquals(2, s2.getSlideNumber());
169: }
170:
171: /**
172: * Test adding a slide to an existing slideshow,
173: * with two slides already
174: */
175: public void testAddSlideToExisting2() throws Exception {
176: //grab UserEditAtom
177: UserEditAtom usredit = null;
178: Record[] _records = hss_two.getRecords();
179: for (int i = 0; i < _records.length; i++) {
180: Record record = _records[i];
181: if (_records[i].getRecordType() == RecordTypes.UserEditAtom.typeID) {
182: usredit = (UserEditAtom) _records[i];
183: }
184: }
185: assertNotNull(usredit);
186:
187: // Has two slides
188: assertEquals(2, ss_two.getSlides().length);
189: Slide s1 = ss_two.getSlides()[0];
190: Slide s2 = ss_two.getSlides()[1];
191:
192: // Check slide 1 is as expected
193: assertEquals(256, s1._getSheetNumber());
194: assertEquals(4, s1._getSheetRefId()); // master has notes
195: assertEquals(1, s1.getSlideNumber());
196: // Check slide 2 is as expected
197: assertEquals(257, s2._getSheetNumber());
198: assertEquals(6, s2._getSheetRefId()); // master and 1 have notes
199: assertEquals(2, s2.getSlideNumber());
200:
201: // Add a third one
202: Slide s3 = ss_two.createSlide();
203: assertEquals(3, ss_two.getSlides().length);
204: assertEquals(258, s3._getSheetNumber());
205: assertEquals(8, s3._getSheetRefId()); // lots of notes before us
206: assertEquals(3, s3.getSlideNumber());
207: assertEquals(usredit.getMaxPersistWritten(), s3
208: ._getSheetRefId());
209:
210: // Write out, and read back in
211: ByteArrayOutputStream baos = new ByteArrayOutputStream();
212: hss_two.write(baos);
213: ByteArrayInputStream bais = new ByteArrayInputStream(baos
214: .toByteArray());
215:
216: HSLFSlideShow hss_read = new HSLFSlideShow(bais);
217: SlideShow ss_read = new SlideShow(hss_read);
218:
219: // Check it now has three slides
220: assertEquals(3, ss_read.getSlides().length);
221:
222: // And check it's as expected
223: s1 = ss_read.getSlides()[0];
224: s2 = ss_read.getSlides()[1];
225: s3 = ss_read.getSlides()[2];
226: assertEquals(256, s1._getSheetNumber());
227: assertEquals(4, s1._getSheetRefId());
228: assertEquals(1, s1.getSlideNumber());
229: assertEquals(257, s2._getSheetNumber());
230: assertEquals(6, s2._getSheetRefId());
231: assertEquals(2, s2.getSlideNumber());
232: assertEquals(258, s3._getSheetNumber());
233: assertEquals(8, s3._getSheetRefId());
234: assertEquals(3, s3.getSlideNumber());
235: }
236: }
|