1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 package fvt.uno.sd.slideshow;
22 
23 import junit.framework.Assert;
24 
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.openoffice.test.common.FileUtil;
29 import org.openoffice.test.common.Testspace;
30 import org.openoffice.test.uno.UnoApp;
31 
32 import testlib.uno.PageUtil;
33 import testlib.uno.ShapeUtil;
34 
35 import com.sun.star.awt.Point;
36 import com.sun.star.awt.Size;
37 import com.sun.star.beans.PropertyValue;
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.drawing.XDrawPage;
40 import com.sun.star.drawing.XDrawPages;
41 import com.sun.star.drawing.XDrawPagesSupplier;
42 import com.sun.star.drawing.XShape;
43 import com.sun.star.drawing.XShapes;
44 import com.sun.star.frame.XStorable;
45 import com.sun.star.lang.XComponent;
46 import com.sun.star.presentation.XPresentation;
47 import com.sun.star.presentation.XPresentationSupplier;
48 import com.sun.star.uno.UnoRuntime;
49 
50 public class SlideShow {
51 	XPresentationSupplier sdDocument = null;
52 	XPresentation pre = null;
53 	XComponent precomp = null;
54 	XComponent impressDocument = null;
55 	XComponent reLoadFile = null;
56 	XDrawPagesSupplier drawsupplier = null;
57 	XDrawPages drawpages = null;
58 
59 	String filePath = null;
60 
61 	UnoApp unoApp = new UnoApp();
62 
63 	/**
64 	 * @throws java.lang.Exception
65 	 */
66 	@Before
setUp()67 	public void setUp() throws Exception {
68 		unoApp.start();
69 		createDocumentAndSlide();
70 	}
71 
72 	@After
tearDown()73 	public void tearDown() throws Exception {
74 		unoApp.closeDocument(impressDocument);
75 		unoApp.closeDocument(reLoadFile);
76 		unoApp.close();
77 	}
78 
79 	@Test
testSlideShow()80 	public void testSlideShow() throws Exception {
81 		Point po = new Point(5000, 5000);
82 
83 		XDrawPage xPage1 = createSlide(0);
84 		XShapes xShapes1 = (XShapes) UnoRuntime.queryInterface(XShapes.class,
85 				xPage1);
86 		XShape xRectangle1 = ShapeUtil.createShape(impressDocument, po,
87 				new Size(21000, 12500), "com.sun.star.drawing.RectangleShape");
88 		xShapes1.add(xRectangle1);
89 		ShapeUtil.addPortion(xRectangle1, "Page1", false);
90 
91 		XDrawPage xPage2 = createSlide(1);
92 		XShapes xShapes2 = (XShapes) UnoRuntime.queryInterface(XShapes.class,
93 				xPage2);
94 		XShape xRectangle2 = ShapeUtil.createShape(impressDocument, po,
95 				new Size(21000, 12500), "com.sun.star.drawing.RectangleShape");
96 		xShapes2.add(xRectangle2);
97 		ShapeUtil.addPortion(xRectangle2, "Page2", false);
98 
99 		XDrawPage xPage3 = createSlide(2);
100 		XShapes xShapes3 = (XShapes) UnoRuntime.queryInterface(XShapes.class,
101 				xPage3);
102 		XShape xRectangle3 = ShapeUtil.createShape(impressDocument, po,
103 				new Size(21000, 12500), "com.sun.star.drawing.RectangleShape");
104 		xShapes3.add(xRectangle3);
105 		ShapeUtil.addPortion(xRectangle3, "Page3", false);
106 
107 		XPropertySet xPresPropSet = (XPropertySet) UnoRuntime.queryInterface(
108 				XPropertySet.class, pre);
109 		xPresPropSet.setPropertyValue("IsEndless", Boolean.FALSE);
110 		xPresPropSet.setPropertyValue("IsFullScreen", Boolean.TRUE);
111 		xPresPropSet.setPropertyValue("Pause", new Integer(0));
112 
113 		saveAndLoadSlide();
114 
115 		Assert.assertEquals("IsEndless set to false",
116 				xPresPropSet.getPropertyValue("IsEndless"), Boolean.FALSE);
117 		Assert.assertEquals("IsFullScreen set to true",
118 				xPresPropSet.getPropertyValue("IsFullScreen"), Boolean.TRUE);
119 		Assert.assertEquals("Pause set to 0",
120 				xPresPropSet.getPropertyValue("Pause"), 0);
121 	}
122 
createSlide(int index)123 	public XDrawPage createSlide(int index) throws Exception {
124 		drawpages.insertNewByIndex(index);
125 		XDrawPage xPage = PageUtil.getDrawPageByIndex(impressDocument, index);
126 		return xPage;
127 	}
128 
129 	/**
130 	 * create a new presentation document and insert a new slide.
131 	 *
132 	 * @throws Exception
133 	 */
createDocumentAndSlide()134 	public void createDocumentAndSlide() throws Exception {
135 		impressDocument = (XComponent) UnoRuntime.queryInterface(
136 				XComponent.class, unoApp.newDocument("simpress"));
137 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
138 				XDrawPagesSupplier.class, impressDocument);
139 		drawpages = drawsupplier.getDrawPages();
140 		sdDocument = (XPresentationSupplier) UnoRuntime.queryInterface(
141 				XPresentationSupplier.class, impressDocument);
142 		pre = sdDocument.getPresentation();
143 	}
144 
145 	/**
146 	 * Save presentation and reLoad the slide.
147 	 *
148 	 * @param no
149 	 * @return void
150 	 * @throws Exception
151 	 */
saveAndLoadSlide()152 	public void saveAndLoadSlide() throws Exception {
153 		reLoadFile = saveAndReloadDoc(impressDocument,
154 				"StarOffice XML (Impress)", "odp");
155 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
156 				XDrawPagesSupplier.class, reLoadFile);
157 		drawpages = drawsupplier.getDrawPages();
158 
159 		sdDocument = (XPresentationSupplier) UnoRuntime.queryInterface(
160 				XPresentationSupplier.class, reLoadFile);
161 		pre = sdDocument.getPresentation();
162 	}
163 
164 	/**
165 	 * save and reload Presentation document.
166 	 *
167 	 * @param presentationDocument
168 	 * @param sFilter
169 	 * @param sExtension
170 	 * @return
171 	 * @throws Exception
172 	 */
saveAndReloadDoc(XComponent presentationDocument, String sFilter, String sExtension)173 	private XComponent saveAndReloadDoc(XComponent presentationDocument,
174 			String sFilter, String sExtension) throws Exception {
175 		filePath = Testspace.getPath("tmp/slideshow." + sExtension);
176 		PropertyValue[] aStoreProperties = new PropertyValue[2];
177 		aStoreProperties[0] = new PropertyValue();
178 		aStoreProperties[1] = new PropertyValue();
179 		aStoreProperties[0].Name = "Override";
180 		aStoreProperties[0].Value = true;
181 		aStoreProperties[1].Name = "FilterName";
182 		aStoreProperties[1].Value = sFilter;
183 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
184 				XStorable.class, presentationDocument);
185 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
186 
187 		return (XComponent) UnoRuntime.queryInterface(XComponent.class,
188 				unoApp.loadDocument(filePath));
189 	}
190 }
191