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.animation;
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.ShapeUtil;
33 
34 import com.sun.star.awt.Point;
35 import com.sun.star.awt.Size;
36 import com.sun.star.beans.PropertyValue;
37 import com.sun.star.beans.XPropertySet;
38 import com.sun.star.container.XIndexContainer;
39 import com.sun.star.container.XNameContainer;
40 import com.sun.star.container.XNamed;
41 import com.sun.star.drawing.XDrawPage;
42 import com.sun.star.drawing.XDrawPages;
43 import com.sun.star.drawing.XDrawPagesSupplier;
44 import com.sun.star.drawing.XShape;
45 import com.sun.star.drawing.XShapes;
46 import com.sun.star.frame.XStorable;
47 import com.sun.star.lang.XComponent;
48 import com.sun.star.lang.XSingleServiceFactory;
49 import com.sun.star.presentation.AnimationEffect;
50 import com.sun.star.presentation.XCustomPresentationSupplier;
51 import com.sun.star.presentation.XPresentation;
52 import com.sun.star.presentation.XPresentationSupplier;
53 import com.sun.star.table.XCell;
54 import com.sun.star.text.ControlCharacter;
55 import com.sun.star.text.XText;
56 import com.sun.star.text.XTextCursor;
57 import com.sun.star.text.XTextRange;
58 import com.sun.star.uno.UnoRuntime;
59 
60 public class CustomAnimation {
61 	XPresentationSupplier sdDocument = null;
62 	XPresentation pre = null;
63 	XComponent precomp = null;
64 	XComponent impressDocument = null;
65 	XComponent reLoadFile = null;
66 	XDrawPagesSupplier drawsupplier = null;
67 	XDrawPages drawpages = null;
68 
69 	String filePath = null;
70 
71 	UnoApp unoApp = new UnoApp();
72 
73 	/**
74 	 * @throws java.lang.Exception
75 	 */
76 	@Before
setUp()77 	public void setUp() throws Exception {
78 		unoApp.start();
79 		createDocumentAndSlide();
80 	}
81 
82 	@After
tearDown()83 	public void tearDown() throws Exception {
84 		unoApp.closeDocument(impressDocument);
85 		unoApp.closeDocument(reLoadFile);
86 		unoApp.close();
87 	}
88 
89 	@Test
testShapeAnimation()90 	public void testShapeAnimation() throws Exception {
91 
92 		XShapes xShapes = null;
93 		XPropertySet xShapePropSet[] = new XPropertySet[23];
94 
95 		drawpages.insertNewByIndex(0);
96 
97 		String aNameArray[] = { "Page 1", "Page 2", "Page 3", "Page 4",
98 				"Page 5", "Page 6", "Page 7", "Page 8", "Page 9", "Page 10",
99 				"Page 11", "Page 12", "Page 13", "Page 14", "Page 15",
100 				"Page 16", "Page 17", "Page 18", "Page 19", "Page 20",
101 				"Page 21", "Page 22", "Page 23", };
102 		AnimationEffect AnimationEffectArray[] = {
103 				AnimationEffect.NONE,
104 				AnimationEffect.FADE_FROM_LEFT,
105 				AnimationEffect.FADE_FROM_TOP,
106 				AnimationEffect.FADE_FROM_RIGHT,
107 				AnimationEffect.FADE_FROM_BOTTOM,
108 				AnimationEffect.FADE_TO_CENTER,
109 				AnimationEffect.FADE_FROM_CENTER,
110 				AnimationEffect.MOVE_FROM_LEFT,
111 				AnimationEffect.MOVE_FROM_TOP,
112 				AnimationEffect.MOVE_FROM_RIGHT,
113 				AnimationEffect.MOVE_FROM_BOTTOM,
114 				AnimationEffect.VERTICAL_STRIPES,
115 				AnimationEffect.HORIZONTAL_STRIPES,
116 				AnimationEffect.CLOCKWISE,
117 				AnimationEffect.COUNTERCLOCKWISE,
118 				AnimationEffect.FADE_FROM_UPPERLEFT,
119 				AnimationEffect.FADE_FROM_UPPERRIGHT,
120 				AnimationEffect.FADE_FROM_LOWERLEFT,
121 				AnimationEffect.FADE_FROM_LOWERRIGHT,
122 				AnimationEffect.CLOSE_VERTICAL,
123 				AnimationEffect.CLOSE_HORIZONTAL,
124 				AnimationEffect.OPEN_VERTICAL,
125 				AnimationEffect.OPEN_HORIZONTAL,
126 				AnimationEffect.PATH,
127 /*				AnimationEffect.MOVE_TO_LEFT,
128 				AnimationEffect.MOVE_TO_TOP,
129 				AnimationEffect.MOVE_TO_RIGHT,
130 				AnimationEffect.MOVE_TO_BOTTOM,
131 				AnimationEffect.SPIRALIN_LEFT,
132 				AnimationEffect.SPIRALIN_RIGHT,
133 				AnimationEffect.SPIRALOUT_LEFT,
134 				AnimationEffect.SPIRALOUT_RIGHT,
135 				AnimationEffect.DISSOLVE,
136 				AnimationEffect.WAVYLINE_FROM_LEFT,
137 				AnimationEffect.RANDOM,
138 				AnimationEffect.VERTICAL_LINES,
139 				AnimationEffect.LASER_FROM_LEFT,
140 				AnimationEffect.APPEAR,
141 				AnimationEffect.HIDE,
142 				AnimationEffect.MOVE_FROM_UPPERLEFT,
143 				AnimationEffect.VERTICAL_CHECKERBOARD,
144 				AnimationEffect.HORIZONTAL_ROTATE,
145 				AnimationEffect.VERTICAL_STRETCH,
146 				AnimationEffect.ZOOM_IN,
147 				AnimationEffect.ZOOM_OUT_FROM_LEFT, */
148 				};
149 
150 		// insert  pages
151 		while (drawpages.getCount() < aNameArray.length)
152 			drawpages.insertNewByIndex(0);
153 
154 		// add text shape to each page
155 		for (int i = 0; i < aNameArray.length; i++) {
156 			XDrawPage xDrawPage = (XDrawPage) UnoRuntime.queryInterface(
157 					XDrawPage.class, drawpages.getByIndex(i));
158 			XNamed xPageName = (XNamed) UnoRuntime.queryInterface(XNamed.class,
159 					xDrawPage);
160 			xPageName.setName(aNameArray[i]);
161 
162 			xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class,
163 					drawpages.getByIndex(i));
164 
165 			// create a rectangle that is placed on the top left of the page
166 			xShapePropSet[i] = ShapeUtil.createAndInsertShape(impressDocument,
167 					xShapes, new Point(1000, 1000), new Size(5000, 5000),
168 					"com.sun.star.drawing.RectangleShape");
169 		}
170 
171 		for (int i = 0; i < aNameArray.length; i++) {
172 			// set an object animation for each slide
173 			xShapePropSet[i]
174 					.setPropertyValue("Effect", AnimationEffectArray[i]);
175 		}
176 
177 		// If this property and the property DimPrevious are both true, the shape is hidden on the next click after the AnimationEffect has finished.
178 		xShapePropSet[1].setPropertyValue("DimHide", new Boolean(false));
179 		// If true, this shape is painted using DimColor on the next click after finishing the AnimationEffect.
180 		xShapePropSet[1].setPropertyValue("DimPrevious", new Boolean(true));
181 		// his color is used to paint the shape on the next click after the animation effect has finished
182 		xShapePropSet[1].setPropertyValue("DimColor", new Integer(0xff0000));
183 		// the sound file specified in Sound is played while the animation effect is executed
184 		xShapePropSet[1].setPropertyValue("SoundOn", new Boolean(false));
185 		// the sound specified in the Sound property of this shape is played completely
186 		xShapePropSet[1].setPropertyValue("PlayFull", new Boolean(false));
187 
188 		saveAndLoadSlide();
189 
190 		for (int i = 0; i < aNameArray.length; i++) {
191 			// assert if Shape Animation is set
192 			Assert.assertEquals(
193 					"Text Animation Effect is AnimationEffectArray[]",
194 					AnimationEffectArray[i],
195 					xShapePropSet[i].getPropertyValue("Effect"));
196 		}
197 		// assert if DimHide is false
198 		Assert.assertEquals("Shape Animation DimHide is false", Boolean.FALSE,
199 				xShapePropSet[1].getPropertyValue("DimHide"));
200 		// assert if DimPrevious is true
201 		Assert.assertEquals("Shape Animation DimPrevious is true",
202 				Boolean.TRUE, xShapePropSet[1].getPropertyValue("DimPrevious"));
203 		// assert if DimColor is red
204 		Assert.assertEquals("Shape Animation DimColor is Red", 0xff0000,
205 				xShapePropSet[1].getPropertyValue("DimColor"));
206 		// assert the sound file specified in Sound don't play while the
207 		// animation effect is executed.
208 		Assert.assertEquals("Shape SoundOn is false", Boolean.FALSE,
209 				xShapePropSet[1].getPropertyValue("SoundOn"));
210 		// assert the sound specified in the Sound property of this shape is
211 		// played completely
212 		Assert.assertEquals("Shape PlayFull is false", Boolean.FALSE,
213 				xShapePropSet[1].getPropertyValue("PlayFull"));
214 
215 	}
216 
addPortion(XShape xShape, String sText, boolean bNewParagraph)217 	public XPropertySet addPortion(XShape xShape, String sText,
218 			boolean bNewParagraph)
219 			throws com.sun.star.lang.IllegalArgumentException {
220 		XText xText = (XText) UnoRuntime.queryInterface(XText.class, xShape);
221 
222 		XTextCursor xTextCursor = xText.createTextCursor();
223 		xTextCursor.gotoEnd(false);
224 		if (bNewParagraph == true) {
225 			xText.insertControlCharacter(xTextCursor,
226 					ControlCharacter.PARAGRAPH_BREAK, false);
227 			xTextCursor.gotoEnd(false);
228 		}
229 		XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(
230 				XTextRange.class, xTextCursor);
231 		xTextRange.setString(sText);
232 		xTextCursor.gotoEnd(true);
233 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
234 				XPropertySet.class, xText);
235 		return xPropSet;
236 	}
237 
238 	/**
239 	 * create a new presentation document and insert a new slide.
240 	 *
241 	 * @throws Exception
242 	 */
createDocumentAndSlide()243 	public void createDocumentAndSlide() throws Exception {
244 		impressDocument = (XComponent) UnoRuntime.queryInterface(
245 				XComponent.class, unoApp.newDocument("simpress"));
246 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
247 				XDrawPagesSupplier.class, impressDocument);
248 		drawpages = drawsupplier.getDrawPages();
249 
250 		sdDocument = (XPresentationSupplier) UnoRuntime.queryInterface(
251 				XPresentationSupplier.class, impressDocument);
252 		pre = sdDocument.getPresentation();
253 	}
254 
255 	/**
256 	 * Save presentation and reLoad the slide.
257 	 *
258 	 * @param no
259 	 * @return void
260 	 * @throws Exception
261 	 */
saveAndLoadSlide()262 	public void saveAndLoadSlide() throws Exception {
263 		reLoadFile = saveAndReloadDoc(impressDocument,
264 				"StarOffice XML (Impress)", "odp");
265 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
266 				XDrawPagesSupplier.class, reLoadFile);
267 		drawpages = drawsupplier.getDrawPages();
268 
269 		sdDocument = (XPresentationSupplier) UnoRuntime.queryInterface(
270 				XPresentationSupplier.class, reLoadFile);
271 		pre = sdDocument.getPresentation();
272 	}
273 
274 	/**
275 	 * save and reload Presentation document.
276 	 *
277 	 * @param presentationDocument
278 	 * @param sFilter
279 	 * @param sExtension
280 	 * @return
281 	 * @throws Exception
282 	 */
saveAndReloadDoc(XComponent presentationDocument, String sFilter, String sExtension)283 	private XComponent saveAndReloadDoc(XComponent presentationDocument,
284 			String sFilter, String sExtension) throws Exception {
285 		filePath = Testspace.getPath("tmp/shapeanimation." + sExtension);
286 		PropertyValue[] aStoreProperties = new PropertyValue[2];
287 		aStoreProperties[0] = new PropertyValue();
288 		aStoreProperties[1] = new PropertyValue();
289 		aStoreProperties[0].Name = "Override";
290 		aStoreProperties[0].Value = true;
291 		aStoreProperties[1].Name = "FilterName";
292 		aStoreProperties[1].Value = sFilter;
293 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
294 				XStorable.class, presentationDocument);
295 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
296 
297 		return (XComponent) UnoRuntime.queryInterface(XComponent.class,
298 				unoApp.loadDocument(filePath));
299 	}
300 }
301