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.Point;
38 import com.sun.star.awt.Size;
39 import com.sun.star.beans.PropertyValue;
40 import com.sun.star.beans.XPropertySet;
41 import com.sun.star.chart.XChartDocument;
42 import com.sun.star.chart.XDiagram;
43 import com.sun.star.drawing.XDrawPage;
44 import com.sun.star.drawing.XDrawPages;
45 import com.sun.star.drawing.XDrawPagesSupplier;
46 import com.sun.star.drawing.XShape;
47 import com.sun.star.drawing.XShapes;
48 import com.sun.star.frame.XStorable;
49 import com.sun.star.lang.XComponent;
50 import com.sun.star.presentation.XPresentation;
51 import com.sun.star.presentation.XPresentationSupplier;
52 import com.sun.star.uno.UnoRuntime;
53 
54 public class ChartTypes {
55 	UnoApp unoApp = new UnoApp();
56 	XPresentationSupplier sdDocument = null;
57 	XPresentation pre = null;
58 	XComponent precomp = null;
59 	XComponent impressDocument = null;
60 	XComponent reLoadFile = null;
61 	XDrawPagesSupplier drawsupplier = null;
62 	XDrawPages drawpages = null;
63 	XShapes xShapes = null;
64 	XDrawPage xpage = null;
65 	String filePath = null;
66 
67 	@Before
setUp()68 	public void setUp() throws Exception {
69 		unoApp.start();
70 		createDocumentAndSlide();
71 	}
72 
73 	@After
tearDown()74 	public void tearDown() throws Exception {
75 		unoApp.closeDocument(impressDocument);
76 		unoApp.closeDocument(reLoadFile);
77 		unoApp.close();
78 		if (filePath != null)
79 			FileUtil.deleteFile(filePath);
80 	}
81 
82 	/**
83 	 * Insert Area Chart
84 	 *
85 	 * @throws Exception
86 	 */
87 	@Test
testInsertAreaChart()88 	public void testInsertAreaChart() throws Exception {
89 		XChartDocument xChartDoc = null;
90 		XDiagram aDiagram = null;
91 		Point po = new Point(1000, 1000);
92 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
93 		XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
94 				15000, 9271), "com.sun.star.drawing.OLE2Shape");
95 		xShapes.add(xShape);
96 		xChartDoc = ChartUtil.retrieveChartDocument(xShape);
97 		aDiagram = ChartUtil.createChart(xChartDoc,
98 				"com.sun.star.chart.AreaDiagram");
99 		xChartDoc.setDiagram(aDiagram);
100 		xShape = saveAndLoadShape(1, 0);
101 		xChartDoc = ChartUtil.getChartDocument(xShape);
102 		assertEquals("Not Area Chart", "com.sun.star.chart.AreaDiagram",
103 				xChartDoc.getDiagram().getDiagramType());
104 	}
105 
106 	/**
107 	 * Insert Bubble Chart
108 	 *
109 	 * @throws Exception
110 	 */
111 	@Test
testInsertBubbleChart()112 	public void testInsertBubbleChart() throws Exception {
113 		XChartDocument xChartDoc = null;
114 		XDiagram aDiagram = null;
115 		Point po = new Point(1000, 1000);
116 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
117 		XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
118 				15000, 9271), "com.sun.star.drawing.OLE2Shape");
119 		xShapes.add(xShape);
120 		xChartDoc = ChartUtil.retrieveChartDocument(xShape);
121 		aDiagram = ChartUtil.createChart(xChartDoc,
122 				"com.sun.star.chart.BubbleDiagram");
123 		xChartDoc.setDiagram(aDiagram);
124 		xShape = saveAndLoadShape(1, 0);
125 		xChartDoc = ChartUtil.getChartDocument(xShape);
126 		assertEquals("Not Area Chart", "com.sun.star.chart.BubbleDiagram",
127 				xChartDoc.getDiagram().getDiagramType());
128 	}
129 
130 	/**
131 	 * Insert Bar Chart (default chart type)
132 	 *
133 	 * @throws Exception
134 	 */
135 	@Test
testInsertBarChart()136 	public void testInsertBarChart() throws Exception {
137 		XChartDocument xChartDoc = null;
138 		Point po = new Point(1000, 1000);
139 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
140 		XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
141 				15000, 9271), "com.sun.star.drawing.OLE2Shape");
142 		xShapes.add(xShape);
143 		ChartUtil.retrieveChartDocument(xShape);
144 		xShape = saveAndLoadShape(1, 0);
145 		xChartDoc = ChartUtil.getChartDocument(xShape);
146 		assertEquals("Not Bar Chart", "com.sun.star.chart.BarDiagram",
147 				xChartDoc.getDiagram().getDiagramType());
148 
149 	}
150 
151 	/**
152 	 * test Insert Cone Chart(3D look of Bar chart)
153 	 *
154 	 * @throws Exception
155 	 */
156 	@Test
testInsertConeChart()157 	public void testInsertConeChart() throws Exception {
158 		XChartDocument xChartDoc = null;
159 		XDiagram aDiagram = null;
160 		Point po = new Point(1000, 1000);
161 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
162 		XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
163 				15000, 9271), "com.sun.star.drawing.OLE2Shape");
164 		xShapes.add(xShape);
165 		xChartDoc = ChartUtil.retrieveChartDocument(xShape);
166 		aDiagram = ChartUtil.createChart(xChartDoc,
167 				"com.sun.star.chart.BarDiagram");
168 		XPropertySet aDiaProp = (XPropertySet) UnoRuntime.queryInterface(
169 				XPropertySet.class, aDiagram);
170 		aDiaProp.setPropertyValue("Dim3D", true);
171 		aDiaProp.setPropertyValue("Deep", true);
172 		// from service Chart3DBarProperties:
173 		aDiaProp.setPropertyValue("SolidType", new Integer(
174 				com.sun.star.chart.ChartSolidType.CONE));
175 		xChartDoc.setDiagram(aDiagram);
176 		xShape = saveAndLoadShape(1, 0);
177 		xChartDoc = ChartUtil.getChartDocument(xShape);
178 		aDiaProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
179 				xChartDoc.getDiagram());
180 		assertEquals("Not Cone Chart", com.sun.star.chart.ChartSolidType.CONE,
181 				aDiaProp.getPropertyValue("SolidType"));
182 
183 	}
184 
185 	/**
186 	 * Insert Line Chart
187 	 *
188 	 * @throws Exception
189 	 */
190 	@Test
testInsertLineChart()191 	public void testInsertLineChart() throws Exception {
192 		XChartDocument xChartDoc = null;
193 		XDiagram aDiagram = null;
194 		Point po = new Point(1000, 1000);
195 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
196 		XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
197 				15000, 9271), "com.sun.star.drawing.OLE2Shape");
198 		xShapes.add(xShape);
199 		xChartDoc = ChartUtil.retrieveChartDocument(xShape);
200 		aDiagram = ChartUtil.createChart(xChartDoc,
201 				"com.sun.star.chart.LineDiagram");
202 		xChartDoc.setDiagram(aDiagram);
203 		xShape = saveAndLoadShape(1, 0);
204 		xChartDoc = ChartUtil.getChartDocument(xShape);
205 		assertEquals("Not Area Chart", "com.sun.star.chart.LineDiagram",
206 				xChartDoc.getDiagram().getDiagramType());
207 	}
208 
209 	/**
210 	 * Insert Pie Chart
211 	 *
212 	 * @throws Exception
213 	 */
214 	@Test
testInsertPieChart()215 	public void testInsertPieChart() throws Exception {
216 		XChartDocument xChartDoc = null;
217 		XDiagram aDiagram = null;
218 		Point po = new Point(1000, 1000);
219 		xShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class, xpage);
220 		XShape xShape = ShapeUtil.createShape(impressDocument, po, new Size(
221 				15000, 9271), "com.sun.star.drawing.OLE2Shape");
222 		xShapes.add(xShape);
223 		xChartDoc = ChartUtil.retrieveChartDocument(xShape);
224 		aDiagram = ChartUtil.createChart(xChartDoc,
225 				"com.sun.star.chart.PieDiagram");
226 		xChartDoc.setDiagram(aDiagram);
227 		xShape = saveAndLoadShape(1, 0);
228 		xChartDoc = ChartUtil.getChartDocument(xShape);
229 		assertEquals("Not Area Chart", "com.sun.star.chart.PieDiagram",
230 				xChartDoc.getDiagram().getDiagramType());
231 	}
232 
233 	/**
234 	 * create a new presentation document and insert a new slide.
235 	 *
236 	 * @throws Exception
237 	 */
createDocumentAndSlide()238 	public void createDocumentAndSlide() throws Exception {
239 		impressDocument = (XComponent) UnoRuntime.queryInterface(
240 				XComponent.class, unoApp.newDocument("simpress"));
241 		drawsupplier = (XDrawPagesSupplier) UnoRuntime.queryInterface(
242 				XDrawPagesSupplier.class, impressDocument);
243 		drawpages = drawsupplier.getDrawPages();
244 		drawpages.insertNewByIndex(1);
245 		xpage = PageUtil.getDrawPageByIndex(impressDocument, 1);
246 	}
247 
248 	/**
249 	 * Save presentation and reLoad the presentation and shape in it.
250 	 *
251 	 * @param po
252 	 * @param shapeType
253 	 * @return
254 	 * @throws Exception
255 	 */
saveAndLoadShape(int pageIndex, int shapeIndex)256 	public XShape saveAndLoadShape(int pageIndex, int shapeIndex)
257 			throws Exception {
258 		reLoadFile = saveAndReloadDoc(impressDocument,
259 				"impress8", "odp");
260 		xShapes = ShapeUtil.getShapes(reLoadFile, pageIndex);
261 		return (XShape) UnoRuntime.queryInterface(XShape.class,
262 				xShapes.getByIndex(shapeIndex));
263 	}
264 
265 	/**
266 	 * save and reload Presentation document.
267 	 *
268 	 * @param presentationDocument
269 	 * @param sFilter
270 	 * @param sExtension
271 	 * @return
272 	 * @throws Exception
273 	 */
saveAndReloadDoc(XComponent presentationDocument, String sFilter, String sExtension)274 	private XComponent saveAndReloadDoc(XComponent presentationDocument,
275 			String sFilter, String sExtension) throws Exception {
276 		filePath = Testspace.getPath("tmp/presentationtest." + sExtension);
277 		PropertyValue[] aStoreProperties = new PropertyValue[2];
278 		aStoreProperties[0] = new PropertyValue();
279 		aStoreProperties[1] = new PropertyValue();
280 		aStoreProperties[0].Name = "Override";
281 		aStoreProperties[0].Value = true;
282 		aStoreProperties[1].Name = "FilterName";
283 		aStoreProperties[1].Value = sFilter;
284 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
285 				XStorable.class, presentationDocument);
286 		xStorable.storeToURL(FileUtil.getUrl(filePath), aStoreProperties);
287 
288 		return (XComponent) UnoRuntime.queryInterface(XComponent.class,
289 				unoApp.loadDocument(filePath));
290 	}
291 }
292