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