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 ************************************************************************/
30 import lib
.MultiMethodTest
;
32 import com
.sun
.star
.sheet
.CellFlags
;
33 import com
.sun
.star
.sheet
.XArrayFormulaRange
;
34 import com
.sun
.star
.sheet
.XCellRangeAddressable
;
35 import com
.sun
.star
.sheet
.XSheetCellCursor
;
36 import com
.sun
.star
.sheet
.XSheetOperation
;
37 import com
.sun
.star
.sheet
.XSpreadsheet
;
38 import com
.sun
.star
.table
.CellRangeAddress
;
39 import com
.sun
.star
.table
.XCellRange
;
40 import com
.sun
.star
.table
.XColumnRowRange
;
41 import com
.sun
.star
.uno
.UnoRuntime
;
42 import com
.sun
.star
.util
.XMergeable
;
45 * Testing <code>com.sun.star.sheet.XSheetCellCursor</code>
48 * <li><code> collapseToCurrentRegion()</code></li>
49 * <li><code> collapseToCurrentArray()</code></li>
50 * <li><code> collapseToMergedArea()</code></li>
51 * <li><code> expandToEntireColumns()</code></li>
52 * <li><code> expandToEntireRows()</code></li>
53 * <li><code> collapseToSize()</code></li>
55 * Component must also implement the following interfaces :
57 * <li> <code> com.sun.star.sheet.XCellRangeAddressable </code> :
58 * to get range address </li>
60 * Range of cursor must be of size 4 x 4. <p>
61 * @see com.sun.star.sheet.XSheetCellCursor
63 public class _XSheetCellCursor
extends MultiMethodTest
{
65 public XSheetCellCursor oObj
= null;
68 * Test creates the array formula, assigns this array to another array,
69 * collapses cursor into one cell, applies method, checks the size of the
70 * result range, erases array formula, checks that array formula has been
72 * Has <b>OK</b> status if no exceptions were thrown, if size of the result
73 * range is equal to size of the range where the array formula was set and
74 * if array formula was successfully cleared. <p>
76 public void _collapseToCurrentArray() {
77 boolean bResult
= false;
79 XCellRangeAddressable crAddr
= (XCellRangeAddressable
)
80 UnoRuntime
.queryInterface(XCellRangeAddressable
.class, oObj
);
81 CellRangeAddress addr
= crAddr
.getRangeAddress() ;
82 int leftCol
= addr
.StartColumn
;
83 int topRow
= addr
.StartRow
;
84 int width
= addr
.EndColumn
- addr
.StartColumn
+ 1 ;
85 int height
= addr
.EndRow
- addr
.StartRow
+ 1 ;
87 log
.println( "Object area is ((" + leftCol
+ "," + topRow
+ "),(" +
88 (leftCol
+ width
- 1) + "," + (topRow
+ height
- 1) + ")" );
90 XCellRange new_range
= null;
92 // first we need to create an array formula
94 oObj
.getCellRangeByPosition(0, 0, 0, height
- 1);
95 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
96 log
.print("Get cell range by position failed: ");
97 e
.printStackTrace(log
);
98 tRes
.tested("collapseToCurrentArray()", false);
101 log
.println("DB: Successfully new range created");
102 XArrayFormulaRange arrFormulaRange
= (XArrayFormulaRange
)
103 UnoRuntime
.queryInterface (XArrayFormulaRange
.class, new_range
);
104 // write a simple formula (this array assigns another array)
105 arrFormulaRange
.setArrayFormula("A1:A" + height
) ;
107 // collapse cursor into one cell and then try to apply the method
108 oObj
.collapseToSize (1, 1) ;
109 oObj
.collapseToCurrentArray() ;
111 // check the size of result range
112 int cols
= ( (XColumnRowRange
)UnoRuntime
.queryInterface(
113 XColumnRowRange
.class, oObj
) ).getColumns().getCount();
114 int rows
= ( (XColumnRowRange
)UnoRuntime
.queryInterface(
115 XColumnRowRange
.class, oObj
) ).getRows().getCount();
117 if (cols
== 1 && rows
== height
) {
121 log
.println("The size of cell range must be 1x" + height
+
122 ", but after method call it was " + cols
+ "x" + rows
);
125 // erase array formula
126 arrFormulaRange
.setArrayFormula("");
128 // check if array formula has been cleared with last statement
130 // if array formula isn't cleared exception is thrown
131 new_range
.getCellByPosition(0,0).setValue(111) ;
132 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
135 "Array formula hasn't been cleared with setArrayFormula(\"\")");
136 XSheetOperation clearRange
= (XSheetOperation
)
137 UnoRuntime
.queryInterface (XSheetOperation
.class, new_range
);
139 CellFlags
.ANNOTATION
| CellFlags
.DATETIME
| CellFlags
.EDITATTR
;
141 | CellFlags
.HARDATTR
| CellFlags
.OBJECTS
| CellFlags
.STRING
;
143 | CellFlags
.VALUE
| CellFlags
.FORMULA
| CellFlags
.STYLES
;
144 clearRange
.clearContents(allFlags
) ;
147 tRes
.tested("collapseToCurrentArray()", bResult
);
151 * Test clears contents of spreadsheet, collapses cursor to current range,
152 * checks size of cursor range, fills a cell that is close to
153 * cursor range, collapses cursor to current range, checks size of cursor
154 * range again and restores original size. <p>
155 * Has <b> OK </b> status if after clearing of content and collapsing cursor
156 * range size remains 4 x 4, if after filling of cell and collapsing cursor
157 * range extends by one in both dimensions and no exceptions were thrown.<p>
159 public void _collapseToCurrentRegion(){
160 boolean bResult
= true;
161 int width
= 4, height
= 4;
162 int leftCol
= -1, topRow
= -1;
164 XSpreadsheet oSheet
= oObj
.getSpreadsheet();
165 ((XSheetOperation
) UnoRuntime
.queryInterface(
166 XSheetOperation
.class, oSheet
) ).clearContents(65535);
167 oObj
.collapseToCurrentRegion();
168 int cols
= ((XColumnRowRange
)
169 UnoRuntime
.queryInterface(
170 XColumnRowRange
.class, oObj
) ).getColumns().getCount();
171 int rows
= ((XColumnRowRange
)
172 UnoRuntime
.queryInterface(
173 XColumnRowRange
.class, oObj
) ).getRows().getCount();
175 if (cols
!= width
|| rows
!= height
) {
177 log
.println("After collapseToCurrentRegion()"
178 + " call Region must have size " + width
+ "x" + height
179 + " but it is " + cols
+ "x" + rows
);
182 // if previous test was successful try more complicated case
184 if (leftCol
!= -1 && topRow
!= -1) {
186 oSheet
.getCellByPosition(
187 leftCol
+ width
, topRow
+ height
).setValue(1);
188 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
189 log
.print("Can't get cell by position:");
190 e
.printStackTrace(log
);
194 oObj
.collapseToCurrentRegion() ;
197 cols
= ((XColumnRowRange
)
198 UnoRuntime
.queryInterface(
199 XColumnRowRange
.class, oObj
)).getColumns().getCount();
200 rows
= ((XColumnRowRange
)
201 UnoRuntime
.queryInterface(
202 XColumnRowRange
.class, oObj
)).getRows().getCount();
204 if (cols
== width
+ 1 && rows
== height
+ 1) {
208 log
.println("After collapseToCurrentRegion() call [2]"
209 + " region must have size " + (width
+1) + "x"
210 + (height
+ 1) + " but it is " + cols
+ "x" + rows
);
215 tRes
.tested("collapseToCurrentRegion()", bResult
);
217 // restore original size
218 oObj
.collapseToSize(width
, height
);
222 * Test merges a cells of range that has a greater size, collapses cursor to
223 * merged area, checks size of cursor range and restores original size
224 * of cursor range. <p>
225 * Has <b> OK </b> status if after merging of cells and collapsing cursor
226 * range extends by one in both dimensions and no exceptions were thrown.<p>
228 public void _collapseToMergedArea(){
229 int width
= 1, height
= 1 ;
230 int leftCol
= 0, topRow
= 0 ;
232 boolean bResult
= true ;
234 log
.println("DB: Starting collapseToMergedArea() method test ...") ;
235 XSpreadsheet oSheet
= oObj
.getSpreadsheet() ;
236 log
.println ("DB: got Spreadsheet.") ;
238 XCellRange newRange
= null;
240 newRange
= oSheet
.getCellRangeByPosition (
241 leftCol
+ width
- 1, topRow
+ height
- 1,
242 leftCol
+ width
, topRow
+ height
);
243 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
244 log
.println("Can't get cell range by position");
245 e
.printStackTrace(log
);
249 XMergeable mergeRange
= (XMergeable
)
250 UnoRuntime
.queryInterface (XMergeable
.class, newRange
);
251 if (mergeRange
== null) {
252 log
.println("DB: newRange doesn't implement XMergeable interface");
254 log
.println("DB: XMergeable interface successfully queried.");
257 mergeRange
.merge(true);
258 log
.println("DB: Successfully merged.") ;
260 oObj
.collapseToMergedArea() ;
261 log
.println("DB: Successfully collapseToMergedArea() method called");
263 // unmerge area to restore SpreadSheet
264 mergeRange
.merge(false);
265 log
.println("DB: Successfully unmerged.") ;
268 int cols
= ((XColumnRowRange
)
269 UnoRuntime
.queryInterface(
270 XColumnRowRange
.class, oObj
) ).getColumns().getCount();
271 int rows
= ((XColumnRowRange
)
272 UnoRuntime
.queryInterface(
273 XColumnRowRange
.class, oObj
) ).getRows().getCount();
274 log
.println("DB: Column and row numbers successfully get") ;
276 if (cols
== width
+ 1 && rows
== height
+ 3) {
281 "After collapseToMergedArea() call region must have size "
282 + (width
+ 1) + "x" + (height
+ 1) + " but it is " + cols
286 tRes
.tested("collapseToMergedArea()", bResult
) ;
288 // restore original size
289 oObj
.collapseToSize(width
, height
);
293 * Test collapses cursor to the new size, checks size
294 * of cursor range and restores original size of cursor range. <p>
295 * Has <b> OK </b> status if after collapsing cursor
296 * range extends by three in both dimensions. <p>
298 public void _collapseToSize(){
299 boolean bResult
= false;
300 int width
= 1, height
= 1;
302 // collapseToSize() method test
303 oObj
.collapseToSize (width
+ 3, height
+ 3);
306 int cols
= ((XColumnRowRange
)
307 UnoRuntime
.queryInterface(
308 XColumnRowRange
.class, oObj
) ).getColumns().getCount();
309 int rows
= ((XColumnRowRange
)
310 UnoRuntime
.queryInterface(
311 XColumnRowRange
.class, oObj
) ).getRows().getCount();
313 if (cols
== width
+ 3 && rows
== height
+ 3) {
317 log
.println( "After collapseToSize() call region must have size "
318 + (width
+ 3) + "x" + (height
+ 3) + " but it is "
322 tRes
.tested("collapseToSize()", bResult
) ;
324 // restore original size
325 oObj
.collapseToSize(width
, height
) ;
329 * Test expands cursor to entire columns, checks size
330 * of cursor range and restores original size of cursor range. <p>
331 * Has <b> OK </b> status if after expanding cursor
332 * range extends to all rows in the columns (number of rows is greater than
333 * 32000 and number of columns remains the same). <p>
335 public void _expandToEntireColumns(){
336 boolean bResult
= false;
337 int width
= 1, height
= 1 ;
339 // expandToEntireColumns() method test
340 oObj
.expandToEntireColumns () ;
343 int cols
= ((XColumnRowRange
)
344 UnoRuntime
.queryInterface(
345 XColumnRowRange
.class, oObj
) ).getColumns().getCount();
346 int rows
= ((XColumnRowRange
)
347 UnoRuntime
.queryInterface(
348 XColumnRowRange
.class, oObj
) ).getRows().getCount();
350 if (cols
== width
&& rows
>= 32000) {
355 "After expandToEntireColumns() call region must have size "+
356 width
+ "x(>=32000) but it is " + cols
+ "x" + rows
);
359 tRes
.tested("expandToEntireColumns()", bResult
) ;
361 // restore original size
362 oObj
.collapseToSize(width
, height
) ;
366 * Test expands cursor to entire rows, checks size
367 * of cursor range and restores original size of cursor range. <p>
368 * Has <b> OK </b> status if after expanding cursor
369 * range extends to all columns in the rows (number of columns is greater
370 * than 256 and number of rows remains the same). <p>
372 public void _expandToEntireRows(){
373 boolean bResult
= false;
374 int width
= 1, height
= 1 ;
376 // expandToEntireRows() method test
377 oObj
.expandToEntireRows () ;
380 int cols
= ((XColumnRowRange
)
381 UnoRuntime
.queryInterface(
382 XColumnRowRange
.class, oObj
) ).getColumns().getCount();
383 int rows
= ((XColumnRowRange
)
384 UnoRuntime
.queryInterface(
385 XColumnRowRange
.class, oObj
) ).getRows().getCount();
387 if (cols
>= 256 && rows
== height
) {
391 log
.println("After expandToEntireRows() call region " +
392 "must have size (>=256)x" + height
+ " but it is " +
396 tRes
.tested("expandToEntireRows()", bResult
) ;
398 // restore original size
399 oObj
.collapseToSize(width
, height
) ;
402 } // EOC _XSheetCellCursor