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 .
21 import lib
.MultiMethodTest
;
23 import lib
.StatusException
;
25 import com
.sun
.star
.table
.XCell
;
26 import com
.sun
.star
.table
.XCellRange
;
27 import com
.sun
.star
.table
.XTableColumns
;
28 import com
.sun
.star
.text
.XSimpleText
;
29 import com
.sun
.star
.uno
.UnoRuntime
;
32 * Testing <code>com.sun.star.table.XTableColumns</code>
35 * <li><code> insertByIndex()</code></li>
36 * <li><code> removeByIndex()</code></li>
39 * This test needs the following object relations :
41 * <li> <code>'XTableColumns.XCellRange'</code> : <code>
42 * com.sun.star.table.XCellRange</code> the cell range of
46 * Test is multithread compilant. <p>
47 * @see com.sun.star.table.XTableColumns
49 public class _XTableColumns
extends MultiMethodTest
{
51 public XTableColumns oObj
= null;
52 private XCellRange xCellRange
= null;
53 private int lastColumn
= 0;
55 public void before() {
56 xCellRange
= (XCellRange
)
57 tEnv
.getObjRelation("XTableColumns.XCellRange") ;
59 if (xCellRange
== null) throw new
60 StatusException(Status
.failed("Relation missing"));
62 lastColumn
= oObj
.getCount() - 1 ;
66 * First a number of cells in cell range are filled with data.
68 * Then columns inserted to valid positions : 1 column at 1,
69 * 1 column at 0, 2 columns at 0. <p>
71 * Then columns inserted to invalid positions : position -1,
72 * the column after last, and 0 columns inserted. <p>
74 * Has <b> OK </b> status if for valid cases :
76 * <li> content of other cells are properly shifted </li>
77 * <li> inserted columns are empty </li>
78 * <li> number of columns increases (in case if it is not the whole
79 * spreadsheet) by proper number. </li>
81 * and for invalid cases exception is thrown.
83 public void _insertByIndex() {
85 boolean result
= true;
86 int origCnt
= oObj
.getCount();
89 log
.println("Filling range ... ");
90 fillRange(xCellRange
);
92 log
.println("Inserting 1 column at position 1 ...");
93 oObj
.insertByIndex(1,1);
95 result
&= checkColumn(0, 0);
96 result
&= checkColumnEmpty(1);
97 result
&= checkColumn(2, 1);
98 result
&= checkColumn(3, 2);
99 result
&= checkColumnEmpty(4);
101 if (lastColumn
< 200) {
102 result
&= checkColumn(lastColumn
+ 1, lastColumn
);
103 result
&= oObj
.getCount() == origCnt
+ 1;
105 result
&= checkColumnEmpty(lastColumn
);
108 log
.println("Inserting 1 column at position 0 ...");
109 oObj
.insertByIndex(0,1);
111 result
&= checkColumnEmpty(0);
112 result
&= checkColumn(1, 0);
113 result
&= checkColumnEmpty(2);
114 result
&= checkColumn(3, 1);
115 result
&= checkColumn(4, 2);
116 result
&= checkColumnEmpty(5);
117 if (lastColumn
< 200) {
118 result
&= checkColumn(lastColumn
+ 2, lastColumn
);
119 result
&= oObj
.getCount() == origCnt
+ 2;
122 log
.println("Inserting 2 columns at position 0 ...");
123 oObj
.insertByIndex(0,2);
125 result
&= checkColumnEmpty(0);
126 result
&= checkColumnEmpty(1);
127 result
&= checkColumnEmpty(2);
128 result
&= checkColumn(3, 0);
129 result
&= checkColumnEmpty(4);
130 result
&= checkColumn(5, 1);
131 result
&= checkColumn(6, 2);
132 result
&= checkColumnEmpty(7);
133 if (lastColumn
< 200) {
134 result
&= checkColumn(lastColumn
+ 4, lastColumn
);
137 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
138 e
.printStackTrace(log
);
143 // spreadsheet supports 256 columns and after inserting
144 // or removing a column their number remains the same
146 log
.println("Checking that number of column increased.");
147 result
&= oObj
.getCount() == origCnt
+ 4;
148 log
.println("Before: " + origCnt
+ ", After: " + oObj
.getCount());
150 log
.println("Number of columns is " + origCnt
+ ",") ;
151 log
.println("supposing that this is the whole spreadsheet and ");
152 log
.println("number of columns should not change.");
156 oObj
.insertByIndex(-1,1);
157 log
.println("No Exception occurred while inserting column at -1");
159 } catch (Exception e
) {
160 log
.println("Inserting column at Index -1 ... OK");
164 int cnt
= oObj
.getCount();
166 oObj
.insertByIndex(cnt
, 1);
167 log
.println("No Exception occurred while inserting column at "
170 } catch (Exception e
) {
171 log
.println("Inserting column at Index " + cnt
+ " ... OK");
175 if (tEnv
.getTestCase().getObjectName().equals("ScTableColumnsObj")) {
178 oObj
.insertByIndex(0,0);
179 log
.println("No Exception occurred while inserting 0 columns");
181 } catch (Exception e
) {
182 log
.println("Inserting 0 columns ... OK");
188 tRes
.tested( "insertByIndex()", result
);
190 } // end insertByIndex()
193 * Columns removed from valid positions : 1 column at 1,
194 * 1 column at 0, 2 columns at 0. <p>
196 * Then columns removed from invalid positions : position -1,
197 * the column after last, and 0 columns removed. <p>
199 * Has <b> OK </b> status if for valid cases :
201 * <li> content of other cells are properly shifted </li>
202 * <li> columns which are shifted left are empty </li>
203 * <li> number of columns decreases (in case if it is not the whole
204 * spreadsheet) by proper number. </li>
206 * and for invalid cases exception is thrown.
208 public void _removeByIndex() {
209 executeMethod("insertByIndex()");
211 boolean result
= true;
212 int origCnt
= oObj
.getCount();
215 log
.println("Filling range ... ");
217 log
.println("Removing 2 columns at position 0 ...");
218 oObj
.removeByIndex(0,2);
220 result
&= checkColumnEmpty(0);
221 result
&= checkColumn(1, 0);
222 result
&= checkColumnEmpty(2);
223 result
&= checkColumn(3, 1);
224 result
&= checkColumn(4, 2);
225 result
&= checkColumnEmpty(5);
226 if (lastColumn
< 200) {
227 result
&= checkColumn(lastColumn
+ 2, lastColumn
);
228 result
&= oObj
.getCount() == origCnt
- 2;
231 log
.println("Removing 1 column at position 0 ...");
232 oObj
.removeByIndex(0,1);
234 result
&= checkColumn(0, 0);
235 result
&= checkColumnEmpty(1);
236 result
&= checkColumn(2, 1);
237 result
&= checkColumn(3, 2);
238 result
&= checkColumnEmpty(4);
239 if (lastColumn
< 200) {
240 result
&= checkColumn(lastColumn
+ 1, lastColumn
);
241 result
&= oObj
.getCount() == origCnt
- 3;
244 log
.println("Removing 1 column at position 1 ...");
245 oObj
.removeByIndex(1,1);
247 result
&= checkColumn(0, 0);
248 result
&= checkColumn(1, 1);
249 result
&= checkColumn(2, 2);
250 result
&= checkColumnEmpty(3);
251 if (lastColumn
< 200) {
252 result
&= checkColumn(lastColumn
, lastColumn
);
255 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
256 e
.printStackTrace(log
);
261 // spreadsheet supports 256 columns and after inserting
262 // or removing a column their number remains the same
264 log
.println("Checking that number of column increased.");
265 result
&= oObj
.getCount() == origCnt
- 4;
266 log
.println("Before: " + origCnt
+ ", After: " + oObj
.getCount());
268 log
.println("Number of columns is " + origCnt
+ ",") ;
269 log
.println("supposing that this is the whole spreadsheet and ");
270 log
.println("number of columns should not change.");
274 oObj
.removeByIndex(-1,1);
275 log
.println("No Exception occurred while removing column at -1");
277 } catch (Exception e
) {
278 log
.println("removing column at Index -1 ... OK");
282 int cnt
= oObj
.getCount();
284 oObj
.removeByIndex(cnt
, 1);
285 log
.println("No Exception occurred while removing column at "
288 } catch (Exception e
) {
289 log
.println("Removing column at Index " + cnt
+ " ... OK");
293 if (tEnv
.getTestCase().getObjectName().equals("ScTableColumnsObj")) {
295 oObj
.removeByIndex(0,0);
296 log
.println("No Exception occurred while removing 0 columns");
298 } catch (Exception e
) {
299 log
.println("removing 0 columns ... OK");
304 tRes
.tested( "removeByIndex()", result
);
305 } // end removeByIndex()
307 private void setCellText(XCell cell
, String text
) {
308 XSimpleText xText
= UnoRuntime
.queryInterface
309 (XSimpleText
.class, cell
) ;
310 xText
.setString(text
);
312 private String
getCellText(XCell cell
) {
313 XSimpleText xText
= UnoRuntime
.queryInterface
314 (XSimpleText
.class, cell
) ;
315 return xText
.getString();
319 * Fills the range with some data : two rows and 3 columns, and
320 * some columns are cleared.
322 * @param xRange Range to fill
323 * @throws IndexOutOfBoundsException if any errors occur during filling.
325 private void fillRange(XCellRange xRange
)
326 throws com
.sun
.star
.lang
.IndexOutOfBoundsException
{
328 for (int i
= 0; i
<= lastColumn
&& i
< 3; i
++) {
329 setCellText(xRange
.getCellByPosition(i
, 0), "" + i
+ "a");
330 setCellText(xRange
.getCellByPosition(i
, 1), "" + i
+ "b");
333 for (int i
= 3; i
<= lastColumn
&& i
< 10; i
++) {
334 setCellText(xRange
.getCellByPosition(i
, 0), "");
335 setCellText(xRange
.getCellByPosition(i
, 1), "");
340 * Check the column (first two rows) if it has values with
343 * @param col Column to check
344 * @param idx What indexes must be in cells
345 * @return <code>true</code> if expected indexes are found,
346 * <code>false</code> otherwise.
347 * @throws IndexOutOfBoundsException
349 private boolean checkColumn(int col
, int idx
)
350 throws com
.sun
.star
.lang
.IndexOutOfBoundsException
{
352 if (col
>= oObj
.getCount()) return true;
354 String c1
= getCellText(xCellRange
.getCellByPosition(col
, 0));
355 String c2
= getCellText(xCellRange
.getCellByPosition(col
, 1));
357 if (!((""+ idx
+ "a").equals(c1
) && (""+ idx
+ "b").equals(c2
))) {
359 log
.println("FAILED for column " + col
+ " and index " + idx
+ "("
360 + c1
+ "," + c2
+ ")");
367 * Checks if the column (first two rows) has no data in its cells.
369 * @param col Column to check
370 * @return <code>true</code> if the column is empty, <code>false</code>
371 * if first two cells contains some strings.
372 * @throws IndexOutOfBoundsException
374 private boolean checkColumnEmpty(int col
)
375 throws com
.sun
.star
.lang
.IndexOutOfBoundsException
{
377 if (col
>= oObj
.getCount()) return true;
379 String c1
= getCellText(xCellRange
.getCellByPosition(col
, 0));
380 String c2
= getCellText(xCellRange
.getCellByPosition(col
, 1));
381 if (!("".equals(c1
) && "".equals(c2
))) {
382 log
.println("FAILED for column " + col
+ " is not empty ("
383 + c1
+ "," + c2
+ ")");
389 } //finish class _XTableColumns