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;
69 protected void before() {
70 oSheet
= (XSpreadsheet
) tEnv
.getObjRelation("SHEET");
73 log
.println("Object relation oSheet is missing");
74 log
.println("Trying to query the needed Interface");
75 oSheet
= UnoRuntime
.queryInterface(
76 XSpreadsheet
.class, tEnv
.getTestObject());
79 throw new StatusException(Status
.failed(
80 "Object relation oSheet is missing"));
85 mExpectedResults
= (String
[])tEnv
.getObjRelation(
86 "XCellRangesQuery.EXPECTEDRESULTS");
88 XColumnRowRange oColumnRowRange
= UnoRuntime
.queryInterface(
89 XColumnRowRange
.class,
91 oRows
= oColumnRowRange
.getRows();
92 oColumns
= oColumnRowRange
.getColumns();
94 // set this in object if the interface has to make its own settings
95 // and the environment has to be disposed: this is necessary for objects
96 // that do not make entries on the sheet themselves
97 Object o
= tEnv
.getObjRelation("XCellRangesQuery.CREATEENTRIES");
98 if (o
instanceof Boolean
) {
99 bMakeEntriesAndDispose
= ((Boolean
)o
).booleanValue();
101 if(bMakeEntriesAndDispose
) {
102 oRows
.removeByIndex(4, oRows
.getCount() - 4);
103 oColumns
.removeByIndex(4, oColumns
.getCount() - 4);
106 oSheet
.getCellByPosition(1, 1).setValue(5);
107 oSheet
.getCellByPosition(1, 2).setValue(15);
108 oSheet
.getCellByPosition(2, 1).setFormula("=B2+B3");
109 oSheet
.getCellByPosition(1, 3).setFormula("=B2+B4");
110 oSheet
.getCellByPosition(3, 2).setFormula("");
111 oSheet
.getCellByPosition(3, 3).setFormula("");
112 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
113 log
.println("Couldn't fill cells " + e
.getLocalizedMessage());
120 * Tested method returns each cell of each column that is different to the
121 * cell in a given row
123 public void _queryColumnDifferences() {
125 XSheetCellRanges ranges
= oObj
.queryColumnDifferences(
126 new CellAddress((short) 0, 1, 1));
127 getting
= ranges
.getRangeAddressesAsString();
128 expected
= mExpectedResults
[QUERYCOLUMNDIFFERENCES
];
130 if (!getting
.startsWith(expected
)) {
131 log
.println("Getting: " + getting
);
132 log
.println("Should have started with: " + expected
);
136 tRes
.tested("queryColumnDifferences()", res
);
140 * Tested method returns all cells of a given type, defined in
142 * @see com.sun.star.sheet.CellFlags
144 public void _queryContentCells() {
146 XSheetCellRanges ranges
= oObj
.queryContentCells(
147 (short) CellFlags
.VALUE
);
148 getting
= ranges
.getRangeAddressesAsString();
149 expected
= mExpectedResults
[QUERYCONTENTCELLS
];
151 if (!getting
.startsWith(expected
)) {
152 log
.println("Getting: " + getting
);
153 log
.println("Should have started with: " + expected
);
157 tRes
.tested("queryContentCells()", res
);
161 * Tested method returns all empty cells of the range
163 public void _queryEmptyCells() {
165 XSheetCellRanges ranges
= oObj
.queryEmptyCells();
166 getting
= ranges
.getRangeAddressesAsString();
167 expected
= mExpectedResults
[QUERYEMPTYCELLS
];
171 String checkString
= null;
173 while (endIndex
!= -1) {
174 startIndex
= endIndex
+ 5;
175 endIndex
= expected
.indexOf(" ... ", startIndex
);
176 if (endIndex
== -1) {
177 checkString
= expected
.substring(startIndex
);
180 checkString
= expected
.substring(startIndex
, endIndex
);
182 res
&= (getting
.indexOf(checkString
) > -1);
186 log
.println("Getting: " + getting
);
187 log
.println("Should have contained: " + expected
);
190 tRes
.tested("queryEmptyCells()", res
);
194 * Tested method returns all cells of a given type, defined in
196 * @see com.sun.star.sheet.FormulaResult
198 public void _queryFormulaCells() {
200 XSheetCellRanges ranges
= oObj
.queryFormulaCells(
201 (short) FormulaResult
.VALUE
);
202 getting
= ranges
.getRangeAddressesAsString();
203 expected
= mExpectedResults
[QUERYFORMULACELLS
];
205 if (!getting
.equals(expected
)) {
206 log
.println("Getting: " + getting
);
207 log
.println("Expected: " + expected
);
211 tRes
.tested("queryFormulaCells()", res
);
214 public void _queryIntersection() {
216 XSheetCellRanges ranges
= oObj
.queryIntersection(
217 new CellRangeAddress((short) 0, 3, 3, 7, 7));
218 getting
= ranges
.getRangeAddressesAsString();
219 expected
= mExpectedResults
[QUERYINTERSECTION
];
221 if (!getting
.startsWith(expected
)) {
222 log
.println("Getting: " + getting
);
223 log
.println("Should have started with: " + expected
);
227 tRes
.tested("queryIntersection()", res
);
231 * Tested method returns each cell of each row that is different to the
232 * cell in a given column
234 public void _queryRowDifferences() {
236 XSheetCellRanges ranges
= oObj
.queryRowDifferences(
237 new CellAddress((short) 0, 1, 1));
238 getting
= ranges
.getRangeAddressesAsString();
239 expected
= mExpectedResults
[QUERYROWDIFFERENCES
];
241 if (!getting
.startsWith(expected
)) {
242 log
.println("Getting: " + getting
);
243 log
.println("Should have started with: " + expected
);
247 tRes
.tested("queryRowDifferences()", res
);
250 public void _queryVisibleCells() {
251 setRowVisible(false);
254 XSheetCellRanges ranges
= oObj
.queryVisibleCells();
255 getting
= ranges
.getRangeAddressesAsString();
256 expected
= mExpectedResults
[QUERYVISIBLECELLS
];
258 if (!getting
.startsWith(expected
)) {
259 log
.println("Getting: " + getting
);
260 log
.println("Should have started with: " + expected
);
265 tRes
.tested("queryVisibleCells()", res
);
268 protected void setRowVisible(boolean vis
) {
270 XPropertySet rowProp
= UnoRuntime
.queryInterface(
272 oRows
.getByIndex(0));
273 rowProp
.setPropertyValue("IsVisible", Boolean
.valueOf(vis
));
274 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
275 log
.println("couldn't get Row " + e
.getLocalizedMessage());
276 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
277 log
.println("problems setting Property 'isVisible' " +
278 e
.getLocalizedMessage());
279 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
280 log
.println("problems setting Property 'isVisible' " +
281 e
.getLocalizedMessage());
282 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
283 log
.println("problems setting Property 'isVisible' " +
284 e
.getLocalizedMessage());
285 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
286 log
.println("problems setting Property 'isVisible' " +
287 e
.getLocalizedMessage());
292 * Forces environment recreation.
295 protected void after() {
296 if(bMakeEntriesAndDispose
) {
297 disposeEnvironment();