Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScChartsObj.java
blobc526105b511853f5fe5c0fa933403029e55d5f91
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.lang.XComponent;
32 import com.sun.star.sheet.XCellRangeAddressable;
33 import com.sun.star.sheet.XSpreadsheet;
34 import com.sun.star.sheet.XSpreadsheetDocument;
35 import com.sun.star.sheet.XSpreadsheets;
36 import com.sun.star.table.CellRangeAddress;
37 import com.sun.star.table.XCell;
38 import com.sun.star.table.XCellRange;
39 import com.sun.star.table.XTableCharts;
40 import com.sun.star.table.XTableChartsSupplier;
41 import com.sun.star.uno.AnyConverter;
42 import com.sun.star.uno.Type;
43 import com.sun.star.uno.UnoRuntime;
45 /**
46 * Test for object which is represented by service
47 * <code>com.sun.star.table.TableCharts</code>. <p>
48 * Object implements the following interfaces :
49 * <ul>
50 * <li> <code>com::sun::star::container::XNameAccess</code></li>
51 * <li> <code>com::sun::star::container::XIndexAccess</code></li>
52 * <li> <code>com::sun::star::container::XElementAccess</code></li>
53 * <li> <code>com::sun::star::table::XTableCharts</code></li>
54 * </ul>
55 * @see com.sun.star.table.TableCharts
56 * @see com.sun.star.container.XNameAccess
57 * @see com.sun.star.container.XIndexAccess
58 * @see com.sun.star.container.XElementAccess
59 * @see com.sun.star.table.XTableCharts
60 * @see ifc.container._XNameAccess
61 * @see ifc.container._XIndexAccess
62 * @see ifc.container._XElementAccess
63 * @see ifc.table._XTableCharts
65 public class ScChartsObj extends TestCase {
66 private XSpreadsheetDocument xSheetDoc = null;
68 /**
69 * Creates Spreadsheet document.
71 @Override
72 protected void initialize( TestParameters tParam, PrintWriter log ) {
73 // get a soffice factory object
74 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF());
76 try {
77 log.println( "creating a sheetdocument" );
78 xSheetDoc = SOF.createCalcDoc(null);
79 } catch (com.sun.star.uno.Exception e) {
80 // Some exception occurs.FAILED
81 e.printStackTrace( log );
82 throw new StatusException( "Couldn't create document", e );
86 /**
87 * Disposes Spreadsheet document.
89 @Override
90 protected void cleanup( TestParameters tParam, PrintWriter log ) {
91 log.println( " disposing xSheetDoc " );
92 XComponent oComp = UnoRuntime.queryInterface(XComponent.class, xSheetDoc);
93 util.DesktopTools.closeDoc(oComp);
96 /**
97 * Creating a Testenvironment for the interfaces to be tested.
98 * Retrieves a collection of spreadsheets from a document
99 * and takes one of them. Inserts some values into the cells of the some cell
100 * range address. Obtains the collection of the charts using the interface
101 * <code>XTableChartsSupplier</code>. Creates and adds the chart that using
102 * the data from the cells of this cell range address. Collection of
103 * the charts is the instance of the service
104 * <code>com.sun.star.table.TableCharts</code>.
105 * Object relations created :
106 * <ul>
107 * <li> <code>'RECT'</code> for
108 * {@link ifc.table._XTableCharts}(of type <code>Rectangle</code>
109 * the position of the chart)</li>
110 * <li> <code>'ADDR'</code> for
111 * {@link ifc.table._XTableCharts}(of type
112 * <code>com.sun.star.table.CellRangeAddress[]</code> data source ranges
113 * for chart creating)</li>
114 * </ul>
115 * @see com.sun.star.container.XNamed
116 * @see com.sun.star.table.XTableChartsSupplier
118 @Override
119 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
121 XSpreadsheet oSheet=null;
123 try {
124 log.println("Getting spreadsheet") ;
125 XSpreadsheets oSheets = xSheetDoc.getSheets() ;
126 XIndexAccess oIndexSheets = UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
127 oSheet = (XSpreadsheet) AnyConverter.toObject(
128 new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
129 } catch (com.sun.star.lang.WrappedTargetException e) {
130 log.println("Couldn't get Sheet ");
131 e.printStackTrace(log);
132 throw new StatusException("Couldn't get sheet", e);
133 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
134 log.println("Couldn't get Sheet ");
135 e.printStackTrace(log);
136 throw new StatusException("Couldn't get sheet", e);
137 } catch (com.sun.star.lang.IllegalArgumentException e) {
138 log.println("Couldn't get Sheet ");
139 e.printStackTrace(log);
140 throw new StatusException("Couldn't get sheet", e);
143 log.println("Creating the Header") ;
145 insertIntoCell(1,0,"JAN",oSheet,"");
146 insertIntoCell(2,0,"FEB",oSheet,"");
147 insertIntoCell(3,0,"MAR",oSheet,"");
148 insertIntoCell(4,0,"APR",oSheet,"");
149 insertIntoCell(5,0,"MAI",oSheet,"");
150 insertIntoCell(6,0,"JUN",oSheet,"");
151 insertIntoCell(7,0,"JUL",oSheet,"");
152 insertIntoCell(8,0,"AUG",oSheet,"");
153 insertIntoCell(9,0,"SEP",oSheet,"");
154 insertIntoCell(10,0,"OCT",oSheet,"");
155 insertIntoCell(11,0,"NOV",oSheet,"");
156 insertIntoCell(12,0,"DEC",oSheet,"");
157 insertIntoCell(13,0,"SUM",oSheet,"");
159 log.println("Fill the lines");
161 insertIntoCell(0,1,"Smith",oSheet,"");
162 insertIntoCell(1,1,"42",oSheet,"V");
163 insertIntoCell(2,1,"58.9",oSheet,"V");
164 insertIntoCell(3,1,"-66.5",oSheet,"V");
165 insertIntoCell(4,1,"43.4",oSheet,"V");
166 insertIntoCell(5,1,"44.5",oSheet,"V");
167 insertIntoCell(6,1,"45.3",oSheet,"V");
168 insertIntoCell(7,1,"-67.3",oSheet,"V");
169 insertIntoCell(8,1,"30.5",oSheet,"V");
170 insertIntoCell(9,1,"23.2",oSheet,"V");
171 insertIntoCell(10,1,"-97.3",oSheet,"V");
172 insertIntoCell(11,1,"22.4",oSheet,"V");
173 insertIntoCell(12,1,"23.5",oSheet,"V");
174 insertIntoCell(13,1,"=SUM(B2:M2)",oSheet,"");
176 insertIntoCell(0,2,"Jones",oSheet,"");
177 insertIntoCell(1,2,"21",oSheet,"V");
178 insertIntoCell(2,2,"40.9",oSheet,"V");
179 insertIntoCell(3,2,"-57.5",oSheet,"V");
180 insertIntoCell(4,2,"-23.4",oSheet,"V");
181 insertIntoCell(5,2,"34.5",oSheet,"V");
182 insertIntoCell(6,2,"59.3",oSheet,"V");
183 insertIntoCell(7,2,"27.3",oSheet,"V");
184 insertIntoCell(8,2,"-38.5",oSheet,"V");
185 insertIntoCell(9,2,"43.2",oSheet,"V");
186 insertIntoCell(10,2,"57.3",oSheet,"V");
187 insertIntoCell(11,2,"25.4",oSheet,"V");
188 insertIntoCell(12,2,"28.5",oSheet,"V");
189 insertIntoCell(13,2,"=SUM(B3:M3)",oSheet,"");
191 insertIntoCell(0,3,"Brown",oSheet,"");
192 insertIntoCell(1,3,"31.45",oSheet,"V");
193 insertIntoCell(2,3,"-20.9",oSheet,"V");
194 insertIntoCell(3,3,"-117.5",oSheet,"V");
195 insertIntoCell(4,3,"23.4",oSheet,"V");
196 insertIntoCell(5,3,"-114.5",oSheet,"V");
197 insertIntoCell(6,3,"115.3",oSheet,"V");
198 insertIntoCell(7,3,"-171.3",oSheet,"V");
199 insertIntoCell(8,3,"89.5",oSheet,"V");
200 insertIntoCell(9,3,"41.2",oSheet,"V");
201 insertIntoCell(10,3,"71.3",oSheet,"V");
202 insertIntoCell(11,3,"25.4",oSheet,"V");
203 insertIntoCell(12,3,"38.5",oSheet,"V");
204 insertIntoCell(13,3,"=SUM(A4:L4)",oSheet,"");
206 // insert a chart
207 Rectangle oRect = new Rectangle(500, 3000, 25000, 11000);
209 XCellRange oRange = UnoRuntime.queryInterface(XCellRange.class, oSheet);
210 XCellRange myRange = oRange.getCellRangeByName("A1:N4");
211 XCellRangeAddressable oRangeAddr = UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange);
212 CellRangeAddress myAddr = oRangeAddr.getRangeAddress();
214 CellRangeAddress[] oAddr = new CellRangeAddress[1];
215 oAddr[0] = myAddr;
216 XTableChartsSupplier oSupp = UnoRuntime.queryInterface(XTableChartsSupplier.class, oSheet);
219 log.println("Insert Chart");
220 XTableCharts oCharts = oSupp.getCharts();
221 oCharts.addNewByName("ScChartObj", oRect, oAddr, true, true);
223 log.println("creating a new environment for object");
224 TestEnvironment tEnv = new TestEnvironment(oCharts);
226 tEnv.addObjRelation("RECT", oRect);
227 tEnv.addObjRelation("ADDR", oAddr);
229 return tEnv;
233 * Inserts a value or a formula in the cell of the spreasheet.
234 * @param CellX is the column index of the cell
235 * @param CellY is the row index of the cell
236 * @param theValue string representation of the value
237 * @param TT1 specify the spreadsheet, the interface
238 * <code>com.sun.star.sheet.XSpreadsheet</code>
239 * @param flag if it's equal to <code>'V'</code> then the method inserts
240 * a double-value in the cell else it inserts a formula in the cell
242 public static void insertIntoCell(
243 int CellX, int CellY, String theValue, XSpreadsheet TT1, String flag) {
245 XCell oCell = null;
247 try {
248 oCell = TT1.getCellByPosition(CellX, CellY);
249 } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
250 System.out.println("Could not get Cell");
253 if (flag.equals("V")) {
254 oCell.setValue(Float.parseFloat(theValue));
255 } else {
256 oCell.setFormula(theValue);
259 } // end of insertIntoCell