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.shape;
22 
23 import java.awt.geom.AffineTransform;
24 import java.io.File;
25 import java.io.IOException;
26 
27 import org.junit.After;
28 import static org.junit.Assert.*;
29 
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.openoffice.test.uno.UnoApp;
34 import org.openoffice.test.common.FileUtil;
35 import org.openoffice.test.common.Testspace;
36 
37 import testlib.uno.PageUtil;
38 import testlib.uno.ShapeUtil;
39 import testlib.uno.TestUtil;
40 
41 import com.sun.star.text.WrapTextMode;
42 import com.sun.star.text.XText;
43 import com.sun.star.text.XTextContent;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.presentation.XPresentation;
46 import com.sun.star.presentation.XPresentationSupplier;
47 import com.sun.star.awt.Gradient;
48 import com.sun.star.awt.GradientStyle;
49 import com.sun.star.awt.Size;
50 import com.sun.star.awt.Point;
51 import com.sun.star.beans.PropertyValue;
52 import com.sun.star.beans.XPropertySet;
53 import com.sun.star.drawing.DashStyle;
54 import com.sun.star.drawing.FillStyle;
55 import com.sun.star.drawing.Hatch;
56 import com.sun.star.drawing.HatchStyle;
57 import com.sun.star.drawing.HomogenMatrix3;
58 import com.sun.star.drawing.LineDash;
59 import com.sun.star.drawing.LineStyle;
60 import com.sun.star.drawing.XDrawPage;
61 import com.sun.star.drawing.XDrawPages;
62 import com.sun.star.drawing.XDrawPagesSupplier;
63 import com.sun.star.drawing.XShape;
64 import com.sun.star.drawing.XShapes;
65 import com.sun.star.frame.XComponentLoader;
66 import com.sun.star.frame.XStorable;
67 import com.sun.star.lang.XComponent;
68 import com.sun.star.lang.XMultiServiceFactory;
69 
70 public class ShapeProperties {
71 	UnoApp unoApp = new UnoApp();
72 	XPresentationSupplier sdDocument = null;
73 	XPresentation pre = null;
74 	XComponent precomp = null;
75 	XComponent impressDocument = null;
76 	XComponent reLoadFile = null;
77 	XDrawPagesSupplier drawsupplier = null;
78 	XDrawPages drawpages = null;
79 	XShapes xShapes = null;
80 	XDrawPage xpage = null;
81 	String filePath = null;
82 
83 	@Before
84 	public void setUp() throws Exception {
85 		unoApp.start();
86 		createDocumentAndSlide();
87 	}
88 
89 	@After
90 	public void tearDown() throws Exception {
91 		unoApp.closeDocument(impressDocument);
92 		unoApp.closeDocument(reLoadFile);
93 		unoApp.close();
94 		if (filePath != null)
95 			FileUtil.deleteFile(filePath);
96 	}
97 
98 	/**
99 	 * test Insert a new slide and Insert a new EllipseShape
100 	 *
101 	 * @throws Exception
102 	 */
103 	 @Test
104 	 public void testInsertShape() throws Exception {
105 	 Point po = new Point(1000, 8000);
106 	 xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
107 	 XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
108 	 5000, 5000), "com.sun.star.drawing.EllipseShape");
109 	 xShapes.add(xShape);
110 	 XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
111 	 XPropertySet.class, xShape);
112 	 xPropSet.setPropertyValue("Name", "test");
113 
114 	 xShape=saveAndLoadShape(1,0);
115 	 XPropertySet xPropSet2 = (XPropertySet) UnoRuntime.queryInterface(
116 	 XPropertySet.class, xShape);
117 	 assertEquals("Not the same shape","test",xPropSet2.getPropertyValue("Name"));
118 	 assertEquals("Not EllopseShape","com.sun.star.drawing.EllipseShape",xShape.getShapeType());
119 	 }
120 
121 	 /**
122 	 * test Insert text to an EllopseShape
123 	 *
124 	 * @throws Exception
125 	 */
126 	 @Test
127 	 public void testInsertTextToShape() throws Exception {
128 	 Point po = new Point(1000, 8000);
129 	 xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
130 	 XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
131 	 5000, 5000), "com.sun.star.drawing.EllipseShape");
132 	 xShapes.add(xShape);
133 	 xShape=saveAndLoadShape(1,0);
134 	 ShapeUtil.addPortion(xShape, "test", false);
135 	 assertEquals("Not put text correctly","test",ShapeUtil.getPortion(xShape));
136 	 }
137 
138 	 /**
139 	 * test Shape fill with Gradient
140 	 *
141 	 * @throws Exception
142 	 */
143 	 @Test
144 	 public void testShapeFillGradient() throws Exception {
145 	 Point po = new Point(1000, 8000);
146 	 xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
147 	 XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
148 	 5000, 5000), "com.sun.star.drawing.EllipseShape");
149 	 xShapes.add(xShape);
150 	 XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
151 	 XPropertySet.class, xShape);
152 	 xPropSet.setPropertyValue("FillStyle", FillStyle.GRADIENT);
153 	 Gradient aGradient = new Gradient();
154 	 aGradient.Style = GradientStyle.LINEAR;
155 	 aGradient.StartColor = 0x00ff00;
156 	 aGradient.EndColor = 0xffff00;
157 	 aGradient.Angle = 450;
158 	 aGradient.Border = 0;
159 	 aGradient.XOffset = 0;
160 	 aGradient.YOffset = 0;
161 	 aGradient.StartIntensity = 100;
162 	 aGradient.EndIntensity = 100;
163 	 aGradient.StepCount = 10;
164 	 xPropSet.setPropertyValue("FillGradient", aGradient);
165 	 // --------------------------
166 	 xShape=saveAndLoadShape(1,0);
167 	 xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xShape);
168 	 // ----------------------------
169 	 assertEquals("Not Gradient Fill Style",FillStyle.GRADIENT,xPropSet.getPropertyValue("FillStyle"));
170 	 aGradient=(Gradient) xPropSet.getPropertyValue("FillGradient");
171 	 assertEquals("Not Linear Gradient",GradientStyle.LINEAR,aGradient.Style);
172 	 }
173 
174 	 /**
175 	 * test Shape fill with yellow color
176 	 *
177 	 * @throws Exception
178 	 */
179 	 @Test
180 	 public void testShapeFillColor() throws Exception {
181 	 Point po = new Point(1000, 8000);
182 	 xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
183 	 XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
184 	 5000, 5000), "com.sun.star.drawing.EllipseShape");
185 	 xShapes.add(xShape);
186 	 XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
187 	 XPropertySet.class, xShape);
188 	 xPropSet.setPropertyValue("FillStyle", FillStyle.SOLID);
189 	 xPropSet.setPropertyValue("FillColor", 0xffff00);
190 	 // --------------------------
191 	 xShape=saveAndLoadShape(1,0);
192 	 xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xShape);
193 	 // ----------------------------------------------------
194 	 assertEquals("Not Color Fill Style",FillStyle.SOLID,xPropSet.getPropertyValue("FillStyle"));
195 	 assertEquals("Not Yellow Color Fill",0xffff00,xPropSet.getPropertyValue("FillColor"));
196 	 }
197 
198 	 /**
199 	 * test shape fill with Hatch Style(Pattern in MS)
200 	 * @throws Exception
201 	 */
202 	 @Test
203 	 public void testShapeFillHatch() throws Exception {
204 	 Point po = new Point(1000, 8000);
205 	 xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
206 	 XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
207 	 5000, 5000), "com.sun.star.drawing.EllipseShape");
208 	 xShapes.add(xShape);
209 	 XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
210 	 XPropertySet.class, xShape);
211 	 xPropSet.setPropertyValue("FillStyle", FillStyle.HATCH);
212 	 Hatch aHatch=new Hatch();
213 	 aHatch.Style=HatchStyle.DOUBLE;
214 	 aHatch.Color=0x00ff00;
215 	 aHatch.Distance=100;
216 	 aHatch.Angle=450;
217 	 xPropSet.setPropertyValue("FillHatch", aHatch);
218 
219 	 // --------------------------
220 	 xShape=saveAndLoadShape(1,0);
221 	 xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xShape);
222 	 // ----------------------------
223 	 assertEquals("Not Gradient Fill Style",FillStyle.HATCH,xPropSet.getPropertyValue("FillStyle"));
224 	 aHatch=(Hatch) xPropSet.getPropertyValue("FillHatch");
225 	 assertEquals("Not Double Hatch",HatchStyle.DOUBLE,aHatch.Style);
226 	 }
227 
228 	// ------------Shape Line-----------------------------
229 	/**
230 	 * test Shape Line style DASH
231 	 *
232 	 * @throws Exception
233 	 */
234 	 @Test
235 	 public void testShapeLineStyle() throws Exception {
236 	 Point po = new Point(1000, 8000);
237 	 xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
238 	 XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
239 	 5000, 5000), "com.sun.star.drawing.EllipseShape");
240 	 xShapes.add(xShape);
241 
242 	 XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
243 	 XPropertySet.class, xShape);
244 	 xPropSet.setPropertyValue("LineStyle", LineStyle.DASH);
245 
246 	 LineDash aLineDash=new LineDash();
247 	 aLineDash.Style=DashStyle.ROUND;
248 	 aLineDash.Dots=2;
249 	 aLineDash.DashLen=100;
250 	 aLineDash.Distance=50;
251 	 xPropSet.setPropertyValue("LineDash", aLineDash);
252 
253 	 // --------------------------
254 	 xShape=saveAndLoadShape(1,0);
255 	 xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xShape);
256 	 // ----------------------------
257 	 assertEquals("Not Dash Line Style",LineStyle.DASH,xPropSet.getPropertyValue("LineStyle"));
258 	 aLineDash=(LineDash) xPropSet.getPropertyValue("LineDash");
259 	 assertEquals("Not Round Dash Style", DashStyle.ROUND ,aLineDash.Style);
260 	 }
261 
262 	 /**
263 	 * test Shape Line Color
264 	 * @throws Exception
265 	 */
266 
267 	 @Test
268 	 public void testShapeLineColor() throws Exception {
269 	 Point po = new Point(1000, 8000);
270 	 xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
271 	 XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
272 	 5000, 5000), "com.sun.star.drawing.EllipseShape");
273 	 xShapes.add(xShape);
274 	 XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
275 	 XPropertySet.class, xShape);
276 	 xPropSet.setPropertyValue("LineStyle", LineStyle.DASH);
277 	 xPropSet.setPropertyValue("LineColor", 0x00ff00);
278 	 xShape=saveAndLoadShape(1,0);
279 	 xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xShape);
280 	 // ----------------------------
281 	 assertEquals("Not Dash Line Style",LineStyle.DASH,xPropSet.getPropertyValue("LineStyle"));
282 	 assertEquals("wrong line color", 0x00ff00,xPropSet.getPropertyValue("LineColor"));
283 	 }
284 
285 	/**
286 	 * test Shape position and size
287 	 *
288 	 * @throws Exception
289 	 */
290 	 @Test
291 	 public void testShapePositionSize() throws Exception {
292 	 Point po = new Point(1000, 8000);
293 	 xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
294 	 XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
295 	 5000, 5000), "com.sun.star.drawing.EllipseShape");
296 	 xShapes.add(xShape);
297 	 xShape=saveAndLoadShape(1,0);
298 	 assertEquals("wrong shape position",1000,xShape.getPosition().X);
299 	 assertEquals("wrong shape Height", 5000 ,xShape.getSize().Height);
300 	 }
301 
302 	/**
303 	 * test Shape rotate with 25 degree.
304 	 * @throws Exception
305 	 */
306 	@Test
307 	public void testShapeRotation() throws Exception {
308 		Point po = new Point(1000, 8000);
309 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
310 		XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
311 				5000, 5000), "com.sun.star.drawing.RectangleShape");
312 		xShapes.add(xShape);
313 		XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(
314 				XPropertySet.class, xShape);
315 		xPropSet.setPropertyValue("RotateAngle", 2500);
316 		xShape=saveAndLoadShape(1,0);
317 		xPropSet = (XPropertySet) UnoRuntime.queryInterface(
318 				 XPropertySet.class, xShape);
319 		assertEquals("RotateAngle is not 2500", 2500, xPropSet.getPropertyValue("RotateAngle"));
320 	}
321 
322 
323 	// ------------------Shape Remove----------------------------
324 	/**
325 	 * test Remove shape from impress Document
326 	 *
327 	 * @throws Exception
328 	 */
329 	 @Test
330 	 public void testShapeRemove() throws Exception {
331 	 Point aPos=null;
332 	 Size aSize=new Size(4000,4000);
333 	 for(int i=0;i<=4;i++){
334 	 aPos=new Point(1000, 3000*i);
335 	 xShapes=ShapeUtil.getShapes(impressDocument, 0);
336 	 ShapeUtil.createAndInsertShape(impressDocument, xShapes, aPos, aSize,
337 	 "com.sun.star.drawing.EllipseShape");
338 	 }
339 	 int countBeforeRemove=ShapeUtil.getShapes(impressDocument, 0).getCount();
340 	 ShapeUtil.removeOneShape(impressDocument, 0, 2);
341 	 int countAfterRemove=ShapeUtil.getShapes(impressDocument, 0).getCount();
342 	 assertEquals("Not remove shape successfully",1,countBeforeRemove-countAfterRemove);
343 	 reLoadFile = saveAndReloadDoc(impressDocument,
344 	 "impress8", "odp");
345 	 xShapes=ShapeUtil.getShapes(reLoadFile, 0);
346 	 assertEquals("Shape count is wrong after reload",6,xShapes.getCount());
347 	 }
348 
349 	/**
350 	 * create a new presentation document and insert a new slide.
351 	 *
352 	 * @throws Exception
353 	 */
354 	public void createDocumentAndSlide() throws Exception {
355 		impressDocument = (XComponent) UnoRuntime.queryInterface(
356 				XComponent.class, unoApp.newDocument("simpress"));
357 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
358 				XDrawPagesSupplier.class, impressDocument);
359 		drawpages = drawsupplier.getDrawPages();
360 		drawpages.insertNewByIndex(1);
361 		xpage = PageUtil.getDrawPageByIndex(impressDocument, 1);
362 	}
363 
364 	/**
365 	 * Save presentation and reLoad the presentation and shape in it.
366 	 *
367 	 * @param po
368 	 * @param shapeType
369 	 * @return
370 	 * @throws Exception
371 	 */
372 	public XShape saveAndLoadShape(int pageIndex, int shapeIndex) throws Exception {
373 		reLoadFile = saveAndReloadDoc(impressDocument,
374 				"impress8", "odp");
375 		xShapes=ShapeUtil.getShapes(reLoadFile, pageIndex);
376 		return  (XShape) UnoRuntime.queryInterface(XShape.class, xShapes.getByIndex(shapeIndex));
377 	}
378 	/**
379 	 * save and reload Presentation document.
380 	 *
381 	 * @param presentationDocument
382 	 * @param sFilter
383 	 * @param sExtension
384 	 * @return
385 	 * @throws Exception
386 	 */
387 	private XComponent saveAndReloadDoc(XComponent presentationDocument,
388 			String sFilter, String sExtension) throws Exception {
389 		filePath = Testspace.getPath("tmp/presentationtest." + sExtension);
390 		PropertyValue[] aStoreProperties = new PropertyValue[2];
391 		aStoreProperties[0] = new PropertyValue();
392 		aStoreProperties[1] = new PropertyValue();
393 		aStoreProperties[0].Name = "Override";
394 		aStoreProperties[0].Value = true;
395 		aStoreProperties[1].Name = "FilterName";
396 		aStoreProperties[1].Value = sFilter;
397 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
398 				XStorable.class, presentationDocument);
399 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
400 
401 		return (XComponent) UnoRuntime.queryInterface(XComponent.class,
402 				unoApp.loadDocument(filePath));
403 	}
404 
405 }
406