Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / sheet / _XSheetCellCursor.java
blob538f1fc40bf7f1917b613f9e01949b73aa75b95f
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc.sheet;
21 import lib.MultiMethodTest;
23 import com.sun.star.sheet.CellFlags;
24 import com.sun.star.sheet.XArrayFormulaRange;
25 import com.sun.star.sheet.XCellRangeAddressable;
26 import com.sun.star.sheet.XSheetCellCursor;
27 import com.sun.star.sheet.XSheetOperation;
28 import com.sun.star.sheet.XSpreadsheet;
29 import com.sun.star.table.CellRangeAddress;
30 import com.sun.star.table.XCellRange;
31 import com.sun.star.table.XColumnRowRange;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.util.XMergeable;
35 /**
36 * Testing <code>com.sun.star.sheet.XSheetCellCursor</code>
37 * interface methods :
38 * <ul>
39 * <li><code> collapseToCurrentRegion()</code></li>
40 * <li><code> collapseToCurrentArray()</code></li>
41 * <li><code> collapseToMergedArea()</code></li>
42 * <li><code> expandToEntireColumns()</code></li>
43 * <li><code> expandToEntireRows()</code></li>
44 * <li><code> collapseToSize()</code></li>
45 * </ul> <p>
46 * Component must also implement the following interfaces :
47 * <ul>
48 * <li> <code> com.sun.star.sheet.XCellRangeAddressable </code> :
49 * to get range address </li>
50 * <ul> <p>
51 * Range of cursor must be of size 4 x 4. <p>
52 * @see com.sun.star.sheet.XSheetCellCursor
54 public class _XSheetCellCursor extends MultiMethodTest {
56 public XSheetCellCursor oObj = null;
58 /**
59 * Test creates the array formula, assigns this array to another array,
60 * collapses cursor into one cell, applies method, checks the size of the
61 * result range, erases array formula, checks that array formula has been
62 * cleared. <p>
63 * Has <b>OK</b> status if no exceptions were thrown, if size of the result
64 * range is equal to size of the range where the array formula was set and
65 * if array formula was successfully cleared. <p>
67 public void _collapseToCurrentArray() {
68 boolean bResult = false;
70 XCellRangeAddressable crAddr = UnoRuntime.queryInterface(XCellRangeAddressable.class, oObj);
71 CellRangeAddress addr = crAddr.getRangeAddress() ;
72 int leftCol = addr.StartColumn ;
73 int topRow = addr.StartRow ;
74 int width = addr.EndColumn - addr.StartColumn + 1 ;
75 int height = addr.EndRow - addr.StartRow + 1 ;
77 log.println( "Object area is ((" + leftCol + "," + topRow + "),(" +
78 (leftCol + width - 1) + "," + (topRow + height - 1) + ")" );
80 XCellRange new_range = null;
81 try {
82 // first we need to create an array formula
83 new_range =
84 oObj.getCellRangeByPosition(0, 0, 0, height - 1);
85 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
86 log.print("Get cell range by position failed: ");
87 e.printStackTrace(log);
88 tRes.tested("collapseToCurrentArray()", false);
91 log.println("DB: Successfully new range created");
92 XArrayFormulaRange arrFormulaRange = UnoRuntime.queryInterface (XArrayFormulaRange.class, new_range);
93 // write a simple formula (this array assigns another array)
94 arrFormulaRange.setArrayFormula("A1:A" + height) ;
96 // collapse cursor into one cell and then try to apply the method
97 oObj.collapseToSize (1, 1) ;
98 oObj.collapseToCurrentArray() ;
100 // check the size of result range
101 int cols = UnoRuntime.queryInterface(
102 XColumnRowRange.class, oObj).getColumns().getCount();
103 int rows = UnoRuntime.queryInterface(
104 XColumnRowRange.class, oObj).getRows().getCount();
106 if (cols == 1 && rows == height) {
107 bResult = true;
108 } else {
109 bResult = false;
110 log.println("The size of cell range must be 1x" + height +
111 ", but after method call it was " + cols + "x" + rows);
114 // erase array formula
115 arrFormulaRange.setArrayFormula("");
117 // check if array formula has been cleared with last statement
118 try {
119 // if array formula isn't cleared exception is thrown
120 new_range.getCellByPosition(0,0).setValue(111) ;
121 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
122 bResult = false ;
123 log.println(
124 "Array formula hasn't been cleared with setArrayFormula(\"\")");
125 XSheetOperation clearRange = UnoRuntime.queryInterface (XSheetOperation.class, new_range);
126 int allFlags =
127 CellFlags.ANNOTATION | CellFlags.DATETIME | CellFlags.EDITATTR;
128 allFlags = allFlags
129 | CellFlags.HARDATTR | CellFlags.OBJECTS | CellFlags.STRING;
130 allFlags = allFlags
131 | CellFlags.VALUE | CellFlags.FORMULA | CellFlags.STYLES;
132 clearRange.clearContents(allFlags) ;
135 tRes.tested("collapseToCurrentArray()", bResult );
139 * Test clears contents of spreadsheet, collapses cursor to current range,
140 * checks size of cursor range, fills a cell that is close to
141 * cursor range, collapses cursor to current range, checks size of cursor
142 * range again and restores original size. <p>
143 * Has <b> OK </b> status if after clearing of content and collapsing cursor
144 * range size remains 4 x 4, if after filling of cell and collapsing cursor
145 * range extends by one in both dimensions and no exceptions were thrown.<p>
147 public void _collapseToCurrentRegion(){
148 boolean bResult = true;
149 int width = 4, height = 4;
150 int leftCol = -1, topRow = -1;
152 XSpreadsheet oSheet = oObj.getSpreadsheet();
153 UnoRuntime.queryInterface(
154 XSheetOperation.class, oSheet).clearContents(65535);
155 oObj.collapseToCurrentRegion();
156 int cols = UnoRuntime.queryInterface(
157 XColumnRowRange.class, oObj).getColumns().getCount();
158 int rows = UnoRuntime.queryInterface(
159 XColumnRowRange.class, oObj).getRows().getCount();
161 if (cols != width || rows != height) {
162 bResult = false ;
163 log.println("After collapseToCurrentRegion()"
164 + " call Region must have size " + width + "x" + height
165 + " but it is " + cols + "x" + rows);
168 // if previous test was successful try more complicated case
169 if (bResult) {
170 if (leftCol != -1 && topRow != -1) {
171 try {
172 oSheet.getCellByPosition(
173 leftCol + width, topRow + height).setValue(1);
174 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
175 log.print("Can't get cell by position:");
176 e.printStackTrace(log);
177 bResult = false;
180 oObj.collapseToCurrentRegion() ;
182 // checking results
183 cols = UnoRuntime.queryInterface(
184 XColumnRowRange.class, oObj).getColumns().getCount();
185 rows = UnoRuntime.queryInterface(
186 XColumnRowRange.class, oObj).getRows().getCount();
188 if (cols == width + 1 && rows == height + 1) {
189 bResult &= true;
190 } else {
191 bResult = false;
192 log.println("After collapseToCurrentRegion() call [2]"
193 + " region must have size " + (width+1) + "x"
194 + (height + 1) + " but it is " + cols + "x" + rows );
199 tRes.tested("collapseToCurrentRegion()", bResult);
201 // restore original size
202 oObj.collapseToSize(width, height);
206 * Test merges a cells of range that has a greater size, collapses cursor to
207 * merged area, checks size of cursor range and restores original size
208 * of cursor range. <p>
209 * Has <b> OK </b> status if after merging of cells and collapsing cursor
210 * range extends by one in both dimensions and no exceptions were thrown.<p>
212 public void _collapseToMergedArea(){
213 int width = 1, height = 1 ;
214 int leftCol = 0, topRow = 0 ;
216 boolean bResult = true ;
218 log.println("DB: Starting collapseToMergedArea() method test ...") ;
219 XSpreadsheet oSheet = oObj.getSpreadsheet() ;
220 log.println ("DB: got Spreadsheet.") ;
222 XCellRange newRange = null;
223 try {
224 newRange = oSheet.getCellRangeByPosition (
225 leftCol + width - 1, topRow + height - 1,
226 leftCol + width, topRow + height );
227 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
228 log.println("Can't get cell range by position");
229 e.printStackTrace(log);
230 bResult = false;
233 XMergeable mergeRange = UnoRuntime.queryInterface (XMergeable.class, newRange);
234 if (mergeRange == null) {
235 log.println("DB: newRange doesn't implement XMergeable interface");
236 } else {
237 log.println("DB: XMergeable interface successfully queried.");
240 mergeRange.merge(true);
241 log.println("DB: Successfully merged.") ;
243 oObj.collapseToMergedArea() ;
244 log.println("DB: Successfully collapseToMergedArea() method called");
246 // unmerge area to restore SpreadSheet
247 mergeRange.merge(false);
248 log.println("DB: Successfully unmerged.") ;
250 // checking results
251 int cols = UnoRuntime.queryInterface(
252 XColumnRowRange.class, oObj).getColumns().getCount();
253 int rows = UnoRuntime.queryInterface(
254 XColumnRowRange.class, oObj).getRows().getCount();
255 log.println("DB: Column and row numbers successfully get") ;
257 if (cols == width + 1 && rows == height + 3) {
258 bResult &= true;
259 } else {
260 bResult = false;
261 log.println(
262 "After collapseToMergedArea() call region must have size "
263 + (width + 1) + "x" + (height + 1) + " but it is " + cols
264 + "x" + rows );
267 tRes.tested("collapseToMergedArea()", bResult) ;
269 // restore original size
270 oObj.collapseToSize(width, height);
274 * Test collapses cursor to the new size, checks size
275 * of cursor range and restores original size of cursor range. <p>
276 * Has <b> OK </b> status if after collapsing cursor
277 * range extends by three in both dimensions. <p>
279 public void _collapseToSize(){
280 boolean bResult = false;
281 int width = 1, height = 1;
283 // collapseToSize() method test
284 oObj.collapseToSize (width + 3, height + 3);
286 // checking results
287 int cols = UnoRuntime.queryInterface(
288 XColumnRowRange.class, oObj).getColumns().getCount();
289 int rows = UnoRuntime.queryInterface(
290 XColumnRowRange.class, oObj).getRows().getCount();
292 if (cols == width + 3 && rows == height + 3) {
293 bResult = true ;
294 } else {
295 bResult = false ;
296 log.println( "After collapseToSize() call region must have size "
297 + (width + 3) + "x" + (height + 3) + " but it is "
298 + cols + "x" +rows);
301 tRes.tested("collapseToSize()", bResult) ;
303 // restore original size
304 oObj.collapseToSize(width, height) ;
308 * Test expands cursor to entire columns, checks size
309 * of cursor range and restores original size of cursor range. <p>
310 * Has <b> OK </b> status if after expanding cursor
311 * range extends to all rows in the columns (number of rows is greater than
312 * 32000 and number of columns remains the same). <p>
314 public void _expandToEntireColumns(){
315 boolean bResult = false;
316 int width = 1, height = 1 ;
318 // expandToEntireColumns() method test
319 oObj.expandToEntireColumns () ;
321 // checking results
322 int cols = UnoRuntime.queryInterface(
323 XColumnRowRange.class, oObj).getColumns().getCount();
324 int rows = UnoRuntime.queryInterface(
325 XColumnRowRange.class, oObj).getRows().getCount();
327 if (cols == width && rows >= 32000) {
328 bResult = true ;
329 } else {
330 bResult = false ;
331 log.println(
332 "After expandToEntireColumns() call region must have size "+
333 width + "x(>=32000) but it is " + cols + "x" + rows);
336 tRes.tested("expandToEntireColumns()", bResult) ;
338 // restore original size
339 oObj.collapseToSize(width, height) ;
343 * Test expands cursor to entire rows, checks size
344 * of cursor range and restores original size of cursor range. <p>
345 * Has <b> OK </b> status if after expanding cursor
346 * range extends to all columns in the rows (number of columns is greater
347 * than 256 and number of rows remains the same). <p>
349 public void _expandToEntireRows(){
350 boolean bResult = false;
351 int width = 1, height = 1 ;
353 // expandToEntireRows() method test
354 oObj.expandToEntireRows () ;
356 // checking results
357 int cols = UnoRuntime.queryInterface(
358 XColumnRowRange.class, oObj).getColumns().getCount();
359 int rows = UnoRuntime.queryInterface(
360 XColumnRowRange.class, oObj).getRows().getCount();
362 if (cols >= 256 && rows == height) {
363 bResult = true;
364 } else {
365 bResult = false ;
366 log.println("After expandToEntireRows() call region " +
367 "must have size (>=256)x" + height + " but it is " +
368 cols + "x" + rows );
371 tRes.tested("expandToEntireRows()", bResult) ;
373 // restore original size
374 oObj.collapseToSize(width, height) ;
377 } // EOC _XSheetCellCursor