1 /*
2  * This file is part of the LibreOffice project.
3  *
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/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
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 .
17  */
18 package mod._sc;
19 
20 import com.sun.star.beans.XPropertySet;
21 import com.sun.star.container.XIndexAccess;
22 import com.sun.star.container.XNamed;
23 import com.sun.star.lang.XComponent;
24 import com.sun.star.sheet.DataPilotFieldOrientation;
25 import com.sun.star.sheet.XDataPilotDescriptor;
26 import com.sun.star.sheet.XDataPilotField;
27 import com.sun.star.sheet.XDataPilotTables;
28 import com.sun.star.sheet.XDataPilotTablesSupplier;
29 import com.sun.star.sheet.XSpreadsheet;
30 import com.sun.star.sheet.XSpreadsheetDocument;
31 import com.sun.star.sheet.XSpreadsheets;
32 import com.sun.star.table.CellAddress;
33 import com.sun.star.table.CellRangeAddress;
34 import com.sun.star.uno.AnyConverter;
35 import com.sun.star.uno.Type;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.uno.XInterface;
38 
39 import java.io.PrintWriter;
40 
41 import lib.TestCase;
42 import lib.TestEnvironment;
43 import lib.TestParameters;
44 
45 import util.SOfficeFactory;
46 
47 
48 /**
49 * Test for object which is represented by service
50 * <code>com.sun.star.sheet.DataPilotField</code>. <p>
51 * Object implements the following interfaces :
52 * <ul>
53 *  <li> <code>com::sun::star::container::XNamed</code></li>
54 *  <li> <code>com::sun::star::sheet::DataPilotField</code></li>
55 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
56 * </ul>
57 * @see com.sun.star.sheet.DataPilotField
58 * @see com.sun.star.container.XNamed
59 * @see com.sun.star.sheet.DataPilotField
60 * @see com.sun.star.beans.XPropertySet
61 * @see ifc.container._XNamed
62 * @see ifc.sheet._DataPilotField
63 * @see ifc.beans._XPropertySet
64 */
65 public class ScDataPilotItemObj extends TestCase {
66     private XSpreadsheetDocument xSheetDoc = null;
67 
68     /**
69      * A field is filled some values. This integer determines the size of the
70      * field in x and y direction.
71      */
72     private static final int mMaxFieldIndex = 6;
73 
74     /**
75     * Creates Spreadsheet document.
76     */
77     @Override
initialize(TestParameters tParam, PrintWriter log)78     protected void initialize(TestParameters tParam, PrintWriter log) throws Exception {
79         SOfficeFactory SOF = SOfficeFactory.getFactory(
80                                      tParam.getMSF());
81 
82         log.println("creating a Spreadsheet document");
83         xSheetDoc = SOF.createCalcDoc(null);
84     }
85 
86     /**
87     * Disposes Spreadsheet document.
88     */
89     @Override
cleanup(TestParameters tParam, PrintWriter log)90     protected void cleanup(TestParameters tParam, PrintWriter log) {
91         log.println("    disposing xSheetDoc ");
92 
93         XComponent oComp = UnoRuntime.queryInterface(
94                                    XComponent.class, xSheetDoc);
95         util.DesktopTools.closeDoc(oComp);
96     }
97 
98     /**
99     * Creating a TestEnvironment for the interfaces to be tested.
100     * Retrieves a collection of spreadsheets from a document
101     * and takes one of them. Fills some table in the spreadsheet.
102     * Obtains the collection of data pilot tables using the interface
103     * <code>XDataPilotTablesSupplier</code>. Creates a data pilot descriptor
104     * for the filled table and inserts new data pilot table with this descriptor
105     * to the collection. Obtains the collection of all the data pilot fields
106     * using the interface <code>XDataPilotDescriptor</code>. Retrieves from
107     * the collection the data pilot field with index 0. This data pilot field
108     * is the instance of the service <code>com.sun.star.sheet.DataPilotField</code>.
109     * @see com.sun.star.sheet.DataPilotField
110     * @see com.sun.star.sheet.XDataPilotTablesSupplier
111     * @see com.sun.star.sheet.XDataPilotDescriptor
112     */
113     @Override
createTestEnvironment(TestParameters Param, PrintWriter log)114     protected TestEnvironment createTestEnvironment(TestParameters Param,
115                                                                  PrintWriter log) throws Exception {
116         XInterface oObj = null;
117 
118 
119         // creation of testobject here
120         // first we write what we are intend to do to log file
121         log.println("Creating a test environment");
122 
123         // the cell range
124         CellRangeAddress sCellRangeAdress = new CellRangeAddress();
125         sCellRangeAdress.Sheet = 0;
126         sCellRangeAdress.StartColumn = 1;
127         sCellRangeAdress.StartRow = 0;
128         sCellRangeAdress.EndColumn = mMaxFieldIndex - 1;
129         sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
130 
131         // position of the data pilot table
132         CellAddress sCellAdress = new CellAddress();
133         sCellAdress.Sheet = 0;
134         sCellAdress.Column = 7;
135         sCellAdress.Row = 8;
136 
137         log.println("Getting a sheet");
138 
139         XSpreadsheets xSpreadsheets = xSheetDoc.getSheets();
140         XSpreadsheet oSheet = null;
141         XSpreadsheet oSheet2 = null;
142         XIndexAccess oIndexAccess = UnoRuntime.queryInterface(
143                                             XIndexAccess.class, xSpreadsheets);
144         // Make sure there are at least two sheets
145         xSpreadsheets.insertNewByName("Some Sheet", (short)0);
146 
147         oSheet = (XSpreadsheet) AnyConverter.toObject(
148                          new Type(XSpreadsheet.class),
149                          oIndexAccess.getByIndex(0));
150         oSheet2 = (XSpreadsheet) AnyConverter.toObject(
151                           new Type(XSpreadsheet.class),
152                           oIndexAccess.getByIndex(1));
153 
154         log.println("Filling a table");
155 
156         for (int i = 1; i < mMaxFieldIndex; i++) {
157             oSheet.getCellByPosition(i, 0).setFormula("Col" + i);
158             oSheet.getCellByPosition(0, i).setFormula("Row" + i);
159             oSheet2.getCellByPosition(i, 0).setFormula("Col" + i);
160             oSheet2.getCellByPosition(0, i).setFormula("Row" + i);
161         }
162 
163         for (int i = 1; i < mMaxFieldIndex; i++)
164             for (int j = 1; j < mMaxFieldIndex; j++) {
165                 oSheet.getCellByPosition(i, j).setValue(i * (j + 1));
166                 oSheet2.getCellByPosition(i, j).setValue(i * (j + 2));
167             }
168 
169         oSheet.getCellByPosition(1, 5);
170 
171         int x = sCellAdress.Column;
172         int y = sCellAdress.Row + 3;
173 
174 
175         oSheet.getCellByPosition(x, y);
176 
177 
178         // create the test objects
179         log.println("Getting test objects");
180 
181         XDataPilotTablesSupplier DPTS = UnoRuntime.queryInterface(
182                                                 XDataPilotTablesSupplier.class,
183                                                 oSheet);
184         XDataPilotTables DPT = DPTS.getDataPilotTables();
185         XDataPilotDescriptor DPDsc = DPT.createDataPilotDescriptor();
186         DPDsc.setSourceRange(sCellRangeAdress);
187 
188         XPropertySet fieldPropSet = null;
189 
190         Object oDataPilotField = DPDsc.getDataPilotFields().getByIndex(0);
191         fieldPropSet = UnoRuntime.queryInterface(
192                                XPropertySet.class, oDataPilotField);
193 
194         fieldPropSet.setPropertyValue("Function",
195                                       com.sun.star.sheet.GeneralFunction.SUM);
196         fieldPropSet.setPropertyValue("Orientation",
197                                       com.sun.star.sheet.DataPilotFieldOrientation.DATA);
198 
199         log.println("Insert the DataPilotTable");
200 
201         if (DPT.hasByName("DataPilotTable")) {
202             DPT.removeByName("DataPilotTable");
203         }
204 
205         XIndexAccess IA = DPDsc.getDataPilotFields();
206         getSRange(IA);
207 
208         DPT.insertNewByName("DataPilotTable", sCellAdress, DPDsc);
209 
210         oObj = (XInterface) AnyConverter.toObject(
211                        new Type(XInterface.class), IA.getByIndex(0));
212 
213         log.println("Creating object - " +
214                     ((oObj == null) ? "FAILED" : "OK"));
215 
216         XDataPilotField xDataPilotField = UnoRuntime.queryInterface(
217                                                   XDataPilotField.class, oObj);
218 
219         XIndexAccess xIA = xDataPilotField.getItems();
220 
221         oObj = UnoRuntime.queryInterface(XInterface.class,
222                                                       xIA.getByIndex(0));
223 
224         TestEnvironment tEnv = new TestEnvironment(oObj);
225 
226         log.println("Implementationname: " + util.utils.getImplName(oObj));
227 
228         // Other parameters required for interface tests
229         tEnv.addObjRelation("NoSetName",  "ScDataPilotItemObj");
230         return tEnv;
231     }
232 
getSRange(XIndexAccess IA)233     private void getSRange(XIndexAccess IA) {
234         int fieldsAmount = IA.getCount() + 1;
235 
236         int i = -1;
237         int cnt = 0;
238 
239         while ((++i) < fieldsAmount) {
240             Object field;
241 
242             try {
243                 field = IA.getByIndex(i);
244             } catch (com.sun.star.lang.WrappedTargetException e) {
245                 e.printStackTrace(log);
246 
247                 return;
248             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
249                 e.printStackTrace(log);
250 
251                 return;
252             }
253 
254             XNamed named = UnoRuntime.queryInterface(XNamed.class,
255                                                               field);
256             String name = named.getName();
257 
258             log.println("**Field : '" + name + "' ... ");
259 
260             if (!name.equals("Data")) {
261 
262                 XPropertySet props = UnoRuntime.queryInterface(
263                                              XPropertySet.class, field);
264 
265                 try {
266                     switch (cnt % 5) {
267                     case 0:
268                         props.setPropertyValue("Orientation",
269                                                DataPilotFieldOrientation.COLUMN);
270                         log.println("  Column");
271 
272                         break;
273 
274                     case 1:
275                         props.setPropertyValue("Orientation",
276                                                DataPilotFieldOrientation.ROW);
277                         log.println("  Row");
278 
279                         break;
280 
281                     case 2:
282                         props.setPropertyValue("Orientation",
283                                                DataPilotFieldOrientation.DATA);
284                         log.println("  Data");
285 
286                         break;
287 
288                     case 3:
289                         props.setPropertyValue("Orientation",
290                                                DataPilotFieldOrientation.HIDDEN);
291                         log.println("  Hidden");
292 
293                         break;
294 
295                     case 4:
296                         props.setPropertyValue("Orientation",
297                                                DataPilotFieldOrientation.PAGE);
298                         log.println("  Page");
299 
300                         break;
301                     }
302                 } catch (com.sun.star.lang.WrappedTargetException e) {
303                     e.printStackTrace(log);
304 
305                     return;
306                 } catch (com.sun.star.lang.IllegalArgumentException e) {
307                     e.printStackTrace(log);
308 
309                     return;
310                 } catch (com.sun.star.beans.PropertyVetoException e) {
311                     e.printStackTrace(log);
312 
313                     return;
314                 } catch (com.sun.star.beans.UnknownPropertyException e) {
315                     e.printStackTrace(log);
316 
317                     return;
318                 }
319 
320                 if ((++cnt) > 4) {
321                     break;
322                 }
323             } else {
324                 return;
325             }
326         }
327     }
328 }
329