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