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.Gradient;
41 import com.sun.star.awt.GradientStyle;
42 import com.sun.star.awt.Point;
43 import com.sun.star.awt.Size;
44 import com.sun.star.beans.PropertyValue;
45 import com.sun.star.beans.XPropertySet;
46 import com.sun.star.chart.ChartDataCaption;
47 import com.sun.star.chart.ChartLegendPosition;
48 import com.sun.star.chart.XAxisYSupplier;
49 import com.sun.star.chart.XChartData;
50 import com.sun.star.chart.XChartDataArray;
51 import com.sun.star.chart.XChartDocument;
52 import com.sun.star.chart.XDiagram;
53 import com.sun.star.chart2.data.XDataSource;
54 import com.sun.star.chart2.data.XLabeledDataSequence;
55 import com.sun.star.drawing.DashStyle;
56 import com.sun.star.drawing.FillStyle;
57 import com.sun.star.drawing.LineDash;
58 import com.sun.star.drawing.LineStyle;
59 import com.sun.star.drawing.XDrawPage;
60 import com.sun.star.drawing.XDrawPages;
61 import com.sun.star.drawing.XDrawPagesSupplier;
62 import com.sun.star.drawing.XShape;
63 import com.sun.star.drawing.XShapes;
64 import com.sun.star.frame.XStorable;
65 import com.sun.star.lang.XComponent;
66 import com.sun.star.presentation.XPresentation;
67 import com.sun.star.presentation.XPresentationSupplier;
68 import com.sun.star.uno.AnyConverter;
69 import com.sun.star.uno.UnoRuntime;
70 import com.sun.star.util.XCloseable;
71 import com.sun.star.util.XModifiable;
72 
73 public class ChartData {
74 	UnoApp unoApp = new UnoApp();
75 	XPresentationSupplier sdDocument = null;
76 	XPresentation pre = null;
77 	XComponent precomp = null;
78 	XComponent impressDocument = null;
79 	XComponent reLoadFile = null;
80 	XDrawPagesSupplier drawsupplier = null;
81 	XDrawPages drawpages = null;
82 	XShapes xShapes = null;
83 	XDrawPage xpage = null;
84 	String filePath = null;
85 	XChartDocument xChartDoc = null;
86 	com.sun.star.chart2.XChartDocument xChart2Doc = null;
87 
88 	@Before
setUp()89 	public void setUp() throws Exception {
90 		unoApp.start();
91 		createDocumentAndSlide();
92 	}
93 
94 	@After
tearDown()95 	public void tearDown() throws Exception {
96 		unoApp.closeDocument(impressDocument);
97 		unoApp.closeDocument(reLoadFile);
98 		unoApp.close();
99 		 if (filePath != null)
100 		 FileUtil.deleteFile(filePath);
101 	}
102 
103 	/**
104 	 * create a new presentation document and insert a new slide.
105 	 *
106 	 * @throws Exception
107 	 */
createDocumentAndSlide()108 	public void createDocumentAndSlide() throws Exception {
109 		impressDocument = (XComponent) UnoRuntime.queryInterface(
110 				XComponent.class, unoApp.newDocument("simpress"));
111 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
112 				XDrawPagesSupplier.class, impressDocument);
113 		drawpages = drawsupplier.getDrawPages();
114 		drawpages.insertNewByIndex(1);
115 		xpage = PageUtil.getDrawPageByIndex(impressDocument, 1);
116 	}
117 
118 	/**
119 	 * Insert default Column Chart to slide.
120 	 *
121 	 * @return
122 	 * @throws Exception
123 	 */
insertDefaultChart()124 	public XChartDocument insertDefaultChart() throws Exception {
125 		Point po = new Point(1000, 1000);
126 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
127 		XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
128 				15000, 9271), "com.sun.star.drawing.OLE2Shape");
129 		xShapes.add(xShape);
130 		xChartDoc = ChartUtil.retrieveChartDocument(xShape);
131 		return xChartDoc;
132 	}
133 
134 	/**
135 	 * test data series
136 	 * @throws Exception
137 	 */
138 	@Test
testDataSeries()139 	public void testDataSeries() throws Exception {
140 		XShape xShape = null;
141 		insertDefaultChart();
142 		// get the second data series.
143 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
144 				XPropertySet.class, xChartDoc.getDiagram()
145 						.getDataRowProperties(1));
146 		aDiaProp.setPropertyValue("FillStyle", FillStyle.SOLID);
147 		aDiaProp.setPropertyValue("FillColor", 0xffff00);
148 		// --------------------------
149 		xShape = saveAndLoadShape(1, 0);
150 		xChartDoc = ChartUtil.getChartDocument(xShape);
151 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
152 				xChartDoc.getDiagram().getDataRowProperties(1));
153 		assertEquals("the second DataSeries fill color isn't yellow", 0xffff00,
154 				aDiaProp.getPropertyValue("FillColor"));
155 
156 	}
157 	/**
158 	 * test data point
159 	 * @throws Exception
160 	 */
161 	@Test
testDataPoint()162 	public void testDataPoint() throws Exception {
163 		XShape xShape = null;
164 		insertDefaultChart();
165 		// set data label to the fourth points, the second series.
166 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
167 				XPropertySet.class, xChartDoc.getDiagram()
168 						.getDataPointProperties(3, 1));
169 		aDiaProp.setPropertyValue("FillStyle", FillStyle.SOLID);
170 		aDiaProp.setPropertyValue("FillColor", 0xffff00);
171 		// --------------------------
172 		xShape = saveAndLoadShape(1, 0);
173 		xChartDoc = ChartUtil.getChartDocument(xShape);
174 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
175 				xChartDoc.getDiagram().getDataPointProperties(3, 1));
176 
177 		assertEquals(
178 				"the fourth point of the second series's fill color isn't yellow",
179 				0xffff00, aDiaProp.getPropertyValue("FillColor"));
180 
181 	}
182 	/**
183 	 * test data of chart, update it with chart
184 	 * @throws Exception
185 	 */
186 	@Test
testDataTable()187 	public void testDataTable() throws Exception {
188 		XShape xShape = null;
189 		insertDefaultChart();
190 		// get data object from chart
191 		XChartDataArray array = (XChartDataArray) UnoRuntime.queryInterface(
192 				XChartDataArray.class, xChartDoc.getData());
193 
194 		// set new chart data
195 		double[][] data = new double[][] { { 2, 2 }, { 2, 2 }, { 2, 2 } };
196 		array.setData(data);
197 		// update chart with data
198 		xChartDoc.attachData(xChartDoc.getData());
199 		// --------------------------
200 		xShape = saveAndLoadShape(1, 0);
201 		xChartDoc = ChartUtil.getChartDocument(xShape);
202 		array = (XChartDataArray) UnoRuntime.queryInterface(
203 				XChartDataArray.class, xChartDoc.getData());
204 		data = array.getData();
205 		assertEquals(2, data[0][0], 0.0);
206 
207 	}
208 
209 	/**
210 	 * Save presentation and reLoad the presentation and shape in it.
211 	 *
212 	 * @param po
213 	 * @param shapeType
214 	 * @return
215 	 * @throws Exception
216 	 */
saveAndLoadShape(int pageIndex, int shapeIndex)217 	public XShape saveAndLoadShape(int pageIndex, int shapeIndex)
218 			throws Exception {
219 		reLoadFile = saveAsAndReloadDoc(impressDocument, "impress8", "odp");
220 		xShapes = ShapeUtil.getShapes(reLoadFile, pageIndex);
221 		return (XShape) UnoRuntime.queryInterface(XShape.class,
222 				xShapes.getByIndex(shapeIndex));
223 	}
224 
225 	/**
226 	 * save and reload Presentation document.
227 	 *
228 	 * @param presentationDocument
229 	 * @param sFilter
230 	 * @param sExtension
231 	 * @return
232 	 * @throws Exception
233 	 */
saveAsAndReloadDoc(XComponent presentationDocument, String sFilter, String sExtension)234 	private XComponent saveAsAndReloadDoc(XComponent presentationDocument,
235 			String sFilter, String sExtension) throws Exception {
236 		filePath = Testspace.getPath("tmp/chartdata." + sExtension);
237 		PropertyValue[] aStoreProperties = new PropertyValue[2];
238 		aStoreProperties[0] = new PropertyValue();
239 		aStoreProperties[1] = new PropertyValue();
240 		aStoreProperties[0].Name = "Override";
241 		aStoreProperties[0].Value = true;
242 		aStoreProperties[1].Name = "FilterName";
243 		aStoreProperties[1].Value = sFilter;
244 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
245 				XStorable.class, presentationDocument);
246 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
247 
248 		return (XComponent) UnoRuntime.queryInterface(XComponent.class,
249 				unoApp.loadDocument(filePath));
250 	}
251 
252 }
253