xref: /AOO41X/test/testuno/source/fvt/uno/sd/chart/ChartData.java (revision eba4d44a33e5be0b2528d5a9a6f0dcbf65adaa0d)
1*eba4d44aSLiu Zhe /**************************************************************
2*eba4d44aSLiu Zhe  *
3*eba4d44aSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4*eba4d44aSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5*eba4d44aSLiu Zhe  * distributed with this work for additional information
6*eba4d44aSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7*eba4d44aSLiu Zhe  * to you under the Apache License, Version 2.0 (the
8*eba4d44aSLiu Zhe  * "License"); you may not use this file except in compliance
9*eba4d44aSLiu Zhe  * with the License.  You may obtain a copy of the License at
10*eba4d44aSLiu Zhe  *
11*eba4d44aSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12*eba4d44aSLiu Zhe  *
13*eba4d44aSLiu Zhe  * Unless required by applicable law or agreed to in writing,
14*eba4d44aSLiu Zhe  * software distributed under the License is distributed on an
15*eba4d44aSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*eba4d44aSLiu Zhe  * KIND, either express or implied.  See the License for the
17*eba4d44aSLiu Zhe  * specific language governing permissions and limitations
18*eba4d44aSLiu Zhe  * under the License.
19*eba4d44aSLiu Zhe  *
20*eba4d44aSLiu Zhe  *************************************************************/
21*eba4d44aSLiu Zhe package fvt.uno.sd.chart;
22*eba4d44aSLiu Zhe 
23*eba4d44aSLiu Zhe import static org.junit.Assert.assertEquals;
24*eba4d44aSLiu Zhe 
25*eba4d44aSLiu Zhe import org.junit.After;
26*eba4d44aSLiu Zhe import org.junit.Before;
27*eba4d44aSLiu Zhe import org.junit.Test;
28*eba4d44aSLiu Zhe import org.openoffice.test.common.FileUtil;
29*eba4d44aSLiu Zhe import org.openoffice.test.common.Testspace;
30*eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp;
31*eba4d44aSLiu Zhe 
32*eba4d44aSLiu Zhe import testlib.uno.ChartUtil;
33*eba4d44aSLiu Zhe import testlib.uno.PageUtil;
34*eba4d44aSLiu Zhe import testlib.uno.ShapeUtil;
35*eba4d44aSLiu Zhe import testlib.uno.TestUtil;
36*eba4d44aSLiu Zhe 
37*eba4d44aSLiu Zhe import com.sun.star.awt.FontRelief;
38*eba4d44aSLiu Zhe import com.sun.star.awt.FontUnderline;
39*eba4d44aSLiu Zhe import com.sun.star.awt.FontWeight;
40*eba4d44aSLiu Zhe import com.sun.star.awt.Gradient;
41*eba4d44aSLiu Zhe import com.sun.star.awt.GradientStyle;
42*eba4d44aSLiu Zhe import com.sun.star.awt.Point;
43*eba4d44aSLiu Zhe import com.sun.star.awt.Size;
44*eba4d44aSLiu Zhe import com.sun.star.beans.PropertyValue;
45*eba4d44aSLiu Zhe import com.sun.star.beans.XPropertySet;
46*eba4d44aSLiu Zhe import com.sun.star.chart.ChartDataCaption;
47*eba4d44aSLiu Zhe import com.sun.star.chart.ChartLegendPosition;
48*eba4d44aSLiu Zhe import com.sun.star.chart.XAxisYSupplier;
49*eba4d44aSLiu Zhe import com.sun.star.chart.XChartData;
50*eba4d44aSLiu Zhe import com.sun.star.chart.XChartDataArray;
51*eba4d44aSLiu Zhe import com.sun.star.chart.XChartDocument;
52*eba4d44aSLiu Zhe import com.sun.star.chart.XDiagram;
53*eba4d44aSLiu Zhe import com.sun.star.chart2.data.XDataSource;
54*eba4d44aSLiu Zhe import com.sun.star.chart2.data.XLabeledDataSequence;
55*eba4d44aSLiu Zhe import com.sun.star.drawing.DashStyle;
56*eba4d44aSLiu Zhe import com.sun.star.drawing.FillStyle;
57*eba4d44aSLiu Zhe import com.sun.star.drawing.LineDash;
58*eba4d44aSLiu Zhe import com.sun.star.drawing.LineStyle;
59*eba4d44aSLiu Zhe import com.sun.star.drawing.XDrawPage;
60*eba4d44aSLiu Zhe import com.sun.star.drawing.XDrawPages;
61*eba4d44aSLiu Zhe import com.sun.star.drawing.XDrawPagesSupplier;
62*eba4d44aSLiu Zhe import com.sun.star.drawing.XShape;
63*eba4d44aSLiu Zhe import com.sun.star.drawing.XShapes;
64*eba4d44aSLiu Zhe import com.sun.star.frame.XStorable;
65*eba4d44aSLiu Zhe import com.sun.star.lang.XComponent;
66*eba4d44aSLiu Zhe import com.sun.star.presentation.XPresentation;
67*eba4d44aSLiu Zhe import com.sun.star.presentation.XPresentationSupplier;
68*eba4d44aSLiu Zhe import com.sun.star.uno.AnyConverter;
69*eba4d44aSLiu Zhe import com.sun.star.uno.UnoRuntime;
70*eba4d44aSLiu Zhe import com.sun.star.util.XCloseable;
71*eba4d44aSLiu Zhe import com.sun.star.util.XModifiable;
72*eba4d44aSLiu Zhe 
73*eba4d44aSLiu Zhe public class ChartData {
74*eba4d44aSLiu Zhe 	UnoApp unoApp = new UnoApp();
75*eba4d44aSLiu Zhe 	XPresentationSupplier sdDocument = null;
76*eba4d44aSLiu Zhe 	XPresentation pre = null;
77*eba4d44aSLiu Zhe 	XComponent precomp = null;
78*eba4d44aSLiu Zhe 	XComponent impressDocument = null;
79*eba4d44aSLiu Zhe 	XComponent reLoadFile = null;
80*eba4d44aSLiu Zhe 	XDrawPagesSupplier drawsupplier = null;
81*eba4d44aSLiu Zhe 	XDrawPages drawpages = null;
82*eba4d44aSLiu Zhe 	XShapes xShapes = null;
83*eba4d44aSLiu Zhe 	XDrawPage xpage = null;
84*eba4d44aSLiu Zhe 	String filePath = null;
85*eba4d44aSLiu Zhe 	XChartDocument xChartDoc = null;
86*eba4d44aSLiu Zhe 	com.sun.star.chart2.XChartDocument xChart2Doc = null;
87*eba4d44aSLiu Zhe 
88*eba4d44aSLiu Zhe 	@Before
setUp()89*eba4d44aSLiu Zhe 	public void setUp() throws Exception {
90*eba4d44aSLiu Zhe 		unoApp.start();
91*eba4d44aSLiu Zhe 		createDocumentAndSlide();
92*eba4d44aSLiu Zhe 	}
93*eba4d44aSLiu Zhe 
94*eba4d44aSLiu Zhe 	@After
tearDown()95*eba4d44aSLiu Zhe 	public void tearDown() throws Exception {
96*eba4d44aSLiu Zhe 		unoApp.closeDocument(impressDocument);
97*eba4d44aSLiu Zhe 		unoApp.closeDocument(reLoadFile);
98*eba4d44aSLiu Zhe 		unoApp.close();
99*eba4d44aSLiu Zhe 		 if (filePath != null)
100*eba4d44aSLiu Zhe 		 FileUtil.deleteFile(filePath);
101*eba4d44aSLiu Zhe 	}
102*eba4d44aSLiu Zhe 
103*eba4d44aSLiu Zhe 	/**
104*eba4d44aSLiu Zhe 	 * create a new presentation document and insert a new slide.
105*eba4d44aSLiu Zhe 	 *
106*eba4d44aSLiu Zhe 	 * @throws Exception
107*eba4d44aSLiu Zhe 	 */
createDocumentAndSlide()108*eba4d44aSLiu Zhe 	public void createDocumentAndSlide() throws Exception {
109*eba4d44aSLiu Zhe 		impressDocument = (XComponent) UnoRuntime.queryInterface(
110*eba4d44aSLiu Zhe 				XComponent.class, unoApp.newDocument("simpress"));
111*eba4d44aSLiu Zhe 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
112*eba4d44aSLiu Zhe 				XDrawPagesSupplier.class, impressDocument);
113*eba4d44aSLiu Zhe 		drawpages = drawsupplier.getDrawPages();
114*eba4d44aSLiu Zhe 		drawpages.insertNewByIndex(1);
115*eba4d44aSLiu Zhe 		xpage = PageUtil.getDrawPageByIndex(impressDocument, 1);
116*eba4d44aSLiu Zhe 	}
117*eba4d44aSLiu Zhe 
118*eba4d44aSLiu Zhe 	/**
119*eba4d44aSLiu Zhe 	 * Insert default Column Chart to slide.
120*eba4d44aSLiu Zhe 	 *
121*eba4d44aSLiu Zhe 	 * @return
122*eba4d44aSLiu Zhe 	 * @throws Exception
123*eba4d44aSLiu Zhe 	 */
insertDefaultChart()124*eba4d44aSLiu Zhe 	public XChartDocument insertDefaultChart() throws Exception {
125*eba4d44aSLiu Zhe 		Point po = new Point(1000, 1000);
126*eba4d44aSLiu Zhe 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
127*eba4d44aSLiu Zhe 		XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
128*eba4d44aSLiu Zhe 				15000, 9271), "com.sun.star.drawing.OLE2Shape");
129*eba4d44aSLiu Zhe 		xShapes.add(xShape);
130*eba4d44aSLiu Zhe 		xChartDoc = ChartUtil.retrieveChartDocument(xShape);
131*eba4d44aSLiu Zhe 		return xChartDoc;
132*eba4d44aSLiu Zhe 	}
133*eba4d44aSLiu Zhe 
134*eba4d44aSLiu Zhe 	/**
135*eba4d44aSLiu Zhe 	 * test data series
136*eba4d44aSLiu Zhe 	 * @throws Exception
137*eba4d44aSLiu Zhe 	 */
138*eba4d44aSLiu Zhe 	@Test
testDataSeries()139*eba4d44aSLiu Zhe 	public void testDataSeries() throws Exception {
140*eba4d44aSLiu Zhe 		XShape xShape = null;
141*eba4d44aSLiu Zhe 		insertDefaultChart();
142*eba4d44aSLiu Zhe 		// get the second data series.
143*eba4d44aSLiu Zhe 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
144*eba4d44aSLiu Zhe 				XPropertySet.class, xChartDoc.getDiagram()
145*eba4d44aSLiu Zhe 						.getDataRowProperties(1));
146*eba4d44aSLiu Zhe 		aDiaProp.setPropertyValue("FillStyle", FillStyle.SOLID);
147*eba4d44aSLiu Zhe 		aDiaProp.setPropertyValue("FillColor", 0xffff00);
148*eba4d44aSLiu Zhe 		// --------------------------
149*eba4d44aSLiu Zhe 		xShape = saveAndLoadShape(1, 0);
150*eba4d44aSLiu Zhe 		xChartDoc = ChartUtil.getChartDocument(xShape);
151*eba4d44aSLiu Zhe 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
152*eba4d44aSLiu Zhe 				xChartDoc.getDiagram().getDataRowProperties(1));
153*eba4d44aSLiu Zhe 		assertEquals("the second DataSeries fill color isn't yellow", 0xffff00,
154*eba4d44aSLiu Zhe 				aDiaProp.getPropertyValue("FillColor"));
155*eba4d44aSLiu Zhe 
156*eba4d44aSLiu Zhe 	}
157*eba4d44aSLiu Zhe 	/**
158*eba4d44aSLiu Zhe 	 * test data point
159*eba4d44aSLiu Zhe 	 * @throws Exception
160*eba4d44aSLiu Zhe 	 */
161*eba4d44aSLiu Zhe 	@Test
testDataPoint()162*eba4d44aSLiu Zhe 	public void testDataPoint() throws Exception {
163*eba4d44aSLiu Zhe 		XShape xShape = null;
164*eba4d44aSLiu Zhe 		insertDefaultChart();
165*eba4d44aSLiu Zhe 		// set data label to the fourth points, the second series.
166*eba4d44aSLiu Zhe 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
167*eba4d44aSLiu Zhe 				XPropertySet.class, xChartDoc.getDiagram()
168*eba4d44aSLiu Zhe 						.getDataPointProperties(3, 1));
169*eba4d44aSLiu Zhe 		aDiaProp.setPropertyValue("FillStyle", FillStyle.SOLID);
170*eba4d44aSLiu Zhe 		aDiaProp.setPropertyValue("FillColor", 0xffff00);
171*eba4d44aSLiu Zhe 		// --------------------------
172*eba4d44aSLiu Zhe 		xShape = saveAndLoadShape(1, 0);
173*eba4d44aSLiu Zhe 		xChartDoc = ChartUtil.getChartDocument(xShape);
174*eba4d44aSLiu Zhe 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
175*eba4d44aSLiu Zhe 				xChartDoc.getDiagram().getDataPointProperties(3, 1));
176*eba4d44aSLiu Zhe 
177*eba4d44aSLiu Zhe 		assertEquals(
178*eba4d44aSLiu Zhe 				"the fourth point of the second series's fill color isn't yellow",
179*eba4d44aSLiu Zhe 				0xffff00, aDiaProp.getPropertyValue("FillColor"));
180*eba4d44aSLiu Zhe 
181*eba4d44aSLiu Zhe 	}
182*eba4d44aSLiu Zhe 	/**
183*eba4d44aSLiu Zhe 	 * test data of chart, update it with chart
184*eba4d44aSLiu Zhe 	 * @throws Exception
185*eba4d44aSLiu Zhe 	 */
186*eba4d44aSLiu Zhe 	@Test
testDataTable()187*eba4d44aSLiu Zhe 	public void testDataTable() throws Exception {
188*eba4d44aSLiu Zhe 		XShape xShape = null;
189*eba4d44aSLiu Zhe 		insertDefaultChart();
190*eba4d44aSLiu Zhe 		// get data object from chart
191*eba4d44aSLiu Zhe 		XChartDataArray array = (XChartDataArray) UnoRuntime.queryInterface(
192*eba4d44aSLiu Zhe 				XChartDataArray.class, xChartDoc.getData());
193*eba4d44aSLiu Zhe 
194*eba4d44aSLiu Zhe 		// set new chart data
195*eba4d44aSLiu Zhe 		double[][] data = new double[][] { { 2, 2 }, { 2, 2 }, { 2, 2 } };
196*eba4d44aSLiu Zhe 		array.setData(data);
197*eba4d44aSLiu Zhe 		// update chart with data
198*eba4d44aSLiu Zhe 		xChartDoc.attachData(xChartDoc.getData());
199*eba4d44aSLiu Zhe 		// --------------------------
200*eba4d44aSLiu Zhe 		xShape = saveAndLoadShape(1, 0);
201*eba4d44aSLiu Zhe 		xChartDoc = ChartUtil.getChartDocument(xShape);
202*eba4d44aSLiu Zhe 		array = (XChartDataArray) UnoRuntime.queryInterface(
203*eba4d44aSLiu Zhe 				XChartDataArray.class, xChartDoc.getData());
204*eba4d44aSLiu Zhe 		data = array.getData();
205*eba4d44aSLiu Zhe 		assertEquals(2, data[0][0], 0.0);
206*eba4d44aSLiu Zhe 
207*eba4d44aSLiu Zhe 	}
208*eba4d44aSLiu Zhe 
209*eba4d44aSLiu Zhe 	/**
210*eba4d44aSLiu Zhe 	 * Save presentation and reLoad the presentation and shape in it.
211*eba4d44aSLiu Zhe 	 *
212*eba4d44aSLiu Zhe 	 * @param po
213*eba4d44aSLiu Zhe 	 * @param shapeType
214*eba4d44aSLiu Zhe 	 * @return
215*eba4d44aSLiu Zhe 	 * @throws Exception
216*eba4d44aSLiu Zhe 	 */
saveAndLoadShape(int pageIndex, int shapeIndex)217*eba4d44aSLiu Zhe 	public XShape saveAndLoadShape(int pageIndex, int shapeIndex)
218*eba4d44aSLiu Zhe 			throws Exception {
219*eba4d44aSLiu Zhe 		reLoadFile = saveAsAndReloadDoc(impressDocument, "impress8", "odp");
220*eba4d44aSLiu Zhe 		xShapes = ShapeUtil.getShapes(reLoadFile, pageIndex);
221*eba4d44aSLiu Zhe 		return (XShape) UnoRuntime.queryInterface(XShape.class,
222*eba4d44aSLiu Zhe 				xShapes.getByIndex(shapeIndex));
223*eba4d44aSLiu Zhe 	}
224*eba4d44aSLiu Zhe 
225*eba4d44aSLiu Zhe 	/**
226*eba4d44aSLiu Zhe 	 * save and reload Presentation document.
227*eba4d44aSLiu Zhe 	 *
228*eba4d44aSLiu Zhe 	 * @param presentationDocument
229*eba4d44aSLiu Zhe 	 * @param sFilter
230*eba4d44aSLiu Zhe 	 * @param sExtension
231*eba4d44aSLiu Zhe 	 * @return
232*eba4d44aSLiu Zhe 	 * @throws Exception
233*eba4d44aSLiu Zhe 	 */
saveAsAndReloadDoc(XComponent presentationDocument, String sFilter, String sExtension)234*eba4d44aSLiu Zhe 	private XComponent saveAsAndReloadDoc(XComponent presentationDocument,
235*eba4d44aSLiu Zhe 			String sFilter, String sExtension) throws Exception {
236*eba4d44aSLiu Zhe 		filePath = Testspace.getPath("tmp/chartdata." + sExtension);
237*eba4d44aSLiu Zhe 		PropertyValue[] aStoreProperties = new PropertyValue[2];
238*eba4d44aSLiu Zhe 		aStoreProperties[0] = new PropertyValue();
239*eba4d44aSLiu Zhe 		aStoreProperties[1] = new PropertyValue();
240*eba4d44aSLiu Zhe 		aStoreProperties[0].Name = "Override";
241*eba4d44aSLiu Zhe 		aStoreProperties[0].Value = true;
242*eba4d44aSLiu Zhe 		aStoreProperties[1].Name = "FilterName";
243*eba4d44aSLiu Zhe 		aStoreProperties[1].Value = sFilter;
244*eba4d44aSLiu Zhe 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
245*eba4d44aSLiu Zhe 				XStorable.class, presentationDocument);
246*eba4d44aSLiu Zhe 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
247*eba4d44aSLiu Zhe 
248*eba4d44aSLiu Zhe 		return (XComponent) UnoRuntime.queryInterface(XComponent.class,
249*eba4d44aSLiu Zhe 				unoApp.loadDocument(filePath));
250*eba4d44aSLiu Zhe 	}
251*eba4d44aSLiu Zhe 
252*eba4d44aSLiu Zhe }
253