1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
29 import lib
.MultiMethodTest
;
31 import lib
.StatusException
;
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
;
47 * Test the XCellRangesQuery interface.
48 * Needed object relations:
50 * <li>"SHEET": an XSpreadSheet object
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.
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;
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;
77 protected void before() {
78 oSheet
= (XSpreadsheet
) tEnv
.getObjRelation("SHEET");
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());
87 throw new StatusException(Status
.failed(
88 "Object relation oSheet is missing"));
93 mExpectedResults
= (String
[])tEnv
.getObjRelation(
94 "XCellRangesQuery.EXPECTEDRESULTS");
96 XColumnRowRange oColumnRowRange
= (XColumnRowRange
) UnoRuntime
.queryInterface(
97 XColumnRowRange
.class,
99 oRows
= (XTableRows
)oColumnRowRange
.getRows();
100 oColumns
= (XTableColumns
) oColumnRowRange
.getColumns();
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();
109 if(bMakeEntriesAndDispose
) {
110 oRows
.removeByIndex(4, oRows
.getCount() - 4);
111 oColumns
.removeByIndex(4, oColumns
.getCount() - 4);
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());
128 * Tested method returns each cell of each column that is different to the
129 * cell in a given row
131 public void _queryColumnDifferences() {
133 XSheetCellRanges ranges
= oObj
.queryColumnDifferences(
134 new CellAddress((short) 0, 1, 1));
135 getting
= ranges
.getRangeAddressesAsString();
136 expected
= mExpectedResults
[QUERYCOLUMNDIFFERENCES
];
138 if (!getting
.startsWith(expected
)) {
139 log
.println("Getting: " + getting
);
140 log
.println("Should have started with: " + expected
);
144 tRes
.tested("queryColumnDifferences()", res
);
148 * Tested method returns all cells of a given type, defind in
150 * @see com.sun.star.sheet.CellFlags
152 public void _queryContentCells() {
154 XSheetCellRanges ranges
= oObj
.queryContentCells(
155 (short) CellFlags
.VALUE
);
156 getting
= ranges
.getRangeAddressesAsString();
157 expected
= mExpectedResults
[QUERYCONTENTCELLS
];
159 if (!getting
.startsWith(expected
)) {
160 log
.println("Getting: " + getting
);
161 log
.println("Should have started with: " + expected
);
165 tRes
.tested("queryContentCells()", res
);
169 * Tested method returns all empty cells of the range
171 public void _queryEmptyCells() {
173 XSheetCellRanges ranges
= oObj
.queryEmptyCells();
174 getting
= ranges
.getRangeAddressesAsString();
175 expected
= mExpectedResults
[QUERYEMPTYCELLS
];
179 String checkString
= null;
181 while (endIndex
!= -1) {
182 startIndex
= endIndex
+ 5;
183 endIndex
= expected
.indexOf(" ... ", startIndex
);
184 if (endIndex
== -1) {
185 checkString
= expected
.substring(startIndex
);
188 checkString
= expected
.substring(startIndex
, endIndex
);
190 res
&= (getting
.indexOf(checkString
) > -1);
194 log
.println("Getting: " + getting
);
195 log
.println("Should have contained: " + expected
);
198 tRes
.tested("queryEmptyCells()", res
);
202 * Tested method returns all cells of a given type, defind in
204 * @see com.sun.star.sheet.FormulaResult
206 public void _queryFormulaCells() {
208 XSheetCellRanges ranges
= oObj
.queryFormulaCells(
209 (short) FormulaResult
.VALUE
);
210 getting
= ranges
.getRangeAddressesAsString();
211 expected
= mExpectedResults
[QUERYFORMULACELLS
];
213 if (!getting
.equals(expected
)) {
214 log
.println("Getting: " + getting
);
215 log
.println("Expected: " + expected
);
219 tRes
.tested("queryFormulaCells()", res
);
222 public void _queryIntersection() {
224 XSheetCellRanges ranges
= oObj
.queryIntersection(
225 new CellRangeAddress((short) 0, 3, 3, 7, 7));
226 getting
= ranges
.getRangeAddressesAsString();
227 expected
= mExpectedResults
[QUERYINTERSECTION
];
229 if (!getting
.startsWith(expected
)) {
230 log
.println("Getting: " + getting
);
231 log
.println("Should have started with: " + expected
);
235 tRes
.tested("queryIntersection()", res
);
239 * Tested method returns each cell of each row that is different to the
240 * cell in a given column
242 public void _queryRowDifferences() {
244 XSheetCellRanges ranges
= oObj
.queryRowDifferences(
245 new CellAddress((short) 0, 1, 1));
246 getting
= ranges
.getRangeAddressesAsString();
247 expected
= mExpectedResults
[QUERYROWDIFFERENCES
];
249 if (!getting
.startsWith(expected
)) {
250 log
.println("Getting: " + getting
);
251 log
.println("Should have started with: " + expected
);
255 tRes
.tested("queryRowDifferences()", res
);
258 public void _queryVisibleCells() {
259 setRowVisible(false);
262 XSheetCellRanges ranges
= oObj
.queryVisibleCells();
263 getting
= ranges
.getRangeAddressesAsString();
264 expected
= mExpectedResults
[QUERYVISIBLECELLS
];
266 if (!getting
.startsWith(expected
)) {
267 log
.println("Getting: " + getting
);
268 log
.println("Should have started with: " + expected
);
273 tRes
.tested("queryVisibleCells()", res
);
276 protected void setRowVisible(boolean vis
) {
278 XPropertySet rowProp
= (XPropertySet
) UnoRuntime
.queryInterface(
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());
300 * Forces environment recreation.
302 protected void after() {
303 if(bMakeEntriesAndDispose
) {
304 disposeEnvironment();