xref: /AOO41X/main/qadevOOo/tests/java/ifc/sheet/_XCellRangesQuery.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package ifc.sheet;
28 
29 import lib.MultiMethodTest;
30 import lib.Status;
31 import lib.StatusException;
32 
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.sheet.CellFlags;
35 import com.sun.star.sheet.FormulaResult;
36 import com.sun.star.sheet.XCellRangesQuery;
37 import com.sun.star.sheet.XSheetCellRanges;
38 import com.sun.star.sheet.XSpreadsheet;
39 import com.sun.star.table.CellAddress;
40 import com.sun.star.table.CellRangeAddress;
41 import com.sun.star.table.XColumnRowRange;
42 import com.sun.star.table.XTableColumns;
43 import com.sun.star.table.XTableRows;
44 import com.sun.star.uno.UnoRuntime;
45 
46 /**
47  * Test the XCellRangesQuery interface.
48  * Needed object relations:
49  * <ul>
50  *   <li>"SHEET": an XSpreadSheet object
51  *   </li>
52  *   <li>"XCellRangesQuery.EXPECTEDRESULTS": the expected results for the test
53  *       methods as a String array.<br>
54  *       @see mod._sc.ScCellCurserObj or
55  *       @see mod._sc.ScCellObj for an example how this should look like.
56  *   </li>
57  * </ul>
58  */
59 public class _XCellRangesQuery extends MultiMethodTest {
60     public XCellRangesQuery oObj;
61     protected XSpreadsheet oSheet;
62     protected XTableRows oRows;
63     protected XTableColumns oColumns;
64     protected String[] mExpectedResults = null;
65     protected boolean bMakeEntriesAndDispose = false;
66     String getting = "";
67     String expected = "";
68     // provide the object with constants to fill the expected results array
69     public static final int QUERYCOLUMNDIFFERENCES = 0;
70     public static final int QUERYCONTENTCELLS = 1;
71     public static final int QUERYEMPTYCELLS = 2;
72     public static final int QUERYFORMULACELLS = 3;
73     public static final int QUERYINTERSECTION = 4;
74     public static final int QUERYROWDIFFERENCES = 5;
75     public static final int QUERYVISIBLECELLS = 6;
76 
77     protected void before() {
78         oSheet = (XSpreadsheet) tEnv.getObjRelation("SHEET");
79 
80         if (oSheet == null) {
81             log.println("Object relation oSheet is missing");
82             log.println("Trying to query the needed Interface");
83             oSheet = (XSpreadsheet) UnoRuntime.queryInterface(
84                              XSpreadsheet.class, tEnv.getTestObject());
85 
86             if (oSheet == null) {
87                 throw new StatusException(Status.failed(
88                                                   "Object relation oSheet is missing"));
89             }
90         }
91 
92         // expected results
93         mExpectedResults = (String[])tEnv.getObjRelation(
94                                             "XCellRangesQuery.EXPECTEDRESULTS");
95 
96         XColumnRowRange oColumnRowRange = (XColumnRowRange) UnoRuntime.queryInterface(
97                                                   XColumnRowRange.class,
98                                                   oSheet);
99         oRows = (XTableRows)oColumnRowRange.getRows();
100         oColumns = (XTableColumns) oColumnRowRange.getColumns();
101 
102         // set this in object if the interface has to make its own settings
103         // and the environment has to be disposed: this is necessary for objects
104         // that do not make entries on the sheet themselves
105         Object o = tEnv.getObjRelation("XCellRangesQuery.CREATEENTRIES");
106         if (o != null && o instanceof Boolean) {
107             bMakeEntriesAndDispose = ((Boolean)o).booleanValue();
108         }
109         if(bMakeEntriesAndDispose) {
110             oRows.removeByIndex(4, oRows.getCount() - 4);
111             oColumns.removeByIndex(4, oColumns.getCount() - 4);
112 
113             try {
114                 oSheet.getCellByPosition(1, 1).setValue(5);
115                 oSheet.getCellByPosition(1, 2).setValue(15);
116                 oSheet.getCellByPosition(2, 1).setFormula("=B2+B3");
117                 oSheet.getCellByPosition(1, 3).setFormula("=B2+B4");
118                 oSheet.getCellByPosition(3, 2).setFormula("");
119                 oSheet.getCellByPosition(3, 3).setFormula("");
120             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
121                 log.println("Couldn't fill cells " + e.getLocalizedMessage());
122             }
123         }
124 
125     }
126 
127     /**
128      * Tested method returns each cell of each column that is different to the
129      * cell in a given row
130      */
131     public void _queryColumnDifferences() {
132         boolean res = true;
133         XSheetCellRanges ranges = oObj.queryColumnDifferences(
134                                           new CellAddress((short) 0, 1, 1));
135         getting = ranges.getRangeAddressesAsString();
136         expected = mExpectedResults[QUERYCOLUMNDIFFERENCES];
137 
138         if (!getting.startsWith(expected)) {
139             log.println("Getting: " + getting);
140             log.println("Should have started with: " + expected);
141             res = false;
142         }
143 
144         tRes.tested("queryColumnDifferences()", res);
145     }
146 
147     /**
148      * Tested method returns all cells of a given type, defind in
149      * CellFlags
150      * @see com.sun.star.sheet.CellFlags
151      */
152     public void _queryContentCells() {
153         boolean res = true;
154         XSheetCellRanges ranges = oObj.queryContentCells(
155                                           (short) CellFlags.VALUE);
156         getting = ranges.getRangeAddressesAsString();
157         expected = mExpectedResults[QUERYCONTENTCELLS];
158 
159         if (!getting.startsWith(expected)) {
160             log.println("Getting: " + getting);
161             log.println("Should have started with: " + expected);
162             res = false;
163         }
164 
165         tRes.tested("queryContentCells()", res);
166     }
167 
168     /**
169      * Tested method returns all empty cells of the range
170      */
171     public void _queryEmptyCells() {
172         boolean res = true;
173         XSheetCellRanges ranges = oObj.queryEmptyCells();
174         getting = ranges.getRangeAddressesAsString();
175         expected = mExpectedResults[QUERYEMPTYCELLS];
176 
177         int startIndex = 0;
178         int endIndex = -5;
179         String checkString = null;
180 
181         while (endIndex != -1) {
182             startIndex = endIndex + 5;
183             endIndex = expected.indexOf(" ... ", startIndex);
184             if (endIndex == -1) {
185                 checkString = expected.substring(startIndex);
186             }
187             else {
188                 checkString = expected.substring(startIndex, endIndex);
189             }
190             res &= (getting.indexOf(checkString) > -1);
191         }
192 
193         if (!res) {
194             log.println("Getting: " + getting);
195             log.println("Should have contained: " + expected);
196         }
197 
198         tRes.tested("queryEmptyCells()", res);
199     }
200 
201     /**
202      * Tested method returns all cells of a given type, defind in
203      * FormulaResult
204      * @see com.sun.star.sheet.FormulaResult
205      */
206     public void _queryFormulaCells() {
207         boolean res = true;
208         XSheetCellRanges ranges = oObj.queryFormulaCells(
209                                           (short) FormulaResult.VALUE);
210         getting = ranges.getRangeAddressesAsString();
211         expected = mExpectedResults[QUERYFORMULACELLS];
212 
213         if (!getting.equals(expected)) {
214             log.println("Getting: " + getting);
215             log.println("Expected: " + expected);
216             res = false;
217         }
218 
219         tRes.tested("queryFormulaCells()", res);
220     }
221 
222     public void _queryIntersection() {
223         boolean res = true;
224         XSheetCellRanges ranges = oObj.queryIntersection(
225                                           new CellRangeAddress((short) 0, 3, 3, 7, 7));
226         getting = ranges.getRangeAddressesAsString();
227         expected = mExpectedResults[QUERYINTERSECTION];
228 
229         if (!getting.startsWith(expected)) {
230             log.println("Getting: " + getting);
231             log.println("Should have started with: " + expected);
232             res = false;
233         }
234 
235         tRes.tested("queryIntersection()", res);
236     }
237 
238     /**
239      * Tested method returns each cell of each row that is different to the
240      * cell in a given column
241      */
242     public void _queryRowDifferences() {
243         boolean res = true;
244         XSheetCellRanges ranges = oObj.queryRowDifferences(
245                                           new CellAddress((short) 0, 1, 1));
246         getting = ranges.getRangeAddressesAsString();
247         expected = mExpectedResults[QUERYROWDIFFERENCES];
248 
249         if (!getting.startsWith(expected)) {
250             log.println("Getting: " + getting);
251             log.println("Should have started with: " + expected);
252             res = false;
253         }
254 
255         tRes.tested("queryRowDifferences()", res);
256     }
257 
258     public void _queryVisibleCells() {
259         setRowVisible(false);
260 
261         boolean res = true;
262         XSheetCellRanges ranges = oObj.queryVisibleCells();
263         getting = ranges.getRangeAddressesAsString();
264         expected = mExpectedResults[QUERYVISIBLECELLS];
265 
266         if (!getting.startsWith(expected)) {
267             log.println("Getting: " + getting);
268             log.println("Should have started with: " + expected);
269             res = false;
270         }
271 
272         setRowVisible(true);
273         tRes.tested("queryVisibleCells()", res);
274     }
275 
276     protected void setRowVisible(boolean vis) {
277         try {
278             XPropertySet rowProp = (XPropertySet) UnoRuntime.queryInterface(
279                                            XPropertySet.class,
280                                            oRows.getByIndex(0));
281             rowProp.setPropertyValue("IsVisible", new Boolean(vis));
282         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
283             log.println("couldn't get Row " + e.getLocalizedMessage());
284         } catch (com.sun.star.lang.WrappedTargetException e) {
285             log.println("problems setting Property 'isVisible' " +
286                         e.getLocalizedMessage());
287         } catch (com.sun.star.beans.UnknownPropertyException e) {
288             log.println("problems setting Property 'isVisible' " +
289                         e.getLocalizedMessage());
290         } catch (com.sun.star.beans.PropertyVetoException e) {
291             log.println("problems setting Property 'isVisible' " +
292                         e.getLocalizedMessage());
293         } catch (com.sun.star.lang.IllegalArgumentException e) {
294             log.println("problems setting Property 'isVisible' " +
295                         e.getLocalizedMessage());
296         }
297     }
298 
299     /**
300     * Forces environment recreation.
301     */
302     protected void after() {
303         if(bMakeEntriesAndDispose) {
304             disposeEnvironment();
305         }
306     }
307 }