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