2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import lib
.MultiMethodTest
;
22 import lib
.StatusException
;
24 import com
.sun
.star
.beans
.XPropertySet
;
25 import com
.sun
.star
.sheet
.CellFlags
;
26 import com
.sun
.star
.sheet
.FormulaResult
;
27 import com
.sun
.star
.sheet
.XCellRangesQuery
;
28 import com
.sun
.star
.sheet
.XSheetCellRanges
;
29 import com
.sun
.star
.sheet
.XSpreadsheet
;
30 import com
.sun
.star
.table
.CellAddress
;
31 import com
.sun
.star
.table
.CellRangeAddress
;
32 import com
.sun
.star
.table
.XColumnRowRange
;
33 import com
.sun
.star
.table
.XTableColumns
;
34 import com
.sun
.star
.table
.XTableRows
;
35 import com
.sun
.star
.uno
.UnoRuntime
;
38 * Test the XCellRangesQuery interface.
39 * Needed object relations:
41 * <li>"SHEET": an XSpreadSheet object
43 * <li>"XCellRangesQuery.EXPECTEDRESULTS": the expected results for the test
44 * methods as a String array.<br>
45 * @see mod._sc.ScCellCursorObj or
46 * @see mod._sc.ScCellObj for an example how this should look like.
50 public class _XCellRangesQuery
extends MultiMethodTest
{
51 public XCellRangesQuery oObj
;
52 protected XSpreadsheet oSheet
;
53 protected XTableRows oRows
;
54 protected XTableColumns oColumns
;
55 protected String
[] mExpectedResults
= null;
56 protected boolean bMakeEntriesAndDispose
= false;
59 // provide the object with constants to fill the expected results array
60 public static final int QUERYCOLUMNDIFFERENCES
= 0;
61 public static final int QUERYCONTENTCELLS
= 1;
62 public static final int QUERYEMPTYCELLS
= 2;
63 public static final int QUERYFORMULACELLS
= 3;
64 public static final int QUERYINTERSECTION
= 4;
65 public static final int QUERYROWDIFFERENCES
= 5;
66 public static final int QUERYVISIBLECELLS
= 6;
68 protected void before() {
69 oSheet
= (XSpreadsheet
) tEnv
.getObjRelation("SHEET");
72 log
.println("Object relation oSheet is missing");
73 log
.println("Trying to query the needed Interface");
74 oSheet
= UnoRuntime
.queryInterface(
75 XSpreadsheet
.class, tEnv
.getTestObject());
78 throw new StatusException(Status
.failed(
79 "Object relation oSheet is missing"));
84 mExpectedResults
= (String
[])tEnv
.getObjRelation(
85 "XCellRangesQuery.EXPECTEDRESULTS");
87 XColumnRowRange oColumnRowRange
= UnoRuntime
.queryInterface(
88 XColumnRowRange
.class,
90 oRows
= oColumnRowRange
.getRows();
91 oColumns
= oColumnRowRange
.getColumns();
93 // set this in object if the interface has to make its own settings
94 // and the environment has to be disposed: this is necessary for objects
95 // that do not make entries on the sheet themselves
96 Object o
= tEnv
.getObjRelation("XCellRangesQuery.CREATEENTRIES");
97 if (o
!= null && o
instanceof Boolean
) {
98 bMakeEntriesAndDispose
= ((Boolean
)o
).booleanValue();
100 if(bMakeEntriesAndDispose
) {
101 oRows
.removeByIndex(4, oRows
.getCount() - 4);
102 oColumns
.removeByIndex(4, oColumns
.getCount() - 4);
105 oSheet
.getCellByPosition(1, 1).setValue(5);
106 oSheet
.getCellByPosition(1, 2).setValue(15);
107 oSheet
.getCellByPosition(2, 1).setFormula("=B2+B3");
108 oSheet
.getCellByPosition(1, 3).setFormula("=B2+B4");
109 oSheet
.getCellByPosition(3, 2).setFormula("");
110 oSheet
.getCellByPosition(3, 3).setFormula("");
111 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
112 log
.println("Couldn't fill cells " + e
.getLocalizedMessage());
119 * Tested method returns each cell of each column that is different to the
120 * cell in a given row
122 public void _queryColumnDifferences() {
124 XSheetCellRanges ranges
= oObj
.queryColumnDifferences(
125 new CellAddress((short) 0, 1, 1));
126 getting
= ranges
.getRangeAddressesAsString();
127 expected
= mExpectedResults
[QUERYCOLUMNDIFFERENCES
];
129 if (!getting
.startsWith(expected
)) {
130 log
.println("Getting: " + getting
);
131 log
.println("Should have started with: " + expected
);
135 tRes
.tested("queryColumnDifferences()", res
);
139 * Tested method returns all cells of a given type, defind in
141 * @see com.sun.star.sheet.CellFlags
143 public void _queryContentCells() {
145 XSheetCellRanges ranges
= oObj
.queryContentCells(
146 (short) CellFlags
.VALUE
);
147 getting
= ranges
.getRangeAddressesAsString();
148 expected
= mExpectedResults
[QUERYCONTENTCELLS
];
150 if (!getting
.startsWith(expected
)) {
151 log
.println("Getting: " + getting
);
152 log
.println("Should have started with: " + expected
);
156 tRes
.tested("queryContentCells()", res
);
160 * Tested method returns all empty cells of the range
162 public void _queryEmptyCells() {
164 XSheetCellRanges ranges
= oObj
.queryEmptyCells();
165 getting
= ranges
.getRangeAddressesAsString();
166 expected
= mExpectedResults
[QUERYEMPTYCELLS
];
170 String checkString
= null;
172 while (endIndex
!= -1) {
173 startIndex
= endIndex
+ 5;
174 endIndex
= expected
.indexOf(" ... ", startIndex
);
175 if (endIndex
== -1) {
176 checkString
= expected
.substring(startIndex
);
179 checkString
= expected
.substring(startIndex
, endIndex
);
181 res
&= (getting
.indexOf(checkString
) > -1);
185 log
.println("Getting: " + getting
);
186 log
.println("Should have contained: " + expected
);
189 tRes
.tested("queryEmptyCells()", res
);
193 * Tested method returns all cells of a given type, defind in
195 * @see com.sun.star.sheet.FormulaResult
197 public void _queryFormulaCells() {
199 XSheetCellRanges ranges
= oObj
.queryFormulaCells(
200 (short) FormulaResult
.VALUE
);
201 getting
= ranges
.getRangeAddressesAsString();
202 expected
= mExpectedResults
[QUERYFORMULACELLS
];
204 if (!getting
.equals(expected
)) {
205 log
.println("Getting: " + getting
);
206 log
.println("Expected: " + expected
);
210 tRes
.tested("queryFormulaCells()", res
);
213 public void _queryIntersection() {
215 XSheetCellRanges ranges
= oObj
.queryIntersection(
216 new CellRangeAddress((short) 0, 3, 3, 7, 7));
217 getting
= ranges
.getRangeAddressesAsString();
218 expected
= mExpectedResults
[QUERYINTERSECTION
];
220 if (!getting
.startsWith(expected
)) {
221 log
.println("Getting: " + getting
);
222 log
.println("Should have started with: " + expected
);
226 tRes
.tested("queryIntersection()", res
);
230 * Tested method returns each cell of each row that is different to the
231 * cell in a given column
233 public void _queryRowDifferences() {
235 XSheetCellRanges ranges
= oObj
.queryRowDifferences(
236 new CellAddress((short) 0, 1, 1));
237 getting
= ranges
.getRangeAddressesAsString();
238 expected
= mExpectedResults
[QUERYROWDIFFERENCES
];
240 if (!getting
.startsWith(expected
)) {
241 log
.println("Getting: " + getting
);
242 log
.println("Should have started with: " + expected
);
246 tRes
.tested("queryRowDifferences()", res
);
249 public void _queryVisibleCells() {
250 setRowVisible(false);
253 XSheetCellRanges ranges
= oObj
.queryVisibleCells();
254 getting
= ranges
.getRangeAddressesAsString();
255 expected
= mExpectedResults
[QUERYVISIBLECELLS
];
257 if (!getting
.startsWith(expected
)) {
258 log
.println("Getting: " + getting
);
259 log
.println("Should have started with: " + expected
);
264 tRes
.tested("queryVisibleCells()", res
);
267 protected void setRowVisible(boolean vis
) {
269 XPropertySet rowProp
= UnoRuntime
.queryInterface(
271 oRows
.getByIndex(0));
272 rowProp
.setPropertyValue("IsVisible", new Boolean(vis
));
273 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
274 log
.println("couldn't get Row " + e
.getLocalizedMessage());
275 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
276 log
.println("problems setting Property 'isVisible' " +
277 e
.getLocalizedMessage());
278 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
279 log
.println("problems setting Property 'isVisible' " +
280 e
.getLocalizedMessage());
281 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
282 log
.println("problems setting Property 'isVisible' " +
283 e
.getLocalizedMessage());
284 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
285 log
.println("problems setting Property 'isVisible' " +
286 e
.getLocalizedMessage());
291 * Forces environment recreation.
293 protected void after() {
294 if(bMakeEntriesAndDispose
) {
295 disposeEnvironment();