1*b832fe9eSHerbert Dürr /**************************************************************
2*b832fe9eSHerbert Dürr  *
3*b832fe9eSHerbert Dürr  * Licensed to the Apache Software Foundation (ASF) under one
4*b832fe9eSHerbert Dürr  * or more contributor license agreements.  See the NOTICE file
5*b832fe9eSHerbert Dürr  * distributed with this work for additional information
6*b832fe9eSHerbert Dürr  * regarding copyright ownership.  The ASF licenses this file
7*b832fe9eSHerbert Dürr  * to you under the Apache License, Version 2.0 (the
8*b832fe9eSHerbert Dürr  * "License"); you may not use this file except in compliance
9*b832fe9eSHerbert Dürr  * with the License.  You may obtain a copy of the License at
10*b832fe9eSHerbert Dürr  *
11*b832fe9eSHerbert Dürr  *   http://www.apache.org/licenses/LICENSE-2.0
12*b832fe9eSHerbert Dürr  *
13*b832fe9eSHerbert Dürr  * Unless required by applicable law or agreed to in writing,
14*b832fe9eSHerbert Dürr  * software distributed under the License is distributed on an
15*b832fe9eSHerbert Dürr  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b832fe9eSHerbert Dürr  * KIND, either express or implied.  See the License for the
17*b832fe9eSHerbert Dürr  * specific language governing permissions and limitations
18*b832fe9eSHerbert Dürr  * under the License.
19*b832fe9eSHerbert Dürr  *
20*b832fe9eSHerbert Dürr  *************************************************************/
2122a547feSLi Feng Wang package fvt.uno.sd.animation;
2222a547feSLi Feng Wang 
2322a547feSLi Feng Wang import junit.framework.Assert;
2422a547feSLi Feng Wang 
2522a547feSLi Feng Wang import org.junit.After;
2622a547feSLi Feng Wang import org.junit.Before;
2722a547feSLi Feng Wang import org.junit.Test;
2822a547feSLi Feng Wang import org.openoffice.test.common.FileUtil;
2922a547feSLi Feng Wang import org.openoffice.test.common.Testspace;
3022a547feSLi Feng Wang import org.openoffice.test.uno.UnoApp;
3122a547feSLi Feng Wang 
3222a547feSLi Feng Wang import testlib.uno.ShapeUtil;
3322a547feSLi Feng Wang 
3422a547feSLi Feng Wang import com.sun.star.awt.Point;
3522a547feSLi Feng Wang import com.sun.star.awt.Size;
3622a547feSLi Feng Wang import com.sun.star.beans.PropertyValue;
3722a547feSLi Feng Wang import com.sun.star.beans.XPropertySet;
3822a547feSLi Feng Wang import com.sun.star.container.XIndexContainer;
3922a547feSLi Feng Wang import com.sun.star.container.XNameContainer;
4022a547feSLi Feng Wang import com.sun.star.container.XNamed;
4122a547feSLi Feng Wang import com.sun.star.drawing.XDrawPage;
4222a547feSLi Feng Wang import com.sun.star.drawing.XDrawPages;
4322a547feSLi Feng Wang import com.sun.star.drawing.XDrawPagesSupplier;
4422a547feSLi Feng Wang import com.sun.star.drawing.XShape;
4522a547feSLi Feng Wang import com.sun.star.drawing.XShapes;
4622a547feSLi Feng Wang import com.sun.star.frame.XStorable;
4722a547feSLi Feng Wang import com.sun.star.lang.XComponent;
4822a547feSLi Feng Wang import com.sun.star.lang.XSingleServiceFactory;
4922a547feSLi Feng Wang import com.sun.star.presentation.AnimationEffect;
5022a547feSLi Feng Wang import com.sun.star.presentation.XCustomPresentationSupplier;
5122a547feSLi Feng Wang import com.sun.star.presentation.XPresentation;
5222a547feSLi Feng Wang import com.sun.star.presentation.XPresentationSupplier;
5322a547feSLi Feng Wang import com.sun.star.table.XCell;
5422a547feSLi Feng Wang import com.sun.star.text.ControlCharacter;
5522a547feSLi Feng Wang import com.sun.star.text.XText;
5622a547feSLi Feng Wang import com.sun.star.text.XTextCursor;
5722a547feSLi Feng Wang import com.sun.star.text.XTextRange;
5822a547feSLi Feng Wang import com.sun.star.uno.UnoRuntime;
5922a547feSLi Feng Wang 
6022a547feSLi Feng Wang public class CustomAnimation {
6122a547feSLi Feng Wang 	XPresentationSupplier sdDocument = null;
6222a547feSLi Feng Wang 	XPresentation pre = null;
6322a547feSLi Feng Wang 	XComponent precomp = null;
6422a547feSLi Feng Wang 	XComponent impressDocument = null;
6522a547feSLi Feng Wang 	XComponent reLoadFile = null;
6622a547feSLi Feng Wang 	XDrawPagesSupplier drawsupplier = null;
6722a547feSLi Feng Wang 	XDrawPages drawpages = null;
6822a547feSLi Feng Wang 
6922a547feSLi Feng Wang 	String filePath = null;
7022a547feSLi Feng Wang 
7122a547feSLi Feng Wang 	UnoApp unoApp = new UnoApp();
7222a547feSLi Feng Wang 
7322a547feSLi Feng Wang 	/**
7422a547feSLi Feng Wang 	 * @throws java.lang.Exception
7522a547feSLi Feng Wang 	 */
7622a547feSLi Feng Wang 	@Before
setUp()7722a547feSLi Feng Wang 	public void setUp() throws Exception {
7822a547feSLi Feng Wang 		unoApp.start();
7922a547feSLi Feng Wang 		createDocumentAndSlide();
8022a547feSLi Feng Wang 	}
8122a547feSLi Feng Wang 
8222a547feSLi Feng Wang 	@After
tearDown()8322a547feSLi Feng Wang 	public void tearDown() throws Exception {
8422a547feSLi Feng Wang 		unoApp.closeDocument(impressDocument);
8522a547feSLi Feng Wang 		unoApp.closeDocument(reLoadFile);
8622a547feSLi Feng Wang 		unoApp.close();
8722a547feSLi Feng Wang 	}
8822a547feSLi Feng Wang 
8922a547feSLi Feng Wang 	@Test
testShapeAnimation()9022a547feSLi Feng Wang 	public void testShapeAnimation() throws Exception {
9122a547feSLi Feng Wang 
9222a547feSLi Feng Wang 		XShapes xShapes = null;
9322a547feSLi Feng Wang 		XPropertySet xShapePropSet[] = new XPropertySet[23];
9422a547feSLi Feng Wang 
9522a547feSLi Feng Wang 		drawpages.insertNewByIndex(0);
9622a547feSLi Feng Wang 
9722a547feSLi Feng Wang 		String aNameArray[] = { "Page 1", "Page 2", "Page 3", "Page 4",
9822a547feSLi Feng Wang 				"Page 5", "Page 6", "Page 7", "Page 8", "Page 9", "Page 10",
9922a547feSLi Feng Wang 				"Page 11", "Page 12", "Page 13", "Page 14", "Page 15",
10022a547feSLi Feng Wang 				"Page 16", "Page 17", "Page 18", "Page 19", "Page 20",
10122a547feSLi Feng Wang 				"Page 21", "Page 22", "Page 23", };
10222a547feSLi Feng Wang 		AnimationEffect AnimationEffectArray[] = {
10322a547feSLi Feng Wang 				AnimationEffect.NONE,
10422a547feSLi Feng Wang 				AnimationEffect.FADE_FROM_LEFT,
10522a547feSLi Feng Wang 				AnimationEffect.FADE_FROM_TOP,
10622a547feSLi Feng Wang 				AnimationEffect.FADE_FROM_RIGHT,
10722a547feSLi Feng Wang 				AnimationEffect.FADE_FROM_BOTTOM,
10822a547feSLi Feng Wang 				AnimationEffect.FADE_TO_CENTER,
10922a547feSLi Feng Wang 				AnimationEffect.FADE_FROM_CENTER,
11022a547feSLi Feng Wang 				AnimationEffect.MOVE_FROM_LEFT,
11122a547feSLi Feng Wang 				AnimationEffect.MOVE_FROM_TOP,
11222a547feSLi Feng Wang 				AnimationEffect.MOVE_FROM_RIGHT,
11322a547feSLi Feng Wang 				AnimationEffect.MOVE_FROM_BOTTOM,
11422a547feSLi Feng Wang 				AnimationEffect.VERTICAL_STRIPES,
11522a547feSLi Feng Wang 				AnimationEffect.HORIZONTAL_STRIPES,
11622a547feSLi Feng Wang 				AnimationEffect.CLOCKWISE,
11722a547feSLi Feng Wang 				AnimationEffect.COUNTERCLOCKWISE,
11822a547feSLi Feng Wang 				AnimationEffect.FADE_FROM_UPPERLEFT,
11922a547feSLi Feng Wang 				AnimationEffect.FADE_FROM_UPPERRIGHT,
12022a547feSLi Feng Wang 				AnimationEffect.FADE_FROM_LOWERLEFT,
12122a547feSLi Feng Wang 				AnimationEffect.FADE_FROM_LOWERRIGHT,
12222a547feSLi Feng Wang 				AnimationEffect.CLOSE_VERTICAL,
12322a547feSLi Feng Wang 				AnimationEffect.CLOSE_HORIZONTAL,
12422a547feSLi Feng Wang 				AnimationEffect.OPEN_VERTICAL,
12522a547feSLi Feng Wang 				AnimationEffect.OPEN_HORIZONTAL,
12622a547feSLi Feng Wang 				AnimationEffect.PATH,
12722a547feSLi Feng Wang /*				AnimationEffect.MOVE_TO_LEFT,
12822a547feSLi Feng Wang 				AnimationEffect.MOVE_TO_TOP,
12922a547feSLi Feng Wang 				AnimationEffect.MOVE_TO_RIGHT,
13022a547feSLi Feng Wang 				AnimationEffect.MOVE_TO_BOTTOM,
13122a547feSLi Feng Wang 				AnimationEffect.SPIRALIN_LEFT,
13222a547feSLi Feng Wang 				AnimationEffect.SPIRALIN_RIGHT,
13322a547feSLi Feng Wang 				AnimationEffect.SPIRALOUT_LEFT,
13422a547feSLi Feng Wang 				AnimationEffect.SPIRALOUT_RIGHT,
13522a547feSLi Feng Wang 				AnimationEffect.DISSOLVE,
13622a547feSLi Feng Wang 				AnimationEffect.WAVYLINE_FROM_LEFT,
13722a547feSLi Feng Wang 				AnimationEffect.RANDOM,
13822a547feSLi Feng Wang 				AnimationEffect.VERTICAL_LINES,
13922a547feSLi Feng Wang 				AnimationEffect.LASER_FROM_LEFT,
14022a547feSLi Feng Wang 				AnimationEffect.APPEAR,
14122a547feSLi Feng Wang 				AnimationEffect.HIDE,
14222a547feSLi Feng Wang 				AnimationEffect.MOVE_FROM_UPPERLEFT,
14322a547feSLi Feng Wang 				AnimationEffect.VERTICAL_CHECKERBOARD,
14422a547feSLi Feng Wang 				AnimationEffect.HORIZONTAL_ROTATE,
14522a547feSLi Feng Wang 				AnimationEffect.VERTICAL_STRETCH,
14622a547feSLi Feng Wang 				AnimationEffect.ZOOM_IN,
14722a547feSLi Feng Wang 				AnimationEffect.ZOOM_OUT_FROM_LEFT, */
14822a547feSLi Feng Wang 				};
14922a547feSLi Feng Wang 
15022a547feSLi Feng Wang 		// insert  pages
15122a547feSLi Feng Wang 		while (drawpages.getCount() < aNameArray.length)
15222a547feSLi Feng Wang 			drawpages.insertNewByIndex(0);
15322a547feSLi Feng Wang 
15422a547feSLi Feng Wang 		// add text shape to each page
15522a547feSLi Feng Wang 		for (int i = 0; i < aNameArray.length; i++) {
15622a547feSLi Feng Wang 			XDrawPage xDrawPage = (XDrawPage) UnoRuntime.queryInterface(
15722a547feSLi Feng Wang 					XDrawPage.class, drawpages.getByIndex(i));
15822a547feSLi Feng Wang 			XNamed xPageName = (XNamed) UnoRuntime.queryInterface(XNamed.class,
15922a547feSLi Feng Wang 					xDrawPage);
16022a547feSLi Feng Wang 			xPageName.setName(aNameArray[i]);
16122a547feSLi Feng Wang 
16222a547feSLi Feng Wang 			xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class,
16322a547feSLi Feng Wang 					drawpages.getByIndex(i));
16422a547feSLi Feng Wang 
16522a547feSLi Feng Wang 			// create a rectangle that is placed on the top left of the page
16622a547feSLi Feng Wang 			xShapePropSet[i] = ShapeUtil.createAndInsertShape(impressDocument,
16722a547feSLi Feng Wang 					xShapes, new Point(1000, 1000), new Size(5000, 5000),
16822a547feSLi Feng Wang 					"com.sun.star.drawing.RectangleShape");
16922a547feSLi Feng Wang 		}
17022a547feSLi Feng Wang 
17122a547feSLi Feng Wang 		for (int i = 0; i < aNameArray.length; i++) {
17222a547feSLi Feng Wang 			// set an object animation for each slide
17322a547feSLi Feng Wang 			xShapePropSet[i]
17422a547feSLi Feng Wang 					.setPropertyValue("Effect", AnimationEffectArray[i]);
17522a547feSLi Feng Wang 		}
17622a547feSLi Feng Wang 
17722a547feSLi Feng Wang 		// If this property and the property DimPrevious are both true, the shape is hidden on the next click after the AnimationEffect has finished.
17822a547feSLi Feng Wang 		xShapePropSet[1].setPropertyValue("DimHide", new Boolean(false));
17922a547feSLi Feng Wang 		// If true, this shape is painted using DimColor on the next click after finishing the AnimationEffect.
18022a547feSLi Feng Wang 		xShapePropSet[1].setPropertyValue("DimPrevious", new Boolean(true));
18122a547feSLi Feng Wang 		// his color is used to paint the shape on the next click after the animation effect has finished
18222a547feSLi Feng Wang 		xShapePropSet[1].setPropertyValue("DimColor", new Integer(0xff0000));
18322a547feSLi Feng Wang 		// the sound file specified in Sound is played while the animation effect is executed
18422a547feSLi Feng Wang 		xShapePropSet[1].setPropertyValue("SoundOn", new Boolean(false));
18522a547feSLi Feng Wang 		// the sound specified in the Sound property of this shape is played completely
18622a547feSLi Feng Wang 		xShapePropSet[1].setPropertyValue("PlayFull", new Boolean(false));
18722a547feSLi Feng Wang 
18822a547feSLi Feng Wang 		saveAndLoadSlide();
18922a547feSLi Feng Wang 
19022a547feSLi Feng Wang 		for (int i = 0; i < aNameArray.length; i++) {
19122a547feSLi Feng Wang 			// assert if Shape Animation is set
19222a547feSLi Feng Wang 			Assert.assertEquals(
19322a547feSLi Feng Wang 					"Text Animation Effect is AnimationEffectArray[]",
19422a547feSLi Feng Wang 					AnimationEffectArray[i],
19522a547feSLi Feng Wang 					xShapePropSet[i].getPropertyValue("Effect"));
19622a547feSLi Feng Wang 		}
19722a547feSLi Feng Wang 		// assert if DimHide is false
19822a547feSLi Feng Wang 		Assert.assertEquals("Shape Animation DimHide is false", Boolean.FALSE,
19922a547feSLi Feng Wang 				xShapePropSet[1].getPropertyValue("DimHide"));
20022a547feSLi Feng Wang 		// assert if DimPrevious is true
20122a547feSLi Feng Wang 		Assert.assertEquals("Shape Animation DimPrevious is true",
20222a547feSLi Feng Wang 				Boolean.TRUE, xShapePropSet[1].getPropertyValue("DimPrevious"));
20322a547feSLi Feng Wang 		// assert if DimColor is red
20422a547feSLi Feng Wang 		Assert.assertEquals("Shape Animation DimColor is Red", 0xff0000,
20522a547feSLi Feng Wang 				xShapePropSet[1].getPropertyValue("DimColor"));
20622a547feSLi Feng Wang 		// assert the sound file specified in Sound don't play while the
20722a547feSLi Feng Wang 		// animation effect is executed.
20822a547feSLi Feng Wang 		Assert.assertEquals("Shape SoundOn is false", Boolean.FALSE,
20922a547feSLi Feng Wang 				xShapePropSet[1].getPropertyValue("SoundOn"));
21022a547feSLi Feng Wang 		// assert the sound specified in the Sound property of this shape is
21122a547feSLi Feng Wang 		// played completely
21222a547feSLi Feng Wang 		Assert.assertEquals("Shape PlayFull is false", Boolean.FALSE,
21322a547feSLi Feng Wang 				xShapePropSet[1].getPropertyValue("PlayFull"));
21422a547feSLi Feng Wang 
21522a547feSLi Feng Wang 	}
21622a547feSLi Feng Wang 
addPortion(XShape xShape, String sText, boolean bNewParagraph)21722a547feSLi Feng Wang 	public XPropertySet addPortion(XShape xShape, String sText,
21822a547feSLi Feng Wang 			boolean bNewParagraph)
21922a547feSLi Feng Wang 			throws com.sun.star.lang.IllegalArgumentException {
22022a547feSLi Feng Wang 		XText xText = (XText) UnoRuntime.queryInterface(XText.class, xShape);
22122a547feSLi Feng Wang 
22222a547feSLi Feng Wang 		XTextCursor xTextCursor = xText.createTextCursor();
22322a547feSLi Feng Wang 		xTextCursor.gotoEnd(false);
22422a547feSLi Feng Wang 		if (bNewParagraph == true) {
22522a547feSLi Feng Wang 			xText.insertControlCharacter(xTextCursor,
22622a547feSLi Feng Wang 					ControlCharacter.PARAGRAPH_BREAK, false);
22722a547feSLi Feng Wang 			xTextCursor.gotoEnd(false);
22822a547feSLi Feng Wang 		}
22922a547feSLi Feng Wang 		XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(
23022a547feSLi Feng Wang 				XTextRange.class, xTextCursor);
23122a547feSLi Feng Wang 		xTextRange.setString(sText);
23222a547feSLi Feng Wang 		xTextCursor.gotoEnd(true);
23322a547feSLi Feng Wang 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
23422a547feSLi Feng Wang 				XPropertySet.class, xText);
23522a547feSLi Feng Wang 		return xPropSet;
23622a547feSLi Feng Wang 	}
23722a547feSLi Feng Wang 
23822a547feSLi Feng Wang 	/**
23922a547feSLi Feng Wang 	 * create a new presentation document and insert a new slide.
24022a547feSLi Feng Wang 	 *
24122a547feSLi Feng Wang 	 * @throws Exception
24222a547feSLi Feng Wang 	 */
createDocumentAndSlide()24322a547feSLi Feng Wang 	public void createDocumentAndSlide() throws Exception {
24422a547feSLi Feng Wang 		impressDocument = (XComponent) UnoRuntime.queryInterface(
24522a547feSLi Feng Wang 				XComponent.class, unoApp.newDocument("simpress"));
24622a547feSLi Feng Wang 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
24722a547feSLi Feng Wang 				XDrawPagesSupplier.class, impressDocument);
24822a547feSLi Feng Wang 		drawpages = drawsupplier.getDrawPages();
24922a547feSLi Feng Wang 
25022a547feSLi Feng Wang 		sdDocument = (XPresentationSupplier) UnoRuntime.queryInterface(
25122a547feSLi Feng Wang 				XPresentationSupplier.class, impressDocument);
25222a547feSLi Feng Wang 		pre = sdDocument.getPresentation();
25322a547feSLi Feng Wang 	}
25422a547feSLi Feng Wang 
25522a547feSLi Feng Wang 	/**
25622a547feSLi Feng Wang 	 * Save presentation and reLoad the slide.
25722a547feSLi Feng Wang 	 *
25822a547feSLi Feng Wang 	 * @param no
25922a547feSLi Feng Wang 	 * @return void
26022a547feSLi Feng Wang 	 * @throws Exception
26122a547feSLi Feng Wang 	 */
saveAndLoadSlide()26222a547feSLi Feng Wang 	public void saveAndLoadSlide() throws Exception {
26322a547feSLi Feng Wang 		reLoadFile = saveAndReloadDoc(impressDocument,
26422a547feSLi Feng Wang 				"StarOffice XML (Impress)", "odp");
26522a547feSLi Feng Wang 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
26622a547feSLi Feng Wang 				XDrawPagesSupplier.class, reLoadFile);
26722a547feSLi Feng Wang 		drawpages = drawsupplier.getDrawPages();
26822a547feSLi Feng Wang 
26922a547feSLi Feng Wang 		sdDocument = (XPresentationSupplier) UnoRuntime.queryInterface(
27022a547feSLi Feng Wang 				XPresentationSupplier.class, reLoadFile);
27122a547feSLi Feng Wang 		pre = sdDocument.getPresentation();
27222a547feSLi Feng Wang 	}
27322a547feSLi Feng Wang 
27422a547feSLi Feng Wang 	/**
27522a547feSLi Feng Wang 	 * save and reload Presentation document.
27622a547feSLi Feng Wang 	 *
27722a547feSLi Feng Wang 	 * @param presentationDocument
27822a547feSLi Feng Wang 	 * @param sFilter
27922a547feSLi Feng Wang 	 * @param sExtension
28022a547feSLi Feng Wang 	 * @return
28122a547feSLi Feng Wang 	 * @throws Exception
28222a547feSLi Feng Wang 	 */
saveAndReloadDoc(XComponent presentationDocument, String sFilter, String sExtension)28322a547feSLi Feng Wang 	private XComponent saveAndReloadDoc(XComponent presentationDocument,
28422a547feSLi Feng Wang 			String sFilter, String sExtension) throws Exception {
28522a547feSLi Feng Wang 		filePath = Testspace.getPath("tmp/shapeanimation." + sExtension);
28622a547feSLi Feng Wang 		PropertyValue[] aStoreProperties = new PropertyValue[2];
28722a547feSLi Feng Wang 		aStoreProperties[0] = new PropertyValue();
28822a547feSLi Feng Wang 		aStoreProperties[1] = new PropertyValue();
28922a547feSLi Feng Wang 		aStoreProperties[0].Name = "Override";
29022a547feSLi Feng Wang 		aStoreProperties[0].Value = true;
29122a547feSLi Feng Wang 		aStoreProperties[1].Name = "FilterName";
29222a547feSLi Feng Wang 		aStoreProperties[1].Value = sFilter;
29322a547feSLi Feng Wang 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
29422a547feSLi Feng Wang 				XStorable.class, presentationDocument);
29522a547feSLi Feng Wang 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
29622a547feSLi Feng Wang 
29722a547feSLi Feng Wang 		return (XComponent) UnoRuntime.queryInterface(XComponent.class,
29822a547feSLi Feng Wang 				unoApp.loadDocument(filePath));
29922a547feSLi Feng Wang 	}
30022a547feSLi Feng Wang }
301