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.model.*;
026:
027: /**
028: * Tests that SlideShow can re-order slides properly
029: *
030: * @author Nick Burch (nick at torchbox dot com)
031: */
032: public class TestReOrderingSlides extends TestCase {
033: // A SlideShow with one slide
034: private HSLFSlideShow hss_one;
035: private SlideShow ss_one;
036:
037: // A SlideShow with two slides
038: private HSLFSlideShow hss_two;
039: private SlideShow ss_two;
040:
041: // A SlideShow with three slides
042: private HSLFSlideShow hss_three;
043: private SlideShow ss_three;
044:
045: /**
046: * Create/open the slideshows
047: */
048: public void setUp() throws Exception {
049: String dirname = System.getProperty("HSLF.testdata.path");
050:
051: String filename = dirname + "/Single_Coloured_Page.ppt";
052: hss_one = new HSLFSlideShow(filename);
053: ss_one = new SlideShow(hss_one);
054:
055: filename = dirname + "/basic_test_ppt_file.ppt";
056: hss_two = new HSLFSlideShow(filename);
057: ss_two = new SlideShow(hss_two);
058:
059: filename = dirname + "/incorrect_slide_order.ppt";
060: hss_three = new HSLFSlideShow(filename);
061: ss_three = new SlideShow(hss_three);
062: }
063:
064: /**
065: * Test that we can "re-order" a slideshow with only 1 slide on it
066: */
067: public void testReOrder1() throws Exception {
068: // Has one slide
069: assertEquals(1, ss_one.getSlides().length);
070: Slide s1 = ss_one.getSlides()[0];
071:
072: // Check slide 1 is as expected
073: assertEquals(256, s1._getSheetNumber());
074: assertEquals(3, s1._getSheetRefId());
075: assertEquals(1, s1.getSlideNumber());
076:
077: // Now move it to one
078: ss_one.reorderSlide(1, 1);
079:
080: // Write out, and read back in
081: ByteArrayOutputStream baos = new ByteArrayOutputStream();
082: hss_one.write(baos);
083: ByteArrayInputStream bais = new ByteArrayInputStream(baos
084: .toByteArray());
085:
086: HSLFSlideShow hss_read = new HSLFSlideShow(bais);
087: SlideShow ss_read = new SlideShow(hss_read);
088:
089: // Check it still has 1 slide
090: assertEquals(1, ss_read.getSlides().length);
091:
092: // And check it's as expected
093: s1 = ss_read.getSlides()[0];
094: assertEquals(256, s1._getSheetNumber());
095: assertEquals(3, s1._getSheetRefId());
096: assertEquals(1, s1.getSlideNumber());
097: }
098:
099: /**
100: * Test doing a dummy re-order on a slideshow with
101: * two slides in it
102: */
103: public void testReOrder2() throws Exception {
104: // Has two slides
105: assertEquals(2, ss_two.getSlides().length);
106: Slide s1 = ss_two.getSlides()[0];
107: Slide s2 = ss_two.getSlides()[1];
108:
109: // Check slide 1 is as expected
110: assertEquals(256, s1._getSheetNumber());
111: assertEquals(4, s1._getSheetRefId()); // master has notes
112: assertEquals(1, s1.getSlideNumber());
113: // Check slide 2 is as expected
114: assertEquals(257, s2._getSheetNumber());
115: assertEquals(6, s2._getSheetRefId()); // master and 1 have notes
116: assertEquals(2, s2.getSlideNumber());
117:
118: // Don't swap them around
119: ss_two.reorderSlide(2, 2);
120:
121: // Write out, and read back in
122: ByteArrayOutputStream baos = new ByteArrayOutputStream();
123: hss_two.write(baos);
124: ByteArrayInputStream bais = new ByteArrayInputStream(baos
125: .toByteArray());
126:
127: HSLFSlideShow hss_read = new HSLFSlideShow(bais);
128: SlideShow ss_read = new SlideShow(hss_read);
129:
130: // Check it still has 2 slides
131: assertEquals(2, ss_read.getSlides().length);
132:
133: // And check it's as expected
134: s1 = ss_read.getSlides()[0];
135: s2 = ss_read.getSlides()[1];
136: assertEquals(256, s1._getSheetNumber());
137: assertEquals(4, s1._getSheetRefId());
138: assertEquals(1, s1.getSlideNumber());
139: assertEquals(257, s2._getSheetNumber());
140: assertEquals(6, s2._getSheetRefId());
141: assertEquals(2, s2.getSlideNumber());
142: }
143:
144: /**
145: * Test re-ordering slides in a slideshow with 2 slides on it
146: */
147: public void testReOrder2swap() throws Exception {
148: // Has two slides
149: assertEquals(2, ss_two.getSlides().length);
150: Slide s1 = ss_two.getSlides()[0];
151: Slide s2 = ss_two.getSlides()[1];
152:
153: // Check slide 1 is as expected
154: assertEquals(256, s1._getSheetNumber());
155: assertEquals(4, s1._getSheetRefId()); // master has notes
156: assertEquals(1, s1.getSlideNumber());
157: // Check slide 2 is as expected
158: assertEquals(257, s2._getSheetNumber());
159: assertEquals(6, s2._getSheetRefId()); // master and 1 have notes
160: assertEquals(2, s2.getSlideNumber());
161:
162: // Swap them around
163: ss_two.reorderSlide(2, 1);
164:
165: // Write out, and read back in
166: ByteArrayOutputStream baos = new ByteArrayOutputStream();
167: hss_two.write(baos);
168: ByteArrayInputStream bais = new ByteArrayInputStream(baos
169: .toByteArray());
170:
171: HSLFSlideShow hss_read = new HSLFSlideShow(bais);
172: SlideShow ss_read = new SlideShow(hss_read);
173:
174: // Check it still has 2 slides
175: assertEquals(2, ss_read.getSlides().length);
176:
177: // And check it's as expected
178: s1 = ss_read.getSlides()[0];
179: s2 = ss_read.getSlides()[1];
180: assertEquals(257, s1._getSheetNumber());
181: assertEquals(6, s1._getSheetRefId());
182: assertEquals(1, s1.getSlideNumber());
183: assertEquals(256, s2._getSheetNumber());
184: assertEquals(4, s2._getSheetRefId());
185: assertEquals(2, s2.getSlideNumber());
186: }
187:
188: /**
189: * Test doing a dummy re-order on a slideshow with
190: * three slides in it
191: */
192: public void testReOrder3() throws Exception {
193: // Has three slides
194: assertEquals(3, ss_three.getSlides().length);
195: Slide s1 = ss_three.getSlides()[0];
196: Slide s2 = ss_three.getSlides()[1];
197: Slide s3 = ss_three.getSlides()[2];
198:
199: // Check slide 1 is as expected
200: assertEquals(256, s1._getSheetNumber());
201: assertEquals(3, s1._getSheetRefId()); // no notes on master
202: assertEquals(1, s1.getSlideNumber());
203: // Check slide 2 is as expected (was re-ordered from 3)
204: assertEquals(258, s2._getSheetNumber());
205: assertEquals(5, s2._getSheetRefId()); // no notes on slide
206: assertEquals(2, s2.getSlideNumber());
207: // Check slide 3 is as expected (was re-ordered from 2)
208: assertEquals(257, s3._getSheetNumber());
209: assertEquals(4, s3._getSheetRefId()); // no notes on slide
210: assertEquals(3, s3.getSlideNumber());
211:
212: // Don't swap them around
213: ss_three.reorderSlide(2, 2);
214:
215: // Write out, and read back in
216: ByteArrayOutputStream baos = new ByteArrayOutputStream();
217: hss_three.write(baos);
218: ByteArrayInputStream bais = new ByteArrayInputStream(baos
219: .toByteArray());
220:
221: HSLFSlideShow hss_read = new HSLFSlideShow(bais);
222: SlideShow ss_read = new SlideShow(hss_read);
223:
224: // Check it still has 3 slides
225: assertEquals(3, ss_read.getSlides().length);
226:
227: // And check it's as expected
228: s1 = ss_read.getSlides()[0];
229: s2 = ss_read.getSlides()[1];
230: s3 = ss_read.getSlides()[2];
231:
232: assertEquals(256, s1._getSheetNumber());
233: assertEquals(3, s1._getSheetRefId());
234: assertEquals(1, s1.getSlideNumber());
235: assertEquals(258, s2._getSheetNumber());
236: assertEquals(5, s2._getSheetRefId());
237: assertEquals(2, s2.getSlideNumber());
238: assertEquals(257, s3._getSheetNumber());
239: assertEquals(4, s3._getSheetRefId());
240: assertEquals(3, s3.getSlideNumber());
241: }
242:
243: /**
244: * Test re-ordering slides in a slideshow with 3 slides on it
245: */
246: public void testReOrder3swap() throws Exception {
247: // Has three slides
248: assertEquals(3, ss_three.getSlides().length);
249: Slide s1 = ss_three.getSlides()[0];
250: Slide s2 = ss_three.getSlides()[1];
251: Slide s3 = ss_three.getSlides()[2];
252:
253: // Check slide 1 is as expected
254: assertEquals(256, s1._getSheetNumber());
255: assertEquals(3, s1._getSheetRefId()); // no notes on master
256: assertEquals(1, s1.getSlideNumber());
257: // Check slide 2 is as expected (was re-ordered from 3)
258: assertEquals(258, s2._getSheetNumber());
259: assertEquals(5, s2._getSheetRefId()); // no notes on slide
260: assertEquals(2, s2.getSlideNumber());
261: // Check slide 3 is as expected (was re-ordered from 2)
262: assertEquals(257, s3._getSheetNumber());
263: assertEquals(4, s3._getSheetRefId()); // no notes on slide
264: assertEquals(3, s3.getSlideNumber());
265:
266: // Put 3 in place of 1
267: // (1 -> 2, 2 -> 3)
268: ss_three.reorderSlide(3, 1);
269:
270: // Write out, and read back in
271: ByteArrayOutputStream baos = new ByteArrayOutputStream();
272: hss_three.write(baos);
273: ByteArrayInputStream bais = new ByteArrayInputStream(baos
274: .toByteArray());
275:
276: HSLFSlideShow hss_read = new HSLFSlideShow(bais);
277: SlideShow ss_read = new SlideShow(hss_read);
278:
279: // Check it still has 3 slides
280: assertEquals(3, ss_read.getSlides().length);
281:
282: // And check it's as expected
283: s1 = ss_read.getSlides()[0];
284: s2 = ss_read.getSlides()[1];
285: s3 = ss_read.getSlides()[2];
286:
287: assertEquals(257, s1._getSheetNumber());
288: assertEquals(4, s1._getSheetRefId());
289: assertEquals(1, s1.getSlideNumber());
290: assertEquals(256, s2._getSheetNumber());
291: assertEquals(3, s2._getSheetRefId());
292: assertEquals(2, s2.getSlideNumber());
293: assertEquals(258, s3._getSheetNumber());
294: assertEquals(5, s3._getSheetRefId());
295: assertEquals(3, s3.getSlideNumber());
296: }
297: }
|