1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 package ifc.sheet;
29 
30 import lib.MultiMethodTest;
31 
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;
43 
44 /**
45 * Testing <code>com.sun.star.sheet.XSheetCellCursor</code>
46 * interface methods :
47 * <ul>
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>
54 * </ul> <p>
55 * Component must also implement the following interfaces :
56 * <ul>
57 *  <li> <code> com.sun.star.sheet.XCellRangeAddressable </code> :
58 *  to get range address </li>
59 * <ul> <p>
60 * Range of cursor must be of size 4 x 4. <p>
61 * @see com.sun.star.sheet.XSheetCellCursor
62 */
63 public class _XSheetCellCursor extends MultiMethodTest {
64 
65     public XSheetCellCursor oObj = null;
66 
67     /**
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
71     * cleared. <p>
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>
75     */
76     public void _collapseToCurrentArray() {
77         boolean bResult = false;
78 
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 ;
86 
87         log.println( "Object area is ((" + leftCol + "," + topRow + "),(" +
88             (leftCol + width - 1) + "," + (topRow + height - 1) + ")" );
89 
90         XCellRange new_range = null;
91         try {
92             // first we need to create an array formula
93             new_range =
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);
99         }
100 
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) ;
106 
107         // collapse cursor into one cell and then try to apply the method
108         oObj.collapseToSize (1, 1) ;
109         oObj.collapseToCurrentArray() ;
110 
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();
116 
117         if (cols == 1 && rows == height) {
118             bResult = true;
119         } else {
120             bResult = false;
121             log.println("The size of cell range must be 1x" + height +
122                 ", but after method call it was " + cols + "x" + rows);
123         }
124 
125         // erase array formula
126         arrFormulaRange.setArrayFormula("");
127 
128         // check if array formula has been cleared with last statement
129         try {
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) {
133             bResult = false ;
134             log.println(
135                 "Array formula hasn't been cleared with setArrayFormula(\"\")");
136             XSheetOperation clearRange = (XSheetOperation)
137                 UnoRuntime.queryInterface (XSheetOperation.class, new_range);
138             int allFlags =
139                 CellFlags.ANNOTATION | CellFlags.DATETIME | CellFlags.EDITATTR;
140             allFlags = allFlags
141                 | CellFlags.HARDATTR | CellFlags.OBJECTS | CellFlags.STRING;
142             allFlags = allFlags
143                 | CellFlags.VALUE | CellFlags.FORMULA | CellFlags.STYLES;
144             clearRange.clearContents(allFlags) ;
145         }
146 
147         tRes.tested("collapseToCurrentArray()", bResult );
148     }
149 
150     /**
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>
158     */
159     public void _collapseToCurrentRegion(){
160         boolean bResult = true;
161         int width = 4, height = 4;
162         int leftCol = -1, topRow = -1;
163 
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();
174 
175         if (cols != width || rows != height) {
176             bResult = false ;
177             log.println("After collapseToCurrentRegion()"
178                  + " call Region must have size " + width + "x" + height
179                  + " but it is " + cols + "x" + rows);
180         }
181 
182         // if previous test was successful try more complicated case
183         if (bResult) {
184             if (leftCol != -1 && topRow != -1) {
185                 try {
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);
191                     bResult = false;
192                 }
193 
194                 oObj.collapseToCurrentRegion() ;
195 
196                 // checking results
197                 cols = ((XColumnRowRange)
198                     UnoRuntime.queryInterface(
199                         XColumnRowRange.class, oObj)).getColumns().getCount();
200                 rows = ((XColumnRowRange)
201                     UnoRuntime.queryInterface(
202                         XColumnRowRange.class, oObj)).getRows().getCount();
203 
204                 if (cols == width + 1 && rows == height + 1) {
205                     bResult &= true;
206                 } else {
207                     bResult = false;
208                     log.println("After collapseToCurrentRegion() call [2]"
209                          + " region must have size " + (width+1) + "x"
210                          + (height + 1) + " but it is " + cols + "x" + rows );
211                 }
212             }
213         }
214 
215         tRes.tested("collapseToCurrentRegion()", bResult);
216 
217         // restore original size
218         oObj.collapseToSize(width, height);
219     }
220 
221     /**
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>
227     */
228     public void _collapseToMergedArea(){
229         int width = 1, height = 1 ;
230         int leftCol = 0, topRow = 0 ;
231 
232         boolean bResult = true ;
233 
234         log.println("DB: Starting collapseToMergedArea() method test ...") ;
235         XSpreadsheet oSheet = oObj.getSpreadsheet() ;
236         log.println ("DB: got Spreadsheet.") ;
237 
238         XCellRange newRange = null;
239         try {
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);
246             bResult = false;
247         }
248 
249         XMergeable mergeRange = (XMergeable)
250             UnoRuntime.queryInterface (XMergeable.class, newRange);
251         if (mergeRange == null) {
252             log.println("DB: newRange doesn't implement XMergeable interface");
253         } else {
254             log.println("DB: XMergeable interface successfully queried.");
255         }
256 
257         mergeRange.merge(true);
258         log.println("DB: Successfuly merged.") ;
259 
260         oObj.collapseToMergedArea() ;
261         log.println("DB: Succesfully collapseToMergedArea() method called");
262 
263         // unmerge area to restore SpreadSheet
264         mergeRange.merge(false);
265         log.println("DB: Successfully unmerged.") ;
266 
267         // checking results
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 succesfully get") ;
275 
276         if (cols == width + 1  && rows == height + 3) {
277             bResult &= true;
278         } else {
279             bResult = false;
280             log.println(
281                 "After collapseToMergedArea() call region must have size "
282                 + (width + 1) + "x" + (height + 1) + " but it is " + cols
283                 + "x" + rows );
284         }
285 
286         tRes.tested("collapseToMergedArea()", bResult) ;
287 
288         // restore original size
289         oObj.collapseToSize(width, height);
290     }
291 
292     /**
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>
297     */
298     public void _collapseToSize(){
299         boolean bResult = false;
300         int width = 1, height = 1;
301 
302         // collapseToSize() method test
303         oObj.collapseToSize (width + 3, height + 3);
304 
305         // checking results
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();
312 
313         if (cols == width + 3  && rows == height + 3) {
314             bResult = true ;
315         } else {
316             bResult = false ;
317             log.println( "After collapseToSize() call region must have size "
318                 + (width + 3) + "x" + (height + 3) + " but it is "
319                 + cols + "x" +rows);
320         }
321 
322         tRes.tested("collapseToSize()", bResult) ;
323 
324         // restore original size
325         oObj.collapseToSize(width, height) ;
326     }
327 
328     /**
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>
334     */
335     public void _expandToEntireColumns(){
336         boolean bResult = false;
337         int width = 1, height = 1 ;
338 
339         // expandToEntireColumns() method test
340         oObj.expandToEntireColumns () ;
341 
342         // checking results
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();
349 
350         if (cols == width && rows >= 32000) {
351             bResult = true ;
352         } else {
353             bResult = false ;
354             log.println(
355                 "After expandToEntireColumns() call region must have size "+
356                 width + "x(>=32000) but it is " + cols + "x" + rows);
357         }
358 
359         tRes.tested("expandToEntireColumns()", bResult) ;
360 
361         // restore original size
362         oObj.collapseToSize(width, height) ;
363     }
364 
365     /**
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>
371     */
372     public void _expandToEntireRows(){
373         boolean bResult = false;
374         int width = 1, height = 1 ;
375 
376         // expandToEntireRows() method test
377         oObj.expandToEntireRows () ;
378 
379         // checking results
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();
386 
387         if (cols >= 256 && rows == height) {
388             bResult = true;
389         } else {
390             bResult = false ;
391             log.println("After expandToEntireRows() call region " +
392                 "must have size (>=256)x" + height + " but it is " +
393                 cols + "x" + rows );
394         }
395 
396         tRes.tested("expandToEntireRows()", bResult) ;
397 
398         // restore original size
399         oObj.collapseToSize(width, height) ;
400     }
401 
402 } // EOC _XSheetCellCursor
403 
404