1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XSheetCellCursor.java,v $
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 ************************************************************************/
33 import lib
.MultiMethodTest
;
35 import com
.sun
.star
.sheet
.CellFlags
;
36 import com
.sun
.star
.sheet
.XArrayFormulaRange
;
37 import com
.sun
.star
.sheet
.XCellRangeAddressable
;
38 import com
.sun
.star
.sheet
.XSheetCellCursor
;
39 import com
.sun
.star
.sheet
.XSheetOperation
;
40 import com
.sun
.star
.sheet
.XSpreadsheet
;
41 import com
.sun
.star
.table
.CellRangeAddress
;
42 import com
.sun
.star
.table
.XCellRange
;
43 import com
.sun
.star
.table
.XColumnRowRange
;
44 import com
.sun
.star
.uno
.UnoRuntime
;
45 import com
.sun
.star
.util
.XMergeable
;
48 * Testing <code>com.sun.star.sheet.XSheetCellCursor</code>
51 * <li><code> collapseToCurrentRegion()</code></li>
52 * <li><code> collapseToCurrentArray()</code></li>
53 * <li><code> collapseToMergedArea()</code></li>
54 * <li><code> expandToEntireColumns()</code></li>
55 * <li><code> expandToEntireRows()</code></li>
56 * <li><code> collapseToSize()</code></li>
58 * Component must also implement the following interfaces :
60 * <li> <code> com.sun.star.sheet.XCellRangeAddressable </code> :
61 * to get range address </li>
63 * Range of cursor must be of size 4 x 4. <p>
64 * @see com.sun.star.sheet.XSheetCellCursor
66 public class _XSheetCellCursor
extends MultiMethodTest
{
68 public XSheetCellCursor oObj
= null;
71 * Test creates the array formula, assigns this array to another array,
72 * collapses cursor into one cell, applies method, checks the size of the
73 * result range, erases array formula, checks that array formula has been
75 * Has <b>OK</b> status if no exceptions were thrown, if size of the result
76 * range is equal to size of the range where the array formula was set and
77 * if array formula was successfully cleared. <p>
79 public void _collapseToCurrentArray() {
80 boolean bResult
= false;
82 XCellRangeAddressable crAddr
= (XCellRangeAddressable
)
83 UnoRuntime
.queryInterface(XCellRangeAddressable
.class, oObj
);
84 CellRangeAddress addr
= crAddr
.getRangeAddress() ;
85 int leftCol
= addr
.StartColumn
;
86 int topRow
= addr
.StartRow
;
87 int width
= addr
.EndColumn
- addr
.StartColumn
+ 1 ;
88 int height
= addr
.EndRow
- addr
.StartRow
+ 1 ;
90 log
.println( "Object area is ((" + leftCol
+ "," + topRow
+ "),(" +
91 (leftCol
+ width
- 1) + "," + (topRow
+ height
- 1) + ")" );
93 XCellRange new_range
= null;
95 // first we need to create an array formula
97 oObj
.getCellRangeByPosition(0, 0, 0, height
- 1);
98 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
99 log
.print("Get cell range by position failed: ");
100 e
.printStackTrace(log
);
101 tRes
.tested("collapseToCurrentArray()", false);
104 log
.println("DB: Successfully new range created");
105 XArrayFormulaRange arrFormulaRange
= (XArrayFormulaRange
)
106 UnoRuntime
.queryInterface (XArrayFormulaRange
.class, new_range
);
107 // write a simple formula (this array assigns another array)
108 arrFormulaRange
.setArrayFormula("A1:A" + height
) ;
110 // collapse cursor into one cell and then try to apply the method
111 oObj
.collapseToSize (1, 1) ;
112 oObj
.collapseToCurrentArray() ;
114 // check the size of result range
115 int cols
= ( (XColumnRowRange
)UnoRuntime
.queryInterface(
116 XColumnRowRange
.class, oObj
) ).getColumns().getCount();
117 int rows
= ( (XColumnRowRange
)UnoRuntime
.queryInterface(
118 XColumnRowRange
.class, oObj
) ).getRows().getCount();
120 if (cols
== 1 && rows
== height
) {
124 log
.println("The size of cell range must be 1x" + height
+
125 ", but after method call it was " + cols
+ "x" + rows
);
128 // erase array formula
129 arrFormulaRange
.setArrayFormula("");
131 // check if array formula has been cleared with last statement
133 // if array formula isn't cleared exception is thrown
134 new_range
.getCellByPosition(0,0).setValue(111) ;
135 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
138 "Array formula hasn't been cleared with setArrayFormula(\"\")");
139 XSheetOperation clearRange
= (XSheetOperation
)
140 UnoRuntime
.queryInterface (XSheetOperation
.class, new_range
);
142 CellFlags
.ANNOTATION
| CellFlags
.DATETIME
| CellFlags
.EDITATTR
;
144 | CellFlags
.HARDATTR
| CellFlags
.OBJECTS
| CellFlags
.STRING
;
146 | CellFlags
.VALUE
| CellFlags
.FORMULA
| CellFlags
.STYLES
;
147 clearRange
.clearContents(allFlags
) ;
150 tRes
.tested("collapseToCurrentArray()", bResult
);
154 * Test clears contents of spreadsheet, collapses cursor to current range,
155 * checks size of cursor range, fills a cell that is close to
156 * cursor range, collapses cursor to current range, checks size of cursor
157 * range again and restores original size. <p>
158 * Has <b> OK </b> status if after clearing of content and collapsing cursor
159 * range size remains 4 x 4, if after filling of cell and collapsing cursor
160 * range extends by one in both dimensions and no exceptions were thrown.<p>
162 public void _collapseToCurrentRegion(){
163 boolean bResult
= true;
164 int width
= 4, height
= 4;
165 int leftCol
= -1, topRow
= -1;
167 XSpreadsheet oSheet
= oObj
.getSpreadsheet();
168 ((XSheetOperation
) UnoRuntime
.queryInterface(
169 XSheetOperation
.class, oSheet
) ).clearContents(65535);
170 oObj
.collapseToCurrentRegion();
171 int cols
= ((XColumnRowRange
)
172 UnoRuntime
.queryInterface(
173 XColumnRowRange
.class, oObj
) ).getColumns().getCount();
174 int rows
= ((XColumnRowRange
)
175 UnoRuntime
.queryInterface(
176 XColumnRowRange
.class, oObj
) ).getRows().getCount();
178 if (cols
!= width
|| rows
!= height
) {
180 log
.println("After collapseToCurrentRegion()"
181 + " call Region must have size " + width
+ "x" + height
182 + " but it is " + cols
+ "x" + rows
);
185 // if previous test was successful try more complicated case
187 if (leftCol
!= -1 && topRow
!= -1) {
189 oSheet
.getCellByPosition(
190 leftCol
+ width
, topRow
+ height
).setValue(1);
191 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
192 log
.print("Can't get cell by position:");
193 e
.printStackTrace(log
);
197 oObj
.collapseToCurrentRegion() ;
200 cols
= ((XColumnRowRange
)
201 UnoRuntime
.queryInterface(
202 XColumnRowRange
.class, oObj
)).getColumns().getCount();
203 rows
= ((XColumnRowRange
)
204 UnoRuntime
.queryInterface(
205 XColumnRowRange
.class, oObj
)).getRows().getCount();
207 if (cols
== width
+ 1 && rows
== height
+ 1) {
211 log
.println("After collapseToCurrentRegion() call [2]"
212 + " region must have size " + (width
+1) + "x"
213 + (height
+ 1) + " but it is " + cols
+ "x" + rows
);
218 tRes
.tested("collapseToCurrentRegion()", bResult
);
220 // restore original size
221 oObj
.collapseToSize(width
, height
);
225 * Test merges a cells of range that has a greater size, collapses cursor to
226 * merged area, checks size of cursor range and restores original size
227 * of cursor range. <p>
228 * Has <b> OK </b> status if after merging of cells and collapsing cursor
229 * range extends by one in both dimensions and no exceptions were thrown.<p>
231 public void _collapseToMergedArea(){
232 int width
= 1, height
= 1 ;
233 int leftCol
= 0, topRow
= 0 ;
235 boolean bResult
= true ;
237 log
.println("DB: Starting collapseToMergedArea() method test ...") ;
238 XSpreadsheet oSheet
= oObj
.getSpreadsheet() ;
239 log
.println ("DB: got Spreadsheet.") ;
241 XCellRange newRange
= null;
243 newRange
= oSheet
.getCellRangeByPosition (
244 leftCol
+ width
- 1, topRow
+ height
- 1,
245 leftCol
+ width
, topRow
+ height
);
246 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
247 log
.println("Can't get cell range by position");
248 e
.printStackTrace(log
);
252 XMergeable mergeRange
= (XMergeable
)
253 UnoRuntime
.queryInterface (XMergeable
.class, newRange
);
254 if (mergeRange
== null) {
255 log
.println("DB: newRange doesn't implement XMergeable interface");
257 log
.println("DB: XMergeable interface successfully queried.");
260 mergeRange
.merge(true);
261 log
.println("DB: Successfuly merged.") ;
263 oObj
.collapseToMergedArea() ;
264 log
.println("DB: Succesfully collapseToMergedArea() method called");
266 // unmerge area to restore SpreadSheet
267 mergeRange
.merge(false);
268 log
.println("DB: Successfully unmerged.") ;
271 int cols
= ((XColumnRowRange
)
272 UnoRuntime
.queryInterface(
273 XColumnRowRange
.class, oObj
) ).getColumns().getCount();
274 int rows
= ((XColumnRowRange
)
275 UnoRuntime
.queryInterface(
276 XColumnRowRange
.class, oObj
) ).getRows().getCount();
277 log
.println("DB: Column and row numbers succesfully get") ;
279 if (cols
== width
+ 1 && rows
== height
+ 3) {
284 "After collapseToMergedArea() call region must have size "
285 + (width
+ 1) + "x" + (height
+ 1) + " but it is " + cols
289 tRes
.tested("collapseToMergedArea()", bResult
) ;
291 // restore original size
292 oObj
.collapseToSize(width
, height
);
296 * Test collapses cursor to the new size, checks size
297 * of cursor range and restores original size of cursor range. <p>
298 * Has <b> OK </b> status if after collapsing cursor
299 * range extends by three in both dimensions. <p>
301 public void _collapseToSize(){
302 boolean bResult
= false;
303 int width
= 1, height
= 1;
305 // collapseToSize() method test
306 oObj
.collapseToSize (width
+ 3, height
+ 3);
309 int cols
= ((XColumnRowRange
)
310 UnoRuntime
.queryInterface(
311 XColumnRowRange
.class, oObj
) ).getColumns().getCount();
312 int rows
= ((XColumnRowRange
)
313 UnoRuntime
.queryInterface(
314 XColumnRowRange
.class, oObj
) ).getRows().getCount();
316 if (cols
== width
+ 3 && rows
== height
+ 3) {
320 log
.println( "After collapseToSize() call region must have size "
321 + (width
+ 3) + "x" + (height
+ 3) + " but it is "
325 tRes
.tested("collapseToSize()", bResult
) ;
327 // restore original size
328 oObj
.collapseToSize(width
, height
) ;
332 * Test expands cursor to entire columns, checks size
333 * of cursor range and restores original size of cursor range. <p>
334 * Has <b> OK </b> status if after expanding cursor
335 * range extends to all rows in the columns (number of rows is greater than
336 * 32000 and number of columns remains the same). <p>
338 public void _expandToEntireColumns(){
339 boolean bResult
= false;
340 int width
= 1, height
= 1 ;
342 // expandToEntireColumns() method test
343 oObj
.expandToEntireColumns () ;
346 int cols
= ((XColumnRowRange
)
347 UnoRuntime
.queryInterface(
348 XColumnRowRange
.class, oObj
) ).getColumns().getCount();
349 int rows
= ((XColumnRowRange
)
350 UnoRuntime
.queryInterface(
351 XColumnRowRange
.class, oObj
) ).getRows().getCount();
353 if (cols
== width
&& rows
>= 32000) {
358 "After expandToEntireColumns() call region must have size "+
359 width
+ "x(>=32000) but it is " + cols
+ "x" + rows
);
362 tRes
.tested("expandToEntireColumns()", bResult
) ;
364 // restore original size
365 oObj
.collapseToSize(width
, height
) ;
369 * Test expands cursor to entire rows, checks size
370 * of cursor range and restores original size of cursor range. <p>
371 * Has <b> OK </b> status if after expanding cursor
372 * range extends to all columns in the rows (number of columns is greater
373 * than 256 and number of rows remains the same). <p>
375 public void _expandToEntireRows(){
376 boolean bResult
= false;
377 int width
= 1, height
= 1 ;
379 // expandToEntireRows() method test
380 oObj
.expandToEntireRows () ;
383 int cols
= ((XColumnRowRange
)
384 UnoRuntime
.queryInterface(
385 XColumnRowRange
.class, oObj
) ).getColumns().getCount();
386 int rows
= ((XColumnRowRange
)
387 UnoRuntime
.queryInterface(
388 XColumnRowRange
.class, oObj
) ).getRows().getCount();
390 if (cols
>= 256 && rows
== height
) {
394 log
.println("After expandToEntireRows() call region " +
395 "must have size (>=256)x" + height
+ " but it is " +
399 tRes
.tested("expandToEntireRows()", bResult
) ;
401 // restore original size
402 oObj
.collapseToSize(width
, height
) ;
405 } // EOC _XSheetCellCursor