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.chart;
22 
23 import static org.junit.Assert.assertEquals;
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.ChartUtil;
33 import testlib.uno.PageUtil;
34 import testlib.uno.ShapeUtil;
35 import testlib.uno.TestUtil;
36 
37 import com.sun.star.awt.FontRelief;
38 import com.sun.star.awt.FontUnderline;
39 import com.sun.star.awt.FontWeight;
40 import com.sun.star.awt.Point;
41 import com.sun.star.awt.Size;
42 import com.sun.star.beans.PropertyValue;
43 import com.sun.star.beans.XPropertySet;
44 import com.sun.star.chart.ChartDataCaption;
45 import com.sun.star.chart.ChartLegendPosition;
46 import com.sun.star.chart.XAxisYSupplier;
47 import com.sun.star.chart.XChartDocument;
48 import com.sun.star.chart.XDiagram;
49 import com.sun.star.drawing.FillStyle;
50 import com.sun.star.drawing.LineStyle;
51 import com.sun.star.drawing.XDrawPage;
52 import com.sun.star.drawing.XDrawPages;
53 import com.sun.star.drawing.XDrawPagesSupplier;
54 import com.sun.star.drawing.XShape;
55 import com.sun.star.drawing.XShapes;
56 import com.sun.star.frame.XStorable;
57 import com.sun.star.lang.XComponent;
58 import com.sun.star.presentation.XPresentation;
59 import com.sun.star.presentation.XPresentationSupplier;
60 import com.sun.star.uno.UnoRuntime;
61 import com.sun.star.util.XCloseable;
62 import com.sun.star.util.XModifiable;
63 
64 public class ChartProperties {
65 	UnoApp unoApp = new UnoApp();
66 	XPresentationSupplier sdDocument = null;
67 	XPresentation pre = null;
68 	XComponent precomp = null;
69 	XComponent impressDocument = null;
70 	XComponent reLoadFile = null;
71 	XDrawPagesSupplier drawsupplier = null;
72 	XDrawPages drawpages = null;
73 	XShapes xShapes = null;
74 	XDrawPage xpage = null;
75 	String filePath = null;
76 	XChartDocument xChartDoc = null;
77 	com.sun.star.chart2.XChartDocument xChart2Doc = null;
78 
79 	@Before
setUp()80 	public void setUp() throws Exception {
81 		unoApp.start();
82 		createDocumentAndSlide();
83 	}
84 
85 	@After
tearDown()86 	public void tearDown() throws Exception {
87 		unoApp.closeDocument(impressDocument);
88 		unoApp.closeDocument(reLoadFile);
89 		unoApp.close();
90 		 if (filePath != null)
91 		 FileUtil.deleteFile(filePath);
92 	}
93 
94 	/**
95 	 * create a new presentation document and insert a new slide.
96 	 *
97 	 * @throws Exception
98 	 */
createDocumentAndSlide()99 	public void createDocumentAndSlide() throws Exception {
100 		impressDocument = (XComponent) UnoRuntime.queryInterface(
101 				XComponent.class, unoApp.newDocument("simpress"));
102 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
103 				XDrawPagesSupplier.class, impressDocument);
104 		drawpages = drawsupplier.getDrawPages();
105 		drawpages.insertNewByIndex(1);
106 		xpage = PageUtil.getDrawPageByIndex(impressDocument, 1);
107 	}
108 
109 	/**
110 	 * Insert default Column Chart to slide.
111 	 *
112 	 * @return
113 	 * @throws Exception
114 	 */
insertDefaultChart()115 	public XChartDocument insertDefaultChart() throws Exception {
116 		Point po = new Point(1000, 1000);
117 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
118 		XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
119 				15000, 9271), "com.sun.star.drawing.OLE2Shape");
120 		xShapes.add(xShape);
121 		xChartDoc = ChartUtil.retrieveChartDocument(xShape);
122 		return xChartDoc;
123 	}
124 
125 	/**
126 	 * Insert default Column Chart2 to slide.
127 	 *
128 	 * @return
129 	 * @throws Exception
130 	 */
insertDefaultChart2()131 	public com.sun.star.chart2.XChartDocument insertDefaultChart2()
132 			throws Exception {
133 		Point po = new Point(1000, 1000);
134 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
135 		XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
136 				15000, 9271), "com.sun.star.drawing.OLE2Shape");
137 		xShapes.add(xShape);
138 		xChart2Doc = ChartUtil.retrieveChart2Document(xShape);
139 		return xChart2Doc;
140 	}
141 
142 	// --------------Chart Area----------------------
143 	/**
144 	 * test Chart Area properties about Border Line
145 	 *
146 	 * @throws Exception
147 	 */
148 	@Test
testAreaBordersProperties()149 	public void testAreaBordersProperties() throws Exception {
150 		XShape xShape = null;
151 		insertDefaultChart();
152 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
153 				XPropertySet.class, xChartDoc.getArea());
154 		aDiaProp.setPropertyValue("LineStyle", LineStyle.DASH);
155 		aDiaProp.setPropertyValue("LineColor", 0x00ff00);
156 
157 		// --------------------------
158 		xShape = saveAndLoadShape(1, 0);
159 		xChartDoc = ChartUtil.getChartDocument(xShape);
160 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
161 				xChartDoc.getArea());
162 		// ----------------------------
163 		assertEquals("Not Dash Line Style", LineStyle.DASH,
164 				aDiaProp.getPropertyValue("LineStyle"));
165 		assertEquals("Not Yellow Line", 0x00ff00,
166 				aDiaProp.getPropertyValue("LineColor"));
167 
168 	}
169 
170 	/**
171 	 * test Area Properties about Gradient Fill
172 	 * @throws Exception
173 	 */
174 	@Test
testAreaFillProperties()175 	public void testAreaFillProperties() throws Exception {
176 		XShape xShape = null;
177 		insertDefaultChart();
178 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
179 				XPropertySet.class, xChartDoc.getArea());
180 		aDiaProp.setPropertyValue("FillStyle", FillStyle.GRADIENT);
181 		aDiaProp.setPropertyValue("FillGradientName", "Radial red/yellow");
182 		// --------------------------
183 		xShape = saveAndLoadShape(1, 0);
184 		xChartDoc = ChartUtil.getChartDocument(xShape);
185 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
186 				xChartDoc.getArea());
187 		// ----------------------------
188 		assertEquals("Not Gradient Fill Style", FillStyle.GRADIENT,
189 				aDiaProp.getPropertyValue("FillStyle"));
190 		assertEquals("Not Radial red/yellow Gradient", "Radial red/yellow",
191 				aDiaProp.getPropertyValue("FillGradientName"));
192 
193 	}
194 
195 	/**
196 	 * test Area properties about Transparency
197 	 * @throws Exception
198 	 */
199 	@Test
testAreaTransparencyProperties()200 	public void testAreaTransparencyProperties() throws Exception {
201 		XShape xShape = null;
202 		insertDefaultChart();
203 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
204 				XPropertySet.class, xChartDoc.getArea());
205 		aDiaProp.setPropertyValue("FillStyle", FillStyle.SOLID);
206 		aDiaProp.setPropertyValue("FillColor", 0xffff00);
207 		aDiaProp.setPropertyValue("FillTransparence", 50);
208 		// --------------------------
209 		xShape = saveAndLoadShape(1, 0);
210 		xChartDoc = ChartUtil.getChartDocument(xShape);
211 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
212 				xChartDoc.getArea());
213 		// ----------------------------
214 		assertEquals("Area Fill Transparency is not 50%", new Short("50"),
215 				aDiaProp.getPropertyValue("FillTransparence"));
216 
217 	}
218 
219 	// ------------Chart Legend-------------------
220 	/**
221 	 * test Legend Border Line Properties
222 	 * @throws Exception
223 	 */
224 	@Test
testLegendBorderProperties()225 	public void testLegendBorderProperties() throws Exception {
226 		XShape xShape = null;
227 		insertDefaultChart();
228 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
229 				XPropertySet.class, xChartDoc.getLegend());
230 		aDiaProp.setPropertyValue("LineStyle", LineStyle.DASH);
231 		aDiaProp.setPropertyValue("LineColor", 0x00ff00);
232 		// --------------------------
233 		xShape = saveAndLoadShape(1, 0);
234 		xChartDoc = ChartUtil.getChartDocument(xShape);
235 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
236 				xChartDoc.getLegend());
237 		// ----------------------------
238 		assertEquals("Not Dash Line Style", LineStyle.DASH,
239 				aDiaProp.getPropertyValue("LineStyle"));
240 		assertEquals("Not Yellow Line", 0x00ff00,
241 				aDiaProp.getPropertyValue("LineColor"));
242 
243 	}
244 
245 	/**
246 	 * test Legend Fill and transparence properties
247 	 * @throws Exception
248 	 */
249 	@Test
testLegendFillAndTransparence()250 	public void testLegendFillAndTransparence() throws Exception {
251 		XShape xShape = null;
252 		insertDefaultChart();
253 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
254 				XPropertySet.class, xChartDoc.getLegend());
255 		aDiaProp.setPropertyValue("FillStyle", FillStyle.SOLID);
256 		aDiaProp.setPropertyValue("FillColor", 0xffff00);
257 		aDiaProp.setPropertyValue("FillTransparence", 50);
258 		// --------------------------
259 		xShape = saveAndLoadShape(1, 0);
260 		xChartDoc = ChartUtil.getChartDocument(xShape);
261 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
262 				xChartDoc.getLegend());
263 		// ----------------------------
264 
265 		assertEquals("Area Fill Transparency is not 50%", new Short("50"),
266 				aDiaProp.getPropertyValue("FillTransparence"));
267 
268 	}
269 
270 	/**
271 	 * test Legend Char Font
272 	 * @throws Exception
273 	 */
274 	@Test
testLegendCharFont()275 	public void testLegendCharFont() throws Exception {
276 		XShape xShape = null;
277 		insertDefaultChart();
278 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
279 				XPropertySet.class, xChartDoc.getLegend());
280 		aDiaProp.setPropertyValue("CharFontName", "Arial");
281 		aDiaProp.setPropertyValue("CharWeight", FontWeight.BOLD);
282 		// --------------------------
283 		xShape = saveAndLoadShape(1, 0);
284 		xChartDoc = ChartUtil.getChartDocument(xShape);
285 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
286 				xChartDoc.getLegend());
287 		// ----------------------------
288 		assertEquals("Legend font name is not Arial", "Arial",
289 				aDiaProp.getPropertyValue("CharFontName"));
290 		assertEquals("Legend font weight is not BOLD", FontWeight.BOLD,
291 				aDiaProp.getPropertyValue("CharWeight"));
292 
293 	}
294 
295 	/**
296 	 * test Legend Char Font Effects
297 	 * @throws Exception
298 	 */
299 	@Test
testLegendCharFontEffects()300 	public void testLegendCharFontEffects() throws Exception {
301 		XShape xShape = null;
302 		insertDefaultChart();
303 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
304 				XPropertySet.class, xChartDoc.getLegend());
305 		aDiaProp.setPropertyValue("CharColor", 0xffff00);
306 		aDiaProp.setPropertyValue("CharUnderline", FontUnderline.DOUBLE);
307 		aDiaProp.setPropertyValue("CharUnderlineColor", 0xffff00);
308 		aDiaProp.setPropertyValue("CharRelief", FontRelief.ENGRAVED);
309 
310 		// --------------------------
311 		xShape = saveAndLoadShape(1, 0);
312 		xChartDoc = ChartUtil.getChartDocument(xShape);
313 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
314 				xChartDoc.getLegend());
315 		// ----------------------------
316 		assertEquals("Legend font color is not yellow", 0xffff00,
317 				aDiaProp.getPropertyValue("CharColor"));
318 		assertEquals("Legend font underline is not DOUBLE",
319 				FontUnderline.DOUBLE,
320 				aDiaProp.getPropertyValue("CharUnderline"));
321 		assertEquals("Legend font underline color is not yellow", 0xffff00,
322 				aDiaProp.getPropertyValue("CharUnderlineColor"));
323 		assertEquals("Legend font relief is not engraved", FontRelief.ENGRAVED,
324 				aDiaProp.getPropertyValue("CharRelief"));
325 
326 	}
327 
328 	/**
329 	 * test Legend Position in Chart
330 	 * @throws Exception
331 	 */
332 	@Test
testLegendposition()333 	public void testLegendposition() throws Exception {
334 		XShape xShape = null;
335 		insertDefaultChart();
336 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
337 				XPropertySet.class, xChartDoc.getLegend());
338 		aDiaProp.setPropertyValue("Alignment", ChartLegendPosition.LEFT);
339 
340 		// --------------------------
341 		xShape = saveAndLoadShape(1, 0);
342 		xChartDoc = ChartUtil.getChartDocument(xShape);
343 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
344 				xChartDoc.getLegend());
345 		// ----------------------------
346 		assertEquals("Legend position is not left", ChartLegendPosition.LEFT,
347 				aDiaProp.getPropertyValue("Alignment"));
348 
349 	}
350 
351 	// --------------Chart Title---------------
352 	/**
353 	 * test Addition and Modification of Title and subTitle
354 	 * @throws Exception
355 	 */
356 	@Test
testTitleAndSubTitle()357 	public void testTitleAndSubTitle() throws Exception {
358 		XShape xShape = null;
359 		insertDefaultChart();
360 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
361 				XPropertySet.class, xChartDoc.getTitle());
362 		aDiaProp.setPropertyValue("String", "TestTitle");
363 		XPropertySet aDiaProp2 = (XPropertySet) UnoRuntime.queryInterface(
364 				XPropertySet.class, xChartDoc.getSubTitle());
365 		aDiaProp2.setPropertyValue("String", "TestSubTitle");
366 		// save and load shape
367 		xShape = saveAndLoadShape(1, 0);
368 		xChartDoc = ChartUtil.getChartDocument(xShape);
369 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
370 				xChartDoc.getTitle());
371 		aDiaProp2 = (XPropertySet) UnoRuntime.queryInterface(
372 				XPropertySet.class, xChartDoc.getSubTitle());
373 		assertEquals("Chart title is wrong", "TestTitle",
374 				aDiaProp.getPropertyValue("String"));
375 		assertEquals("Chart sub title is wrong", "TestSubTitle",
376 				aDiaProp2.getPropertyValue("String"));
377 		// modify title and subtitle
378 		aDiaProp.setPropertyValue("String", "AnotherTestTitle");
379 		aDiaProp2.setPropertyValue("String", "AnotherTestSubTitle");
380 
381 		// close document and save modification.
382 		XModifiable modified = (XModifiable) UnoRuntime.queryInterface(
383 				XModifiable.class, impressDocument);
384 		XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
385 				XCloseable.class, impressDocument);
386 		if (modified != null)
387 			modified.setModified(true);
388 		closer.close(true);
389 
390 		// load and get Chart
391 		impressDocument = (XComponent) UnoRuntime.queryInterface(XComponent.class,
392 				unoApp.loadDocument(filePath));
393 		xShapes = ShapeUtil.getShapes(reLoadFile, 1);
394 		xShape = (XShape) UnoRuntime.queryInterface(XShape.class,
395 				xShapes.getByIndex(0));
396 		xChartDoc = ChartUtil.getChartDocument(xShape);
397 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
398 				xChartDoc.getTitle());
399 		aDiaProp2 = (XPropertySet) UnoRuntime.queryInterface(
400 				XPropertySet.class, xChartDoc.getSubTitle());
401 		assertEquals("Chart title is wrong", "AnotherTestTitle",
402 				aDiaProp.getPropertyValue("String"));
403 		assertEquals("Chart sub title is wrong", "AnotherTestSubTitle",
404 				aDiaProp2.getPropertyValue("String"));
405 	}
406 
407 	/**
408 	 * test Data label and show it as value
409 	 * @throws Exception
410 	 */
411 	@Test
testDataLabel()412 	public void testDataLabel() throws Exception {
413 		XShape xShape = null;
414 		insertDefaultChart();
415 		// set data label to the fourth points, the second series.
416 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
417 				XPropertySet.class, xChartDoc.getDiagram()
418 						.getDataPointProperties(3, 1));
419 		aDiaProp.setPropertyValue("DataCaption", ChartDataCaption.VALUE);
420 
421 		// --------------------------
422 		xShape = saveAndLoadShape(1, 0);
423 		xChartDoc = ChartUtil.getChartDocument(xShape);
424 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
425 				xChartDoc.getDiagram().getDataPointProperties(3, 1));
426 
427 		assertEquals(
428 				"Data label of fourth points, the second series not show as value",
429 				ChartDataCaption.VALUE,
430 				aDiaProp.getPropertyValue("DataCaption"));
431 
432 	}
433 
434 	/**
435 	 * test GridLine of Y Axis.
436 	 * @throws Exception
437 	 */
438 	@Test
testGridLine()439 	public void testGridLine() throws Exception {
440 		XShape xShape = null;
441 		insertDefaultChart();
442 		XDiagram xdiagram = xChartDoc.getDiagram();
443 		// get Y axis
444 		XAxisYSupplier aYAxisSupplier = (XAxisYSupplier) UnoRuntime
445 				.queryInterface(XAxisYSupplier.class, xdiagram);
446 		// get Y axis's gridline
447 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
448 				XPropertySet.class, aYAxisSupplier.getYMainGrid());
449 		aDiaProp.setPropertyValue("LineColor", 0xffff00);
450 
451 		// --------------------------
452 		xShape = saveAndLoadShape(1, 0);
453 		xChartDoc = ChartUtil.getChartDocument(xShape);
454 		xdiagram = xChartDoc.getDiagram();
455 		aYAxisSupplier = (XAxisYSupplier) UnoRuntime.queryInterface(
456 				XAxisYSupplier.class, xdiagram);
457 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
458 				aYAxisSupplier.getYMainGrid());
459 		assertEquals("the Y grid line color is not yellow", 0xffff00,
460 				aDiaProp.getPropertyValue("LineColor"));
461 	}
462 
463 	/**
464 	 * test Wall Fill properties
465 	 * @throws Exception
466 	 */
467 	@Test
testWall()468 	public void testWall() throws Exception {
469 		XShape xShape = null;
470 		insertDefaultChart2();
471 		com.sun.star.chart2.XDiagram xDiagram2 = xChart2Doc.getFirstDiagram();
472 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
473 				XPropertySet.class, xDiagram2.getWall());
474 		aDiaProp.setPropertyValue("FillStyle", FillStyle.SOLID);
475 		aDiaProp.setPropertyValue("FillColor", 0xffff00);
476 		// --------------------------
477 		xShape = saveAndLoadShape(1, 0);
478 		xChart2Doc = ChartUtil.getChart2Document(xShape);
479 		xDiagram2 = xChart2Doc.getFirstDiagram();
480 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
481 				xDiagram2.getWall());
482 		assertEquals("the Chart wall is not yellow", 0xffff00,
483 				aDiaProp.getPropertyValue("FillColor"));
484 	}
485 
486 	/**
487 	 * Save presentation and reLoad the presentation and shape in it.
488 	 *
489 	 * @param po
490 	 * @param shapeType
491 	 * @return
492 	 * @throws Exception
493 	 */
saveAndLoadShape(int pageIndex, int shapeIndex)494 	public XShape saveAndLoadShape(int pageIndex, int shapeIndex)
495 			throws Exception {
496 		reLoadFile = saveAsAndReloadDoc(impressDocument, "impress8", "odp");
497 		xShapes = ShapeUtil.getShapes(reLoadFile, pageIndex);
498 		return (XShape) UnoRuntime.queryInterface(XShape.class,
499 				xShapes.getByIndex(shapeIndex));
500 	}
501 
502 	/**
503 	 * save and reload Presentation document.
504 	 *
505 	 * @param presentationDocument
506 	 * @param sFilter
507 	 * @param sExtension
508 	 * @return
509 	 * @throws Exception
510 	 */
saveAsAndReloadDoc(XComponent presentationDocument, String sFilter, String sExtension)511 	private XComponent saveAsAndReloadDoc(XComponent presentationDocument,
512 			String sFilter, String sExtension) throws Exception {
513 		filePath = Testspace.getPath("tmp/chartproperties." + sExtension);
514 		PropertyValue[] aStoreProperties = new PropertyValue[2];
515 		aStoreProperties[0] = new PropertyValue();
516 		aStoreProperties[1] = new PropertyValue();
517 		aStoreProperties[0].Name = "Override";
518 		aStoreProperties[0].Value = true;
519 		aStoreProperties[1].Name = "FilterName";
520 		aStoreProperties[1].Value = sFilter;
521 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
522 				XStorable.class, presentationDocument);
523 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
524 
525 		return (XComponent) UnoRuntime.queryInterface(XComponent.class,
526 				unoApp.loadDocument(filePath));
527 	}
528 
529 }
530