Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScChartObj.java
blobd50f2f9dbdae4e2d328d293e5e62494ca6339986
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package mod._sc;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.SOfficeFactory;
29 import com.sun.star.awt.Rectangle;
30 import com.sun.star.container.XIndexAccess;
31 import com.sun.star.container.XNameAccess;
32 import com.sun.star.lang.XComponent;
33 import com.sun.star.sheet.XCellRangeAddressable;
34 import com.sun.star.sheet.XSpreadsheet;
35 import com.sun.star.sheet.XSpreadsheetDocument;
36 import com.sun.star.sheet.XSpreadsheets;
37 import com.sun.star.table.CellRangeAddress;
38 import com.sun.star.table.XCell;
39 import com.sun.star.table.XCellRange;
40 import com.sun.star.table.XTableChart;
41 import com.sun.star.table.XTableCharts;
42 import com.sun.star.table.XTableChartsSupplier;
43 import com.sun.star.uno.AnyConverter;
44 import com.sun.star.uno.Type;
45 import com.sun.star.uno.UnoRuntime;
47 /**
48 * Test for object which is represented by service
49 * <code>com.sun.star.table.TableChart</code>. <p>
50 * Object implements the following interfaces :
51 * <ul>
52 * <li> <code>com::sun::star::container::XNamed</code></li>
53 * <li> <code>com::sun::star::table::XTableChart</code></li>
54 * <li> <code>com::sun::star::document::XEmbeddedObjectSupplier</code></li>
55 * </ul>
56 * @see com.sun.star.table.TableChart
57 * @see com.sun.star.container.XNamed
58 * @see com.sun.star.table.XTableChart
59 * @see com.sun.star.document.XEmbeddedObjectSupplier
60 * @see ifc.container._XNamed
61 * @see ifc.table._XTableChart
62 * @see ifc.document._XEmbeddedObjectSupplier
64 public class ScChartObj extends TestCase {
65 private XSpreadsheetDocument xSheetDoc = null;
67 /**
68 * Creates Spreadsheet document.
70 @Override
71 protected void initialize( TestParameters tParam, PrintWriter log ) {
72 // get a soffice factory object
73 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF());
75 try {
76 log.println( "creating a sheetdocument" );
77 xSheetDoc = SOF.createCalcDoc(null);
78 } catch (com.sun.star.uno.Exception e) {
79 // Some exception occurs.FAILED
80 e.printStackTrace( log );
81 throw new StatusException( "Couldn't create document", e );
85 /**
86 * Disposes Spreadsheet document.
88 @Override
89 protected void cleanup( TestParameters tParam, PrintWriter log ) {
90 log.println( " disposing xSheetDoc " );
91 XComponent oComp = UnoRuntime.queryInterface(XComponent.class, xSheetDoc);
92 util.DesktopTools.closeDoc(oComp);
95 /**
96 * Creating a Testenvironment for the interfaces to be tested.
97 * Retrieves a collection of spreadsheets from a document
98 * and takes one of them. Inserts some values into the cells of the some cell
99 * range address. Adds the chart that using the data from the cells of this
100 * cell range address. New chart is the instance of
101 * the service <code>com.sun.star.table.TableChart</code>.
102 * Object relations created :
103 * <ul>
104 * <li> <code>'setName'</code> for
105 * {@link ifc.container._XNamed}(the method <code>setName</code> must
106 * not be tested)</li>
107 * </ul>
108 * @see com.sun.star.container.XNamed
110 @Override
111 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
113 XSpreadsheet oSheet=null;
115 try {
116 log.println("Getting spreadsheet") ;
117 XSpreadsheets oSheets = xSheetDoc.getSheets() ;
118 XIndexAccess oIndexSheets = UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
119 oSheet = (XSpreadsheet) AnyConverter.toObject(
120 new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
121 } catch (com.sun.star.lang.WrappedTargetException e) {
122 log.println("Couldn't get Sheet ");
123 e.printStackTrace(log);
124 throw new StatusException("Couldn't get sheet", e);
125 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
126 log.println("Couldn't get Sheet ");
127 e.printStackTrace(log);
128 throw new StatusException("Couldn't get sheet", e);
129 } catch (com.sun.star.lang.IllegalArgumentException e) {
130 log.println("Couldn't get Sheet ");
131 e.printStackTrace(log);
132 throw new StatusException("Couldn't get sheet", e);
135 log.println("Creating the Header") ;
137 insertIntoCell(1,0,"JAN",oSheet,"");
138 insertIntoCell(2,0,"FEB",oSheet,"");
139 insertIntoCell(3,0,"MAR",oSheet,"");
140 insertIntoCell(4,0,"APR",oSheet,"");
141 insertIntoCell(5,0,"MAI",oSheet,"");
142 insertIntoCell(6,0,"JUN",oSheet,"");
143 insertIntoCell(7,0,"JUL",oSheet,"");
144 insertIntoCell(8,0,"AUG",oSheet,"");
145 insertIntoCell(9,0,"SEP",oSheet,"");
146 insertIntoCell(10,0,"OCT",oSheet,"");
147 insertIntoCell(11,0,"NOV",oSheet,"");
148 insertIntoCell(12,0,"DEC",oSheet,"");
149 insertIntoCell(13,0,"SUM",oSheet,"");
151 log.println("Fill the lines");
153 insertIntoCell(0,1,"Smith",oSheet,"");
154 insertIntoCell(1,1,"42",oSheet,"V");
155 insertIntoCell(2,1,"58.9",oSheet,"V");
156 insertIntoCell(3,1,"-66.5",oSheet,"V");
157 insertIntoCell(4,1,"43.4",oSheet,"V");
158 insertIntoCell(5,1,"44.5",oSheet,"V");
159 insertIntoCell(6,1,"45.3",oSheet,"V");
160 insertIntoCell(7,1,"-67.3",oSheet,"V");
161 insertIntoCell(8,1,"30.5",oSheet,"V");
162 insertIntoCell(9,1,"23.2",oSheet,"V");
163 insertIntoCell(10,1,"-97.3",oSheet,"V");
164 insertIntoCell(11,1,"22.4",oSheet,"V");
165 insertIntoCell(12,1,"23.5",oSheet,"V");
166 insertIntoCell(13,1,"=SUM(B2:M2)",oSheet,"");
168 insertIntoCell(0,2,"Jones",oSheet,"");
169 insertIntoCell(1,2,"21",oSheet,"V");
170 insertIntoCell(2,2,"40.9",oSheet,"V");
171 insertIntoCell(3,2,"-57.5",oSheet,"V");
172 insertIntoCell(4,2,"-23.4",oSheet,"V");
173 insertIntoCell(5,2,"34.5",oSheet,"V");
174 insertIntoCell(6,2,"59.3",oSheet,"V");
175 insertIntoCell(7,2,"27.3",oSheet,"V");
176 insertIntoCell(8,2,"-38.5",oSheet,"V");
177 insertIntoCell(9,2,"43.2",oSheet,"V");
178 insertIntoCell(10,2,"57.3",oSheet,"V");
179 insertIntoCell(11,2,"25.4",oSheet,"V");
180 insertIntoCell(12,2,"28.5",oSheet,"V");
181 insertIntoCell(13,2,"=SUM(B3:M3)",oSheet,"");
183 insertIntoCell(0,3,"Brown",oSheet,"");
184 insertIntoCell(1,3,"31.45",oSheet,"V");
185 insertIntoCell(2,3,"-20.9",oSheet,"V");
186 insertIntoCell(3,3,"-117.5",oSheet,"V");
187 insertIntoCell(4,3,"23.4",oSheet,"V");
188 insertIntoCell(5,3,"-114.5",oSheet,"V");
189 insertIntoCell(6,3,"115.3",oSheet,"V");
190 insertIntoCell(7,3,"-171.3",oSheet,"V");
191 insertIntoCell(8,3,"89.5",oSheet,"V");
192 insertIntoCell(9,3,"41.2",oSheet,"V");
193 insertIntoCell(10,3,"71.3",oSheet,"V");
194 insertIntoCell(11,3,"25.4",oSheet,"V");
195 insertIntoCell(12,3,"38.5",oSheet,"V");
196 insertIntoCell(13,3,"=SUM(A4:L4)",oSheet,"");
198 // insert a chart
200 Rectangle oRect = new Rectangle(500, 3000, 25000, 11000);
202 XCellRange oRange = UnoRuntime.queryInterface(XCellRange.class, oSheet);
203 XCellRange myRange = oRange.getCellRangeByName("A1:N4");
204 XCellRangeAddressable oRangeAddr = UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
205 CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
207 CellRangeAddress[] oAddr = new CellRangeAddress[1];
208 oAddr[0] = myAddr;
209 XTableChartsSupplier oSupp = UnoRuntime.queryInterface(XTableChartsSupplier.class, oSheet);
211 log.println("Insert Chart");
213 XTableCharts oCharts = oSupp.getCharts();
214 oCharts.addNewByName("ScChartObj", oRect, oAddr, true, true);
216 // get the TableChart
217 XTableChart oChart = null;
218 try {
219 XNameAccess names = (XNameAccess) AnyConverter.toObject(
220 new Type(XNameAccess.class),UnoRuntime.queryInterface(
221 XNameAccess.class, oCharts));
223 oChart = (XTableChart) AnyConverter.toObject(
224 new Type(XTableChart.class),names.getByName("ScChartObj"));
226 } catch (com.sun.star.lang.WrappedTargetException e) {
227 log.println("Couldn't get TableChart");
228 e.printStackTrace(log);
229 throw new StatusException("Couldn't get TableChart", e);
230 } catch (com.sun.star.container.NoSuchElementException e) {
231 log.println("Couldn't get TableChart");
232 e.printStackTrace(log);
233 throw new StatusException("Couldn't get TableChart", e);
234 } catch (com.sun.star.lang.IllegalArgumentException e) {
235 log.println("Couldn't get TableChart");
236 e.printStackTrace(log);
237 throw new StatusException("Couldn't get TableChart", e);
240 log.println("creating a new environment for object");
241 TestEnvironment tEnv = new TestEnvironment(oChart);
243 //adding ObjRelation 'setName' for 'XNamed'
244 tEnv.addObjRelation("setName", Boolean.TRUE);
246 return tEnv;
250 * Inserts a value or a formula in the cell of the spreasheet.
251 * @param CellX is the column index of the cell
252 * @param CellY is the row index of the cell
253 * @param theValue string representation of the value
254 * @param TT1 specify the spreadsheet, the interface
255 * <code>com.sun.star.sheet.XSpreadsheet</code>
256 * @param flag if it's equal to <code>'V'</code> then the method inserts
257 * a double-value in the cell else it inserts a formula in the cell
259 public static void insertIntoCell(
260 int CellX, int CellY, String theValue, XSpreadsheet TT1, String flag)
262 XCell oCell = null;
264 try {
265 oCell = TT1.getCellByPosition(CellX, CellY);
266 } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
267 System.out.println("Could not get Cell");
270 if (flag.equals("V")) {
271 oCell.setValue(Float.parseFloat(theValue));
272 } else {
273 oCell.setFormula(theValue);
275 } // end of insertIntoCell