1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.sheet;
25 
26 import lib.MultiMethodTest;
27 
28 import com.sun.star.sheet.CellFlags;
29 import com.sun.star.sheet.XArrayFormulaRange;
30 import com.sun.star.sheet.XCellRangeAddressable;
31 import com.sun.star.sheet.XSheetCellCursor;
32 import com.sun.star.sheet.XSheetOperation;
33 import com.sun.star.sheet.XSpreadsheet;
34 import com.sun.star.table.CellRangeAddress;
35 import com.sun.star.table.XCellRange;
36 import com.sun.star.table.XColumnRowRange;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.util.XMergeable;
39 
40 /**
41 * Testing <code>com.sun.star.sheet.XSheetCellCursor</code>
42 * interface methods :
43 * <ul>
44 *  <li><code> collapseToCurrentRegion()</code></li>
45 *  <li><code> collapseToCurrentArray()</code></li>
46 *  <li><code> collapseToMergedArea()</code></li>
47 *  <li><code> expandToEntireColumns()</code></li>
48 *  <li><code> expandToEntireRows()</code></li>
49 *  <li><code> collapseToSize()</code></li>
50 * </ul> <p>
51 * Component must also implement the following interfaces :
52 * <ul>
53 *  <li> <code> com.sun.star.sheet.XCellRangeAddressable </code> :
54 *  to get range address </li>
55 * <ul> <p>
56 * Range of cursor must be of size 4 x 4. <p>
57 * @see com.sun.star.sheet.XSheetCellCursor
58 */
59 public class _XSheetCellCursor extends MultiMethodTest {
60 
61     public XSheetCellCursor oObj = null;
62 
63     /**
64     * Test creates the array formula, assigns this array to another array,
65     * collapses cursor into one cell, applies method, checks the size of the
66     * result range, erases array formula, checks that array formula has been
67     * cleared. <p>
68     * Has <b>OK</b> status if no exceptions were thrown, if size of the result
69     * range is equal to size of the range where the array formula was set and
70     * if array formula was successfully cleared. <p>
71     */
_collapseToCurrentArray()72     public void _collapseToCurrentArray() {
73         boolean bResult = false;
74 
75         XCellRangeAddressable crAddr = (XCellRangeAddressable)
76             UnoRuntime.queryInterface(XCellRangeAddressable.class, oObj);
77         CellRangeAddress addr = crAddr.getRangeAddress() ;
78         int leftCol = addr.StartColumn ;
79         int topRow = addr.StartRow ;
80         int width = addr.EndColumn - addr.StartColumn + 1 ;
81         int height = addr.EndRow - addr.StartRow + 1 ;
82 
83         log.println( "Object area is ((" + leftCol + "," + topRow + "),(" +
84             (leftCol + width - 1) + "," + (topRow + height - 1) + ")" );
85 
86         XCellRange new_range = null;
87         try {
88             // first we need to create an array formula
89             new_range =
90                 oObj.getCellRangeByPosition(0, 0, 0, height - 1);
91         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
92             log.print("Get cell range by position failed: ");
93             e.printStackTrace(log);
94             tRes.tested("collapseToCurrentArray()", false);
95         }
96 
97         log.println("DB: Successfully new range created");
98         XArrayFormulaRange arrFormulaRange = (XArrayFormulaRange)
99             UnoRuntime.queryInterface (XArrayFormulaRange.class, new_range);
100         // write a simple formula (this array assigns another array)
101         arrFormulaRange.setArrayFormula("A1:A" + height) ;
102 
103         // collapse cursor into one cell and then try to apply the method
104         oObj.collapseToSize (1, 1) ;
105         oObj.collapseToCurrentArray() ;
106 
107         // check the size of result range
108         int cols = ( (XColumnRowRange)UnoRuntime.queryInterface(
109                   XColumnRowRange.class, oObj) ).getColumns().getCount();
110         int rows = ( (XColumnRowRange)UnoRuntime.queryInterface(
111                   XColumnRowRange.class, oObj) ).getRows().getCount();
112 
113         if (cols == 1 && rows == height) {
114             bResult = true;
115         } else {
116             bResult = false;
117             log.println("The size of cell range must be 1x" + height +
118                 ", but after method call it was " + cols + "x" + rows);
119         }
120 
121         // erase array formula
122         arrFormulaRange.setArrayFormula("");
123 
124         // check if array formula has been cleared with last statement
125         try {
126             // if array formula isn't cleared exception is thrown
127             new_range.getCellByPosition(0,0).setValue(111) ;
128         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
129             bResult = false ;
130             log.println(
131                 "Array formula hasn't been cleared with setArrayFormula(\"\")");
132             XSheetOperation clearRange = (XSheetOperation)
133                 UnoRuntime.queryInterface (XSheetOperation.class, new_range);
134             int allFlags =
135                 CellFlags.ANNOTATION | CellFlags.DATETIME | CellFlags.EDITATTR;
136             allFlags = allFlags
137                 | CellFlags.HARDATTR | CellFlags.OBJECTS | CellFlags.STRING;
138             allFlags = allFlags
139                 | CellFlags.VALUE | CellFlags.FORMULA | CellFlags.STYLES;
140             clearRange.clearContents(allFlags) ;
141         }
142 
143         tRes.tested("collapseToCurrentArray()", bResult );
144     }
145 
146     /**
147     * Test clears contents of spreadsheet, collapses cursor to current range,
148     * checks size of cursor range, fills a cell that is close to
149     * cursor range, collapses cursor to current range, checks size of cursor
150     * range again and restores original size. <p>
151     * Has <b> OK </b> status if after clearing of content and collapsing cursor
152     * range size remains 4 x 4, if after filling of cell and collapsing cursor
153     * range extends by one in both dimensions and no exceptions were thrown.<p>
154     */
_collapseToCurrentRegion()155     public void _collapseToCurrentRegion(){
156         boolean bResult = true;
157         int width = 4, height = 4;
158         int leftCol = -1, topRow = -1;
159 
160         XSpreadsheet oSheet = oObj.getSpreadsheet();
161         ((XSheetOperation) UnoRuntime.queryInterface(
162             XSheetOperation.class, oSheet) ).clearContents(65535);
163         oObj.collapseToCurrentRegion();
164         int cols = ((XColumnRowRange)
165             UnoRuntime.queryInterface(
166                 XColumnRowRange.class, oObj) ).getColumns().getCount();
167         int rows = ((XColumnRowRange)
168             UnoRuntime.queryInterface(
169                 XColumnRowRange.class, oObj) ).getRows().getCount();
170 
171         if (cols != width || rows != height) {
172             bResult = false ;
173             log.println("After collapseToCurrentRegion()"
174                  + " call Region must have size " + width + "x" + height
175                  + " but it is " + cols + "x" + rows);
176         }
177 
178         // if previous test was successful try more complicated case
179         if (bResult) {
180             if (leftCol != -1 && topRow != -1) {
181                 try {
182                     oSheet.getCellByPosition(
183                         leftCol + width, topRow + height).setValue(1);
184                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
185                     log.print("Can't get cell by position:");
186                     e.printStackTrace(log);
187                     bResult = false;
188                 }
189 
190                 oObj.collapseToCurrentRegion() ;
191 
192                 // checking results
193                 cols = ((XColumnRowRange)
194                     UnoRuntime.queryInterface(
195                         XColumnRowRange.class, oObj)).getColumns().getCount();
196                 rows = ((XColumnRowRange)
197                     UnoRuntime.queryInterface(
198                         XColumnRowRange.class, oObj)).getRows().getCount();
199 
200                 if (cols == width + 1 && rows == height + 1) {
201                     bResult &= true;
202                 } else {
203                     bResult = false;
204                     log.println("After collapseToCurrentRegion() call [2]"
205                          + " region must have size " + (width+1) + "x"
206                          + (height + 1) + " but it is " + cols + "x" + rows );
207                 }
208             }
209         }
210 
211         tRes.tested("collapseToCurrentRegion()", bResult);
212 
213         // restore original size
214         oObj.collapseToSize(width, height);
215     }
216 
217     /**
218     * Test merges a cells of range that has a greater size, collapses cursor to
219     * merged area, checks size of cursor range and restores original size
220     * of cursor range. <p>
221     * Has <b> OK </b> status if after merging of cells and collapsing cursor
222     * range extends by one in both dimensions and no exceptions were thrown.<p>
223     */
_collapseToMergedArea()224     public void _collapseToMergedArea(){
225         int width = 1, height = 1 ;
226         int leftCol = 0, topRow = 0 ;
227 
228         boolean bResult = true ;
229 
230         log.println("DB: Starting collapseToMergedArea() method test ...") ;
231         XSpreadsheet oSheet = oObj.getSpreadsheet() ;
232         log.println ("DB: got Spreadsheet.") ;
233 
234         XCellRange newRange = null;
235         try {
236             newRange = oSheet.getCellRangeByPosition (
237                 leftCol + width - 1, topRow + height - 1,
238                 leftCol + width, topRow + height );
239         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
240             log.println("Can't get cell range by position");
241             e.printStackTrace(log);
242             bResult = false;
243         }
244 
245         XMergeable mergeRange = (XMergeable)
246             UnoRuntime.queryInterface (XMergeable.class, newRange);
247         if (mergeRange == null) {
248             log.println("DB: newRange doesn't implement XMergeable interface");
249         } else {
250             log.println("DB: XMergeable interface successfully queried.");
251         }
252 
253         mergeRange.merge(true);
254         log.println("DB: Successfuly merged.") ;
255 
256         oObj.collapseToMergedArea() ;
257         log.println("DB: Succesfully collapseToMergedArea() method called");
258 
259         // unmerge area to restore SpreadSheet
260         mergeRange.merge(false);
261         log.println("DB: Successfully unmerged.") ;
262 
263         // checking results
264         int cols = ((XColumnRowRange)
265             UnoRuntime.queryInterface(
266                 XColumnRowRange.class, oObj) ).getColumns().getCount();
267         int rows = ((XColumnRowRange)
268             UnoRuntime.queryInterface(
269                 XColumnRowRange.class, oObj) ).getRows().getCount();
270         log.println("DB: Column and row numbers succesfully get") ;
271 
272         if (cols == width + 1  && rows == height + 3) {
273             bResult &= true;
274         } else {
275             bResult = false;
276             log.println(
277                 "After collapseToMergedArea() call region must have size "
278                 + (width + 1) + "x" + (height + 1) + " but it is " + cols
279                 + "x" + rows );
280         }
281 
282         tRes.tested("collapseToMergedArea()", bResult) ;
283 
284         // restore original size
285         oObj.collapseToSize(width, height);
286     }
287 
288     /**
289     * Test collapses cursor to the new size, checks size
290     * of cursor range and restores original size of cursor range. <p>
291     * Has <b> OK </b> status if after collapsing cursor
292     * range extends by three in both dimensions. <p>
293     */
_collapseToSize()294     public void _collapseToSize(){
295         boolean bResult = false;
296         int width = 1, height = 1;
297 
298         // collapseToSize() method test
299         oObj.collapseToSize (width + 3, height + 3);
300 
301         // checking results
302         int cols = ((XColumnRowRange)
303             UnoRuntime.queryInterface(
304                 XColumnRowRange.class, oObj) ).getColumns().getCount();
305         int rows = ((XColumnRowRange)
306             UnoRuntime.queryInterface(
307                 XColumnRowRange.class, oObj) ).getRows().getCount();
308 
309         if (cols == width + 3  && rows == height + 3) {
310             bResult = true ;
311         } else {
312             bResult = false ;
313             log.println( "After collapseToSize() call region must have size "
314                 + (width + 3) + "x" + (height + 3) + " but it is "
315                 + cols + "x" +rows);
316         }
317 
318         tRes.tested("collapseToSize()", bResult) ;
319 
320         // restore original size
321         oObj.collapseToSize(width, height) ;
322     }
323 
324     /**
325     * Test expands cursor to entire columns, checks size
326     * of cursor range and restores original size of cursor range. <p>
327     * Has <b> OK </b> status if after expanding cursor
328     * range extends to all rows in the columns (number of rows is greater than
329     * 32000 and number of columns remains the same). <p>
330     */
_expandToEntireColumns()331     public void _expandToEntireColumns(){
332         boolean bResult = false;
333         int width = 1, height = 1 ;
334 
335         // expandToEntireColumns() method test
336         oObj.expandToEntireColumns () ;
337 
338         // checking results
339         int cols = ((XColumnRowRange)
340             UnoRuntime.queryInterface(
341                 XColumnRowRange.class, oObj) ).getColumns().getCount();
342         int rows = ((XColumnRowRange)
343             UnoRuntime.queryInterface(
344                 XColumnRowRange.class, oObj) ).getRows().getCount();
345 
346         if (cols == width && rows >= 32000) {
347             bResult = true ;
348         } else {
349             bResult = false ;
350             log.println(
351                 "After expandToEntireColumns() call region must have size "+
352                 width + "x(>=32000) but it is " + cols + "x" + rows);
353         }
354 
355         tRes.tested("expandToEntireColumns()", bResult) ;
356 
357         // restore original size
358         oObj.collapseToSize(width, height) ;
359     }
360 
361     /**
362     * Test expands cursor to entire rows, checks size
363     * of cursor range and restores original size of cursor range. <p>
364     * Has <b> OK </b> status if after expanding cursor
365     * range extends to all columns in the rows (number of columns is greater
366     * than 256 and number of rows remains the same). <p>
367     */
_expandToEntireRows()368     public void _expandToEntireRows(){
369         boolean bResult = false;
370         int width = 1, height = 1 ;
371 
372         // expandToEntireRows() method test
373         oObj.expandToEntireRows () ;
374 
375         // checking results
376         int cols = ((XColumnRowRange)
377             UnoRuntime.queryInterface(
378                 XColumnRowRange.class, oObj) ).getColumns().getCount();
379         int rows = ((XColumnRowRange)
380             UnoRuntime.queryInterface(
381                 XColumnRowRange.class, oObj) ).getRows().getCount();
382 
383         if (cols >= 256 && rows == height) {
384             bResult = true;
385         } else {
386             bResult = false ;
387             log.println("After expandToEntireRows() call region " +
388                 "must have size (>=256)x" + height + " but it is " +
389                 cols + "x" + rows );
390         }
391 
392         tRes.tested("expandToEntireRows()", bResult) ;
393 
394         // restore original size
395         oObj.collapseToSize(width, height) ;
396     }
397 
398 } // EOC _XSheetCellCursor
399 
400