summaryrefslogtreecommitdiff
path: root/qadevOOo/tests/java/ifc/sheet/_XSheetCellCursor.java
blob: ec484f9bf0d643f076eba277c7730f5663326a74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

package ifc.sheet;

import lib.MultiMethodTest;

import com.sun.star.sheet.CellFlags;
import com.sun.star.sheet.XArrayFormulaRange;
import com.sun.star.sheet.XCellRangeAddressable;
import com.sun.star.sheet.XSheetCellCursor;
import com.sun.star.sheet.XSheetOperation;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.table.CellRangeAddress;
import com.sun.star.table.XCellRange;
import com.sun.star.table.XColumnRowRange;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.util.XMergeable;

/**
* Testing <code>com.sun.star.sheet.XSheetCellCursor</code>
* interface methods :
* <ul>
*  <li><code> collapseToCurrentRegion()</code></li>
*  <li><code> collapseToCurrentArray()</code></li>
*  <li><code> collapseToMergedArea()</code></li>
*  <li><code> expandToEntireColumns()</code></li>
*  <li><code> expandToEntireRows()</code></li>
*  <li><code> collapseToSize()</code></li>
* </ul> <p>
* Component must also implement the following interfaces :
* <ul>
*  <li> <code> com.sun.star.sheet.XCellRangeAddressable </code> :
*  to get range address </li>
* <ul> <p>
* Range of cursor must be of size 4 x 4. <p>
* @see com.sun.star.sheet.XSheetCellCursor
*/
public class _XSheetCellCursor extends MultiMethodTest {

    public XSheetCellCursor oObj = null;

    /**
    * Test creates the array formula, assigns this array to another array,
    * collapses cursor into one cell, applies method, checks the size of the
    * result range, erases array formula, checks that array formula has been
    * cleared. <p>
    * Has <b>OK</b> status if no exceptions were thrown, if size of the result
    * range is equal to size of the range where the array formula was set and
    * if array formula was successfully cleared. <p>
    */
    public void _collapseToCurrentArray() {
        boolean bResult = false;

        XCellRangeAddressable crAddr = (XCellRangeAddressable)
            UnoRuntime.queryInterface(XCellRangeAddressable.class, oObj);
        CellRangeAddress addr = crAddr.getRangeAddress() ;
        int leftCol = addr.StartColumn ;
        int topRow = addr.StartRow ;
        int width = addr.EndColumn - addr.StartColumn + 1 ;
        int height = addr.EndRow - addr.StartRow + 1 ;

        log.println( "Object area is ((" + leftCol + "," + topRow + "),(" +
            (leftCol + width - 1) + "," + (topRow + height - 1) + ")" );

        XCellRange new_range = null;
        try {
            // first we need to create an array formula
            new_range =
                oObj.getCellRangeByPosition(0, 0, 0, height - 1);
        } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
            log.print("Get cell range by position failed: ");
            e.printStackTrace(log);
            tRes.tested("collapseToCurrentArray()", false);
        }

        log.println("DB: Successfully new range created");
        XArrayFormulaRange arrFormulaRange = (XArrayFormulaRange)
            UnoRuntime.queryInterface (XArrayFormulaRange.class, new_range);
        // write a simple formula (this array assigns another array)
        arrFormulaRange.setArrayFormula("A1:A" + height) ;

        // collapse cursor into one cell and then try to apply the method
        oObj.collapseToSize (1, 1) ;
        oObj.collapseToCurrentArray() ;

        // check the size of result range
        int cols = ( (XColumnRowRange)UnoRuntime.queryInterface(
                  XColumnRowRange.class, oObj) ).getColumns().getCount();
        int rows = ( (XColumnRowRange)UnoRuntime.queryInterface(
                  XColumnRowRange.class, oObj) ).getRows().getCount();

        if (cols == 1 && rows == height) {
            bResult = true;
        } else {
            bResult = false;
            log.println("The size of cell range must be 1x" + height +
                ", but after method call it was " + cols + "x" + rows);
        }

        // erase array formula
        arrFormulaRange.setArrayFormula("");

        // check if array formula has been cleared with last statement
        try {
            // if array formula isn't cleared exception is thrown
            new_range.getCellByPosition(0,0).setValue(111) ;
        } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
            bResult = false ;
            log.println(
                "Array formula hasn't been cleared with setArrayFormula(\"\")");
            XSheetOperation clearRange = (XSheetOperation)
                UnoRuntime.queryInterface (XSheetOperation.class, new_range);
            int allFlags =
                CellFlags.ANNOTATION | CellFlags.DATETIME | CellFlags.EDITATTR;
            allFlags = allFlags
                | CellFlags.HARDATTR | CellFlags.OBJECTS | CellFlags.STRING;
            allFlags = allFlags
                | CellFlags.VALUE | CellFlags.FORMULA | CellFlags.STYLES;
            clearRange.clearContents(allFlags) ;
        }

        tRes.tested("collapseToCurrentArray()", bResult );
    }

    /**
    * Test clears contents of spreadsheet, collapses cursor to current range,
    * checks size of cursor range, fills a cell that is close to
    * cursor range, collapses cursor to current range, checks size of cursor
    * range again and restores original size. <p>
    * Has <b> OK </b> status if after clearing of content and collapsing cursor
    * range size remains 4 x 4, if after filling of cell and collapsing cursor
    * range extends by one in both dimensions and no exceptions were thrown.<p>
    */
    public void _collapseToCurrentRegion(){
        boolean bResult = true;
        int width = 4, height = 4;
        int leftCol = -1, topRow = -1;

        XSpreadsheet oSheet = oObj.getSpreadsheet();
        ((XSheetOperation) UnoRuntime.queryInterface(
            XSheetOperation.class, oSheet) ).clearContents(65535);
        oObj.collapseToCurrentRegion();
        int cols = ((XColumnRowRange)
            UnoRuntime.queryInterface(
                XColumnRowRange.class, oObj) ).getColumns().getCount();
        int rows = ((XColumnRowRange)
            UnoRuntime.queryInterface(
                XColumnRowRange.class, oObj) ).getRows().getCount();

        if (cols != width || rows != height) {
            bResult = false ;
            log.println("After collapseToCurrentRegion()"
                 + " call Region must have size " + width + "x" + height
                 + " but it is " + cols + "x" + rows);
        }

        // if previous test was successful try more complicated case
        if (bResult) {
            if (leftCol != -1 && topRow != -1) {
                try {
                    oSheet.getCellByPosition(
                        leftCol + width, topRow + height).setValue(1);
                } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
                    log.print("Can't get cell by position:");
                    e.printStackTrace(log);
                    bResult = false;
                }

                oObj.collapseToCurrentRegion() ;

                // checking results
                cols = ((XColumnRowRange)
                    UnoRuntime.queryInterface(
                        XColumnRowRange.class, oObj)).getColumns().getCount();
                rows = ((XColumnRowRange)
                    UnoRuntime.queryInterface(
                        XColumnRowRange.class, oObj)).getRows().getCount();

                if (cols == width + 1 && rows == height + 1) {
                    bResult &= true;
                } else {
                    bResult = false;
                    log.println("After collapseToCurrentRegion() call [2]"
                         + " region must have size " + (width+1) + "x"
                         + (height + 1) + " but it is " + cols + "x" + rows );
                }
            }
        }

        tRes.tested("collapseToCurrentRegion()", bResult);

        // restore original size
        oObj.collapseToSize(width, height);
    }

    /**
    * Test merges a cells of range that has a greater size, collapses cursor to
    * merged area, checks size of cursor range and restores original size
    * of cursor range. <p>
    * Has <b> OK </b> status if after merging of cells and collapsing cursor
    * range extends by one in both dimensions and no exceptions were thrown.<p>
    */
    public void _collapseToMergedArea(){
        int width = 1, height = 1 ;
        int leftCol = 0, topRow = 0 ;

        boolean bResult = true ;

        log.println("DB: Starting collapseToMergedArea() method test ...") ;
        XSpreadsheet oSheet = oObj.getSpreadsheet() ;
        log.println ("DB: got Spreadsheet.") ;

        XCellRange newRange = null;
        try {
            newRange = oSheet.getCellRangeByPosition (
                leftCol + width - 1, topRow + height - 1,
                leftCol + width, topRow + height );
        } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
            log.println("Can't get cell range by position");
            e.printStackTrace(log);
            bResult = false;
        }

        XMergeable mergeRange = (XMergeable)
            UnoRuntime.queryInterface (XMergeable.class, newRange);
        if (mergeRange == null) {
            log.println("DB: newRange doesn't implement XMergeable interface");
        } else {
            log.println("DB: XMergeable interface successfully queried.");
        }

        mergeRange.merge(true);
        log.println("DB: Successfuly merged.") ;

        oObj.collapseToMergedArea() ;
        log.println("DB: Succesfully collapseToMergedArea() method called");

        // unmerge area to restore SpreadSheet
        mergeRange.merge(false);
        log.println("DB: Successfully unmerged.") ;

        // checking results
        int cols = ((XColumnRowRange)
            UnoRuntime.queryInterface(
                XColumnRowRange.class, oObj) ).getColumns().getCount();
        int rows = ((XColumnRowRange)
            UnoRuntime.queryInterface(
                XColumnRowRange.class, oObj) ).getRows().getCount();
        log.println("DB: Column and row numbers succesfully get") ;

        if (cols == width + 1  && rows == height + 3) {
            bResult &= true;
        } else {
            bResult = false;
            log.println(
                "After collapseToMergedArea() call region must have size "
                + (width + 1) + "x" + (height + 1) + " but it is " + cols
                + "x" + rows );
        }

        tRes.tested("collapseToMergedArea()", bResult) ;

        // restore original size
        oObj.collapseToSize(width, height);
    }

    /**
    * Test collapses cursor to the new size, checks size
    * of cursor range and restores original size of cursor range. <p>
    * Has <b> OK </b> status if after collapsing cursor
    * range extends by three in both dimensions. <p>
    */
    public void _collapseToSize(){
        boolean bResult = false;
        int width = 1, height = 1;

        // collapseToSize() method test
        oObj.collapseToSize (width + 3, height + 3);

        // checking results
        int cols = ((XColumnRowRange)
            UnoRuntime.queryInterface(
                XColumnRowRange.class, oObj) ).getColumns().getCount();
        int rows = ((XColumnRowRange)
            UnoRuntime.queryInterface(
                XColumnRowRange.class, oObj) ).getRows().getCount();

        if (cols == width + 3  && rows == height + 3) {
            bResult = true ;
        } else {
            bResult = false ;
            log.println( "After collapseToSize() call region must have size "
                + (width + 3) + "x" + (height + 3) + " but it is "
                + cols + "x" +rows);
        }

        tRes.tested("collapseToSize()", bResult) ;

        // restore original size
        oObj.collapseToSize(width, height) ;
    }

    /**
    * Test expands cursor to entire columns, checks size
    * of cursor range and restores original size of cursor range. <p>
    * Has <b> OK </b> status if after expanding cursor
    * range extends to all rows in the columns (number of rows is greater than
    * 32000 and number of columns remains the same). <p>
    */
    public void _expandToEntireColumns(){
        boolean bResult = false;
        int width = 1, height = 1 ;

        // expandToEntireColumns() method test
        oObj.expandToEntireColumns () ;

        // checking results
        int cols = ((XColumnRowRange)
            UnoRuntime.queryInterface(
                XColumnRowRange.class, oObj) ).getColumns().getCount();
        int rows = ((XColumnRowRange)
            UnoRuntime.queryInterface(
                XColumnRowRange.class, oObj) ).getRows().getCount();

        if (cols == width && rows >= 32000) {
            bResult = true ;
        } else {
            bResult = false ;
            log.println(
                "After expandToEntireColumns() call region must have size "+
                width + "x(>=32000) but it is " + cols + "x" + rows);
        }

        tRes.tested("expandToEntireColumns()", bResult) ;

        // restore original size
        oObj.collapseToSize(width, height) ;
    }

    /**
    * Test expands cursor to entire rows, checks size
    * of cursor range and restores original size of cursor range. <p>
    * Has <b> OK </b> status if after expanding cursor
    * range extends to all columns in the rows (number of columns is greater
    * than 256 and number of rows remains the same). <p>
    */
    public void _expandToEntireRows(){
        boolean bResult = false;
        int width = 1, height = 1 ;

        // expandToEntireRows() method test
        oObj.expandToEntireRows () ;

        // checking results
        int cols = ((XColumnRowRange)
            UnoRuntime.queryInterface(
                XColumnRowRange.class, oObj) ).getColumns().getCount();
        int rows = ((XColumnRowRange)
            UnoRuntime.queryInterface(
                XColumnRowRange.class, oObj) ).getRows().getCount();

        if (cols >= 256 && rows == height) {
            bResult = true;
        } else {
            bResult = false ;
            log.println("After expandToEntireRows() call region " +
                "must have size (>=256)x" + height + " but it is " +
                cols + "x" + rows );
        }

        tRes.tested("expandToEntireRows()", bResult) ;

        // restore original size
        oObj.collapseToSize(width, height) ;
    }

} // EOC _XSheetCellCursor