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