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