170af8983SLiu Zhe /**************************************************************
270af8983SLiu Zhe  *
370af8983SLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
470af8983SLiu Zhe  * or more contributor license agreements.  See the NOTICE file
570af8983SLiu Zhe  * distributed with this work for additional information
670af8983SLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
770af8983SLiu Zhe  * to you under the Apache License, Version 2.0 (the
870af8983SLiu Zhe  * "License"); you may not use this file except in compliance
970af8983SLiu Zhe  * with the License.  You may obtain a copy of the License at
1070af8983SLiu Zhe  *
1170af8983SLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
1270af8983SLiu Zhe  *
1370af8983SLiu Zhe  * Unless required by applicable law or agreed to in writing,
1470af8983SLiu Zhe  * software distributed under the License is distributed on an
1570af8983SLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1670af8983SLiu Zhe  * KIND, either express or implied.  See the License for the
1770af8983SLiu Zhe  * specific language governing permissions and limitations
1870af8983SLiu Zhe  * under the License.
1970af8983SLiu Zhe  *
2070af8983SLiu Zhe  *************************************************************/
21*eba4d44aSLiu Zhe package fvt.uno.sd.chart;
2270af8983SLiu Zhe 
2370af8983SLiu Zhe import static org.junit.Assert.assertEquals;
2470af8983SLiu Zhe 
2570af8983SLiu Zhe import org.junit.After;
2670af8983SLiu Zhe import org.junit.Before;
2770af8983SLiu Zhe import org.junit.Test;
2870af8983SLiu Zhe import org.openoffice.test.common.FileUtil;
2970af8983SLiu Zhe import org.openoffice.test.common.Testspace;
3070af8983SLiu Zhe import org.openoffice.test.uno.UnoApp;
3170af8983SLiu Zhe 
3270af8983SLiu Zhe import testlib.uno.ChartUtil;
3370af8983SLiu Zhe import testlib.uno.PageUtil;
3470af8983SLiu Zhe import testlib.uno.ShapeUtil;
3570af8983SLiu Zhe import testlib.uno.TestUtil;
3670af8983SLiu Zhe 
3770af8983SLiu Zhe import com.sun.star.awt.FontRelief;
3870af8983SLiu Zhe import com.sun.star.awt.FontUnderline;
3970af8983SLiu Zhe import com.sun.star.awt.FontWeight;
4070af8983SLiu Zhe import com.sun.star.awt.Gradient;
4170af8983SLiu Zhe import com.sun.star.awt.GradientStyle;
4270af8983SLiu Zhe import com.sun.star.awt.Point;
4370af8983SLiu Zhe import com.sun.star.awt.Size;
4470af8983SLiu Zhe import com.sun.star.beans.PropertyValue;
4570af8983SLiu Zhe import com.sun.star.beans.XPropertySet;
4670af8983SLiu Zhe import com.sun.star.chart.ChartDataCaption;
4770af8983SLiu Zhe import com.sun.star.chart.ChartLegendPosition;
4870af8983SLiu Zhe import com.sun.star.chart.XAxisYSupplier;
4970af8983SLiu Zhe import com.sun.star.chart.XChartData;
5070af8983SLiu Zhe import com.sun.star.chart.XChartDataArray;
5170af8983SLiu Zhe import com.sun.star.chart.XChartDocument;
5270af8983SLiu Zhe import com.sun.star.chart.XDiagram;
5370af8983SLiu Zhe import com.sun.star.chart2.data.XDataSource;
5470af8983SLiu Zhe import com.sun.star.chart2.data.XLabeledDataSequence;
5570af8983SLiu Zhe import com.sun.star.drawing.DashStyle;
5670af8983SLiu Zhe import com.sun.star.drawing.FillStyle;
5770af8983SLiu Zhe import com.sun.star.drawing.LineDash;
5870af8983SLiu Zhe import com.sun.star.drawing.LineStyle;
5970af8983SLiu Zhe import com.sun.star.drawing.XDrawPage;
6070af8983SLiu Zhe import com.sun.star.drawing.XDrawPages;
6170af8983SLiu Zhe import com.sun.star.drawing.XDrawPagesSupplier;
6270af8983SLiu Zhe import com.sun.star.drawing.XShape;
6370af8983SLiu Zhe import com.sun.star.drawing.XShapes;
6470af8983SLiu Zhe import com.sun.star.frame.XStorable;
6570af8983SLiu Zhe import com.sun.star.lang.XComponent;
6670af8983SLiu Zhe import com.sun.star.presentation.XPresentation;
6770af8983SLiu Zhe import com.sun.star.presentation.XPresentationSupplier;
6870af8983SLiu Zhe import com.sun.star.uno.AnyConverter;
6970af8983SLiu Zhe import com.sun.star.uno.UnoRuntime;
7070af8983SLiu Zhe import com.sun.star.util.XCloseable;
7170af8983SLiu Zhe import com.sun.star.util.XModifiable;
7270af8983SLiu Zhe 
7370af8983SLiu Zhe public class ChartData {
7470af8983SLiu Zhe 	UnoApp unoApp = new UnoApp();
7570af8983SLiu Zhe 	XPresentationSupplier sdDocument = null;
7670af8983SLiu Zhe 	XPresentation pre = null;
7770af8983SLiu Zhe 	XComponent precomp = null;
7870af8983SLiu Zhe 	XComponent impressDocument = null;
7970af8983SLiu Zhe 	XComponent reLoadFile = null;
8070af8983SLiu Zhe 	XDrawPagesSupplier drawsupplier = null;
8170af8983SLiu Zhe 	XDrawPages drawpages = null;
8270af8983SLiu Zhe 	XShapes xShapes = null;
8370af8983SLiu Zhe 	XDrawPage xpage = null;
8470af8983SLiu Zhe 	String filePath = null;
8570af8983SLiu Zhe 	XChartDocument xChartDoc = null;
8670af8983SLiu Zhe 	com.sun.star.chart2.XChartDocument xChart2Doc = null;
8770af8983SLiu Zhe 
8870af8983SLiu Zhe 	@Before
setUp()8970af8983SLiu Zhe 	public void setUp() throws Exception {
9070af8983SLiu Zhe 		unoApp.start();
9170af8983SLiu Zhe 		createDocumentAndSlide();
9270af8983SLiu Zhe 	}
9370af8983SLiu Zhe 
9470af8983SLiu Zhe 	@After
tearDown()9570af8983SLiu Zhe 	public void tearDown() throws Exception {
9670af8983SLiu Zhe 		unoApp.closeDocument(impressDocument);
9770af8983SLiu Zhe 		unoApp.closeDocument(reLoadFile);
9870af8983SLiu Zhe 		unoApp.close();
9970af8983SLiu Zhe 		 if (filePath != null)
10070af8983SLiu Zhe 		 FileUtil.deleteFile(filePath);
10170af8983SLiu Zhe 	}
10270af8983SLiu Zhe 
10370af8983SLiu Zhe 	/**
10470af8983SLiu Zhe 	 * create a new presentation document and insert a new slide.
10570af8983SLiu Zhe 	 *
10670af8983SLiu Zhe 	 * @throws Exception
10770af8983SLiu Zhe 	 */
createDocumentAndSlide()10870af8983SLiu Zhe 	public void createDocumentAndSlide() throws Exception {
10970af8983SLiu Zhe 		impressDocument = (XComponent) UnoRuntime.queryInterface(
11070af8983SLiu Zhe 				XComponent.class, unoApp.newDocument("simpress"));
11170af8983SLiu Zhe 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
11270af8983SLiu Zhe 				XDrawPagesSupplier.class, impressDocument);
11370af8983SLiu Zhe 		drawpages = drawsupplier.getDrawPages();
11470af8983SLiu Zhe 		drawpages.insertNewByIndex(1);
11570af8983SLiu Zhe 		xpage = PageUtil.getDrawPageByIndex(impressDocument, 1);
11670af8983SLiu Zhe 	}
11770af8983SLiu Zhe 
11870af8983SLiu Zhe 	/**
11970af8983SLiu Zhe 	 * Insert default Column Chart to slide.
12070af8983SLiu Zhe 	 *
12170af8983SLiu Zhe 	 * @return
12270af8983SLiu Zhe 	 * @throws Exception
12370af8983SLiu Zhe 	 */
insertDefaultChart()12470af8983SLiu Zhe 	public XChartDocument insertDefaultChart() throws Exception {
12570af8983SLiu Zhe 		Point po = new Point(1000, 1000);
12670af8983SLiu Zhe 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
12770af8983SLiu Zhe 		XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
12870af8983SLiu Zhe 				15000, 9271), "com.sun.star.drawing.OLE2Shape");
12970af8983SLiu Zhe 		xShapes.add(xShape);
13070af8983SLiu Zhe 		xChartDoc = ChartUtil.retrieveChartDocument(xShape);
13170af8983SLiu Zhe 		return xChartDoc;
13270af8983SLiu Zhe 	}
13370af8983SLiu Zhe 
13470af8983SLiu Zhe 	/**
13570af8983SLiu Zhe 	 * test data series
13670af8983SLiu Zhe 	 * @throws Exception
13770af8983SLiu Zhe 	 */
13870af8983SLiu Zhe 	@Test
testDataSeries()13970af8983SLiu Zhe 	public void testDataSeries() throws Exception {
14070af8983SLiu Zhe 		XShape xShape = null;
14170af8983SLiu Zhe 		insertDefaultChart();
14270af8983SLiu Zhe 		// get the second data series.
14370af8983SLiu Zhe 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
14470af8983SLiu Zhe 				XPropertySet.class, xChartDoc.getDiagram()
14570af8983SLiu Zhe 						.getDataRowProperties(1));
14670af8983SLiu Zhe 		aDiaProp.setPropertyValue("FillStyle", FillStyle.SOLID);
14770af8983SLiu Zhe 		aDiaProp.setPropertyValue("FillColor", 0xffff00);
14870af8983SLiu Zhe 		// --------------------------
14970af8983SLiu Zhe 		xShape = saveAndLoadShape(1, 0);
15070af8983SLiu Zhe 		xChartDoc = ChartUtil.getChartDocument(xShape);
15170af8983SLiu Zhe 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
15270af8983SLiu Zhe 				xChartDoc.getDiagram().getDataRowProperties(1));
15370af8983SLiu Zhe 		assertEquals("the second DataSeries fill color isn't yellow", 0xffff00,
15470af8983SLiu Zhe 				aDiaProp.getPropertyValue("FillColor"));
15570af8983SLiu Zhe 
15670af8983SLiu Zhe 	}
15770af8983SLiu Zhe 	/**
15870af8983SLiu Zhe 	 * test data point
15970af8983SLiu Zhe 	 * @throws Exception
16070af8983SLiu Zhe 	 */
16170af8983SLiu Zhe 	@Test
testDataPoint()16270af8983SLiu Zhe 	public void testDataPoint() throws Exception {
16370af8983SLiu Zhe 		XShape xShape = null;
16470af8983SLiu Zhe 		insertDefaultChart();
16570af8983SLiu Zhe 		// set data label to the fourth points, the second series.
16670af8983SLiu Zhe 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
16770af8983SLiu Zhe 				XPropertySet.class, xChartDoc.getDiagram()
16870af8983SLiu Zhe 						.getDataPointProperties(3, 1));
16970af8983SLiu Zhe 		aDiaProp.setPropertyValue("FillStyle", FillStyle.SOLID);
17070af8983SLiu Zhe 		aDiaProp.setPropertyValue("FillColor", 0xffff00);
17170af8983SLiu Zhe 		// --------------------------
17270af8983SLiu Zhe 		xShape = saveAndLoadShape(1, 0);
17370af8983SLiu Zhe 		xChartDoc = ChartUtil.getChartDocument(xShape);
17470af8983SLiu Zhe 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
17570af8983SLiu Zhe 				xChartDoc.getDiagram().getDataPointProperties(3, 1));
17670af8983SLiu Zhe 
17770af8983SLiu Zhe 		assertEquals(
17870af8983SLiu Zhe 				"the fourth point of the second series's fill color isn't yellow",
17970af8983SLiu Zhe 				0xffff00, aDiaProp.getPropertyValue("FillColor"));
18070af8983SLiu Zhe 
18170af8983SLiu Zhe 	}
18270af8983SLiu Zhe 	/**
18370af8983SLiu Zhe 	 * test data of chart, update it with chart
18470af8983SLiu Zhe 	 * @throws Exception
18570af8983SLiu Zhe 	 */
18670af8983SLiu Zhe 	@Test
testDataTable()18770af8983SLiu Zhe 	public void testDataTable() throws Exception {
18870af8983SLiu Zhe 		XShape xShape = null;
18970af8983SLiu Zhe 		insertDefaultChart();
19070af8983SLiu Zhe 		// get data object from chart
19170af8983SLiu Zhe 		XChartDataArray array = (XChartDataArray) UnoRuntime.queryInterface(
19270af8983SLiu Zhe 				XChartDataArray.class, xChartDoc.getData());
19370af8983SLiu Zhe 
19470af8983SLiu Zhe 		// set new chart data
19570af8983SLiu Zhe 		double[][] data = new double[][] { { 2, 2 }, { 2, 2 }, { 2, 2 } };
19670af8983SLiu Zhe 		array.setData(data);
19770af8983SLiu Zhe 		// update chart with data
19870af8983SLiu Zhe 		xChartDoc.attachData(xChartDoc.getData());
19970af8983SLiu Zhe 		// --------------------------
20070af8983SLiu Zhe 		xShape = saveAndLoadShape(1, 0);
20170af8983SLiu Zhe 		xChartDoc = ChartUtil.getChartDocument(xShape);
20270af8983SLiu Zhe 		array = (XChartDataArray) UnoRuntime.queryInterface(
20370af8983SLiu Zhe 				XChartDataArray.class, xChartDoc.getData());
20470af8983SLiu Zhe 		data = array.getData();
20570af8983SLiu Zhe 		assertEquals(2, data[0][0], 0.0);
20670af8983SLiu Zhe 
20770af8983SLiu Zhe 	}
20870af8983SLiu Zhe 
20970af8983SLiu Zhe 	/**
21070af8983SLiu Zhe 	 * Save presentation and reLoad the presentation and shape in it.
21170af8983SLiu Zhe 	 *
21270af8983SLiu Zhe 	 * @param po
21370af8983SLiu Zhe 	 * @param shapeType
21470af8983SLiu Zhe 	 * @return
21570af8983SLiu Zhe 	 * @throws Exception
21670af8983SLiu Zhe 	 */
saveAndLoadShape(int pageIndex, int shapeIndex)21770af8983SLiu Zhe 	public XShape saveAndLoadShape(int pageIndex, int shapeIndex)
21870af8983SLiu Zhe 			throws Exception {
21970af8983SLiu Zhe 		reLoadFile = saveAsAndReloadDoc(impressDocument, "impress8", "odp");
22070af8983SLiu Zhe 		xShapes = ShapeUtil.getShapes(reLoadFile, pageIndex);
22170af8983SLiu Zhe 		return (XShape) UnoRuntime.queryInterface(XShape.class,
22270af8983SLiu Zhe 				xShapes.getByIndex(shapeIndex));
22370af8983SLiu Zhe 	}
22470af8983SLiu Zhe 
22570af8983SLiu Zhe 	/**
22670af8983SLiu Zhe 	 * save and reload Presentation document.
22770af8983SLiu Zhe 	 *
22870af8983SLiu Zhe 	 * @param presentationDocument
22970af8983SLiu Zhe 	 * @param sFilter
23070af8983SLiu Zhe 	 * @param sExtension
23170af8983SLiu Zhe 	 * @return
23270af8983SLiu Zhe 	 * @throws Exception
23370af8983SLiu Zhe 	 */
saveAsAndReloadDoc(XComponent presentationDocument, String sFilter, String sExtension)23470af8983SLiu Zhe 	private XComponent saveAsAndReloadDoc(XComponent presentationDocument,
23570af8983SLiu Zhe 			String sFilter, String sExtension) throws Exception {
23670af8983SLiu Zhe 		filePath = Testspace.getPath("tmp/chartdata." + sExtension);
23770af8983SLiu Zhe 		PropertyValue[] aStoreProperties = new PropertyValue[2];
23870af8983SLiu Zhe 		aStoreProperties[0] = new PropertyValue();
23970af8983SLiu Zhe 		aStoreProperties[1] = new PropertyValue();
24070af8983SLiu Zhe 		aStoreProperties[0].Name = "Override";
24170af8983SLiu Zhe 		aStoreProperties[0].Value = true;
24270af8983SLiu Zhe 		aStoreProperties[1].Name = "FilterName";
24370af8983SLiu Zhe 		aStoreProperties[1].Value = sFilter;
24470af8983SLiu Zhe 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
24570af8983SLiu Zhe 				XStorable.class, presentationDocument);
24670af8983SLiu Zhe 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
24770af8983SLiu Zhe 
2484c280f34SLinyi Li 		return (XComponent) UnoRuntime.queryInterface(XComponent.class,
24970af8983SLiu Zhe 				unoApp.loadDocument(filePath));
25070af8983SLiu Zhe 	}
25170af8983SLiu Zhe 
25270af8983SLiu Zhe }
253