1*22a547feSLi Feng Wang package fvt.uno.sd.animation;
2*22a547feSLi Feng Wang 
3*22a547feSLi Feng Wang import junit.framework.Assert;
4*22a547feSLi Feng Wang 
5*22a547feSLi Feng Wang import org.junit.After;
6*22a547feSLi Feng Wang import org.junit.Before;
7*22a547feSLi Feng Wang import org.junit.Test;
8*22a547feSLi Feng Wang import org.openoffice.test.common.FileUtil;
9*22a547feSLi Feng Wang import org.openoffice.test.common.Testspace;
10*22a547feSLi Feng Wang import org.openoffice.test.uno.UnoApp;
11*22a547feSLi Feng Wang 
12*22a547feSLi Feng Wang import testlib.uno.ShapeUtil;
13*22a547feSLi Feng Wang 
14*22a547feSLi Feng Wang import com.sun.star.awt.Point;
15*22a547feSLi Feng Wang import com.sun.star.awt.Size;
16*22a547feSLi Feng Wang import com.sun.star.beans.PropertyValue;
17*22a547feSLi Feng Wang import com.sun.star.beans.XPropertySet;
18*22a547feSLi Feng Wang import com.sun.star.container.XIndexContainer;
19*22a547feSLi Feng Wang import com.sun.star.container.XNameContainer;
20*22a547feSLi Feng Wang import com.sun.star.container.XNamed;
21*22a547feSLi Feng Wang import com.sun.star.drawing.XDrawPage;
22*22a547feSLi Feng Wang import com.sun.star.drawing.XDrawPages;
23*22a547feSLi Feng Wang import com.sun.star.drawing.XDrawPagesSupplier;
24*22a547feSLi Feng Wang import com.sun.star.drawing.XShape;
25*22a547feSLi Feng Wang import com.sun.star.drawing.XShapes;
26*22a547feSLi Feng Wang import com.sun.star.frame.XStorable;
27*22a547feSLi Feng Wang import com.sun.star.lang.XComponent;
28*22a547feSLi Feng Wang import com.sun.star.lang.XSingleServiceFactory;
29*22a547feSLi Feng Wang import com.sun.star.presentation.AnimationEffect;
30*22a547feSLi Feng Wang import com.sun.star.presentation.XCustomPresentationSupplier;
31*22a547feSLi Feng Wang import com.sun.star.presentation.XPresentation;
32*22a547feSLi Feng Wang import com.sun.star.presentation.XPresentationSupplier;
33*22a547feSLi Feng Wang import com.sun.star.text.ControlCharacter;
34*22a547feSLi Feng Wang import com.sun.star.text.XText;
35*22a547feSLi Feng Wang import com.sun.star.text.XTextCursor;
36*22a547feSLi Feng Wang import com.sun.star.text.XTextRange;
37*22a547feSLi Feng Wang import com.sun.star.uno.UnoRuntime;
38*22a547feSLi Feng Wang 
39*22a547feSLi Feng Wang public class TextAnimation {
40*22a547feSLi Feng Wang 	XPresentationSupplier sdDocument = null;
41*22a547feSLi Feng Wang 	XPresentation pre = null;
42*22a547feSLi Feng Wang 	XComponent precomp = null;
43*22a547feSLi Feng Wang 	XComponent impressDocument = null;
44*22a547feSLi Feng Wang 	XComponent reLoadFile = null;
45*22a547feSLi Feng Wang 	XDrawPagesSupplier drawsupplier = null;
46*22a547feSLi Feng Wang 	XDrawPages drawpages = null;
47*22a547feSLi Feng Wang 
48*22a547feSLi Feng Wang 	String filePath = null;
49*22a547feSLi Feng Wang 
50*22a547feSLi Feng Wang 	UnoApp unoApp = new UnoApp();
51*22a547feSLi Feng Wang 
52*22a547feSLi Feng Wang 	/**
53*22a547feSLi Feng Wang 	 * @throws java.lang.Exception
54*22a547feSLi Feng Wang 	 */
55*22a547feSLi Feng Wang 	@Before
56*22a547feSLi Feng Wang 	public void setUp() throws Exception {
57*22a547feSLi Feng Wang 		unoApp.start();
58*22a547feSLi Feng Wang 		createDocumentAndSlide();
59*22a547feSLi Feng Wang 	}
60*22a547feSLi Feng Wang 
61*22a547feSLi Feng Wang 	@After
62*22a547feSLi Feng Wang 	public void tearDown() throws Exception {
63*22a547feSLi Feng Wang 		unoApp.closeDocument(impressDocument);
64*22a547feSLi Feng Wang 		unoApp.closeDocument(reLoadFile);
65*22a547feSLi Feng Wang 		unoApp.close();
66*22a547feSLi Feng Wang 	}
67*22a547feSLi Feng Wang 
68*22a547feSLi Feng Wang 	@Test
69*22a547feSLi Feng Wang 	public void testTextAnimation() throws Exception {
70*22a547feSLi Feng Wang 
71*22a547feSLi Feng Wang 		XShapes xShapes;
72*22a547feSLi Feng Wang 		XPropertySet xShapePropSet;
73*22a547feSLi Feng Wang 		Point po = new Point(5000, 5000);
74*22a547feSLi Feng Wang 
75*22a547feSLi Feng Wang 		// create pages, so that three are available
76*22a547feSLi Feng Wang 		drawpages.insertNewByIndex(0);
77*22a547feSLi Feng Wang 		// get the shape container for page one
78*22a547feSLi Feng Wang 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class,
79*22a547feSLi Feng Wang 				drawpages.getByIndex(0));
80*22a547feSLi Feng Wang 		// create a rectangle that is placed on the top left of the page
81*22a547feSLi Feng Wang 		XShape xRectangle = ShapeUtil.createShape(impressDocument, po,
82*22a547feSLi Feng Wang 				new Size(21000, 12500), "com.sun.star.drawing.RectangleShape");
83*22a547feSLi Feng Wang 		xShapes.add(xRectangle);
84*22a547feSLi Feng Wang 
85*22a547feSLi Feng Wang 		XPropertySet xTextPropSet = addPortion(xRectangle, "Text Animation",
86*22a547feSLi Feng Wang 				false);
87*22a547feSLi Feng Wang 
88*22a547feSLi Feng Wang 		xTextPropSet.setPropertyValue("TextEffect",
89*22a547feSLi Feng Wang 				AnimationEffect.MOVE_FROM_RIGHT);
90*22a547feSLi Feng Wang 
91*22a547feSLi Feng Wang 		saveAndLoadSlide();
92*22a547feSLi Feng Wang 
93*22a547feSLi Feng Wang 		// assert if Text Animation is set
94*22a547feSLi Feng Wang 		Assert.assertEquals("Text Animation Effect is MOVE_FROM_RIGHT",
95*22a547feSLi Feng Wang 				AnimationEffect.MOVE_FROM_RIGHT,
96*22a547feSLi Feng Wang 				xTextPropSet.getPropertyValue("TextEffect"));
97*22a547feSLi Feng Wang 
98*22a547feSLi Feng Wang 	}
99*22a547feSLi Feng Wang 
100*22a547feSLi Feng Wang 	public XPropertySet addPortion(XShape xShape, String sText,
101*22a547feSLi Feng Wang 			boolean bNewParagraph)
102*22a547feSLi Feng Wang 			throws com.sun.star.lang.IllegalArgumentException {
103*22a547feSLi Feng Wang 		XText xText = (XText) UnoRuntime.queryInterface(XText.class, xShape);
104*22a547feSLi Feng Wang 
105*22a547feSLi Feng Wang 		XTextCursor xTextCursor = xText.createTextCursor();
106*22a547feSLi Feng Wang 		xTextCursor.gotoEnd(false);
107*22a547feSLi Feng Wang 		if (bNewParagraph == true) {
108*22a547feSLi Feng Wang 			xText.insertControlCharacter(xTextCursor,
109*22a547feSLi Feng Wang 					ControlCharacter.PARAGRAPH_BREAK, false);
110*22a547feSLi Feng Wang 			xTextCursor.gotoEnd(false);
111*22a547feSLi Feng Wang 		}
112*22a547feSLi Feng Wang 		XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(
113*22a547feSLi Feng Wang 				XTextRange.class, xTextCursor);
114*22a547feSLi Feng Wang 		xTextRange.setString(sText);
115*22a547feSLi Feng Wang 		xTextCursor.gotoEnd(true);
116*22a547feSLi Feng Wang 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
117*22a547feSLi Feng Wang 				XPropertySet.class, xText);
118*22a547feSLi Feng Wang 		return xPropSet;
119*22a547feSLi Feng Wang 	}
120*22a547feSLi Feng Wang 
121*22a547feSLi Feng Wang 	/**
122*22a547feSLi Feng Wang 	 * create a new presentation document and insert a new slide.
123*22a547feSLi Feng Wang 	 *
124*22a547feSLi Feng Wang 	 * @throws Exception
125*22a547feSLi Feng Wang 	 */
126*22a547feSLi Feng Wang 	public void createDocumentAndSlide() throws Exception {
127*22a547feSLi Feng Wang 		impressDocument = (XComponent) UnoRuntime.queryInterface(
128*22a547feSLi Feng Wang 				XComponent.class, unoApp.newDocument("simpress"));
129*22a547feSLi Feng Wang 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
130*22a547feSLi Feng Wang 				XDrawPagesSupplier.class, impressDocument);
131*22a547feSLi Feng Wang 		drawpages = drawsupplier.getDrawPages();
132*22a547feSLi Feng Wang 
133*22a547feSLi Feng Wang 		sdDocument = (XPresentationSupplier) UnoRuntime.queryInterface(
134*22a547feSLi Feng Wang 				XPresentationSupplier.class, impressDocument);
135*22a547feSLi Feng Wang 		pre = sdDocument.getPresentation();
136*22a547feSLi Feng Wang 	}
137*22a547feSLi Feng Wang 
138*22a547feSLi Feng Wang 	/**
139*22a547feSLi Feng Wang 	 * Save presentation and reLoad the slide.
140*22a547feSLi Feng Wang 	 *
141*22a547feSLi Feng Wang 	 * @param no
142*22a547feSLi Feng Wang 	 * @return void
143*22a547feSLi Feng Wang 	 * @throws Exception
144*22a547feSLi Feng Wang 	 */
145*22a547feSLi Feng Wang 	public void saveAndLoadSlide() throws Exception {
146*22a547feSLi Feng Wang 		reLoadFile = saveAndReloadDoc(impressDocument,
147*22a547feSLi Feng Wang 				"StarOffice XML (Impress)", "odp");
148*22a547feSLi Feng Wang 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
149*22a547feSLi Feng Wang 				XDrawPagesSupplier.class, reLoadFile);
150*22a547feSLi Feng Wang 		drawpages = drawsupplier.getDrawPages();
151*22a547feSLi Feng Wang 
152*22a547feSLi Feng Wang 		sdDocument = (XPresentationSupplier) UnoRuntime.queryInterface(
153*22a547feSLi Feng Wang 				XPresentationSupplier.class, reLoadFile);
154*22a547feSLi Feng Wang 		pre = sdDocument.getPresentation();
155*22a547feSLi Feng Wang 	}
156*22a547feSLi Feng Wang 
157*22a547feSLi Feng Wang 	/**
158*22a547feSLi Feng Wang 	 * save and reload Presentation document.
159*22a547feSLi Feng Wang 	 *
160*22a547feSLi Feng Wang 	 * @param presentationDocument
161*22a547feSLi Feng Wang 	 * @param sFilter
162*22a547feSLi Feng Wang 	 * @param sExtension
163*22a547feSLi Feng Wang 	 * @return
164*22a547feSLi Feng Wang 	 * @throws Exception
165*22a547feSLi Feng Wang 	 */
166*22a547feSLi Feng Wang 	private XComponent saveAndReloadDoc(XComponent presentationDocument,
167*22a547feSLi Feng Wang 			String sFilter, String sExtension) throws Exception {
168*22a547feSLi Feng Wang 		filePath = Testspace.getPath("tmp/customshow." + sExtension);
169*22a547feSLi Feng Wang 		PropertyValue[] aStoreProperties = new PropertyValue[2];
170*22a547feSLi Feng Wang 		aStoreProperties[0] = new PropertyValue();
171*22a547feSLi Feng Wang 		aStoreProperties[1] = new PropertyValue();
172*22a547feSLi Feng Wang 		aStoreProperties[0].Name = "Override";
173*22a547feSLi Feng Wang 		aStoreProperties[0].Value = true;
174*22a547feSLi Feng Wang 		aStoreProperties[1].Name = "FilterName";
175*22a547feSLi Feng Wang 		aStoreProperties[1].Value = sFilter;
176*22a547feSLi Feng Wang 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
177*22a547feSLi Feng Wang 				XStorable.class, presentationDocument);
178*22a547feSLi Feng Wang 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
179*22a547feSLi Feng Wang 
180*22a547feSLi Feng Wang 		return UnoRuntime.queryInterface(XComponent.class,
181*22a547feSLi Feng Wang 				unoApp.loadDocument(filePath));
182*22a547feSLi Feng Wang 	}
183*22a547feSLi Feng Wang }
184